upgrade models to OpenROADM service 5.1.0
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / stub / StubRendererServiceOperations.java
1 /*
2  * Copyright © 2018 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.stub;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import com.google.common.util.concurrent.ListeningExecutorService;
12 import com.google.common.util.concurrent.MoreExecutors;
13 import java.util.Optional;
14 import java.util.concurrent.Callable;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Executors;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.TimeoutException;
19 import org.opendaylight.mdsal.binding.api.DataBroker;
20 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
21 import org.opendaylight.mdsal.binding.api.ReadTransaction;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.transportpce.common.OperationResult;
24 import org.opendaylight.transportpce.common.ResponseCodes;
25 import org.opendaylight.transportpce.common.Timeouts;
26 import org.opendaylight.transportpce.renderer.NetworkModelWavelengthService;
27 import org.opendaylight.transportpce.renderer.provisiondevice.RendererServiceOperations;
28 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceDeleteInput;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceDeleteOutput;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceDeleteOutputBuilder;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceImplementationRequestInput;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceImplementationRequestOutput;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceImplementationRequestOutputBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceRpcResultSp;
35 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceRpcResultSpBuilder;
36 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.service.rpc.result.sp.PathTopology;
37 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.service.rpc.result.sp.PathTopologyBuilder;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.configuration.response.common.ConfigurationResponseCommonBuilder;
39 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev191009.RpcStatusEx;
40 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev191009.ServicePathNotificationTypes;
41 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev191009.service.path.PathDescription;
42 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.ServicePathList;
43 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePaths;
44 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePathsKey;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.binding.Notification;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 public class StubRendererServiceOperations implements RendererServiceOperations {
51     private static final Logger LOG = LoggerFactory.getLogger(StubRendererServiceOperations.class);
52     private final ListeningExecutorService executor;
53     private final NotificationPublishService notificationPublishService;
54     private final NetworkModelWavelengthService networkModelWavelengthService;
55     private final DataBroker dataBroker;
56     private Boolean rendererFailed;
57     private Boolean isnetworkModelWlService;
58
59     public StubRendererServiceOperations(NetworkModelWavelengthService networkModelWavelengthService,
60             DataBroker dataBroker, NotificationPublishService notificationPublishService) {
61         this.notificationPublishService = notificationPublishService;
62         this.networkModelWavelengthService = networkModelWavelengthService;
63         this.dataBroker = dataBroker;
64         this.rendererFailed = false;
65         this.isnetworkModelWlService = true;
66         executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
67     }
68
69     private void sendNotifications(Notification notif) {
70         try {
71             LOG.info("putting notification : {}", notif);
72             notificationPublishService.putNotification(notif);
73         } catch (InterruptedException e) {
74             LOG.info("notification offer rejected : ", e.getMessage());
75         }
76     }
77
78     @Override
79     public ListenableFuture<ServiceImplementationRequestOutput>
80             serviceImplementation(ServiceImplementationRequestInput input) {
81         return executor.submit(new Callable<ServiceImplementationRequestOutput>() {
82             @Override
83             public ServiceImplementationRequestOutput call() {
84                 LOG.info("serviceImplementation request ...");
85                 String serviceName = input.getServiceName();
86                 RpcStatusEx rpcStatusEx = RpcStatusEx.Pending;
87                 ServiceRpcResultSp notification = new ServiceRpcResultSpBuilder()
88                         .setNotificationType(ServicePathNotificationTypes.ServiceImplementationRequest)
89                         .setServiceName(serviceName).setStatus(rpcStatusEx)
90                         .setStatusMessage("Service compliant, submitting serviceImplementation Request ...").build();
91                 sendNotifications(notification);
92                 String message = "";
93                 String responseCode = null;
94                 ConfigurationResponseCommonBuilder configurationResponseCommon = null;
95                 ServiceImplementationRequestOutput output = null;
96                 try {
97                     LOG.info("Wait for 5s til beginning the Renderer serviceImplementation request");
98                     Thread.sleep(5000);
99                 } catch (InterruptedException e) {
100                     message = "renderer failed !";
101                     rpcStatusEx = RpcStatusEx.Failed;
102                     responseCode = ResponseCodes.RESPONSE_FAILED;
103                     LOG.error(message);
104                     notification = new ServiceRpcResultSpBuilder()
105                             .setNotificationType(ServicePathNotificationTypes.ServiceImplementationRequest)
106                             .setServiceName(serviceName).setStatus(rpcStatusEx).setStatusMessage(message).build();
107                     sendNotifications(notification);
108                     configurationResponseCommon =
109                             new ConfigurationResponseCommonBuilder().setAckFinalIndicator(ResponseCodes.FINAL_ACK_YES)
110                                     .setRequestId(input.getServiceHandlerHeader().getRequestId())
111                                     .setResponseCode(responseCode).setResponseMessage(message);
112                     output = new ServiceImplementationRequestOutputBuilder()
113                             .setConfigurationResponseCommon(configurationResponseCommon.build()).build();
114                     return output;
115                 }
116                 if (rendererFailed) {
117                     LOG.info("forcing renderer to fail");
118                     message = "renderer failed !";
119                     rpcStatusEx = RpcStatusEx.Failed;
120                     LOG.error(message);
121                     responseCode = ResponseCodes.RESPONSE_FAILED;
122                 } else {
123                     if (isnetworkModelWlService) {
124                         networkModelWavelengthService.useWavelengths(input.getPathDescription());
125                     } else {
126                         LOG.warn("No need to execute networkModelWavelengthService...");
127                     }
128                     message = "service implemented !";
129                     rpcStatusEx = RpcStatusEx.Successful;
130                     LOG.info(message);
131                     responseCode = ResponseCodes.RESPONSE_OK;
132                 }
133                 PathTopology pathTopology = new PathTopologyBuilder().build();
134                 notification = new ServiceRpcResultSpBuilder()
135                         .setNotificationType(ServicePathNotificationTypes.ServiceImplementationRequest)
136                         .setServiceName(serviceName).setStatus(rpcStatusEx)
137                         .setStatusMessage(message).setPathTopology(pathTopology).build();
138                 sendNotifications(notification);
139                 responseCode = ResponseCodes.RESPONSE_OK;
140                 configurationResponseCommon = new ConfigurationResponseCommonBuilder()
141                         .setAckFinalIndicator(ResponseCodes.FINAL_ACK_YES)
142                         .setRequestId(input.getServiceHandlerHeader().getRequestId())
143                         .setResponseCode(responseCode)
144                         .setResponseMessage(message);
145                 output = new ServiceImplementationRequestOutputBuilder()
146                         .setConfigurationResponseCommon(configurationResponseCommon.build())
147                         .build();
148                 return output;
149             }
150         });
151     }
152
153     @Override
154     public ListenableFuture<ServiceDeleteOutput> serviceDelete(ServiceDeleteInput input) {
155         return executor.submit(new Callable<ServiceDeleteOutput>() {
156             @Override
157             public ServiceDeleteOutput call() {
158                 LOG.info("ServiceDelete request ...");
159                 String serviceName = input.getServiceName();
160                 RpcStatusEx rpcStatusEx = RpcStatusEx.Pending;
161                 ServiceRpcResultSp notification =
162                         new ServiceRpcResultSpBuilder().setNotificationType(ServicePathNotificationTypes.ServiceDelete)
163                                 .setServiceName(serviceName).setStatus(rpcStatusEx)
164                         .setStatusMessage("Service compliant, submitting serviceDelete Request ...").build();
165                 sendNotifications(notification);
166                 String message = "";
167                 String responseCode = null;
168                 ConfigurationResponseCommonBuilder configurationResponseCommon = null;
169                 ServiceDeleteOutput output = null;
170                 try {
171                     LOG.info("Wait for 5s til beginning the Renderer serviceDelete request");
172                     Thread.sleep(5000);
173                 } catch (InterruptedException e) {
174                     message = "deleting service failed !";
175                     LOG.error("deleting service failed !", e);
176                     responseCode = ResponseCodes.RESPONSE_FAILED;
177                     rpcStatusEx = RpcStatusEx.Failed;
178                     notification = new ServiceRpcResultSpBuilder()
179                             .setNotificationType(ServicePathNotificationTypes.ServiceDelete).setServiceName(serviceName)
180                             .setStatus(rpcStatusEx).setStatusMessage(message).build();
181                     sendNotifications(notification);
182                     configurationResponseCommon =
183                             new ConfigurationResponseCommonBuilder().setAckFinalIndicator(ResponseCodes.FINAL_ACK_YES)
184                                     .setRequestId(input.getServiceHandlerHeader().getRequestId())
185                                     .setResponseCode(responseCode).setResponseMessage(message);
186                     output = new ServiceDeleteOutputBuilder()
187                             .setConfigurationResponseCommon(configurationResponseCommon.build()).build();
188                 }
189                 if (rendererFailed) {
190                     LOG.info("forcing renderer to fail");
191                     message = "renderer failed !";
192                     rpcStatusEx = RpcStatusEx.Failed;
193                     LOG.error(message);
194                     responseCode = ResponseCodes.RESPONSE_FAILED;
195                 } else {
196                     if (isnetworkModelWlService) {
197                         // Obtain path description
198                         Optional<PathDescription> pathDescriptionOpt = getPathDescriptionFromDatastore(serviceName);
199                         PathDescription pathDescription;
200                         if (pathDescriptionOpt.isPresent()) {
201                             pathDescription = pathDescriptionOpt.get();
202                             networkModelWavelengthService.freeWavelengths(pathDescription);
203                         } else {
204                             LOG.warn("failed to get pathDescription for service : {}", serviceName);
205                         }
206                     } else {
207                         LOG.warn("No need to execute networkModelWavelengthService...");
208                     }
209                     message = "service deleted !";
210                     rpcStatusEx = RpcStatusEx.Successful;
211                     LOG.info(message);
212                     responseCode = ResponseCodes.RESPONSE_OK;
213                 }
214                 notification = new ServiceRpcResultSpBuilder()
215                         .setNotificationType(ServicePathNotificationTypes.ServiceDelete)
216                         .setServiceName(serviceName).setStatus(rpcStatusEx)
217                         .setStatusMessage(message).build();
218                 sendNotifications(notification);
219                 responseCode = ResponseCodes.RESPONSE_OK;
220                 configurationResponseCommon = new ConfigurationResponseCommonBuilder()
221                         .setAckFinalIndicator(ResponseCodes.FINAL_ACK_YES)
222                         .setRequestId(input.getServiceHandlerHeader().getRequestId())
223                         .setResponseCode(responseCode)
224                         .setResponseMessage(message);
225                 output = new ServiceDeleteOutputBuilder()
226                         .setConfigurationResponseCommon(configurationResponseCommon.build())
227                         .build();
228                 return output;
229             }
230         });
231     }
232
233     private Optional<PathDescription> getPathDescriptionFromDatastore(String serviceName) {
234         InstanceIdentifier<PathDescription> pathDescriptionIID = InstanceIdentifier.create(ServicePathList.class)
235                 .child(ServicePaths.class, new ServicePathsKey(serviceName)).child(PathDescription.class);
236         ReadTransaction pathDescReadTx = this.dataBroker.newReadOnlyTransaction();
237         try {
238             LOG.debug("Getting path description for service {}", serviceName);
239             return pathDescReadTx.read(LogicalDatastoreType.OPERATIONAL, pathDescriptionIID)
240                     .get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS);
241         } catch (InterruptedException | ExecutionException | TimeoutException e) {
242             LOG.warn("Exception while getting path description from datastore {} for service {}!", pathDescriptionIID,
243                     serviceName, e);
244             return Optional.empty();
245         }
246     }
247
248     public void setRendererFailed(Boolean rendererFailed) {
249         this.rendererFailed = rendererFailed;
250     }
251
252     public void setIsnetworkModelWlService(Boolean isnetworkModelWlService) {
253         this.isnetworkModelWlService = isnetworkModelWlService;
254     }
255
256     @Override
257     public OperationResult reserveResource(PathDescription pathDescription) {
258         return null;
259     }
260
261     @Override
262     public OperationResult freeResource(PathDescription pathDescription) {
263         return null;
264     }
265 }