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;
12 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
13 import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener;
14 import org.opendaylight.transportpce.common.OperationResult;
15 import org.opendaylight.transportpce.pce.service.PathComputationService;
16 import org.opendaylight.transportpce.renderer.provisiondevice.RendererServiceOperations;
17 import org.opendaylight.transportpce.servicehandler.ModelMappingUtils;
18 import org.opendaylight.transportpce.servicehandler.ServiceInput;
19 import org.opendaylight.transportpce.servicehandler.service.PCEServiceWrapper;
20 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperations;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.PathComputationRequestOutputBuilder;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.ServicePathRpcResult;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.service.path.rpc.result.PathDescription;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.service.path.rpc.result.PathDescriptionBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.ServiceImplementationRequestInput;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev191129.State;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.service.list.Services;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.RpcStatusEx;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.response.parameters.sp.ResponseParametersBuilder;
30 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishNotificationProcessService;
31 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishNotificationProcessServiceBuilder;
32 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.notification.process.service.ServiceAEndBuilder;
33 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.notification.process.service.ServiceZEndBuilder;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Reference;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 @Component(service = {PceNotificationHandler.class, PceListener.class})
41 public class PceNotificationHandler implements PceListener {
43 private static final Logger LOG = LoggerFactory.getLogger(PceNotificationHandler.class);
44 private static final String PUBLISHER = "PceListener";
46 private ServicePathRpcResult servicePathRpcResult;
47 private RendererServiceOperations rendererServiceOperations;
48 private ServiceDataStoreOperations serviceDataStoreOperations;
49 private PCEServiceWrapper pceServiceWrapper;
50 private ServiceInput input;
51 private Boolean serviceReconfigure;
52 private Boolean tempService;
53 private Boolean serviceFeasiblity;
54 private NotificationPublishService notificationPublishService;
57 public PceNotificationHandler(
58 @Reference RendererServiceOperations rendererServiceOperations,
59 @Reference PathComputationService pathComputationService,
60 @Reference NotificationPublishService notificationPublishService,
61 @Reference ServiceDataStoreOperations serviceDataStoreOperations) {
62 this.rendererServiceOperations = rendererServiceOperations;
63 this.pceServiceWrapper = new PCEServiceWrapper(pathComputationService, notificationPublishService);
64 this.serviceDataStoreOperations = serviceDataStoreOperations;
65 setServiceReconfigure(false);
67 setTempService(false);
68 setServiceFeasiblity(false);
69 this.notificationPublishService = notificationPublishService;
72 public CompositeListener getCompositeListener() {
73 return new CompositeListener(Set.of(
74 new CompositeListener.Component<>(ServicePathRpcResult.class, this::onServicePathRpcResult)));
77 private void onServicePathRpcResult(ServicePathRpcResult notification) {
78 if (compareServicePathRpcResult(notification)) {
79 LOG.warn("ServicePathRpcResult already wired !");
82 servicePathRpcResult = notification;
83 switch (servicePathRpcResult.getNotificationType().getIntValue()) {
84 /* path-computation-request. */
86 onPathComputationResult(notification);
88 /* cancel-resource-reserve. */
90 onCancelResourceResult();
98 * Process path computation request result.
99 * @param notification the result notification.
101 private void onPathComputationResult(ServicePathRpcResult notification) {
102 LOG.info("PCE '{}' Notification received : {}", servicePathRpcResult.getNotificationType().getName(),
105 if (!checkStatus(notification)) {
108 if (servicePathRpcResult.getPathDescription() == null) {
109 LOG.error("'PathDescription' parameter is null ");
112 PathDescription pathDescription =
113 new PathDescriptionBuilder()
114 .setAToZDirection(servicePathRpcResult.getPathDescription().getAToZDirection())
115 .setZToADirection(servicePathRpcResult.getPathDescription().getZToADirection())
117 LOG.info("PathDescription gets : {}", pathDescription);
118 if (serviceFeasiblity) {
119 LOG.warn("service-feasibility-check RPC ");
123 LOG.error("Input is null !");
126 OperationResult operationResult = null;
129 this.serviceDataStoreOperations.createTempService(
130 input.getTempServiceCreateInput(), pathDescription);
131 if (!operationResult.isSuccess()) {
132 LOG.error("Temp Service not created in datastore !");
135 operationResult = this.serviceDataStoreOperations.createService(input.getServiceCreateInput());
136 if (!operationResult.isSuccess()) {
137 LOG.error("Service not created in datastore !");
140 if (!this.serviceDataStoreOperations
144 new PathComputationRequestOutputBuilder()
145 .setResponseParameters(
146 new ResponseParametersBuilder()
148 new org.opendaylight.yang.gen.v1
149 .http.org.transportpce.b.c._interface.service.types.rev220118
150 .response.parameters.sp.response.parameters
151 .PathDescriptionBuilder(pathDescription)
156 LOG.error("Service Path not created in datastore !");
158 ServiceImplementationRequestInput serviceImplementationRequest =
159 ModelMappingUtils.createServiceImplementationRequest(input, pathDescription);
160 LOG.info("Sending serviceImplementation request : {}", serviceImplementationRequest);
161 LOG.debug("Temp-service value is {}", tempService);
162 this.rendererServiceOperations.serviceImplementation(serviceImplementationRequest, tempService);
166 * Check status of notification and send nbi notification.
167 * @param notification ServicePathRpcResult the notification to check.
168 * @return true is status is Successful, false otherwise.
170 private boolean checkStatus(ServicePathRpcResult notification) {
171 PublishNotificationProcessService nbiNotification = getPublishNotificationProcessService(notification);
172 PublishNotificationProcessServiceBuilder publishNotificationProcessServiceBuilder =
173 new PublishNotificationProcessServiceBuilder(nbiNotification);
174 //TODO is it worth to instantiate the 2 variables above if status is 'Pending' or 'Successful' ?
175 switch (servicePathRpcResult.getStatus()) {
177 LOG.error("PCE path computation failed !");
178 nbiNotification = publishNotificationProcessServiceBuilder
179 .setMessage("ServiceCreate request failed ...")
180 .setResponseFailed("PCE path computation failed !")
181 .setOperationalState(State.Degraded).build();
182 sendNbiNotification(nbiNotification);
185 LOG.warn("PCE path computation returned a Pending RpcStatusEx code!");
188 LOG.info("PCE calculation done OK !");
191 LOG.error("PCE path computation returned an unknown RpcStatusEx code {}",
192 servicePathRpcResult.getStatus());
193 nbiNotification = publishNotificationProcessServiceBuilder
194 .setMessage("ServiceCreate request failed ...")
195 .setResponseFailed("PCE path computation returned an unknown RpcStatusEx code!")
196 .setOperationalState(State.Degraded).build();
197 sendNbiNotification(nbiNotification);
202 private PublishNotificationProcessService getPublishNotificationProcessService(ServicePathRpcResult notification) {
204 return new PublishNotificationProcessServiceBuilder()
205 .setServiceName(notification.getServiceName())
206 .setPublisherName(PUBLISHER)
209 return new PublishNotificationProcessServiceBuilder()
210 .setServiceName(input.getServiceName())
211 .setServiceAEnd(new ServiceAEndBuilder(input.getServiceAEnd()).build())
212 .setServiceZEnd(new ServiceZEndBuilder(input.getServiceZEnd()).build())
213 .setCommonId(input.getCommonId())
214 .setConnectionType(input.getConnectionType())
215 .setPublisherName(PUBLISHER)
220 * Process cancel resource result.
222 private void onCancelResourceResult() {
223 if (servicePathRpcResult.getStatus() == RpcStatusEx.Pending) {
224 LOG.warn("PCE cancel returned a Pending RpcStatusEx code !");
226 } else if (servicePathRpcResult.getStatus() != RpcStatusEx.Successful
227 && servicePathRpcResult.getStatus() != RpcStatusEx.Failed) {
228 LOG.error("PCE cancel returned an unknown RpcStatusEx code !");
231 PublishNotificationProcessServiceBuilder nbiNotificationBuilder;
232 State serviceOpState;
234 org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.temp.service.list.Services
235 tempServiceList = serviceDataStoreOperations.getTempService(input.getServiceName()).orElseThrow();
236 serviceOpState = tempServiceList.getOperationalState();
237 nbiNotificationBuilder =
238 new PublishNotificationProcessServiceBuilder()
239 .setServiceAEnd(new ServiceAEndBuilder(tempServiceList.getServiceAEnd()).build())
240 .setServiceZEnd(new ServiceZEndBuilder(tempServiceList.getServiceZEnd()).build())
241 .setCommonId(tempServiceList.getCommonId())
242 .setConnectionType(tempServiceList.getConnectionType())
243 .setPublisherName(PUBLISHER);
245 Services service = serviceDataStoreOperations.getService(input.getServiceName()).orElseThrow();
246 serviceOpState = service.getOperationalState();
247 nbiNotificationBuilder =
248 new PublishNotificationProcessServiceBuilder()
249 .setServiceName(service.getServiceName())
250 .setServiceAEnd(new ServiceAEndBuilder(service.getServiceAEnd()).build())
251 .setServiceZEnd(new ServiceZEndBuilder(service.getServiceZEnd()).build())
252 .setCommonId(service.getCommonId())
253 .setConnectionType(service.getConnectionType())
254 .setPublisherName(PUBLISHER);
258 if (servicePathRpcResult.getStatus() == RpcStatusEx.Failed) {
259 LOG.info("PCE cancel resource failed !");
261 nbiNotificationBuilder
262 .setResponseFailed("PCE cancel resource failed !")
263 .setMessage("ServiceDelete request failed ...")
264 .setOperationalState(serviceOpState)
268 LOG.info("PCE cancel resource done OK !");
269 // Here the input refers to the transportPCE API and the serviceName will be commonId for temp-service
270 OperationResult deleteServicePathOperationResult =
271 this.serviceDataStoreOperations.deleteServicePath(input.getServiceName());
272 if (!deleteServicePathOperationResult.isSuccess()) {
273 LOG.warn("Service path was not removed from datastore !");
275 OperationResult deleteServiceOperationResult;
276 String serviceType = "";
278 deleteServiceOperationResult = this.serviceDataStoreOperations.deleteTempService(input.getServiceName());
279 serviceType = "Temp ";
281 deleteServiceOperationResult = this.serviceDataStoreOperations.deleteService(input.getServiceName());
283 if (deleteServiceOperationResult.isSuccess()) {
285 nbiNotificationBuilder
286 .setResponseFailed("")
287 .setMessage("{} Service deleted !")
288 .setOperationalState(State.Degraded)
291 LOG.warn("{} Service was not removed from datastore !", serviceType);
293 nbiNotificationBuilder
294 .setResponseFailed(serviceType + "Service was not removed from datastore !")
295 .setMessage("ServiceDelete request failed ...")
296 .setOperationalState(serviceOpState)
299 // TODO: should we re-initialize the temp-service boolean to false?
301 * if it was an RPC serviceReconfigure, re-launch PCR.
303 if (this.serviceReconfigure) {
304 LOG.info("cancel resource reserve done, relaunching PCE path computation ...");
305 this.pceServiceWrapper.performPCE(input.getServiceCreateInput(), true);
306 this.serviceReconfigure = false;
311 value = "ES_COMPARING_STRINGS_WITH_EQ",
312 justification = "false positives, not strings but real object references comparisons")
313 private Boolean compareServicePathRpcResult(ServicePathRpcResult notification) {
314 if (servicePathRpcResult == null) {
317 if (servicePathRpcResult.getNotificationType() != notification.getNotificationType()) {
320 if (servicePathRpcResult.getServiceName() != notification.getServiceName()) {
323 if (servicePathRpcResult.getStatus() != notification.getStatus()) {
326 if (servicePathRpcResult.getStatusMessage() != notification.getStatusMessage()) {
333 public void setInput(ServiceInput serviceInput) {
334 this.input = serviceInput;
338 public void setServiceReconfigure(Boolean serv) {
339 this.serviceReconfigure = serv;
343 public void setserviceDataStoreOperations(ServiceDataStoreOperations serviceData) {
344 this.serviceDataStoreOperations = serviceData;
348 public void setTempService(Boolean tempService) {
349 this.tempService = tempService;
353 public void setServiceFeasiblity(Boolean serviceFeasiblity) {
354 this.serviceFeasiblity = serviceFeasiblity;
358 * Send notification to NBI notification in order to publish message.
359 * @param service PublishNotificationService
361 private void sendNbiNotification(PublishNotificationProcessService service) {
363 notificationPublishService.putNotification(service);
364 } catch (InterruptedException e) {
365 LOG.warn("Cannot send notification to nbi", e);
366 Thread.currentThread().interrupt();