upgrade models to OpenROADM service 5.1.0
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / service / RendererServiceWrapper.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.service;
9
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
16 import java.util.concurrent.Executors;
17
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.rev171017.ServiceRpcResultSh;
26 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.servicehandler.rev171017.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.rev191009.RpcStatusEx;
32 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev191009.service.handler.header.ServiceHandlerHeader;
33 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev191009.service.handler.header.ServiceHandlerHeaderBuilder;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Class to call RendererServiceOperations.
39  *
40  * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
41  *
42  */
43 public class RendererServiceWrapper {
44
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;
50
51     public RendererServiceWrapper(RendererServiceOperations rendererServiceOperations,
52             NotificationPublishService notificationPublishService) {
53         this.rendererServiceOperations = rendererServiceOperations;
54         this.notificationPublishService = notificationPublishService;
55         this.executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(5));
56     }
57
58     private void sendNotifications(ServiceRpcResultSh notif) {
59         try {
60             notificationPublishService.putNotification(notif);
61         } catch (InterruptedException e) {
62             LOG.info("notification offer rejected : ", e);
63         }
64     }
65
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);
71         } else {
72             return returnRendererFailed();
73         }
74     }
75
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);
83         } else {
84             return returnRendererFailed();
85         }
86     }
87
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>() {
95
96             String message = "";
97             ServiceRpcResultSh notification = null;
98
99             @Override
100             public void onSuccess(ServiceDeleteOutput response) {
101                 if (response != null) {
102                     /**
103                      * If PCE reply is received before timer expiration with a positive result, a
104                      * service is created with admin and operational status 'down'.
105                      */
106                     message = "Renderer replied to service delete Request !";
107                     LOG.info("Renderer replied to service delete Request : {}", response);
108                     notification =
109                             new ServiceRpcResultShBuilder().setNotificationType(notifType).setServiceName(serviceName)
110                                     .setStatus(RpcStatusEx.Successful).setStatusMessage(message).build();
111                     sendNotifications(notification);
112                 } else {
113                     message = "Renderer service delete failed ";
114                     notification = new ServiceRpcResultShBuilder().setNotificationType(notifType).setServiceName("")
115                             .setStatus(RpcStatusEx.Failed).setStatusMessage(message).build();
116                     sendNotifications(notification);
117                 }
118             }
119
120             @Override
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);
128             }
129         };
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();
140     }
141
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)
146                 .build();
147     }
148
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();
154     }
155
156     private Boolean validateParams(String serviceName, ServiceHandlerHeader serviceHandlerHeader, boolean temp) {
157         boolean result = true;
158         if (!checkString(serviceName)) {
159             result = false;
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");
163             result = false;
164         }
165         return result;
166     }
167
168     private static boolean checkString(String value) {
169         return ((value != null) && (value.compareTo("") != 0));
170     }
171 }
172