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