Bump upstream dependencies to Ca
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / impl / ServiceRerouteImpl.java
1 /*
2  * Copyright © 2024 Orange, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.transportpce.servicehandler.impl;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.Map;
12 import java.util.Optional;
13 import org.opendaylight.transportpce.common.ResponseCodes;
14 import org.opendaylight.transportpce.servicehandler.ModelMappingUtils;
15 import org.opendaylight.transportpce.servicehandler.ServiceInput;
16 import org.opendaylight.transportpce.servicehandler.impl.ServicehandlerImpl.LogMessages;
17 import org.opendaylight.transportpce.servicehandler.service.PCEServiceWrapper;
18 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperations;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev240205.PathComputationRerouteRequestOutput;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev240205.path.computation.reroute.request.input.EndpointsBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev230526.configuration.response.common.ConfigurationResponseCommon;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceReroute;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceRerouteInput;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceRerouteOutput;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.service.list.Services;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.path.description.atoz.direction.AToZ;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.path.description.atoz.direction.AToZKey;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.pce.resource.resource.resource.TerminationPoint;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePaths;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35 public class ServiceRerouteImpl implements ServiceReroute {
36     private static final Logger LOG = LoggerFactory.getLogger(ServiceRerouteImpl.class);
37
38     private ServiceDataStoreOperations serviceDataStoreOperations;
39     private PCEServiceWrapper pceServiceWrapper;
40
41     public ServiceRerouteImpl(final ServiceDataStoreOperations serviceDataStoreOperations,
42             PCEServiceWrapper pceServiceWrapper) {
43         this.serviceDataStoreOperations = serviceDataStoreOperations;
44         this.pceServiceWrapper = pceServiceWrapper;
45     }
46
47     @Override
48     public ListenableFuture<RpcResult<ServiceRerouteOutput>> invoke(ServiceRerouteInput input) {
49         String serviceName = input.getServiceName();
50         LOG.info("RPC serviceReroute received for {}", serviceName);
51         Optional<Services> servicesObject = this.serviceDataStoreOperations.getService(serviceName);
52         if (servicesObject.isEmpty()) {
53             LOG.warn("serviceReroute: {}", LogMessages.serviceNotInDS(serviceName));
54             return ModelMappingUtils.createRerouteServiceReply(
55                     input, ResponseCodes.FINAL_ACK_YES,
56                     LogMessages.serviceNotInDS(serviceName),
57                     ResponseCodes.RESPONSE_FAILED);
58         }
59         Services service = servicesObject.orElseThrow();
60         Optional<ServicePaths> servicePathsObject = this.serviceDataStoreOperations.getServicePath(serviceName);
61         if (servicePathsObject.isEmpty()) {
62             LOG.warn("serviceReroute: {}", LogMessages.servicePathNotInDS(serviceName));
63             return ModelMappingUtils.createRerouteServiceReply(
64                     input, ResponseCodes.FINAL_ACK_YES,
65                     LogMessages.servicePathNotInDS(serviceName),
66                     ResponseCodes.RESPONSE_FAILED);
67         }
68         ServicePaths servicePaths = servicePathsObject.orElseThrow();
69         // serviceInput for later use maybe...
70         ServiceInput serviceInput = new ServiceInput(input);
71         serviceInput.setServiceAEnd(service.getServiceAEnd());
72         serviceInput.setServiceZEnd(service.getServiceZEnd());
73         serviceInput.setConnectionType(service.getConnectionType());
74         serviceInput.setCommonId(service.getCommonId());
75         serviceInput.setHardConstraints(service.getHardConstraints());
76         serviceInput.setSoftConstraints(service.getSoftConstraints());
77         serviceInput.setCustomer(service.getCustomer());
78         serviceInput.setCustomerContact(service.getCustomerContact());
79
80         // Get the network xpdr termination points
81         Map<AToZKey, AToZ> mapaToz = servicePaths.getPathDescription().getAToZDirection().getAToZ();
82         String aendtp = ((TerminationPoint) mapaToz.get(new AToZKey(String.valueOf(mapaToz.size() - 3)))
83                 .getResource()
84                 .getResource())
85                 .getTpId();
86         String zendtp = ((TerminationPoint) mapaToz.get(new AToZKey("2"))
87                 .getResource()
88                 .getResource())
89                 .getTpId();
90
91         PathComputationRerouteRequestOutput output = this.pceServiceWrapper.performPCEReroute(
92                 service.getHardConstraints(), service.getSoftConstraints(), input.getSdncRequestHeader(),
93                 service.getServiceAEnd(), service.getServiceZEnd(),
94                 new EndpointsBuilder().setAEndTp(aendtp).setZEndTp(zendtp).build());
95
96         if (output == null) {
97             LOG.error("serviceReroute: {}", LogMessages.PCE_FAILED);
98             return ModelMappingUtils.createRerouteServiceReply(
99                     input, ResponseCodes.FINAL_ACK_YES,
100                     LogMessages.PCE_FAILED, ResponseCodes.RESPONSE_FAILED);
101         }
102         LOG.info("RPC ServiceReroute is done");
103         ConfigurationResponseCommon common = output.getConfigurationResponseCommon();
104         return ModelMappingUtils.createRerouteServiceReply(input, common.getAckFinalIndicator(),
105                 common.getResponseMessage(), common.getResponseCode());
106     }
107
108 }