[ONOS-4164] PceManager device and link event handling

Change-Id: I55d8f60e0fcc0e23f4c14045a8e20bd89f635913
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 aa16f80..2e148db 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
@@ -19,6 +19,7 @@
 
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
@@ -60,6 +61,8 @@
 import org.onosproject.incubator.net.tunnel.TunnelName;
 import org.onosproject.incubator.net.tunnel.TunnelService;
 import org.onosproject.mastership.MastershipService;
+import org.onosproject.net.config.NetworkConfigEvent;
+import org.onosproject.net.config.NetworkConfigListener;
 import org.onosproject.net.config.NetworkConfigService;
 import org.onosproject.net.DefaultAnnotations;
 import org.onosproject.net.DefaultAnnotations.Builder;
@@ -67,12 +70,17 @@
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.Link;
 import org.onosproject.net.Path;
+import org.onosproject.net.device.DeviceEvent;
+import org.onosproject.net.device.DeviceListener;
 import org.onosproject.net.device.DeviceService;
 import org.onosproject.net.flowobjective.FlowObjectiveService;
 import org.onosproject.net.flowobjective.Objective;
 import org.onosproject.net.intent.Constraint;
 import org.onosproject.net.intent.constraint.BandwidthConstraint;
+import org.onosproject.net.link.LinkListener;
 import org.onosproject.net.link.LinkEvent;
+import org.onosproject.net.link.LinkService;
+import org.onosproject.net.MastershipRole;
 import org.onosproject.pce.pceservice.constraint.CapabilityConstraint;
 import org.onosproject.pce.pceservice.constraint.CapabilityConstraint.CapabilityType;
 import org.onosproject.pce.pceservice.constraint.CostConstraint;
@@ -134,8 +142,14 @@
 public class PceManager implements PceService {
     private static final Logger log = LoggerFactory.getLogger(PceManager.class);
 
+    public static final long GLOBAL_LABEL_SPACE_MIN = 4097;
+    public static final long GLOBAL_LABEL_SPACE_MAX = 5121;
+    private static final String DEVICE_NULL = "Device-cannot be null";
+    private static final String LINK_NULL = "Link-cannot be null";
     public static final String PCE_SERVICE_APP = "org.onosproject.pce";
     private static final String LOCAL_LSP_ID_GEN_TOPIC = "pcep-local-lsp-id";
+    public static final String DEVICE_TYPE = "type";
+    public static final String L3_DEVICE = "L3";
     private static final int PREFIX_LENGTH = 32;
 
     private static final String TUNNEL_CONSUMER_ID_GEN_TOPIC = "pcep-tunnel-consumer-id";
@@ -150,6 +164,10 @@
     private IdGenerator localLspIdIdGen;
     protected DistributedSet<Short> localLspIdFreeList;
 
+    // LSR-id and device-id mapping for checking capability if L3 device is not
+    // having its capability
+    private Map<String, DeviceId> lsrIdDeviceIdMap = new HashMap<>();
+
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected CoreService coreService;
 
@@ -178,6 +196,9 @@
     protected DeviceService deviceService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected LinkService linkService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected NetworkConfigService netCfgService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
@@ -196,6 +217,9 @@
     protected TopologyService topologyService;
 
     private TunnelListener listener = new InnerTunnelListener();
+    private DeviceListener deviceListener = new InternalDeviceListener();
+    private LinkListener linkListener = new InternalLinkListener();
+    private InternalConfigListener cfgListener = new InternalConfigListener();
     private BasicPceccHandler crHandler;
     private PceccSrTeBeHandler srTeHandler;
     private ApplicationId appId;
@@ -220,9 +244,13 @@
         crHandler.initialize(labelRsrcService, flowObjectiveService, appId, pceStore);
 
         srTeHandler = PceccSrTeBeHandler.getInstance();
-        srTeHandler.initialize(labelRsrcAdminService, labelRsrcService, flowObjectiveService, appId, pceStore);
+        srTeHandler.initialize(labelRsrcAdminService, labelRsrcService, flowObjectiveService, appId, pceStore,
+                               deviceService);
 
         tunnelService.addListener(listener);
+        deviceService.addListener(deviceListener);
+        linkService.addListener(linkListener);
+        netCfgService.addListener(cfgListener);
 
         tunnelConsumerIdGen = coreService.getIdGenerator(TUNNEL_CONSUMER_ID_GEN_TOPIC);
         localLspIdIdGen = coreService.getIdGenerator(LOCAL_LSP_ID_GEN_TOPIC);
@@ -237,15 +265,24 @@
         executor = Executors.newSingleThreadScheduledExecutor();
         //Start a timer when the component is up, with initial delay of 30min and periodic delays at 30min
         executor.scheduleAtFixedRate(new GlobalOptimizationTimer(), INITIAL_DELAY, PERIODIC_DELAY, TimeUnit.MINUTES);
+
+        // Reserve global node pool
+        if (!srTeHandler.reserveGlobalPool(GLOBAL_LABEL_SPACE_MIN, GLOBAL_LABEL_SPACE_MAX)) {
+            log.debug("Global node pool was already reserved.");
+        }
+
         log.info("Started");
     }
 
     @Deactivate
     protected void deactivate() {
         tunnelService.removeListener(listener);
+        deviceService.removeListener(deviceListener);
+        linkService.removeListener(linkListener);
+        netCfgService.removeListener(cfgListener);
         packetService.removeProcessor(processor);
         topologyService.removeListener(topologyListener);
-        //Shutdown the thread when component is deactivated
+        // Shutdown the thread when component is deactivated
         executor.shutdown();
         log.info("Stopped");
     }
@@ -819,6 +856,234 @@
             resource = Resources.continuous(link.src().deviceId(), link.src().port(), Bandwidth.class)
                     .resource(bandwidth);
             resourceService.allocate(consumer, resource); // Reusing new tunnel's TunnelConsumerId intentionally.
+
+        }
+    }
+
+    /**
+     * Allocates node label to specific device.
+     *
+     * @param specificDevice device to which node label needs to be allocated
+     */
+    public void allocateNodeLabel(Device specificDevice) {
+        checkNotNull(specificDevice, DEVICE_NULL);
+
+        DeviceId deviceId = specificDevice.id();
+
+        // Retrieve lsrId of a specific device
+        if (specificDevice.annotations() == null) {
+            log.debug("Device {} does not have annotations.", specificDevice.toString());
+            return;
+        }
+
+        String lsrId = specificDevice.annotations().value(LSRID);
+        if (lsrId == null) {
+            log.debug("Unable to retrieve lsr-id of a device {}.", specificDevice.toString());
+            return;
+        }
+
+        // Get capability config from netconfig
+        DeviceCapability cfg = netCfgService.getConfig(DeviceId.deviceId(lsrId), DeviceCapability.class);
+        if (cfg == null) {
+            log.error("Unable to find corresponding capability for a lsrd {} from NetConfig.", lsrId);
+            // Save info. When PCEP session is comes up then allocate node-label
+            lsrIdDeviceIdMap.put(lsrId, specificDevice.id());
+            return;
+        }
+
+        // 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());
+            }
+        }
+    }
+
+    /**
+     * Releases node label of a specific device.
+     *
+     * @param specificDevice this device label and lsr-id information will be
+     *            released in other existing devices
+     */
+    public void releaseNodeLabel(Device specificDevice) {
+        checkNotNull(specificDevice, DEVICE_NULL);
+
+        DeviceId deviceId = specificDevice.id();
+
+        // Retrieve lsrId of a specific device
+        if (specificDevice.annotations() == null) {
+            log.debug("Device {} does not have annotations.", specificDevice.toString());
+            return;
+        }
+
+        String lsrId = specificDevice.annotations().value(LSRID);
+        if (lsrId == null) {
+            log.debug("Unable to retrieve lsr-id of a device {}.", specificDevice.toString());
+            return;
+        }
+
+        // Get capability config from netconfig
+        DeviceCapability cfg = netCfgService.getConfig(DeviceId.deviceId(lsrId), DeviceCapability.class);
+        if (cfg == null) {
+            log.error("Unable to find corresponding capabilty for a lsrd {} from NetConfig.", lsrId);
+            return;
+        }
+
+        // Check whether device has SR-TE Capability
+        if (cfg.labelStackCap()) {
+            if (!srTeHandler.releaseNodeLabel(deviceId, lsrId)) {
+                log.error("Unable to release node label for a device id {}.", deviceId.toString());
+            }
+        }
+    }
+
+    /**
+     * Allocates adjacency label for a link.
+     *
+     * @param link link
+     */
+    public void allocateAdjacencyLabel(Link link) {
+        checkNotNull(link, LINK_NULL);
+
+        Device specificDevice = deviceService.getDevice(link.src().deviceId());
+        DeviceId deviceId = specificDevice.id();
+
+        // Retrieve lsrId of a specific device
+        if (specificDevice.annotations() == null) {
+            log.debug("Device {} does not have annotations.", specificDevice.toString());
+            return;
+        }
+
+        String lsrId = specificDevice.annotations().value(LSRID);
+        if (lsrId == null) {
+            log.debug("Unable to retrieve lsr-id of a device {}.", specificDevice.toString());
+            return;
+        }
+
+        // Get capability config from netconfig
+        DeviceCapability cfg = netCfgService.getConfig(DeviceId.deviceId(lsrId), DeviceCapability.class);
+        if (cfg == null) {
+            log.error("Unable to find corresponding capabilty for a lsrd {} from NetConfig.", lsrId);
+            // Save info. When PCEP session comes up then allocate adjacency
+            // label
+            if (lsrIdDeviceIdMap.get(lsrId) != null) {
+                lsrIdDeviceIdMap.put(lsrId, specificDevice.id());
+            }
+            return;
+        }
+
+        // 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;
+            }
+        }
+
+        return;
+    }
+
+    /**
+     * Releases allocated adjacency label of a link.
+     *
+     * @param link link
+     */
+    public void releaseAdjacencyLabel(Link link) {
+        checkNotNull(link, LINK_NULL);
+
+        Device specificDevice = deviceService.getDevice(link.src().deviceId());
+        DeviceId deviceId = specificDevice.id();
+
+        // Retrieve lsrId of a specific device
+        if (specificDevice.annotations() == null) {
+            log.debug("Device {} does not have annotations.", specificDevice.toString());
+            return;
+        }
+
+        String lsrId = specificDevice.annotations().value(LSRID);
+        if (lsrId == null) {
+            log.debug("Unable to retrieve lsr-id of a device {}.", specificDevice.toString());
+            return;
+        }
+
+        // Get capability config from netconfig
+        DeviceCapability cfg = netCfgService.getConfig(DeviceId.deviceId(lsrId), DeviceCapability.class);
+        if (cfg == null) {
+            log.error("Unable to find corresponding capabilty for a lsrd {} from NetConfig.", lsrId);
+            return;
+        }
+
+        // Check whether device has SR-TE Capability
+        if (cfg.labelStackCap()) {
+            if (!srTeHandler.releaseAdjacencyLabel(link)) {
+                log.error("Unable to release adjacency labels for a link {}.", link.toString());
+                return;
+            }
+        }
+
+        return;
+    }
+
+    /*
+     * Handle device events.
+     */
+    private class InternalDeviceListener implements DeviceListener {
+        @Override
+        public void event(DeviceEvent event) {
+            Device specificDevice = (Device) event.subject();
+            if (specificDevice == null) {
+                log.error("Unable to find device from device event.");
+                return;
+            }
+
+            switch (event.type()) {
+
+            case DEVICE_ADDED:
+                // Node-label allocation is being done during Label DB Sync.
+                // So, when device is detected, no need to do node-label
+                // allocation.
+                break;
+
+            case DEVICE_REMOVED:
+                // Release node-label
+                if (mastershipService.getLocalRole(specificDevice.id()) == MastershipRole.MASTER) {
+                    releaseNodeLabel(specificDevice);
+                }
+                break;
+
+            default:
+                break;
+            }
+        }
+    }
+
+    /*
+     * Handle link events.
+     */
+    private class InternalLinkListener implements LinkListener {
+        @Override
+        public void event(LinkEvent event) {
+            Link link = (Link) event.subject();
+
+            switch (event.type()) {
+
+            case LINK_ADDED:
+                // Allocate adjacency label
+                if (mastershipService.getLocalRole(link.src().deviceId()) == MastershipRole.MASTER) {
+                    allocateAdjacencyLabel(link);
+                }
+                break;
+
+            case LINK_REMOVED:
+                // Release adjacency label
+                if (mastershipService.getLocalRole(link.src().deviceId()) == MastershipRole.MASTER) {
+                    releaseAdjacencyLabel(link);
+                }
+                break;
+
+            default:
+                break;
+            }
         }
     }
 
@@ -851,8 +1116,11 @@
 
             case TUNNEL_UPDATED:
                 // Allocate/send labels for basic PCECC tunnels.
-                if ((tunnel.state() == ESTABLISHED) && (lspType == WITHOUT_SIGNALLING_AND_WITHOUT_SR)) {
-                    crHandler.allocateLabel(tunnel);
+                if ((tunnel.state() == ESTABLISHED) && (lspType == WITHOUT_SIGNALLING_AND_WITHOUT_SR)
+                        && (mastershipService.getLocalRole(tunnel.path().src().deviceId()) == MastershipRole.MASTER)) {
+                    if (!crHandler.allocateLabel(tunnel)) {
+                        log.error("Unable to allocate labels for a tunnel {}.", tunnel.toString());
+                    }
                 }
 
                 if (tunnel.state() == UNSTABLE) {
@@ -899,7 +1167,9 @@
                         // Delete stored tunnel consumer id from PCE store (while still retaining label list.)
                         PceccTunnelInfo pceccTunnelInfo = pceStore.getTunnelInfo(tunnel.tunnelId());
                         pceccTunnelInfo.tunnelConsumerId(null);
-                        crHandler.releaseLabel(tunnel);
+                        if (mastershipService.getLocalRole(tunnel.path().src().deviceId()) == MastershipRole.MASTER) {
+                            crHandler.releaseLabel(tunnel);
+                        }
                     } else {
                         pceStore.removeTunnelInfo(tunnel.tunnelId());
                     }
@@ -914,8 +1184,62 @@
         }
     }
 
+    private class InternalConfigListener implements NetworkConfigListener {
+
+        @Override
+        public void event(NetworkConfigEvent event) {
+
+            if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED)
+                    && event.configClass().equals(DeviceCapability.class)) {
+
+                DeviceId deviceIdLsrId = (DeviceId) event.subject();
+                String lsrId = deviceIdLsrId.toString();
+                DeviceId deviceId = lsrIdDeviceIdMap.get(lsrId);
+                if (deviceId == null) {
+                    log.debug("Unable to find device id for a lsr-id {} from lsr-id and device-id map.", lsrId);
+                    return;
+                }
+
+                DeviceCapability cfg = netCfgService.getConfig(DeviceId.deviceId(lsrId), DeviceCapability.class);
+                if (cfg == null) {
+                    log.error("Unable to find corresponding capabilty for a lsrd {}.", lsrId);
+                    return;
+                }
+
+                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());
+                        }
+
+                        // 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;
+                            }
+                        }
+                    }
+                }
+
+                // Remove lsrId info from map
+                lsrIdDeviceIdMap.remove(lsrId);
+            }
+        }
+    }
+
     private boolean syncLabelDb(DeviceId deviceId) {
         checkNotNull(deviceId);
+
+        Device specificDevice = deviceService.getDevice(deviceId);
+        if (specificDevice == null) {
+            log.error("Unable to find device for specific device id {}.", deviceId.toString());
+            return false;
+        }
+
         Map<DeviceId, LabelResourceId> globalNodeLabelMap = pceStore.getGlobalNodeLabels();
 
         for (Entry<DeviceId, LabelResourceId> entry : globalNodeLabelMap.entrySet()) {
@@ -957,6 +1281,18 @@
                                            LabelResourceId.labelResourceId(0),
                                            IpPrefix.valueOf(END_OF_SYNC_IP_PREFIX),
                                            Objective.Operation.ADD, true);
+        if (mastershipService.getLocalRole(specificDevice.id()) == MastershipRole.MASTER) {
+            // Allocate node-label to this specific device.
+            allocateNodeLabel(specificDevice);
+
+            // Allocate adjacency label
+            Set<Link> links = linkService.getDeviceEgressLinks(specificDevice.id());
+            if (links != null) {
+                for (Link link : links) {
+                    allocateAdjacencyLabel(link);
+                }
+            }
+        }
 
         return true;
     }
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 24413c1..c760bc1 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
@@ -37,6 +37,8 @@
 import org.onosproject.incubator.net.resource.label.LabelResourceService;
 import org.onosproject.incubator.net.tunnel.DefaultLabelStack;
 import org.onosproject.incubator.net.tunnel.LabelStack;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
 import org.onosproject.pce.pcestore.api.PceStore;
 import org.onosproject.net.Link;
@@ -69,10 +71,8 @@
     private static final String LABEL_RESOURCE_ADMIN_SERVICE_NULL = "Label Resource Admin Service cannot be null";
     private static final String LABEL_RESOURCE_SERVICE_NULL = "Label Resource Service cannot be null";
     private static final String PCE_STORE_NULL = "PCE Store cannot be null";
-    private static final String DEVICE_SERVICE_NULL = "Device Service cannot be null";
     private static final String DEVICE_ID_NULL = "Device-Id cannot be null";
     private static final String LSR_ID_NULL = "LSR-Id cannot be null";
-    private static final String DEVICE_ID_LSR_ID_MAP_NULL = "Device-Id and LSR-Id map cannot be null";
     private static final String LINK_NULL = "Link cannot be null";
     private static final String PATH_NULL = "Path cannot be null";
     private static final String LSR_ID = "lsrId";
@@ -81,6 +81,7 @@
     private LabelResourceAdminService labelRsrcAdminService;
     private LabelResourceService labelRsrcService;
     private FlowObjectiveService flowObjectiveService;
+    private DeviceService deviceService;
     private PceStore pceStore;
     private ApplicationId appId;
 
@@ -110,16 +111,17 @@
      * @param flowObjectiveService flow objective service to push device label information
      * @param appId application id
      * @param pceStore PCE label store
+     * @param deviceService device service
      */
-    public void initialize(LabelResourceAdminService labelRsrcAdminService,
-                           LabelResourceService labelRsrcService,
-                           FlowObjectiveService flowObjectiveService,
-                           ApplicationId appId, PceStore pceStore) {
+    public void initialize(LabelResourceAdminService labelRsrcAdminService, LabelResourceService labelRsrcService,
+                           FlowObjectiveService flowObjectiveService, ApplicationId appId, PceStore pceStore,
+                           DeviceService deviceService) {
         this.labelRsrcAdminService = labelRsrcAdminService;
         this.labelRsrcService = labelRsrcService;
         this.flowObjectiveService = flowObjectiveService;
         this.pceStore = pceStore;
         this.appId = appId;
+        this.deviceService = deviceService;
     }
 
     /**
@@ -136,72 +138,102 @@
     }
 
     /**
+     * Retrieve lsr-id from device annotation.
+     *
+     * @param deviceId specific device id from which lsr-id needs to be retrieved
+     * @return lsr-id of a device
+     */
+    public String getLsrId(DeviceId deviceId) {
+        checkNotNull(deviceId, DEVICE_ID_NULL);
+        Device device = deviceService.getDevice(deviceId);
+        if (device == null) {
+            log.debug("Device is not available for device id {} in device service.", deviceId.toString());
+            return null;
+        }
+
+        // Retrieve lsr-id from device
+        if (device.annotations() == null) {
+            log.debug("Device {} does not have annotation.", device.toString());
+            return null;
+        }
+
+        String lsrId = device.annotations().value(LSR_ID);
+        if (lsrId == null) {
+            log.debug("The lsr-id of device {} is null.", device.toString());
+            return null;
+        }
+        return lsrId;
+    }
+
+    /**
      * Allocates node label from global node label pool to specific device.
      * Configure this device with labels and lsrid mapping of all other devices and vice versa.
      *
      * @param specificDeviceId node label needs to be allocated to specific device
      * @param specificLsrId lsrid of specific device
-     * @param deviceIdLsrIdMap deviceid and lsrid mapping
      * @return success or failure
      */
-    public boolean allocateNodeLabel(DeviceId specificDeviceId, String specificLsrId,
-                                     Map<DeviceId, String> deviceIdLsrIdMap) {
+    public boolean allocateNodeLabel(DeviceId specificDeviceId, String specificLsrId) {
         long applyNum = 1; // For each node only one node label
         LabelResourceId specificLabelId = null;
 
         checkNotNull(specificDeviceId, DEVICE_ID_NULL);
         checkNotNull(specificLsrId, LSR_ID_NULL);
-        checkNotNull(deviceIdLsrIdMap, DEVICE_ID_LSR_ID_MAP_NULL);
         checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL);
         checkNotNull(pceStore, PCE_STORE_NULL);
 
-        // The specificDeviceId is the new device and is not there in the deviceIdLsrIdMap.
+        // 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());
+            return false;
+        }
+
+        // The specificDeviceId is the new device and is not there in the pce store.
         // So, first generate its label and configure label and its lsr-id to it.
         Collection<LabelResource> result = labelRsrcService.applyFromGlobalPool(applyNum);
         if (result.size() > 0) {
-           // Only one element (label-id) to retrieve
-           Iterator<LabelResource> iterator = result.iterator();
-           DefaultLabelResource defaultLabelResource = (DefaultLabelResource) iterator.next();
-           specificLabelId = defaultLabelResource.labelResourceId();
-           if (specificLabelId == null) {
-              log.error("Unable to retrieve global node label for a device id {}.", specificDeviceId.toString());
-              return false;
-           }
+            // Only one element (label-id) to retrieve
+            Iterator<LabelResource> iterator = result.iterator();
+            DefaultLabelResource defaultLabelResource = (DefaultLabelResource) iterator.next();
+            specificLabelId = defaultLabelResource.labelResourceId();
+            if (specificLabelId == null) {
+                log.error("Unable to retrieve global node label for a device id {}.", specificDeviceId.toString());
+                return false;
+            }
         } else {
-           log.error("Unable to allocate global node label for a device id {}.", specificDeviceId.toString());
-           return false;
+            log.error("Unable to allocate global node label for a device id {}.", specificDeviceId.toString());
+            return false;
         }
 
+        // store it
         pceStore.addGlobalNodeLabel(specificDeviceId, specificLabelId);
+
         // Push its label information into specificDeviceId
         advertiseNodeLabelRule(specificDeviceId, specificLabelId,
                                IpPrefix.valueOf(IpAddress.valueOf(specificLsrId), PREFIX_LENGTH),
                                Objective.Operation.ADD, false);
 
-        // Configure (node-label, lsr-id) mapping of each devices in list to specific device and vice versa.
-        for (Map.Entry<DeviceId, String> element:deviceIdLsrIdMap.entrySet()) {
-           DeviceId otherDevId = element.getKey();
-           String otherLsrId = element.getValue();
-           if (otherLsrId == null) {
-               log.error("The lsr-id of device id {} is null.", otherDevId.toString());
-               releaseNodeLabel(specificDeviceId, specificLsrId, deviceIdLsrIdMap);
-               return false;
-           }
+        // Configure (node-label, lsr-id) mapping of each devices into specific device and vice versa.
+        for (Map.Entry<DeviceId, LabelResourceId> element : pceStore.getGlobalNodeLabels().entrySet()) {
+            DeviceId otherDevId = element.getKey();
+            LabelResourceId otherLabelId = element.getValue();
 
-           // Label for other device in list should be already allocated.
-           LabelResourceId otherLabelId = pceStore.getGlobalNodeLabel(otherDevId);
-           if (otherLabelId == null) {
-              log.error("Unable to find global node label in store for a device id {}.", otherDevId.toString());
-              releaseNodeLabel(specificDeviceId, specificLsrId, deviceIdLsrIdMap);
-              return false;
-           }
+            // Get lsr-id of a device
+            String otherLsrId = getLsrId(otherDevId);
+            if (otherLsrId == null) {
+                log.error("The lsr-id of device id {} is null.", otherDevId.toString());
+                releaseNodeLabel(specificDeviceId, specificLsrId);
+                return false;
+            }
 
-           // 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);
+            // 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);
         }
 
         return true;
@@ -213,14 +245,11 @@
      *
      * @param specificDeviceId node label needs to be released for specific device
      * @param specificLsrId lsrid of specific device
-     * @param deviceIdLsrIdMap deviceid and lsrid mapping
      * @return success or failure
      */
-    public boolean releaseNodeLabel(DeviceId specificDeviceId, String specificLsrId,
-                                    Map<DeviceId, String> deviceIdLsrIdMap) {
+    public boolean releaseNodeLabel(DeviceId specificDeviceId, String specificLsrId) {
         checkNotNull(specificDeviceId, DEVICE_ID_NULL);
         checkNotNull(specificLsrId, LSR_ID_NULL);
-        checkNotNull(deviceIdLsrIdMap, DEVICE_ID_LSR_ID_MAP_NULL);
         checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL);
         checkNotNull(pceStore, PCE_STORE_NULL);
         boolean retValue = true;
@@ -229,20 +258,21 @@
         // Retrieve node label of this specific device from store
         LabelResourceId labelId = pceStore.getGlobalNodeLabel(specificDeviceId);
         if (labelId == null) {
-           log.error("Unable to retrieve label of a device id {} from store.", specificDeviceId.toString());
-           return false;
+            log.error("Unable to retrieve label of a device id {} from store.", specificDeviceId.toString());
+            return false;
         }
 
-        // Go through all devices in the map and remove label entry
-        for (Map.Entry<DeviceId, String> element:deviceIdLsrIdMap.entrySet()) {
-           DeviceId otherDevId = element.getKey();
+        // Go through all devices in the pce store and remove label entry from device
+        for (Map.Entry<DeviceId, LabelResourceId> element : pceStore.getGlobalNodeLabels().entrySet()) {
+            DeviceId otherDevId = element.getKey();
 
-           // Remove this specific device label information from all other nodes except
-           // this specific node where connection already lost.
-           if (!specificDeviceId.equals(otherDevId)) {
-              advertiseNodeLabelRule(otherDevId, labelId, IpPrefix.valueOf(IpAddress.valueOf(specificLsrId),
-                                     PREFIX_LENGTH), Objective.Operation.REMOVE, false);
-           }
+            // Remove this specific device label information from all other nodes except
+            // this specific node where connection already lost.
+            if (!specificDeviceId.equals(otherDevId)) {
+                advertiseNodeLabelRule(otherDevId, labelId,
+                                       IpPrefix.valueOf(IpAddress.valueOf(specificLsrId), PREFIX_LENGTH),
+                                       Objective.Operation.REMOVE, false);
+            }
         }
 
         // Release from label manager
@@ -277,24 +307,30 @@
         checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL);
         checkNotNull(pceStore, PCE_STORE_NULL);
 
+        // Checks whether adjacency label was already allocated
+        LabelResourceId labelId = pceStore.getAdjLabel(link);
+        if (labelId != null) {
+            log.debug("Adjacency label {} was already allocated for a link {}.", labelId.toString(), link.toString());
+            return false;
+        }
+
         // Allocate adjacency label to a link from label manager.
         // Take label from source device pool to allocate.
         labelList = labelRsrcService.applyFromDevicePool(srcDeviceId, applyNum);
         if (labelList.size() <= 0) {
-           log.error("Unable to allocate label to a device id {}.", srcDeviceId.toString());
-           return false;
+            log.error("Unable to allocate label to a device id {}.", srcDeviceId.toString());
+            return false;
         }
 
         // Currently only one label to a device. So, no need to iterate through list
         Iterator<LabelResource> iterator = labelList.iterator();
         DefaultLabelResource defaultLabelResource = (DefaultLabelResource) iterator.next();
-        LabelResourceId labelId = defaultLabelResource.labelResourceId();
+        labelId = defaultLabelResource.labelResourceId();
         if (labelId == null) {
-           log.error("Unable to allocate label to a device id {}.", srcDeviceId.toString());
-           return false;
+            log.error("Unable to allocate label to a device id {}.", srcDeviceId.toString());
+            return false;
         }
-        log.debug("Allocated adjacency label {} to a link {}.", labelId.toString(),
-                  link.toString());
+        log.debug("Allocated adjacency label {} to a link {}.", labelId.toString(), link.toString());
 
         // Push adjacency label to device
         installAdjLabelRule(srcDeviceId, labelId, link.src().port(), link.dst().port(), Objective.Operation.ADD);
@@ -304,46 +340,46 @@
         return true;
     }
 
-   /**
-     * Releases unused adjacency labels from device pools.
-     *
-     * @param link between devices
-     * @return success or failure
-     */
+    /**
+      * Releases unused adjacency labels from device pools.
+      *
+      * @param link between devices
+      * @return success or failure
+      */
     public boolean releaseAdjacencyLabel(Link link) {
-       checkNotNull(link, LINK_NULL);
-       checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL);
-       checkNotNull(pceStore, PCE_STORE_NULL);
-       boolean retValue = true;
+        checkNotNull(link, LINK_NULL);
+        checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL);
+        checkNotNull(pceStore, PCE_STORE_NULL);
+        boolean retValue = true;
 
-       // Retrieve link label from store
-       LabelResourceId labelId = pceStore.getAdjLabel(link);
-       if (labelId == null) {
-          log.error("Unabel to retrieve label for a link {} from store.", link.toString());
-          return false;
-       }
+        // Retrieve link label from store
+        LabelResourceId labelId = pceStore.getAdjLabel(link);
+        if (labelId == null) {
+            log.error("Unabel to retrieve label for a link {} from store.", link.toString());
+            return false;
+        }
 
-       // Device
-       DeviceId srcDeviceId = link.src().deviceId();
+        // Device
+        DeviceId srcDeviceId = link.src().deviceId();
 
-       // Release adjacency label from device
-       installAdjLabelRule(srcDeviceId, labelId, link.src().port(), link.dst().port(), Objective.Operation.REMOVE);
+        // Release adjacency label from device
+        installAdjLabelRule(srcDeviceId, labelId, link.src().port(), link.dst().port(), Objective.Operation.REMOVE);
 
-       // Release link label from label manager
-       Multimap<DeviceId, LabelResource> release = ArrayListMultimap.create();
-       DefaultLabelResource defaultLabelResource = new DefaultLabelResource(srcDeviceId, labelId);
-       release.put(srcDeviceId, defaultLabelResource);
-       if (!labelRsrcService.releaseToDevicePool(release)) {
-          log.error("Unable to release label id {} from label manager.", labelId.toString());
-          retValue = false;
-       }
+        // Release link label from label manager
+        Multimap<DeviceId, LabelResource> release = ArrayListMultimap.create();
+        DefaultLabelResource defaultLabelResource = new DefaultLabelResource(srcDeviceId, labelId);
+        release.put(srcDeviceId, defaultLabelResource);
+        if (!labelRsrcService.releaseToDevicePool(release)) {
+            log.error("Unable to release label id {} from label manager.", labelId.toString());
+            retValue = false;
+        }
 
-       // Remove adjacency label from store
-       if (!pceStore.removeAdjLabel(link)) {
-          log.error("Unable to remove adjacency label id {} from store.", labelId.toString());
-          retValue = false;
-       }
-       return retValue;
+        // Remove adjacency label from store
+        if (!pceStore.removeAdjLabel(link)) {
+            log.error("Unable to remove adjacency label id {} from store.", labelId.toString());
+            retValue = false;
+        }
+        return retValue;
     }
 
     /**
@@ -372,16 +408,16 @@
                 deviceId = link.src().deviceId();
                 nodeLabelId = pceStore.getGlobalNodeLabel(deviceId);
                 if (nodeLabelId == null) {
-                   log.error("Unable to find node label for a device id {} in store.", deviceId.toString());
-                   return null;
+                    log.error("Unable to find node label for a device id {} in store.", deviceId.toString());
+                    return null;
                 }
                 labelStack.add(nodeLabelId);
 
                 // Add adjacency label for this link
                 adjLabelId = pceStore.getAdjLabel(link);
                 if (adjLabelId == null) {
-                   log.error("Adjacency label id is null for a link {}.", link.toString());
-                   return null;
+                    log.error("Adjacency label id is null for a link {}.", link.toString());
+                    return null;
                 }
                 labelStack.add(adjLabelId);
             }
@@ -389,13 +425,13 @@
             // This is the last link in path
             // Add destination device label now.
             if (link != null) {
-               deviceId = link.dst().deviceId();
-               nodeLabelId = pceStore.getGlobalNodeLabel(deviceId);
-               if (nodeLabelId == null) {
-                  log.error("Unable to find node label for a device id {} in store.", deviceId.toString());
-                  return null;
-               }
-               labelStack.add(nodeLabelId);
+                deviceId = link.dst().deviceId();
+                nodeLabelId = pceStore.getGlobalNodeLabel(deviceId);
+                if (nodeLabelId == null) {
+                    log.error("Unable to find node label for a device id {} in store.", deviceId.toString());
+                    return null;
+                }
+                labelStack.add(nodeLabelId);
             }
         } else {
             log.debug("Empty link in path.");
@@ -418,15 +454,11 @@
 
         selectorBuilder.matchMplsLabel(MplsLabel.mplsLabel(labelId.id().intValue()));
 
-        TrafficTreatment treatment = DefaultTrafficTreatment.builder()
-                .build();
+        TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
 
         ForwardingObjective.Builder forwardingObjective = DefaultForwardingObjective.builder()
-                .withSelector(selectorBuilder.build())
-                .withTreatment(treatment)
-                .withFlag(ForwardingObjective.Flag.VERSATILE)
-                .fromApp(appId)
-                .makePermanent();
+                .withSelector(selectorBuilder.build()).withTreatment(treatment)
+                .withFlag(ForwardingObjective.Flag.VERSATILE).fromApp(appId).makePermanent();
 
         if (type.equals(Objective.Operation.ADD)) {
 
@@ -445,8 +477,8 @@
      * @param type type of operation
      * @param bBos is this the end of sync push
      */
-    public void advertiseNodeLabelRule(DeviceId deviceId, LabelResourceId labelId,
-                                        IpPrefix ipPrefix, Objective.Operation type, boolean bBos) {
+    public void advertiseNodeLabelRule(DeviceId deviceId, LabelResourceId labelId, IpPrefix ipPrefix,
+                                       Objective.Operation type, boolean bBos) {
         checkNotNull(flowObjectiveService);
         checkNotNull(appId);
         TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
@@ -458,15 +490,11 @@
             selectorBuilder.matchMplsBos(bBos);
         }
 
-        TrafficTreatment treatment = DefaultTrafficTreatment.builder()
-                .build();
+        TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
 
         ForwardingObjective.Builder forwardingObjective = DefaultForwardingObjective.builder()
-                .withSelector(selectorBuilder.build())
-                .withTreatment(treatment)
-                .withFlag(ForwardingObjective.Flag.VERSATILE)
-                .fromApp(appId)
-                .makePermanent();
+                .withSelector(selectorBuilder.build()).withTreatment(treatment)
+                .withFlag(ForwardingObjective.Flag.VERSATILE).fromApp(appId).makePermanent();
 
         if (type.equals(Objective.Operation.ADD)) {
             flowObjectiveService.forward(deviceId, forwardingObjective.add());
@@ -484,9 +512,8 @@
      * @param dstPortNum remote port of the adjacency
      * @param type type of operation
      */
-    public void installAdjLabelRule(DeviceId deviceId, LabelResourceId labelId,
-                                     PortNumber srcPortNum, PortNumber dstPortNum,
-                                     Objective.Operation type) {
+    public void installAdjLabelRule(DeviceId deviceId, LabelResourceId labelId, PortNumber srcPortNum,
+                                    PortNumber dstPortNum, Objective.Operation type) {
         checkNotNull(flowObjectiveService);
         checkNotNull(appId);
         TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
@@ -495,15 +522,11 @@
         selectorBuilder.matchTcpSrc(TpPort.tpPort((int) srcPortNum.toLong()));
         selectorBuilder.matchTcpDst(TpPort.tpPort((int) dstPortNum.toLong()));
 
-        TrafficTreatment treatment = DefaultTrafficTreatment.builder()
-                .build();
+        TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
 
         ForwardingObjective.Builder forwardingObjective = DefaultForwardingObjective.builder()
-                .withSelector(selectorBuilder.build())
-                .withTreatment(treatment)
-                .withFlag(ForwardingObjective.Flag.VERSATILE)
-                .fromApp(appId)
-                .makePermanent();
+                .withSelector(selectorBuilder.build()).withTreatment(treatment)
+                .withFlag(ForwardingObjective.Flag.VERSATILE).fromApp(appId).makePermanent();
 
         if (type.equals(Objective.Operation.ADD)) {
             flowObjectiveService.forward(deviceId, forwardingObjective.add());