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