41ac6e991c47908879ae0fe893faef4c0d05adb7
[transportpce.git] / tests / stubpce / src / main / java / org / opendaylight / transportpce / stubpce / impl / StubpceImpl.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.stubpce.impl;
11
12 import java.util.concurrent.Future;
13
14 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
15 import org.opendaylight.transportpce.stubpce.CompliancyCheck;
16 import org.opendaylight.transportpce.stubpce.SendingPceRPCs;
17 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.CancelResourceReserveInput;
18 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.CancelResourceReserveOutput;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.CancelResourceReserveOutputBuilder;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.PathComputationRequestInput;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.PathComputationRequestOutput;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.PathComputationRequestOutputBuilder;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.ServicePathRpcResult;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.ServicePathRpcResultBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.StubpceService;
26 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubpce.rev170426.service.path.rpc.result.PathDescription;
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.transportpce.b.c._interface.service.types.rev170426.RpcStatusEx;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426.ServicePathNotificationTypes;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426.response.parameters.sp.response.parameters.PathDescriptionBuilder;
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
39  * StubpceService
40  * StubpceListener.
41  *
42  * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
43  *
44  */
45
46 public class StubpceImpl implements StubpceService {
47
48     /* Logging. */
49     private static final Logger LOG = LoggerFactory.getLogger(StubpceImpl.class);
50
51     private CompliancyCheck compliancyCheck;
52     /* send notification. */
53     private NotificationPublishService notificationPublishService;
54     private ServicePathRpcResult notification;
55
56     public StubpceImpl(NotificationPublishService notificationPublishService) {
57         this.notificationPublishService = notificationPublishService;
58     }
59
60     @Override
61     public Future<RpcResult<CancelResourceReserveOutput>> cancelResourceReserve(CancelResourceReserveInput input) {
62         LOG.info("RPC cancelResourceReserve  request received");
63         String message = "";
64
65         notification = new ServicePathRpcResultBuilder()
66             .setNotificationType(ServicePathNotificationTypes.CancelResourceReserve)
67             .setServiceName(input.getServiceName())
68             .setStatus(RpcStatusEx.Pending)
69             .setStatusMessage("Service compliant, submitting cancelResourceReserve Request ...")
70             .build();
71         try {
72             notificationPublishService.putNotification(notification);
73         } catch (InterruptedException e) {
74             LOG.info("notification offer rejected : " + e);
75         }
76
77         SendingPceRPCs sendingPCE = new SendingPceRPCs();
78         sendingPCE.cancelResourceReserve();
79         if (sendingPCE.getSuccess()) {
80             message = "ResourceReserve cancelled ! ";
81         } else {
82             message = "Cancelling ResourceReserve failed ! ";
83         }
84         LOG.info(message);
85         ConfigurationResponseCommonBuilder configurationResponseCommon = new ConfigurationResponseCommonBuilder();
86         configurationResponseCommon
87             .setAckFinalIndicator("Yes")
88             .setRequestId(input.getServiceHandlerHeader().getRequestId())
89             .setResponseCode("200")
90             .setResponseMessage("")
91             .setResponseMessage(message);
92         CancelResourceReserveOutputBuilder output  = new CancelResourceReserveOutputBuilder();
93         output
94             .setConfigurationResponseCommon(configurationResponseCommon.build());
95         return RpcResultBuilder.success(output.build()).buildFuture();
96     }
97
98
99     @Override
100     public Future<RpcResult<PathComputationRequestOutput>> pathComputationRequest(PathComputationRequestInput input) {
101         LOG.info("RPC pathcomputation request received");
102         String message = "";
103         PathComputationRequestOutputBuilder output = new PathComputationRequestOutputBuilder();
104         ConfigurationResponseCommonBuilder configurationResponseCommon = new ConfigurationResponseCommonBuilder();
105
106         compliancyCheck = new CompliancyCheck(input);
107         if (!compliancyCheck.check()) {
108             configurationResponseCommon
109                 .setAckFinalIndicator("Yes")
110                 .setRequestId(input.getServiceHandlerHeader().getRequestId())
111                 .setResponseCode("Path not calculated")
112                 .setResponseMessage(compliancyCheck.getMessage());
113
114             output
115                 .setConfigurationResponseCommon(configurationResponseCommon.build())
116                 .setResponseParameters(null);
117
118             return RpcResultBuilder.success(output.build()).buildFuture();
119         }
120         notification = new ServicePathRpcResultBuilder()
121             .setNotificationType(ServicePathNotificationTypes.PathComputationRequest)
122             .setServiceName(input.getServiceName())
123             .setStatus(RpcStatusEx.Pending)
124             .setStatusMessage("Service compliant, submitting pathComputation Request ...")
125             .build();
126         try {
127             notificationPublishService.putNotification(notification);
128         } catch (InterruptedException e) {
129             LOG.info("notification offer rejected : " + e);
130         }
131
132         SendingPceRPCs sendingPCE = new SendingPceRPCs();
133         sendingPCE.pathComputation();
134         if (sendingPCE.getSuccess()) {
135             message = "Path Computated !";
136             ServicePathRpcResultBuilder tmp = new ServicePathRpcResultBuilder()
137                 .setNotificationType(ServicePathNotificationTypes.PathComputationRequest)
138                 .setServiceName(input.getServiceName())
139                 .setStatus(RpcStatusEx.Successful)
140                 .setStatusMessage(message);
141             PathDescriptionBuilder path = sendingPCE.getPathDescription();
142             if (path != null) {
143                 PathDescription pathDescription = new org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce
144                         .stubpce.rev170426.service.path.rpc.result.PathDescriptionBuilder()
145                     .setAToZDirection(path.getAToZDirection())
146                     .setZToADirection(path.getZToADirection())
147                     .build();
148                 tmp.setPathDescription(pathDescription);
149             }
150             notification = tmp.build();
151             try {
152                 notificationPublishService.putNotification(notification);
153             } catch (InterruptedException e) {
154                 LOG.info("notification offer rejected : " + e);
155             }
156         } else {
157             message = "Path Computating failed !";
158         }
159         LOG.info(message);
160         configurationResponseCommon
161             .setAckFinalIndicator("Yes")
162             .setRequestId(input.getServiceHandlerHeader().getRequestId())
163             .setResponseCode("200")
164             .setResponseMessage(message);
165
166         output
167             .setConfigurationResponseCommon(configurationResponseCommon.build());
168         return RpcResultBuilder.success(output.build()).buildFuture();
169
170     }
171 }