Merge "bump deps to latest Al SR1 dev platform versions"
[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.Failed) {
86             LOG.error("PCE path computation failed !");
87             return;
88         } else if (servicePathRpcResult.getStatus() == RpcStatusEx.Pending) {
89             LOG.warn("PCE path computation returned a Penging RpcStatusEx code!");
90             return;
91         } else if (servicePathRpcResult.getStatus() != RpcStatusEx.Successful) {
92             LOG.error("PCE path computation returned an unknown RpcStatusEx code!");
93             return;
94         }
95
96         LOG.info("PCE calculation done OK !");
97         if (servicePathRpcResult.getPathDescription() == null) {
98             LOG.error("'PathDescription' parameter is null ");
99             return;
100         }
101         PathDescription pathDescription = new PathDescriptionBuilder()
102                 .setAToZDirection(servicePathRpcResult.getPathDescription().getAToZDirection())
103                 .setZToADirection(servicePathRpcResult.getPathDescription().getZToADirection())
104                 .build();
105         LOG.info("PathDescription gets : {}", pathDescription);
106         if (serviceFeasiblity) {
107             LOG.warn("service-feasibility-check RPC ");
108             return;
109         }
110         if (input == null) {
111             LOG.error("Input is null !");
112             return;
113         }
114         OperationResult operationResult = null;
115         if (tempService) {
116             operationResult = this.serviceDataStoreOperations.createTempService(input.getTempServiceCreateInput());
117             if (!operationResult.isSuccess()) {
118                 LOG.error("Temp Service not created in datastore !");
119             }
120         } else {
121             operationResult = this.serviceDataStoreOperations.createService(input.getServiceCreateInput());
122             if (!operationResult.isSuccess()) {
123                 LOG.error("Service not created in datastore !");
124             }
125         }
126         ResponseParameters responseParameters = new ResponseParametersBuilder()
127                 .setPathDescription(new org.opendaylight.yang.gen.v1.http.org
128                         .transportpce.b.c._interface.service.types.rev200128
129                         .response.parameters.sp.response.parameters
130                         .PathDescriptionBuilder(pathDescription).build())
131                 .build();
132         PathComputationRequestOutput pceResponse = new PathComputationRequestOutputBuilder()
133                 .setResponseParameters(responseParameters).build();
134         OperationResult operationServicePathSaveResult = this.serviceDataStoreOperations
135                 .createServicePath(input, pceResponse);
136         if (!operationServicePathSaveResult.isSuccess()) {
137             LOG.error("Service Path not created in datastore !");
138         }
139         ServiceImplementationRequestInput serviceImplementationRequest = ModelMappingUtils
140                 .createServiceImplementationRequest(input, pathDescription);
141         LOG.info("Sending serviceImplementation request : {}", serviceImplementationRequest);
142         this.rendererServiceOperations.serviceImplementation(serviceImplementationRequest);
143     }
144
145     /**
146      * Process cancel resource result.
147      */
148     private void onCancelResourceResult() {
149         if (servicePathRpcResult.getStatus() == RpcStatusEx.Failed) {
150             LOG.info("PCE cancel resource failed !");
151             return;
152         } else if (servicePathRpcResult.getStatus() == RpcStatusEx.Pending) {
153             LOG.warn("PCE cancel returned a Penging RpcStatusEx code!");
154             return;
155         } else if (servicePathRpcResult.getStatus() != RpcStatusEx.Successful) {
156             LOG.error("PCE cancel returned an unknown RpcStatusEx code!");
157             return;
158         }
159         LOG.info("PCE cancel resource done OK !");
160         OperationResult deleteServicePathOperationResult =
161                 this.serviceDataStoreOperations.deleteServicePath(input.getServiceName());
162         if (!deleteServicePathOperationResult.isSuccess()) {
163             LOG.warn("Service path was not removed from datastore!");
164         }
165         OperationResult deleteServiceOperationResult = null;
166         if (tempService) {
167             deleteServiceOperationResult =
168                     this.serviceDataStoreOperations.deleteTempService(input.getServiceName());
169             if (!deleteServiceOperationResult.isSuccess()) {
170                 LOG.warn("Temp Service was not removed from datastore!");
171             }
172         } else {
173             deleteServiceOperationResult =
174                     this.serviceDataStoreOperations.deleteService(input.getServiceName());
175             if (!deleteServiceOperationResult.isSuccess()) {
176                 LOG.warn("Service was not removed from datastore!");
177             }
178         }
179         /**
180          * if it was an RPC serviceReconfigure, re-launch PCR.
181          */
182         if (this.serviceReconfigure) {
183             LOG.info("cancel resource reserve done, relaunching PCE path computation ...");
184             this.pceServiceWrapper.performPCE(input.getServiceCreateInput(), true);
185             this.serviceReconfigure = false;
186         }
187     }
188
189     @SuppressFBWarnings(
190         value = "ES_COMPARING_STRINGS_WITH_EQ",
191         justification = "false positives, not strings but real object references comparisons")
192     private Boolean compareServicePathRpcResult(ServicePathRpcResult notification) {
193         if (servicePathRpcResult == null) {
194             return false;
195         }
196         if (servicePathRpcResult.getNotificationType() != notification.getNotificationType()) {
197             return false;
198         }
199         if (servicePathRpcResult.getServiceName() != notification.getServiceName()) {
200             return false;
201         }
202         if (servicePathRpcResult.getStatus() != notification.getStatus()) {
203             return false;
204         }
205         if (servicePathRpcResult.getStatusMessage() != notification.getStatusMessage()) {
206             return false;
207         }
208         return true;
209     }
210
211     public void setInput(ServiceInput serviceInput) {
212         this.input = serviceInput;
213     }
214
215     public void setServiceReconfigure(Boolean serv) {
216         this.serviceReconfigure = serv;
217     }
218
219     public void setserviceDataStoreOperations(ServiceDataStoreOperations serviceData) {
220         this.serviceDataStoreOperations = serviceData;
221     }
222
223     public void setTempService(Boolean tempService) {
224         this.tempService = tempService;
225     }
226
227     public void setServiceFeasiblity(Boolean serviceFeasiblity) {
228         this.serviceFeasiblity = serviceFeasiblity;
229     }
230
231 }