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