[ONOS] cherry pick from onos1.6 to master all defects merges

Change-Id: I0ff6595a55b1104cf59a270ac2b10a7f831f6555
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 31c7d37..4a0b3ac 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
@@ -110,14 +110,14 @@
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 
-import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS;
+import static org.onosproject.incubator.net.tunnel.Tunnel.State.ACTIVE;
 import static org.onosproject.incubator.net.tunnel.Tunnel.State.INIT;
 import static org.onosproject.incubator.net.tunnel.Tunnel.State.ESTABLISHED;
 import static org.onosproject.incubator.net.tunnel.Tunnel.State.UNSTABLE;
+import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS;
 import static org.onosproject.pce.pceservice.LspType.WITH_SIGNALLING;
 import static org.onosproject.pce.pceservice.LspType.SR_WITHOUT_SIGNALLING;
 import static org.onosproject.pce.pceservice.LspType.WITHOUT_SIGNALLING_AND_WITHOUT_SR;
-
 import static org.onosproject.pce.pceservice.PcepAnnotationKeys.BANDWIDTH;
 import static org.onosproject.pce.pceservice.PcepAnnotationKeys.LOCAL_LSP_ID;
 import static org.onosproject.pce.pceservice.PcepAnnotationKeys.LSP_SIG_TYPE;
@@ -509,7 +509,7 @@
             }
 
             if (existingBwValue != null) {
-                if (bwConstraintValue == 0) {
+                if (bwConstraintValue == 0 && bwConstraint != null) {
                     bwConstraintValue = existingBwValue.bps();
                 }
                 //If bandwidth constraints not specified , take existing bandwidth for shared bandwidth calculation
@@ -621,6 +621,12 @@
             return false;
         }
 
+        LspType lspType = LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE));
+        // Release basic PCECC labels.
+        if (lspType == WITHOUT_SIGNALLING_AND_WITHOUT_SR) {
+            crHandler.releaseLabel(tunnel);
+        }
+
         // 2. Call tunnel service.
         return tunnelService.downTunnel(appId, tunnel.tunnelId());
     }
@@ -641,7 +647,7 @@
      *
      * @return value of local LSP identifier
      */
-    private short getNextLocalLspId() {
+    private synchronized short getNextLocalLspId() {
         // If there is any free id use it. Otherwise generate new id.
         if (localLspIdFreeList.isEmpty()) {
             return (short) localLspIdIdGen.getNewId();
@@ -778,7 +784,7 @@
             bwToAllocate = 0;
             if ((shBwConstraint != null) && (shBwConstraint.links().contains(link))) {
                 if (additionalBwValue != null) {
-                    bwToAllocate = bandwidthConstraint - additionalBwValue;
+                    bwToAllocate = additionalBwValue;
                 }
             } else {
                 bwToAllocate = bandwidthConstraint;
@@ -857,7 +863,12 @@
         // 1. Release old tunnel's bandwidth.
         resourceService.release(pceStore.getTunnelInfo(oldTunnel.tunnelId()).tunnelConsumerId());
 
-        // 2. Release new tunnel's bandwidth
+        // 2. Release new tunnel's bandwidth, if new tunnel bandwidth is allocated
+        if (pceStore.getTunnelInfo(newTunnel.tunnelId()) == null) {
+            //If bandwidth for new tunnel is not allocated i,e 0 then no need to allocate
+            return;
+        }
+
         ResourceConsumer consumer = pceStore.getTunnelInfo(newTunnel.tunnelId()).tunnelConsumerId();
         resourceService.release(consumer);
 
@@ -1148,6 +1159,22 @@
                     }
                 }
 
+                //In CR case, release labels when new tunnel for it is updated.
+                if (lspType == WITHOUT_SIGNALLING_AND_WITHOUT_SR && tunnel.state() == ACTIVE
+                        && mastershipService.getLocalRole(tunnel.path().src().deviceId()) == MastershipRole.MASTER) {
+                    Collection<Tunnel> tunnels = tunnelService.queryTunnel(tunnel.src(), tunnel.dst());
+
+                    for (Tunnel t : tunnels) {
+                          if (tunnel.annotations().value(PLSP_ID).equals(t.annotations().value(PLSP_ID))
+                              && !tunnel.annotations().value(LOCAL_LSP_ID)
+                                  .equals(t.annotations().value(LOCAL_LSP_ID))) {
+                              // Release basic PCECC labels.
+                              crHandler.releaseLabel(t);
+                              break;
+                          }
+                    }
+                }
+
                 if (tunnel.state() == UNSTABLE) {
                     /*
                      * During LSP DB sync if PCC doesn't report LSP which was PCE initiated, it's state is turned into
@@ -1183,23 +1210,16 @@
                 if (lspType != WITH_SIGNALLING) {
                     localLspIdFreeList.add(Short.valueOf(tunnel.annotations().value(LOCAL_LSP_ID)));
                 }
-
                 // If not zero bandwidth, and delegated (initiated LSPs will also be delegated).
-                if (bwConstraintValue != 0) {
-                    releaseBandwidth(event.subject());
-
-                    // Release basic PCECC labels.
-                    if (lspType == WITHOUT_SIGNALLING_AND_WITHOUT_SR) {
-                        // Delete stored tunnel consumer id from PCE store (while still retaining label list.)
-                        PceccTunnelInfo pceccTunnelInfo = pceStore.getTunnelInfo(tunnel.tunnelId());
-                        pceccTunnelInfo.tunnelConsumerId(null);
-                        if (mastershipService.getLocalRole(tunnel.path().src().deviceId()) == MastershipRole.MASTER) {
-                            crHandler.releaseLabel(tunnel);
-                        }
-                    } else {
-                        pceStore.removeTunnelInfo(tunnel.tunnelId());
-                    }
+                if (Float.parseFloat(tunnel.annotations().value(BANDWIDTH)) != 0
+                        && mastershipService.getLocalRole(tunnel.path().src().deviceId()) == MastershipRole.MASTER) {
+                    releaseBandwidth(tunnel);
                 }
+
+                if (pceStore.getTunnelInfo(tunnel.tunnelId()) != null) {
+                    pceStore.removeTunnelInfo(tunnel.tunnelId());
+                }
+
                 break;
 
             default: