NBI notification support for service-result-rpc
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / listeners / ServiceListener.java
index 434aa2cb31be964e6e9dcba2f5d47ad352bafbec..fcb4bf183e8f0961fc4e894a23946c177386c912 100644 (file)
@@ -7,9 +7,6 @@
  */
 package org.opendaylight.transportpce.servicehandler.listeners;
 
-import static org.opendaylight.transportpce.servicehandler.ModelMappingUtils.createServiceAEndReroute;
-import static org.opendaylight.transportpce.servicehandler.ModelMappingUtils.createServiceZEndReroute;
-
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.MoreExecutors;
 import java.util.Collection;
@@ -25,57 +22,55 @@ import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
 import org.opendaylight.mdsal.binding.api.DataTreeModification;
 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
 import org.opendaylight.transportpce.common.ResponseCodes;
-import org.opendaylight.transportpce.pce.service.PathComputationService;
 import org.opendaylight.transportpce.servicehandler.ServiceInput;
-import org.opendaylight.transportpce.servicehandler.impl.ServicehandlerImpl;
 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperations;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.PathComputationRerouteRequestInput;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.PathComputationRerouteRequestInputBuilder;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.PathComputationRerouteRequestOutput;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.path.computation.reroute.request.input.EndpointsBuilder;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev211210.Restorable;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev211210.RpcActions;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev211210.sdnc.request.header.SdncRequestHeaderBuilder;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev230526.Restorable;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev230526.RpcActions;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev230526.routing.metric.RoutingMetric;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev230526.sdnc.request.header.SdncRequestHeaderBuilder;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev230526.service.resiliency.ServiceResiliency;
 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.rev211210.ServiceCreateInputBuilder;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.ServiceCreateOutput;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.ServiceDeleteInputBuilder;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.ServiceDeleteOutput;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.service.create.input.ServiceAEndBuilder;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.service.create.input.ServiceZEndBuilder;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.service.delete.input.ServiceDeleteReqInfo;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.service.delete.input.ServiceDeleteReqInfoBuilder;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.service.list.Services;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.atoz.direction.AToZ;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.atoz.direction.AToZKey;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.pce.resource.resource.resource.TerminationPoint;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.PceMetric;
-import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePaths;
-import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishNotificationAlarmService;
-import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishNotificationAlarmServiceBuilder;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.OrgOpenroadmServiceService;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceCreateInputBuilder;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceCreateOutput;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceDeleteInputBuilder;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceDeleteOutput;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceRerouteInput;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceRerouteInputBuilder;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceRerouteOutput;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.service.create.input.ServiceAEndBuilder;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.service.create.input.ServiceZEndBuilder;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.service.delete.input.ServiceDeleteReqInfo;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.service.delete.input.ServiceDeleteReqInfoBuilder;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.service.list.Services;
+import org.opendaylight.yang.gen.v1.nbi.notifications.rev230726.PublishNotificationAlarmService;
+import org.opendaylight.yang.gen.v1.nbi.notifications.rev230726.PublishNotificationAlarmServiceBuilder;
 import org.opendaylight.yangtools.yang.common.RpcResult;
+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
 public class ServiceListener implements DataTreeChangeListener<Services> {
 
     private static final Logger LOG = LoggerFactory.getLogger(ServiceListener.class);
     private static final String PUBLISHER = "ServiceListener";
-    private ServicehandlerImpl servicehandlerImpl;
+    private OrgOpenroadmServiceService servicehandlerImpl;
     private ServiceDataStoreOperations serviceDataStoreOperations;
     private NotificationPublishService notificationPublishService;
-    private PathComputationService pathComputationService;
     private Map<String, ServiceInput> mapServiceInputReroute;
     private final ScheduledExecutorService executor;
 
-    public ServiceListener(ServicehandlerImpl servicehandlerImpl, ServiceDataStoreOperations serviceDataStoreOperations,
-                           NotificationPublishService notificationPublishService,
-                           PathComputationService pathComputationService) {
+    @Activate
+    public ServiceListener(@Reference OrgOpenroadmServiceService servicehandlerImpl,
+            @Reference ServiceDataStoreOperations serviceDataStoreOperations,
+            @Reference NotificationPublishService notificationPublishService) {
         this.servicehandlerImpl = servicehandlerImpl;
         this.notificationPublishService = notificationPublishService;
         this.serviceDataStoreOperations = serviceDataStoreOperations;
-        this.pathComputationService = pathComputationService;
         this.executor = MoreExecutors.getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(4));
         mapServiceInputReroute = new HashMap<>();
     }
@@ -114,7 +109,8 @@ public class ServiceListener implements DataTreeChangeListener<Services> {
                                 && inputAfter.getServiceResiliency().getResiliency() != null
                                 && inputAfter.getServiceResiliency().getResiliency().equals(Restorable.VALUE)) {
                             LOG.info("Attempting to reroute the service '{}'...", serviceInputName);
-                            if (!serviceRerouteCheck(inputBefore)) {
+                            if (!serviceRerouteCheck(serviceInputName, inputAfter.getServiceResiliency(),
+                                    inputAfter.getRoutingMetric())) {
                                 LOG.info("No other path available, cancelling reroute process of service '{}'...",
                                         serviceInputName);
                                 continue;
@@ -175,7 +171,7 @@ public class ServiceListener implements DataTreeChangeListener<Services> {
             LOG.warn("Service '{}' does not exist in datastore", serviceNameToReroute);
             return;
         }
-        Services service = serviceOpt.get();
+        Services service = serviceOpt.orElseThrow();
         ListenableFuture<RpcResult<ServiceDeleteOutput>> res = this.servicehandlerImpl.serviceDelete(
                 new ServiceDeleteInputBuilder()
                         .setSdncRequestHeader(new SdncRequestHeaderBuilder(service.getSdncRequestHeader())
@@ -237,37 +233,27 @@ public class ServiceListener implements DataTreeChangeListener<Services> {
     }
 
     /**
-     * Call the PCE RPC path-computation-reroute-request to check if any other path exists.
+     * Prior to the reroute steps: check that an alternative route of the service is possible.
      *
-     * @param input Service to be rerouted
+     * @param serviceNameToReroute Name of the service
+     * @param serviceResiliency Resiliency of the service
+     * @param routingMetric Metric of the routing
      */
-    protected boolean serviceRerouteCheck(Services input) {
-        Optional<ServicePaths> servicePaths = serviceDataStoreOperations.getServicePath(input.getServiceName());
-        if (servicePaths.isEmpty()) {
-            LOG.warn("Service path of '{}' does not exist in datastore", input.getServiceName());
-            return false;
-        }
-        // Get the network xpdr termination points
-        Map<AToZKey, AToZ> mapaToz = servicePaths.get().getPathDescription().getAToZDirection().getAToZ();
-        String aendtp = ((TerminationPoint) mapaToz.get(new AToZKey(String.valueOf(mapaToz.size() - 3)))
-                .getResource().getResource()).getTpId();
-        String zendtp = ((TerminationPoint) mapaToz.get(new AToZKey("2")).getResource()
-                .getResource()).getTpId();
-        PathComputationRerouteRequestInput inputPC = new PathComputationRerouteRequestInputBuilder()
-                .setHardConstraints(input.getHardConstraints())
-                .setSoftConstraints(input.getSoftConstraints())
-                .setServiceAEnd(createServiceAEndReroute(input.getServiceAEnd()))
-                .setServiceZEnd(createServiceZEndReroute(input.getServiceZEnd()))
-                .setPceRoutingMetric(PceMetric.TEMetric)
-                .setEndpoints(new EndpointsBuilder()
-                        .setAEndTp(aendtp)
-                        .setZEndTp(zendtp)
+    private boolean serviceRerouteCheck(String serviceNameToReroute, ServiceResiliency serviceResiliency,
+                                        RoutingMetric routingMetric) {
+        ServiceRerouteInput serviceRerouteInput = new ServiceRerouteInputBuilder()
+                .setServiceName(serviceNameToReroute)
+                .setServiceResiliency(serviceResiliency)
+                .setRoutingMetric(routingMetric)
+                .setSdncRequestHeader(new SdncRequestHeaderBuilder()
+                        .setRpcAction(RpcActions.ServiceReroute)
                         .build())
                 .build();
-        ListenableFuture<PathComputationRerouteRequestOutput> res =
-                pathComputationService.pathComputationRerouteRequest(inputPC);
+        ListenableFuture<RpcResult<ServiceRerouteOutput>> res = this.servicehandlerImpl.serviceReroute(
+                serviceRerouteInput);
         try {
-            return res.get().getConfigurationResponseCommon().getResponseCode().equals(ResponseCodes.RESPONSE_OK);
+            return res.get().getResult().getConfigurationResponseCommon().getResponseCode()
+                    .equals(ResponseCodes.RESPONSE_OK);
         } catch (ExecutionException | InterruptedException e) {
             LOG.warn("ServiceRerouteCheck FAILED ! ", e);
             return false;