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.rev210701.PathComputationRequestOutput;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev210701.PathComputationRequestOutputBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev210701.ServicePathRpcResult;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev210701.TransportpcePceListener;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev210701.service.path.rpc.result.PathDescription;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev210701.service.path.rpc.result.PathDescriptionBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210618.ServiceImplementationRequestInput;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev181130.State;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.service.list.Services;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.RpcStatusEx;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.response.parameters.sp.ResponseParameters;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.response.parameters.sp.ResponseParametersBuilder;
31 import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.PublishNotificationService;
32 import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.PublishNotificationServiceBuilder;
33 import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.notification.service.ServiceAEndBuilder;
34 import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.notification.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 TOPIC = "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.rev200128
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 PublishNotificationService nbiNotification = getPublishNotificationService(notification);
151 PublishNotificationServiceBuilder publishNotificationServiceBuilder = new PublishNotificationServiceBuilder(
153 switch (servicePathRpcResult.getStatus()) {
155 LOG.error("PCE path computation failed !");
156 nbiNotification = publishNotificationServiceBuilder.setMessage("ServiceCreate request failed ...")
157 .setResponseFailed("PCE path computation failed !")
158 .setOperationalState(State.Degraded).build();
159 sendNbiNotification(nbiNotification);
162 LOG.warn("PCE path computation returned a Pending RpcStatusEx code!");
165 LOG.info("PCE calculation done OK !");
168 LOG.error("PCE path computation returned an unknown RpcStatusEx code {}",
169 servicePathRpcResult.getStatus());
170 nbiNotification = publishNotificationServiceBuilder.setMessage("ServiceCreate request failed ...")
171 .setResponseFailed("PCE path computation returned an unknown RpcStatusEx code!")
172 .setOperationalState(State.Degraded).build();
173 sendNbiNotification(nbiNotification);
178 private PublishNotificationService getPublishNotificationService(ServicePathRpcResult notification) {
179 PublishNotificationServiceBuilder nbiNotificationBuilder = new PublishNotificationServiceBuilder();
181 nbiNotificationBuilder.setServiceName(input.getServiceName())
182 .setServiceAEnd(new ServiceAEndBuilder(input.getServiceAEnd()).build())
183 .setServiceZEnd(new ServiceZEndBuilder(input.getServiceZEnd()).build())
184 .setCommonId(input.getCommonId()).setConnectionType(input.getConnectionType());
186 nbiNotificationBuilder.setServiceName(notification.getServiceName());
188 nbiNotificationBuilder.setTopic(TOPIC);
189 return nbiNotificationBuilder.build();
193 * Process cancel resource result.
195 private void onCancelResourceResult() {
196 if (servicePathRpcResult.getStatus() == RpcStatusEx.Pending) {
197 LOG.warn("PCE cancel returned a Pending RpcStatusEx code !");
199 } else if (servicePathRpcResult.getStatus() != RpcStatusEx.Successful
200 && servicePathRpcResult.getStatus() != RpcStatusEx.Failed) {
201 LOG.error("PCE cancel returned an unknown RpcStatusEx code !");
204 Services service = serviceDataStoreOperations.getService(input.getServiceName()).get();
205 PublishNotificationServiceBuilder nbiNotificationBuilder = new PublishNotificationServiceBuilder()
206 .setServiceName(service.getServiceName())
207 .setServiceAEnd(new ServiceAEndBuilder(service.getServiceAEnd()).build())
208 .setServiceZEnd(new ServiceZEndBuilder(service.getServiceZEnd()).build())
209 .setCommonId(service.getCommonId())
210 .setConnectionType(service.getConnectionType())
212 if (servicePathRpcResult.getStatus() == RpcStatusEx.Failed) {
213 LOG.info("PCE cancel resource failed !");
214 sendNbiNotification(nbiNotificationBuilder
215 .setResponseFailed("PCE cancel resource failed !")
216 .setMessage("ServiceDelete request failed ...")
217 .setOperationalState(service.getOperationalState())
221 LOG.info("PCE cancel resource done OK !");
222 OperationResult deleteServicePathOperationResult =
223 this.serviceDataStoreOperations.deleteServicePath(input.getServiceName());
224 if (!deleteServicePathOperationResult.isSuccess()) {
225 LOG.warn("Service path was not removed from datastore !");
227 OperationResult deleteServiceOperationResult;
228 String serviceType = "";
230 deleteServiceOperationResult = this.serviceDataStoreOperations.deleteTempService(input.getServiceName());
231 serviceType = "Temp ";
233 deleteServiceOperationResult = this.serviceDataStoreOperations.deleteService(input.getServiceName());
235 if (deleteServiceOperationResult.isSuccess()) {
236 sendNbiNotification(nbiNotificationBuilder
237 .setResponseFailed("")
238 .setMessage("Service deleted !")
239 .setOperationalState(State.Degraded)
242 LOG.warn("{}Service was not removed from datastore !", serviceType);
243 sendNbiNotification(nbiNotificationBuilder
244 .setResponseFailed(serviceType + "Service was not removed from datastore !")
245 .setMessage("ServiceDelete request failed ...")
246 .setOperationalState(service.getOperationalState())
250 * if it was an RPC serviceReconfigure, re-launch PCR.
252 if (this.serviceReconfigure) {
253 LOG.info("cancel resource reserve done, relaunching PCE path computation ...");
254 this.pceServiceWrapper.performPCE(input.getServiceCreateInput(), true);
255 this.serviceReconfigure = false;
260 value = "ES_COMPARING_STRINGS_WITH_EQ",
261 justification = "false positives, not strings but real object references comparisons")
262 private Boolean compareServicePathRpcResult(ServicePathRpcResult notification) {
263 if (servicePathRpcResult == null) {
266 if (servicePathRpcResult.getNotificationType() != notification.getNotificationType()) {
269 if (servicePathRpcResult.getServiceName() != notification.getServiceName()) {
272 if (servicePathRpcResult.getStatus() != notification.getStatus()) {
275 if (servicePathRpcResult.getStatusMessage() != notification.getStatusMessage()) {
281 public void setInput(ServiceInput serviceInput) {
282 this.input = serviceInput;
285 public void setServiceReconfigure(Boolean serv) {
286 this.serviceReconfigure = serv;
289 public void setserviceDataStoreOperations(ServiceDataStoreOperations serviceData) {
290 this.serviceDataStoreOperations = serviceData;
293 public void setTempService(Boolean tempService) {
294 this.tempService = tempService;
297 public void setServiceFeasiblity(Boolean serviceFeasiblity) {
298 this.serviceFeasiblity = serviceFeasiblity;
302 * Send notification to NBI notification in order to publish message.
303 * @param service PublishNotificationService
305 private void sendNbiNotification(PublishNotificationService service) {
307 notificationPublishService.putNotification(service);
308 } catch (InterruptedException e) {
309 LOG.warn("Cannot send notification to nbi", e);
310 Thread.currentThread().interrupt();