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.rev220118.PathComputationRequestOutput;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.PathComputationRequestOutputBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.ServicePathRpcResult;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.TransportpcePceListener;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.service.path.rpc.result.PathDescription;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220118.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.rev211210.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.ResponseParameters;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.response.parameters.sp.ResponseParametersBuilder;
31 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.PublishNotificationProcessService;
32 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.PublishNotificationProcessServiceBuilder;
33 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.notification.process.service.ServiceAEndBuilder;
34 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.notification.process.service.ServiceZEndBuilder;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class PceListenerImpl implements TransportpcePceListener {
40 private static final Logger LOG = LoggerFactory.getLogger(PceListenerImpl.class);
41 private static final String PUBLISHER = "PceListener";
43 private ServicePathRpcResult servicePathRpcResult;
44 private RendererServiceOperations rendererServiceOperations;
45 private ServiceDataStoreOperations serviceDataStoreOperations;
46 private PCEServiceWrapper pceServiceWrapper;
47 private ServiceInput input;
48 private Boolean serviceReconfigure;
49 private Boolean tempService;
50 private Boolean serviceFeasiblity;
51 private NotificationPublishService notificationPublishService;
53 public PceListenerImpl(RendererServiceOperations rendererServiceOperations,
54 PathComputationService pathComputationService, NotificationPublishService notificationPublishService,
55 ServiceDataStoreOperations serviceDataStoreOperations) {
56 this.rendererServiceOperations = rendererServiceOperations;
57 this.pceServiceWrapper = new PCEServiceWrapper(pathComputationService, notificationPublishService);
58 this.serviceDataStoreOperations = serviceDataStoreOperations;
59 setServiceReconfigure(false);
61 setTempService(false);
62 setServiceFeasiblity(false);
63 this.notificationPublishService = notificationPublishService;
67 public void onServicePathRpcResult(ServicePathRpcResult notification) {
68 if (compareServicePathRpcResult(notification)) {
69 LOG.warn("ServicePathRpcResult already wired !");
72 servicePathRpcResult = notification;
73 switch (servicePathRpcResult.getNotificationType().getIntValue()) {
74 /* path-computation-request. */
76 onPathComputationResult(notification);
78 /* cancel-resource-reserve. */
80 onCancelResourceResult();
88 * Process path computation request result.
89 * @param notification the result notification.
91 private void onPathComputationResult(ServicePathRpcResult notification) {
92 LOG.info("PCE '{}' Notification received : {}", servicePathRpcResult.getNotificationType().getName(),
94 if (!checkStatus(notification)) {
97 if (servicePathRpcResult.getPathDescription() == null) {
98 LOG.error("'PathDescription' parameter is null ");
101 PathDescription pathDescription = new PathDescriptionBuilder()
102 .setAToZDirection(servicePathRpcResult.getPathDescription().getAToZDirection())
103 .setZToADirection(servicePathRpcResult.getPathDescription().getZToADirection())
105 LOG.info("PathDescription gets : {}", pathDescription);
106 if (serviceFeasiblity) {
107 LOG.warn("service-feasibility-check RPC ");
111 LOG.error("Input is null !");
114 OperationResult operationResult = null;
116 operationResult = this.serviceDataStoreOperations.createTempService(input.getTempServiceCreateInput());
117 if (!operationResult.isSuccess()) {
118 LOG.error("Temp Service not created in datastore !");
121 operationResult = this.serviceDataStoreOperations.createService(input.getServiceCreateInput());
122 if (!operationResult.isSuccess()) {
123 LOG.error("Service not created in datastore !");
126 ResponseParameters responseParameters = new ResponseParametersBuilder()
127 .setPathDescription(new org.opendaylight.yang.gen.v1.http
128 .org.transportpce.b.c._interface.service.types.rev220118
129 .response.parameters.sp.response.parameters.PathDescriptionBuilder(pathDescription).build())
131 PathComputationRequestOutput pceResponse = new PathComputationRequestOutputBuilder()
132 .setResponseParameters(responseParameters).build();
133 OperationResult operationServicePathSaveResult = this.serviceDataStoreOperations
134 .createServicePath(input, pceResponse);
135 if (!operationServicePathSaveResult.isSuccess()) {
136 LOG.error("Service Path not created in datastore !");
138 ServiceImplementationRequestInput serviceImplementationRequest = ModelMappingUtils
139 .createServiceImplementationRequest(input, pathDescription);
140 LOG.info("Sending serviceImplementation request : {}", serviceImplementationRequest);
141 this.rendererServiceOperations.serviceImplementation(serviceImplementationRequest);
145 * Check status of notification and send nbi notification.
146 * @param notification ServicePathRpcResult the notification to check.
147 * @return true is status is Successful, false otherwise.
149 private boolean checkStatus(ServicePathRpcResult notification) {
150 PublishNotificationProcessService nbiNotification = getPublishNotificationProcessService(notification);
151 PublishNotificationProcessServiceBuilder publishNotificationProcessServiceBuilder =
152 new PublishNotificationProcessServiceBuilder(nbiNotification);
153 switch (servicePathRpcResult.getStatus()) {
155 LOG.error("PCE path computation failed !");
156 nbiNotification = publishNotificationProcessServiceBuilder
157 .setMessage("ServiceCreate request failed ...")
158 .setResponseFailed("PCE path computation failed !")
159 .setOperationalState(State.Degraded).build();
160 sendNbiNotification(nbiNotification);
163 LOG.warn("PCE path computation returned a Pending RpcStatusEx code!");
166 LOG.info("PCE calculation done OK !");
169 LOG.error("PCE path computation returned an unknown RpcStatusEx code {}",
170 servicePathRpcResult.getStatus());
171 nbiNotification = publishNotificationProcessServiceBuilder
172 .setMessage("ServiceCreate request failed ...")
173 .setResponseFailed("PCE path computation returned an unknown RpcStatusEx code!")
174 .setOperationalState(State.Degraded).build();
175 sendNbiNotification(nbiNotification);
180 private PublishNotificationProcessService getPublishNotificationProcessService(ServicePathRpcResult notification) {
181 PublishNotificationProcessServiceBuilder nbiNotificationBuilder =
182 new PublishNotificationProcessServiceBuilder();
184 nbiNotificationBuilder.setServiceName(input.getServiceName())
185 .setServiceAEnd(new ServiceAEndBuilder(input.getServiceAEnd()).build())
186 .setServiceZEnd(new ServiceZEndBuilder(input.getServiceZEnd()).build())
187 .setCommonId(input.getCommonId())
188 .setConnectionType(input.getConnectionType());
190 nbiNotificationBuilder.setServiceName(notification.getServiceName());
192 nbiNotificationBuilder.setPublisherName(PUBLISHER);
193 return nbiNotificationBuilder.build();
197 * Process cancel resource result.
199 private void onCancelResourceResult() {
200 if (servicePathRpcResult.getStatus() == RpcStatusEx.Pending) {
201 LOG.warn("PCE cancel returned a Pending RpcStatusEx code !");
203 } else if (servicePathRpcResult.getStatus() != RpcStatusEx.Successful
204 && servicePathRpcResult.getStatus() != RpcStatusEx.Failed) {
205 LOG.error("PCE cancel returned an unknown RpcStatusEx code !");
208 Services service = serviceDataStoreOperations.getService(input.getServiceName()).get();
209 PublishNotificationProcessServiceBuilder nbiNotificationBuilder = new PublishNotificationProcessServiceBuilder()
210 .setServiceName(service.getServiceName())
211 .setServiceAEnd(new ServiceAEndBuilder(service.getServiceAEnd()).build())
212 .setServiceZEnd(new ServiceZEndBuilder(service.getServiceZEnd()).build())
213 .setCommonId(service.getCommonId())
214 .setConnectionType(service.getConnectionType())
215 .setPublisherName(PUBLISHER);
216 if (servicePathRpcResult.getStatus() == RpcStatusEx.Failed) {
217 LOG.info("PCE cancel resource failed !");
218 sendNbiNotification(nbiNotificationBuilder
219 .setResponseFailed("PCE cancel resource failed !")
220 .setMessage("ServiceDelete request failed ...")
221 .setOperationalState(service.getOperationalState())
225 LOG.info("PCE cancel resource done OK !");
226 OperationResult deleteServicePathOperationResult =
227 this.serviceDataStoreOperations.deleteServicePath(input.getServiceName());
228 if (!deleteServicePathOperationResult.isSuccess()) {
229 LOG.warn("Service path was not removed from datastore !");
231 OperationResult deleteServiceOperationResult;
232 String serviceType = "";
234 deleteServiceOperationResult = this.serviceDataStoreOperations.deleteTempService(input.getServiceName());
235 serviceType = "Temp ";
237 deleteServiceOperationResult = this.serviceDataStoreOperations.deleteService(input.getServiceName());
239 if (deleteServiceOperationResult.isSuccess()) {
240 sendNbiNotification(nbiNotificationBuilder
241 .setResponseFailed("")
242 .setMessage("Service deleted !")
243 .setOperationalState(State.Degraded)
246 LOG.warn("{}Service was not removed from datastore !", serviceType);
247 sendNbiNotification(nbiNotificationBuilder
248 .setResponseFailed(serviceType + "Service was not removed from datastore !")
249 .setMessage("ServiceDelete request failed ...")
250 .setOperationalState(service.getOperationalState())
254 * if it was an RPC serviceReconfigure, re-launch PCR.
256 if (this.serviceReconfigure) {
257 LOG.info("cancel resource reserve done, relaunching PCE path computation ...");
258 this.pceServiceWrapper.performPCE(input.getServiceCreateInput(), true);
259 this.serviceReconfigure = false;
264 value = "ES_COMPARING_STRINGS_WITH_EQ",
265 justification = "false positives, not strings but real object references comparisons")
266 private Boolean compareServicePathRpcResult(ServicePathRpcResult notification) {
267 if (servicePathRpcResult == null) {
270 if (servicePathRpcResult.getNotificationType() != notification.getNotificationType()) {
273 if (servicePathRpcResult.getServiceName() != notification.getServiceName()) {
276 if (servicePathRpcResult.getStatus() != notification.getStatus()) {
279 if (servicePathRpcResult.getStatusMessage() != notification.getStatusMessage()) {
285 public void setInput(ServiceInput serviceInput) {
286 this.input = serviceInput;
289 public void setServiceReconfigure(Boolean serv) {
290 this.serviceReconfigure = serv;
293 public void setserviceDataStoreOperations(ServiceDataStoreOperations serviceData) {
294 this.serviceDataStoreOperations = serviceData;
297 public void setTempService(Boolean tempService) {
298 this.tempService = tempService;
301 public void setServiceFeasiblity(Boolean serviceFeasiblity) {
302 this.serviceFeasiblity = serviceFeasiblity;
306 * Send notification to NBI notification in order to publish message.
307 * @param service PublishNotificationService
309 private void sendNbiNotification(PublishNotificationProcessService service) {
311 notificationPublishService.putNotification(service);
312 } catch (InterruptedException e) {
313 LOG.warn("Cannot send notification to nbi", e);
314 Thread.currentThread().interrupt();