[ONOS-5597] TE Topology NBI app YANG notificatin support

Change-Id: Ia5c7fe9d54f9d0104d05351cd0837c28117f3ccd
diff --git a/apps/tenbi/topology/src/main/java/org/onosproject/tenbi/topology/impl/TeTopologyNbiManager.java b/apps/tenbi/topology/src/main/java/org/onosproject/tenbi/topology/impl/TeTopologyNbiManager.java
index a014a48..1cd53ad 100644
--- a/apps/tenbi/topology/src/main/java/org/onosproject/tenbi/topology/impl/TeTopologyNbiManager.java
+++ b/apps/tenbi/topology/src/main/java/org/onosproject/tenbi/topology/impl/TeTopologyNbiManager.java
@@ -15,8 +15,6 @@
  */
 package org.onosproject.tenbi.topology.impl;
 
-import static com.google.common.base.Preconditions.checkNotNull;
-
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
@@ -24,6 +22,8 @@
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
 import org.onosproject.event.AbstractListenerManager;
+import org.onosproject.tetopology.management.api.TeTopologyEvent;
+import org.onosproject.tetopology.management.api.TeTopologyListener;
 import org.onosproject.tetopology.management.api.TeTopologyService;
 import org.onosproject.teyang.api.OperationType;
 import org.onosproject.teyang.utils.topology.NetworkConverter;
@@ -34,30 +34,31 @@
 import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev20151208.ietfnetwork.Networks;
 import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev20151208.ietfnetwork.NetworksState;
 import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev20151208.IetfNetworkTopology;
-import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev20151208
-               .IetfNetworkTopologyOpParam;
-import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev20151208
-               .IetfNetworkTopologyService;
+import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology
+        .rev20151208.IetfNetworkTopologyOpParam;
+import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology
+        .rev20151208.IetfNetworkTopologyService;
 import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.IetfTeTopology;
 import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.IetfTeTopologyOpParam;
-import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708
-               .IetfTeTopologyService;
-import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708
-               .ietftetopology.IetfTeTopologyEvent;
-import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708
-               .ietftetopology.IetfTeTopologyEventListener;
+import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.IetfTeTopologyService;
+import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.ietftetopology
+        .IetfTeTopologyEvent;
+import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.ietftetopology
+        .IetfTeTopologyEventListener;
 import org.onosproject.yms.ymsm.YmsService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+
 /**
  * The IETF TE Topology NBI Manager implementation.
  */
 @Component(immediate = true)
 @Service
 public class TeTopologyNbiManager
-    extends  AbstractListenerManager<IetfTeTopologyEvent, IetfTeTopologyEventListener>
-    implements IetfNetworkService, IetfNetworkTopologyService, IetfTeTopologyService {
+        extends AbstractListenerManager<IetfTeTopologyEvent, IetfTeTopologyEventListener>
+        implements IetfNetworkService, IetfNetworkTopologyService, IetfTeTopologyService {
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
@@ -67,18 +68,40 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected YmsService ymsService;
 
+
+    /**
+     * Activation helper function.
+     */
+    private void activateBasics() {
+        eventDispatcher.addSink(IetfTeTopologyEvent.class, listenerRegistry);
+    }
+
+    /**
+     * Deactivation helper function.
+     */
+    private void deactivateBasics() {
+        eventDispatcher.removeSink(IetfTeTopologyEvent.class);
+    }
+
     @Activate
     protected void activate() {
+        activateBasics();
+
         // Register 3 services with YMS.
         ymsService.registerService(this, IetfNetworkService.class, null);
         ymsService.registerService(this, IetfNetworkTopologyService.class, null);
         ymsService.registerService(this, IetfTeTopologyService.class, null);
 
+        // Listens to TE Topology events
+        teTopologyService.addListener(new InternalTeTopologyListener());
+
         log.info("Started");
     }
 
     @Deactivate
     protected void deactivate() {
+        deactivateBasics();
+
         // Unregister 3 services.
         ymsService.unRegisterService(this, IetfNetworkService.class);
         ymsService.unRegisterService(this, IetfNetworkTopologyService.class);
@@ -89,8 +112,6 @@
 
     @Override
     public IetfNetwork getIetfNetwork(IetfNetworkOpParam ietfNetwork) {
-        log.info("getIetfNetwork: input {}", ietfNetwork);
-
         checkNotNull(ietfNetwork, "getIetfNetwork: ietfNetwork cannot be null");
 
         // Get the entire data tree from TE Subsystem core.
@@ -100,17 +121,18 @@
         Networks networks = NetworkConverter.teSubsystem2YangNetworks(teNetworks, OperationType.QUERY);
         NetworksState networkStates = NetworkConverter.teSubsystem2YangNetworkStates(teNetworks, OperationType.QUERY);
 
-        IetfNetworkOpParam.IetfNetworkBuilder builder =  new IetfNetworkOpParam.IetfNetworkBuilder();
+        IetfNetworkOpParam.IetfNetworkBuilder builder = new IetfNetworkOpParam.IetfNetworkBuilder();
         IetfNetwork newNetwork = builder.networks(networks)
-                                        .networksState(networkStates)
-                                        .yangIetfNetworkOpType(OnosYangOpType.NONE)
-                                        .build();
+                .networksState(networkStates)
+                .yangIetfNetworkOpType(OnosYangOpType.NONE)
+                .build();
 
         // processSubtreeFiltering() filters the entire data tree based on the
         // user's query and returns the filtered data.
-        return ietfNetwork.processSubtreeFiltering(
-                                                   (IetfNetworkOpParam) newNetwork,
-                                                   false);
+        IetfNetwork result = ietfNetwork.processSubtreeFiltering(
+                (IetfNetworkOpParam) newNetwork,
+                false);
+        return result;
     }
 
     @Override
@@ -143,7 +165,7 @@
     @Override
     public IetfNetworkTopology getIetfNetworkTopology(IetfNetworkTopologyOpParam ietfNetworkTopology) {
         // unused methods.
-       return ietfNetworkTopology;
+        return ietfNetworkTopology;
     }
 
     @Override
@@ -162,4 +184,11 @@
         // unused methods.
     }
 
+    private class InternalTeTopologyListener implements TeTopologyListener {
+        @Override
+        public void event(TeTopologyEvent event) {
+            IetfTeTopologyEvent yangEvent = NetworkConverter.teTopoEvent2YangIetfTeTopoEvent(event);
+            post(yangEvent);
+        }
+    }
 }
diff --git a/apps/tenbi/utils/src/main/java/org/onosproject/teyang/utils/topology/NetworkConverter.java b/apps/tenbi/utils/src/main/java/org/onosproject/teyang/utils/topology/NetworkConverter.java
index 74eed10..cb24d60 100644
--- a/apps/tenbi/utils/src/main/java/org/onosproject/teyang/utils/topology/NetworkConverter.java
+++ b/apps/tenbi/utils/src/main/java/org/onosproject/teyang/utils/topology/NetworkConverter.java
@@ -21,6 +21,7 @@
 import java.util.List;
 
 import org.onosproject.tetopology.management.api.KeyId;
+import org.onosproject.tetopology.management.api.TeTopologyEvent;
 import org.onosproject.tetopology.management.api.link.NetworkLink;
 import org.onosproject.tetopology.management.api.node.NetworkNode;
 import org.onosproject.teyang.api.OperationType;
@@ -71,6 +72,7 @@
 import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.ietftetopology.tetopologyaugment.Te;
 import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.ietftetopology.tetopologytype.DefaultTeTopology;
 import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.ietftetopology.tetopologytype.TeTopology;
+import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.ietftetopology.IetfTeTopologyEvent;
 import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705
         .ietftetypes.TeGlobalId;
 import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705
@@ -479,6 +481,18 @@
 
         return teNetwork;
     }
+
+    /**
+     * Converts a TE Topology event from the data format used in core to its
+     * corresponding YANG Object (YO) format.
+     *
+     * @param event TE Topology event from the core
+     * @return YANG Object converted from event
+     */
+    public static IetfTeTopologyEvent teTopoEvent2YangIetfTeTopoEvent(TeTopologyEvent event) {
+      // TODO: implementation will be submitted as a seperate review.
+      return null;
+    }
 }
 
 
diff --git a/apps/tenbi/utils/src/test/java/org/onosproject/teyang/utils/topology/DefaultBuilder.java b/apps/tenbi/utils/src/test/java/org/onosproject/teyang/utils/topology/DefaultBuilder.java
index c078d1b..faefdd8 100644
--- a/apps/tenbi/utils/src/test/java/org/onosproject/teyang/utils/topology/DefaultBuilder.java
+++ b/apps/tenbi/utils/src/test/java/org/onosproject/teyang/utils/topology/DefaultBuilder.java
@@ -13,11 +13,10 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 package org.onosproject.teyang.utils.topology;
 
-import java.math.BigInteger;
-import java.util.List;
-
+import com.google.common.collect.Lists;
 import org.onosproject.tetopology.management.api.DefaultNetwork;
 import org.onosproject.tetopology.management.api.DefaultNetworks;
 import org.onosproject.tetopology.management.api.KeyId;
@@ -39,7 +38,8 @@
 import org.onosproject.tetopology.management.api.node.TerminationPoint;
 import org.onosproject.tetopology.management.api.node.TerminationPointKey;
 
-import com.google.common.collect.Lists;
+import java.math.BigInteger;
+import java.util.List;
 
 /**
  * Builds a sample Topology, which consists of two Nodes, one link,
@@ -123,8 +123,8 @@
         node2.setTerminationPoints(tps2);
 
         TeNode teNode2 = new TeNode(NODE2_IP);
-        teNode1.setAdminStatus(TeStatus.UP);
-        teNode1.setAbstract(false);
+        teNode2.setAdminStatus(TeStatus.UP);
+        teNode2.setAbstract(false);
         ConnectivityMatrix cMatrix2 =
                 new ConnectivityMatrix(1,
                                        new TerminationPointKey(huaweiNetworkNew.networkId(),
@@ -133,8 +133,8 @@
                                                                node2.nodeId(), tp22.id()),
                                        true);
         List<ConnectivityMatrix> connMatrices2 = Lists.newArrayList();
-        connMatrices.add(cMatrix2);
-        teNode1.setConnectivityMatrices(connMatrices2);
+        connMatrices2.add(cMatrix2);
+        teNode2.setConnectivityMatrices(connMatrices2);
         node2.setTe(teNode2);
 
         nodes.add(node1);
@@ -146,10 +146,10 @@
         DefaultNetworkLink link1 = new DefaultNetworkLink(KeyId.keyId(LINK1FORNETWORK1));
         link1.setSource(new TerminationPointKey(huaweiNetworkNew.networkId(),
                                                 node1.nodeId(),
-                                                tp11.id()));
+                                                tp12.id()));
         link1.setDestination(new TerminationPointKey(huaweiNetworkNew.networkId(),
                                                      node2.nodeId(),
-                                                     tp21.id()));
+                                                     tp22.id()));
         TeLink teLink1 = new TeLink(BigInteger.valueOf(1));
         teLink1.setIsAbstract(false);
         teLink1.setAdminStatus(TeStatus.UP);