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