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;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.concurrent.Callable;
17 import java.util.concurrent.Executors;
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;
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;
57 public class PathComputationServiceImpl implements PathComputationService {
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;
65 public PathComputationServiceImpl(NetworkTransactionService networkTransactionService,
66 NotificationPublishService notificationPublishService) {
67 this.notificationPublishService = notificationPublishService;
68 this.networkTransactionService = networkTransactionService;
69 this.executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(5));
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);
88 this.notification = servicePathRpcResultBuilder.build();
90 notificationPublishService.putNotification(this.notification);
91 } catch (InterruptedException e) {
92 LOG.info("notification offer rejected: ", e);
97 public ListenableFuture<CancelResourceReserveOutput> cancelResourceReserve(CancelResourceReserveInput input) {
98 LOG.info("cancelResourceReserve");
99 return executor.submit(new Callable<CancelResourceReserveOutput>() {
102 public CancelResourceReserveOutput call() throws Exception {
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 !";
111 message = "Cancelling ResourceReserve failed !";
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();
129 public ListenableFuture<PathComputationRequestOutput> pathComputationRequest(PathComputationRequestInput input) {
130 LOG.info("pathComputationRequest");
131 return executor.submit(new Callable<PathComputationRequestOutput>() {
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();
150 sendNotifications(ServicePathNotificationTypes.PathComputationRequest, input.getServiceName(),
151 RpcStatusEx.Pending, "Service compliant, submitting pathComputation Request ...", null);
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);
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);
170 if (gnpyZtoA != null) {
171 GnpyResponse respZtoA = generateGnpyResponse(gnpyZtoA.getResponse(),"Z-to-A");
172 listResponse.add(respZtoA);
174 output.setGnpyResponse(listResponse);
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();
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())
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())
201 ResponseParametersBuilder rpb = new ResponseParametersBuilder().setPathDescription(pathDescription1);
202 output.setConfigurationResponseCommon(configurationResponseCommon.build())
203 .setResponseParameters(rpb.build());
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));
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));
220 return output.build();
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;
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);
253 PathProperties pathProperties = new PathPropertiesBuilder().setPathMetric(gnpyPathMetricList).build();
254 PathCase gnpyPathCase = new PathCaseBuilder().setPathProperties(pathProperties).build();
255 respType = gnpyPathCase;
259 GnpyResponse gnpypResp = new GnpyResponseBuilder().setPathDir(pathDir).setResponseType(respType)
260 .setFeasibility(feasible).build();