2 * Copyright © 2017 Orange, Inc. and others. All rights reserved.
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
8 package org.opendaylight.transportpce.servicehandler.listeners;
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.rev220808.PathComputationRequestOutputBuilder;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.ServicePathRpcResult;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.TransportpcePceListener;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.service.path.rpc.result.PathDescription;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.service.path.rpc.result.PathDescriptionBuilder;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.ServiceImplementationRequestInput;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev191129.State;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.service.list.Services;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.RpcStatusEx;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.response.parameters.sp.ResponseParametersBuilder;
29 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishNotificationProcessService;
30 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishNotificationProcessServiceBuilder;
31 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.notification.process.service.ServiceAEndBuilder;
32 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.notification.process.service.ServiceZEndBuilder;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class PceListenerImpl implements TransportpcePceListener {
38 private static final Logger LOG = LoggerFactory.getLogger(PceListenerImpl.class);
39 private static final String PUBLISHER = "PceListener";
41 private ServicePathRpcResult servicePathRpcResult;
42 private RendererServiceOperations rendererServiceOperations;
43 private ServiceDataStoreOperations serviceDataStoreOperations;
44 private PCEServiceWrapper pceServiceWrapper;
45 private ServiceInput input;
46 private Boolean serviceReconfigure;
47 private Boolean tempService;
48 private Boolean serviceFeasiblity;
49 private NotificationPublishService notificationPublishService;
51 public PceListenerImpl(
52 RendererServiceOperations rendererServiceOperations,
53 PathComputationService pathComputationService,
54 NotificationPublishService notificationPublishService,
55 ServiceDataStoreOperations serviceDataStoreOperations) {
56 this.rendererServiceOperations = rendererServiceOperations;
57 this.pceServiceWrapper = new PCEServiceWrapper(pathComputationService, notificationPublishService);
58 this.serviceDataStoreOperations = serviceDataStoreOperations;
59 setServiceReconfigure(false);
61 setTempService(false);
62 setServiceFeasiblity(false);
63 this.notificationPublishService = notificationPublishService;
67 public void onServicePathRpcResult(ServicePathRpcResult notification) {
68 if (compareServicePathRpcResult(notification)) {
69 LOG.warn("ServicePathRpcResult already wired !");
72 servicePathRpcResult = notification;
73 switch (servicePathRpcResult.getNotificationType().getIntValue()) {
74 /* path-computation-request. */
76 onPathComputationResult(notification);
78 /* cancel-resource-reserve. */
80 onCancelResourceResult();
88 * Process path computation request result.
89 * @param notification the result notification.
91 private void onPathComputationResult(ServicePathRpcResult notification) {
92 LOG.info("PCE '{}' Notification received : {}", servicePathRpcResult.getNotificationType().getName(),
94 if (!checkStatus(notification)) {
97 if (servicePathRpcResult.getPathDescription() == null) {
98 LOG.error("'PathDescription' parameter is null ");
101 PathDescription pathDescription =
102 new PathDescriptionBuilder()
103 .setAToZDirection(servicePathRpcResult.getPathDescription().getAToZDirection())
104 .setZToADirection(servicePathRpcResult.getPathDescription().getZToADirection())
106 LOG.info("PathDescription gets : {}", pathDescription);
107 if (serviceFeasiblity) {
108 LOG.warn("service-feasibility-check RPC ");
112 LOG.error("Input is null !");
115 OperationResult operationResult = null;
117 operationResult = this.serviceDataStoreOperations.createTempService(input.getTempServiceCreateInput());
118 if (!operationResult.isSuccess()) {
119 LOG.error("Temp Service not created in datastore !");
122 operationResult = this.serviceDataStoreOperations.createService(input.getServiceCreateInput());
123 if (!operationResult.isSuccess()) {
124 LOG.error("Service not created in datastore !");
127 if (!this.serviceDataStoreOperations
131 new PathComputationRequestOutputBuilder()
132 .setResponseParameters(
133 new ResponseParametersBuilder()
135 new org.opendaylight.yang.gen.v1
136 .http.org.transportpce.b.c._interface.service.types.rev220118
137 .response.parameters.sp.response.parameters
138 .PathDescriptionBuilder(pathDescription)
143 LOG.error("Service Path not created in datastore !");
145 ServiceImplementationRequestInput serviceImplementationRequest =
146 ModelMappingUtils.createServiceImplementationRequest(input, pathDescription);
147 LOG.info("Sending serviceImplementation request : {}", serviceImplementationRequest);
148 this.rendererServiceOperations.serviceImplementation(serviceImplementationRequest);
152 * Check status of notification and send nbi notification.
153 * @param notification ServicePathRpcResult the notification to check.
154 * @return true is status is Successful, false otherwise.
156 private boolean checkStatus(ServicePathRpcResult notification) {
157 PublishNotificationProcessService nbiNotification = getPublishNotificationProcessService(notification);
158 PublishNotificationProcessServiceBuilder publishNotificationProcessServiceBuilder =
159 new PublishNotificationProcessServiceBuilder(nbiNotification);
160 //TODO is it worth to instantiate the 2 variables above if status is 'Pending' or 'Successful' ?
161 switch (servicePathRpcResult.getStatus()) {
163 LOG.error("PCE path computation failed !");
164 nbiNotification = publishNotificationProcessServiceBuilder
165 .setMessage("ServiceCreate request failed ...")
166 .setResponseFailed("PCE path computation failed !")
167 .setOperationalState(State.Degraded).build();
168 sendNbiNotification(nbiNotification);
171 LOG.warn("PCE path computation returned a Pending RpcStatusEx code!");
174 LOG.info("PCE calculation done OK !");
177 LOG.error("PCE path computation returned an unknown RpcStatusEx code {}",
178 servicePathRpcResult.getStatus());
179 nbiNotification = publishNotificationProcessServiceBuilder
180 .setMessage("ServiceCreate request failed ...")
181 .setResponseFailed("PCE path computation returned an unknown RpcStatusEx code!")
182 .setOperationalState(State.Degraded).build();
183 sendNbiNotification(nbiNotification);
188 private PublishNotificationProcessService getPublishNotificationProcessService(ServicePathRpcResult notification) {
190 return new PublishNotificationProcessServiceBuilder()
191 .setServiceName(notification.getServiceName())
192 .setPublisherName(PUBLISHER)
195 return new PublishNotificationProcessServiceBuilder()
196 .setServiceName(input.getServiceName())
197 .setServiceAEnd(new ServiceAEndBuilder(input.getServiceAEnd()).build())
198 .setServiceZEnd(new ServiceZEndBuilder(input.getServiceZEnd()).build())
199 .setCommonId(input.getCommonId())
200 .setConnectionType(input.getConnectionType())
201 .setPublisherName(PUBLISHER)
206 * Process cancel resource result.
208 private void onCancelResourceResult() {
209 if (servicePathRpcResult.getStatus() == RpcStatusEx.Pending) {
210 LOG.warn("PCE cancel returned a Pending RpcStatusEx code !");
212 } else if (servicePathRpcResult.getStatus() != RpcStatusEx.Successful
213 && servicePathRpcResult.getStatus() != RpcStatusEx.Failed) {
214 LOG.error("PCE cancel returned an unknown RpcStatusEx code !");
217 Services service = serviceDataStoreOperations.getService(input.getServiceName()).get();
218 PublishNotificationProcessServiceBuilder nbiNotificationBuilder =
219 new PublishNotificationProcessServiceBuilder()
220 .setServiceName(service.getServiceName())
221 .setServiceAEnd(new ServiceAEndBuilder(service.getServiceAEnd()).build())
222 .setServiceZEnd(new ServiceZEndBuilder(service.getServiceZEnd()).build())
223 .setCommonId(service.getCommonId())
224 .setConnectionType(service.getConnectionType())
225 .setPublisherName(PUBLISHER);
226 if (servicePathRpcResult.getStatus() == RpcStatusEx.Failed) {
227 LOG.info("PCE cancel resource failed !");
229 nbiNotificationBuilder
230 .setResponseFailed("PCE cancel resource failed !")
231 .setMessage("ServiceDelete request failed ...")
232 .setOperationalState(service.getOperationalState())
236 LOG.info("PCE cancel resource done OK !");
237 OperationResult deleteServicePathOperationResult =
238 this.serviceDataStoreOperations.deleteServicePath(input.getServiceName());
239 if (!deleteServicePathOperationResult.isSuccess()) {
240 LOG.warn("Service path was not removed from datastore !");
242 OperationResult deleteServiceOperationResult;
243 String serviceType = "";
245 deleteServiceOperationResult = this.serviceDataStoreOperations.deleteTempService(input.getServiceName());
246 serviceType = "Temp ";
248 deleteServiceOperationResult = this.serviceDataStoreOperations.deleteService(input.getServiceName());
250 if (deleteServiceOperationResult.isSuccess()) {
252 nbiNotificationBuilder
253 .setResponseFailed("")
254 .setMessage("Service deleted !")
255 .setOperationalState(State.Degraded)
258 LOG.warn("{}Service was not removed from datastore !", serviceType);
260 nbiNotificationBuilder
261 .setResponseFailed(serviceType + "Service was not removed from datastore !")
262 .setMessage("ServiceDelete request failed ...")
263 .setOperationalState(service.getOperationalState())
267 * if it was an RPC serviceReconfigure, re-launch PCR.
269 if (this.serviceReconfigure) {
270 LOG.info("cancel resource reserve done, relaunching PCE path computation ...");
271 this.pceServiceWrapper.performPCE(input.getServiceCreateInput(), true);
272 this.serviceReconfigure = false;
277 value = "ES_COMPARING_STRINGS_WITH_EQ",
278 justification = "false positives, not strings but real object references comparisons")
279 private Boolean compareServicePathRpcResult(ServicePathRpcResult notification) {
280 if (servicePathRpcResult == null) {
283 if (servicePathRpcResult.getNotificationType() != notification.getNotificationType()) {
286 if (servicePathRpcResult.getServiceName() != notification.getServiceName()) {
289 if (servicePathRpcResult.getStatus() != notification.getStatus()) {
292 if (servicePathRpcResult.getStatusMessage() != notification.getStatusMessage()) {
298 public void setInput(ServiceInput serviceInput) {
299 this.input = serviceInput;
302 public void setServiceReconfigure(Boolean serv) {
303 this.serviceReconfigure = serv;
306 public void setserviceDataStoreOperations(ServiceDataStoreOperations serviceData) {
307 this.serviceDataStoreOperations = serviceData;
310 public void setTempService(Boolean tempService) {
311 this.tempService = tempService;
314 public void setServiceFeasiblity(Boolean serviceFeasiblity) {
315 this.serviceFeasiblity = serviceFeasiblity;
319 * Send notification to NBI notification in order to publish message.
320 * @param service PublishNotificationService
322 private void sendNbiNotification(PublishNotificationProcessService service) {
324 notificationPublishService.putNotification(service);
325 } catch (InterruptedException e) {
326 LOG.warn("Cannot send notification to nbi", e);
327 Thread.currentThread().interrupt();