upgrade models to OpenROADM service 5.1.0
[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.rev161014.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                 if (!(sendingPCE.getSuccess()) || (path == null)) {
161                     configurationResponseCommon.setAckFinalIndicator("Yes")
162                             .setRequestId(input.getServiceHandlerHeader().getRequestId()).setResponseCode(responseCode)
163                             .setResponseMessage(message);
164                     output.setConfigurationResponseCommon(configurationResponseCommon.build());
165                     sendNotifications(ServicePathNotificationTypes.PathComputationRequest, input.getServiceName(),
166                             RpcStatusEx.Failed, "Path not calculated", null);
167                     return output.build();
168                 }
169                 // Path calculator returned Success
170                 configurationResponseCommon.setAckFinalIndicator("Yes")
171                         .setRequestId(input.getServiceHandlerHeader().getRequestId()).setResponseCode(responseCode)
172                         .setResponseMessage(message);
173                 PathDescription pathDescription = new org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce
174                         .pce.rev190624.service.path.rpc.result.PathDescriptionBuilder()
175                                 .setAToZDirection(path.getAToZDirection()).setZToADirection(path.getZToADirection())
176                                 .build();
177                 sendNotifications(ServicePathNotificationTypes.PathComputationRequest, input.getServiceName(),
178                         RpcStatusEx.Successful, message, pathDescription);
179                 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev191009.response
180                     .parameters.sp.response.parameters.PathDescription pathDescription1 = new org.opendaylight.yang.gen
181                         .v1.http.org.transportpce.b.c._interface.service.types.rev191009.response.parameters.sp
182                         .response.parameters.PathDescriptionBuilder()
183                                 .setAToZDirection(path.getAToZDirection()).setZToADirection(path.getZToADirection())
184                                 .build();
185                 ResponseParametersBuilder rpb = new ResponseParametersBuilder().setPathDescription(pathDescription1);
186                 output.setConfigurationResponseCommon(configurationResponseCommon.build())
187                         .setResponseParameters(rpb.build());
188
189               //add the GNPy result
190                 GnpyResult gnpyAtoZ = sendingPCE.getGnpyAtoZ();
191                 GnpyResult gnpyZtoA = sendingPCE.getGnpyZtoA();
192                 List<GnpyResponse> listResponse = new ArrayList<>();
193                 if (gnpyAtoZ != null) {
194                     GnpyResponse respAtoZ = generateGnpyResponse(gnpyAtoZ.getResponse(),"A-to-Z");
195                     listResponse.add(respAtoZ);
196                 }
197                 if (gnpyZtoA != null) {
198                     GnpyResponse respZtoA = generateGnpyResponse(gnpyZtoA.getResponse(),"Z-to-A");
199                     listResponse.add(respZtoA);
200                 }
201                 output.setGnpyResponse(listResponse);
202
203                 //debug prints
204                 AToZDirection atoz = pathDescription.getAToZDirection();
205                 if ((atoz != null) && (atoz.getAToZ() != null)) {
206                     LOG.debug("Impl AtoZ Notification: [{}] elements in description", atoz.getAToZ().size());
207                     for (int i = 0; i < atoz.getAToZ().size(); i++) {
208                         LOG.debug("Impl AtoZ Notification: [{}] {}", i, atoz.getAToZ().get(i));
209                     }
210                 }
211                 ZToADirection ztoa = pathDescription.getZToADirection();
212                 if ((ztoa != null) && (ztoa.getZToA() != null)) {
213                     LOG.debug("Impl ZtoA Notification: [{}] elements in description", ztoa.getZToA().size());
214                     for (int i = 0; i < ztoa.getZToA().size(); i++) {
215                         LOG.debug("Impl ZtoA Notification: [{}] {}", i, ztoa.getZToA().get(i));
216                     }
217                 }
218                 return output.build();
219             }
220         });
221     }
222
223     public GnpyResponse generateGnpyResponse(Response responseGnpy, String pathDir) {
224         ResponseType respType = null;
225         boolean feasible = true;
226         if (responseGnpy != null) {
227             if (responseGnpy.getResponseType() instanceof org.opendaylight.yang.gen.v1.gnpy.path.rev190502.result
228                     .response.response.type.NoPathCase) {
229                 org.opendaylight.yang.gen.v1.gnpy.path.rev190502.result.response.response.type.NoPathCase
230                     noPathGnpy = (org.opendaylight.yang.gen.v1.gnpy.path.rev190502.result.response.response.type
231                     .NoPathCase) responseGnpy.getResponseType();
232                 NoPathCase noPathCase = new NoPathCaseBuilder().setNoPath(noPathGnpy.getNoPath()).build();
233                 respType = noPathCase;
234                 feasible = false;
235             } else if (responseGnpy.getResponseType() instanceof org.opendaylight.yang.gen.v1.gnpy.path.rev190502.result
236                     .response.response.type.PathCase) {
237                 LOG.info("GNPy : path is feasible");
238                 org.opendaylight.yang.gen.v1.gnpy.path.rev190502.result.response.response.type.PathCase pathCase =
239                         (org.opendaylight.yang.gen.v1.gnpy.path.rev190502.result.response.response.type.PathCase)
240                         responseGnpy.getResponseType();
241                 List<org.opendaylight.yang.gen.v1.gnpy.path.rev190502.generic.path.properties.path.properties
242                     .PathMetric> pathMetricList = pathCase.getPathProperties().getPathMetric();
243                 List<PathMetric> gnpyPathMetricList = new ArrayList<>();
244                 for (org.opendaylight.yang.gen.v1.gnpy.path.rev190502.generic.path.properties.path.properties.PathMetric
245                         pathMetricGnpy : pathMetricList) {
246                     PathMetric pathMetric = new PathMetricBuilder().setMetricType(pathMetricGnpy.getMetricType())
247                             .setAccumulativeValue(pathMetricGnpy.getAccumulativeValue()).build();
248                     gnpyPathMetricList.add(pathMetric);
249                 }
250                 PathProperties pathProperties = new PathPropertiesBuilder().setPathMetric(gnpyPathMetricList).build();
251                 PathCase gnpyPathCase = new PathCaseBuilder().setPathProperties(pathProperties).build();
252                 respType = gnpyPathCase;
253                 feasible = true;
254             }
255         }
256         GnpyResponse gnpypResp = new GnpyResponseBuilder().setPathDir(pathDir).setResponseType(respType)
257                 .setFeasibility(feasible).build();
258         return gnpypResp;
259     }
260
261 }