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