Reintroduce SP 1.6 models in TPCE
[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.rev171017.CancelResourceReserveInput;
16 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.CancelResourceReserveOutput;
17 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.CancelResourceReserveOutputBuilder;
18 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.PathComputationRequestInput;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.PathComputationRequestOutput;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.PathComputationRequestOutputBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.ServicePathRpcResult;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.ServicePathRpcResultBuilder;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.service.path.rpc.result.PathDescription;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.service.path.rpc.result.PathDescriptionBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.configuration.response.common.ConfigurationResponseCommonBuilder;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.AToZDirection;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.ZToADirection;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev171016.RpcStatusEx;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev171016.ServicePathNotificationTypes;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev171016.response.parameters.sp.ResponseParametersBuilder;
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
135         LOG.info("PCE response: {} {}", message, responseCode);
136         if ((sendingPCE.getSuccess() == false) || (path == null)) {
137             configurationResponseCommon
138                     .setAckFinalIndicator("Yes")
139                     .setRequestId(input.getServiceHandlerHeader().getRequestId())
140                     .setResponseCode(responseCode)
141                     .setResponseMessage(message);
142
143             output.setConfigurationResponseCommon(configurationResponseCommon.build());
144             return output.build();
145         }
146
147         // Path calculator returned Success
148         configurationResponseCommon
149                 .setAckFinalIndicator("Yes")
150                 .setRequestId(input.getServiceHandlerHeader().getRequestId())
151                 .setResponseCode(responseCode)
152                 .setResponseMessage(message);
153
154         ServicePathRpcResultBuilder tmp = new ServicePathRpcResultBuilder()
155                 .setNotificationType(ServicePathNotificationTypes.PathComputationRequest)
156                 .setServiceName(input.getServiceName())
157                 .setStatus(RpcStatusEx.Successful)
158                 .setStatusMessage(message);
159         PathDescription pathDescription = new org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce
160                 .pce.rev171017.service.path.rpc.result.PathDescriptionBuilder()
161                 .setAToZDirection(path.getAToZDirection())
162                 .setZToADirection(path.getZToADirection())
163                 .build();
164         tmp.setPathDescription(pathDescription);
165
166         notification = tmp.build();
167         try {
168             notificationPublishService.putNotification(notification);
169         } catch (InterruptedException e) {
170             LOG.error("notification offer rejected : {}", e.getMessage());
171         }
172
173         org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types
174                 .rev171016.response.parameters.sp.response.parameters.PathDescription pathDescription1
175                 = new org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types
176                 .rev171016.response.parameters.sp.response.parameters.PathDescriptionBuilder()
177                 .setAToZDirection(path.getAToZDirection())
178                 .setZToADirection(path.getZToADirection())
179                 .build();
180         ResponseParametersBuilder rpb  = new ResponseParametersBuilder()
181                 .setPathDescription(pathDescription1);
182
183         output.setConfigurationResponseCommon(configurationResponseCommon.build())
184               .setResponseParameters(rpb.build());
185
186         //debug prints
187         AToZDirection atoz = pathDescription.getAToZDirection();
188         if ((atoz != null) && (atoz.getAToZ() != null)) {
189             LOG.info("Impl AtoZ Notification: [{}] elements in description", atoz.getAToZ().size());
190             for (int i = 0; i < atoz.getAToZ().size(); i++) {
191                 LOG.info("Impl AtoZ Notification: [{}] {}", i, atoz.getAToZ().get(i));
192             }
193         }
194         ZToADirection ztoa = pathDescription.getZToADirection();
195         if ((ztoa != null) && (ztoa.getZToA() != null)) {
196             LOG.info("Impl ZtoA Notification: [{}] elements in description", ztoa.getZToA().size());
197             for (int i = 0; i < ztoa.getZToA().size(); i++) {
198                 LOG.info("Impl ZtoA Notification: [{}] {}", i, ztoa.getZToA().get(i));
199             }
200         }
201         //debug prints
202         return output.build();
203     }
204
205 }