pass teTopologyService as a parameter
add code to retrieve linkRef, nodeRef and underlayTopologyId

Change-Id: I9711fbd0d20da940ffc53c842111587f9eb9e833
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 fc2fdee..726f84d 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
@@ -196,7 +196,7 @@
         @Override
         public void event(TeTopologyEvent event) {
             IetfTeTopologyEvent yangEvent = NetworkConverter
-                    .teTopoEvent2YangIetfTeTopoEvent(event);
+                    .teTopoEvent2YangIetfTeTopoEvent(event, teTopologyService);
             post(yangEvent);
         }
     }
diff --git a/apps/tenbi/utils/src/main/java/org/onosproject/teyang/utils/topology/LinkConverter.java b/apps/tenbi/utils/src/main/java/org/onosproject/teyang/utils/topology/LinkConverter.java
index 6a9cb94..b1c36db52 100644
--- a/apps/tenbi/utils/src/main/java/org/onosproject/teyang/utils/topology/LinkConverter.java
+++ b/apps/tenbi/utils/src/main/java/org/onosproject/teyang/utils/topology/LinkConverter.java
@@ -283,9 +283,10 @@
      * TE Link State object conversion from TE Topology subsystem to YANG.
      *
      * @param teLink TE link object
+     * @param teTopologyService TE Topology Service object
      * @return TE Link State YANG object
      */
-    private static State teLink2YangState(TeLink teLink) {
+    private static State teLink2YangState(TeLink teLink, TeTopologyService teTopologyService) {
         TeLinkAttributes
             .TeLinkAttributesBuilder attrBuilder =
                         DefaultTeLinkAttributes
@@ -365,7 +366,11 @@
             InformationSourceState.InformationSourceStateBuilder issBuilder = DefaultInformationSourceState.builder();
 
             Topology.TopologyBuilder topologyBuilder = DefaultTopology.builder();
-            // TODO: pass teTopologyService and retrieve linkRef and NetworkRef from there
+            topologyBuilder = topologyBuilder
+                    .linkRef(teTopologyService.linkKey(teLink.sourceTeLinkId())
+                            .linkId())
+                    .networkRef(teTopologyService
+                            .linkKey(teLink.sourceTeLinkId()).networkId());
 
             issBuilder = issBuilder.topology(topologyBuilder.build());
             yangStateBuilder.informationSourceState(issBuilder.build());
@@ -429,10 +434,8 @@
             TeLink teData = teSubsNetworkLink.teLink();
             TeBuilder yangTeBuilder = DefaultTe.builder()
                                                .config(teLink2YangConfig(teData, teTopologyService))
-                                               .state(teLink2YangState(teData));
-            // ignoring supportingTeLinkId when converting from core to yang?
-            // if (teData.supportingTeLinkId() != null) {
-            // }
+                                               .state(teLink2YangState(teData, teTopologyService));
+
             AugmentedNtLinkBuilder linkAugmentBuilder =
                     DefaultAugmentedNtLink.builder()
                                           .te(yangTeBuilder.build());
@@ -580,8 +583,33 @@
      * @return TeTopologyKey the TE TopologyKey
      */
     public static TeTopologyKey findTopologyId(Networks yangNetworks, Object networkRef) {
-        // TODO: add implementation
-        return null;
+        if (networkRef == null) {
+            return null;
+        }
+        NetworkId networkId = NetworkId.fromString((String) networkRef);
+
+        TeTopologyKey topologyId = null;
+        Network targetTeNetwork = null;
+        if (yangNetworks.network() != null
+                && !yangNetworks.network().isEmpty() && networkId != null) {
+            for (Network ynetItem : yangNetworks.network()) {
+                if (ynetItem.networkId() != null) {
+                    if (ynetItem.networkId().equals(networkId)) {
+                        targetTeNetwork = ynetItem;
+                        break;
+                    }
+                }
+            }
+        }
+        if (targetTeNetwork != null && targetTeNetwork
+                .yangAugmentedInfo(AugmentedNwNetwork.class) != null) {
+            AugmentedNwNetwork augmentTeIds = (AugmentedNwNetwork) targetTeNetwork
+                    .yangAugmentedInfo(AugmentedNwNetwork.class);
+            topologyId = new TeTopologyKey(augmentTeIds.providerId().uint32(),
+                                                   augmentTeIds.clientId().uint32(),
+                                                   Long.valueOf(augmentTeIds.teTopologyId().string()));
+        }
+        return topologyId;
     }
 
     private static TeLink yangLinkAttr2TeLinkAttributes(TeLinkAttributes yangLinkAtrr,
@@ -975,10 +1003,12 @@
      *
      * @param eventType Link event type
      * @param linkData  TE Topology link event data
+     * @param teTopologyService TE Topology Service object
      * @return YANG Object converted from linkData
      */
     public static TeLinkEvent teNetworkLink2yangTeLinkEvent(TeTopologyEventTypeEnum eventType,
-                                                            NetworkLinkEventSubject linkData) {
+                                                            NetworkLinkEventSubject linkData,
+                                                            TeTopologyService teTopologyService) {
         TeLinkEvent.TeLinkEventBuilder teLinkEventBuilder = new DefaultTeLinkEvent.TeLinkEventBuilder();
 
         TeTopologyEventType yangEventType = new TeTopologyEventType(eventType);
@@ -990,7 +1020,7 @@
 
         if (linkData != null && linkData.networkLink() != null) {
             NetworkLink link = linkData.networkLink();
-            State yangTeLinkState = teLink2YangState(link.teLink());
+            State yangTeLinkState = teLink2YangState(link.teLink(), teTopologyService);
 
             teLinkEventBuilder.operStatus(yangTeLinkState.operStatus());
             teLinkEventBuilder.informationSource(yangTeLinkState.informationSource());
@@ -1088,7 +1118,20 @@
         TeLinkTpKey peerTeLinkKey = oldTeLink.peerTeLinkKey();
 
         TeTopologyKey underlayTopologyId = null;
-        // TODO: add code to retrieve underlayTopologyId
+        KeyId networkRef = null;
+        if (yangTeLinkAttrs.underlay() != null &&
+                yangTeLinkAttrs.underlay().primaryPath() != null &&
+                yangTeLinkAttrs.underlay().primaryPath().networkRef() != null) {
+            networkRef = (KeyId) yangTeLinkAttrs.underlay().primaryPath().networkRef();
+        }
+
+        if (networkRef != null && teTopologyService.network(networkRef) != null
+                && teTopologyService.network(networkRef).teTopologyId() != null) {
+            long clientId = teTopologyService.network(networkRef).teTopologyId().clientId();
+            long providerId = teTopologyService.network(networkRef).teTopologyId().providerId();
+            long topologyId = Long.valueOf(teTopologyService.network(networkRef).teTopologyId().topologyId());
+            underlayTopologyId = new TeTopologyKey(providerId, clientId, topologyId);
+        }
 
         TeLink updatedTeLink = yangLinkAttr2TeLinkAttributes(yangTeLinkAttrs, opStatus, teNodeIdSrc, teNodeIdDest,
                                                              teLinkKey,
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 631cbc8..9437d4a1 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
@@ -86,7 +86,11 @@
         .tetopologytype.DefaultTeTopology;
 import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20170110.ietftetopology
         .tetopologytype.TeTopology;
+import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes.Cost;
+import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes.Delay;
+import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes.NotOptimized;
 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.ietftetypes.TeOptimizationCriterion;
 import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes.TeTopologyId;
 import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes.tetopologyeventtype.TeTopologyEventTypeEnum;
 import org.slf4j.Logger;
@@ -324,7 +328,20 @@
         TeTopologyKey teTopoKey = new TeTopologyKey(nt.teTopologyId().providerId(),
                                                     nt.teTopologyId().clientId(),
                                                     Long.valueOf(nt.teTopologyId().topologyId()));
-        // TODO: add code to set configBuilder.optimizationCriterion
+        switch (teTopologyService.teTopology(teTopoKey).optimization()) {
+        case LEAST_COST:
+            configBuilder = configBuilder.optimizationCriterion(Cost.class);
+            break;
+        case SHORTEST_DELAY:
+            configBuilder = configBuilder.optimizationCriterion(Delay.class);
+            break;
+        case NOT_OPTIMIZED:
+            configBuilder = configBuilder.optimizationCriterion(NotOptimized.class);
+            break;
+        default:
+            configBuilder = configBuilder.optimizationCriterion(TeOptimizationCriterion.class);
+            break;
+        }
 
         teBuilder = teBuilder.config(configBuilder.build());
 
@@ -549,9 +566,11 @@
      * corresponding YANG Object (YO) format.
      *
      * @param event TE Topology event from the core
+     * @param teTopologyService TE Topology Service object
      * @return YANG Object converted from event
      */
-    public static IetfTeTopologyEvent teTopoEvent2YangIetfTeTopoEvent(TeTopologyEvent event) {
+    public static IetfTeTopologyEvent teTopoEvent2YangIetfTeTopoEvent(TeTopologyEvent event,
+                                                                      TeTopologyService teTopologyService) {
         IetfTeTopologyEvent yangEvent = null;
         IetfTeTopologyEventSubject eventSubject = new IetfTeTopologyEventSubject();
 
@@ -560,7 +579,7 @@
             NetworkLinkEventSubject eventData = (NetworkLinkEventSubject) event.subject();
             TeTopologyEventTypeEnum linkEventType = teTopoEventType2YangTeTopoEventType(event.type());
             TeLinkEvent yangLinkEvent = LinkConverter
-                    .teNetworkLink2yangTeLinkEvent(linkEventType, eventData);
+                    .teNetworkLink2yangTeLinkEvent(linkEventType, eventData, teTopologyService);
             eventSubject.teLinkEvent(yangLinkEvent);
             yangEvent = new IetfTeTopologyEvent(IetfTeTopologyEvent.Type.TE_LINK_EVENT, eventSubject);
         } else if (yangEventType == IetfTeTopologyEvent.Type.TE_NODE_EVENT) {