Service Handler optimizations and technical debt
[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.rev200520.ServiceRpcResultSp;
18 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev200520.TransportpceRendererListener;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.ServiceNotificationTypes;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev181130.State;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev181130.AdminStates;
22 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.RpcStatusEx;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Calls to listen to Renderer notifications.
28  *
29  * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
30  *
31  */
32 public class RendererListenerImpl implements TransportpceRendererListener {
33
34     private static final Logger LOG = LoggerFactory.getLogger(RendererListenerImpl.class);
35     private ServiceRpcResultSp serviceRpcResultSp;
36     private ServiceDataStoreOperations serviceDataStoreOperations;
37     private ServiceInput input;
38     private PCEServiceWrapper pceServiceWrapper;
39     private Boolean tempService;
40
41     public RendererListenerImpl(PathComputationService pathComputationService,
42             NotificationPublishService notificationPublishService) {
43         this.pceServiceWrapper = new PCEServiceWrapper(pathComputationService, notificationPublishService);
44         setServiceInput(null);
45         setTempService(false);
46     }
47
48     @Override
49     public void onServiceRpcResultSp(ServiceRpcResultSp notification) {
50         if (compareServiceRpcResultSp(notification)) {
51             LOG.warn("ServiceRpcResultSp already wired !");
52             return;
53         }
54         serviceRpcResultSp = notification;
55         String serviceName = serviceRpcResultSp.getServiceName();
56         int notifType = serviceRpcResultSp.getNotificationType().getIntValue();
57         LOG.info("Renderer '{}' Notification received : {}", serviceRpcResultSp.getNotificationType().getName(),
58                 notification);
59         switch (notifType) {
60             /* service-implementation-request. */
61             case 3 :
62                 onServiceImplementationResult(serviceName);
63                 break;
64             /* service-delete. */
65             case 4 :
66                 onServiceDeleteResult(serviceName);
67                 break;
68             default:
69                 break;
70         }
71     }
72
73     /**
74      * Process service delete result for serviceName.
75      * @param serviceName String
76      */
77     private void onServiceDeleteResult(String serviceName) {
78         if (serviceRpcResultSp.getStatus() == RpcStatusEx.Failed) {
79             LOG.error("Renderer service delete failed !");
80             return;
81         } else if (serviceRpcResultSp.getStatus() == RpcStatusEx.Pending) {
82             LOG.warn("Renderer service delete returned a Penging RpcStatusEx code!");
83             return;
84         } else if (serviceRpcResultSp.getStatus() != RpcStatusEx.Successful) {
85             LOG.error("Renderer service delete returned an unknown RpcStatusEx code!");
86             return;
87         }
88         LOG.info("Service '{}' deleted !", serviceName);
89         if (this.input == null) {
90             LOG.error("ServiceInput parameter is null !");
91             return;
92         }
93         LOG.info("sending PCE cancel resource reserve for '{}'",  this.input.getServiceName());
94         this.pceServiceWrapper.cancelPCEResource(this.input.getServiceName(),
95                 ServiceNotificationTypes.ServiceDeleteResult);
96     }
97
98     /**
99      * Process service implementation result for serviceName.
100      * @param serviceName String
101      * @param serviceName String
102      */
103     private void onServiceImplementationResult(String serviceName) {
104         if (serviceRpcResultSp.getStatus() == RpcStatusEx.Successful) {
105             onSuccededServiceImplementation();
106         } else if (serviceRpcResultSp.getStatus() == RpcStatusEx.Failed) {
107             onFailedServiceImplementation(serviceName);
108         } else if (serviceRpcResultSp.getStatus() == RpcStatusEx.Pending) {
109             LOG.warn("Service Implementation still pending according to RpcStatusEx");
110         } else {
111             LOG.warn("Service Implementation has an unknown RpcStatusEx code");
112         }
113
114     }
115
116     /**
117      * Process succeeded service implementation for service.
118      */
119     private void onSuccededServiceImplementation() {
120         LOG.info("Service implemented !");
121         if (serviceDataStoreOperations == null) {
122             LOG.debug("serviceDataStoreOperations is null");
123             return;
124         }
125         OperationResult operationResult = null;
126         if (tempService) {
127             operationResult = this.serviceDataStoreOperations.modifyTempService(
128                     serviceRpcResultSp.getServiceName(), State.InService, AdminStates.InService);
129             if (!operationResult.isSuccess()) {
130                 LOG.warn("Temp Service status not updated in datastore !");
131             }
132         } else {
133             operationResult = this.serviceDataStoreOperations.modifyService(
134                     serviceRpcResultSp.getServiceName(),
135                     State.InService,
136                     AdminStates.InService);
137             if (!operationResult.isSuccess()) {
138                 LOG.warn("Service status not updated in datastore !");
139             }
140         }
141     }
142
143     /**
144      * Process failed service implementation for serviceName.
145      * @param serviceName String
146      */
147     private void onFailedServiceImplementation(String serviceName) {
148         LOG.error("Renderer implementation failed !");
149         OperationResult deleteServicePathOperationResult =
150                 this.serviceDataStoreOperations.deleteServicePath(serviceName);
151         if (!deleteServicePathOperationResult.isSuccess()) {
152             LOG.warn("Service path was not removed from datastore!");
153         }
154         if (tempService) {
155             OperationResult deleteServiceOperationResult =
156                     this.serviceDataStoreOperations.deleteTempService(serviceName);
157             if (!deleteServiceOperationResult.isSuccess()) {
158                 LOG.warn("Temp Service was not removed from datastore!");
159             }
160         } else {
161             OperationResult deleteServiceOperationResult =
162                     this.serviceDataStoreOperations.deleteService(serviceName);
163             if (!deleteServiceOperationResult.isSuccess()) {
164                 LOG.warn("Service was not removed from datastore!");
165             }
166         }
167     }
168
169     @SuppressFBWarnings(
170         value = "ES_COMPARING_STRINGS_WITH_EQ",
171         justification = "false positives, not strings but real object references comparisons")
172     private Boolean compareServiceRpcResultSp(ServiceRpcResultSp notification) {
173         if (serviceRpcResultSp == null) {
174             return false;
175         }
176         if (serviceRpcResultSp.getNotificationType() != notification.getNotificationType()) {
177             return false;
178         }
179         if (serviceRpcResultSp.getServiceName() != notification.getServiceName()) {
180             return false;
181         }
182         if (serviceRpcResultSp.getStatus() != notification.getStatus()) {
183             return false;
184         }
185         if (serviceRpcResultSp.getStatusMessage() != notification.getStatusMessage()) {
186             return false;
187         }
188         return true;
189     }
190
191     public void setServiceInput(ServiceInput serviceInput) {
192         this.input = serviceInput;
193     }
194
195     public void setserviceDataStoreOperations(ServiceDataStoreOperations serviceData) {
196         this.serviceDataStoreOperations = serviceData;
197     }
198
199     public void setTempService(Boolean tempService) {
200         this.tempService = tempService;
201     }
202 }