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