Minimal refactor of renderer to prepare flexgrid
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / listeners / RendererListenerImpl.java
1 /*
2  * Copyright © 2017 Orange, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.transportpce.servicehandler.listeners;
9
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.rev181130.State;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev181130.AdminStates;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Calls to listen to Renderer notifications.
29  *
30  * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
31  *
32  */
33 public class RendererListenerImpl implements TransportpceRendererListener {
34
35     private static final Logger LOG = LoggerFactory.getLogger(RendererListenerImpl.class);
36     private RendererRpcResultSp serviceRpcResultSp;
37     private ServiceDataStoreOperations serviceDataStoreOperations;
38     private ServiceInput input;
39     private PCEServiceWrapper pceServiceWrapper;
40     private Boolean tempService;
41     private NotificationPublishService notificationPublishService;
42
43     public RendererListenerImpl(PathComputationService pathComputationService,
44             NotificationPublishService notificationPublishService) {
45         this.pceServiceWrapper = new PCEServiceWrapper(pathComputationService, notificationPublishService);
46         setServiceInput(null);
47         setTempService(false);
48         this.notificationPublishService = notificationPublishService;
49     }
50
51     @Override
52     public void onRendererRpcResultSp(RendererRpcResultSp notification) {
53         if (compareServiceRpcResultSp(notification)) {
54             LOG.warn("ServiceRpcResultSp already wired !");
55             return;
56         }
57         serviceRpcResultSp = notification;
58         int notifType = serviceRpcResultSp.getNotificationType().getIntValue();
59         LOG.info("Renderer '{}' Notification received : {}", serviceRpcResultSp.getNotificationType().getName(),
60                 notification);
61         switch (notifType) {
62             /* service-implementation-request. */
63             case 3 :
64                 onServiceImplementationResult(notification);
65                 break;
66             /* service-delete. */
67             case 4 :
68                 onServiceDeleteResult(notification);
69                 break;
70             default:
71                 break;
72         }
73     }
74
75     /**
76      * Process service delete result for serviceName.
77      * @param notification RendererRpcResultSp
78      */
79     private void onServiceDeleteResult(RendererRpcResultSp notification) {
80         switch (serviceRpcResultSp.getStatus()) {
81             case Successful:
82                 LOG.info("Service '{}' deleted !", notification.getServiceName());
83                 break;
84             case Failed:
85                 LOG.error("Renderer service delete failed !");
86                 return;
87             case  Pending:
88                 LOG.warn("Renderer service delete returned a Penging RpcStatusEx code!");
89                 return;
90             default:
91                 LOG.error("Renderer service delete returned an unknown RpcStatusEx code!");
92                 return;
93         }
94         if (this.input == null) {
95             LOG.error("ServiceInput parameter is null !");
96             return;
97         }
98         LOG.info("sending PCE cancel resource reserve for '{}'",  this.input.getServiceName());
99         this.pceServiceWrapper.cancelPCEResource(this.input.getServiceName(),
100                 ServiceNotificationTypes.ServiceDeleteResult);
101         sendServiceHandlerNotification(notification, ServiceNotificationTypes.ServiceDeleteResult);
102     }
103
104     /**
105      * Process service implementation result for serviceName.
106      * @param notification RendererRpcResultSp
107      */
108     private void onServiceImplementationResult(RendererRpcResultSp notification) {
109         switch (serviceRpcResultSp.getStatus()) {
110             case Successful:
111                 onSuccededServiceImplementation(notification);
112                 break;
113             case Failed:
114                 onFailedServiceImplementation(notification.getServiceName());
115                 break;
116             case  Pending:
117                 LOG.warn("Service Implementation still pending according to RpcStatusEx");
118                 break;
119             default:
120                 LOG.warn("Service Implementation has an unknown RpcStatusEx code");
121                 break;
122         }
123     }
124
125     /**
126      * Process succeeded service implementation for service.
127      * @param notification RendererRpcResultSp
128      */
129     private void onSuccededServiceImplementation(RendererRpcResultSp notification) {
130         LOG.info("Service implemented !");
131         if (serviceDataStoreOperations == null) {
132             LOG.debug("serviceDataStoreOperations is null");
133             return;
134         }
135         OperationResult operationResult = null;
136         if (tempService) {
137             operationResult = this.serviceDataStoreOperations.modifyTempService(
138                     serviceRpcResultSp.getServiceName(), State.InService, AdminStates.InService);
139             if (!operationResult.isSuccess()) {
140                 LOG.warn("Temp Service status not updated in datastore !");
141             }
142         } else {
143             operationResult = this.serviceDataStoreOperations.modifyService(
144                     serviceRpcResultSp.getServiceName(),
145                     State.InService,
146                     AdminStates.InService);
147             if (!operationResult.isSuccess()) {
148                 LOG.warn("Service status not updated in datastore !");
149             } else {
150                 sendServiceHandlerNotification(notification, ServiceNotificationTypes.ServiceCreateResult);
151             }
152         }
153     }
154
155     /**
156      * Create and send service handler notification.
157      * @param notification RendererRpcResultSp
158      * @param type ServiceNotificationTypes
159      */
160     private void sendServiceHandlerNotification(RendererRpcResultSp notification, ServiceNotificationTypes type) {
161         try {
162             ServiceRpcResultSh serviceHandlerNotification = new ServiceRpcResultShBuilder()
163                     .setAToZDirection(notification.getAToZDirection())
164                     .setZToADirection(notification.getZToADirection())
165                     .setServiceName(notification.getServiceName())
166                     .setStatus(notification.getStatus())
167                     .setStatusMessage(notification.getStatusMessage())
168                     .setNotificationType(type)
169                     .build();
170             LOG.debug("Service update in datastore OK, sending notification {}", serviceHandlerNotification);
171             notificationPublishService.putNotification(
172                     serviceHandlerNotification);
173         } catch (InterruptedException e) {
174             LOG.warn("Something went wrong while sending notification for sevice {}",
175                     serviceRpcResultSp.getServiceName(), e);
176             Thread.currentThread().interrupt();
177         }
178     }
179
180     /**
181      * Process failed service implementation for serviceName.
182      * @param serviceName String
183      */
184     private void onFailedServiceImplementation(String serviceName) {
185         LOG.error("Renderer implementation failed !");
186         OperationResult deleteServicePathOperationResult =
187                 this.serviceDataStoreOperations.deleteServicePath(serviceName);
188         if (!deleteServicePathOperationResult.isSuccess()) {
189             LOG.warn("Service path was not removed from datastore!");
190         }
191         if (tempService) {
192             OperationResult deleteServiceOperationResult =
193                     this.serviceDataStoreOperations.deleteTempService(serviceName);
194             if (!deleteServiceOperationResult.isSuccess()) {
195                 LOG.warn("Temp Service was not removed from datastore!");
196             }
197         } else {
198             OperationResult deleteServiceOperationResult =
199                     this.serviceDataStoreOperations.deleteService(serviceName);
200             if (!deleteServiceOperationResult.isSuccess()) {
201                 LOG.warn("Service was not removed from datastore!");
202             }
203         }
204     }
205
206     @SuppressFBWarnings(
207         value = "ES_COMPARING_STRINGS_WITH_EQ",
208         justification = "false positives, not strings but real object references comparisons")
209     private Boolean compareServiceRpcResultSp(RendererRpcResultSp notification) {
210         if (serviceRpcResultSp == null) {
211             return false;
212         }
213         if (serviceRpcResultSp.getNotificationType() != notification.getNotificationType()) {
214             return false;
215         }
216         if (serviceRpcResultSp.getServiceName() != notification.getServiceName()) {
217             return false;
218         }
219         if (serviceRpcResultSp.getStatus() != notification.getStatus()) {
220             return false;
221         }
222         if (serviceRpcResultSp.getStatusMessage() != notification.getStatusMessage()) {
223             return false;
224         }
225         return true;
226     }
227
228     public void setServiceInput(ServiceInput serviceInput) {
229         this.input = serviceInput;
230     }
231
232     public void setserviceDataStoreOperations(ServiceDataStoreOperations serviceData) {
233         this.serviceDataStoreOperations = serviceData;
234     }
235
236     public void setTempService(Boolean tempService) {
237         this.tempService = tempService;
238     }
239 }