[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/cli/PceUpdatePathCommand.java b/apps/pce/app/src/main/java/org/onosproject/pce/cli/PceUpdatePathCommand.java
index 6ef5fc1..536eda8 100644
--- a/apps/pce/app/src/main/java/org/onosproject/pce/cli/PceUpdatePathCommand.java
+++ b/apps/pce/app/src/main/java/org/onosproject/pce/cli/PceUpdatePathCommand.java
@@ -65,16 +65,13 @@
             constrntList.add(BandwidthConstraint.of(Double.valueOf(bandwidth), DataRateUnit.valueOf("BPS")));
         }
 
-        // Assign cost
-        if (cost != 0) {
-            // Cost validation
-            if ((cost < 1) || (cost > 2)) {
-                error("The cost attribute value is either IGP cost(1) or TE cost(2).");
-                return;
-            }
-            CostConstraint.Type costType = CostConstraint.Type.values()[cost - 1];
-            constrntList.add(CostConstraint.of(costType));
+        // Cost validation
+        if ((cost < 1) || (cost > 2)) {
+            error("The cost attribute value is either IGP cost(1) or TE cost(2).");
+            return;
         }
+        CostConstraint.Type costType = CostConstraint.Type.values()[cost - 1];
+        constrntList.add(CostConstraint.of(costType));
 
         if (!service.updatePath(TunnelId.valueOf(id), constrntList)) {
             error("Path updation failed.");
diff --git a/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/BasicPceccHandler.java b/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/BasicPceccHandler.java
index 3d981e4..e30b966 100644
--- a/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/BasicPceccHandler.java
+++ b/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/BasicPceccHandler.java
@@ -269,7 +269,6 @@
      * @param tunnel tunnel between ingress to egress
      */
     public void releaseLabel(Tunnel tunnel) {
-       boolean isLastLabelToPush = false;
 
        checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL);
        checkNotNull(pceStore, PCE_STORE_NULL);
@@ -287,19 +286,14 @@
                    PortNumber inPort = lspLocalLabelInfo.inPort();
                    PortNumber outPort = lspLocalLabelInfo.outPort();
 
-                   // Check whether this is last link label to push
-                   if (!iterator.hasNext()) {
-                      isLastLabelToPush = true;
-                   }
-
                    // Push into device
                    if ((inLabelId != null) && (inPort != null)) {
-                       installLocalLabelRule(deviceId, inLabelId, inPort, tunnel.tunnelId(), isLastLabelToPush,
+                       installLocalLabelRule(deviceId, inLabelId, inPort, tunnel.tunnelId(), false,
                                              Long.valueOf(LabelType.IN_LABEL.value), Objective.Operation.REMOVE);
                    }
 
                    if ((outLabelId != null) && (outPort != null)) {
-                       installLocalLabelRule(deviceId, outLabelId, outPort, tunnel.tunnelId(), isLastLabelToPush,
+                       installLocalLabelRule(deviceId, outLabelId, outPort, tunnel.tunnelId(), false,
                                              Long.valueOf(LabelType.OUT_LABEL.value), Objective.Operation.REMOVE);
                    }
 
@@ -331,7 +325,7 @@
    }
 
     // Install a rule for pushing local labels to the device which is specific to path.
-    private void installLocalLabelRule(DeviceId deviceId, LabelResourceId labelId,
+    private synchronized void installLocalLabelRule(DeviceId deviceId, LabelResourceId labelId,
                                        PortNumber portNum, TunnelId tunnelId,
                                        Boolean isBos, Long labelType,
                                        Objective.Operation type) {
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:
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 0cda281..ea5dca0 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
@@ -44,6 +44,7 @@
 import org.onosproject.pce.pceservice.constraint.CostConstraint;
 import org.onosproject.pce.pceservice.TunnelConsumerId;
 import org.onosproject.pce.pceservice.LspType;
+import org.onosproject.pce.pceservice.constraint.SharedBandwidthConstraint;
 import org.onosproject.pce.pcestore.api.LspLocalLabelInfo;
 import org.onosproject.pce.pcestore.api.PceStore;
 import org.onosproject.store.serializers.KryoNamespaces;
@@ -142,6 +143,7 @@
                                           CostConstraint.class,
                                           CostConstraint.Type.class,
                                           BandwidthConstraint.class,
+                                          SharedBandwidthConstraint.class,
                                           CapabilityConstraint.class,
                                           CapabilityConstraint.CapabilityType.class,
                                           LspType.class)
diff --git a/apps/pce/app/src/main/java/org/onosproject/pce/pcestore/PceccTunnelInfo.java b/apps/pce/app/src/main/java/org/onosproject/pce/pcestore/PceccTunnelInfo.java
index 3186d3a..e41947d 100644
--- a/apps/pce/app/src/main/java/org/onosproject/pce/pcestore/PceccTunnelInfo.java
+++ b/apps/pce/app/src/main/java/org/onosproject/pce/pcestore/PceccTunnelInfo.java
@@ -115,8 +115,8 @@
     public String toString() {
         return MoreObjects.toStringHelper(getClass())
                 .omitNullValues()
-                .add("DeviceLabelInfoList", lspLocalLabelInfoList.toString())
-                .add("TunnelConsumerId", tunnelConsumerId.toString())
+                .add("DeviceLabelInfoList", lspLocalLabelInfoList)
+                .add("TunnelConsumerId", tunnelConsumerId)
                 .toString();
     }
 }