upgrade models to OpenROADM service 5.1.0
[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.mdsal.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.rev191009.RpcStatusEx;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev191009.response.parameters.sp.ResponseParameters;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev191009.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                 /* path-computation-request. */
63                 case 1:
64                     LOG.info("PCE '{}' Notification received : {}",servicePathRpcResult.getNotificationType().getName(),
65                             notification);
66                     if (servicePathRpcResult.getStatus() == RpcStatusEx.Successful) {
67                         LOG.info("PCE calculation done OK !");
68                         if (servicePathRpcResult.getPathDescription() != null) {
69                             pathDescription = new PathDescriptionBuilder()
70                                     .setAToZDirection(servicePathRpcResult.getPathDescription().getAToZDirection())
71                                 .setZToADirection(servicePathRpcResult.getPathDescription().getZToADirection()).build();
72                             LOG.info("PathDescription gets : {}", pathDescription);
73                             if (!serviceFeasiblity) {
74                                 if (input == null) {
75                                     LOG.error("Input is null !");
76                                     return;
77                                 }
78                                 OperationResult operationResult = null;
79                                 if (tempService) {
80                                     operationResult = this.serviceDataStoreOperations
81                                         .createTempService(input.getTempServiceCreateInput());
82                                     if (!operationResult.isSuccess()) {
83                                         LOG.error("Temp Service not created in datastore !");
84                                     }
85                                 } else {
86                                     operationResult = this.serviceDataStoreOperations
87                                         .createService(input.getServiceCreateInput());
88                                     if (!operationResult.isSuccess()) {
89                                         LOG.error("Service not created in datastore !");
90                                     }
91                                 }
92                                 ResponseParameters responseParameters = new ResponseParametersBuilder()
93                                         .setPathDescription(new org.opendaylight.yang.gen.v1.http.org.transportpce.b.c
94                                                 ._interface.service.types.rev191009.response.parameters.sp.response
95                                                 .parameters.PathDescriptionBuilder(pathDescription).build())
96                                         .build();
97                                 PathComputationRequestOutput pceResponse = new PathComputationRequestOutputBuilder()
98                                         .setResponseParameters(responseParameters).build();
99                                 OperationResult operationServicePathSaveResult =
100                                         this.serviceDataStoreOperations.createServicePath(input, pceResponse);
101                                 if (!operationServicePathSaveResult.isSuccess()) {
102                                     LOG.error("Service Path not created in datastore !");
103                                 }
104                                 ServiceImplementationRequestInput serviceImplementationRequest =
105                                         ModelMappingUtils.createServiceImplementationRequest(input, pathDescription);
106                                 LOG.info("Sending serviceImplementation request : {}", serviceImplementationRequest);
107                                 this.rendererServiceOperations.serviceImplementation(serviceImplementationRequest);
108                             } else {
109                                 LOG.warn("service-feasibility-check RPC ");
110                             }
111                         } else {
112                             LOG.error("'PathDescription' parameter is null ");
113                             return;
114                         }
115                     } else if (servicePathRpcResult.getStatus() == RpcStatusEx.Failed) {
116                         LOG.error("PCE path computation failed !");
117                         return;
118                     }
119                     break;
120                 /* cancel-resource-reserve. */
121                 case 2:
122                     if (servicePathRpcResult.getStatus() == RpcStatusEx.Successful) {
123                         LOG.info("PCE cancel resource done OK !");
124                         OperationResult deleteServicePathOperationResult =
125                                 this.serviceDataStoreOperations.deleteServicePath(input.getServiceName());
126                         if (!deleteServicePathOperationResult.isSuccess()) {
127                             LOG.warn("Service path was not removed from datastore!");
128                         }
129                         OperationResult deleteServiceOperationResult = null;
130                         if (tempService) {
131                             deleteServiceOperationResult =
132                                     this.serviceDataStoreOperations.deleteTempService(input.getServiceName());
133                             if (!deleteServiceOperationResult.isSuccess()) {
134                                 LOG.warn("Service was not removed from datastore!");
135                             }
136                         } else {
137                             deleteServiceOperationResult =
138                                     this.serviceDataStoreOperations.deleteService(input.getServiceName());
139                             if (!deleteServiceOperationResult.isSuccess()) {
140                                 LOG.warn("Service was not removed from datastore!");
141                             }
142                         }
143                         /**
144                          * if it was an RPC serviceReconfigure, re-launch PCR.
145                          */
146                         if (this.serviceReconfigure) {
147                             LOG.info("cancel resource reserve done, relaunching PCE path computation ...");
148                             this.pceServiceWrapper.performPCE(input.getServiceCreateInput(), true);
149                             this.serviceReconfigure = false;
150                         }
151                     } else if (servicePathRpcResult.getStatus() == RpcStatusEx.Failed) {
152                         LOG.info("PCE cancel resource failed !");
153                     }
154                     break;
155                 default:
156                     break;
157             }
158         } else {
159             LOG.warn("ServicePathRpcResult already wired !");
160         }
161     }
162
163     private Boolean compareServicePathRpcResult(ServicePathRpcResult notification) {
164         Boolean result = true;
165         if (servicePathRpcResult == null) {
166             result = false;
167         } else {
168             if (servicePathRpcResult.getNotificationType() != notification.getNotificationType()) {
169                 result = false;
170             }
171             if (servicePathRpcResult.getServiceName() != notification.getServiceName()) {
172                 result = false;
173             }
174             if (servicePathRpcResult.getStatus() != notification.getStatus()) {
175                 result = false;
176             }
177             if (servicePathRpcResult.getStatusMessage() != notification.getStatusMessage()) {
178                 result = false;
179             }
180         }
181         return result;
182     }
183
184     public void setInput(ServiceInput serviceInput) {
185         this.input = serviceInput;
186     }
187
188     public void setServiceReconfigure(Boolean serv) {
189         this.serviceReconfigure = serv;
190     }
191
192     public void setserviceDataStoreOperations(ServiceDataStoreOperations serviceData) {
193         this.serviceDataStoreOperations = serviceData;
194     }
195
196     public void setTempService(Boolean tempService) {
197         this.tempService = tempService;
198     }
199
200     public void setServiceFeasiblity(Boolean serviceFeasiblity) {
201         this.serviceFeasiblity = serviceFeasiblity;
202     }
203
204 }