cherry pick [ONOS-4986] [ONOS-4985] Json defect fix to master

Change-Id: Ia9ead1babf3de43e6f492f4f3b6f4d6b9377b042
diff --git a/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/PcePathCodec.java b/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/PcePathCodec.java
index 034d045..7692c7d 100644
--- a/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/PcePathCodec.java
+++ b/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/PcePathCodec.java
@@ -21,6 +21,8 @@
 import org.onosproject.codec.JsonCodec;
 import org.onosproject.pce.pceservice.PcePath;
 import org.onosproject.pce.pceservice.DefaultPcePath;
+import org.onosproject.net.intent.constraint.BandwidthConstraint;
+import org.onosproject.pce.pceservice.constraint.CostConstraint;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -70,6 +72,11 @@
         jNode = json.get(LSP_TYPE);
         if (jNode != null) {
             String lspType = jNode.asText();
+            //Validating LSP type
+            int type = Integer.parseInt(lspType);
+            if ((type < 0) || (type > 2)) {
+                return null;
+            }
             resultBuilder.lspType(lspType);
         }
 
@@ -87,6 +94,11 @@
             jNode = constraintJNode.get(COST);
             if (jNode != null) {
                 String cost = jNode.asText();
+                //Validating Cost type
+                int costType = Integer.parseInt(cost);
+                if ((costType < 1) || (costType > 2)) {
+                    return null;
+                }
                 resultBuilder.costConstraint(cost);
             }
 
@@ -94,6 +106,10 @@
             jNode = constraintJNode.get(BANDWIDTH);
             if (jNode != null) {
                 String bandwidth = jNode.asText();
+                double bw = Double.parseDouble(bandwidth);
+                if (bw < 0) {
+                    return null;
+                }
                 resultBuilder.bandwidthConstraint(bandwidth);
             }
         }
@@ -114,8 +130,8 @@
 
         ObjectNode constraintNode = context.mapper()
                 .createObjectNode()
-                .put(COST, path.costConstraint().toString())
-                .put(BANDWIDTH, path.bandwidthConstraint().toString());
+                .put(COST, ((CostConstraint) path.costConstraint()).type().type())
+                .put(BANDWIDTH, ((BandwidthConstraint) path.bandwidthConstraint()).bandwidth().bps());
 
         result.set(CONSTRAINT, constraintNode);
         return result;
diff --git a/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/PcePathWebResource.java b/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/PcePathWebResource.java
index 2b0ba86..a3cd11e 100644
--- a/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/PcePathWebResource.java
+++ b/apps/pce/pcerest/src/main/java/org/onosproject/pcerest/PcePathWebResource.java
@@ -101,6 +101,9 @@
         Tunnel tunnel = nullIsNotFound(get(PceService.class).queryPath(TunnelId.valueOf(id)),
                                        PCE_PATH_NOT_FOUND);
         PcePath path = DefaultPcePath.builder().of(tunnel).build();
+        if (path == null) {
+            return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build();
+        }
         ObjectNode result = mapper().createObjectNode();
         result.set("path", codec(PcePath.class).encode(path, this));
         return ok(result.toString()).build();
@@ -122,6 +125,10 @@
             JsonNode port = jsonTree.get("path");
             TunnelService tunnelService = get(TunnelService.class);
             PcePath path = codec(PcePath.class).decode((ObjectNode) port, this);
+            if (path == null) {
+                return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build();
+            }
+
             //Validating tunnel name, duplicated tunnel names not allowed
             Collection<Tunnel> existingTunnels = tunnelService.queryTunnel(Tunnel.Type.MPLS);
             if (existingTunnels != null) {
@@ -171,6 +178,9 @@
             ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
             JsonNode pathNode = jsonTree.get("path");
             PcePath path = codec(PcePath.class).decode((ObjectNode) pathNode, this);
+            if (path == null) {
+                return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build();
+            }
             List<Constraint> constrntList = new LinkedList<Constraint>();
             // Assign bandwidth
             if (path.bandwidthConstraint() != null) {