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.service;
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.ListeningExecutorService;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import java.util.concurrent.Executors;
16 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
17 import org.opendaylight.transportpce.common.ResponseCodes;
18 import org.opendaylight.transportpce.renderer.provisiondevice.RendererServiceOperations;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev200520.ServiceDeleteInput;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev200520.ServiceDeleteInputBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev200520.ServiceDeleteOutput;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev200520.ServiceDeleteOutputBuilder;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.servicehandler.rev171017.ServiceRpcResultSh;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.servicehandler.rev171017.ServiceRpcResultShBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.ServiceNotificationTypes;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.configuration.response.common.ConfigurationResponseCommon;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.configuration.response.common.ConfigurationResponseCommonBuilder;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.TempServiceDeleteInput;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.service.list.Services;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.RpcStatusEx;
31 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.service.handler.header.ServiceHandlerHeader;
32 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.service.handler.header.ServiceHandlerHeaderBuilder;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Class to call RendererServiceOperations.
39 * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
42 public class RendererServiceWrapper {
44 private static final Logger LOG = LoggerFactory.getLogger(RendererServiceWrapper.class);
45 private final RendererServiceOperations rendererServiceOperations;
46 private final NotificationPublishService notificationPublishService;
47 private ServiceRpcResultSh notification = null;
48 private final ListeningExecutorService executor;
50 public RendererServiceWrapper(RendererServiceOperations rendererServiceOperations,
51 NotificationPublishService notificationPublishService) {
52 this.rendererServiceOperations = rendererServiceOperations;
53 this.notificationPublishService = notificationPublishService;
54 this.executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(5));
57 private void sendNotifications(ServiceRpcResultSh notif) {
59 notificationPublishService.putNotification(notif);
60 } catch (InterruptedException e) {
61 LOG.info("notification offer rejected : ", e);
65 public ServiceDeleteOutput performRenderer(ServiceDeleteInput serviceDeleteInput,
66 ServiceNotificationTypes notifType, Services service) {
67 if (validateParams(serviceDeleteInput.getServiceName(), serviceDeleteInput.getServiceHandlerHeader(), false)) {
68 return performRenderer(serviceDeleteInput.getServiceName(), serviceDeleteInput.getServiceHandlerHeader(),
69 ServiceNotificationTypes.ServiceDeleteResult, service);
71 return returnRendererFailed();
75 public ServiceDeleteOutput performRenderer(TempServiceDeleteInput tempServiceDeleteInput,
76 ServiceNotificationTypes notifType) {
77 String commonId = tempServiceDeleteInput.getCommonId();
78 if (validateParams(commonId, null, true)) {
79 ServiceHandlerHeader serviceHandler = new ServiceHandlerHeaderBuilder().setRequestId(commonId).build();
80 return performRenderer(tempServiceDeleteInput.getCommonId(), serviceHandler,
81 ServiceNotificationTypes.ServiceDeleteResult, null);
83 return returnRendererFailed();
87 private ServiceDeleteOutput performRenderer(String serviceName, ServiceHandlerHeader serviceHandlerHeader,
88 ServiceNotificationTypes notifType, Services service) {
89 notification = new ServiceRpcResultShBuilder().setNotificationType(notifType).setServiceName(serviceName)
90 .setStatus(RpcStatusEx.Pending)
91 .setStatusMessage("Service compliant, submitting temp service delete Request ...").build();
92 sendNotifications(notification);
93 FutureCallback<ServiceDeleteOutput> rendererCallback = new FutureCallback<ServiceDeleteOutput>() {
96 ServiceRpcResultSh notification = null;
99 public void onSuccess(ServiceDeleteOutput response) {
100 if (response != null) {
102 * If PCE reply is received before timer expiration with a positive result, a
103 * service is created with admin and operational status 'down'.
105 message = "Renderer replied to service delete Request !";
106 LOG.info("Renderer replied to service delete Request : {}", response);
108 new ServiceRpcResultShBuilder().setNotificationType(notifType).setServiceName(serviceName)
109 .setStatus(RpcStatusEx.Successful).setStatusMessage(message).build();
110 sendNotifications(notification);
112 message = "Renderer service delete failed ";
113 notification = new ServiceRpcResultShBuilder().setNotificationType(notifType).setServiceName("")
114 .setStatus(RpcStatusEx.Failed).setStatusMessage(message).build();
115 sendNotifications(notification);
120 public void onFailure(Throwable arg0) {
121 LOG.error("Renderer service delete failed !");
122 notification = new ServiceRpcResultShBuilder().setNotificationType(notifType)
123 .setServiceName(serviceName)
124 .setStatus(RpcStatusEx.Failed)
125 .setStatusMessage("Renderer service delete request failed : " + arg0.getMessage()).build();
126 sendNotifications(notification);
129 ServiceDeleteInput serviceDeleteInput = createRendererRequestInput(serviceName, serviceHandlerHeader);
130 ListenableFuture<ServiceDeleteOutput> renderer =
131 this.rendererServiceOperations.serviceDelete(serviceDeleteInput, service);
132 Futures.addCallback(renderer, rendererCallback, executor);
133 ConfigurationResponseCommon value =
134 new ConfigurationResponseCommonBuilder().setAckFinalIndicator(ResponseCodes.FINAL_ACK_NO)
135 .setRequestId(serviceDeleteInput.getServiceHandlerHeader().getRequestId())
136 .setResponseCode(ResponseCodes.RESPONSE_OK)
137 .setResponseMessage("Renderer service delete in progress").build();
138 return new ServiceDeleteOutputBuilder().setConfigurationResponseCommon(value).build();
141 private ServiceDeleteInput createRendererRequestInput(String serviceName,
142 ServiceHandlerHeader serviceHandlerHeader) {
143 LOG.info("Mapping ServiceDeleteInput or TempServiceDelete to Renderer requests");
144 return new ServiceDeleteInputBuilder().setServiceHandlerHeader(serviceHandlerHeader).setServiceName(serviceName)
148 private static ServiceDeleteOutput returnRendererFailed() {
149 ConfigurationResponseCommon configurationResponseCommon = new ConfigurationResponseCommonBuilder()
150 .setAckFinalIndicator(ResponseCodes.FINAL_ACK_YES).setResponseCode(ResponseCodes.RESPONSE_FAILED)
151 .setResponseMessage("Renderer service delete failed !").build();
152 return new ServiceDeleteOutputBuilder().setConfigurationResponseCommon(configurationResponseCommon).build();
155 private Boolean validateParams(String serviceName, ServiceHandlerHeader serviceHandlerHeader, boolean temp) {
156 boolean result = true;
157 if (!checkString(serviceName)) {
159 LOG.error("Service Name (common-id for Temp service) is not set");
160 } else if (!temp && (serviceHandlerHeader == null)) {
161 LOG.error("Service serviceHandlerHeader 'request-id' is not set");
167 private static boolean checkString(String value) {
168 return ((value != null) && (value.compareTo("") != 0));