[ONOS-4663] [ONOS-4664] ST defect fixes in session establishment/sync

Change-Id: I0bdb4ad08295fe30c488e9170923847112fab647
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 2e148db..b415534 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
@@ -1281,6 +1281,9 @@
                                            LabelResourceId.labelResourceId(0),
                                            IpPrefix.valueOf(END_OF_SYNC_IP_PREFIX),
                                            Objective.Operation.ADD, true);
+
+        log.debug("End of label DB sync for device {}", deviceId);
+
         if (mastershipService.getLocalRole(specificDevice.id()) == MastershipRole.MASTER) {
             // Allocate node-label to this specific device.
             allocateNodeLabel(specificDevice);
@@ -1304,6 +1307,7 @@
         public void process(PacketContext context) {
             // Stop processing if the packet has been handled, since we
             // can't do any more to it.
+            log.debug("Received trigger for label DB sync.");
             if (context.isHandled()) {
                 return;
             }
diff --git a/protocols/pcep/ctl/src/main/java/org/onosproject/pcep/controller/impl/PcepChannelHandler.java b/protocols/pcep/ctl/src/main/java/org/onosproject/pcep/controller/impl/PcepChannelHandler.java
index e8c52cc..6fe05e0 100644
--- a/protocols/pcep/ctl/src/main/java/org/onosproject/pcep/controller/impl/PcepChannelHandler.java
+++ b/protocols/pcep/ctl/src/main/java/org/onosproject/pcep/controller/impl/PcepChannelHandler.java
@@ -177,18 +177,23 @@
                          * The socket IP is stored in channel.
                          */
                         LinkedList<PcepValueType> optionalTlvs = pOpenmsg.getPcepOpenObject().getOptionalTlv();
-                        for (PcepValueType optionalTlv : optionalTlvs) {
-                            if (optionalTlv instanceof NodeAttributesTlv) {
-                                List<PcepValueType> subTlvs = ((NodeAttributesTlv) optionalTlv)
-                                        .getllNodeAttributesSubTLVs();
-                                for (PcepValueType subTlv : subTlvs) {
-                                    if (subTlv instanceof IPv4RouterIdOfLocalNodeSubTlv) {
-                                        h.thispccId = PccId.pccId(IpAddress
-                                                .valueOf(((IPv4RouterIdOfLocalNodeSubTlv) subTlv).getInt()));
+                        if (optionalTlvs != null) {
+                            for (PcepValueType optionalTlv : optionalTlvs) {
+                                if (optionalTlv instanceof NodeAttributesTlv) {
+                                    List<PcepValueType> subTlvs = ((NodeAttributesTlv) optionalTlv)
+                                            .getllNodeAttributesSubTLVs();
+                                    if (subTlvs == null) {
                                         break;
                                     }
+                                    for (PcepValueType subTlv : subTlvs) {
+                                        if (subTlv instanceof IPv4RouterIdOfLocalNodeSubTlv) {
+                                            h.thispccId = PccId.pccId(IpAddress
+                                                    .valueOf(((IPv4RouterIdOfLocalNodeSubTlv) subTlv).getInt()));
+                                            break;
+                                        }
+                                    }
+                                    break;
                                 }
-                                break;
                             }
                         }
 
diff --git a/protocols/pcep/ctl/src/main/java/org/onosproject/pcep/controller/impl/PcepClientControllerImpl.java b/protocols/pcep/ctl/src/main/java/org/onosproject/pcep/controller/impl/PcepClientControllerImpl.java
index b7dbfda..cb40ea0 100644
--- a/protocols/pcep/ctl/src/main/java/org/onosproject/pcep/controller/impl/PcepClientControllerImpl.java
+++ b/protocols/pcep/ctl/src/main/java/org/onosproject/pcep/controller/impl/PcepClientControllerImpl.java
@@ -236,8 +236,10 @@
                 ListIterator<PcepStateReport> listIterator = ((PcepReportMsg) msg).getStateReportList().listIterator();
                 while (listIterator.hasNext()) {
                     PcepStateReport stateRpt = listIterator.next();
-                    if (stateRpt.getLspObject().getSFlag()) {
+                    PcepLspObject lspObj = stateRpt.getLspObject();
+                    if (lspObj.getSFlag()) {
                         if (pc.lspDbSyncStatus() != PcepSyncStatus.IN_SYNC) {
+                            log.debug("LSP DB sync started for PCC {}", pc.getPccId().id().toString());
                             // Initialize LSP DB sync and temporary cache.
                             pc.setLspDbSyncStatus(PcepSyncStatus.IN_SYNC);
                             pc.initializeSyncMsgList(pccId);
@@ -247,13 +249,16 @@
 
                         // Don't send to provider as of now.
                         continue;
-                    } else {
-                        if (pc.lspDbSyncStatus() == PcepSyncStatus.IN_SYNC) {
+                    } else if (lspObj.getPlspId() == 0) {
+                        if (pc.lspDbSyncStatus() == PcepSyncStatus.IN_SYNC
+                                || pc.lspDbSyncStatus() == PcepSyncStatus.NOT_SYNCED) {
                             // Set end of LSPDB sync.
+                            log.debug("LSP DB sync completed for PCC {}", pc.getPccId().id().toString());
                             pc.setLspDbSyncStatus(PcepSyncStatus.SYNCED);
 
                             // Call packet provider to initiate label DB sync (only if PCECC capable).
                             if (pc.capability().pceccCapability()) {
+                                log.debug("Trigger label DB sync for PCC {}", pc.getPccId().id().toString());
                                 pc.setLabelDbSyncStatus(IN_SYNC);
                                 for (PcepPacketListener l : pcepPacketListener) {
                                     l.sendPacketIn(pccId);
diff --git a/providers/pcep/packet/src/main/java/org/onosproject/provider/pcep/packet/impl/PcepPacketProvider.java b/providers/pcep/packet/src/main/java/org/onosproject/provider/pcep/packet/impl/PcepPacketProvider.java
index 48f2408..13a2f99 100644
--- a/providers/pcep/packet/src/main/java/org/onosproject/provider/pcep/packet/impl/PcepPacketProvider.java
+++ b/providers/pcep/packet/src/main/java/org/onosproject/provider/pcep/packet/impl/PcepPacketProvider.java
@@ -107,6 +107,7 @@
             }
 
             if (pccDeviceId == null) {
+                log.error("Device not found to perform label DB sync.");
                 return;
             }
 
diff --git a/providers/pcep/topology/src/main/java/org/onosproject/provider/pcep/topology/impl/PcepTopologyProvider.java b/providers/pcep/topology/src/main/java/org/onosproject/provider/pcep/topology/impl/PcepTopologyProvider.java
index eaebc57..7efdec8 100644
--- a/providers/pcep/topology/src/main/java/org/onosproject/provider/pcep/topology/impl/PcepTopologyProvider.java
+++ b/providers/pcep/topology/src/main/java/org/onosproject/provider/pcep/topology/impl/PcepTopologyProvider.java
@@ -141,7 +141,7 @@
 
     private final ConfigFactory<DeviceId, DeviceCapability> configFactory =
             new ConfigFactory<DeviceId, DeviceCapability>(SubjectFactories.DEVICE_SUBJECT_FACTORY,
-                    DeviceCapability.class, "deviceCapability", true) {
+                    DeviceCapability.class, "deviceCapability", false) {
                 @Override
                 public DeviceCapability createConfig() {
                     return new DeviceCapability();