[ONOS-5833] (vCore) Refactor vnet services
to enable event delivery mechanism

Changes
1. Abstact Vnet Listener Manager is added
2. Refactor Vnet services to use it

Change-Id: I178342bfc882c0739f216960358a281903e1385a
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/AbstractVnetService.java b/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/AbstractVnetService.java
new file mode 100644
index 0000000..2eac0af
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/AbstractVnetService.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.incubator.net.virtual;
+
+import org.onlab.osgi.DefaultServiceDirectory;
+import org.onlab.osgi.ServiceDirectory;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Basis for virtual network service.
+ */
+public abstract class AbstractVnetService
+        implements VnetService {
+
+    private static final String NETWORK_NULL = "Network ID cannot be null";
+
+    protected NetworkId networkId;
+    protected VirtualNetworkService manager;
+    protected ServiceDirectory serviceDirectory;
+
+    public AbstractVnetService(VirtualNetworkService manager,
+                               NetworkId networkId) {
+        checkNotNull(networkId, NETWORK_NULL);
+        this.manager = manager;
+        this.networkId = networkId;
+        this.serviceDirectory = new DefaultServiceDirectory();
+    }
+
+    @Override
+    public NetworkId networkId() {
+        return this.networkId;
+    }
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VirtualNetworkAdminService.java b/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VirtualNetworkAdminService.java
index a7fbe13..fd318ce 100644
--- a/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VirtualNetworkAdminService.java
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VirtualNetworkAdminService.java
@@ -55,7 +55,6 @@
      */
     Set<TenantId> getTenantIds();
 
-
     /**
      * Creates a new virtual network for the specified tenant.
      *
@@ -71,7 +70,6 @@
      */
     void removeVirtualNetwork(NetworkId networkId);
 
-
     /**
      * Creates a new virtual device within the specified network. The device id
      * must be unique within the bounds of the network.
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VirtualNetworkService.java b/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VirtualNetworkService.java
index 530c67a..2e852e8 100644
--- a/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VirtualNetworkService.java
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VirtualNetworkService.java
@@ -16,6 +16,7 @@
 package org.onosproject.incubator.net.virtual;
 
 import com.google.common.annotations.Beta;
+import org.onlab.osgi.ServiceDirectory;
 import org.onosproject.net.DeviceId;
 
 import java.util.Set;
@@ -105,4 +106,10 @@
      */
     <T> T get(NetworkId networkId, Class<T> serviceClass);
 
+    /**
+     * Returns service directory.
+     *
+     * @return a service directory
+     */
+    ServiceDirectory getServiceDirectory();
 }
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VnetService.java b/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VnetService.java
index f4b1bd8..3da437d 100644
--- a/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VnetService.java
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/VnetService.java
@@ -20,5 +20,5 @@
  * Virtual network service interface.
  */
 public interface VnetService {
-    VirtualNetwork network();
+    NetworkId networkId();
 }
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/event/AbstractVirtualListenerManager.java b/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/event/AbstractVirtualListenerManager.java
index fe6b2f9..e87876c 100644
--- a/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/event/AbstractVirtualListenerManager.java
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/virtual/event/AbstractVirtualListenerManager.java
@@ -15,28 +15,41 @@
  */
 package org.onosproject.incubator.net.virtual.event;
 
+import org.onlab.osgi.ServiceDirectory;
 import org.onosproject.event.Event;
 import org.onosproject.event.EventDeliveryService;
 import org.onosproject.event.EventListener;
 import org.onosproject.event.ListenerService;
 import org.onosproject.incubator.net.virtual.NetworkId;
+import org.onosproject.incubator.net.virtual.VirtualNetworkService;
+import org.onosproject.incubator.net.virtual.VnetService;
 
 /**
  * Basis for virtual event components which need to export listener mechanism.
  */
 public abstract class AbstractVirtualListenerManager
         <E extends Event, L extends EventListener<E>>
-    implements ListenerService<E, L> {
+    implements ListenerService<E, L>, VnetService {
+
+    private static final String NETWORK_NULL = "Network ID cannot be null";
 
     protected final NetworkId networkId;
+    protected final VirtualNetworkService manager;
+    protected final ServiceDirectory serviceDirectory;
 
     protected EventDeliveryService eventDispatcher;
 
     VirtualListenerRegistryManager listenerManager =
             VirtualListenerRegistryManager.getInstance();
 
-    public AbstractVirtualListenerManager(NetworkId networkId) {
+    public AbstractVirtualListenerManager(VirtualNetworkService manager,
+                                          NetworkId networkId) {
+        this.manager = manager;
         this.networkId = networkId;
+        this.serviceDirectory = manager.getServiceDirectory();
+
+        //Set default event delivery service by default
+        this.eventDispatcher = serviceDirectory.get(EventDeliveryService.class);
     }
 
     @Override
@@ -66,13 +79,18 @@
         }
     }
 
+    @Override
+    public NetworkId networkId() {
+        return this.networkId;
+    }
+
     /**
      * Returns the class type of parameter type.
      * More specifically, it returns the class type of event class.
      *
      * @return the class type of provider service of the service
      */
-    private Class getEventClass() {
+    public Class getEventClass() {
         String className = this.getClass().getGenericSuperclass().toString();
         String pramType = className.split("<")[1].split(",")[0];
 
@@ -84,5 +102,4 @@
 
         return null;
     }
-
 }
diff --git a/incubator/api/src/test/java/org/onosproject/incubator/net/virtual/event/AbstractVirtualListenerManagerTest.java b/incubator/api/src/test/java/org/onosproject/incubator/net/virtual/event/AbstractVirtualListenerManagerTest.java
index 87b3fc0..33fce56 100644
--- a/incubator/api/src/test/java/org/onosproject/incubator/net/virtual/event/AbstractVirtualListenerManagerTest.java
+++ b/incubator/api/src/test/java/org/onosproject/incubator/net/virtual/event/AbstractVirtualListenerManagerTest.java
@@ -16,15 +16,26 @@
 
 package org.onosproject.incubator.net.virtual.event;
 
+import com.google.common.collect.ClassToInstanceMap;
+import com.google.common.collect.MutableClassToInstanceMap;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
+import org.onlab.osgi.ServiceDirectory;
 import org.onosproject.event.AbstractEvent;
 import org.onosproject.event.Event;
 import org.onosproject.event.EventDeliveryService;
 import org.onosproject.event.EventListener;
 import org.onosproject.event.EventSink;
 import org.onosproject.incubator.net.virtual.NetworkId;
+import org.onosproject.incubator.net.virtual.TenantId;
+import org.onosproject.incubator.net.virtual.VirtualDevice;
+import org.onosproject.incubator.net.virtual.VirtualHost;
+import org.onosproject.incubator.net.virtual.VirtualLink;
+import org.onosproject.incubator.net.virtual.VirtualNetwork;
+import org.onosproject.incubator.net.virtual.VirtualNetworkService;
+import org.onosproject.incubator.net.virtual.VirtualPort;
+import org.onosproject.net.DeviceId;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -39,6 +50,8 @@
  */
 public class AbstractVirtualListenerManagerTest {
 
+    private VirtualNetworkService manager;
+
     TestEventDispatcher dispatcher = new TestEventDispatcher();
     VirtualListenerRegistryManager listenerRegistryManager =
             VirtualListenerRegistryManager.getInstance();
@@ -54,21 +67,20 @@
 
     @Before
     public void setUp() {
+        manager = new TestVirtualNetworkManager();
+
         dispatcher.addSink(VirtualEvent.class, listenerRegistryManager);
 
         prickleListener = new PrickleListener();
-        prickleManager = new PrickleManager(NetworkId.networkId(1));
-        prickleManager.eventDispatcher = dispatcher;
+        prickleManager = new PrickleManager(manager, NetworkId.networkId(1));
         prickleManager.addListener(prickleListener);
 
         gooListener = new GooListener();
-        gooManager = new GooManager(NetworkId.networkId(1));
-        gooManager.eventDispatcher = dispatcher;
+        gooManager = new GooManager(manager, NetworkId.networkId(1));
         gooManager.addListener(gooListener);
 
         barListener = new BarListener();
-        barManager = new BarManager(NetworkId.networkId(2));
-        barManager.eventDispatcher = dispatcher;
+        barManager = new BarManager(manager, NetworkId.networkId(2));
         barManager.addListener(barListener);
     }
 
@@ -168,7 +180,6 @@
 
         @Override
         public void event(E event) {
-            System.out.println(this.getClass().toString());
             events.add(event);
             latch.countDown();
         }
@@ -184,20 +195,20 @@
     }
 
     private class PrickleManager extends AbstractVirtualListenerManager<Prickle, PrickleListener> {
-        public PrickleManager(NetworkId networkId) {
-            super(networkId);
+        public PrickleManager(VirtualNetworkService service, NetworkId networkId) {
+            super(service, networkId);
         }
     }
 
     private class GooManager extends AbstractVirtualListenerManager<Goo, GooListener> {
-        public GooManager(NetworkId networkId) {
-            super(networkId);
+        public GooManager(VirtualNetworkService service, NetworkId networkId) {
+            super(service, networkId);
         }
     }
 
     private class BarManager extends AbstractVirtualListenerManager<Bar, BarListener> {
-        public BarManager(NetworkId networkId) {
-            super(networkId);
+        public BarManager(VirtualNetworkService service, NetworkId networkId) {
+            super(service, networkId);
         }
     }
 
@@ -242,4 +253,70 @@
             }
         }
     }
+
+    private class TestVirtualNetworkManager implements VirtualNetworkService {
+        TestServiceDirectory serviceDirectory = new TestServiceDirectory();
+
+        public TestVirtualNetworkManager() {
+            serviceDirectory.add(EventDeliveryService.class, dispatcher);
+        }
+
+        @Override
+        public Set<VirtualNetwork> getVirtualNetworks(TenantId tenantId) {
+            return null;
+        }
+
+        @Override
+        public Set<VirtualDevice> getVirtualDevices(NetworkId networkId) {
+            return null;
+        }
+
+        @Override
+        public Set<VirtualHost> getVirtualHosts(NetworkId networkId) {
+            return null;
+        }
+
+        @Override
+        public Set<VirtualLink> getVirtualLinks(NetworkId networkId) {
+            return null;
+        }
+
+        @Override
+        public Set<VirtualPort> getVirtualPorts(NetworkId networkId, DeviceId deviceId) {
+            return null;
+        }
+
+        @Override
+        public <T> T get(NetworkId networkId, Class<T> serviceClass) {
+            return null;
+        }
+
+        @Override
+        public ServiceDirectory getServiceDirectory() {
+            return serviceDirectory;
+        }
+    }
+
+    private  class TestServiceDirectory implements ServiceDirectory {
+
+        private ClassToInstanceMap<Object> services = MutableClassToInstanceMap.create();
+
+        @Override
+        public <T> T get(Class<T> serviceClass) {
+            return services.getInstance(serviceClass);
+        }
+
+        /**
+         * Adds a new service to the directory.
+         *
+         * @param serviceClass service class
+         * @param service service instance
+         * @return self
+         */
+        public TestServiceDirectory add(Class serviceClass, Object service) {
+            services.putInstance(serviceClass, service);
+            return this;
+        }
+
+    }
 }
\ No newline at end of file