2 * Copyright © 2017 Orange, 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.servicehandler.listeners;
10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
12 import org.opendaylight.transportpce.common.OperationResult;
13 import org.opendaylight.transportpce.pce.service.PathComputationService;
14 import org.opendaylight.transportpce.renderer.provisiondevice.RendererServiceOperations;
15 import org.opendaylight.transportpce.servicehandler.ModelMappingUtils;
16 import org.opendaylight.transportpce.servicehandler.ServiceInput;
17 import org.opendaylight.transportpce.servicehandler.service.PCEServiceWrapper;
18 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperations;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.PathComputationRequestOutputBuilder;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.ServicePathRpcResult;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.TransportpcePceListener;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.service.path.rpc.result.PathDescription;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.service.path.rpc.result.PathDescriptionBuilder;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.ServiceImplementationRequestInput;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev191129.State;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.service.list.Services;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.RpcStatusEx;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.response.parameters.sp.ResponseParametersBuilder;
29 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishNotificationProcessService;
30 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishNotificationProcessServiceBuilder;
31 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.notification.process.service.ServiceAEndBuilder;
32 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.notification.process.service.ServiceZEndBuilder;
33 import org.osgi.service.component.annotations.Activate;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.Reference;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 public class PceListenerImpl implements TransportpcePceListener, PceListener {
42 private static final Logger LOG = LoggerFactory.getLogger(PceListenerImpl.class);
43 private static final String PUBLISHER = "PceListener";
45 private ServicePathRpcResult servicePathRpcResult;
46 private RendererServiceOperations rendererServiceOperations;
47 private ServiceDataStoreOperations serviceDataStoreOperations;
48 private PCEServiceWrapper pceServiceWrapper;
49 private ServiceInput input;
50 private Boolean serviceReconfigure;
51 private Boolean tempService;
52 private Boolean serviceFeasiblity;
53 private NotificationPublishService notificationPublishService;
56 public PceListenerImpl(
57 @Reference RendererServiceOperations rendererServiceOperations,
58 @Reference PathComputationService pathComputationService,
59 @Reference NotificationPublishService notificationPublishService,
60 @Reference ServiceDataStoreOperations serviceDataStoreOperations) {
61 this.rendererServiceOperations = rendererServiceOperations;
62 this.pceServiceWrapper = new PCEServiceWrapper(pathComputationService, notificationPublishService);
63 this.serviceDataStoreOperations = serviceDataStoreOperations;
64 setServiceReconfigure(false);
66 setTempService(false);
67 setServiceFeasiblity(false);
68 this.notificationPublishService = notificationPublishService;
72 public void onServicePathRpcResult(ServicePathRpcResult notification) {
73 if (compareServicePathRpcResult(notification)) {
74 LOG.warn("ServicePathRpcResult already wired !");
77 servicePathRpcResult = notification;
78 switch (servicePathRpcResult.getNotificationType().getIntValue()) {
79 /* path-computation-request. */
81 onPathComputationResult(notification);
83 /* cancel-resource-reserve. */
85 onCancelResourceResult();
93 * Process path computation request result.
94 * @param notification the result notification.
96 private void onPathComputationResult(ServicePathRpcResult notification) {
97 LOG.info("PCE '{}' Notification received : {}", servicePathRpcResult.getNotificationType().getName(),
100 if (!checkStatus(notification)) {
103 if (servicePathRpcResult.getPathDescription() == null) {
104 LOG.error("'PathDescription' parameter is null ");
107 PathDescription pathDescription =
108 new PathDescriptionBuilder()
109 .setAToZDirection(servicePathRpcResult.getPathDescription().getAToZDirection())
110 .setZToADirection(servicePathRpcResult.getPathDescription().getZToADirection())
112 LOG.info("PathDescription gets : {}", pathDescription);
113 if (serviceFeasiblity) {
114 LOG.warn("service-feasibility-check RPC ");
118 LOG.error("Input is null !");
121 OperationResult operationResult = null;
124 this.serviceDataStoreOperations.createTempService(
125 input.getTempServiceCreateInput(), pathDescription);
126 if (!operationResult.isSuccess()) {
127 LOG.error("Temp Service not created in datastore !");
130 operationResult = this.serviceDataStoreOperations.createService(input.getServiceCreateInput());
131 if (!operationResult.isSuccess()) {
132 LOG.error("Service not created in datastore !");
135 if (!this.serviceDataStoreOperations
139 new PathComputationRequestOutputBuilder()
140 .setResponseParameters(
141 new ResponseParametersBuilder()
143 new org.opendaylight.yang.gen.v1
144 .http.org.transportpce.b.c._interface.service.types.rev220118
145 .response.parameters.sp.response.parameters
146 .PathDescriptionBuilder(pathDescription)
151 LOG.error("Service Path not created in datastore !");
153 ServiceImplementationRequestInput serviceImplementationRequest =
154 ModelMappingUtils.createServiceImplementationRequest(input, pathDescription);
155 LOG.info("Sending serviceImplementation request : {}", serviceImplementationRequest);
156 LOG.debug("Temp-service value is {}", tempService);
157 this.rendererServiceOperations.serviceImplementation(serviceImplementationRequest, tempService);
161 * Check status of notification and send nbi notification.
162 * @param notification ServicePathRpcResult the notification to check.
163 * @return true is status is Successful, false otherwise.
165 private boolean checkStatus(ServicePathRpcResult notification) {
166 PublishNotificationProcessService nbiNotification = getPublishNotificationProcessService(notification);
167 PublishNotificationProcessServiceBuilder publishNotificationProcessServiceBuilder =
168 new PublishNotificationProcessServiceBuilder(nbiNotification);
169 //TODO is it worth to instantiate the 2 variables above if status is 'Pending' or 'Successful' ?
170 switch (servicePathRpcResult.getStatus()) {
172 LOG.error("PCE path computation failed !");
173 nbiNotification = publishNotificationProcessServiceBuilder
174 .setMessage("ServiceCreate request failed ...")
175 .setResponseFailed("PCE path computation failed !")
176 .setOperationalState(State.Degraded).build();
177 sendNbiNotification(nbiNotification);
180 LOG.warn("PCE path computation returned a Pending RpcStatusEx code!");
183 LOG.info("PCE calculation done OK !");
186 LOG.error("PCE path computation returned an unknown RpcStatusEx code {}",
187 servicePathRpcResult.getStatus());
188 nbiNotification = publishNotificationProcessServiceBuilder
189 .setMessage("ServiceCreate request failed ...")
190 .setResponseFailed("PCE path computation returned an unknown RpcStatusEx code!")
191 .setOperationalState(State.Degraded).build();
192 sendNbiNotification(nbiNotification);
197 private PublishNotificationProcessService getPublishNotificationProcessService(ServicePathRpcResult notification) {
199 return new PublishNotificationProcessServiceBuilder()
200 .setServiceName(notification.getServiceName())
201 .setPublisherName(PUBLISHER)
204 return new PublishNotificationProcessServiceBuilder()
205 .setServiceName(input.getServiceName())
206 .setServiceAEnd(new ServiceAEndBuilder(input.getServiceAEnd()).build())
207 .setServiceZEnd(new ServiceZEndBuilder(input.getServiceZEnd()).build())
208 .setCommonId(input.getCommonId())
209 .setConnectionType(input.getConnectionType())
210 .setPublisherName(PUBLISHER)
215 * Process cancel resource result.
217 private void onCancelResourceResult() {
218 if (servicePathRpcResult.getStatus() == RpcStatusEx.Pending) {
219 LOG.warn("PCE cancel returned a Pending RpcStatusEx code !");
221 } else if (servicePathRpcResult.getStatus() != RpcStatusEx.Successful
222 && servicePathRpcResult.getStatus() != RpcStatusEx.Failed) {
223 LOG.error("PCE cancel returned an unknown RpcStatusEx code !");
226 PublishNotificationProcessServiceBuilder nbiNotificationBuilder;
227 State serviceOpState;
229 org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.temp.service.list.Services
230 tempServiceList = serviceDataStoreOperations.getTempService(input.getServiceName()).orElseThrow();
231 serviceOpState = tempServiceList.getOperationalState();
232 nbiNotificationBuilder =
233 new PublishNotificationProcessServiceBuilder()
234 .setServiceAEnd(new ServiceAEndBuilder(tempServiceList.getServiceAEnd()).build())
235 .setServiceZEnd(new ServiceZEndBuilder(tempServiceList.getServiceZEnd()).build())
236 .setCommonId(tempServiceList.getCommonId())
237 .setConnectionType(tempServiceList.getConnectionType())
238 .setPublisherName(PUBLISHER);
240 Services service = serviceDataStoreOperations.getService(input.getServiceName()).orElseThrow();
241 serviceOpState = service.getOperationalState();
242 nbiNotificationBuilder =
243 new PublishNotificationProcessServiceBuilder()
244 .setServiceName(service.getServiceName())
245 .setServiceAEnd(new ServiceAEndBuilder(service.getServiceAEnd()).build())
246 .setServiceZEnd(new ServiceZEndBuilder(service.getServiceZEnd()).build())
247 .setCommonId(service.getCommonId())
248 .setConnectionType(service.getConnectionType())
249 .setPublisherName(PUBLISHER);
253 if (servicePathRpcResult.getStatus() == RpcStatusEx.Failed) {
254 LOG.info("PCE cancel resource failed !");
256 nbiNotificationBuilder
257 .setResponseFailed("PCE cancel resource failed !")
258 .setMessage("ServiceDelete request failed ...")
259 .setOperationalState(serviceOpState)
263 LOG.info("PCE cancel resource done OK !");
264 // Here the input refers to the transportPCE API and the serviceName will be commonId for temp-service
265 OperationResult deleteServicePathOperationResult =
266 this.serviceDataStoreOperations.deleteServicePath(input.getServiceName());
267 if (!deleteServicePathOperationResult.isSuccess()) {
268 LOG.warn("Service path was not removed from datastore !");
270 OperationResult deleteServiceOperationResult;
271 String serviceType = "";
273 deleteServiceOperationResult = this.serviceDataStoreOperations.deleteTempService(input.getServiceName());
274 serviceType = "Temp ";
276 deleteServiceOperationResult = this.serviceDataStoreOperations.deleteService(input.getServiceName());
278 if (deleteServiceOperationResult.isSuccess()) {
280 nbiNotificationBuilder
281 .setResponseFailed("")
282 .setMessage("{} Service deleted !")
283 .setOperationalState(State.Degraded)
286 LOG.warn("{} Service was not removed from datastore !", serviceType);
288 nbiNotificationBuilder
289 .setResponseFailed(serviceType + "Service was not removed from datastore !")
290 .setMessage("ServiceDelete request failed ...")
291 .setOperationalState(serviceOpState)
294 // TODO: should we re-initialize the temp-service boolean to false?
296 * if it was an RPC serviceReconfigure, re-launch PCR.
298 if (this.serviceReconfigure) {
299 LOG.info("cancel resource reserve done, relaunching PCE path computation ...");
300 this.pceServiceWrapper.performPCE(input.getServiceCreateInput(), true);
301 this.serviceReconfigure = false;
306 value = "ES_COMPARING_STRINGS_WITH_EQ",
307 justification = "false positives, not strings but real object references comparisons")
308 private Boolean compareServicePathRpcResult(ServicePathRpcResult notification) {
309 if (servicePathRpcResult == null) {
312 if (servicePathRpcResult.getNotificationType() != notification.getNotificationType()) {
315 if (servicePathRpcResult.getServiceName() != notification.getServiceName()) {
318 if (servicePathRpcResult.getStatus() != notification.getStatus()) {
321 if (servicePathRpcResult.getStatusMessage() != notification.getStatusMessage()) {
328 public void setInput(ServiceInput serviceInput) {
329 this.input = serviceInput;
333 public void setServiceReconfigure(Boolean serv) {
334 this.serviceReconfigure = serv;
338 public void setserviceDataStoreOperations(ServiceDataStoreOperations serviceData) {
339 this.serviceDataStoreOperations = serviceData;
343 public void setTempService(Boolean tempService) {
344 this.tempService = tempService;
348 public void setServiceFeasiblity(Boolean serviceFeasiblity) {
349 this.serviceFeasiblity = serviceFeasiblity;
353 * Send notification to NBI notification in order to publish message.
354 * @param service PublishNotificationService
356 private void sendNbiNotification(PublishNotificationProcessService service) {
358 notificationPublishService.putNotification(service);
359 } catch (InterruptedException e) {
360 LOG.warn("Cannot send notification to nbi", e);
361 Thread.currentThread().interrupt();