Enable UUID and name in TAPI RPCs
[transportpce.git] / tapi / src / main / java / org / opendaylight / transportpce / tapi / connectivity / TapiConnectivityImpl.java
index 7011804a85fc278ba46bc87081fd852f69f9bcba..c62645344fa24bc294d9d33607f23921a1328c61 100644 (file)
@@ -8,7 +8,7 @@
 package org.opendaylight.transportpce.tapi.connectivity;
 
 import com.google.common.util.concurrent.ListenableFuture;
-import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.UUID;
@@ -230,7 +230,7 @@ public class TapiConnectivityImpl implements TapiConnectivityService {
     public ListenableFuture<RpcResult<GetConnectivityServiceDetailsOutput>> getConnectivityServiceDetails(
             GetConnectivityServiceDetailsInput input) {
         // TODO Auto-generated method stub
-        Uuid serviceUuid = new Uuid(input.getServiceIdOrName());
+        Uuid serviceUuid = getUuidFromIput(input.getServiceIdOrName());
         ConnectivityService service = this.tapiContext.getConnectivityService(serviceUuid);
         if (service == null) {
             LOG.error("Service {} doesnt exist in tapi context", input.getServiceIdOrName());
@@ -254,8 +254,7 @@ public class TapiConnectivityImpl implements TapiConnectivityService {
     public ListenableFuture<RpcResult<GetConnectionDetailsOutput>> getConnectionDetails(
             GetConnectionDetailsInput input) {
         // TODO Auto-generated method stub
-        Uuid connectionUuid = new Uuid(UUID.nameUUIDFromBytes(input.getConnectionIdOrName()
-            .getBytes(Charset.forName("UTF-8"))).toString());
+        Uuid connectionUuid = getUuidFromIput(input.getConnectionIdOrName());
         Connection connection = this.tapiContext.getConnection(connectionUuid);
         if (connection == null) {
             LOG.error("Connection {} doesnt exist in tapi context", input.getConnectionIdOrName());
@@ -271,27 +270,36 @@ public class TapiConnectivityImpl implements TapiConnectivityService {
             DeleteConnectivityServiceInput input) {
         //TODO Auto-generated method stub
         // TODO add try
-        Uuid serviceUuid = new Uuid(input.getServiceIdOrName());
-        this.tapiContext.deleteConnectivityService(serviceUuid);
-        ListenableFuture<RpcResult<ServiceDeleteOutput>> output =
-            this.serviceHandler.serviceDelete(new ServiceDeleteInputBuilder()
-                .setServiceDeleteReqInfo(new ServiceDeleteReqInfoBuilder()
-                    .setServiceName(input.getServiceIdOrName())
-                    .setTailRetention(ServiceDeleteReqInfo.TailRetention.No)
-                    .build())
-                .setSdncRequestHeader(new SdncRequestHeaderBuilder()
-                    .setRequestId("request-1")
-                    .setRpcAction(RpcActions.ServiceDelete)
-                    .setNotificationUrl("notification url")
-                    .setRequestSystemId("appname")
-                    .build())
-                .build());
-        if (output == null) {
-            return RpcResultBuilder.<DeleteConnectivityServiceOutput>failed().withError(RpcError.ErrorType.RPC,
-                "Failed to delete Link").buildFuture();
+        if (input.getServiceIdOrName() != null) {
+            try {
+                Uuid serviceUuid = getUuidFromIput(input.getServiceIdOrName());
+                this.tapiContext.deleteConnectivityService(serviceUuid);
+                ListenableFuture<RpcResult<ServiceDeleteOutput>> output =
+                    this.serviceHandler.serviceDelete(new ServiceDeleteInputBuilder()
+                        .setServiceDeleteReqInfo(new ServiceDeleteReqInfoBuilder()
+                            .setServiceName(input.getServiceIdOrName())
+                            .setTailRetention(ServiceDeleteReqInfo.TailRetention.No)
+                            .build())
+                        .setSdncRequestHeader(new SdncRequestHeaderBuilder()
+                            .setRequestId("request-1")
+                            .setRpcAction(RpcActions.ServiceDelete)
+                            .setNotificationUrl("notification url")
+                            .setRequestSystemId("appname")
+                            .build())
+                        .build());
+                RpcResult<ServiceDeleteOutput> rpcResult = output.get();
+                if (!rpcResult.getResult().getConfigurationResponseCommon().getResponseCode()
+                        .equals(ResponseCodes.RESPONSE_FAILED)) {
+                    LOG.info("Service is being deleted and devices are being rolled back");
+                    return RpcResultBuilder.success(new DeleteConnectivityServiceOutputBuilder().build()).buildFuture();
+                }
+                LOG.error("Failed to delete service. Deletion process failed");
+            } catch (InterruptedException | ExecutionException e) {
+                LOG.error("Failed to delete service.", e);
+            }
         }
-        LOG.info("Service is being deleted and devices are being rolled back");
-        return RpcResultBuilder.success(new DeleteConnectivityServiceOutputBuilder().build()).buildFuture();
+        return RpcResultBuilder.<DeleteConnectivityServiceOutput>failed().withError(RpcError.ErrorType.RPC,
+            "Failed to delete Service").buildFuture();
     }
 
     @Override
@@ -320,14 +328,10 @@ public class TapiConnectivityImpl implements TapiConnectivityService {
     public ListenableFuture<RpcResult<GetConnectionEndPointDetailsOutput>> getConnectionEndPointDetails(
             GetConnectionEndPointDetailsInput input) {
         // TODO Auto-generated method stub
-        Uuid topoUuid = new Uuid(UUID.nameUUIDFromBytes(input.getTopologyIdOrName()
-            .getBytes(Charset.forName("UTF-8"))).toString());
-        Uuid nodeUuid = new Uuid(UUID.nameUUIDFromBytes(input.getNodeIdOrName()
-            .getBytes(Charset.forName("UTF-8"))).toString());
-        Uuid nepUuid = new Uuid(UUID.nameUUIDFromBytes(input.getNepIdOrName()
-            .getBytes(Charset.forName("UTF-8"))).toString());
-        Uuid cepUuid = new Uuid(UUID.nameUUIDFromBytes(input.getCepIdOrName()
-            .getBytes(Charset.forName("UTF-8"))).toString());
+        Uuid topoUuid = getUuidFromIput(input.getTopologyIdOrName());
+        Uuid nodeUuid = getUuidFromIput(input.getNodeIdOrName());
+        Uuid nepUuid = getUuidFromIput(input.getNepIdOrName());
+        Uuid cepUuid = getUuidFromIput(input.getCepIdOrName());
         ConnectionEndPoint cep = this.tapiContext.getTapiCEP(topoUuid, nodeUuid, nepUuid, cepUuid);
         if (cep == null) {
             LOG.error("Cep doesnt exist in tapi context");
@@ -337,4 +341,15 @@ public class TapiConnectivityImpl implements TapiConnectivityService {
         return RpcResultBuilder.success(new GetConnectionEndPointDetailsOutputBuilder().setConnectionEndPoint(
             new ConnectionEndPointBuilder(cep).build()).build()).buildFuture();
     }
+
+    private Uuid getUuidFromIput(String serviceIdOrName) {
+        try {
+            UUID.fromString(serviceIdOrName);
+            LOG.info("Given attribute {} is a UUID", serviceIdOrName);
+            return new Uuid(serviceIdOrName);
+        } catch (IllegalArgumentException e) {
+            LOG.info("Given attribute {} is not a UUID", serviceIdOrName);
+            return new Uuid(UUID.nameUUIDFromBytes(serviceIdOrName.getBytes(StandardCharsets.UTF_8)).toString());
+        }
+    }
 }