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