ONOS-4107: Changes to provider

Change-Id: Ice1a5d49a66fc4f8e5033b47c0ee90601d55589e
diff --git a/providers/isis/BUCK b/providers/isis/BUCK
index 1663d60..adc7a6f 100644
--- a/providers/isis/BUCK
+++ b/providers/isis/BUCK
@@ -3,7 +3,7 @@
     '//protocols/isis/ctl:onos-protocols-isis-ctl',
     '//protocols/isis/isisio:onos-protocols-isis-isisio',
     '//providers/isis/cfg:onos-providers-isis-cfg',
-    '//providers/isis/device:onos-providers-isis-device',
+    '//providers/isis/topology:onos-providers-isis-topology',
 ]
 
 onos_app (
diff --git a/providers/isis/app/app.xml b/providers/isis/app/app.xml
index 3158848..c07a454 100644
--- a/providers/isis/app/app.xml
+++ b/providers/isis/app/app.xml
@@ -21,6 +21,7 @@
     <artifact>mvn:${project.groupId}/onos-isis-api/${project.version}</artifact>
     <artifact>mvn:${project.groupId}/onos-isis-isisio/${project.version}</artifact>
     <artifact>mvn:${project.groupId}/onos-isis-ctl/${project.version}</artifact>
-    <artifact>mvn:${project.groupId}/onos-isis-provider-device/${project.version}</artifact>
+    <artifact>mvn:${project.groupId}/onos-isis-provider-topology/${project.version}</artifact>
     <artifact>mvn:${project.groupId}/onos-isis-provider-cfg/${project.version}</artifact>
+    <artifact>mvn:${project.groupId}/onos-isis-provider-cli/${project.version}</artifact>
 </app>
\ No newline at end of file
diff --git a/providers/isis/app/features.xml b/providers/isis/app/features.xml
index 132bcda..d4a13cc 100644
--- a/providers/isis/app/features.xml
+++ b/providers/isis/app/features.xml
@@ -15,13 +15,15 @@
   ~ limitations under the License.
   -->
 <features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" name="${project.artifactId}-${project.version}">
+    <repository>mvn:${project.groupId}/${project.artifactId}/${project.version}/xml/features</repository>
     <feature name="${project.artifactId}" version="${project.version}"
              description="${project.description}">
         <feature>onos-api</feature>
         <bundle>mvn:${project.groupId}/onos-isis-api/${project.version}</bundle>
         <bundle>mvn:${project.groupId}/onos-isis-isisio/${project.version}</bundle>
         <bundle>mvn:${project.groupId}/onos-isis-ctl/${project.version}</bundle>
-        <bundle>mvn:${project.groupId}/onos-isis-provider-device/${project.version}</bundle>
+        <bundle>mvn:${project.groupId}/onos-isis-provider-topology/${project.version}</bundle>
         <bundle>mvn:${project.groupId}/onos-isis-provider-cfg/${project.version}</bundle>
+        <bundle>mvn:${project.groupId}/onos-isis-provider-cli/${project.version}</bundle>
     </feature>
-</features>
+</features>
\ No newline at end of file
diff --git a/providers/isis/app/pom.xml b/providers/isis/app/pom.xml
index b0c0aa0..9f15602 100644
--- a/providers/isis/app/pom.xml
+++ b/providers/isis/app/pom.xml
@@ -49,7 +49,7 @@
         </dependency>
         <dependency>
             <groupId>org.onosproject</groupId>
-            <artifactId>onos-isis-provider-device</artifactId>
+            <artifactId>onos-isis-provider-topology</artifactId>
             <version>${project.version}</version>
         </dependency>
         <dependency>
@@ -57,5 +57,10 @@
             <artifactId>onos-isis-provider-cfg</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-isis-provider-cli</artifactId>
+            <version>${project.version}</version>
+        </dependency>
     </dependencies>
-</project>
\ No newline at end of file
+</project>
diff --git a/providers/isis/device/src/main/java/org/onosproject/provider/isis/device/impl/IsisTopologyProvider.java b/providers/isis/device/src/main/java/org/onosproject/provider/isis/device/impl/IsisTopologyProvider.java
deleted file mode 100644
index 5557429..0000000
--- a/providers/isis/device/src/main/java/org/onosproject/provider/isis/device/impl/IsisTopologyProvider.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * 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.provider.isis.device.impl;
-
-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.onosproject.isis.controller.IsisController;
-import org.onosproject.net.provider.AbstractProvider;
-import org.onosproject.net.provider.ProviderId;
-import org.slf4j.Logger;
-
-import static org.slf4j.LoggerFactory.getLogger;
-
-/**
- * Provider which advertises device descriptions to the core.
- */
-@Component(immediate = true)
-public class IsisTopologyProvider extends AbstractProvider {
-
-    private static final Logger log = getLogger(IsisTopologyProvider.class);
-    final InternalDeviceProvider listener = new InternalDeviceProvider();
-    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    private IsisController isisController;
-
-
-    /**
-     * Creates an ISIS device provider.
-     */
-    public IsisTopologyProvider() {
-        super(new ProviderId("isis", "org.onosproject.provider.isis"));
-    }
-
-    @Activate
-    public void activate() {
-        log.debug("Activate...!!!");
-    }
-
-    @Deactivate
-    public void deactivate() {
-        log.debug("Deactivate...!!!");
-    }
-
-    /**
-     * Internal device provider implementation.
-     */
-    private class InternalDeviceProvider {
-
-    }
-}
diff --git a/providers/isis/pom.xml b/providers/isis/pom.xml
index c6968ac..20801dd28 100644
--- a/providers/isis/pom.xml
+++ b/providers/isis/pom.xml
@@ -32,10 +32,10 @@
     <description>ONOS ISIS protocol adapters</description>
 
     <modules>
-        <module>device</module>
         <module>app</module>
         <module>cfg</module>
         <module>cli</module>
+        <module>topology</module>
     </modules>
 
     <dependencies>
diff --git a/providers/isis/device/BUCK b/providers/isis/topology/BUCK
similarity index 100%
rename from providers/isis/device/BUCK
rename to providers/isis/topology/BUCK
diff --git a/providers/isis/device/pom.xml b/providers/isis/topology/pom.xml
similarity index 96%
rename from providers/isis/device/pom.xml
rename to providers/isis/topology/pom.xml
index 9576791..106386c 100644
--- a/providers/isis/device/pom.xml
+++ b/providers/isis/topology/pom.xml
@@ -26,7 +26,7 @@
         <relativePath>../pom.xml</relativePath>
     </parent>
 
-    <artifactId>onos-isis-provider-device</artifactId>
+    <artifactId>onos-isis-provider-topology</artifactId>
     <packaging>bundle</packaging>
 
     <description>ONOS ISIS Providers</description>
diff --git a/providers/isis/topology/src/main/java/org/onosproject/provider/isis/topology/impl/IsisTopologyProvider.java b/providers/isis/topology/src/main/java/org/onosproject/provider/isis/topology/impl/IsisTopologyProvider.java
new file mode 100644
index 0000000..8a58289
--- /dev/null
+++ b/providers/isis/topology/src/main/java/org/onosproject/provider/isis/topology/impl/IsisTopologyProvider.java
@@ -0,0 +1,322 @@
+/*
+ * 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.provider.isis.topology.impl;
+
+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.onlab.packet.ChassisId;
+import org.onlab.util.Bandwidth;
+import org.onosproject.isis.controller.IsisController;
+import org.onosproject.isis.controller.topology.IsisLink;
+import org.onosproject.isis.controller.topology.IsisLinkListener;
+import org.onosproject.isis.controller.topology.IsisLinkTed;
+import org.onosproject.isis.controller.topology.IsisRouter;
+import org.onosproject.isis.controller.topology.IsisRouterId;
+import org.onosproject.isis.controller.topology.IsisRouterListener;
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Link;
+import org.onosproject.net.MastershipRole;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.config.NetworkConfigService;
+import org.onosproject.net.config.basics.BandwidthCapacity;
+import org.onosproject.net.device.DefaultDeviceDescription;
+import org.onosproject.net.device.DefaultPortDescription;
+import org.onosproject.net.device.DeviceDescription;
+import org.onosproject.net.device.DeviceProvider;
+import org.onosproject.net.device.DeviceProviderRegistry;
+import org.onosproject.net.device.DeviceProviderService;
+import org.onosproject.net.device.PortDescription;
+import org.onosproject.net.link.DefaultLinkDescription;
+import org.onosproject.net.link.LinkDescription;
+import org.onosproject.net.link.LinkProvider;
+import org.onosproject.net.link.LinkProviderRegistry;
+import org.onosproject.net.link.LinkProviderService;
+import org.onosproject.net.provider.AbstractProvider;
+import org.onosproject.net.provider.ProviderId;
+import org.slf4j.Logger;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Provider which advertises device descriptions to the core.
+ */
+@Component(immediate = true)
+public class IsisTopologyProvider extends AbstractProvider implements DeviceProvider, LinkProvider {
+
+    public static final long PSEUDO_PORT = 0xffffffff;
+    public static final String ADMINISTRATIVEGROUP = "administrativeGroup";
+    public static final String TE_METRIC = "teMetric";
+    public static final String MAXRESERVABLEBANDWIDTH = "maxReservableBandwidth";
+    public static final String ROUTERID = "routerId";
+    public static final String NEIGHBORID = "neighborId";
+    private static final Logger log = getLogger(IsisTopologyProvider.class);
+    // Default values for tunable parameters
+    private static final String UNKNOWN = "unknown";
+    final InternalTopologyProvider listener = new InternalTopologyProvider();
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected DeviceProviderRegistry deviceProviderRegistry;
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected LinkProviderRegistry linkProviderRegistry;
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected NetworkConfigService networkConfigService;
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected IsisController controller;
+    //This Interface that defines how this provider can interact with the core.
+    private LinkProviderService linkProviderService;
+    // The interface that defines how this Provider can interact with the core
+    private DeviceProviderService deviceProviderService;
+    private HashMap<DeviceId, List<PortDescription>> portMap = new HashMap<>();
+
+    /**
+     * Creates an ISIS device provider.
+     */
+    public IsisTopologyProvider() {
+        super(new ProviderId("l3", "org.onosproject.provider.isis"));
+    }
+
+    @Activate
+    public void activate() {
+        deviceProviderService = deviceProviderRegistry.register(this);
+        linkProviderService = linkProviderRegistry.register(this);
+        controller.addRouterListener(listener);
+        controller.addLinkListener(listener);
+        log.debug("IsisDeviceProvider::activate...!!!!");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        log.debug("IsisDeviceProvider::deactivate...!!!!");
+        deviceProviderRegistry.unregister(this);
+        deviceProviderService = null;
+        linkProviderRegistry.unregister(this);
+        linkProviderService = null;
+        controller.removeRouterListener(listener);
+        controller.removeLinkListener(listener);
+    }
+
+    @Override
+    public void triggerProbe(DeviceId deviceId) {
+        log.debug("IsisDeviceProvider::triggerProbe...!!!!");
+    }
+
+    @Override
+    public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
+        log.debug("IsisDeviceProvider::roleChanged...!!!!");
+    }
+
+    @Override
+    public boolean isReachable(DeviceId deviceId) {
+        log.debug("IsisDeviceProvider::isReachable...!!!!");
+        return false;
+    }
+
+    @Override
+    public void changePortState(DeviceId deviceId, PortNumber portNumber, boolean enable) {
+        log.debug("IsisDeviceProvider::changePortState...!!!!");
+    }
+
+    /**
+     * Builds link description.
+     *
+     * @param isisLink ISIS link instance
+     * @return link description
+     */
+    private LinkDescription buildLinkDes(IsisLink isisLink) {
+        long srcAddress = 0;
+        long dstAddress = 0;
+        String localSystemId = isisLink.localSystemId();
+        String remoteSystemId = isisLink.remoteSystemId();
+        //Changing of port numbers
+        srcAddress = isisLink.interfaceIp().toInt();
+        dstAddress = isisLink.neighborIp().toInt();
+        DeviceId srcId = DeviceId.deviceId(IsisRouterId.uri(localSystemId));
+        DeviceId dstId = DeviceId.deviceId(IsisRouterId.uri(remoteSystemId));
+        if (srcAddress == 0) {
+            srcAddress = PSEUDO_PORT;
+        } else if (dstAddress == 0) {
+            dstAddress = PSEUDO_PORT;
+        }
+
+        ConnectPoint src = new ConnectPoint(srcId, PortNumber.portNumber(srcAddress));
+        ConnectPoint dst = new ConnectPoint(dstId, PortNumber.portNumber(dstAddress));
+        DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
+        if (isisLink != null) {
+            annotationBuilder = buildAnnotations(annotationBuilder, isisLink);
+        }
+
+        return new DefaultLinkDescription(src, dst, Link.Type.DIRECT, false, annotationBuilder.build());
+    }
+
+    /**
+     * Builds port description.
+     *
+     * @param deviceId   device ID for the port
+     * @param portNumber port number of the link
+     * @return list of port description
+     */
+    private List<PortDescription> buildPortDescriptions(DeviceId deviceId,
+                                                        PortNumber portNumber) {
+
+        List<PortDescription> portList;
+
+        if (portMap.containsKey(deviceId)) {
+            portList = portMap.get(deviceId);
+        } else {
+            portList = new ArrayList<>();
+        }
+        if (portNumber != null) {
+            PortDescription portDescriptions = new DefaultPortDescription(portNumber, true);
+            portList.add(portDescriptions);
+        }
+
+        portMap.put(deviceId, portList);
+        return portList;
+    }
+
+    /**
+     * Builds the annotation details.
+     *
+     * @param annotationBuilder default annotation builder instance
+     * @param isisLink          ISIS link instance
+     * @return annotation builder instance
+     */
+    private DefaultAnnotations.Builder buildAnnotations(DefaultAnnotations.Builder annotationBuilder,
+                                                        IsisLink isisLink) {
+        int administrativeGroup = 0;
+        long teMetric = 0;
+        Bandwidth maxReservableBandwidth = Bandwidth.bps(0);
+        String routerId = null;
+        String neighborId = null;
+
+        //TE Info
+        IsisLinkTed isisLinkTed = isisLink.linkTed();
+        administrativeGroup = isisLinkTed.administrativeGroup();
+        teMetric = isisLinkTed.teDefaultMetric();
+        maxReservableBandwidth = isisLinkTed.maximumReservableLinkBandwidth();
+        routerId = isisLink.localSystemId();
+        neighborId = isisLink.remoteSystemId();
+        annotationBuilder.set(ADMINISTRATIVEGROUP, String.valueOf(administrativeGroup));
+        annotationBuilder.set(TE_METRIC, String.valueOf(teMetric));
+        annotationBuilder.set(MAXRESERVABLEBANDWIDTH, String.valueOf(maxReservableBandwidth));
+        annotationBuilder.set(ROUTERID, String.valueOf(routerId));
+        annotationBuilder.set(NEIGHBORID, String.valueOf(neighborId));
+        return annotationBuilder;
+    }
+
+    /**
+     * Internal device provider implementation.
+     */
+    private class InternalTopologyProvider implements IsisRouterListener, IsisLinkListener {
+
+        @Override
+        public void routerAdded(IsisRouter isisRouter) {
+            String systemId = isisRouter.systemId();
+            log.info("Added device {}", systemId);
+            DeviceId deviceId = DeviceId.deviceId(IsisRouterId.uri(systemId));
+            Device.Type deviceType = Device.Type.ROUTER;
+            //If our routerType is Dr or Bdr type is PSEUDO
+            if (isisRouter.isDis()) {
+                deviceType = Device.Type.ROUTER;
+            } else {
+                deviceType = Device.Type.VIRTUAL;
+            }
+            ChassisId cId = new ChassisId();
+            DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder();
+            newBuilder.set(AnnotationKeys.TYPE, "L3");
+            newBuilder.set("RouterId", systemId);
+            DeviceDescription description =
+                    new DefaultDeviceDescription(IsisRouterId.uri(systemId), deviceType, UNKNOWN, UNKNOWN, UNKNOWN,
+                                                 UNKNOWN, cId, newBuilder.build());
+            deviceProviderService.deviceConnected(deviceId, description);
+        }
+
+        @Override
+        public void routerRemoved(IsisRouter isisRouter) {
+            String systemId = isisRouter.systemId();
+            log.info("Delete device {}", systemId);
+            DeviceId deviceId = DeviceId.deviceId(IsisRouterId.uri(systemId));
+            if (deviceProviderService == null) {
+                return;
+            }
+            deviceProviderService.deviceDisconnected(deviceId);
+            log.info("delete device {}", systemId);
+        }
+
+        @Override
+        public void addLink(IsisLink isisLink) {
+            log.debug("Addlink {}", isisLink.localSystemId());
+            if (linkProviderService == null) {
+                return;
+            }
+            LinkDescription linkDes = buildLinkDes(isisLink);
+            //Updating ports of the link
+            deviceProviderService.updatePorts(linkDes.src().deviceId(), buildPortDescriptions(linkDes.src().deviceId(),
+                                                                                              linkDes.src().port()));
+            deviceProviderService.updatePorts(linkDes.dst().deviceId(), buildPortDescriptions(linkDes.dst().deviceId(),
+                                                                                              linkDes.dst().port()));
+            registerBandwidth(linkDes, isisLink);
+            linkProviderService.linkDetected(linkDes);
+        }
+
+        @Override
+        public void deleteLink(IsisLink isisLink) {
+            log.debug("Delete link {}", isisLink.localSystemId());
+            if (linkProviderService == null) {
+                return;
+            }
+            LinkDescription linkDes = buildLinkDes(isisLink);
+            linkProviderService.linkVanished(linkDes);
+        }
+
+        /**
+         * Registers the bandwidth for source and destination points.
+         *
+         * @param linkDes  link description instance
+         * @param isisLink ISIS link instance
+         */
+        private void registerBandwidth(LinkDescription linkDes, IsisLink isisLink) {
+            if (isisLink == null) {
+                log.error("Could not able to register bandwidth ");
+                return;
+            }
+            IsisLinkTed isisLinkTed = isisLink.linkTed();
+            Bandwidth maxReservableBw = isisLinkTed.maximumReservableLinkBandwidth();
+            if (maxReservableBw != null) {
+                if (maxReservableBw.compareTo(Bandwidth.bps(0)) == 0) {
+                    return;
+                }
+                //Configure bandwidth for src and dst port
+                BandwidthCapacity config = networkConfigService.addConfig(linkDes.src(), BandwidthCapacity.class);
+                config.capacity(maxReservableBw).apply();
+
+                config = networkConfigService.addConfig(linkDes.dst(), BandwidthCapacity.class);
+                config.capacity(maxReservableBw).apply();
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/providers/isis/device/src/main/java/org/onosproject/provider/isis/device/impl/package-info.java b/providers/isis/topology/src/main/java/org/onosproject/provider/isis/topology/impl/package-info.java
similarity index 92%
rename from providers/isis/device/src/main/java/org/onosproject/provider/isis/device/impl/package-info.java
rename to providers/isis/topology/src/main/java/org/onosproject/provider/isis/topology/impl/package-info.java
index eb8d960..886a345 100644
--- a/providers/isis/device/src/main/java/org/onosproject/provider/isis/device/impl/package-info.java
+++ b/providers/isis/topology/src/main/java/org/onosproject/provider/isis/topology/impl/package-info.java
@@ -17,4 +17,4 @@
 /**
  * Provider that uses ISIS request as a means of infrastructure device discovery.
  */
-package org.onosproject.provider.isis.device.impl;
\ No newline at end of file
+package org.onosproject.provider.isis.topology.impl;