fix import extra separations
[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 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.concurrent.Callable;
16 import java.util.concurrent.Executors;
17 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
18 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
19 import org.opendaylight.transportpce.pce.PceComplianceCheck;
20 import org.opendaylight.transportpce.pce.PceComplianceCheckResult;
21 import org.opendaylight.transportpce.pce.PceSendingPceRPCs;
22 import org.opendaylight.transportpce.pce.gnpy.GnpyResult;
23 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.result.Response;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.CancelResourceReserveInput;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.CancelResourceReserveOutput;
26 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.CancelResourceReserveOutputBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestInput;
28 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestOutput;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestOutputBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.ServicePathRpcResult;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.ServicePathRpcResultBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.gnpy.GnpyResponse;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.gnpy.GnpyResponseBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.gnpy.gnpy.response.ResponseType;
35 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.gnpy.gnpy.response.response.type.NoPathCase;
36 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.gnpy.gnpy.response.response.type.NoPathCaseBuilder;
37 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.gnpy.gnpy.response.response.type.PathCase;
38 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.gnpy.gnpy.response.response.type.PathCaseBuilder;
39 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.path.performance.PathProperties;
40 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.path.performance.PathPropertiesBuilder;
41 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.path.performance.path.properties.PathMetric;
42 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.path.performance.path.properties.PathMetricBuilder;
43 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.service.path.rpc.result.PathDescription;
44 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.service.path.rpc.result.PathDescriptionBuilder;
45 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.configuration.response.common.ConfigurationResponseCommonBuilder;
46 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.AToZDirection;
47 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.ZToADirection;
48 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.RpcStatusEx;
49 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.ServicePathNotificationTypes;
50 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.response.parameters.sp.ResponseParametersBuilder;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 public class PathComputationServiceImpl implements PathComputationService {
55
56     private static final Logger LOG = LoggerFactory.getLogger(PathComputationServiceImpl.class);
57     private final NotificationPublishService notificationPublishService;
58     private NetworkTransactionService networkTransactionService;
59     private final ListeningExecutorService executor;
60     ServicePathRpcResult notification = null;
61
62     public PathComputationServiceImpl(NetworkTransactionService networkTransactionService,
63                                       NotificationPublishService notificationPublishService) {
64         this.notificationPublishService = notificationPublishService;
65         this.networkTransactionService = networkTransactionService;
66         this.executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(5));
67     }
68
69     public void init() {
70         LOG.info("init ...");
71     }
72
73     public void close() {
74         LOG.info("close.");
75     }
76
77     private void sendNotifications(ServicePathNotificationTypes servicePathNotificationTypes, String serviceName,
78             RpcStatusEx rpcStatusEx, String message, PathDescription pathDescription) {
79         ServicePathRpcResultBuilder servicePathRpcResultBuilder =
80                 new ServicePathRpcResultBuilder().setNotificationType(servicePathNotificationTypes)
81                         .setServiceName(serviceName).setStatus(rpcStatusEx).setStatusMessage(message);
82         if (pathDescription != null) {
83             servicePathRpcResultBuilder.setPathDescription(pathDescription);
84         }
85         this.notification = servicePathRpcResultBuilder.build();
86         try {
87             notificationPublishService.putNotification(this.notification);
88         } catch (InterruptedException e) {
89             LOG.info("notification offer rejected: ", e);
90         }
91     }
92
93     @Override
94     public ListenableFuture<CancelResourceReserveOutput> cancelResourceReserve(CancelResourceReserveInput input) {
95         LOG.info("cancelResourceReserve");
96         return executor.submit(new Callable<CancelResourceReserveOutput>() {
97
98             @Override
99             public CancelResourceReserveOutput call() throws Exception {
100                 String message = "";
101                 sendNotifications(ServicePathNotificationTypes.CancelResourceReserve, input.getServiceName(),
102                         RpcStatusEx.Pending, "Service compliant, submitting cancelResourceReserve Request ...", null);
103                 PceSendingPceRPCs sendingPCE = new PceSendingPceRPCs();
104                 sendingPCE.cancelResourceReserve();
105                 if (Boolean.TRUE.equals(sendingPCE.getSuccess())) {
106                     message = "ResourceReserve cancelled !";
107                 } else {
108                     message = "Cancelling ResourceReserve failed !";
109                 }
110                 LOG.info("in PathComputationServiceImpl : {}",message);
111                 sendNotifications(ServicePathNotificationTypes.CancelResourceReserve, input.getServiceName(),
112                         RpcStatusEx.Successful, "cancel Resource Reserve successful!", null);
113                 ConfigurationResponseCommonBuilder configurationResponseCommon =
114                         new ConfigurationResponseCommonBuilder();
115                 configurationResponseCommon.setAckFinalIndicator("Yes")
116                         .setRequestId(input.getServiceHandlerHeader().getRequestId()).setResponseCode("200")
117                         .setResponseMessage("");
118                 CancelResourceReserveOutputBuilder output = new CancelResourceReserveOutputBuilder();
119                 output.setConfigurationResponseCommon(configurationResponseCommon.build());
120                 return output.build();
121             }
122         });
123     }
124
125     @Override
126     public ListenableFuture<PathComputationRequestOutput> pathComputationRequest(PathComputationRequestInput input) {
127         LOG.info("pathComputationRequest");
128         return executor.submit(new Callable<PathComputationRequestOutput>() {
129
130             @Override
131             public PathComputationRequestOutput call() throws Exception {
132                 PathComputationRequestOutputBuilder output = new PathComputationRequestOutputBuilder();
133                 ConfigurationResponseCommonBuilder configurationResponseCommon =
134                         new ConfigurationResponseCommonBuilder();
135                 PceComplianceCheckResult check = PceComplianceCheck.check(input);
136                 if (!check.hasPassed()) {
137                     LOG.error("Path not calculated, service not compliant : {}", check.getMessage());
138                     sendNotifications(ServicePathNotificationTypes.PathComputationRequest, input.getServiceName(),
139                             RpcStatusEx.Failed, "Path not calculated, service not compliant", null);
140                     configurationResponseCommon.setAckFinalIndicator("Yes")
141                             .setRequestId(input.getServiceHandlerHeader().getRequestId())
142                             .setResponseCode("Path not calculated").setResponseMessage(check.getMessage());
143                     output.setConfigurationResponseCommon(configurationResponseCommon.build())
144                             .setResponseParameters(null);
145                     return output.build();
146                 }
147                 sendNotifications(ServicePathNotificationTypes.PathComputationRequest, input.getServiceName(),
148                         RpcStatusEx.Pending, "Service compliant, submitting pathComputation Request ...", null);
149                 String message = "";
150                 String responseCode = "";
151                 PceSendingPceRPCs sendingPCE = new PceSendingPceRPCs(input, networkTransactionService);
152                 sendingPCE.pathComputation();
153                 message = sendingPCE.getMessage();
154                 responseCode = sendingPCE.getResponseCode();
155                 PathDescriptionBuilder path = null;
156                 path = sendingPCE.getPathDescription();
157                 LOG.info("PCE response: {} {}", message, responseCode);
158
159                 //add the GNPy result
160                 GnpyResult gnpyAtoZ = sendingPCE.getGnpyAtoZ();
161                 GnpyResult gnpyZtoA = sendingPCE.getGnpyZtoA();
162                 List<GnpyResponse> listResponse = new ArrayList<>();
163                 if (gnpyAtoZ != null) {
164                     GnpyResponse respAtoZ = generateGnpyResponse(gnpyAtoZ.getResponse(),"A-to-Z");
165                     listResponse.add(respAtoZ);
166                 }
167                 if (gnpyZtoA != null) {
168                     GnpyResponse respZtoA = generateGnpyResponse(gnpyZtoA.getResponse(),"Z-to-A");
169                     listResponse.add(respZtoA);
170                 }
171                 output.setGnpyResponse(listResponse);
172
173                 if (Boolean.FALSE.equals(sendingPCE.getSuccess()) || (path == null)) {
174                     configurationResponseCommon.setAckFinalIndicator("Yes")
175                             .setRequestId(input.getServiceHandlerHeader().getRequestId()).setResponseCode(responseCode)
176                             .setResponseMessage(message);
177                     output.setConfigurationResponseCommon(configurationResponseCommon.build());
178                     sendNotifications(ServicePathNotificationTypes.PathComputationRequest, input.getServiceName(),
179                             RpcStatusEx.Failed, "Path not calculated", null);
180                     return output.build();
181                 }
182                 // Path calculator returned Success
183                 configurationResponseCommon.setAckFinalIndicator("Yes")
184                         .setRequestId(input.getServiceHandlerHeader().getRequestId()).setResponseCode(responseCode)
185                         .setResponseMessage(message);
186                 PathDescription pathDescription = new org.opendaylight.yang.gen.v1.http.org.opendaylight
187                         .transportpce.pce.rev200128.service.path.rpc.result.PathDescriptionBuilder()
188                                 .setAToZDirection(path.getAToZDirection()).setZToADirection(path.getZToADirection())
189                                 .build();
190                 sendNotifications(ServicePathNotificationTypes.PathComputationRequest, input.getServiceName(),
191                         RpcStatusEx.Successful, message, pathDescription);
192                 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.response
193                     .parameters.sp.response.parameters.PathDescription pathDescription1 = new org.opendaylight.yang.gen
194                         .v1.http.org.transportpce.b.c._interface.service.types.rev200128.response.parameters.sp
195                         .response.parameters.PathDescriptionBuilder()
196                                 .setAToZDirection(path.getAToZDirection()).setZToADirection(path.getZToADirection())
197                                 .build();
198                 ResponseParametersBuilder rpb = new ResponseParametersBuilder().setPathDescription(pathDescription1);
199                 output.setConfigurationResponseCommon(configurationResponseCommon.build())
200                         .setResponseParameters(rpb.build());
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.rev200202.result
227                     .response.response.type.NoPathCase) {
228                 LOG.info("GNPy : path is not feasible");
229                 org.opendaylight.yang.gen.v1.gnpy.path.rev200202.result.response.response.type.NoPathCase
230                     noPathGnpy = (org.opendaylight.yang.gen.v1.gnpy.path.rev200202.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.rev200202.result
236                     .response.response.type.PathCase) {
237                 LOG.info("GNPy : path is feasible");
238                 org.opendaylight.yang.gen.v1.gnpy.path.rev200202.result.response.response.type.PathCase pathCase =
239                         (org.opendaylight.yang.gen.v1.gnpy.path.rev200202.result.response.response.type.PathCase)
240                         responseGnpy.getResponseType();
241                 List<org.opendaylight.yang.gen.v1.gnpy.path.rev200202.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.rev200202.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         return new GnpyResponseBuilder().setPathDir(pathDir).setResponseType(respType).setFeasibility(feasible).build();
257     }
258
259 }