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