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