upgrade transportpce-renderer YANG to 1.6.1
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / listeners / PceListenerImpl.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 package org.opendaylight.transportpce.servicehandler.listeners;
9
10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
12 import org.opendaylight.transportpce.common.OperationResult;
13 import org.opendaylight.transportpce.pce.service.PathComputationService;
14 import org.opendaylight.transportpce.renderer.provisiondevice.RendererServiceOperations;
15 import org.opendaylight.transportpce.servicehandler.ModelMappingUtils;
16 import org.opendaylight.transportpce.servicehandler.ServiceInput;
17 import org.opendaylight.transportpce.servicehandler.service.PCEServiceWrapper;
18 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperations;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestOutput;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestOutputBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.ServicePathRpcResult;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.TransportpcePceListener;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.service.path.rpc.result.PathDescription;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.service.path.rpc.result.PathDescriptionBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev200520.ServiceImplementationRequestInput;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.RpcStatusEx;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.response.parameters.sp.ResponseParameters;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.response.parameters.sp.ResponseParametersBuilder;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class PceListenerImpl implements TransportpcePceListener {
33
34     private static final Logger LOG = LoggerFactory.getLogger(PceListenerImpl.class);
35
36     private ServicePathRpcResult servicePathRpcResult;
37     private RendererServiceOperations rendererServiceOperations;
38     private ServiceDataStoreOperations serviceDataStoreOperations;
39     private PCEServiceWrapper pceServiceWrapper;
40     private ServiceInput input;
41     private Boolean serviceReconfigure;
42     private Boolean tempService;
43     private Boolean serviceFeasiblity;
44
45     public PceListenerImpl(RendererServiceOperations rendererServiceOperations,
46             PathComputationService pathComputationService, NotificationPublishService notificationPublishService,
47             ServiceDataStoreOperations serviceDataStoreOperations) {
48         this.rendererServiceOperations = rendererServiceOperations;
49         this.pceServiceWrapper = new PCEServiceWrapper(pathComputationService, notificationPublishService);
50         this.serviceDataStoreOperations = serviceDataStoreOperations;
51         setServiceReconfigure(false);
52         setInput(null);
53         setTempService(false);
54         setServiceFeasiblity(false);
55     }
56
57     @Override
58     public void onServicePathRpcResult(ServicePathRpcResult notification) {
59         if (!compareServicePathRpcResult(notification)) {
60             servicePathRpcResult = notification;
61             PathDescription pathDescription = null;
62             switch (servicePathRpcResult.getNotificationType().getIntValue()) {
63                 /* path-computation-request. */
64                 case 1:
65                     LOG.info("PCE '{}' Notification received : {}",servicePathRpcResult.getNotificationType().getName(),
66                             notification);
67                     if (servicePathRpcResult.getStatus() == RpcStatusEx.Successful) {
68                         LOG.info("PCE calculation done OK !");
69                         if (servicePathRpcResult.getPathDescription() != null) {
70                             pathDescription = new PathDescriptionBuilder()
71                                     .setAToZDirection(servicePathRpcResult.getPathDescription().getAToZDirection())
72                                 .setZToADirection(servicePathRpcResult.getPathDescription().getZToADirection()).build();
73                             LOG.info("PathDescription gets : {}", pathDescription);
74                             if (!serviceFeasiblity) {
75                                 if (input == null) {
76                                     LOG.error("Input is null !");
77                                     return;
78                                 }
79                                 OperationResult operationResult = null;
80                                 if (tempService) {
81                                     operationResult = this.serviceDataStoreOperations
82                                         .createTempService(input.getTempServiceCreateInput());
83                                     if (!operationResult.isSuccess()) {
84                                         LOG.error("Temp Service not created in datastore !");
85                                     }
86                                 } else {
87                                     operationResult = this.serviceDataStoreOperations
88                                         .createService(input.getServiceCreateInput());
89                                     if (!operationResult.isSuccess()) {
90                                         LOG.error("Service not created in datastore !");
91                                     }
92                                 }
93                                 ResponseParameters responseParameters = new ResponseParametersBuilder()
94                                         .setPathDescription(new org.opendaylight.yang.gen.v1.http.org
95                                                 .transportpce.b.c._interface.service.types.rev200128
96                                                 .response.parameters.sp.response.parameters
97                                                 .PathDescriptionBuilder(pathDescription).build())
98                                         .build();
99                                 PathComputationRequestOutput pceResponse = new PathComputationRequestOutputBuilder()
100                                         .setResponseParameters(responseParameters).build();
101                                 OperationResult operationServicePathSaveResult =
102                                         this.serviceDataStoreOperations.createServicePath(input, pceResponse);
103                                 if (!operationServicePathSaveResult.isSuccess()) {
104                                     LOG.error("Service Path not created in datastore !");
105                                 }
106                                 ServiceImplementationRequestInput serviceImplementationRequest =
107                                         ModelMappingUtils.createServiceImplementationRequest(input, pathDescription);
108                                 LOG.info("Sending serviceImplementation request : {}", serviceImplementationRequest);
109                                 this.rendererServiceOperations.serviceImplementation(serviceImplementationRequest);
110                             } else {
111                                 LOG.warn("service-feasibility-check RPC ");
112                             }
113                         } else {
114                             LOG.error("'PathDescription' parameter is null ");
115                             return;
116                         }
117                     } else if (servicePathRpcResult.getStatus() == RpcStatusEx.Failed) {
118                         LOG.error("PCE path computation failed !");
119                         return;
120                     }
121                     break;
122                 /* cancel-resource-reserve. */
123                 case 2:
124                     if (servicePathRpcResult.getStatus() == RpcStatusEx.Successful) {
125                         LOG.info("PCE cancel resource done OK !");
126                         OperationResult deleteServicePathOperationResult =
127                                 this.serviceDataStoreOperations.deleteServicePath(input.getServiceName());
128                         if (!deleteServicePathOperationResult.isSuccess()) {
129                             LOG.warn("Service path was not removed from datastore!");
130                         }
131                         OperationResult deleteServiceOperationResult = null;
132                         if (tempService) {
133                             deleteServiceOperationResult =
134                                     this.serviceDataStoreOperations.deleteTempService(input.getServiceName());
135                             if (!deleteServiceOperationResult.isSuccess()) {
136                                 LOG.warn("Service was not removed from datastore!");
137                             }
138                         } else {
139                             deleteServiceOperationResult =
140                                     this.serviceDataStoreOperations.deleteService(input.getServiceName());
141                             if (!deleteServiceOperationResult.isSuccess()) {
142                                 LOG.warn("Service was not removed from datastore!");
143                             }
144                         }
145                         /**
146                          * if it was an RPC serviceReconfigure, re-launch PCR.
147                          */
148                         if (this.serviceReconfigure) {
149                             LOG.info("cancel resource reserve done, relaunching PCE path computation ...");
150                             this.pceServiceWrapper.performPCE(input.getServiceCreateInput(), true);
151                             this.serviceReconfigure = false;
152                         }
153                     } else if (servicePathRpcResult.getStatus() == RpcStatusEx.Failed) {
154                         LOG.info("PCE cancel resource failed !");
155                     }
156                     break;
157                 default:
158                     break;
159             }
160         } else {
161             LOG.warn("ServicePathRpcResult already wired !");
162         }
163     }
164
165     @SuppressFBWarnings(
166         value = "ES_COMPARING_STRINGS_WITH_EQ",
167         justification = "false positives, not strings but real object references comparisons")
168     private Boolean compareServicePathRpcResult(ServicePathRpcResult notification) {
169         Boolean result = true;
170         if (servicePathRpcResult == null) {
171             result = false;
172         } else {
173             if (servicePathRpcResult.getNotificationType() != notification.getNotificationType()) {
174                 result = false;
175             }
176             if (servicePathRpcResult.getServiceName() != notification.getServiceName()) {
177                 result = false;
178             }
179             if (servicePathRpcResult.getStatus() != notification.getStatus()) {
180                 result = false;
181             }
182             if (servicePathRpcResult.getStatusMessage() != notification.getStatusMessage()) {
183                 result = false;
184             }
185         }
186         return result;
187     }
188
189     public void setInput(ServiceInput serviceInput) {
190         this.input = serviceInput;
191     }
192
193     public void setServiceReconfigure(Boolean serv) {
194         this.serviceReconfigure = serv;
195     }
196
197     public void setserviceDataStoreOperations(ServiceDataStoreOperations serviceData) {
198         this.serviceDataStoreOperations = serviceData;
199     }
200
201     public void setTempService(Boolean tempService) {
202         this.tempService = tempService;
203     }
204
205     public void setServiceFeasiblity(Boolean serviceFeasiblity) {
206         this.serviceFeasiblity = serviceFeasiblity;
207     }
208
209 }