[ONOS] RSVP defect fix

Change-Id: I7ac7a9f9dd806949d900743a3978eec34ab4a3fb
diff --git a/apps/pce/app/src/main/java/org/onosproject/pce/cli/PceSetupPathCommand.java b/apps/pce/app/src/main/java/org/onosproject/pce/cli/PceSetupPathCommand.java
index d7e514b..a14e6e1 100644
--- a/apps/pce/app/src/main/java/org/onosproject/pce/cli/PceSetupPathCommand.java
+++ b/apps/pce/app/src/main/java/org/onosproject/pce/cli/PceSetupPathCommand.java
@@ -86,10 +86,9 @@
         LspType lspType = LspType.values()[type];
 
         // Add bandwidth
-        // bandwidth default data rate unit is in MBPS, since bandwidth value in network config
-        //stored in MPBS
+        // bandwidth default data rate unit is in BPS
         if (bandwidth != 0.0) {
-            listConstrnt.add(BandwidthConstraint.of(bandwidth, DataRateUnit.valueOf("MBPS")));
+            listConstrnt.add(BandwidthConstraint.of(bandwidth, DataRateUnit.valueOf("BPS")));
         }
 
         // Add cost
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 41e14dd..d4d4808 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
@@ -515,7 +515,14 @@
             }
 
             if (existingBwValue != null) {
-                shBwConstraint = new SharedBandwidthConstraint(links, existingBwValue, bwConstraint.bandwidth());
+                if (bwConstraintValue == 0) {
+                    bwConstraintValue = existingBwValue.bps();
+                }
+                //If bandwidth constraints not specified , take existing bandwidth for shared bandwidth calculation
+                shBwConstraint = bwConstraint != null ? new SharedBandwidthConstraint(links,
+                        existingBwValue, bwConstraint.bandwidth()) : new SharedBandwidthConstraint(links,
+                        existingBwValue, existingBwValue);
+
                 constraints.add(shBwConstraint);
             }
         } else {
diff --git a/protocols/pcep/ctl/src/main/java/org/onosproject/pcep/controller/impl/PcepClientImpl.java b/protocols/pcep/ctl/src/main/java/org/onosproject/pcep/controller/impl/PcepClientImpl.java
index 91a79de..42d4b07 100644
--- a/protocols/pcep/ctl/src/main/java/org/onosproject/pcep/controller/impl/PcepClientImpl.java
+++ b/protocols/pcep/ctl/src/main/java/org/onosproject/pcep/controller/impl/PcepClientImpl.java
@@ -72,7 +72,7 @@
     private byte deadTime;
     private byte sessionId;
     private PcepPacketStatsImpl pktStats;
-    private Map<LspKey, Boolean> lspDelegationInfo;
+    private Map<LspKey, Boolean> lspDelegationInfo = new HashMap<>();
     private Map<PccId, List<PcepStateReport>> syncRptCache = new HashMap<>();
 
     @Override
diff --git a/providers/pcep/tunnel/src/main/java/org/onosproject/provider/pcep/tunnel/impl/PcepTunnelProvider.java b/providers/pcep/tunnel/src/main/java/org/onosproject/provider/pcep/tunnel/impl/PcepTunnelProvider.java
index 8448528..163e11d 100644
--- a/providers/pcep/tunnel/src/main/java/org/onosproject/provider/pcep/tunnel/impl/PcepTunnelProvider.java
+++ b/providers/pcep/tunnel/src/main/java/org/onosproject/provider/pcep/tunnel/impl/PcepTunnelProvider.java
@@ -125,6 +125,7 @@
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Strings.isNullOrEmpty;
 import static org.onlab.util.Tools.get;
+import static org.onosproject.incubator.net.tunnel.Tunnel.State.INIT;
 import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS;
 import static org.onosproject.net.DefaultAnnotations.EMPTY;
 import static org.onosproject.net.DeviceId.deviceId;
@@ -136,6 +137,7 @@
 import static org.onosproject.pcep.controller.PcepAnnotationKeys.LOCAL_LSP_ID;
 import static org.onosproject.pcep.controller.PcepAnnotationKeys.LSP_SIG_TYPE;
 import static org.onosproject.pcep.controller.PcepAnnotationKeys.PCC_TUNNEL_ID;
+import static org.onosproject.pcep.controller.PcepAnnotationKeys.PCE_INIT;
 import static org.onosproject.pcep.controller.PcepAnnotationKeys.PLSP_ID;
 import static org.onosproject.pcep.controller.PcepAnnotationKeys.DELEGATE;
 import static org.onosproject.pcep.controller.PcepAnnotationKeys.COST_TYPE;
@@ -298,6 +300,7 @@
     @Override
     public void setupTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
 
+        //TODO: tunnel which is passed doesn't have tunnelID
         if (tunnel.annotations().value(PLSP_ID) != null) {
             updateTunnel(tunnel, path);
             return;
@@ -317,8 +320,8 @@
         PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
 
         if (!(pc instanceof PcepClient)) {
-            log.error("There is no PCC connected with ip addresss {}"
-                    + ((IpElementId) srcElement).ipAddress().toString());
+            log.error("There is no PCC connected with this device {}"
+                    + srcElement.toString());
             return;
         }
 
@@ -405,6 +408,22 @@
             return;
         }
 
+        //To get new tunnel ID (modified tunnel ID)
+        Collection<Tunnel> tunnels = tunnelService.queryTunnel(tunnel.src(), tunnel.dst());
+        for (Tunnel t : tunnels) {
+            if (t.state().equals(INIT) && t.tunnelName().equals(tunnel.tunnelName())) {
+                tunnel = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
+                        tunnel.dst(), tunnel.type(),
+                        t.state(), tunnel.groupId(),
+                        t.tunnelId(),
+                        tunnel.tunnelName(),
+                        tunnel.path(),
+                        tunnel.resource(),
+                        tunnel.annotations());
+                        break;
+            }
+        }
+
         PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
 
         if (!(pc instanceof PcepClient)) {
@@ -415,11 +434,27 @@
 
         // If delegation flag is set then only send update message[means delegated PCE can send update msg for that
         // LSP].If annotation is null D flag is not set else it is set.
-        if (pc.capability().statefulPceCapability()
-                && pc.delegationInfo(
-                        new LspKey(Integer.valueOf(tunnel.annotations().value(PLSP_ID)), Short.valueOf(tunnel
-                                .annotations().value(LOCAL_LSP_ID)))) != null) {
-            pcepUpdateTunnel(tunnel, path, pc);
+        Short localLspId = 0;
+        for (Tunnel t : tunnels) {
+            if (!t.tunnelId().equals(tunnel.tunnelId()) && t.tunnelName().equals(tunnel.tunnelName())) {
+                localLspId = Short.valueOf(t.annotations().value(LOCAL_LSP_ID));
+            }
+        }
+
+        if (localLspId == 0) {
+            log.error("Local LSP ID for old tunnel not found");
+            return;
+        }
+
+        //PCInitiate tunnels are always have D flag set, else check for tunnels who are delegated via LspKey
+        if (pc.capability().statefulPceCapability()) {
+            if (tunnel.annotations().value(PCE_INIT) != null && tunnel.annotations().value(PCE_INIT).equals("true")) {
+                pcepUpdateTunnel(tunnel, path, pc);
+            } else if (pc.delegationInfo(
+                    new LspKey(Integer.valueOf(tunnel.annotations().value(PLSP_ID)),
+                            localLspId.shortValue())) != null) {
+                pcepUpdateTunnel(tunnel, path, pc);
+            }
         }
     }
 
@@ -886,6 +921,7 @@
             }
         }
 
+        tunnel.annotations().value(LSP_SIG_TYPE);
         tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt()),
                                                 localLspId, (short) 0, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
                                                         .getIp4Address().toInt()));
@@ -907,9 +943,9 @@
         //build ERO object
         PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
 
-        float iBandwidth = DEFAULT_BANDWIDTH_VALUE;
+        int  iBandwidth = DEFAULT_BANDWIDTH_VALUE;
         if (tunnel.annotations().value(BANDWIDTH) != null) {
-            iBandwidth = Float.parseFloat(tunnel.annotations().value(BANDWIDTH));
+            iBandwidth = Float.floatToIntBits(Float.parseFloat(tunnel.annotations().value(BANDWIDTH)));
         }
         // build bandwidth object
         PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
@@ -933,6 +969,26 @@
     private void pcepSetupTunnel(Tunnel tunnel, Path path, PcepClient pc) {
         try {
             int srpId = SrpIdGenerators.create();
+            Collection<Tunnel> tunnels = tunnelService.queryTunnel(tunnel.src(), tunnel.dst());
+            for (Tunnel t : tunnels) {
+                if (t.tunnelName().equals(tunnel.tunnelName())) {
+                    tunnel = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
+                            tunnel.dst(), tunnel.type(),
+                            t.state(), tunnel.groupId(),
+                            t.tunnelId(),
+                            tunnel.tunnelName(),
+                            tunnel.path(),
+                            tunnel.resource(),
+                            tunnel.annotations());
+                            break;
+                }
+            }
+
+            if (tunnel.tunnelId() == null) {
+                log.error("Tunnel ID not found");
+                return;
+            }
+
             PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, CREATE);
 
             pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
@@ -996,6 +1052,26 @@
 
             tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
             llOptionalTlv.add(tlv);
+
+            String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
+            String pccTunnelIdString = tunnel.annotations().value(PCC_TUNNEL_ID);
+            short localLspId = 0;
+            short pccTunnelId = 0;
+
+            if (localLspIdString != null) {
+                localLspId = Short.valueOf(localLspIdString);
+            }
+
+            if (pccTunnelIdString != null) {
+                pccTunnelId = Short.valueOf(pccTunnelIdString);
+            }
+
+            tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src())
+                    .ip().getIp4Address().toInt()),
+                    localLspId, pccTunnelId, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
+                    .getIp4Address().toInt()));
+            llOptionalTlv.add(tlv);
+
             // build lsp object, set r flag as false to delete the tunnel
             PcepLspObject lspobj = pc.factory().buildLspObject().setRFlag(false).setPlspId(plspId)
                     .setOptionalTlv(llOptionalTlv).build();
@@ -1047,14 +1123,6 @@
 
             llOptionalTlv = new LinkedList<PcepValueType>();
 
-            if (!(pcepTunnelApiMapper.checkFromTunnelDBQueue(tunnelId))) {
-                log.error("Tunnel doesnot exists in DB");
-                return;
-            } else {
-                PcepTunnelData pcepTunnelDBData = pcepTunnelApiMapper.getDataFromTunnelDBQueue(tunnelId);
-                plspId = pcepTunnelDBData.plspId();
-            }
-
             if (lspSigType != WITH_SIGNALLING) {
                 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
                 String pccTunnelIdString = tunnel.annotations().value(PCC_TUNNEL_ID);
@@ -1082,14 +1150,15 @@
             }
 
             // build lsp object
-            PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true).setPlspId(plspId)
+            PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true)
+                    .setPlspId(Integer.valueOf(tunnel.annotations().value(PLSP_ID)))
                     .setOptionalTlv(llOptionalTlv).build();
             // build ero object
             PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
 
-            int iBandwidth = DEFAULT_BANDWIDTH_VALUE;
+            float iBandwidth = DEFAULT_BANDWIDTH_VALUE;
             if (tunnel.annotations().value(BANDWIDTH) != null) {
-                iBandwidth = Integer.parseInt(tunnel.annotations().value(BANDWIDTH));
+                iBandwidth = Float.parseFloat(tunnel.annotations().value(BANDWIDTH));
             }
             // build bandwidth object
             PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
@@ -1241,6 +1310,10 @@
                 annotationBuilder.set(LOCAL_LSP_ID, String.valueOf(ipv4LspTlv.getLspId()));
             }
 
+            if (tunnel.annotations().value(PCC_TUNNEL_ID) == null) {
+                annotationBuilder.set(PCC_TUNNEL_ID, String.valueOf(ipv4LspTlv.getTunnelId()));
+            }
+
             SparseAnnotations annotations = annotationBuilder.build();
             DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(),
                                                                        tunnel.dst(), tunnel.type(), tunnel.groupId(),
@@ -1248,6 +1321,7 @@
                                                                        annotations);
 
             if (CREATE == pcepTunnelData.requestType()) {
+                pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
                 pcepTunnelApiMapper.handleCreateTunnelRequestQueue(srpId, pcepTunnelData);
             } else if (DELETE == pcepTunnelData.requestType()) {
                 pcepTunnelApiMapper.handleRemoveFromTunnelRequestQueue(srpId, pcepTunnelData);
@@ -1552,7 +1626,8 @@
             List<Link> links = new ArrayList<Link>();
             LinkedList<PcepValueType> llSubObj = eroObj.getSubObjects();
             if (0 == llSubObj.size()) {
-                log.error("ERO in report message does not have hop information");
+                log.debug("ERO in report message does not have hop information");
+                return null;
             }
             ListIterator<PcepValueType> tlvIterator = llSubObj.listIterator();