[ONOS-4687] GoldenEye - Label map download issues.

Change-Id: I5b56ccc6b8bad3c585f4d33ddd6d9094751c8797
diff --git a/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/PceManager.java b/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/PceManager.java
index b415534..41e14dd 100644
--- a/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/PceManager.java
+++ b/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/PceManager.java
@@ -893,9 +893,7 @@
 
         // Check whether device has SR-TE Capability
         if (cfg.labelStackCap()) {
-            if (!srTeHandler.allocateNodeLabel(deviceId, lsrId)) {
-                log.error("Node label allocation for a device id {} has failed.", deviceId.toString());
-            }
+            srTeHandler.allocateNodeLabel(deviceId, lsrId);
         }
     }
 
@@ -974,10 +972,7 @@
 
         // Check whether device has SR-TE Capability
         if (cfg.labelStackCap()) {
-            if (!srTeHandler.allocateAdjacencyLabel(link)) {
-                log.error("Unable to allocate adjacency label for a link {}.", link.toString());
-                return;
-            }
+            srTeHandler.allocateAdjacencyLabel(link);
         }
 
         return;
@@ -1209,16 +1204,13 @@
                 if (cfg.labelStackCap()) {
                     if (mastershipService.getLocalRole(deviceId) == MastershipRole.MASTER) {
                         // Allocate node-label
-                        if (!srTeHandler.allocateNodeLabel(deviceId, lsrId)) {
-                            log.error("Node label allocation for a device id {} has failed.", deviceId.toString());
-                        }
+                        srTeHandler.allocateNodeLabel(deviceId, lsrId);
 
                         // Allocate adjacency label to links which are
                         // originated from this specific device id
                         Set<Link> links = linkService.getDeviceEgressLinks(deviceId);
                         for (Link link : links) {
                             if (!srTeHandler.allocateAdjacencyLabel(link)) {
-                                log.debug("Unable to allocate adjacency labels for a link {}.", link.toString());
                                 return;
                             }
                         }
@@ -1240,40 +1232,42 @@
             return false;
         }
 
-        Map<DeviceId, LabelResourceId> globalNodeLabelMap = pceStore.getGlobalNodeLabels();
+        if (pceStore.getGlobalNodeLabel(deviceId) != null) {
+            Map<DeviceId, LabelResourceId> globalNodeLabelMap = pceStore.getGlobalNodeLabels();
 
-        for (Entry<DeviceId, LabelResourceId> entry : globalNodeLabelMap.entrySet()) {
+            for (Entry<DeviceId, LabelResourceId> entry : globalNodeLabelMap.entrySet()) {
 
-            // Convert from DeviceId to TunnelEndPoint
-            Device srcDevice = deviceService.getDevice(entry.getKey());
+                // Convert from DeviceId to TunnelEndPoint
+                Device srcDevice = deviceService.getDevice(entry.getKey());
 
-            /*
-             * If there is a slight difference in timing such that if device subsystem has removed the device but PCE
-             * store still has it, just ignore such devices.
-             */
-            if (srcDevice == null) {
-                continue;
+                /*
+                 * If there is a slight difference in timing such that if device subsystem has removed the device but
+                 * PCE store still has it, just ignore such devices.
+                 */
+                if (srcDevice == null) {
+                    continue;
+                }
+
+                String srcLsrId = srcDevice.annotations().value(LSRID);
+                if (srcLsrId == null) {
+                    continue;
+                }
+
+                srTeHandler.advertiseNodeLabelRule(deviceId,
+                                                   entry.getValue(),
+                                                   IpPrefix.valueOf(IpAddress.valueOf(srcLsrId), PREFIX_LENGTH),
+                                                   Objective.Operation.ADD, false);
             }
 
-            String srcLsrId = srcDevice.annotations().value(LSRID);
-            if (srcLsrId == null) {
-                continue;
-            }
-
-            srTeHandler.advertiseNodeLabelRule(deviceId,
-                                               entry.getValue(),
-                                               IpPrefix.valueOf(IpAddress.valueOf(srcLsrId), PREFIX_LENGTH),
-                                               Objective.Operation.ADD, false);
-        }
-
-        Map<Link, LabelResourceId> adjLabelMap = pceStore.getAdjLabels();
-        for (Entry<Link, LabelResourceId> entry : adjLabelMap.entrySet()) {
-            if (entry.getKey().src().deviceId().equals(deviceId)) {
-                srTeHandler.installAdjLabelRule(deviceId,
-                                                entry.getValue(),
-                                                entry.getKey().src().port(),
-                                                entry.getKey().dst().port(),
-                                                Objective.Operation.ADD);
+            Map<Link, LabelResourceId> adjLabelMap = pceStore.getAdjLabels();
+            for (Entry<Link, LabelResourceId> entry : adjLabelMap.entrySet()) {
+                if (entry.getKey().src().deviceId().equals(deviceId)) {
+                    srTeHandler.installAdjLabelRule(deviceId,
+                                                    entry.getValue(),
+                                                    entry.getKey().src().port(),
+                                                    entry.getKey().dst().port(),
+                                                    Objective.Operation.ADD);
+                }
             }
         }
 
diff --git a/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/PceccSrTeBeHandler.java b/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/PceccSrTeBeHandler.java
index c760bc1..63eed1b 100644
--- a/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/PceccSrTeBeHandler.java
+++ b/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/PceccSrTeBeHandler.java
@@ -28,7 +28,6 @@
 import org.onlab.packet.IpAddress;
 import org.onlab.packet.IpPrefix;
 import org.onlab.packet.MplsLabel;
-import org.onlab.packet.TpPort;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.incubator.net.resource.label.DefaultLabelResource;
 import org.onosproject.incubator.net.resource.label.LabelResource;
@@ -184,7 +183,7 @@
 
         // Check whether node-label was already configured for this specific device.
         if (pceStore.getGlobalNodeLabel(specificDeviceId) != null) {
-            log.error("Node label was already configured for device {}.", specificDeviceId.toString());
+            log.debug("Node label was already configured for device {}.", specificDeviceId.toString());
             return false;
         }
 
@@ -228,12 +227,15 @@
 
             // Push to device
             // Push label information of specificDeviceId to otherDevId in list and vice versa.
-            advertiseNodeLabelRule(otherDevId, specificLabelId,
-                                   IpPrefix.valueOf(IpAddress.valueOf(specificLsrId), PREFIX_LENGTH),
-                                   Objective.Operation.ADD, false);
-            advertiseNodeLabelRule(specificDeviceId, otherLabelId,
-                                   IpPrefix.valueOf(IpAddress.valueOf(otherLsrId), PREFIX_LENGTH),
-                                   Objective.Operation.ADD, false);
+            if (!otherDevId.equals(specificDeviceId)) {
+                advertiseNodeLabelRule(otherDevId, specificLabelId,
+                                       IpPrefix.valueOf(IpAddress.valueOf(specificLsrId), PREFIX_LENGTH),
+                                       Objective.Operation.ADD, false);
+
+                advertiseNodeLabelRule(specificDeviceId, otherLabelId,
+                                       IpPrefix.valueOf(IpAddress.valueOf(otherLsrId), PREFIX_LENGTH),
+                                       Objective.Operation.ADD, false);
+            }
         }
 
         return true;
@@ -519,8 +521,8 @@
         TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
 
         selectorBuilder.matchMplsLabel(MplsLabel.mplsLabel(labelId.id().intValue()));
-        selectorBuilder.matchTcpSrc(TpPort.tpPort((int) srcPortNum.toLong()));
-        selectorBuilder.matchTcpDst(TpPort.tpPort((int) dstPortNum.toLong()));
+        selectorBuilder.matchIPSrc(IpPrefix.valueOf((int) srcPortNum.toLong(), 32));
+        selectorBuilder.matchIPDst(IpPrefix.valueOf((int) dstPortNum.toLong(), 32));
 
         TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
 
diff --git a/apps/pce/app/src/main/java/org/onosproject/pce/pcestore/DistributedPceStore.java b/apps/pce/app/src/main/java/org/onosproject/pce/pcestore/DistributedPceStore.java
index 1c1aece..6afdb30 100644
--- a/apps/pce/app/src/main/java/org/onosproject/pce/pcestore/DistributedPceStore.java
+++ b/apps/pce/app/src/main/java/org/onosproject/pce/pcestore/DistributedPceStore.java
@@ -219,19 +219,19 @@
     @Override
     public LabelResourceId getGlobalNodeLabel(DeviceId id) {
         checkNotNull(id, DEVICE_ID_NULL);
-        return globalNodeLabelMap.get(id).value();
+        return globalNodeLabelMap.get(id) == null ? null : globalNodeLabelMap.get(id).value();
     }
 
     @Override
     public LabelResourceId getAdjLabel(Link link) {
         checkNotNull(link, LINK_NULL);
-        return adjLabelMap.get(link).value();
+        return adjLabelMap.get(link) == null ? null : adjLabelMap.get(link).value();
     }
 
     @Override
     public PceccTunnelInfo getTunnelInfo(TunnelId tunnelId) {
         checkNotNull(tunnelId, TUNNEL_ID_NULL);
-        return tunnelInfoMap.get(tunnelId).value();
+        return tunnelInfoMap.get(tunnelId) == null ? null : tunnelInfoMap.get(tunnelId).value();
     }
 
     @Override