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