Initial commit for ServiceHandler
[transportpce.git] / stubrenderer / src / main / java / org / opendaylight / transportpce / stubrenderer / impl / 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.stubrenderer.impl;
11
12 import java.util.concurrent.Future;
13
14 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
15 import org.opendaylight.transportpce.stubrenderer.SendingRendererRPCs;
16 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.ServiceDeleteInput;
17 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.ServiceDeleteOutput;
18 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.ServiceDeleteOutputBuilder;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.ServiceImplementationRequestInput;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.ServiceImplementationRequestOutput;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.ServiceImplementationRequestOutputBuilder;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.ServiceRpcResultSp;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.ServiceRpcResultSpBuilder;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.StubrendererService;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.service.rpc.result.sp.PathTopology;
26 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.service.rpc.result.sp.PathTopologyBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.configuration.response.common.ConfigurationResponseCommonBuilder;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.service.TopologyBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426.RpcStatusEx;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426.ServicePathNotificationTypes;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37 /*
38  * Class to implement StubrendererService.
39  * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
40  *
41  */
42 public class StubrendererImpl implements StubrendererService {
43     /* Logging. */
44     private static final Logger LOG = LoggerFactory.getLogger(StubrendererImpl.class);
45     /* send notification. */
46     private NotificationPublishService notificationPublishService;
47     private ServiceRpcResultSp notification;
48
49     public StubrendererImpl(NotificationPublishService notificationPublishService) {
50         this.notificationPublishService = notificationPublishService;
51     }
52
53     @Override
54     public Future<RpcResult<ServiceImplementationRequestOutput>> serviceImplementationRequest(
55             ServiceImplementationRequestInput input) {
56         String message = "";
57         LOG.info("RPC  serviceImplementationRequest request received");
58
59         notification = new ServiceRpcResultSpBuilder()
60         .setNotificationType(ServicePathNotificationTypes.ServiceImplementationRequest)
61         .setServiceName(input.getServiceName())
62         .setStatus(RpcStatusEx.Pending)
63         .setStatusMessage("Service compliant, submitting serviceImplementation Request ...")
64         .build();
65         try {
66             notificationPublishService.putNotification(notification);
67         } catch (InterruptedException e) {
68             LOG.info("notification offer rejected : " + e);
69         }
70
71         SendingRendererRPCs sendingRenderer = new SendingRendererRPCs();
72         sendingRenderer.serviceImplementation();
73         if (sendingRenderer.getSuccess()) {
74             message = "Service implemented !";
75             ServiceRpcResultSpBuilder tmp = new ServiceRpcResultSpBuilder()
76                 .setNotificationType(ServicePathNotificationTypes.ServiceImplementationRequest)
77                 .setServiceName(input.getServiceName())
78                 .setStatus(RpcStatusEx.Successful)
79                 .setStatusMessage(message);
80             TopologyBuilder topo = sendingRenderer.getTopology();
81             if (topo != null) {
82                 PathTopology path = new PathTopologyBuilder()
83                     .setAToZ(topo.getAToZ())
84                     .setZToA(topo.getZToA())
85                     .build();
86                 tmp.setPathTopology(path);
87             }
88             notification = tmp.build();
89             try {
90                 notificationPublishService.putNotification(notification);
91             } catch (InterruptedException e) {
92                 LOG.info("notification offer rejected : " + e);
93             }
94
95         } else {
96             message = "Service not implemented !";
97         }
98         LOG.info(message);
99         ConfigurationResponseCommonBuilder configurationResponseCommon = new ConfigurationResponseCommonBuilder()
100             .setAckFinalIndicator("Yes")
101             .setRequestId(input.getServiceHandlerHeader().getRequestId())
102             .setResponseCode("200")
103             .setResponseMessage(message);
104
105         ServiceImplementationRequestOutput output = new ServiceImplementationRequestOutputBuilder()
106             .setConfigurationResponseCommon(configurationResponseCommon.build())
107             .build();
108
109         return RpcResultBuilder.success(output).buildFuture();
110     }
111
112     @Override
113     public Future<RpcResult<ServiceDeleteOutput>> serviceDelete(ServiceDeleteInput input) {
114         String message = "";
115         LOG.info("RPC  serviceDelete request received");
116
117         notification = new ServiceRpcResultSpBuilder()
118             .setNotificationType(ServicePathNotificationTypes.ServiceDelete)
119             .setServiceName(input.getServiceName())
120             .setStatus(RpcStatusEx.Pending)
121             .setStatusMessage("Service compliant, submitting ServiceDelete Request ...")
122             .build();
123         try {
124             notificationPublishService.putNotification(notification);
125         } catch (InterruptedException e) {
126             LOG.info("notification offer rejected : " + e);
127         }
128
129         SendingRendererRPCs sendingRenderer = new SendingRendererRPCs();
130         sendingRenderer.serviceDelete();
131         if (sendingRenderer.getSuccess()) {
132             message = "Service deleted ! ";
133             LOG.info(message);
134             ServiceRpcResultSpBuilder tmp = new ServiceRpcResultSpBuilder()
135                 .setNotificationType(ServicePathNotificationTypes.ServiceDelete)
136                 .setServiceName(input.getServiceName())
137                 .setStatus(RpcStatusEx.Successful)
138                 .setStatusMessage(message);
139             notification = tmp.build();
140             try {
141                 notificationPublishService.putNotification(notification);
142             } catch (InterruptedException e) {
143                 LOG.info("notification offer rejected : " + e);
144             }
145         } else {
146             message = "Service not deleted !";
147         }
148         LOG.info(message);
149         ConfigurationResponseCommonBuilder configurationResponseCommon = new ConfigurationResponseCommonBuilder()
150             .setAckFinalIndicator("yes")
151             .setRequestId(input.getServiceHandlerHeader().getRequestId())
152             .setResponseCode("200")
153             .setResponseMessage(message);
154         ServiceDeleteOutput output = new ServiceDeleteOutputBuilder()
155             .setConfigurationResponseCommon(configurationResponseCommon.build())
156             .build();
157
158         return RpcResultBuilder.success(output).buildFuture();
159
160     }
161 }