Priyanka B

[ONOS] duplicate tunnelname not allowed in PCE REST and WEB

Change-Id: Id801fdccb8110759f55034fc8ee00ac5c40eca41
......@@ -18,6 +18,7 @@ package org.onosproject.pcerest;
import static javax.ws.rs.core.Response.Status.OK;
import static org.onlab.util.Tools.nullIsNotFound;
import java.util.Collection;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
......@@ -36,6 +37,7 @@ import javax.ws.rs.core.Response;
import org.onosproject.incubator.net.tunnel.Tunnel;
import org.onosproject.incubator.net.tunnel.TunnelId;
import org.onosproject.incubator.net.tunnel.TunnelService;
import org.onosproject.net.DeviceId;
import org.onosproject.net.intent.Constraint;
import org.onosproject.pce.pceservice.api.PceService;
......@@ -118,7 +120,17 @@ public class PcePathWebResource extends AbstractWebResource {
try {
ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
JsonNode port = jsonTree.get("path");
TunnelService tunnelService = get(TunnelService.class);
PcePath path = codec(PcePath.class).decode((ObjectNode) port, this);
//Validating tunnel name, duplicated tunnel names not allowed
Collection<Tunnel> existingTunnels = tunnelService.queryTunnel(Tunnel.Type.MPLS);
if (existingTunnels != null) {
for (Tunnel t : existingTunnels) {
if (t.tunnelName().toString().equals(path.name())) {
return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build();
}
}
}
DeviceId srcDevice = DeviceId.deviceId(path.source());
DeviceId dstDevice = DeviceId.deviceId(path.destination());
......
......@@ -24,7 +24,6 @@ import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.onosproject.net.Link.Type.DIRECT;
import com.eclipsesource.json.Json;
......@@ -34,6 +33,7 @@ import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.LinkedList;
import java.util.List;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
......@@ -43,7 +43,6 @@ import javax.ws.rs.NotFoundException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.osgi.ServiceDirectory;
import org.onlab.osgi.TestServiceDirectory;
import org.onlab.packet.IpAddress;
......@@ -56,6 +55,7 @@ import org.onosproject.incubator.net.tunnel.Tunnel;
import org.onosproject.incubator.net.tunnel.TunnelId;
import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
import org.onosproject.incubator.net.tunnel.TunnelName;
import org.onosproject.incubator.net.tunnel.TunnelService;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DefaultAnnotations;
import org.onosproject.net.DefaultLink;
......@@ -73,6 +73,7 @@ import org.onosproject.net.provider.ProviderId;
*/
public class PcePathResourceTest extends PceResourceTest {
private final PceService pceService = createMock(PceService.class);
private final TunnelService tunnelService = createMock(TunnelService.class);
private final TunnelEndPoint src = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(23423));
private final TunnelEndPoint dst = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(32421));
private final DefaultGroupId groupId = new DefaultGroupId(92034);
......@@ -100,6 +101,7 @@ public class PcePathResourceTest extends PceResourceTest {
// Mock environment setup
MockPceCodecContext context = new MockPceCodecContext();
ServiceDirectory testDirectory = new TestServiceDirectory().add(PceService.class, pceService)
.add(TunnelService.class, tunnelService)
.add(CodecService.class, context.codecManager());
BaseResource.setServiceDirectory(testDirectory);
......
......@@ -197,10 +197,20 @@ public class PceWebTopovMessageHandler extends UiMessageHandler {
String lspType = string(payload, LSPTYPE);
String tunnelName = string(payload, TUNNEL_NAME);
if (tunnelName.equals(STRING_NULL)) {
log.error("PCE setup path is failed as tunnel name should not be empty");
if (tunnelName == null || tunnelName.equals(STRING_NULL)) {
log.error("tunnel name should not be empty");
return;
}
//Validating tunnel name, duplicated tunnel names not allowed
Collection<Tunnel> existingTunnels = tunnelService.queryTunnel(Tunnel.Type.MPLS);
if (existingTunnels != null) {
for (Tunnel t : existingTunnels) {
if (t.tunnelName().toString().equals(tunnelName)) {
log.error("Path creation failed, Tunnel name already exists");
return;
}
}
}
if (pceService == null) {
log.error("PCE service is not active");
......