2 * Copyright © 2017 Orange, Inc. and others. All rights reserved.
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
8 package org.opendaylight.transportpce.servicehandler.stub;
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;
40 public class StubPceServiceOperations implements PathComputationService {
42 private static final Logger LOG = LoggerFactory.getLogger(StubPceServiceOperations.class);
43 private final NotificationPublishService notificationPublishService;
44 private Boolean pceFailed;
45 private final ListeningExecutorService executor;
47 public StubPceServiceOperations(NotificationPublishService notificationPublishService) {
48 this.notificationPublishService = notificationPublishService;
49 executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
50 this.pceFailed = false;
53 private void sendNotifications(Notification notif) {
55 LOG.info("putting notification : {}", notif);
56 notificationPublishService.putNotification(notif);
57 } catch (InterruptedException e) {
58 LOG.info("notification offer rejected: ", e);
63 public ListenableFuture<CancelResourceReserveOutput> cancelResourceReserve(CancelResourceReserveInput input) {
64 return executor.submit(new Callable<CancelResourceReserveOutput>() {
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);
75 String responseCode = null;
76 ConfigurationResponseCommon configurationResponseCommon = null;
77 CancelResourceReserveOutput output = null;
79 LOG.info("Wait for 5s til beginning the PCE cancel resource request");
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();
98 LOG.info("forcing pce to fail");
99 message = "pce failed !";
100 rpcStatusEx = RpcStatusEx.Failed;
102 responseCode = ResponseCodes.RESPONSE_FAILED;
104 message = "path computated !";
105 rpcStatusEx = RpcStatusEx.Successful;
107 responseCode = ResponseCodes.RESPONSE_OK;
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();
127 public ListenableFuture<PathComputationRequestOutput> pathComputationRequest(PathComputationRequestInput input) {
128 return executor.submit(new Callable<PathComputationRequestOutput>() {
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);
139 String responseCode = null;
140 ConfigurationResponseCommon configurationResponseCommon = null;
141 PathComputationRequestOutput output = null;
143 LOG.info("Wait for 5s til beginning the PCE pathComputation request");
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();
161 PathDescription value;
164 LOG.info("forcing pce to fail");
165 message = "pce failed !";
166 rpcStatusEx = RpcStatusEx.Failed;
168 responseCode = ResponseCodes.RESPONSE_FAILED;
170 value = createPathDescription(0L, 5L, 0L, 5L);
171 message = "path computated !";
172 rpcStatusEx = RpcStatusEx.Successful;
174 responseCode = ResponseCodes.RESPONSE_OK;
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();
194 private static PathDescription createPathDescription(long azRate, long azWaveLength, long zaRate,
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;
205 public void setPceFailed(Boolean pceFailed) {
206 this.pceFailed = pceFailed;