Add skeleton code for TAPI manager and topology generation feature

Change-Id: If7eaa3ceb7fe64122b1e640ac864d06d578530cb
diff --git a/apps/odtn/service/src/main/java/org/onosproject/odtn/impl/ServiceApplicationComponent.java b/apps/odtn/service/src/main/java/org/onosproject/odtn/impl/ServiceApplicationComponent.java
index 87ae4f4..b52afbf 100644
--- a/apps/odtn/service/src/main/java/org/onosproject/odtn/impl/ServiceApplicationComponent.java
+++ b/apps/odtn/service/src/main/java/org/onosproject/odtn/impl/ServiceApplicationComponent.java
@@ -27,9 +27,16 @@
 import org.apache.felix.scr.annotations.Deactivate;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.net.Device;
+import org.onosproject.net.Link;
 import org.onosproject.net.config.NetworkConfigService;
-import org.onosproject.yang.model.SchemaContextProvider;
-import org.onosproject.yang.runtime.YangRuntimeService;
+import org.onosproject.net.device.DeviceEvent;
+import org.onosproject.net.device.DeviceListener;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.link.LinkEvent;
+import org.onosproject.net.link.LinkListener;
+import org.onosproject.net.link.LinkService;
+import org.onosproject.odtn.internal.TapiTopologyManager;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -69,7 +76,8 @@
 import org.onosproject.yang.model.RpcService;
 import org.onosproject.yang.model.RpcInput;
 import org.onosproject.yang.model.RpcOutput;
-
+import org.onosproject.yang.model.SchemaContextProvider;
+import org.onosproject.yang.runtime.YangRuntimeService;
 
 /**
  * OSGi Component for ODTN Service application.
@@ -83,6 +91,12 @@
     protected DynamicConfigService dynConfigService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected DeviceService deviceService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected LinkService linkService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected NetworkConfigService netcfgService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
@@ -97,21 +111,27 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected ModelConverter modelConverter;
 
-
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected TapiTopologyManager tapiTopologyManager;
 
     // Listener for events from the DCS
     private final DynamicConfigListener dynamicConfigServiceListener =
-        new InternalDynamicConfigListener();
+            new InternalDynamicConfigListener();
+
+    private DeviceListener deviceListener = new InternalDeviceListener();
+    private final LinkListener linkListener = new InternalLinkListener();
 
     // Rpc Service for TAPI Connectivity
     private final RpcService rpcTapiConnectivity =
-        new TapiConnectivityRpc();
+            new TapiConnectivityRpc();
 
 
     @Activate
     protected void activate() {
         log.info("Started");
         dynConfigService.addListener(dynamicConfigServiceListener);
+        deviceService.addListener(deviceListener);
+        linkService.addListener(linkListener);
         rpcRegistry.registerRpcService(rpcTapiConnectivity);
     }
 
@@ -120,11 +140,74 @@
     protected void deactivate() {
         log.info("Stopped");
         rpcRegistry.unregisterRpcService(rpcTapiConnectivity);
+        deviceService.removeListener(deviceListener);
+        linkService.removeListener(linkListener);
         dynConfigService.removeListener(dynamicConfigServiceListener);
     }
 
 
+    /**
+     * Representation of internal listener, listening for device event.
+     */
+    private class InternalDeviceListener implements DeviceListener {
 
+        /**
+         * Process an Event from the Device Service.
+         *
+         * @param event device event
+         */
+        @Override
+        public void event(DeviceEvent event) {
+            Device device = event.subject();
+
+            switch (event.type()) {
+                case DEVICE_ADDED:
+                    tapiTopologyManager.addDevice(device);
+                    break;
+                case DEVICE_REMOVED:
+                    tapiTopologyManager.removeDevice(device);
+                    break;
+                case PORT_ADDED:
+                    tapiTopologyManager.addPort(device);
+                    break;
+                case PORT_REMOVED:
+                    tapiTopologyManager.removePort(device);
+                    break;
+                default:
+                    log.warn("Unknown Event", event.type());
+                    break;
+            }
+
+        }
+    }
+
+    /**
+     * Representation of internal listener, listening for link event.
+     */
+    private class InternalLinkListener implements LinkListener {
+
+        /**
+         * Process an Event from the Device Service.
+         *
+         * @param event link event
+         */
+        @Override
+        public void event(LinkEvent event) {
+            Link link = event.subject();
+
+            switch (event.type()) {
+                case LINK_ADDED:
+                    tapiTopologyManager.addLink(link);
+                    break;
+                case LINK_REMOVED:
+                    tapiTopologyManager.removeLink(link);
+                    break;
+                default:
+                    log.warn("Unknown Event", event.type());
+                    break;
+            }
+        }
+    }
 
     /**
      * Representation of internal listener, listening for dynamic config event.
@@ -141,15 +224,13 @@
         public boolean isRelevant(DynamicConfigEvent event) {
             // Only care about add and delete
             if ((event.type() != NODE_ADDED) &&
-                (event.type() != NODE_DELETED)) {
+                    (event.type() != NODE_DELETED)) {
                 return false;
             }
             return true;
         }
 
 
-
-
         /**
          * Process an Event from the Dynamic Configuration Store.
          *
@@ -181,7 +262,6 @@
         }
 
 
-
         /**
          * Process the event that a node has been added to the DCS.
          *
@@ -209,7 +289,7 @@
             }
 
             // Consolidate events
-            log.info("namespace {}", schemaId.namespace());
+//            log.info("namespace {}", schemaId.namespace());
         }
 
 
@@ -225,9 +305,6 @@
     }
 
 
-
-
-
     private class TapiConnectivityRpc implements TapiConnectivityService {
 
 
@@ -260,7 +337,6 @@
         }
 
 
-
         /**
          * Service interface of deleteConnectivityService.
          *
@@ -273,7 +349,6 @@
         }
 
 
-
         /**
          * Service interface of getConnectionDetails.
          *
@@ -326,9 +401,9 @@
 
         private ResourceData createResourceData(DataNode dataNode, ResourceId resId) {
             return DefaultResourceData.builder()
-                .addDataNode(dataNode)
-                .resourceId(resId)
-                .build();
+                    .addDataNode(dataNode)
+                    .resourceId(resId)
+                    .build();
         }
 
         /**
@@ -345,6 +420,5 @@
         }
 
 
-
     }
 }
diff --git a/apps/odtn/service/src/main/java/org/onosproject/odtn/internal/DcsBasedTapiTopologyManager.java b/apps/odtn/service/src/main/java/org/onosproject/odtn/internal/DcsBasedTapiTopologyManager.java
new file mode 100644
index 0000000..272319c
--- /dev/null
+++ b/apps/odtn/service/src/main/java/org/onosproject/odtn/internal/DcsBasedTapiTopologyManager.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.odtn.internal;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
+import org.onosproject.config.DynamicConfigService;
+import org.onosproject.config.FailedException;
+import org.onosproject.config.Filter;
+import org.onosproject.d.config.DeviceResourceIds;
+import org.onosproject.d.config.ResourceIds;
+import org.onosproject.net.Device;
+import org.onosproject.net.Link;
+import org.onosproject.yang.model.DataNode;
+import org.onosproject.yang.model.InnerNode;
+import org.slf4j.Logger;
+
+import static org.onosproject.d.config.DeviceResourceIds.DCS_NAMESPACE;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * OSGi Component for ODTN Tapi manager application.
+ */
+@Component(immediate = true)
+@Service
+public class DcsBasedTapiTopologyManager implements TapiTopologyManager {
+
+    private final Logger log = getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected DynamicConfigService dcs;
+
+    @Activate
+    public void activate() {
+        initDcsIfRootNotExist();
+        log.info("Started");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        log.info("Stopped");
+    }
+
+    @Override
+    public void addDevice(Device device) {
+        log.info("Add device: {}", device);
+    }
+
+    @Override
+    public void removeDevice(Device device) {
+        log.info("Remove device: {}", device);
+    }
+
+    @Override
+    public void addLink(Link link) {
+        log.info("Add link: {}", link);
+    }
+
+    @Override
+    public void removeLink(Link link) {
+        log.info("Remove link: {}", link);
+    }
+
+    @Override
+    public void addPort(Device device) {
+        log.info("Add port: {}", device);
+    }
+
+    @Override
+    public void removePort(Device device) {
+        log.info("Remove port: {}", device);
+    }
+
+    private void initDcsIfRootNotExist() {
+
+        log.info("read root:");
+        try {
+            DataNode all = dcs.readNode(ResourceIds.ROOT_ID, Filter.builder().build());
+            log.info("all: {}", all);
+        } catch (FailedException e) {
+            // FIXME debug this issue
+            log.info("nothing retrievable in DCS?");
+            //e.printStackTrace(System.out);
+        }
+        if (!dcs.nodeExist(ResourceIds.ROOT_ID)) {
+            log.info("Root node does not exist!, creating...");
+            try {
+                log.info("create 'root' node");
+                dcs.createNode(null,
+                        InnerNode.builder(DeviceResourceIds.ROOT_NAME, DCS_NAMESPACE)
+                                .type(DataNode.Type.SINGLE_INSTANCE_NODE).build());
+            } catch (FailedException e) {
+                log.info("Failed to create root???");
+                //e.printStackTrace(System.out);
+            }
+        }
+        if (!dcs.nodeExist(ResourceIds.ROOT_ID)) {
+            log.info("'root' was created without error, but still not there. WTF!");
+        }
+    }
+}
diff --git a/apps/odtn/service/src/main/java/org/onosproject/odtn/internal/TapiTopologyManager.java b/apps/odtn/service/src/main/java/org/onosproject/odtn/internal/TapiTopologyManager.java
new file mode 100644
index 0000000..d997b45
--- /dev/null
+++ b/apps/odtn/service/src/main/java/org/onosproject/odtn/internal/TapiTopologyManager.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.odtn.internal;
+
+import org.onosproject.net.Device;
+import org.onosproject.net.Link;
+
+public interface TapiTopologyManager {
+
+    void addDevice(Device device);
+
+    void removeDevice(Device device);
+
+    void addLink(Link link);
+
+    void removeLink(Link link);
+
+    void addPort(Device device);
+
+    void removePort(Device device);
+}
diff --git a/apps/odtn/service/src/main/java/org/onosproject/odtn/internal/package-info.java b/apps/odtn/service/src/main/java/org/onosproject/odtn/internal/package-info.java
new file mode 100644
index 0000000..b75ad48
--- /dev/null
+++ b/apps/odtn/service/src/main/java/org/onosproject/odtn/internal/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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 to place ODTN internal implementations.
+ */
+package org.onosproject.odtn.internal;
\ No newline at end of file