Move PCE label handling from APP to protocol.

Change-Id: I26ae21b27ac2dc9ae3302030f6860e0e371c342c
diff --git a/providers/pcep/tunnel/src/main/java/org/onosproject/provider/pcep/tunnel/impl/PcepTunnelProvider.java b/providers/pcep/tunnel/src/main/java/org/onosproject/provider/pcep/tunnel/impl/PcepTunnelProvider.java
index f821f21..767d026 100644
--- a/providers/pcep/tunnel/src/main/java/org/onosproject/provider/pcep/tunnel/impl/PcepTunnelProvider.java
+++ b/providers/pcep/tunnel/src/main/java/org/onosproject/provider/pcep/tunnel/impl/PcepTunnelProvider.java
@@ -100,7 +100,6 @@
 import org.onosproject.pcepio.protocol.PcepMessage;
 import org.onosproject.pcepio.protocol.PcepMetricObject;
 import org.onosproject.pcepio.protocol.PcepMsgPath;
-import org.onosproject.pcepio.protocol.PcepNai;
 import org.onosproject.pcepio.protocol.PcepReportMsg;
 import org.onosproject.pcepio.protocol.PcepSrpObject;
 import org.onosproject.pcepio.protocol.PcepStateReport;
@@ -109,7 +108,6 @@
 import org.onosproject.pcepio.types.IPv4SubObject;
 import org.onosproject.pcepio.types.PathSetupTypeTlv;
 import org.onosproject.pcepio.types.PcepNaiIpv4Adjacency;
-import org.onosproject.pcepio.types.PcepNaiIpv4NodeId;
 import org.onosproject.pcepio.types.PcepValueType;
 import org.onosproject.pcepio.types.SrEroSubObject;
 import org.onosproject.pcepio.types.StatefulIPv4LspIdentifiersTlv;
@@ -124,7 +122,6 @@
 import java.util.Collections;
 import java.util.Dictionary;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.ListIterator;
@@ -280,7 +277,6 @@
             collectors.values().forEach(tsc -> tsc.adjustPollInterval(tunnelStatsPollFrequency));
             log.info("New setting: tunnelStatsPollFrequency={}", tunnelStatsPollFrequency);
         }
-
     }
 
     @Override
@@ -318,8 +314,27 @@
         //TODO: tunnel which is passed doesn't have tunnelID
         if (tunnel.annotations().value(PLSP_ID) != null) {
             if (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) != WITHOUT_SIGNALLING_AND_WITHOUT_SR) {
-                // For CR LSPs, BGP flow provider will send update message after pushing labels.
                 updateTunnel(tunnel, path);
+            } else {
+                // Download labels and send update message.
+                // To get new tunnel ID (modified tunnel ID)
+                Collection<Tunnel> tunnels = tunnelService.queryTunnel(tunnel.src(), tunnel.dst());
+                for (Tunnel t : tunnels) {
+                    if (t.state().equals(INIT) && t.tunnelName().equals(tunnel.tunnelName())) {
+                        tunnel = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
+                                tunnel.dst(), tunnel.type(),
+                                t.state(), tunnel.groupId(),
+                                t.tunnelId(),
+                                tunnel.tunnelName(),
+                                tunnel.path(),
+                                tunnel.resource(),
+                                tunnel.annotations());
+                                break;
+                    }
+                }
+                if (!pcepClientController.allocateLocalLabel(tunnel)) {
+                    log.error("Unable to allocate labels for the tunnel {}.", tunnel.toString());
+                }
             }
             return;
         }
@@ -806,13 +821,11 @@
                                                    extendAnnotations);
 
         }
-        TunnelDescription tunnel = new DefaultTunnelDescription(
-                                                                tunnelId,
+        TunnelDescription tunnel = new DefaultTunnelDescription(tunnelId,
                                                                 srcPoint,
                                                                 dstPoint,
                                                                 tunnelType,
-                                                                new DefaultGroupId(
-                                                                                   0),
+                                                                new DefaultGroupId(0),
                                                                 id(), name,
                                                                 path,
                                                                 annotations);
@@ -904,55 +917,6 @@
     }
 
     /**
-     * Creates label stack for ERO object from network resource.
-     *
-     * @param labelStack
-     * @param path (hop list)
-     * @return list of ERO subobjects
-     */
-    private LinkedList<PcepValueType> createPcepLabelStack(DefaultLabelStack labelStack, Path path) {
-        checkNotNull(labelStack);
-
-        LinkedList<PcepValueType> llSubObjects = new LinkedList<PcepValueType>();
-        Iterator<Link> links = path.links().iterator();
-        LabelResourceId label = null;
-        Link link = null;
-        PcepValueType subObj = null;
-        PcepNai nai = null;
-        Device dstNode = null;
-        long srcPortNo, dstPortNo;
-
-        ListIterator<LabelResourceId> labelListIterator = labelStack.labelResources().listIterator();
-        while (labelListIterator.hasNext()) {
-            label = labelListIterator.next();
-            link = links.next();
-
-            srcPortNo = link.src().port().toLong();
-            srcPortNo = ((srcPortNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? srcPortNo & SET : srcPortNo;
-
-            dstPortNo = link.dst().port().toLong();
-            dstPortNo = ((dstPortNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? dstPortNo & SET : dstPortNo;
-
-            nai = new PcepNaiIpv4Adjacency((int) srcPortNo, (int) dstPortNo);
-            subObj = new SrEroSubObject(PcepNaiIpv4Adjacency.ST_TYPE, false, false, false, true, (int) label.labelId(),
-                                        nai);
-            llSubObjects.add(subObj);
-
-            dstNode = deviceService.getDevice(link.dst().deviceId());
-            nai = new PcepNaiIpv4NodeId(Ip4Address.valueOf(dstNode.annotations().value(LSRID)).toInt());
-
-            if (!labelListIterator.hasNext()) {
-                log.error("Malformed label stack.");
-            }
-            label = labelListIterator.next();
-            subObj = new SrEroSubObject(PcepNaiIpv4NodeId.ST_TYPE, false, false, false, true, (int) label.labelId(),
-                                        nai);
-            llSubObjects.add(subObj);
-        }
-        return llSubObjects;
-    }
-
-    /**
      * Creates PcInitiated lsp request list for setup tunnel.
      *
      * @param tunnel mpls tunnel
@@ -967,10 +931,18 @@
                                                                           throws PcepParseException {
         PcepValueType tlv;
         LinkedList<PcepValueType> llSubObjects = null;
+        LspType lspType = LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE));
 
-        NetworkResource labelStack = tunnel.resource();
-        if (labelStack != null && labelStack instanceof DefaultLabelStack) {
-            llSubObjects = createPcepLabelStack((DefaultLabelStack) labelStack, path);
+        if (lspType == SR_WITHOUT_SIGNALLING) {
+            NetworkResource labelStack = tunnel.resource();
+            if (labelStack == null || !(labelStack instanceof DefaultLabelStack)) {
+                labelStack = pcepClientController.computeLabelStack(tunnel.path());
+                if (labelStack == null) {
+                    log.error("Unable to create label stack.");
+                    return null;
+                }
+            }
+            llSubObjects = pcepClientController.createPcepLabelStack((DefaultLabelStack) labelStack, path);
         } else {
             llSubObjects = createPcepPath(path);
         }
@@ -983,7 +955,7 @@
         LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
 
         // set PathSetupTypeTlv of SRP object
-        tlv = new PathSetupTypeTlv(LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)).type());
+        tlv = new PathSetupTypeTlv(lspType.type());
         llOptionalTlv.add(tlv);
 
         // build SRP object
@@ -1115,7 +1087,6 @@
             PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, DELETE);
             pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
             int srpId = SrpIdGenerators.create();
-            TunnelId tunnelId = tunnel.tunnelId();
 
             PcepValueType tlv;
             LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
@@ -1193,14 +1164,21 @@
             PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, UPDATE);
             pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
             int srpId = SrpIdGenerators.create();
-            TunnelId tunnelId = tunnel.tunnelId();
             PcepValueType tlv;
-            int plspId = 0;
 
             LinkedList<PcepValueType> llSubObjects = null;
-            NetworkResource labelStack = tunnel.resource();
-            if (labelStack != null && labelStack instanceof DefaultLabelStack) {
-                llSubObjects = createPcepLabelStack((DefaultLabelStack) labelStack, path);
+            LspType lspSigType = LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE));
+
+            if (lspSigType == SR_WITHOUT_SIGNALLING) {
+                NetworkResource labelStack = tunnel.resource();
+                if (labelStack == null || !(labelStack instanceof DefaultLabelStack)) {
+                    labelStack = pcepClientController.computeLabelStack(tunnel.path());
+                    if (labelStack == null) {
+                        log.error("Unable to create label stack.");
+                        return;
+                    }
+                }
+                llSubObjects = pcepClientController.createPcepLabelStack((DefaultLabelStack) labelStack, path);
             } else {
                 llSubObjects = createPcepPath(path);
             }
@@ -1209,7 +1187,6 @@
             LinkedList<PcepUpdateRequest> llUpdateRequestList = new LinkedList<PcepUpdateRequest>();
 
             // set PathSetupTypeTlv of SRP object
-            LspType lspSigType = LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE));
             tlv = new PathSetupTypeTlv(lspSigType.type());
             llOptionalTlv.add(tlv);
 
@@ -1285,8 +1262,6 @@
         }
     }
 
-
-
     private class InnerTunnelProvider implements PcepTunnelListener, PcepEventListener, PcepClientListener {
 
         @Override
@@ -1302,7 +1277,6 @@
             }
 
             TunnelId tunnelId = getTunnelId(tunnelKey);
-
             tunnel = buildOpticalTunnel(pcepTunnel, tunnelId);
 
             OperationType operType = pcepTunnel.getOperationType();
@@ -1737,7 +1711,7 @@
         private void tunnelUpdateInDelegatedCase(PccId pccId, SparseAnnotations annotations,
                 DefaultTunnelDescription td, ProviderId providerId, State tunnelState,
                                                  StatefulIPv4LspIdentifiersTlv ipv4LspIdentifiersTlv) {
-            //Wait for 2sec then query tunnel based on ingress PLSP-ID and local LSP-ID
+            // Wait for 2sec then query tunnel based on ingress PLSP-ID and local LSP-ID
 
             /*
              * If ONOS is not the master for that PCC then check if D flag is set, if yes wait [while
@@ -1891,14 +1865,11 @@
 
 
             if (endOfSyncAction == PcepLspSyncAction.UNSTABLE) {
-
                 // Send PCInit msg again after global reoptimization.
                 tunnelUpdated(td, UNSTABLE);
 
-                // To remove the old tunnel from store whose PLSPID is not
-                // recognized by ingress PCC.
+                // To remove the old tunnel from store whose PLSPID is not recognized by ingress PCC.
                 tunnelRemoved(td);
-
             } else if (endOfSyncAction == REMOVE) {
                 tunnelRemoved(td);
             }