Priyanka B

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

Change-Id: Id801fdccb8110759f55034fc8ee00ac5c40eca41
...@@ -18,6 +18,7 @@ package org.onosproject.pcerest; ...@@ -18,6 +18,7 @@ package org.onosproject.pcerest;
18 import static javax.ws.rs.core.Response.Status.OK; 18 import static javax.ws.rs.core.Response.Status.OK;
19 import static org.onlab.util.Tools.nullIsNotFound; 19 import static org.onlab.util.Tools.nullIsNotFound;
20 20
21 +import java.util.Collection;
21 import java.io.IOException; 22 import java.io.IOException;
22 import java.io.InputStream; 23 import java.io.InputStream;
23 import java.util.List; 24 import java.util.List;
...@@ -36,6 +37,7 @@ import javax.ws.rs.core.Response; ...@@ -36,6 +37,7 @@ import javax.ws.rs.core.Response;
36 37
37 import org.onosproject.incubator.net.tunnel.Tunnel; 38 import org.onosproject.incubator.net.tunnel.Tunnel;
38 import org.onosproject.incubator.net.tunnel.TunnelId; 39 import org.onosproject.incubator.net.tunnel.TunnelId;
40 +import org.onosproject.incubator.net.tunnel.TunnelService;
39 import org.onosproject.net.DeviceId; 41 import org.onosproject.net.DeviceId;
40 import org.onosproject.net.intent.Constraint; 42 import org.onosproject.net.intent.Constraint;
41 import org.onosproject.pce.pceservice.api.PceService; 43 import org.onosproject.pce.pceservice.api.PceService;
...@@ -118,7 +120,17 @@ public class PcePathWebResource extends AbstractWebResource { ...@@ -118,7 +120,17 @@ public class PcePathWebResource extends AbstractWebResource {
118 try { 120 try {
119 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); 121 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
120 JsonNode port = jsonTree.get("path"); 122 JsonNode port = jsonTree.get("path");
123 + TunnelService tunnelService = get(TunnelService.class);
121 PcePath path = codec(PcePath.class).decode((ObjectNode) port, this); 124 PcePath path = codec(PcePath.class).decode((ObjectNode) port, this);
125 + //Validating tunnel name, duplicated tunnel names not allowed
126 + Collection<Tunnel> existingTunnels = tunnelService.queryTunnel(Tunnel.Type.MPLS);
127 + if (existingTunnels != null) {
128 + for (Tunnel t : existingTunnels) {
129 + if (t.tunnelName().toString().equals(path.name())) {
130 + return Response.status(OK).entity(PCE_SETUP_PATH_FAILED).build();
131 + }
132 + }
133 + }
122 134
123 DeviceId srcDevice = DeviceId.deviceId(path.source()); 135 DeviceId srcDevice = DeviceId.deviceId(path.source());
124 DeviceId dstDevice = DeviceId.deviceId(path.destination()); 136 DeviceId dstDevice = DeviceId.deviceId(path.destination());
......
...@@ -24,7 +24,6 @@ import static org.hamcrest.Matchers.is; ...@@ -24,7 +24,6 @@ import static org.hamcrest.Matchers.is;
24 import static org.hamcrest.Matchers.notNullValue; 24 import static org.hamcrest.Matchers.notNullValue;
25 import static org.junit.Assert.assertThat; 25 import static org.junit.Assert.assertThat;
26 import static org.junit.Assert.fail; 26 import static org.junit.Assert.fail;
27 -
28 import static org.onosproject.net.Link.Type.DIRECT; 27 import static org.onosproject.net.Link.Type.DIRECT;
29 28
30 import com.eclipsesource.json.Json; 29 import com.eclipsesource.json.Json;
...@@ -34,6 +33,7 @@ import java.io.InputStream; ...@@ -34,6 +33,7 @@ import java.io.InputStream;
34 import java.net.HttpURLConnection; 33 import java.net.HttpURLConnection;
35 import java.util.LinkedList; 34 import java.util.LinkedList;
36 import java.util.List; 35 import java.util.List;
36 +
37 import javax.ws.rs.client.Entity; 37 import javax.ws.rs.client.Entity;
38 import javax.ws.rs.client.WebTarget; 38 import javax.ws.rs.client.WebTarget;
39 import javax.ws.rs.core.MediaType; 39 import javax.ws.rs.core.MediaType;
...@@ -43,7 +43,6 @@ import javax.ws.rs.NotFoundException; ...@@ -43,7 +43,6 @@ import javax.ws.rs.NotFoundException;
43 import org.junit.After; 43 import org.junit.After;
44 import org.junit.Before; 44 import org.junit.Before;
45 import org.junit.Test; 45 import org.junit.Test;
46 -
47 import org.onlab.osgi.ServiceDirectory; 46 import org.onlab.osgi.ServiceDirectory;
48 import org.onlab.osgi.TestServiceDirectory; 47 import org.onlab.osgi.TestServiceDirectory;
49 import org.onlab.packet.IpAddress; 48 import org.onlab.packet.IpAddress;
...@@ -56,6 +55,7 @@ import org.onosproject.incubator.net.tunnel.Tunnel; ...@@ -56,6 +55,7 @@ import org.onosproject.incubator.net.tunnel.Tunnel;
56 import org.onosproject.incubator.net.tunnel.TunnelId; 55 import org.onosproject.incubator.net.tunnel.TunnelId;
57 import org.onosproject.incubator.net.tunnel.TunnelEndPoint; 56 import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
58 import org.onosproject.incubator.net.tunnel.TunnelName; 57 import org.onosproject.incubator.net.tunnel.TunnelName;
58 +import org.onosproject.incubator.net.tunnel.TunnelService;
59 import org.onosproject.net.ConnectPoint; 59 import org.onosproject.net.ConnectPoint;
60 import org.onosproject.net.DefaultAnnotations; 60 import org.onosproject.net.DefaultAnnotations;
61 import org.onosproject.net.DefaultLink; 61 import org.onosproject.net.DefaultLink;
...@@ -73,6 +73,7 @@ import org.onosproject.net.provider.ProviderId; ...@@ -73,6 +73,7 @@ import org.onosproject.net.provider.ProviderId;
73 */ 73 */
74 public class PcePathResourceTest extends PceResourceTest { 74 public class PcePathResourceTest extends PceResourceTest {
75 private final PceService pceService = createMock(PceService.class); 75 private final PceService pceService = createMock(PceService.class);
76 + private final TunnelService tunnelService = createMock(TunnelService.class);
76 private final TunnelEndPoint src = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(23423)); 77 private final TunnelEndPoint src = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(23423));
77 private final TunnelEndPoint dst = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(32421)); 78 private final TunnelEndPoint dst = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(32421));
78 private final DefaultGroupId groupId = new DefaultGroupId(92034); 79 private final DefaultGroupId groupId = new DefaultGroupId(92034);
...@@ -100,6 +101,7 @@ public class PcePathResourceTest extends PceResourceTest { ...@@ -100,6 +101,7 @@ public class PcePathResourceTest extends PceResourceTest {
100 // Mock environment setup 101 // Mock environment setup
101 MockPceCodecContext context = new MockPceCodecContext(); 102 MockPceCodecContext context = new MockPceCodecContext();
102 ServiceDirectory testDirectory = new TestServiceDirectory().add(PceService.class, pceService) 103 ServiceDirectory testDirectory = new TestServiceDirectory().add(PceService.class, pceService)
104 + .add(TunnelService.class, tunnelService)
103 .add(CodecService.class, context.codecManager()); 105 .add(CodecService.class, context.codecManager());
104 BaseResource.setServiceDirectory(testDirectory); 106 BaseResource.setServiceDirectory(testDirectory);
105 107
......
...@@ -197,10 +197,20 @@ public class PceWebTopovMessageHandler extends UiMessageHandler { ...@@ -197,10 +197,20 @@ public class PceWebTopovMessageHandler extends UiMessageHandler {
197 String lspType = string(payload, LSPTYPE); 197 String lspType = string(payload, LSPTYPE);
198 String tunnelName = string(payload, TUNNEL_NAME); 198 String tunnelName = string(payload, TUNNEL_NAME);
199 199
200 - if (tunnelName.equals(STRING_NULL)) { 200 + if (tunnelName == null || tunnelName.equals(STRING_NULL)) {
201 - log.error("PCE setup path is failed as tunnel name should not be empty"); 201 + log.error("tunnel name should not be empty");
202 return; 202 return;
203 } 203 }
204 + //Validating tunnel name, duplicated tunnel names not allowed
205 + Collection<Tunnel> existingTunnels = tunnelService.queryTunnel(Tunnel.Type.MPLS);
206 + if (existingTunnels != null) {
207 + for (Tunnel t : existingTunnels) {
208 + if (t.tunnelName().toString().equals(tunnelName)) {
209 + log.error("Path creation failed, Tunnel name already exists");
210 + return;
211 + }
212 + }
213 + }
204 214
205 if (pceService == null) { 215 if (pceService == null) {
206 log.error("PCE service is not active"); 216 log.error("PCE service is not active");
......