Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Start by creating two files in the byon-cli package " 'RemoveHostCommand.java" and "RemoveNetworkCommand.java". These CLI commands are '. This CLI command is simple and very similar to the add host CLI commandscommand, so do this now as an exercise. When you have written these commandsthis command, you will need to add the following XML to the 'shell-config.xml' under resources.

Code Block
<command>
	<action class="org.onos.byon.cli.RemoveHostCommand"/>
    <completers>
		<ref component-id="networkCompleter"/>
        <ref component-id="hostIdCompleter"/>
        <null/>
	</completers>
</command>
<command>
	<action class="org.onos.byon.cli.RemoveNetworkCommand"/>
	<completers>
		<ref component-id="networkCompleter"/>
        <null/>
    </completers>
</command>


 Once again if you recompile your code and use onos-app update your application on the ONOS instances. You should now be able to add and remove networks as well as hosts. Try it out!

...

Components in ONOS can use events to asynchronously notify other components when their state has changed. We will demonstrate how this can be done by creating a new event type, NetworkEvent, to notify listeners when a virtual network has been updated. These events will be fired by the distributed store and forwarded by the manager to listeners in the peer ONOS instance. 

Let's start by adding a inspecting the NetworkEvent class:

Code Block
languagejava
public class NetworkEvent extends AbstractEvent<NetworkEvent.Type, String> {

    enum Type {
        NETWORK_ADDED,
        NETWORK_REMOVED,
        NETWORK_UPDATED
    }

    public NetworkEvent(Type type, String subject) {
        super(type, subject);
    }

}

We are also going to need a couple interfaces that will be needed by the listeners and the and the delegates. The listeners are components which have registered to obtain events from this service, these are usually local. A delegate is a manager which is receiving events from a neighbouring store for the purpose of either taking action on the store event or notifying listeners. So we are going to add NetworkListener.Let's also inspect the provided abstraction for listeners capable of receiving network events, which is the NetworkListener class:

Code Block
languagejava
public interface NetworkListener extends EventListener<NetworkEvent> {}

With ONOS being a clustered system, it needs to allow managers to learn about events that may have occurred on other cluster nodes. For this reason, in every ONOS subsystem the events are generated within the distributed stores and then are passed to the respective manager components via StoreDelegate interface. Upon receiving the event via the delegate interface, the manager can then take local action and then disseminate the event to its event listeners.

Let's inspect the provided NetworkStoreDelegate interface: and a NetworkStoreDelegate interfaces

Code Block
languagejava
public interface NetworkStoreDelegate extends StoreDelegate<NetworkEvent> {}

Of course now if we want To enable the store to be delegate-capable /manager delegate relationship, we need to make it the existing store interface extend the abstract Store interface as shown below.

Code Block
languagejava
public interface NetworkStore extends Store<NetworkEvent, NetworkStoreDelegate> {

And since we changed the interface of the Store we will have to add something to the implementation, namely DistributedNetworkStore. This abstract class is used to define the methods for posting event to its delegates.

Code Block
public class DistributedNetworkStore extends AbstractStore<NetworkEvent, NetworkStoreDelegate>

We need to add the following methods to the NetworkService in order to allow components to add and remove listeners.

It basically states that the NetworkStore is capable of having a NetworkStoreDelegate to which it will send NetworkEvent notifications:

Code Block
languagejava
public interface NetworkStore extends Store<NetworkEvent, NetworkStoreDelegate> {

And since we changed the interface of the Store we will have to make sure that the implementation, namely DistributedNetworkStore, properly adheres the revised contract. The AbstractStore base class provides methods for setting delegate and for posting events to that delegate.

Code Block
public class DistributedNetworkStore extends AbstractStore<NetworkEvent, NetworkStoreDelegate>
Code Block
languagejava
    /**
     * Register a listener for network events.
     *
     * @param listener listener
     */
    void addListener(NetworkListener listener);

    /**
     * Unregister a listener for network events.
     *
     * @param listener listener
     */
    void removeListener(NetworkListener listener);

The ConsistentMap will already generate notifications when updates are made locally and remotely, so we can mostly leverage that feature to regenerate our events.

To do this, we will need to create a MapEventListener<StringMapEventListener<String, Set<HostId>> class in the DistributedNetworkStore.

Code Block
private class InternalListener implements MapEventListener<String, Set<HostId>> {
    @Override
    public void event(MapEvent<String, Set<HostId>> mapEvent) {
        final NetworkEvent.Type type;
        switch (mapEvent.type()) {
            case INSERT:
                type = NETWORK_ADDED;
                break;
            case UPDATE:
                type = NETWORK_UPDATED;
                break;
            case REMOVE:
            default:
                type = NETWORK_REMOVED;
                break;
        }
        notifyDelegate(new NetworkEvent(type, mapEvent.key()));
    }
}

...