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