ServiceHandler unit testing update
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / service / PathComputationServiceImpl.java
1 /*
2  * Copyright © 2017 AT&T, 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.pce.service;
9
10 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
11 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
12 import org.opendaylight.transportpce.pce.PceComplianceCheck;
13 import org.opendaylight.transportpce.pce.PceComplianceCheckResult;
14 import org.opendaylight.transportpce.pce.PceSendingPceRPCs;
15 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev170426.CancelResourceReserveInput;
16 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev170426.CancelResourceReserveOutput;
17 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev170426.CancelResourceReserveOutputBuilder;
18 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev170426.PathComputationRequestInput;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev170426.PathComputationRequestOutput;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev170426.PathComputationRequestOutputBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev170426.ServicePathRpcResult;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev170426.ServicePathRpcResultBuilder;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev170426.service.path.rpc.result.PathDescription;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.configuration.response.common.ConfigurationResponseCommonBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.AToZDirection;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev170426.path.description.ZToADirection;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426.RpcStatusEx;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426.ServicePathNotificationTypes;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426.response.parameters.sp.ResponseParametersBuilder;
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.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class PathComputationServiceImpl implements PathComputationService {
35
36     private static final Logger LOG = LoggerFactory.getLogger(PathComputationServiceImpl.class);
37
38     private final NotificationPublishService notificationPublishService;
39     private final DataBroker dataBroker;
40
41     public PathComputationServiceImpl(DataBroker dataBroker,
42                                       NotificationPublishService notificationPublishService) {
43         this.notificationPublishService = notificationPublishService;
44         this.dataBroker = dataBroker;
45     }
46
47     public void init() {
48         LOG.info("init ...");
49     }
50
51     public void close() {
52         LOG.info("close.");
53     }
54
55     @Override
56     public CancelResourceReserveOutput cancelResourceReserve(CancelResourceReserveInput input) {
57         LOG.info("cancelResourceReserve");
58         String message = "";
59
60         ServicePathRpcResult notification = new ServicePathRpcResultBuilder()
61                 .setNotificationType(ServicePathNotificationTypes.CancelResourceReserve)
62                 .setServiceName(input.getServiceName())
63                 .setStatus(RpcStatusEx.Pending)
64                 .setStatusMessage("Service compliant, submitting cancelResourceReserve Request ...")
65                 .build();
66         try {
67             notificationPublishService.putNotification(notification);
68         } catch (InterruptedException e) {
69             LOG.info("notification offer rejected : ", e.getMessage());
70         }
71
72         PceSendingPceRPCs sendingPCE = new PceSendingPceRPCs();
73         sendingPCE.cancelResourceReserve();
74         if (sendingPCE.getSuccess()) {
75             message = "ResourceReserve cancelled !";
76         } else {
77             message = "Cancelling ResourceReserve failed !";
78         }
79         LOG.info(message);
80         ConfigurationResponseCommonBuilder configurationResponseCommon = new ConfigurationResponseCommonBuilder();
81         configurationResponseCommon
82                 .setAckFinalIndicator("Yes")
83                 .setRequestId(input.getServiceHandlerHeader().getRequestId())
84                 .setResponseCode("200")
85                 .setResponseMessage("")
86                 .setResponseMessage(message);
87         CancelResourceReserveOutputBuilder output  = new CancelResourceReserveOutputBuilder();
88         output.setConfigurationResponseCommon(configurationResponseCommon.build());
89         return output.build();
90     }
91
92     @Override
93     public PathComputationRequestOutput pathComputationRequest(PathComputationRequestInput input) {
94         LOG.info("pathComputationRequest");
95
96         PathComputationRequestOutputBuilder output = new PathComputationRequestOutputBuilder();
97         ConfigurationResponseCommonBuilder configurationResponseCommon = new ConfigurationResponseCommonBuilder();
98
99         PceComplianceCheckResult check = PceComplianceCheck.check(input);
100         if (!check.hasPassed()) {
101             configurationResponseCommon
102                     .setAckFinalIndicator("Yes")
103                     .setRequestId(input.getServiceHandlerHeader().getRequestId())
104                     .setResponseCode("Path not calculated")
105                     .setResponseMessage(check.getMessage());
106
107
108             output.setConfigurationResponseCommon(configurationResponseCommon.build())
109                     .setResponseParameters(null);
110
111             return output.build();
112         }
113         ServicePathRpcResult notification = new ServicePathRpcResultBuilder()
114                 .setNotificationType(ServicePathNotificationTypes.PathComputationRequest)
115                 .setServiceName(input.getServiceName())
116                 .setStatus(RpcStatusEx.Pending)
117                 .setStatusMessage("Service compliant, submitting pathComputation Request ...")
118                 .build();
119         try {
120             notificationPublishService.putNotification(notification);
121         } catch (InterruptedException e) {
122             LOG.info("notification offer rejected : ", e.getMessage());
123         }
124
125         String message = "";
126         String responseCode = "";
127         PceSendingPceRPCs sendingPCE = new PceSendingPceRPCs(input, dataBroker);
128         sendingPCE.pathComputation();
129         message = sendingPCE.getMessage();
130         responseCode = sendingPCE.getResponseCode();
131         PathDescriptionBuilder path = null;
132         path = sendingPCE.getPathDescription();
133
134         LOG.info("PCE response: {} {}", message, responseCode);
135         if ((sendingPCE.getSuccess() == false) || (path == null)) {
136             configurationResponseCommon
137                     .setAckFinalIndicator("Yes")
138                     .setRequestId(input.getServiceHandlerHeader().getRequestId())
139                     .setResponseCode(responseCode)
140                     .setResponseMessage(message);
141
142             output.setConfigurationResponseCommon(configurationResponseCommon.build());
143             return output.build();
144         }
145
146         // Path calculator returned Success
147         configurationResponseCommon
148                 .setAckFinalIndicator("Yes")
149                 .setRequestId(input.getServiceHandlerHeader().getRequestId())
150                 .setResponseCode(responseCode)
151                 .setResponseMessage(message);
152
153         ServicePathRpcResultBuilder tmp = new ServicePathRpcResultBuilder()
154                 .setNotificationType(ServicePathNotificationTypes.PathComputationRequest)
155                 .setServiceName(input.getServiceName())
156                 .setStatus(RpcStatusEx.Successful)
157                 .setStatusMessage(message);
158         PathDescription pathDescription = new org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce
159                 .pce.rev170426.service.path.rpc.result.PathDescriptionBuilder()
160                 .setAToZDirection(path.getAToZDirection())
161                 .setZToADirection(path.getZToADirection())
162                 .build();
163         tmp.setPathDescription(pathDescription);
164
165         notification = tmp.build();
166         try {
167             notificationPublishService.putNotification(notification);
168         } catch (InterruptedException e) {
169             LOG.error("notification offer rejected : {}", e.getMessage());
170         }
171
172         org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types
173                 .rev170426.response.parameters.sp.response.parameters.PathDescription pathDescription1
174                 = new org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types
175                 .rev170426.response.parameters.sp.response.parameters.PathDescriptionBuilder()
176                 .setAToZDirection(path.getAToZDirection())
177                 .setZToADirection(path.getZToADirection())
178                 .build();
179         ResponseParametersBuilder rpb  = new ResponseParametersBuilder()
180                 .setPathDescription(pathDescription1);
181
182         output.setConfigurationResponseCommon(configurationResponseCommon.build())
183               .setResponseParameters(rpb.build());
184
185         //debug prints
186         AToZDirection atoz = pathDescription.getAToZDirection();
187         if (atoz != null && atoz.getAToZ() != null) {
188             LOG.info("Impl AtoZ Notification: [{}] elements in description", atoz.getAToZ().size());
189             for (int i = 0; i < atoz.getAToZ().size(); i++) {
190                 LOG.info("Impl AtoZ Notification: [{}] {}", i, atoz.getAToZ().get(i));
191             }
192         }
193         ZToADirection ztoa = pathDescription.getZToADirection();
194         if (ztoa != null && ztoa.getZToA() != null) {
195             LOG.info("Impl ZtoA Notification: [{}] elements in description", ztoa.getZToA().size());
196             for (int i = 0; i < ztoa.getZToA().size(); i++) {
197                 LOG.info("Impl ZtoA Notification: [{}] {}", i, ztoa.getZToA().get(i));
198             }
199         }
200         //debug prints
201         return output.build();
202     }
203
204 }