Priyanka B

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

Change-Id: Ia9ead1babf3de43e6f492f4f3b6f4d6b9377b042
......@@ -80,7 +80,8 @@ public class PceQueryPathCommand extends AbstractShellCommand {
"constraints: \n" +
" cost : %s \n" +
" bandwidth : %s",
tunnel.tunnelId().id(), tunnel.src().toString(), tunnel.dst().toString(),
tunnel.tunnelId().id(), tunnel.path().src().deviceId().toString(),
tunnel.path().dst().deviceId().toString(),
tunnel.type().name(), tunnel.tunnelName(), tunnel.annotations().value(COST_TYPE),
tunnel.annotations().value(AnnotationKeys.BANDWIDTH));
}
......
......@@ -47,7 +47,7 @@ public class PceUpdatePathCommand extends AbstractShellCommand {
@Option(name = "-c", aliases = "--cost", description = "The cost attribute IGP cost (1) or TE cost (2).",
required = false, multiValued = false)
int cost = -1;
Integer cost = null;
@Option(name = "-b", aliases = "--bandwidth", description = "The bandwidth attribute of path. "
+ "Data rate unit is in Bps.", required = false, multiValued = false)
......@@ -66,7 +66,7 @@ public class PceUpdatePathCommand extends AbstractShellCommand {
}
// Cost validation
if (cost != -1) {
if (cost != null) {
if ((cost < 1) || (cost > 2)) {
error("The cost attribute value is either IGP cost(1) or TE cost(2).");
return;
......
......@@ -162,13 +162,14 @@ public final class DefaultPcePath implements PcePath {
@Override
public String toString() {
return toStringHelper(this)
.omitNullValues()
.add("id", id())
.add("source", source)
.add("destination", destination)
.add("lsptype", lspType)
.add("name", name)
.add("costConstraint", costConstraint.toString())
.add("bandwidthConstraint", bandwidthConstraint.toString())
.add("costConstraint", costConstraint)
.add("bandwidthConstraint", bandwidthConstraint)
.toString();
}
......@@ -241,8 +242,8 @@ public final class DefaultPcePath implements PcePath {
@Override
public Builder of(Tunnel tunnel) {
this.id = TunnelId.valueOf(tunnel.tunnelId().id());
this.source = tunnel.src().toString();
this.destination = tunnel.dst().toString();
this.source = tunnel.path().src().deviceId().toString();
this.destination = tunnel.path().dst().deviceId().toString();
this.name = tunnel.tunnelName().toString();
// LSP type
String lspType = tunnel.annotations().value(PcepAnnotationKeys.LSP_SIG_TYPE);
......
......@@ -509,7 +509,7 @@ public class PceManager implements PceService {
}
if (existingBwValue != null) {
if (bwConstraintValue == 0 && bwConstraint != null) {
if (bwConstraint == null) {
bwConstraintValue = existingBwValue.bps();
}
//If bandwidth constraints not specified , take existing bandwidth for shared bandwidth calculation
......@@ -526,6 +526,11 @@ public class PceManager implements PceService {
constraints.add(CapabilityConstraint.of(CapabilityType.valueOf(lspSigType)));
if (costConstraint != null) {
constraints.add(costConstraint);
} else {
//Take cost constraint from old tunnel if it is not specified in update flow
costType = tunnel.annotations().value(COST_TYPE);
costConstraint = CostConstraint.of(CostConstraint.Type.valueOf(costType));
constraints.add(costConstraint);
}
computedPathSet = computePath(links.get(0).src().deviceId(), links.get(links.size() - 1).dst().deviceId(),
......@@ -845,6 +850,10 @@ public class PceManager implements PceService {
return;
}
if (pceStore.getTunnelInfo(tunnel.tunnelId()) == null) {
//If bandwidth for old tunnel is not allocated i,e 0 then no need to release
return;
}
resourceService.release(pceStore.getTunnelInfo(tunnel.tunnelId()).tunnelConsumerId());
return;
......@@ -1211,7 +1220,7 @@ public class PceManager implements PceService {
localLspIdFreeList.add(Short.valueOf(tunnel.annotations().value(LOCAL_LSP_ID)));
}
// If not zero bandwidth, and delegated (initiated LSPs will also be delegated).
if (Float.parseFloat(tunnel.annotations().value(BANDWIDTH)) != 0
if (bwConstraintValue != 0
&& mastershipService.getLocalRole(tunnel.path().src().deviceId()) == MastershipRole.MASTER) {
releaseBandwidth(tunnel);
}
......
......@@ -98,6 +98,17 @@ public class DistributedPceStore implements PceStore {
// List of PCC LSR ids whose BGP device information was not available to perform
// label db sync.
private HashSet<DeviceId> pendinglabelDbSyncPccMap = new HashSet();
private static final Serializer SERIALIZER = Serializer
.using(new KryoNamespace.Builder().register(KryoNamespaces.API)
.register(PcePathInfo.class)
.register(CostConstraint.class)
.register(CostConstraint.Type.class)
.register(BandwidthConstraint.class)
.register(SharedBandwidthConstraint.class)
.register(CapabilityConstraint.class)
.register(CapabilityConstraint.CapabilityType.class)
.register(LspType.class)
.build());
@Activate
protected void activate() {
......@@ -136,19 +147,7 @@ public class DistributedPceStore implements PceStore {
failedPathSet = storageService.<PcePathInfo>setBuilder()
.withName("failed-path-info")
.withSerializer(Serializer.using(
new KryoNamespace.Builder()
.register(KryoNamespaces.API)
.register(PcePathInfo.class,
CostConstraint.class,
CostConstraint.Type.class,
BandwidthConstraint.class,
SharedBandwidthConstraint.class,
CapabilityConstraint.class,
CapabilityConstraint.CapabilityType.class,
LspType.class)
.build()))
.withSerializer(SERIALIZER)
.build()
.asDistributedSet();
......
......@@ -618,7 +618,10 @@ public class PceManagerTest {
build4RouterTopo(false, true, true, true, 100);
// Setup tunnel.
boolean result = pceManager.setupPath(D1.deviceId(), D2.deviceId(), "T123", null, WITH_SIGNALLING);
List<Constraint> constraints = new LinkedList<Constraint>();
CostConstraint costConstraint = new CostConstraint(TE_COST);
constraints.add(costConstraint);
boolean result = pceManager.setupPath(D1.deviceId(), D2.deviceId(), "T123", constraints, WITH_SIGNALLING);
assertThat(result, is(true));
Collection<Tunnel> tunnels = (Collection<Tunnel>) pceManager.queryAllPath();
......
......@@ -21,6 +21,8 @@ import org.onosproject.codec.CodecContext;
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 @@ public final class PcePathCodec extends JsonCodec<PcePath> {
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 @@ public final class PcePathCodec extends JsonCodec<PcePath> {
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 @@ public final class PcePathCodec extends JsonCodec<PcePath> {
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 @@ public final class PcePathCodec extends JsonCodec<PcePath> {
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;
......
......@@ -101,6 +101,9 @@ public class PcePathWebResource extends AbstractWebResource {
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 @@ public class PcePathWebResource extends AbstractWebResource {
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 @@ public class PcePathWebResource extends AbstractWebResource {
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) {
......