Technical debt - Service handler Sonar issues
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / listeners / PceListenerImpl.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.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.rev200128.PathComputationRequestOutput;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestOutputBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.ServicePathRpcResult;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.TransportpcePceListener;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.service.path.rpc.result.PathDescription;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.service.path.rpc.result.PathDescriptionBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev200520.ServiceImplementationRequestInput;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.RpcStatusEx;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.response.parameters.sp.ResponseParameters;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.response.parameters.sp.ResponseParametersBuilder;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class PceListenerImpl implements TransportpcePceListener {
33
34     private static final Logger LOG = LoggerFactory.getLogger(PceListenerImpl.class);
35
36     private ServicePathRpcResult servicePathRpcResult;
37     private RendererServiceOperations rendererServiceOperations;
38     private ServiceDataStoreOperations serviceDataStoreOperations;
39     private PCEServiceWrapper pceServiceWrapper;
40     private ServiceInput input;
41     private Boolean serviceReconfigure;
42     private Boolean tempService;
43     private Boolean serviceFeasiblity;
44
45     public PceListenerImpl(RendererServiceOperations rendererServiceOperations,
46             PathComputationService pathComputationService, NotificationPublishService notificationPublishService,
47             ServiceDataStoreOperations serviceDataStoreOperations) {
48         this.rendererServiceOperations = rendererServiceOperations;
49         this.pceServiceWrapper = new PCEServiceWrapper(pathComputationService, notificationPublishService);
50         this.serviceDataStoreOperations = serviceDataStoreOperations;
51         setServiceReconfigure(false);
52         setInput(null);
53         setTempService(false);
54         setServiceFeasiblity(false);
55     }
56
57     @Override
58     public void onServicePathRpcResult(ServicePathRpcResult notification) {
59         if (!compareServicePathRpcResult(notification)) {
60             servicePathRpcResult = notification;
61             switch (servicePathRpcResult.getNotificationType().getIntValue()) {
62                 /* path-computation-request. */
63                 case 1:
64                     onPathComputationResult(notification);
65                     break;
66                 /* cancel-resource-reserve. */
67                 case 2:
68                     onCancelResourceResult();
69                     break;
70                 default:
71                     break;
72             }
73         } else {
74             LOG.warn("ServicePathRpcResult already wired !");
75         }
76     }
77
78     /**
79      * Process path computation request result.
80      * @param notification the result notification.
81      */
82     private void onPathComputationResult(ServicePathRpcResult notification) {
83         LOG.info("PCE '{}' Notification received : {}",servicePathRpcResult.getNotificationType().getName(),
84                 notification);
85         if (servicePathRpcResult.getStatus() == RpcStatusEx.Successful) {
86             LOG.info("PCE calculation done OK !");
87             if (servicePathRpcResult.getPathDescription() != null) {
88                 PathDescription pathDescription = new PathDescriptionBuilder()
89                         .setAToZDirection(servicePathRpcResult.getPathDescription().getAToZDirection())
90                     .setZToADirection(servicePathRpcResult.getPathDescription().getZToADirection()).build();
91                 LOG.info("PathDescription gets : {}", pathDescription);
92                 if (!serviceFeasiblity) {
93                     if (input == null) {
94                         LOG.error("Input is null !");
95                     } else {
96                         OperationResult operationResult = null;
97                         if (tempService) {
98                             operationResult = this.serviceDataStoreOperations
99                                     .createTempService(input.getTempServiceCreateInput());
100                             if (!operationResult.isSuccess()) {
101                                 LOG.error("Temp Service not created in datastore !");
102                             }
103                         } else {
104                             operationResult = this.serviceDataStoreOperations
105                                     .createService(input.getServiceCreateInput());
106                             if (!operationResult.isSuccess()) {
107                                 LOG.error("Service not created in datastore !");
108                             }
109                         }
110                         ResponseParameters responseParameters = new ResponseParametersBuilder()
111                                 .setPathDescription(new org.opendaylight.yang.gen.v1.http.org
112                                         .transportpce.b.c._interface.service.types.rev200128
113                                         .response.parameters.sp.response.parameters
114                                         .PathDescriptionBuilder(pathDescription).build())
115                                 .build();
116                         PathComputationRequestOutput pceResponse = new PathComputationRequestOutputBuilder()
117                                 .setResponseParameters(responseParameters).build();
118                         OperationResult operationServicePathSaveResult = this.serviceDataStoreOperations
119                                 .createServicePath(input, pceResponse);
120                         if (!operationServicePathSaveResult.isSuccess()) {
121                             LOG.error("Service Path not created in datastore !");
122                         }
123                         ServiceImplementationRequestInput serviceImplementationRequest = ModelMappingUtils
124                                 .createServiceImplementationRequest(input, pathDescription);
125                         LOG.info("Sending serviceImplementation request : {}", serviceImplementationRequest);
126                         this.rendererServiceOperations.serviceImplementation(serviceImplementationRequest);
127                     }
128                 } else {
129                     LOG.warn("service-feasibility-check RPC ");
130                 }
131             } else {
132                 LOG.error("'PathDescription' parameter is null ");
133             }
134         } else if (servicePathRpcResult.getStatus() == RpcStatusEx.Failed) {
135             LOG.error("PCE path computation failed !");
136         }
137     }
138
139     /**
140      * Process cancel resource result.
141      */
142     private void onCancelResourceResult() {
143         if (servicePathRpcResult.getStatus() == RpcStatusEx.Successful) {
144             LOG.info("PCE cancel resource done OK !");
145             OperationResult deleteServicePathOperationResult =
146                     this.serviceDataStoreOperations.deleteServicePath(input.getServiceName());
147             if (!deleteServicePathOperationResult.isSuccess()) {
148                 LOG.warn("Service path was not removed from datastore!");
149             }
150             OperationResult deleteServiceOperationResult = null;
151             if (tempService) {
152                 deleteServiceOperationResult =
153                         this.serviceDataStoreOperations.deleteTempService(input.getServiceName());
154                 if (!deleteServiceOperationResult.isSuccess()) {
155                     LOG.warn("Service was not removed from datastore!");
156                 }
157             } else {
158                 deleteServiceOperationResult =
159                         this.serviceDataStoreOperations.deleteService(input.getServiceName());
160                 if (!deleteServiceOperationResult.isSuccess()) {
161                     LOG.warn("Service was not removed from datastore!");
162                 }
163             }
164             /**
165              * if it was an RPC serviceReconfigure, re-launch PCR.
166              */
167             if (this.serviceReconfigure) {
168                 LOG.info("cancel resource reserve done, relaunching PCE path computation ...");
169                 this.pceServiceWrapper.performPCE(input.getServiceCreateInput(), true);
170                 this.serviceReconfigure = false;
171             }
172         } else if (servicePathRpcResult.getStatus() == RpcStatusEx.Failed) {
173             LOG.info("PCE cancel resource failed !");
174         }
175     }
176
177     @SuppressFBWarnings(
178         value = "ES_COMPARING_STRINGS_WITH_EQ",
179         justification = "false positives, not strings but real object references comparisons")
180     private Boolean compareServicePathRpcResult(ServicePathRpcResult notification) {
181         Boolean result = true;
182         if (servicePathRpcResult == null) {
183             result = false;
184         } else {
185             if (servicePathRpcResult.getNotificationType() != notification.getNotificationType()) {
186                 result = false;
187             }
188             if (servicePathRpcResult.getServiceName() != notification.getServiceName()) {
189                 result = false;
190             }
191             if (servicePathRpcResult.getStatus() != notification.getStatus()) {
192                 result = false;
193             }
194             if (servicePathRpcResult.getStatusMessage() != notification.getStatusMessage()) {
195                 result = false;
196             }
197         }
198         return result;
199     }
200
201     public void setInput(ServiceInput serviceInput) {
202         this.input = serviceInput;
203     }
204
205     public void setServiceReconfigure(Boolean serv) {
206         this.serviceReconfigure = serv;
207     }
208
209     public void setserviceDataStoreOperations(ServiceDataStoreOperations serviceData) {
210         this.serviceDataStoreOperations = serviceData;
211     }
212
213     public void setTempService(Boolean tempService) {
214         this.tempService = tempService;
215     }
216
217     public void setServiceFeasiblity(Boolean serviceFeasiblity) {
218         this.serviceFeasiblity = serviceFeasiblity;
219     }
220
221 }