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

Change-Id: Id801fdccb8110759f55034fc8ee00ac5c40eca41
(cherry picked from commit edf1cd84129199451560428786d514592f2480e0)
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 1b7c48b..2b0ba86 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
@@ -18,6 +18,7 @@
 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 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 @@
         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());
diff --git a/apps/pce/pcerest/src/test/java/org/onosproject/pcerest/PcePathResourceTest.java b/apps/pce/pcerest/src/test/java/org/onosproject/pcerest/PcePathResourceTest.java
index a61a294..862fb14 100644
--- a/apps/pce/pcerest/src/test/java/org/onosproject/pcerest/PcePathResourceTest.java
+++ b/apps/pce/pcerest/src/test/java/org/onosproject/pcerest/PcePathResourceTest.java
@@ -24,7 +24,6 @@
 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.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 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.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 @@
  */
 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 @@
        // 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);
 
diff --git a/apps/pce/pceweb/src/main/java/org/onosproject/pceweb/PceWebTopovMessageHandler.java b/apps/pce/pceweb/src/main/java/org/onosproject/pceweb/PceWebTopovMessageHandler.java
index 2c2ff0f..4b848a6 100644
--- a/apps/pce/pceweb/src/main/java/org/onosproject/pceweb/PceWebTopovMessageHandler.java
+++ b/apps/pce/pceweb/src/main/java/org/onosproject/pceweb/PceWebTopovMessageHandler.java
@@ -197,10 +197,20 @@
             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");