reintroduce SP 1.6 in SH
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / listeners / RendererListenerImpl.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.servicehandler.ServiceInput;
14 import org.opendaylight.transportpce.servicehandler.service.PCEServiceWrapper;
15 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperations;
16 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceRpcResultSp;
17 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.TransportpceRendererListener;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.ServiceNotificationTypes;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.State;
20 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev171016.RpcStatusEx;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Calls to listen to Renderer notifications.
26  *
27  * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
28  *
29  */
30 public class RendererListenerImpl implements TransportpceRendererListener {
31
32     private static final Logger LOG = LoggerFactory.getLogger(RendererListenerImpl.class);
33     private ServiceRpcResultSp serviceRpcResultSp;
34     private ServiceDataStoreOperations serviceDataStoreOperations;
35     private ServiceInput input;
36     private PCEServiceWrapper pceServiceWrapper;
37     private Boolean tempService;
38
39     public RendererListenerImpl(PathComputationService pathComputationService,
40             NotificationPublishService notificationPublishService) {
41         this.pceServiceWrapper = new PCEServiceWrapper(pathComputationService, notificationPublishService);
42         setServiceInput(null);
43         setTempService(false);
44     }
45
46     @Override
47     public void onServiceRpcResultSp(ServiceRpcResultSp notification) {
48         if (!compareServiceRpcResultSp(notification)) {
49             serviceRpcResultSp = notification;
50             String serviceName = serviceRpcResultSp.getServiceName();
51             int notifType = serviceRpcResultSp.getNotificationType().getIntValue();
52             LOG.info("Renderer '{}' Notification received : {}", serviceRpcResultSp.getNotificationType().getName(),
53                     notification);
54             switch (notifType) {
55                 case 3 : /** service-implementation-request. */
56                     if (serviceRpcResultSp.getStatus() == RpcStatusEx.Successful) {
57                         LOG.info("Service implemented !");
58                         OperationResult operationResult = null;
59                         if (tempService) {
60                             operationResult = this.serviceDataStoreOperations.modifyTempService(
61                                     serviceRpcResultSp.getServiceName(),
62                                     State.InService, State.InService);
63                             if (!operationResult.isSuccess()) {
64                                 LOG.warn("Temp Service status not updated in datastore !");
65                             }
66                         } else {
67                             operationResult = this.serviceDataStoreOperations.modifyService(
68                                     serviceRpcResultSp.getServiceName(),
69                                     State.InService, State.InService);
70                             if (!operationResult.isSuccess()) {
71                                 LOG.warn("Service status not updated in datastore !");
72                             }
73                         }
74                     } else if (serviceRpcResultSp.getStatus() == RpcStatusEx.Failed) {
75                         LOG.error("Renderer implementation failed !");
76                         OperationResult deleteServicePathOperationResult =
77                                 this.serviceDataStoreOperations.deleteServicePath(serviceName);
78                         if (!deleteServicePathOperationResult.isSuccess()) {
79                             LOG.warn("Service path was not removed from datastore!");
80                         }
81                         if (tempService) {
82                             OperationResult deleteServiceOperationResult =
83                                     this.serviceDataStoreOperations.deleteTempService(serviceName);
84                             if (!deleteServiceOperationResult.isSuccess()) {
85                                 LOG.warn("Temp Service was not removed from datastore!");
86                             }
87                         } else {
88                             OperationResult deleteServiceOperationResult =
89                                     this.serviceDataStoreOperations.deleteService(serviceName);
90                             if (!deleteServiceOperationResult.isSuccess()) {
91                                 LOG.warn("Service was not removed from datastore!");
92                             }
93                         }
94                     }
95                     break;
96
97                 case 4 : /** service-delete. */
98                     if (serviceRpcResultSp.getStatus() == RpcStatusEx.Successful) {
99                         LOG.info("Service '{}' deleted !", serviceName);
100                         if (this.input != null) {
101                             LOG.info("sending PCE cancel resource reserve for '{}'",  this.input.getServiceName());
102                             this.pceServiceWrapper.cancelPCEResource(this.input.getServiceName(),
103                                     ServiceNotificationTypes.ServiceDeleteResult);
104                         } else {
105                             LOG.error("ServiceInput parameter is null !");
106                         }
107                     } else if (serviceRpcResultSp.getStatus() == RpcStatusEx.Failed) {
108                         LOG.error("Renderer service delete failed !");
109                         return;
110                     }
111                     break;
112                 default:
113                     break;
114             }
115         } else {
116             LOG.warn("ServiceRpcResultSp already wired !");
117         }
118     }
119
120     private Boolean compareServiceRpcResultSp(ServiceRpcResultSp notification) {
121         Boolean result = true;
122         if (serviceRpcResultSp == null) {
123             result = false;
124         } else {
125             if (serviceRpcResultSp.getNotificationType() != notification.getNotificationType()) {
126                 result = false;
127             }
128             if (serviceRpcResultSp.getServiceName() != notification.getServiceName()) {
129                 result = false;
130             }
131             if (serviceRpcResultSp.getStatus() != notification.getStatus()) {
132                 result = false;
133             }
134             if (serviceRpcResultSp.getStatusMessage() != notification.getStatusMessage()) {
135                 result = false;
136             }
137         }
138         return result;
139     }
140
141     public void setServiceInput(ServiceInput serviceInput) {
142         this.input = serviceInput;
143     }
144
145     public void setserviceDataStoreOperations(ServiceDataStoreOperations serviceData) {
146         this.serviceDataStoreOperations = serviceData;
147     }
148
149     public void setTempService(Boolean tempService) {
150         this.tempService = tempService;
151     }
152 }