X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=servicehandler%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Ftransportpce%2Fservicehandler%2Fservice%2FServiceDataStoreOperationsImpl.java;h=9c9c8de52c3e0b5135fcba8947944338330be587;hb=116031ee55870cc5a3c90a18716fbd0fc26bb399;hp=372b7b801be95041298ab4d436c9b61c1e8caddc;hpb=0779ad5796bb645690874959c9a891c42e2f7175;p=transportpce.git diff --git a/servicehandler/src/main/java/org/opendaylight/transportpce/servicehandler/service/ServiceDataStoreOperationsImpl.java b/servicehandler/src/main/java/org/opendaylight/transportpce/servicehandler/service/ServiceDataStoreOperationsImpl.java index 372b7b801..9c9c8de52 100644 --- a/servicehandler/src/main/java/org/opendaylight/transportpce/servicehandler/service/ServiceDataStoreOperationsImpl.java +++ b/servicehandler/src/main/java/org/opendaylight/transportpce/servicehandler/service/ServiceDataStoreOperationsImpl.java @@ -7,68 +7,159 @@ */ package org.opendaylight.transportpce.servicehandler.service; +import com.google.common.util.concurrent.FluentFuture; import java.util.Optional; 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; -import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.eclipse.jdt.annotation.NonNull; +import org.opendaylight.mdsal.binding.api.DataBroker; +import org.opendaylight.mdsal.binding.api.WriteTransaction; +import org.opendaylight.mdsal.common.api.CommitInfo; +import org.opendaylight.mdsal.common.api.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.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.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; +import org.opendaylight.transportpce.servicehandler.ServiceInput; +import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev230925.PathComputationRequestOutput; +import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev191129.State; +import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev191129.AdminStates; +import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceCreateInput; +import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceList; +import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceListBuilder; +import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.TempServiceCreateInput; +import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.TempServiceList; +import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.TempServiceListBuilder; +import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.service.list.Services; +import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.service.list.ServicesBuilder; +import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.service.list.ServicesKey; +import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.service.path.PathDescription; import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.ServicePathList; import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePaths; +import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePathsBuilder; import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePathsKey; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +@Component(immediate = true) public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperations { - private static final Logger LOG = LoggerFactory.getLogger(ServiceDataStoreOperationsImpl.class); - private static final String SUCCESSFUL_MESSAGE = "Successful"; - + private static final String CREATE_MSG = "create"; + private static final String DELETING_SERVICE_MSG = "Deleting '{}' Service"; private DataBroker dataBroker; - public ServiceDataStoreOperationsImpl(DataBroker dataBroker) { + // This is class is public so that these messages can be accessed from Junit (avoid duplications). + public static final class LogMessages { + + public static final String SUCCESSFUL_MESSAGE; + public static final String SERVICE_NOT_FOUND; + public static final String SERVICE_PATH_NOT_FOUND; + + // Static blocks are generated once and spare memory. + static { + SUCCESSFUL_MESSAGE = "Successful"; + SERVICE_NOT_FOUND = "Service not found"; + SERVICE_PATH_NOT_FOUND = "Service path not found"; + } + + public static String failedTo(String action, String serviceName) { + return "Failed to " + action + " service " + serviceName; + } + + private LogMessages() { + } + } + + + @Activate + public ServiceDataStoreOperationsImpl(@Reference DataBroker dataBroker) { this.dataBroker = dataBroker; } @Override public void initialize() { + initializeServiceList(); + initializeTempServiceList(); + } + + private void initializeServiceList() { try { LOG.info("initializing service registry"); WriteTransaction transaction = this.dataBroker.newWriteOnlyTransaction(); - InstanceIdentifier iid = InstanceIdentifier.create(ServiceList.class); - ServiceList initialRegistry = new ServiceListBuilder().build(); - transaction.put(LogicalDatastoreType.OPERATIONAL, iid, initialRegistry); - Future future = transaction.submit(); + transaction.merge( + LogicalDatastoreType.OPERATIONAL, + InstanceIdentifier.create(ServiceList.class), + new ServiceListBuilder().build()); + FluentFuture future = transaction.commit(); future.get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e) { - LOG.warn("init failed: {}", e.getMessage()); + LOG.error("init failed: ", e); + } + } + + private void initializeTempServiceList() { + try { + LOG.info("initializing temp service registry"); + WriteTransaction transaction = this.dataBroker.newWriteOnlyTransaction(); + transaction.merge( + LogicalDatastoreType.OPERATIONAL, + InstanceIdentifier.create(TempServiceList.class), + new TempServiceListBuilder().build()); + FluentFuture future = transaction.commit(); + future.get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + LOG.error("init failed: ", e); } } @Override public Optional getService(String serviceName) { try { - ReadOnlyTransaction readTx = this.dataBroker.newReadOnlyTransaction(); - InstanceIdentifier iid = InstanceIdentifier - .create(ServiceList.class).child(Services.class, new ServicesKey(serviceName)); - Future> future - = readTx.read(LogicalDatastoreType.OPERATIONAL, iid); - return future.get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS).toJavaUtil(); + return this.dataBroker.newReadOnlyTransaction() + .read( + LogicalDatastoreType.OPERATIONAL, + InstanceIdentifier.create(ServiceList.class) + .child(Services.class, new ServicesKey(serviceName)) + ) + .get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + LOG.warn("Reading service {} failed:", serviceName, e); + } + return Optional.empty(); + } + + @Override + public Optional getServices() { + try { + return this.dataBroker.newReadOnlyTransaction() + .read( + LogicalDatastoreType.OPERATIONAL, + InstanceIdentifier.create(ServiceList.class)) + .get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + LOG.warn("Reading services failed:", e); + } + return Optional.empty(); + } + + @Override + public Optional getTempService(String serviceName) { + try { + return this.dataBroker.newReadOnlyTransaction() + .read( + LogicalDatastoreType.OPERATIONAL, + InstanceIdentifier.create(TempServiceList.class) + .child( + org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526 + .temp.service.list.Services.class, + new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526 + .temp.service.list.ServicesKey(serviceName))) + .get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e) { LOG.warn("Reading service {} failed:", serviceName, e); } @@ -77,183 +168,324 @@ public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperation @Override public OperationResult deleteService(String serviceName) { - LOG.debug("Deleting '{}' Service", serviceName); + LOG.debug(DELETING_SERVICE_MSG, serviceName); try { WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction(); - InstanceIdentifier 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); + writeTx.delete( + LogicalDatastoreType.OPERATIONAL, + InstanceIdentifier.create(ServiceList.class) + .child(Services.class, new ServicesKey(serviceName))); + writeTx.commit().get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS); + return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE); } catch (TimeoutException | InterruptedException | ExecutionException e) { - String message = "Failed to delete service " + serviceName + " from Service List"; - LOG.warn(message, e); - return OperationResult.failed(message); + LOG.warn("deleteService : {}", LogMessages.failedTo("delete", serviceName), e); + return OperationResult.failed(LogMessages.failedTo("delete", serviceName)); } } @Override - public OperationResult modifyService(String serviceName, State operationalState, State administrativeState) { + public OperationResult deleteTempService(String commonId) { + LOG.debug(DELETING_SERVICE_MSG, commonId); + try { + WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction(); + writeTx.delete( + LogicalDatastoreType.OPERATIONAL, + InstanceIdentifier.create(TempServiceList.class) + .child( + org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526 + .temp.service.list.Services.class, + new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526 + .temp.service.list.ServicesKey(commonId))); + writeTx.commit().get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS); + return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE); + } catch (TimeoutException | InterruptedException | ExecutionException e) { + LOG.warn("deleteTempService : {}", LogMessages.failedTo("delete Temp", commonId), e); + return OperationResult.failed(LogMessages.failedTo("delete Temp", commonId)); + } + } + + @Override + public OperationResult modifyService(String serviceName, State operationalState, AdminStates administrativeState) { LOG.debug("Modifying '{}' Service", serviceName); Optional readService = getService(serviceName); - if (readService.isPresent()) { - try { - WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction(); - InstanceIdentifier iid = InstanceIdentifier.create(ServiceList.class) - .child(Services.class, new ServicesKey(serviceName)); - Services services = new 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 service " + serviceName + " from Service List"; - LOG.warn(message, e); - return OperationResult.failed(message); - } - } else { - String message = "Service " + serviceName + " is not present!"; - LOG.warn(message); - return OperationResult.failed(message); + if (readService.isEmpty()) { + LOG.warn("modifyService: {}", LogMessages.SERVICE_NOT_FOUND); + return OperationResult.failed(LogMessages.SERVICE_NOT_FOUND); + } + try { + WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction(); + writeTx.merge( + LogicalDatastoreType.OPERATIONAL, + InstanceIdentifier.create(ServiceList.class) + .child(Services.class, new ServicesKey(serviceName)), + new ServicesBuilder(readService.orElseThrow()) + .setOperationalState(operationalState) + .setAdministrativeState(administrativeState) + .build()); + writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS); + return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE); + } catch (TimeoutException | InterruptedException | ExecutionException e) { + LOG.warn("modifyService : {}", LogMessages.failedTo("modify", serviceName), e); + return OperationResult.failed(LogMessages.failedTo("modify", serviceName)); } } @Override - public OperationResult createService(ServiceCreateInput serviceCreateInput, - PathComputationRequestOutput outputFromPce) { - LOG.debug("Writing '{}' Service", serviceCreateInput.getServiceName()); + public OperationResult modifyTempService( + String serviceName, State operationalState, AdminStates administrativeState) { + LOG.debug("Modifying '{}' Temp Service", serviceName); + Optional readService = getTempService(serviceName); + if (readService.isEmpty()) { + LOG.warn("modifyTempService: {}", LogMessages.SERVICE_NOT_FOUND); + return OperationResult.failed(LogMessages.SERVICE_NOT_FOUND); + } try { - InstanceIdentifier 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); - writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS); - return OperationResult.ok(SUCCESSFUL_MESSAGE); + writeTx.merge( + LogicalDatastoreType.OPERATIONAL, + InstanceIdentifier.create(TempServiceList.class) + .child( + org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526 + .temp.service.list.Services.class, + new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526 + .temp.service.list.ServicesKey(serviceName)), + new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526 + .temp.service.list.ServicesBuilder(readService.orElseThrow()) + .setOperationalState(operationalState) + .setAdministrativeState(administrativeState) + .build()); + writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS); + return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE); } catch (TimeoutException | InterruptedException | ExecutionException e) { - String message = "Failed to create service " + serviceCreateInput.getServiceName() + " to Service List"; - LOG.warn(message, e); - return OperationResult.failed(message); + LOG.warn("modifyTempService : {}", LogMessages.failedTo("modify Temp", serviceName), e); + return OperationResult.failed(LogMessages.failedTo("modify Temp", serviceName)); } } @Override - public OperationResult createServicePath(ServiceCreateInput serviceCreateInput, - PathComputationRequestOutput outputFromPce) { + public OperationResult createService(ServiceCreateInput serviceCreateInput) { LOG.debug("Writing '{}' Service", serviceCreateInput.getServiceName()); try { - InstanceIdentifier servicePathsIID = InstanceIdentifier - .create(ServicePathList.class) - .child(ServicePaths.class, new ServicePathsKey(serviceCreateInput.getServiceName())); - ServicePaths servicePath = ModelMappingUtils.mappingServicePaths(serviceCreateInput, null, 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); + writeTx.put( + LogicalDatastoreType.OPERATIONAL, + InstanceIdentifier.create(ServiceList.class) + .child(Services.class, new ServicesKey(serviceCreateInput.getServiceName())), + ModelMappingUtils.mappingServices(serviceCreateInput, null)); + writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS); + return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE); } catch (TimeoutException | InterruptedException | ExecutionException e) { - String message = "Failed to create servicePath " + serviceCreateInput.getServiceName() - + " to ServicePath List"; - LOG.warn(message, e); - return OperationResult.failed(message); + LOG.warn("createService : {}", LogMessages.failedTo(CREATE_MSG, serviceCreateInput.getServiceName()), e); + return OperationResult.failed(LogMessages.failedTo(CREATE_MSG, serviceCreateInput.getServiceName())); + } + } + + @Override + public OperationResult createTempService( + TempServiceCreateInput tempServiceCreateInput, + org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev230925 + .service.path.rpc.result.PathDescription pathDescription) { + LOG.debug("Writing '{}' Temp Service", tempServiceCreateInput.getCommonId()); + try { + WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction(); + writeTx.put( + LogicalDatastoreType.OPERATIONAL, + InstanceIdentifier.create(TempServiceList.class) + .child(org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526 + .temp.service.list.Services.class, + new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526 + .temp.service.list.ServicesKey(tempServiceCreateInput.getCommonId())), + ModelMappingUtils.mappingServices(tempServiceCreateInput, pathDescription)); + writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS); + return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE); + } catch (TimeoutException | InterruptedException | ExecutionException e) { + LOG.warn("createTempService : {}", + LogMessages.failedTo("create Temp", tempServiceCreateInput.getCommonId()), e); + return OperationResult.failed(LogMessages.failedTo("create Temp", tempServiceCreateInput.getCommonId())); + } + } + + @Override + public Optional getServicePaths() { + LOG.debug("Retrieving list of ServicePath..."); + try { + return this.dataBroker.newReadOnlyTransaction() + .read( + LogicalDatastoreType.OPERATIONAL, + InstanceIdentifier.create(ServicePathList.class)) + .get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + LOG.error("Reading service path list failed. Error={}", e.getMessage()); + } + return Optional.empty(); + } + + @Override + public Optional getServicePath(String serviceName) { + LOG.debug("Retrieving service path of service {}", serviceName); + try { + return this.dataBroker.newReadOnlyTransaction() + .read( + LogicalDatastoreType.OPERATIONAL, + InstanceIdentifier.create(ServicePathList.class) + .child(ServicePaths.class, new ServicePathsKey(serviceName))) + .get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + LOG.error("Reading service path failed. Error={}", e.getMessage()); + } + return Optional.empty(); + } + + @Override + public OperationResult createServicePath(ServiceInput serviceInput, PathComputationRequestOutput outputFromPce) { + LOG.debug("Writing '{}' ServicePath ", serviceInput.getServiceName()); + try { + WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction(); + writeTx.put( + LogicalDatastoreType.OPERATIONAL, + InstanceIdentifier.create(ServicePathList.class) + .child(ServicePaths.class, new ServicePathsKey(serviceInput.getServiceName())), + ModelMappingUtils.mappingServicePaths(serviceInput, outputFromPce)); + writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS); + return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE); + } catch (TimeoutException | InterruptedException | ExecutionException e) { + LOG.warn("createServicePath : {}", + LogMessages.failedTo("create servicePath", serviceInput.getCommonId()), e); + return OperationResult.failed(LogMessages.failedTo("create servicePath", serviceInput.getCommonId())); + } + } + + @Override + public OperationResult modifyServicePath(PathDescription pathDescription, String serviceName) { + LOG.debug("Updating servicePath because of a change in the openroadm-topology"); + Optional readServicePath = getServicePath(serviceName); + if (readServicePath.isEmpty()) { + LOG.warn("modifyServicePath: {}", LogMessages.SERVICE_PATH_NOT_FOUND); + return OperationResult.failed(LogMessages.SERVICE_PATH_NOT_FOUND); + } + try { + WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction(); + writeTx.merge( + LogicalDatastoreType.OPERATIONAL, + InstanceIdentifier.create(ServicePathList.class) + .child(ServicePaths.class, new ServicePathsKey(serviceName)), + new ServicePathsBuilder() + .setServiceAEnd(readServicePath.orElseThrow().getServiceAEnd()) + .setServiceHandlerHeader(readServicePath.orElseThrow().getServiceHandlerHeader()) + .setServicePathName(readServicePath.orElseThrow().getServicePathName()) + .setServiceZEnd(readServicePath.orElseThrow().getServiceZEnd()) + .setSupportingServiceName(readServicePath.orElseThrow().getSupportingServiceName()) + .setEquipmentSrgs(readServicePath.orElseThrow().getEquipmentSrgs()) + .setFiberSpanSrlgs(readServicePath.orElseThrow().getFiberSpanSrlgs()) + .setHardConstraints(readServicePath.orElseThrow().getHardConstraints()) + .setLatency(readServicePath.orElseThrow().getLatency()) + .setPathDescription(pathDescription) + .setPceRoutingMetric(readServicePath.orElseThrow().getPceRoutingMetric()) + .setSoftConstraints(readServicePath.orElseThrow().getSoftConstraints()) + .build()); + writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS); + return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE); + } catch (TimeoutException | InterruptedException | ExecutionException e) { + LOG.warn("modifyServicePath : {}", LogMessages.failedTo("modify service path", serviceName), e); + return OperationResult.failed(LogMessages.failedTo("modify service path", serviceName)); } } @Override public OperationResult deleteServicePath(String serviceName) { - InstanceIdentifier servicePathsIID = InstanceIdentifier.create(ServicePathList.class) + InstanceIdentifier servicePathsIID = + InstanceIdentifier.create(ServicePathList.class) .child(ServicePaths.class, new ServicePathsKey(serviceName)); LOG.debug("Deleting service from {}", servicePathsIID); WriteTransaction servicePathsWriteTx = this.dataBroker.newWriteOnlyTransaction(); servicePathsWriteTx.delete(LogicalDatastoreType.OPERATIONAL, servicePathsIID); try { - servicePathsWriteTx.submit().get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS); - return OperationResult.ok(SUCCESSFUL_MESSAGE); + servicePathsWriteTx.commit().get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS); + return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE); } catch (InterruptedException | ExecutionException | TimeoutException e) { - String message = "Unable to delete service path " + serviceName; - LOG.error(message, e); - return OperationResult.failed(message); + LOG.error("deleteServicePath : {}", LogMessages.failedTo("delete servicePath", serviceName), e); + return OperationResult.failed(LogMessages.failedTo("delete servicePath", serviceName)); } } /* * 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); + public String writeOrModifyOrDeleteServiceList( + String serviceName, ServiceCreateInput input, PathComputationRequestOutput output, int choice) { + LOG.debug("WriteOrModifyOrDeleting '{}' Service", serviceName); WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction(); - String result = null; Optional readService = getService(serviceName); - if (readService.isPresent()) { - /* - * Modify / Delete Service. - */ - InstanceIdentifier 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. */ - 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 */ - LOG.debug("Deleting '{}' Service", serviceName); - writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid); - action = "deleteService"; - break; - - default: - LOG.debug("No choice found"); - break; - + /* + * Write Service. + */ + if (readService.isEmpty()) { + if (choice != 2) { + LOG.warn("writeOrModifyOrDeleteServiceList: {}", LogMessages.SERVICE_NOT_FOUND); + return LogMessages.SERVICE_NOT_FOUND; } + LOG.debug("Writing '{}' Service", serviceName); + writeTx.put( + LogicalDatastoreType.OPERATIONAL, + InstanceIdentifier.create(ServiceList.class) + .child(Services.class, new ServicesKey(serviceName)), + ModelMappingUtils.mappingServices(input, null)); try { - writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS); - } catch (InterruptedException | ExecutionException | TimeoutException e) { - LOG.error("Failed to {} service from Service List", action, e); - result = "Failed to " + action + " service from Service List"; + writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS); + return null; + } catch (InterruptedException | TimeoutException | ExecutionException e) { + LOG.error("writeOrModifyOrDeleteServiceList : {}", LogMessages.failedTo(CREATE_MSG, serviceName), e); + return LogMessages.failedTo(CREATE_MSG, serviceName); } - } else { - if (choice == 2) { /* Write Service */ - LOG.debug("Writing '{}' Service", serviceName); - InstanceIdentifier iid = InstanceIdentifier.create(ServiceList.class).child(Services.class, - new ServicesKey(serviceName)); + } - Services service = ModelMappingUtils.mappingServices(input, null); - writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service); - try { - writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS); - result = null; - } catch (InterruptedException | TimeoutException | ExecutionException e) { - LOG.error("Failed to createService service to Service List", e); - result = "Failed to createService service to Service List"; - } - } else { - LOG.info("Service is not present ! "); - result = "Service is not present ! "; - } + /* + * Modify / Delete Service. + */ + InstanceIdentifier iid = + InstanceIdentifier.create(ServiceList.class).child(Services.class, new ServicesKey(serviceName)); + String action = null; + switch (choice) { + case 0 : /* Modify. */ + LOG.debug("Modifying '{}' Service", serviceName); + writeTx.merge( + LogicalDatastoreType.OPERATIONAL, + iid, + new ServicesBuilder(readService.orElseThrow()) + .setOperationalState(State.InService) + .setAdministrativeState(AdminStates.InService) + .build()); + action = "modifyService"; + break; + case 1 : /* Delete */ + LOG.debug(DELETING_SERVICE_MSG, serviceName); + writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid); + action = "deleteService"; + break; + default: + LOG.debug("No choice found"); + break; + } + try { + writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + LOG.error("writeOrModifyOrDeleteServiceList : {}", LogMessages.failedTo(action, serviceName), e); + return LogMessages.failedTo(action, serviceName); } - return result; - } + return null; + } }