Technical debt - Service handler Sonar issues
[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             serviceRpcResultSp = notification;
52             String serviceName = serviceRpcResultSp.getServiceName();
53             int notifType = serviceRpcResultSp.getNotificationType().getIntValue();
54             LOG.info("Renderer '{}' Notification received : {}", serviceRpcResultSp.getNotificationType().getName(),
55                     notification);
56             switch (notifType) {
57                 /* service-implementation-request. */
58                 case 3 :
59                     onServiceImplementationResult(serviceName);
60                     break;
61                 /* service-delete. */
62                 case 4 :
63                     onServiceDeleteResult(serviceName);
64                     break;
65                 default:
66                     break;
67             }
68         } else {
69             LOG.warn("ServiceRpcResultSp already wired !");
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.Successful) {
79             LOG.info("Service '{}' deleted !", serviceName);
80             if (this.input != null) {
81                 LOG.info("sending PCE cancel resource reserve for '{}'",  this.input.getServiceName());
82                 this.pceServiceWrapper.cancelPCEResource(this.input.getServiceName(),
83                         ServiceNotificationTypes.ServiceDeleteResult);
84             } else {
85                 LOG.error("ServiceInput parameter is null !");
86             }
87         } else if (serviceRpcResultSp.getStatus() == RpcStatusEx.Failed) {
88             LOG.error("Renderer service delete failed !");
89         }
90     }
91
92     /**
93      * Process service implementation result for serviceName.
94      * @param serviceName String
95      * @param serviceName String
96      */
97     private void onServiceImplementationResult(String serviceName) {
98         if (serviceRpcResultSp.getStatus() == RpcStatusEx.Successful) {
99             onSuccededServiceImplementation();
100         } else if (serviceRpcResultSp.getStatus() == RpcStatusEx.Failed) {
101             onFailedServiceImplementation(serviceName);
102         }
103     }
104
105     /**
106      * Process succeeded service implementation for service.
107      */
108     private void onSuccededServiceImplementation() {
109         LOG.info("Service implemented !");
110         if (serviceDataStoreOperations != null) {
111             OperationResult operationResult = null;
112             if (tempService) {
113                 operationResult = this.serviceDataStoreOperations.modifyTempService(
114                         serviceRpcResultSp.getServiceName(), State.InService, AdminStates.InService);
115                 if (!operationResult.isSuccess()) {
116                     LOG.warn("Temp Service status not updated in datastore !");
117                 }
118             } else {
119                 operationResult = this.serviceDataStoreOperations
120                         .modifyService(serviceRpcResultSp.getServiceName(),
121                                 State.InService, AdminStates.InService);
122                 if (!operationResult.isSuccess()) {
123                     LOG.warn("Service status not updated in datastore !");
124                 }
125             }
126         }
127     }
128
129     /**
130      * Process failed service implementation for serviceName.
131      * @param serviceName String
132      */
133     private void onFailedServiceImplementation(String serviceName) {
134         LOG.error("Renderer implementation failed !");
135         OperationResult deleteServicePathOperationResult =
136                 this.serviceDataStoreOperations.deleteServicePath(serviceName);
137         if (!deleteServicePathOperationResult.isSuccess()) {
138             LOG.warn("Service path was not removed from datastore!");
139         }
140         if (tempService) {
141             OperationResult deleteServiceOperationResult =
142                     this.serviceDataStoreOperations.deleteTempService(serviceName);
143             if (!deleteServiceOperationResult.isSuccess()) {
144                 LOG.warn("Temp Service was not removed from datastore!");
145             }
146         } else {
147             OperationResult deleteServiceOperationResult =
148                     this.serviceDataStoreOperations.deleteService(serviceName);
149             if (!deleteServiceOperationResult.isSuccess()) {
150                 LOG.warn("Service was not removed from datastore!");
151             }
152         }
153     }
154
155     @SuppressFBWarnings(
156         value = "ES_COMPARING_STRINGS_WITH_EQ",
157         justification = "false positives, not strings but real object references comparisons")
158     private Boolean compareServiceRpcResultSp(ServiceRpcResultSp notification) {
159         Boolean result = true;
160         if (serviceRpcResultSp == null) {
161             result = false;
162         } else {
163             if (serviceRpcResultSp.getNotificationType() != notification.getNotificationType()) {
164                 result = false;
165             }
166             if (serviceRpcResultSp.getServiceName() != notification.getServiceName()) {
167                 result = false;
168             }
169             if (serviceRpcResultSp.getStatus() != notification.getStatus()) {
170                 result = false;
171             }
172             if (serviceRpcResultSp.getStatusMessage() != notification.getStatusMessage()) {
173                 result = false;
174             }
175         }
176         return result;
177     }
178
179     public void setServiceInput(ServiceInput serviceInput) {
180         this.input = serviceInput;
181     }
182
183     public void setserviceDataStoreOperations(ServiceDataStoreOperations serviceData) {
184         this.serviceDataStoreOperations = serviceData;
185     }
186
187     public void setTempService(Boolean tempService) {
188         this.tempService = tempService;
189     }
190 }