add gnpy results to pce:path-computation-request
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / service / ServiceDataStoreOperationsImpl.java
index 372b7b801be95041298ab4d436c9b61c1e8caddc..5e91b983ab3736764b8b7c81af771eba26f7e388 100644 (file)
@@ -12,6 +12,7 @@ import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
+
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
@@ -19,11 +20,15 @@ import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.transportpce.common.OperationResult;
 import org.opendaylight.transportpce.common.Timeouts;
 import org.opendaylight.transportpce.servicehandler.ModelMappingUtils;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.PathComputationRequestOutput;
+import org.opendaylight.transportpce.servicehandler.ServiceInput;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev190624.PathComputationRequestOutput;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.State;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceCreateInput;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceList;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceListBuilder;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.TempServiceCreateInput;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.TempServiceList;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.TempServiceListBuilder;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.Services;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.ServicesBuilder;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.ServicesKey;
@@ -35,10 +40,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperations {
-
     private static final Logger LOG = LoggerFactory.getLogger(ServiceDataStoreOperationsImpl.class);
     private static final String SUCCESSFUL_MESSAGE = "Successful";
-
     private DataBroker dataBroker;
 
     public ServiceDataStoreOperationsImpl(DataBroker dataBroker) {
@@ -47,6 +50,11 @@ public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperation
 
     @Override
     public void initialize() {
+        initializeServiceList();
+        initializeTempServiceList();
+    }
+
+    private void initializeServiceList() {
         try {
             LOG.info("initializing service registry");
             WriteTransaction transaction = this.dataBroker.newWriteOnlyTransaction();
@@ -60,14 +68,47 @@ public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperation
         }
     }
 
+    private void initializeTempServiceList() {
+        try {
+            LOG.info("initializing temp service registry");
+            WriteTransaction transaction = this.dataBroker.newWriteOnlyTransaction();
+            InstanceIdentifier<TempServiceList> iid = InstanceIdentifier.create(TempServiceList.class);
+            TempServiceList initialRegistry = new TempServiceListBuilder().build();
+            transaction.put(LogicalDatastoreType.OPERATIONAL, iid, initialRegistry);
+            Future<Void> future = transaction.submit();
+            future.get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
+        } catch (InterruptedException | ExecutionException | TimeoutException e) {
+            LOG.warn("init failed: {}", e.getMessage());
+        }
+    }
+
     @Override
     public Optional<Services> getService(String serviceName) {
         try {
             ReadOnlyTransaction readTx = this.dataBroker.newReadOnlyTransaction();
-            InstanceIdentifier<Services> iid = InstanceIdentifier
-                    .create(ServiceList.class).child(Services.class, new ServicesKey(serviceName));
-            Future<com.google.common.base.Optional<Services>> future
-                    = readTx.read(LogicalDatastoreType.OPERATIONAL, iid);
+            InstanceIdentifier<Services> iid =
+                    InstanceIdentifier.create(ServiceList.class).child(Services.class, new ServicesKey(serviceName));
+            Future<com.google.common.base.Optional<Services>> future =
+                    readTx.read(LogicalDatastoreType.OPERATIONAL, iid);
+            return future.get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS).toJavaUtil();
+        } catch (InterruptedException | ExecutionException | TimeoutException e) {
+            LOG.warn("Reading service {} failed:", serviceName, e);
+        }
+        return Optional.empty();
+    }
+
+    @Override
+    public Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
+        .Services> getTempService(String serviceName) {
+        try {
+            ReadOnlyTransaction readTx = this.dataBroker.newReadOnlyTransaction();
+            InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
+                .Services> iid = InstanceIdentifier.create(TempServiceList.class).child(org.opendaylight.yang.gen.v1
+                        .http.org.openroadm.service.rev161014.temp.service.list.Services.class,
+                        new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
+                            .ServicesKey(serviceName));
+            Future<com.google.common.base.Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014
+                .temp.service.list.Services>> future =  readTx.read(LogicalDatastoreType.OPERATIONAL, iid);
             return future.get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS).toJavaUtil();
         } catch (InterruptedException | ExecutionException | TimeoutException e) {
             LOG.warn("Reading service {} failed:", serviceName, e);
@@ -80,8 +121,8 @@ public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperation
         LOG.debug("Deleting '{}' Service", serviceName);
         try {
             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
-            InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class)
-                    .child(Services.class, new ServicesKey(serviceName));
+            InstanceIdentifier<Services> iid =
+                    InstanceIdentifier.create(ServiceList.class).child(Services.class, new ServicesKey(serviceName));
             writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
             writeTx.submit().get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS);
             return OperationResult.ok(SUCCESSFUL_MESSAGE);
@@ -92,6 +133,26 @@ public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperation
         }
     }
 
+    @Override
+    public OperationResult deleteTempService(String commonId) {
+        LOG.debug("Deleting '{}' Service", commonId);
+        try {
+            WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
+            InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
+                .Services> iid = InstanceIdentifier.create(TempServiceList.class).child(org.opendaylight.yang.gen.v1
+                        .http.org.openroadm.service.rev161014.temp.service.list.Services.class,
+                        new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
+                            .ServicesKey(commonId));
+            writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
+            writeTx.submit().get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS);
+            return OperationResult.ok(SUCCESSFUL_MESSAGE);
+        } catch (TimeoutException | InterruptedException | ExecutionException e) {
+            String message = "Failed to delete service " + commonId + " from Service List";
+            LOG.warn(message, e);
+            return OperationResult.failed(message);
+        }
+    }
+
     @Override
     public OperationResult modifyService(String serviceName, State operationalState, State administrativeState) {
         LOG.debug("Modifying '{}' Service", serviceName);
@@ -101,8 +162,7 @@ public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperation
                 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
                 InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class)
                         .child(Services.class, new ServicesKey(serviceName));
-                Services services = new ServicesBuilder(readService.get())
-                        .setOperationalState(operationalState)
+                Services services = new ServicesBuilder(readService.get()).setOperationalState(operationalState)
                         .setAdministrativeState(administrativeState)
                         .build();
                 writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, services);
@@ -121,13 +181,44 @@ public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperation
     }
 
     @Override
-    public OperationResult createService(ServiceCreateInput serviceCreateInput,
-            PathComputationRequestOutput outputFromPce) {
+    public OperationResult modifyTempService(String serviceName, State operationalState, State administrativeState) {
+        LOG.debug("Modifying '{}' Temp Service", serviceName);
+        Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
+            .Services> readService = getTempService(serviceName);
+        if (readService.isPresent()) {
+            try {
+                WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
+                InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
+                    .Services> iid = InstanceIdentifier.create(TempServiceList.class)
+                        .child(org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
+                                .Services.class, new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014
+                                    .temp.service.list.ServicesKey(serviceName));
+                org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
+                    .Services services = new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp
+                        .service.list.ServicesBuilder(readService.get()).setOperationalState(operationalState)
+                            .setAdministrativeState(administrativeState)
+                            .build();
+                writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, services);
+                writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
+                return OperationResult.ok(SUCCESSFUL_MESSAGE);
+            } catch (TimeoutException | InterruptedException | ExecutionException e) {
+                String message = "Failed to modify temp service " + serviceName + " from Temp Service List";
+                LOG.warn(message, e);
+                return OperationResult.failed(message);
+            }
+        } else {
+            String message = "Temp Service " + serviceName + " is not present!";
+            LOG.warn(message);
+            return OperationResult.failed(message);
+        }
+    }
+
+    @Override
+    public OperationResult createService(ServiceCreateInput serviceCreateInput) {
         LOG.debug("Writing '{}' Service", serviceCreateInput.getServiceName());
         try {
-            InstanceIdentifier<Services> iid = InstanceIdentifier
-                    .create(ServiceList.class).child(Services.class,
-                            new ServicesKey(serviceCreateInput.getServiceName()));
+            InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class)
+                    .child(Services.class, new ServicesKey(serviceCreateInput.getServiceName()));
             Services service = ModelMappingUtils.mappingServices(serviceCreateInput, null);
             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
             writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
@@ -141,21 +232,41 @@ public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperation
     }
 
     @Override
-    public OperationResult createServicePath(ServiceCreateInput serviceCreateInput,
-            PathComputationRequestOutput outputFromPce) {
-        LOG.debug("Writing '{}' Service", serviceCreateInput.getServiceName());
+    public OperationResult createTempService(TempServiceCreateInput tempServiceCreateInput) {
+        LOG.debug("Writing '{}' Temp Service", tempServiceCreateInput.getCommonId());
+        try {
+            InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
+                .Services> iid = InstanceIdentifier.create(TempServiceList.class)
+                    .child(org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
+                            .Services.class, new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp
+                                .service.list.ServicesKey(tempServiceCreateInput.getCommonId()));
+            org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
+                .Services service = ModelMappingUtils.mappingServices(tempServiceCreateInput);
+            WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
+            writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
+            writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
+            return OperationResult.ok(SUCCESSFUL_MESSAGE);
+        } catch (TimeoutException | InterruptedException | ExecutionException e) {
+            String message = "Failed to create Temp service " + tempServiceCreateInput.getCommonId()
+                + " to TempService List";
+            LOG.warn(message, e);
+            return OperationResult.failed(message);
+        }
+    }
+
+    @Override
+    public OperationResult createServicePath(ServiceInput serviceInput, PathComputationRequestOutput outputFromPce) {
+        LOG.debug("Writing '{}' ServicePath ", serviceInput.getServiceName());
         try {
-            InstanceIdentifier<ServicePaths> servicePathsIID = InstanceIdentifier
-                    .create(ServicePathList.class)
-                    .child(ServicePaths.class, new ServicePathsKey(serviceCreateInput.getServiceName()));
-            ServicePaths servicePath =  ModelMappingUtils.mappingServicePaths(serviceCreateInput, null, outputFromPce);
+            InstanceIdentifier<ServicePaths> servicePathsIID = InstanceIdentifier.create(ServicePathList.class)
+                    .child(ServicePaths.class, new ServicePathsKey(serviceInput.getServiceName()));
+            ServicePaths servicePath = ModelMappingUtils.mappingServicePaths(serviceInput, outputFromPce);
             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
             writeTx.put(LogicalDatastoreType.OPERATIONAL, servicePathsIID, servicePath);
             writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
             return OperationResult.ok(SUCCESSFUL_MESSAGE);
         } catch (TimeoutException | InterruptedException | ExecutionException e) {
-            String message = "Failed to create servicePath " + serviceCreateInput.getServiceName()
-                + " to ServicePath List";
+            String message = "Failed to create servicePath " + serviceInput.getCommonId() + " to ServicePath List";
             LOG.warn(message, e);
             return OperationResult.failed(message);
         }
@@ -181,21 +292,21 @@ public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperation
     /*
      * Write or Modify or Delete Service from/to SreviceList.
      *
-     * @param serviceName
-     *            Name of service
-     * @param input
-     *            ServiceCreateInput
-     * @param output
-     *            PathComputationRequestOutput
-     * @param choice
-     *            0 - Modify 1 - Delete 2 - Write
+     * @param serviceName Name of service
+     *
+     * @param input ServiceCreateInput
+     *
+     * @param output PathComputationRequestOutput
+     *
+     * @param choice 0 - Modify 1 - Delete 2 - Write
+     *
      * @return String operations result, null if ok or not otherwise
      */
     @Deprecated
     @Override
     public String writeOrModifyOrDeleteServiceList(String serviceName, ServiceCreateInput input,
-                                                    PathComputationRequestOutput output, int choice) {
-        LOG.debug("WriteOrModifyOrDeleting '{}' Service",serviceName);
+            PathComputationRequestOutput output, int choice) {
+        LOG.debug("WriteOrModifyOrDeleting '{}' Service", serviceName);
         WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
         String result = null;
         Optional<Services> readService = getService(serviceName);
@@ -203,29 +314,25 @@ public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperation
             /*
              * Modify / Delete Service.
              */
-            InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class).child(Services.class,
-                    new ServicesKey(serviceName));
+            InstanceIdentifier<Services> iid =
+                    InstanceIdentifier.create(ServiceList.class).child(Services.class, new ServicesKey(serviceName));
             ServicesBuilder service = new ServicesBuilder(readService.get());
-
             String action = null;
             switch (choice) {
-                case 0: /* Modify. */
+                case 0 : /* Modify. */
                     LOG.debug("Modifying '{}' Service", serviceName);
                     service.setOperationalState(State.InService).setAdministrativeState(State.InService);
                     writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, service.build());
                     action = "modifyService";
                     break;
-
-                case 1: /* Delete */
+                case 1 : /* Delete */
                     LOG.debug("Deleting '{}' Service", serviceName);
                     writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
                     action = "deleteService";
                     break;
-
                 default:
                     LOG.debug("No choice found");
                     break;
-
             }
             try {
                 writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
@@ -236,9 +343,8 @@ public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperation
         } else {
             if (choice == 2) { /* Write Service */
                 LOG.debug("Writing '{}' Service", serviceName);
-                InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class).child(Services.class,
-                        new ServicesKey(serviceName));
-
+                InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class)
+                        .child(Services.class, new ServicesKey(serviceName));
                 Services service = ModelMappingUtils.mappingServices(input, null);
                 writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
                 try {
@@ -255,5 +361,4 @@ public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperation
         }
         return result;
     }
-
 }