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