ONOS-2184 - Implemented Virtual Network DeviceService and LinkService to use
VirtualNetwork service.

Change-Id: I695af440bc2fc5d688f8b9cf5201375bacd02e8a
diff --git a/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkDeviceService.java b/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkDeviceService.java
new file mode 100644
index 0000000..5dfdd57
--- /dev/null
+++ b/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkDeviceService.java
@@ -0,0 +1,160 @@
+/*
+ * Copyright 2016-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.impl;
+
+import com.google.common.collect.ImmutableList;
+import org.onosproject.event.AbstractListenerManager;
+import org.onosproject.incubator.net.virtual.VirtualDevice;
+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.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.MastershipRole;
+import org.onosproject.net.Port;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.device.DeviceEvent;
+import org.onosproject.net.device.DeviceListener;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.device.PortStatistics;
+
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Device service implementation built on the virtual network service.
+ */
+public class VirtualNetworkDeviceService extends AbstractListenerManager<DeviceEvent, DeviceListener>
+        implements DeviceService, VnetService {
+
+    private static final String NETWORK_NULL = "Network ID cannot be null";
+    private static final String TYPE_NULL = "Type cannot be null";
+    private static final String DEVICE_NULL = "Device cannot be null";
+
+    private final VirtualNetwork network;
+    private final VirtualNetworkService manager;
+
+    /**
+     * Creates a new VirtualNetworkDeviceService object.
+     *
+     * @param virtualNetworkManager virtual network manager service
+     * @param network               virtual network
+     */
+    public VirtualNetworkDeviceService(VirtualNetworkService virtualNetworkManager, VirtualNetwork network) {
+        checkNotNull(network, NETWORK_NULL);
+        this.network = network;
+        this.manager = virtualNetworkManager;
+    }
+
+    @Override
+    public int getDeviceCount() {
+        return manager.getVirtualDevices(this.network.id()).size();
+    }
+
+    @Override
+    public Iterable<Device> getDevices() {
+        return manager.getVirtualDevices(this.network.id()).stream().collect(Collectors.toSet());
+    }
+
+    @Override
+    public Iterable<Device> getDevices(Device.Type type) {
+        checkNotNull(type, TYPE_NULL);
+        return manager.getVirtualDevices(this.network.id())
+                .stream()
+                .filter(device -> type.equals(device.type()))
+                .collect(Collectors.toSet());
+    }
+
+    @Override
+    public Iterable<Device> getAvailableDevices() {
+        return getDevices();
+    }
+
+    @Override
+    public Iterable<Device> getAvailableDevices(Device.Type type) {
+        return getDevices(type);
+    }
+
+    @Override
+    public Device getDevice(DeviceId deviceId) {
+        checkNotNull(deviceId, DEVICE_NULL);
+        Optional<VirtualDevice> foundDevice =  manager.getVirtualDevices(this.network.id())
+                .stream()
+                .filter(device -> deviceId.equals(device.id()))
+                .findFirst();
+        if (foundDevice.isPresent()) {
+            return foundDevice.get();
+        }
+        return null;
+    }
+
+    @Override
+    public MastershipRole getRole(DeviceId deviceId) {
+        checkNotNull(deviceId, DEVICE_NULL);
+        // TODO hard coded to master for now.
+        return MastershipRole.MASTER;
+    }
+
+    @Override
+    public List<Port> getPorts(DeviceId deviceId) {
+        checkNotNull(deviceId, DEVICE_NULL);
+        return manager.getVirtualPorts(this.network.id(), deviceId)
+                .stream()
+                .collect(Collectors.toList());
+    }
+
+    @Override
+    public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
+        checkNotNull(deviceId, DEVICE_NULL);
+        // TODO not supported at the moment.
+        return ImmutableList.of();
+    }
+
+    @Override
+    public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) {
+        checkNotNull(deviceId, DEVICE_NULL);
+        // TODO not supported at the moment.
+        return ImmutableList.of();
+    }
+
+    @Override
+    public Port getPort(DeviceId deviceId, PortNumber portNumber) {
+        checkNotNull(deviceId, DEVICE_NULL);
+
+        Optional<VirtualPort> foundPort =  manager.getVirtualPorts(this.network.id(), deviceId)
+                .stream()
+                .filter(port -> port.number().equals(portNumber))
+                .findFirst();
+        if (foundPort.isPresent()) {
+            return foundPort.get();
+        }
+        return null;
+    }
+
+    @Override
+    public boolean isAvailable(DeviceId deviceId) {
+        return getDevice(deviceId) != null;
+    }
+
+    @Override
+    public VirtualNetwork network() {
+        return network;
+    }
+}
diff --git a/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkLinkService.java b/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkLinkService.java
new file mode 100644
index 0000000..7af0267
--- /dev/null
+++ b/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkLinkService.java
@@ -0,0 +1,156 @@
+/*
+ * Copyright 2016-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.impl;
+
+import org.onosproject.event.AbstractListenerManager;
+import org.onosproject.incubator.net.virtual.VirtualLink;
+import org.onosproject.incubator.net.virtual.VirtualNetwork;
+import org.onosproject.incubator.net.virtual.VirtualNetworkService;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Link;
+import org.onosproject.net.link.LinkEvent;
+import org.onosproject.net.link.LinkListener;
+import org.onosproject.net.link.LinkService;
+
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Link service implementation built on the virtual network service.
+ */
+public class VirtualNetworkLinkService extends AbstractListenerManager<LinkEvent, LinkListener>
+        implements LinkService, VnetService {
+
+    private static final String NETWORK_NULL = "Network ID cannot be null";
+    private static final String DEVICE_NULL = "Device cannot be null";
+    private static final String CONNECT_POINT_NULL = "Connect point cannot be null";
+
+    private final VirtualNetwork network;
+    private final VirtualNetworkService manager;
+
+    /**
+     * Creates a new VirtualNetworkLinkService object.
+     *
+     * @param virtualNetworkManager virtual network manager service
+     * @param network               virtual network
+     */
+    public VirtualNetworkLinkService(VirtualNetworkService virtualNetworkManager, VirtualNetwork network) {
+        checkNotNull(network, NETWORK_NULL);
+        this.network = network;
+        this.manager = virtualNetworkManager;
+    }
+
+    @Override
+    public VirtualNetwork network() {
+        return network;
+    }
+
+    @Override
+    public int getLinkCount() {
+        return manager.getVirtualLinks(this.network.id()).size();
+    }
+
+    @Override
+    public Iterable<Link> getLinks() {
+        return manager.getVirtualLinks(this.network.id()).stream().collect(Collectors.toSet());
+    }
+
+    @Override
+    public Iterable<Link> getActiveLinks() {
+
+        return manager.getVirtualLinks(this.network.id())
+                .stream()
+                .filter(link -> (link.state().equals(Link.State.ACTIVE)))
+                .collect(Collectors.toSet());
+    }
+
+    @Override
+    public Set<Link> getDeviceLinks(DeviceId deviceId) {
+        checkNotNull(deviceId, DEVICE_NULL);
+        return manager.getVirtualLinks(this.network.id())
+                .stream()
+                .filter(link -> (deviceId.equals(link.src().elementId()) ||
+                        deviceId.equals(link.dst().elementId())))
+                .collect(Collectors.toSet());
+    }
+
+    @Override
+    public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
+        checkNotNull(deviceId, DEVICE_NULL);
+        return manager.getVirtualLinks(this.network.id())
+                .stream()
+                .filter(link -> (deviceId.equals(link.dst().elementId())))
+                .collect(Collectors.toSet());
+    }
+
+    @Override
+    public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
+        checkNotNull(deviceId, DEVICE_NULL);
+        return manager.getVirtualLinks(this.network.id())
+                .stream()
+                .filter(link -> (deviceId.equals(link.src().elementId())))
+                .collect(Collectors.toSet());
+    }
+
+    @Override
+    public Set<Link> getLinks(ConnectPoint connectPoint) {
+        checkNotNull(connectPoint, CONNECT_POINT_NULL);
+        return manager.getVirtualLinks(this.network.id())
+                .stream()
+                .filter(link -> (connectPoint.equals(link.src()) ||
+                        connectPoint.equals(link.dst())))
+                .collect(Collectors.toSet());
+    }
+
+    @Override
+    public Set<Link> getEgressLinks(ConnectPoint connectPoint) {
+        checkNotNull(connectPoint, CONNECT_POINT_NULL);
+        return manager.getVirtualLinks(this.network.id())
+                .stream()
+                .filter(link -> (connectPoint.equals(link.dst())))
+                .collect(Collectors.toSet());
+    }
+
+    @Override
+    public Set<Link> getIngressLinks(ConnectPoint connectPoint) {
+        checkNotNull(connectPoint, CONNECT_POINT_NULL);
+        return manager.getVirtualLinks(this.network.id())
+                .stream()
+                .filter(link -> (connectPoint.equals(link.src())))
+                .collect(Collectors.toSet());
+    }
+
+    @Override
+    public Link getLink(ConnectPoint src, ConnectPoint dst) {
+        checkNotNull(src, CONNECT_POINT_NULL);
+        checkNotNull(dst, CONNECT_POINT_NULL);
+        Optional<VirtualLink> foundLink =  manager.getVirtualLinks(this.network.id())
+                .stream()
+                .filter(link -> (src.equals(link.src()) &&
+                        dst.equals(link.dst())))
+                .findFirst();
+
+        if (foundLink.isPresent()) {
+            return foundLink.get();
+        }
+        return null;
+    }
+}
diff --git a/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManager.java b/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManager.java
index ee1e01c..54f49f3 100644
--- a/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManager.java
+++ b/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VirtualNetworkManager.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.incubator.net.virtual.impl;
 
+import com.google.common.collect.Maps;
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
@@ -42,11 +43,14 @@
 import org.onosproject.net.Link;
 import org.onosproject.net.Port;
 import org.onosproject.net.PortNumber;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.link.LinkService;
 import org.onosproject.net.provider.AbstractListenerProviderRegistry;
 import org.onosproject.net.provider.AbstractProviderService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.Map;
 import java.util.Set;
 
 import static com.google.common.base.Preconditions.checkNotNull;
@@ -231,6 +235,11 @@
         return store.getNetworks(tenantId);
     }
 
+    private VirtualNetwork getVirtualNetwork(NetworkId networkId) {
+        checkNotNull(networkId, NETWORK_NULL);
+        return store.getNetwork(networkId);
+    }
+
     @Override
     public Set<VirtualDevice> getVirtualDevices(NetworkId networkId) {
         checkNotNull(networkId, NETWORK_NULL);
@@ -249,10 +258,59 @@
         return store.getPorts(networkId, deviceId);
     }
 
+    private final Map<ServiceKey, VnetService> networkServices = Maps.newConcurrentMap();
+
     @Override
     public <T> T get(NetworkId networkId, Class<T> serviceClass) {
         checkNotNull(networkId, NETWORK_NULL);
-        return null;
+        ServiceKey serviceKey = networkServiceKey(networkId, serviceClass);
+        VnetService service = lookup(serviceKey);
+        if (service == null) {
+            service = create(serviceKey);
+        }
+        return (T) service;
+    }
+
+    private VnetService lookup(ServiceKey serviceKey) {
+        return networkServices.get(serviceKey);
+    }
+
+    private <T> ServiceKey networkServiceKey(NetworkId networkId, Class<T> serviceClass) {
+        return new ServiceKey(networkId, serviceClass);
+    }
+
+
+    private VnetService create(ServiceKey serviceKey) {
+        VirtualNetwork network = getVirtualNetwork(serviceKey.networkId());
+        VnetService service;
+        if (serviceKey.serviceClass.equals(DeviceService.class)) {
+            service = new VirtualNetworkDeviceService(this, network);
+        } else if (serviceKey.serviceClass.equals(LinkService.class)) {
+            service = new VirtualNetworkLinkService(this, network);
+        } else {
+            return null;
+        }
+        networkServices.put(serviceKey, service);
+        return service;
+    }
+
+    private class ServiceKey {
+        final NetworkId networkId;
+        final Class serviceClass;
+
+        public ServiceKey(NetworkId networkId, Class serviceClass) {
+            checkNotNull(networkId, NETWORK_NULL);
+            this.networkId = networkId;
+            this.serviceClass = serviceClass;
+        }
+
+        public NetworkId networkId() {
+            return networkId;
+        }
+
+        public Class serviceClass() {
+            return serviceClass;
+        }
     }
 
     @Override
diff --git a/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VnetService.java b/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VnetService.java
new file mode 100644
index 0000000..7c83f75
--- /dev/null
+++ b/incubator/net/src/main/java/org/onosproject/incubator/net/virtual/impl/VnetService.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2016-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.impl;
+
+import org.onosproject.incubator.net.virtual.VirtualNetwork;
+
+/**
+ * Virtual network service interface.
+ */
+interface VnetService {
+    VirtualNetwork network();
+
+}