fix import extra separations
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / stub / StubPceServiceOperations.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.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.concurrent.Callable;
14 import java.util.concurrent.Executors;
15 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
16 import org.opendaylight.transportpce.common.ResponseCodes;
17 import org.opendaylight.transportpce.pce.service.PathComputationService;
18 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.CancelResourceReserveInput;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.CancelResourceReserveOutput;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.CancelResourceReserveOutputBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestInput;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestOutput;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestOutputBuilder;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.ServicePathRpcResult;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.ServicePathRpcResultBuilder;
26 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.service.path.rpc.result.PathDescription;
27 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.service.path.rpc.result.PathDescriptionBuilder;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.configuration.response.common.ConfigurationResponseCommon;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.configuration.response.common.ConfigurationResponseCommonBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.AToZDirection;
31 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.AToZDirectionBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.ZToADirection;
33 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.ZToADirectionBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.RpcStatusEx;
35 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.ServicePathNotificationTypes;
36 import org.opendaylight.yangtools.yang.binding.Notification;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class StubPceServiceOperations implements PathComputationService {
41
42     private static final Logger LOG = LoggerFactory.getLogger(StubPceServiceOperations.class);
43     private final NotificationPublishService notificationPublishService;
44     private Boolean pceFailed;
45     private final ListeningExecutorService executor;
46
47     public StubPceServiceOperations(NotificationPublishService notificationPublishService) {
48         this.notificationPublishService = notificationPublishService;
49         executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
50         this.pceFailed = false;
51     }
52
53     private void sendNotifications(Notification notif) {
54         try {
55             LOG.info("putting notification : {}", notif);
56             notificationPublishService.putNotification(notif);
57         } catch (InterruptedException e) {
58             LOG.info("notification offer rejected: ", e);
59         }
60     }
61
62     @Override
63     public ListenableFuture<CancelResourceReserveOutput> cancelResourceReserve(CancelResourceReserveInput input) {
64         return executor.submit(new Callable<CancelResourceReserveOutput>() {
65             @Override
66             public CancelResourceReserveOutput call() throws Exception {
67                 String serviceName = input.getServiceName();
68                 RpcStatusEx rpcStatusEx = RpcStatusEx.Pending;
69                 ServicePathRpcResult notification = new ServicePathRpcResultBuilder()
70                         .setNotificationType(ServicePathNotificationTypes.CancelResourceReserve)
71                         .setServiceName(serviceName).setStatus(rpcStatusEx)
72                         .setStatusMessage("Service compliant, submitting cancel resource Request ...").build();
73                 sendNotifications(notification);
74                 String message = "";
75                 String responseCode = null;
76                 ConfigurationResponseCommon configurationResponseCommon = null;
77                 CancelResourceReserveOutput output = null;
78                 try {
79                     LOG.info("Wait for 5s til beginning the PCE cancel resource request");
80                     Thread.sleep(5000);
81                 } catch (InterruptedException e) {
82                     message = "path computation service failed !";
83                     LOG.error("path computation service failed !", e);
84                     responseCode = ResponseCodes.RESPONSE_FAILED;
85                     rpcStatusEx = RpcStatusEx.Failed;
86                     notification = new ServicePathRpcResultBuilder()
87                             .setNotificationType(ServicePathNotificationTypes.CancelResourceReserve)
88                             .setStatus(rpcStatusEx).setStatusMessage(message).setServiceName(serviceName).build();
89                     sendNotifications(notification);
90                     configurationResponseCommon =
91                             new ConfigurationResponseCommonBuilder().setAckFinalIndicator(ResponseCodes.FINAL_ACK_YES)
92                                     .setRequestId(input.getServiceHandlerHeader().getRequestId())
93                                     .setResponseCode(responseCode).setResponseMessage(message).build();
94                     output = new CancelResourceReserveOutputBuilder()
95                             .setConfigurationResponseCommon(configurationResponseCommon).build();
96                 }
97                 if (pceFailed) {
98                     LOG.info("forcing pce to fail");
99                     message = "pce failed !";
100                     rpcStatusEx = RpcStatusEx.Failed;
101                     LOG.error(message);
102                     responseCode = ResponseCodes.RESPONSE_FAILED;
103                 } else {
104                     message = "path computated !";
105                     rpcStatusEx = RpcStatusEx.Successful;
106                     LOG.error(message);
107                     responseCode = ResponseCodes.RESPONSE_OK;
108                 }
109                 notification = new ServicePathRpcResultBuilder()
110                         .setNotificationType(ServicePathNotificationTypes.CancelResourceReserve)
111                         .setStatus(RpcStatusEx.Successful).setStatusMessage(message)
112                         .setServiceName(serviceName).build();
113                 sendNotifications(notification);
114                 configurationResponseCommon =
115                         new ConfigurationResponseCommonBuilder().setAckFinalIndicator(ResponseCodes.FINAL_ACK_YES)
116                                 .setRequestId(input.getServiceHandlerHeader().getRequestId())
117                                 .setResponseCode(responseCode).setResponseMessage(message).build();
118                 output = new CancelResourceReserveOutputBuilder()
119                         .setConfigurationResponseCommon(configurationResponseCommon).build();
120                 return output;
121             }
122         });
123
124     }
125
126     @Override
127     public ListenableFuture<PathComputationRequestOutput> pathComputationRequest(PathComputationRequestInput input) {
128         return executor.submit(new Callable<PathComputationRequestOutput>() {
129             @Override
130             public PathComputationRequestOutput call() throws Exception {
131                 String serviceName = input.getServiceName();
132                 RpcStatusEx rpcStatusEx = RpcStatusEx.Pending;
133                 ServicePathRpcResult notification = new ServicePathRpcResultBuilder()
134                         .setNotificationType(ServicePathNotificationTypes.PathComputationRequest)
135                         .setServiceName(serviceName).setStatus(rpcStatusEx)
136                         .setStatusMessage("Service compliant, submitting pathComputation Request ...").build();
137                 sendNotifications(notification);
138                 String message = "";
139                 String responseCode = null;
140                 ConfigurationResponseCommon configurationResponseCommon = null;
141                 PathComputationRequestOutput output = null;
142                 try {
143                     LOG.info("Wait for 5s til beginning the PCE pathComputation request");
144                     Thread.sleep(5000);
145                 } catch (InterruptedException e) {
146                     message = "path computation service failed !";
147                     LOG.error("path computation service failed !", e);
148                     responseCode = ResponseCodes.RESPONSE_FAILED;
149                     rpcStatusEx = RpcStatusEx.Failed;
150                     notification = new ServicePathRpcResultBuilder()
151                             .setNotificationType(ServicePathNotificationTypes.PathComputationRequest)
152                             .setStatus(rpcStatusEx).setStatusMessage(message).setServiceName(serviceName).build();
153                     sendNotifications(notification);
154                     configurationResponseCommon =
155                             new ConfigurationResponseCommonBuilder().setAckFinalIndicator(ResponseCodes.FINAL_ACK_YES)
156                                     .setRequestId(input.getServiceHandlerHeader().getRequestId())
157                                     .setResponseCode(responseCode).setResponseMessage(message).build();
158                     output = new PathComputationRequestOutputBuilder()
159                             .setConfigurationResponseCommon(configurationResponseCommon).build();
160                 }
161                 PathDescription value;
162                 if (pceFailed) {
163                     value = null;
164                     LOG.info("forcing pce to fail");
165                     message = "pce failed !";
166                     rpcStatusEx = RpcStatusEx.Failed;
167                     LOG.error(message);
168                     responseCode = ResponseCodes.RESPONSE_FAILED;
169                 } else {
170                     value = createPathDescription(0L, 5L, 0L, 5L);
171                     message = "path computated !";
172                     rpcStatusEx = RpcStatusEx.Successful;
173                     LOG.error(message);
174                     responseCode = ResponseCodes.RESPONSE_OK;
175                 }
176                 notification = new ServicePathRpcResultBuilder()
177                         .setNotificationType(ServicePathNotificationTypes.PathComputationRequest)
178                         .setPathDescription(value)
179                         .setStatus(rpcStatusEx).setStatusMessage(message)
180                         .setServiceName(serviceName).build();
181                 sendNotifications(notification);
182                 configurationResponseCommon =
183                         new ConfigurationResponseCommonBuilder().setAckFinalIndicator(ResponseCodes.FINAL_ACK_YES)
184                                 .setRequestId(input.getServiceHandlerHeader().getRequestId())
185                                 .setResponseCode(responseCode)
186                                 .setResponseMessage(message).build();
187                 output = new PathComputationRequestOutputBuilder()
188                         .setConfigurationResponseCommon(configurationResponseCommon).build();
189                 return output;
190             }
191         });
192     }
193
194     private static PathDescription createPathDescription(long azRate, long azWaveLength, long zaRate,
195             long zaWaveLength) {
196         AToZDirection atozDirection =
197                 new AToZDirectionBuilder().setRate(azRate).setAToZWavelengthNumber(azWaveLength).setAToZ(null).build();
198         ZToADirection ztoaDirection =
199                 new ZToADirectionBuilder().setRate(zaRate).setZToAWavelengthNumber(zaWaveLength).setZToA(null).build();
200         PathDescription pathDescription =
201                 new PathDescriptionBuilder().setAToZDirection(atozDirection).setZToADirection(ztoaDirection).build();
202         return pathDescription;
203     }
204
205     public void setPceFailed(Boolean pceFailed) {
206         this.pceFailed = pceFailed;
207     }
208 }