PCE module init
[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             output.setConfigurationResponseCommon(configurationResponseCommon.build())
108                     .setResponseParameters(null);
109
110             return output.build();
111         }
112         ServicePathRpcResult notification = new ServicePathRpcResultBuilder()
113                 .setNotificationType(ServicePathNotificationTypes.PathComputationRequest)
114                 .setServiceName(input.getServiceName())
115                 .setStatus(RpcStatusEx.Pending)
116                 .setStatusMessage("Service compliant, submitting pathComputation Request ...")
117                 .build();
118         try {
119             notificationPublishService.putNotification(notification);
120         } catch (InterruptedException e) {
121             LOG.info("notification offer rejected : ", e.getMessage());
122         }
123
124         String message = "";
125         String responseCode = "";
126         PceSendingPceRPCs sendingPCE = new PceSendingPceRPCs(input, dataBroker);
127         sendingPCE.pathComputation();
128         message = sendingPCE.getMessage();
129         responseCode = sendingPCE.getResponseCode();
130         PathDescriptionBuilder path = null;
131         path = sendingPCE.getPathDescription();
132
133         LOG.info("PCE response: {} {}", message, responseCode);
134         if ((sendingPCE.getSuccess() == false) || (path == null)) {
135             configurationResponseCommon
136                     .setAckFinalIndicator("Yes")
137                     .setRequestId(input.getServiceHandlerHeader().getRequestId())
138                     .setResponseCode(responseCode)
139                     .setResponseMessage(message);
140
141             output.setConfigurationResponseCommon(configurationResponseCommon.build());
142             return output.build();
143         }
144
145         // Path calculator returned Success
146         configurationResponseCommon
147                 .setAckFinalIndicator("Yes")
148                 .setRequestId(input.getServiceHandlerHeader().getRequestId())
149                 .setResponseCode(responseCode)
150                 .setResponseMessage(message);
151
152         ServicePathRpcResultBuilder tmp = new ServicePathRpcResultBuilder()
153                 .setNotificationType(ServicePathNotificationTypes.PathComputationRequest)
154                 .setServiceName(input.getServiceName())
155                 .setStatus(RpcStatusEx.Successful)
156                 .setStatusMessage(message);
157         PathDescription pathDescription = new org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce
158                 .pce.rev170426.service.path.rpc.result.PathDescriptionBuilder()
159                 .setAToZDirection(path.getAToZDirection())
160                 .setZToADirection(path.getZToADirection())
161                 .build();
162         tmp.setPathDescription(pathDescription);
163
164         notification = tmp.build();
165         try {
166             notificationPublishService.putNotification(notification);
167         } catch (InterruptedException e) {
168             LOG.error("notification offer rejected : {}", e.getMessage());
169         }
170
171         org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types
172                 .rev170426.response.parameters.sp.response.parameters.PathDescription pathDescription1
173                 = new org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types
174                 .rev170426.response.parameters.sp.response.parameters.PathDescriptionBuilder()
175                 .setAToZDirection(path.getAToZDirection())
176                 .setZToADirection(path.getZToADirection())
177                 .build();
178         ResponseParametersBuilder rpb  = new ResponseParametersBuilder()
179                 .setPathDescription(pathDescription1);
180
181         output.setConfigurationResponseCommon(configurationResponseCommon.build())
182               .setResponseParameters(rpb.build());
183
184         //debug prints
185         AToZDirection atoz = pathDescription.getAToZDirection();
186         if (atoz != null && atoz.getAToZ() != null) {
187             LOG.info("Impl AtoZ Notification: [{}] elements in description", atoz.getAToZ().size());
188             for (int i = 0; i < atoz.getAToZ().size(); i++) {
189                 LOG.info("Impl AtoZ Notification: [{}] {}", i, atoz.getAToZ().get(i));
190             }
191         }
192         ZToADirection ztoa = pathDescription.getZToADirection();
193         if (ztoa != null && ztoa.getZToA() != null) {
194             LOG.info("Impl ZtoA Notification: [{}] elements in description", ztoa.getZToA().size());
195             for (int i = 0; i < ztoa.getZToA().size(); i++) {
196                 LOG.info("Impl ZtoA Notification: [{}] {}", i, ztoa.getZToA().get(i));
197             }
198         }
199         //debug prints
200         return output.build();
201     }
202
203 }