2 * Copyright © 2017 AT&T, Inc. and others. All rights reserved.
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
8 package org.opendaylight.transportpce.pce.service;
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;
55 public class PathComputationServiceImpl implements PathComputationService {
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;
63 public PathComputationServiceImpl(NetworkTransactionService networkTransactionService,
64 NotificationPublishService notificationPublishService) {
65 this.notificationPublishService = notificationPublishService;
66 this.networkTransactionService = networkTransactionService;
67 this.executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(5));
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);
89 this.notification = servicePathRpcResultBuilder.build();
91 notificationPublishService.putNotification(this.notification);
92 } catch (InterruptedException e) {
93 LOG.info("notification offer rejected: ", e);
98 public ListenableFuture<CancelResourceReserveOutput> cancelResourceReserve(CancelResourceReserveInput input) {
99 LOG.info("cancelResourceReserve");
100 return executor.submit(new Callable<CancelResourceReserveOutput>() {
103 public CancelResourceReserveOutput call() throws Exception {
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 !";
112 message = "Cancelling ResourceReserve failed !";
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();
130 public ListenableFuture<PathComputationRequestOutput> pathComputationRequest(PathComputationRequestInput input) {
131 LOG.info("pathComputationRequest");
132 return executor.submit(new Callable<PathComputationRequestOutput>() {
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();
151 sendNotifications(ServicePathNotificationTypes.PathComputationRequest, input.getServiceName(),
152 RpcStatusEx.Pending, "Service compliant, submitting pathComputation Request ...", null);
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);
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);
171 if (gnpyZtoA != null) {
172 GnpyResponse respZtoA = generateGnpyResponse(gnpyZtoA.getResponse(),"Z-to-A");
173 listResponse.add(respZtoA);
175 output.setGnpyResponse(listResponse);
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();
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())
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())
202 ResponseParametersBuilder rpb = new ResponseParametersBuilder().setPathDescription(pathDescription1);
203 output.setConfigurationResponseCommon(configurationResponseCommon.build())
204 .setResponseParameters(rpb.build());
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 (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629
211 .path.description.atoz.direction.AToZKey key : atoz.getAToZ().keySet()) {
212 LOG.debug("Impl AtoZ Notification: [{}] {}", key, atoz.getAToZ().get(key));
215 ZToADirection ztoa = pathDescription.getZToADirection();
216 if ((ztoa != null) && (ztoa.getZToA() != null)) {
217 LOG.debug("Impl ZtoA Notification: [{}] elements in description", ztoa.getZToA().size());
218 for (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629
219 .path.description.ztoa.direction.ZToAKey key : ztoa.getZToA().keySet()) {
220 LOG.debug("Impl ZtoA Notification: [{}] {}", key, ztoa.getZToA().get(key));
223 return output.build();
228 public GnpyResponse generateGnpyResponse(Response responseGnpy, String pathDir) {
229 ResponseType respType = null;
230 boolean feasible = true;
231 if (responseGnpy != null) {
232 if (responseGnpy.getResponseType() instanceof org.opendaylight.yang.gen.v1.gnpy.path.rev200202.result
233 .response.response.type.NoPathCase) {
234 LOG.info("GNPy : path is not feasible");
235 org.opendaylight.yang.gen.v1.gnpy.path.rev200202.result.response.response.type.NoPathCase
236 noPathGnpy = (org.opendaylight.yang.gen.v1.gnpy.path.rev200202.result.response.response.type
237 .NoPathCase) responseGnpy.getResponseType();
238 NoPathCase noPathCase = new NoPathCaseBuilder().setNoPath(noPathGnpy.getNoPath()).build();
239 respType = noPathCase;
241 } else if (responseGnpy.getResponseType() instanceof org.opendaylight.yang.gen.v1.gnpy.path.rev200202.result
242 .response.response.type.PathCase) {
243 LOG.info("GNPy : path is feasible");
244 org.opendaylight.yang.gen.v1.gnpy.path.rev200202.result.response.response.type.PathCase pathCase =
245 (org.opendaylight.yang.gen.v1.gnpy.path.rev200202.result.response.response.type.PathCase)
246 responseGnpy.getResponseType();
247 List<org.opendaylight.yang.gen.v1.gnpy.path.rev200202.generic.path.properties.path.properties
248 .PathMetric> pathMetricList = new ArrayList(pathCase.getPathProperties().getPathMetric().values());
249 List<PathMetric> gnpyPathMetricList = new ArrayList<>();
250 for (org.opendaylight.yang.gen.v1.gnpy.path.rev200202.generic.path.properties.path.properties.PathMetric
251 pathMetricGnpy : pathMetricList) {
252 PathMetric pathMetric = new PathMetricBuilder().setMetricType(pathMetricGnpy.getMetricType())
253 .setAccumulativeValue(pathMetricGnpy.getAccumulativeValue()).build();
254 gnpyPathMetricList.add(pathMetric);
256 PathProperties pathProperties = new PathPropertiesBuilder().setPathMetric(gnpyPathMetricList).build();
257 PathCase gnpyPathCase = new PathCaseBuilder().setPathProperties(pathProperties).build();
258 respType = gnpyPathCase;
262 return new GnpyResponseBuilder().setPathDir(pathDir).setResponseType(respType).setFeasibility(feasible).build();