a964c97dd5dda3063b9cae1e6680edbab88d360c
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / stub / StubrendererImpl.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
9
10 package org.opendaylight.transportpce.servicehandler.stub;
11
12 import com.google.common.base.Optional;
13 import com.google.common.util.concurrent.ListenableFuture;
14
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.transportpce.common.ResponseCodes;
23 import org.opendaylight.transportpce.common.Timeouts;
24 import org.opendaylight.transportpce.renderer.NetworkModelWavelengthService;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceDeleteInput;
26 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceDeleteOutput;
27 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceDeleteOutputBuilder;
28 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceImplementationRequestInput;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceImplementationRequestOutput;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceImplementationRequestOutputBuilder;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.configuration.response.common.ConfigurationResponseCommonBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev171016.service.path.PathDescription;
33 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.ServicePathList;
34 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePaths;
35 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePathsKey;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42
43
44 /**
45  * Class to implement StubrendererService.
46  * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
47  *
48  */
49 public class StubrendererImpl {
50     /** Logging. */
51     private static final Logger LOG = LoggerFactory.getLogger(StubrendererImpl.class);
52     /** check service sdnc-request-header compliancy. */
53     private final NetworkModelWavelengthService networkModelWavelengthService;
54     private final DataBroker dataBroker;
55
56     public StubrendererImpl(NetworkModelWavelengthService networkModelWavelengthService, DataBroker dataBroker) {
57         this.networkModelWavelengthService = networkModelWavelengthService;
58         this.dataBroker = dataBroker;
59     }
60
61     public ListenableFuture<RpcResult<ServiceDeleteOutput>> serviceDelete(ServiceDeleteInput input) {
62         LOG.info("ServiceDelete request ...");
63         String serviceName = input.getServiceName();
64         String message = "";
65         String responseCode = null;
66         try {
67             LOG.info("Wait for 1s til beginning the Renderer serviceDelete request");
68             Thread.sleep(1000); //sleep for 1s
69         } catch (InterruptedException e) {
70             message = "deleting service failed !";
71             LOG.error("deleting service failed !", e);
72             responseCode = ResponseCodes.RESPONSE_FAILED;
73         }
74         // Obtain path description
75         Optional<PathDescription> pathDescriptionOpt = getPathDescriptionFromDatastore(serviceName);
76         PathDescription pathDescription;
77         if (pathDescriptionOpt.isPresent()) {
78             pathDescription = pathDescriptionOpt.get();
79             this.networkModelWavelengthService.freeWavelengths(pathDescription);
80             responseCode = ResponseCodes.RESPONSE_OK;
81             message = "service deleted !";
82         } else {
83             LOG.error("failed to get pathDescription for service : {}", serviceName);
84             responseCode = ResponseCodes.RESPONSE_FAILED;
85             message = "failed to get pathDescription for service : " + serviceName;
86         }
87         ConfigurationResponseCommonBuilder configurationResponseCommon = new ConfigurationResponseCommonBuilder()
88                 .setAckFinalIndicator(ResponseCodes.FINAL_ACK_YES)
89                 .setRequestId(input.getServiceHandlerHeader().getRequestId())
90                 .setResponseCode(responseCode)
91                 .setResponseMessage(message);
92         ServiceDeleteOutput output =  new ServiceDeleteOutputBuilder()
93                 .setConfigurationResponseCommon(configurationResponseCommon.build())
94                 .build();
95         return RpcResultBuilder.success(output).buildFuture();
96     }
97
98     public ListenableFuture<RpcResult<ServiceImplementationRequestOutput>>
99         serviceImplementation(ServiceImplementationRequestInput input) {
100         LOG.info("serviceImplementation request ...");
101         String message = "";
102         String responseCode = null;
103         try {
104             LOG.info("Wait for 1s til beginning the Renderer serviceDelete request");
105             Thread.sleep(1000); //sleep for 1s
106         } catch (InterruptedException e) {
107             message = "implementing service failed !";
108             LOG.error(message);
109             responseCode = ResponseCodes.RESPONSE_FAILED;
110         }
111         this.networkModelWavelengthService.useWavelengths(input.getPathDescription());
112         message = "service implemented !";
113         responseCode = ResponseCodes.RESPONSE_OK;
114         ConfigurationResponseCommonBuilder configurationResponseCommon = new ConfigurationResponseCommonBuilder()
115                 .setAckFinalIndicator(ResponseCodes.FINAL_ACK_YES)
116                 .setRequestId(input.getServiceHandlerHeader().getRequestId())
117                 .setResponseCode(responseCode)
118                 .setResponseMessage(message);
119         ServiceImplementationRequestOutput output =  new ServiceImplementationRequestOutputBuilder()
120                 .setConfigurationResponseCommon(configurationResponseCommon.build())
121                 .build();
122         return RpcResultBuilder.success(output).buildFuture();
123     }
124
125     private Optional<PathDescription> getPathDescriptionFromDatastore(String serviceName) {
126         InstanceIdentifier<PathDescription> pathDescriptionIID = InstanceIdentifier.create(ServicePathList.class)
127                 .child(ServicePaths.class, new ServicePathsKey(serviceName)).child(PathDescription.class);
128         ReadOnlyTransaction pathDescReadTx = this.dataBroker.newReadOnlyTransaction();
129         try {
130             LOG.debug("Getting path description for service {}", serviceName);
131             return pathDescReadTx.read(LogicalDatastoreType.OPERATIONAL, pathDescriptionIID)
132                     .get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS);
133         } catch (InterruptedException | ExecutionException | TimeoutException e) {
134             LOG.warn("Exception while getting path description from datastore {} for service {}!", pathDescriptionIID,
135                     serviceName, e);
136             return Optional.absent();
137         }
138     }
139 }