d3536def95575a62112371c9c7b1572fa06bf6ac
[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.rev200909.result.Response;
28 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.CancelResourceReserveInput;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.CancelResourceReserveOutput;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.CancelResourceReserveOutputBuilder;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestInput;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestOutput;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestOutputBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.ServicePathRpcResult;
35 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.ServicePathRpcResultBuilder;
36 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.gnpy.GnpyResponse;
37 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.gnpy.GnpyResponseBuilder;
38 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.gnpy.gnpy.response.ResponseType;
39 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.gnpy.gnpy.response.response.type.NoPathCase;
40 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.gnpy.gnpy.response.response.type.NoPathCaseBuilder;
41 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.gnpy.gnpy.response.response.type.PathCase;
42 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.gnpy.gnpy.response.response.type.PathCaseBuilder;
43 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.path.performance.PathProperties;
44 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.path.performance.PathPropertiesBuilder;
45 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.path.performance.path.properties.PathMetric;
46 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.path.performance.path.properties.PathMetricBuilder;
47 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.service.path.rpc.result.PathDescription;
48 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.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.rev201210.path.description.AToZDirection;
51 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210.path.description.ZToADirection;
52 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.RpcStatusEx;
53 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.ServicePathNotificationTypes;
54 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.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 = new org.opendaylight.yang.gen.v1.http.org.opendaylight
201                         .transportpce.pce.rev200128.service.path.rpc.result.PathDescriptionBuilder()
202                                 .setAToZDirection(path.getAToZDirection()).setZToADirection(path.getZToADirection())
203                                 .build();
204                 sendNotifications(ServicePathNotificationTypes.PathComputationRequest, input.getServiceName(),
205                         RpcStatusEx.Successful, message, pathDescription);
206                 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.response
207                     .parameters.sp.response.parameters.PathDescription pathDescription1 = new org.opendaylight.yang.gen
208                         .v1.http.org.transportpce.b.c._interface.service.types.rev200128.response.parameters.sp
209                         .response.parameters.PathDescriptionBuilder()
210                                 .setAToZDirection(path.getAToZDirection()).setZToADirection(path.getZToADirection())
211                                 .build();
212                 ResponseParametersBuilder rpb = new ResponseParametersBuilder().setPathDescription(pathDescription1);
213                 output.setConfigurationResponseCommon(configurationResponseCommon.build())
214                         .setResponseParameters(rpb.build());
215
216                 //debug prints
217                 AToZDirection atoz = pathDescription.getAToZDirection();
218                 if ((atoz != null) && (atoz.getAToZ() != null)) {
219                     LOG.debug("Impl AtoZ Notification: [{}] elements in description", atoz.getAToZ().size());
220                     for (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210
221                             .path.description.atoz.direction.AToZKey key : atoz.getAToZ().keySet()) {
222                         LOG.debug("Impl AtoZ Notification: [{}] {}", key, atoz.getAToZ().get(key));
223                     }
224                 }
225                 ZToADirection ztoa = pathDescription.getZToADirection();
226                 if ((ztoa != null) && (ztoa.getZToA() != null)) {
227                     LOG.debug("Impl ZtoA Notification: [{}] elements in description", ztoa.getZToA().size());
228                     for (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210
229                             .path.description.ztoa.direction.ZToAKey key : ztoa.getZToA().keySet()) {
230                         LOG.debug("Impl ZtoA Notification: [{}] {}", key, ztoa.getZToA().get(key));
231                     }
232                 }
233                 return output.build();
234             }
235         });
236     }
237
238     public GnpyResponse generateGnpyResponse(Response responseGnpy, String pathDir) {
239         ResponseType respType = null;
240         boolean feasible = true;
241         if (responseGnpy != null) {
242             if (responseGnpy.getResponseType() instanceof org.opendaylight.yang.gen.v1.gnpy.path.rev200909.result
243                     .response.response.type.NoPathCase) {
244                 LOG.info("GNPy : path is not feasible");
245                 org.opendaylight.yang.gen.v1.gnpy.path.rev200909.result.response.response.type.NoPathCase
246                     noPathGnpy = (org.opendaylight.yang.gen.v1.gnpy.path.rev200909.result.response.response.type
247                     .NoPathCase) responseGnpy.getResponseType();
248                 NoPathCase noPathCase = new NoPathCaseBuilder().setNoPath(noPathGnpy.getNoPath()).build();
249                 respType = noPathCase;
250                 feasible = false;
251             } else if (responseGnpy.getResponseType() instanceof org.opendaylight.yang.gen.v1.gnpy.path.rev200909.result
252                     .response.response.type.PathCase) {
253                 LOG.info("GNPy : path is feasible");
254                 org.opendaylight.yang.gen.v1.gnpy.path.rev200909.result.response.response.type.PathCase pathCase =
255                         (org.opendaylight.yang.gen.v1.gnpy.path.rev200909.result.response.response.type.PathCase)
256                         responseGnpy.getResponseType();
257                 List<org.opendaylight.yang.gen.v1.gnpy.path.rev200909.generic.path.properties.path.properties
258                     .PathMetric> pathMetricList =
259                     new ArrayList<>(pathCase.getPathProperties().getPathMetric().values());
260                 List<PathMetric> gnpyPathMetricList = new ArrayList<>();
261                 for (org.opendaylight.yang.gen.v1.gnpy.path.rev200909.generic.path.properties.path.properties.PathMetric
262                         pathMetricGnpy : pathMetricList) {
263                     PathMetric pathMetric = new PathMetricBuilder().setMetricType(pathMetricGnpy.getMetricType())
264                             .setAccumulativeValue(pathMetricGnpy.getAccumulativeValue()).build();
265                     gnpyPathMetricList.add(pathMetric);
266                 }
267                 PathProperties pathProperties = new PathPropertiesBuilder()
268                         .setPathMetric(gnpyPathMetricList.stream()
269                                 .collect(Collectors.toMap(PathMetric::key, pathMetric -> pathMetric)))
270                         .build();
271                 PathCase gnpyPathCase = new PathCaseBuilder().setPathProperties(pathProperties).build();
272                 respType = gnpyPathCase;
273                 feasible = true;
274             }
275         }
276         return new GnpyResponseBuilder().setPathDir(pathDir).setResponseType(respType).setFeasibility(feasible).build();
277     }
278
279 }