Upgrade to Service Path 1.7
[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.rev200128.PathComputationRequestOutput;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestOutputBuilder;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.ServicePathRpcResult;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.TransportpcePceListener;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.service.path.rpc.result.PathDescription;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.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.rev200128.RpcStatusEx;
26 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.response.parameters.sp.ResponseParameters;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.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
94                                                 .transportpce.b.c._interface.service.types.rev200128
95                                                 .response.parameters.sp.response.parameters
96                                                 .PathDescriptionBuilder(pathDescription).build())
97                                         .build();
98                                 PathComputationRequestOutput pceResponse = new PathComputationRequestOutputBuilder()
99                                         .setResponseParameters(responseParameters).build();
100                                 OperationResult operationServicePathSaveResult =
101                                         this.serviceDataStoreOperations.createServicePath(input, pceResponse);
102                                 if (!operationServicePathSaveResult.isSuccess()) {
103                                     LOG.error("Service Path not created in datastore !");
104                                 }
105                                 ServiceImplementationRequestInput serviceImplementationRequest =
106                                         ModelMappingUtils.createServiceImplementationRequest(input, pathDescription);
107                                 LOG.info("Sending serviceImplementation request : {}", serviceImplementationRequest);
108                                 this.rendererServiceOperations.serviceImplementation(serviceImplementationRequest);
109                             } else {
110                                 LOG.warn("service-feasibility-check RPC ");
111                             }
112                         } else {
113                             LOG.error("'PathDescription' parameter is null ");
114                             return;
115                         }
116                     } else if (servicePathRpcResult.getStatus() == RpcStatusEx.Failed) {
117                         LOG.error("PCE path computation failed !");
118                         return;
119                     }
120                     break;
121                 /* cancel-resource-reserve. */
122                 case 2:
123                     if (servicePathRpcResult.getStatus() == RpcStatusEx.Successful) {
124                         LOG.info("PCE cancel resource done OK !");
125                         OperationResult deleteServicePathOperationResult =
126                                 this.serviceDataStoreOperations.deleteServicePath(input.getServiceName());
127                         if (!deleteServicePathOperationResult.isSuccess()) {
128                             LOG.warn("Service path was not removed from datastore!");
129                         }
130                         OperationResult deleteServiceOperationResult = null;
131                         if (tempService) {
132                             deleteServiceOperationResult =
133                                     this.serviceDataStoreOperations.deleteTempService(input.getServiceName());
134                             if (!deleteServiceOperationResult.isSuccess()) {
135                                 LOG.warn("Service was not removed from datastore!");
136                             }
137                         } else {
138                             deleteServiceOperationResult =
139                                     this.serviceDataStoreOperations.deleteService(input.getServiceName());
140                             if (!deleteServiceOperationResult.isSuccess()) {
141                                 LOG.warn("Service was not removed from datastore!");
142                             }
143                         }
144                         /**
145                          * if it was an RPC serviceReconfigure, re-launch PCR.
146                          */
147                         if (this.serviceReconfigure) {
148                             LOG.info("cancel resource reserve done, relaunching PCE path computation ...");
149                             this.pceServiceWrapper.performPCE(input.getServiceCreateInput(), true);
150                             this.serviceReconfigure = false;
151                         }
152                     } else if (servicePathRpcResult.getStatus() == RpcStatusEx.Failed) {
153                         LOG.info("PCE cancel resource failed !");
154                     }
155                     break;
156                 default:
157                     break;
158             }
159         } else {
160             LOG.warn("ServicePathRpcResult already wired !");
161         }
162     }
163
164     private Boolean compareServicePathRpcResult(ServicePathRpcResult notification) {
165         Boolean result = true;
166         if (servicePathRpcResult == null) {
167             result = false;
168         } else {
169             if (servicePathRpcResult.getNotificationType() != notification.getNotificationType()) {
170                 result = false;
171             }
172             if (servicePathRpcResult.getServiceName() != notification.getServiceName()) {
173                 result = false;
174             }
175             if (servicePathRpcResult.getStatus() != notification.getStatus()) {
176                 result = false;
177             }
178             if (servicePathRpcResult.getStatusMessage() != notification.getStatusMessage()) {
179                 result = false;
180             }
181         }
182         return result;
183     }
184
185     public void setInput(ServiceInput serviceInput) {
186         this.input = serviceInput;
187     }
188
189     public void setServiceReconfigure(Boolean serv) {
190         this.serviceReconfigure = serv;
191     }
192
193     public void setserviceDataStoreOperations(ServiceDataStoreOperations serviceData) {
194         this.serviceDataStoreOperations = serviceData;
195     }
196
197     public void setTempService(Boolean tempService) {
198         this.tempService = tempService;
199     }
200
201     public void setServiceFeasiblity(Boolean serviceFeasiblity) {
202         this.serviceFeasiblity = serviceFeasiblity;
203     }
204
205 }