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.servicehandler.ServiceInput;
15 import org.opendaylight.transportpce.servicehandler.service.PCEServiceWrapper;
16 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperations;
17 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev201125.RendererRpcResultSp;
18 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev201125.TransportpceRendererListener;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.servicehandler.rev201125.ServiceRpcResultSh;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.servicehandler.rev201125.ServiceRpcResultShBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.ServiceNotificationTypes;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev191129.State;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev191129.AdminStates;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.service.list.Services;
25 import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.PublishNotificationService;
26 import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.PublishNotificationServiceBuilder;
27 import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.notification.service.ServiceAEndBuilder;
28 import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.notification.service.ServiceZEndBuilder;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Calls to listen to Renderer notifications.
35 * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
38 public class RendererListenerImpl implements TransportpceRendererListener {
40 private static final String TOPIC = "RendererListener";
41 private static final Logger LOG = LoggerFactory.getLogger(RendererListenerImpl.class);
42 private RendererRpcResultSp serviceRpcResultSp;
43 private ServiceDataStoreOperations serviceDataStoreOperations;
44 private ServiceInput input;
45 private PCEServiceWrapper pceServiceWrapper;
46 private Boolean tempService;
47 private NotificationPublishService notificationPublishService;
49 public RendererListenerImpl(PathComputationService pathComputationService,
50 NotificationPublishService notificationPublishService) {
51 this.pceServiceWrapper = new PCEServiceWrapper(pathComputationService, notificationPublishService);
52 setServiceInput(null);
53 setTempService(false);
54 this.notificationPublishService = notificationPublishService;
58 public void onRendererRpcResultSp(RendererRpcResultSp notification) {
59 if (compareServiceRpcResultSp(notification)) {
60 LOG.warn("ServiceRpcResultSp already wired !");
63 serviceRpcResultSp = notification;
64 int notifType = serviceRpcResultSp.getNotificationType().getIntValue();
65 LOG.info("Renderer '{}' Notification received : {}", serviceRpcResultSp.getNotificationType().getName(),
68 /* service-implementation-request. */
70 onServiceImplementationResult(notification);
74 onServiceDeleteResult(notification);
82 * Process service delete result for serviceName.
83 * @param notification RendererRpcResultSp
85 private void onServiceDeleteResult(RendererRpcResultSp notification) {
86 switch (serviceRpcResultSp.getStatus()) {
88 LOG.info("Service '{}' deleted !", notification.getServiceName());
91 LOG.error("Renderer service delete failed !");
94 LOG.warn("Renderer service delete returned a Penging RpcStatusEx code!");
97 LOG.error("Renderer service delete returned an unknown RpcStatusEx code!");
100 Services service = serviceDataStoreOperations.getService(notification.getServiceName()).get();
101 PublishNotificationService nbiNotification = new PublishNotificationServiceBuilder()
102 .setServiceName(service.getServiceName())
103 .setServiceAEnd(new ServiceAEndBuilder(service.getServiceAEnd()).build())
104 .setServiceZEnd(new ServiceZEndBuilder(service.getServiceZEnd()).build())
105 .setCommonId(service.getCommonId())
106 .setConnectionType(service.getConnectionType())
107 .setResponseFailed("")
108 .setMessage("Service deleted !")
109 .setOperationalState(org.opendaylight.yang.gen.v1.http
110 .org.openroadm.common.state.types.rev181130.State.Degraded)
113 sendNbiNotification(nbiNotification);
114 LOG.info("Service '{}' deleted !", notification.getServiceName());
115 if (this.input == null) {
116 LOG.error("ServiceInput parameter is null !");
119 LOG.info("sending PCE cancel resource reserve for '{}'", this.input.getServiceName());
120 this.pceServiceWrapper.cancelPCEResource(this.input.getServiceName(),
121 ServiceNotificationTypes.ServiceDeleteResult);
122 sendServiceHandlerNotification(notification, ServiceNotificationTypes.ServiceDeleteResult);
126 * Process service implementation result for serviceName.
127 * @param notification RendererRpcResultSp
129 private void onServiceImplementationResult(RendererRpcResultSp notification) {
130 switch (serviceRpcResultSp.getStatus()) {
132 onSuccededServiceImplementation(notification);
135 onFailedServiceImplementation(notification.getServiceName());
138 LOG.warn("Service Implementation still pending according to RpcStatusEx");
141 LOG.warn("Service Implementation has an unknown RpcStatusEx code");
147 * Process succeeded service implementation for service.
148 * @param notification RendererRpcResultSp
150 private void onSuccededServiceImplementation(RendererRpcResultSp notification) {
151 LOG.info("Service implemented !");
152 PublishNotificationService nbiNotification = new PublishNotificationServiceBuilder()
153 .setServiceName(input.getServiceName())
154 .setServiceAEnd(new ServiceAEndBuilder(input.getServiceAEnd()).build())
155 .setServiceZEnd(new ServiceZEndBuilder(input.getServiceZEnd()).build())
156 .setCommonId(input.getCommonId()).setConnectionType(input.getConnectionType())
157 .setResponseFailed("")
158 .setMessage("Service implemented !")
159 .setOperationalState(org.opendaylight.yang.gen.v1.http
160 .org.openroadm.common.state.types.rev181130.State.InService)
163 sendNbiNotification(nbiNotification);
164 if (serviceDataStoreOperations == null) {
165 LOG.debug("serviceDataStoreOperations is null");
168 OperationResult operationResult = null;
170 operationResult = this.serviceDataStoreOperations.modifyTempService(
171 serviceRpcResultSp.getServiceName(), State.InService, AdminStates.InService);
172 if (!operationResult.isSuccess()) {
173 LOG.warn("Temp Service status not updated in datastore !");
176 operationResult = this.serviceDataStoreOperations.modifyService(
177 serviceRpcResultSp.getServiceName(), State.InService, AdminStates.InService);
178 if (!operationResult.isSuccess()) {
179 LOG.warn("Service status not updated in datastore !");
181 sendServiceHandlerNotification(notification, ServiceNotificationTypes.ServiceCreateResult);
187 * Create and send service handler notification.
188 * @param notification RendererRpcResultSp
189 * @param type ServiceNotificationTypes
191 private void sendServiceHandlerNotification(RendererRpcResultSp notification, ServiceNotificationTypes type) {
193 ServiceRpcResultSh serviceHandlerNotification = new ServiceRpcResultShBuilder()
194 .setAToZDirection(notification.getAToZDirection())
195 .setZToADirection(notification.getZToADirection())
196 .setServiceName(notification.getServiceName())
197 .setStatus(notification.getStatus())
198 .setStatusMessage(notification.getStatusMessage())
199 .setNotificationType(type)
201 LOG.debug("Service update in datastore OK, sending notification {}", serviceHandlerNotification);
202 notificationPublishService.putNotification(
203 serviceHandlerNotification);
204 } catch (InterruptedException e) {
205 LOG.warn("Something went wrong while sending notification for sevice {}",
206 serviceRpcResultSp.getServiceName(), e);
207 Thread.currentThread().interrupt();
212 * Process failed service implementation for serviceName.
213 * @param serviceName String
215 private void onFailedServiceImplementation(String serviceName) {
216 LOG.error("Renderer implementation failed !");
217 OperationResult deleteServicePathOperationResult =
218 this.serviceDataStoreOperations.deleteServicePath(serviceName);
219 if (!deleteServicePathOperationResult.isSuccess()) {
220 LOG.warn("Service path was not removed from datastore!");
223 OperationResult deleteServiceOperationResult =
224 this.serviceDataStoreOperations.deleteTempService(serviceName);
225 if (!deleteServiceOperationResult.isSuccess()) {
226 LOG.warn("Temp Service was not removed from datastore!");
229 OperationResult deleteServiceOperationResult =
230 this.serviceDataStoreOperations.deleteService(serviceName);
231 if (!deleteServiceOperationResult.isSuccess()) {
232 LOG.warn("Service was not removed from datastore!");
238 value = "ES_COMPARING_STRINGS_WITH_EQ",
239 justification = "false positives, not strings but real object references comparisons")
240 private Boolean compareServiceRpcResultSp(RendererRpcResultSp notification) {
241 if (serviceRpcResultSp == null) {
244 if (serviceRpcResultSp.getNotificationType() != notification.getNotificationType()) {
247 if (serviceRpcResultSp.getServiceName() != notification.getServiceName()) {
250 if (serviceRpcResultSp.getStatus() != notification.getStatus()) {
253 if (serviceRpcResultSp.getStatusMessage() != notification.getStatusMessage()) {
259 public void setServiceInput(ServiceInput serviceInput) {
260 this.input = serviceInput;
263 public void setserviceDataStoreOperations(ServiceDataStoreOperations serviceData) {
264 this.serviceDataStoreOperations = serviceData;
267 public void setTempService(Boolean tempService) {
268 this.tempService = tempService;
272 * Send notification to NBI notification in order to publish message.
273 * @param service PublishNotificationService
275 private void sendNbiNotification(PublishNotificationService service) {
277 notificationPublishService.putNotification(service);
278 } catch (InterruptedException e) {
279 LOG.warn("Cannot send notification to nbi", e);
280 Thread.currentThread().interrupt();