Refactor transportpce-routing-constraint model
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / service / PathComputationServiceImpl.java
1 /*
2  * Copyright © 2017 AT&T, 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.pce.service;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import com.google.common.util.concurrent.ListeningExecutorService;
12 import com.google.common.util.concurrent.MoreExecutors;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.concurrent.Callable;
17 import java.util.concurrent.Executors;
18 import java.util.stream.Collectors;
19 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
20 import org.opendaylight.transportpce.common.mapping.PortMapping;
21 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
22 import org.opendaylight.transportpce.pce.PceComplianceCheck;
23 import org.opendaylight.transportpce.pce.PceComplianceCheckResult;
24 import org.opendaylight.transportpce.pce.PceSendingPceRPCs;
25 import org.opendaylight.transportpce.pce.gnpy.GnpyResult;
26 import org.opendaylight.transportpce.pce.gnpy.consumer.GnpyConsumer;
27 import org.opendaylight.yang.gen.v1.gnpy.path.rev220221.result.Response;
28 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.CancelResourceReserveInput;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.CancelResourceReserveOutput;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.CancelResourceReserveOutputBuilder;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.PathComputationRequestInput;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.PathComputationRequestOutput;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.PathComputationRequestOutputBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.ServicePathRpcResult;
35 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.ServicePathRpcResultBuilder;
36 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.gnpy.GnpyResponse;
37 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.gnpy.GnpyResponseBuilder;
38 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.gnpy.gnpy.response.ResponseType;
39 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.gnpy.gnpy.response.response.type.NoPathCase;
40 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.gnpy.gnpy.response.response.type.NoPathCaseBuilder;
41 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.gnpy.gnpy.response.response.type.PathCase;
42 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.gnpy.gnpy.response.response.type.PathCaseBuilder;
43 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.path.performance.PathProperties;
44 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.path.performance.PathPropertiesBuilder;
45 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.path.performance.path.properties.PathMetric;
46 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.path.performance.path.properties.PathMetricBuilder;
47 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.service.path.rpc.result.PathDescription;
48 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.service.path.rpc.result.PathDescriptionBuilder;
49 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.configuration.response.common.ConfigurationResponseCommonBuilder;
50 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.AToZDirection;
51 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.ZToADirection;
52 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.RpcStatusEx;
53 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.ServicePathNotificationTypes;
54 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.response.parameters.sp.ResponseParametersBuilder;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57
58 public class PathComputationServiceImpl implements PathComputationService {
59
60     private static final Logger LOG = LoggerFactory.getLogger(PathComputationServiceImpl.class);
61     private final NotificationPublishService notificationPublishService;
62     private NetworkTransactionService networkTransactionService;
63     private final ListeningExecutorService executor;
64     private ServicePathRpcResult notification = null;
65     private final GnpyConsumer gnpyConsumer;
66     private PortMapping portMapping;
67
68     public PathComputationServiceImpl(NetworkTransactionService networkTransactionService,
69                                       NotificationPublishService notificationPublishService,
70                                       GnpyConsumer gnpyConsumer, PortMapping portMapping) {
71         this.notificationPublishService = notificationPublishService;
72         this.networkTransactionService = networkTransactionService;
73         this.executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(5));
74         this.gnpyConsumer = gnpyConsumer;
75         this.portMapping = portMapping;
76     }
77
78     public void init() {
79         LOG.info("init ...");
80     }
81
82     public void close() {
83         LOG.info("close.");
84     }
85
86     @SuppressFBWarnings(
87         value = "UPM_UNCALLED_PRIVATE_METHOD",
88         justification = "false positive, this method is used by public method cancelResourceReserve")
89     private void sendNotifications(ServicePathNotificationTypes servicePathNotificationTypes, String serviceName,
90             RpcStatusEx rpcStatusEx, String message, PathDescription pathDescription) {
91         ServicePathRpcResultBuilder servicePathRpcResultBuilder =
92                 new ServicePathRpcResultBuilder().setNotificationType(servicePathNotificationTypes)
93                         .setServiceName(serviceName).setStatus(rpcStatusEx).setStatusMessage(message);
94         if (pathDescription != null) {
95             servicePathRpcResultBuilder.setPathDescription(pathDescription);
96         }
97         this.notification = servicePathRpcResultBuilder.build();
98         try {
99             notificationPublishService.putNotification(this.notification);
100         } catch (InterruptedException e) {
101             LOG.info("notification offer rejected: ", e);
102         }
103     }
104
105     @Override
106     public ListenableFuture<CancelResourceReserveOutput> cancelResourceReserve(CancelResourceReserveInput input) {
107         LOG.info("cancelResourceReserve");
108         return executor.submit(new Callable<CancelResourceReserveOutput>() {
109
110             @Override
111             public CancelResourceReserveOutput call() throws Exception {
112                 String message = "";
113                 sendNotifications(ServicePathNotificationTypes.CancelResourceReserve, input.getServiceName(),
114                         RpcStatusEx.Pending, "Service compliant, submitting cancelResourceReserve Request ...", null);
115                 PceSendingPceRPCs sendingPCE = new PceSendingPceRPCs(gnpyConsumer);
116                 sendingPCE.cancelResourceReserve();
117                 if (Boolean.TRUE.equals(sendingPCE.getSuccess())) {
118                     message = "ResourceReserve cancelled !";
119                 } else {
120                     message = "Cancelling ResourceReserve failed !";
121                 }
122                 LOG.info("in PathComputationServiceImpl : {}",message);
123                 sendNotifications(ServicePathNotificationTypes.CancelResourceReserve, input.getServiceName(),
124                         RpcStatusEx.Successful, "cancel Resource Reserve successful!", null);
125                 ConfigurationResponseCommonBuilder configurationResponseCommon =
126                         new ConfigurationResponseCommonBuilder();
127                 configurationResponseCommon.setAckFinalIndicator("Yes")
128                         .setRequestId(input.getServiceHandlerHeader().getRequestId()).setResponseCode("200")
129                         .setResponseMessage("");
130                 CancelResourceReserveOutputBuilder output = new CancelResourceReserveOutputBuilder();
131                 output.setConfigurationResponseCommon(configurationResponseCommon.build());
132                 return output.build();
133             }
134         });
135     }
136
137     @Override
138     public ListenableFuture<PathComputationRequestOutput> pathComputationRequest(PathComputationRequestInput input) {
139         LOG.info("pathComputationRequest");
140         return executor.submit(new Callable<PathComputationRequestOutput>() {
141
142             @Override
143             public PathComputationRequestOutput call() throws Exception {
144                 PathComputationRequestOutputBuilder output = new PathComputationRequestOutputBuilder();
145                 ConfigurationResponseCommonBuilder configurationResponseCommon =
146                         new ConfigurationResponseCommonBuilder();
147                 PceComplianceCheckResult check = PceComplianceCheck.check(input);
148                 if (!check.hasPassed()) {
149                     LOG.error("Path not calculated, service not compliant : {}", check.getMessage());
150                     sendNotifications(ServicePathNotificationTypes.PathComputationRequest, input.getServiceName(),
151                             RpcStatusEx.Failed, "Path not calculated, service not compliant", null);
152                     configurationResponseCommon.setAckFinalIndicator("Yes")
153                             .setRequestId(input.getServiceHandlerHeader().getRequestId())
154                             .setResponseCode("Path not calculated").setResponseMessage(check.getMessage());
155                     output.setConfigurationResponseCommon(configurationResponseCommon.build())
156                             .setResponseParameters(null);
157                     return output.build();
158                 }
159                 sendNotifications(ServicePathNotificationTypes.PathComputationRequest, input.getServiceName(),
160                         RpcStatusEx.Pending, "Service compliant, submitting pathComputation Request ...", null);
161                 String message = "";
162                 String responseCode = "";
163                 PceSendingPceRPCs sendingPCE = new PceSendingPceRPCs(input, networkTransactionService,
164                         gnpyConsumer, portMapping);
165                 sendingPCE.pathComputation();
166                 message = sendingPCE.getMessage();
167                 responseCode = sendingPCE.getResponseCode();
168                 PathDescriptionBuilder path = null;
169                 path = sendingPCE.getPathDescription();
170                 LOG.info("PCE response: {} {}", message, responseCode);
171
172                 //add the GNPy result
173                 GnpyResult gnpyAtoZ = sendingPCE.getGnpyAtoZ();
174                 GnpyResult gnpyZtoA = sendingPCE.getGnpyZtoA();
175                 List<GnpyResponse> listResponse = new ArrayList<>();
176                 if (gnpyAtoZ != null) {
177                     GnpyResponse respAtoZ = generateGnpyResponse(gnpyAtoZ.getResponse(),"A-to-Z");
178                     listResponse.add(respAtoZ);
179                 }
180                 if (gnpyZtoA != null) {
181                     GnpyResponse respZtoA = generateGnpyResponse(gnpyZtoA.getResponse(),"Z-to-A");
182                     listResponse.add(respZtoA);
183                 }
184                 output.setGnpyResponse(listResponse.stream()
185                         .collect(Collectors.toMap(GnpyResponse::key, gnpyResponse -> gnpyResponse)));
186
187                 if (Boolean.FALSE.equals(sendingPCE.getSuccess()) || (path == null)) {
188                     configurationResponseCommon.setAckFinalIndicator("Yes")
189                             .setRequestId(input.getServiceHandlerHeader().getRequestId()).setResponseCode(responseCode)
190                             .setResponseMessage(message);
191                     output.setConfigurationResponseCommon(configurationResponseCommon.build());
192                     sendNotifications(ServicePathNotificationTypes.PathComputationRequest, input.getServiceName(),
193                             RpcStatusEx.Failed, "Path not calculated", null);
194                     return output.build();
195                 }
196                 // Path calculator returned Success
197                 configurationResponseCommon.setAckFinalIndicator("Yes")
198                         .setRequestId(input.getServiceHandlerHeader().getRequestId()).setResponseCode(responseCode)
199                         .setResponseMessage(message);
200                 PathDescription pathDescription =
201                     new org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.service.path.rpc
202                             .result.PathDescriptionBuilder()
203                         .setAToZDirection(path.getAToZDirection())
204                         .setZToADirection(path.getZToADirection())
205                         .build();
206                 sendNotifications(ServicePathNotificationTypes.PathComputationRequest, input.getServiceName(),
207                         RpcStatusEx.Successful, message, pathDescription);
208                 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.response
209                     .parameters.sp.response.parameters.PathDescription pathDescription1 =
210                     new org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118
211                             .response.parameters.sp.response.parameters.PathDescriptionBuilder()
212                         .setAToZDirection(path.getAToZDirection())
213                         .setZToADirection(path.getZToADirection())
214                         .build();
215                 ResponseParametersBuilder rpb = new ResponseParametersBuilder().setPathDescription(pathDescription1);
216                 output.setConfigurationResponseCommon(configurationResponseCommon.build())
217                         .setResponseParameters(rpb.build());
218
219                 //debug prints
220                 AToZDirection atoz = pathDescription.getAToZDirection();
221                 if ((atoz != null) && (atoz.getAToZ() != null)) {
222                     LOG.debug("Impl AtoZ Notification: [{}] elements in description", atoz.getAToZ().size());
223                     for (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705
224                             .path.description.atoz.direction.AToZKey key : atoz.getAToZ().keySet()) {
225                         LOG.debug("Impl AtoZ Notification: [{}] {}", key, atoz.getAToZ().get(key));
226                     }
227                 }
228                 ZToADirection ztoa = pathDescription.getZToADirection();
229                 if ((ztoa != null) && (ztoa.getZToA() != null)) {
230                     LOG.debug("Impl ZtoA Notification: [{}] elements in description", ztoa.getZToA().size());
231                     for (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705
232                             .path.description.ztoa.direction.ZToAKey key : ztoa.getZToA().keySet()) {
233                         LOG.debug("Impl ZtoA Notification: [{}] {}", key, ztoa.getZToA().get(key));
234                     }
235                 }
236                 return output.build();
237             }
238         });
239     }
240
241     public GnpyResponse generateGnpyResponse(Response responseGnpy, String pathDir) {
242         ResponseType respType = null;
243         boolean feasible = true;
244         if (responseGnpy != null) {
245             if (responseGnpy.getResponseType() instanceof org.opendaylight.yang.gen.v1.gnpy.path.rev220221.result
246                     .response.response.type.NoPathCase) {
247                 LOG.info("GNPy : path is not feasible");
248                 org.opendaylight.yang.gen.v1.gnpy.path.rev220221.result.response.response.type.NoPathCase
249                     noPathGnpy = (org.opendaylight.yang.gen.v1.gnpy.path.rev220221.result.response.response.type
250                     .NoPathCase) responseGnpy.getResponseType();
251                 NoPathCase noPathCase = new NoPathCaseBuilder().setNoPath(noPathGnpy.getNoPath()).build();
252                 respType = noPathCase;
253                 feasible = false;
254             } else if (responseGnpy.getResponseType() instanceof org.opendaylight.yang.gen.v1.gnpy.path.rev220221.result
255                     .response.response.type.PathCase) {
256                 LOG.info("GNPy : path is feasible");
257                 org.opendaylight.yang.gen.v1.gnpy.path.rev220221.result.response.response.type.PathCase pathCase =
258                         (org.opendaylight.yang.gen.v1.gnpy.path.rev220221.result.response.response.type.PathCase)
259                         responseGnpy.getResponseType();
260                 List<org.opendaylight.yang.gen.v1.gnpy.path.rev220221.generic.path.properties.path.properties
261                     .PathMetric> pathMetricList =
262                     new ArrayList<>(pathCase.getPathProperties().getPathMetric().values());
263                 List<PathMetric> gnpyPathMetricList = new ArrayList<>();
264                 for (org.opendaylight.yang.gen.v1.gnpy.path.rev220221.generic.path.properties.path.properties.PathMetric
265                         pathMetricGnpy : pathMetricList) {
266                     PathMetric pathMetric = new PathMetricBuilder().setMetricType(pathMetricGnpy.getMetricType())
267                             .setAccumulativeValue(pathMetricGnpy.getAccumulativeValue()).build();
268                     gnpyPathMetricList.add(pathMetric);
269                 }
270                 PathProperties pathProperties = new PathPropertiesBuilder()
271                         .setPathMetric(gnpyPathMetricList.stream()
272                                 .collect(Collectors.toMap(PathMetric::key, pathMetric -> pathMetric)))
273                         .build();
274                 PathCase gnpyPathCase = new PathCaseBuilder().setPathProperties(pathProperties).build();
275                 respType = gnpyPathCase;
276                 feasible = true;
277             }
278         }
279         return new GnpyResponseBuilder().setPathDir(pathDir).setResponseType(respType).setFeasibility(feasible).build();
280     }
281
282 }