upgrade transportpce-renderer YANG to 1.6.1
[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                         OperationResult operationResult = null;
62                         if (tempService) {
63                             operationResult = this.serviceDataStoreOperations.modifyTempService(
64                                     serviceRpcResultSp.getServiceName(),
65                                     State.InService, AdminStates.InService);
66                             if (!operationResult.isSuccess()) {
67                                 LOG.warn("Temp Service status not updated in datastore !");
68                             }
69                         } else {
70                             operationResult = this.serviceDataStoreOperations.modifyService(
71                                     serviceRpcResultSp.getServiceName(),
72                                     State.InService, AdminStates.InService);
73                             if (!operationResult.isSuccess()) {
74                                 LOG.warn("Service status not updated in datastore !");
75                             }
76                         }
77                     } else if (serviceRpcResultSp.getStatus() == RpcStatusEx.Failed) {
78                         LOG.error("Renderer implementation failed !");
79                         OperationResult deleteServicePathOperationResult =
80                                 this.serviceDataStoreOperations.deleteServicePath(serviceName);
81                         if (!deleteServicePathOperationResult.isSuccess()) {
82                             LOG.warn("Service path was not removed from datastore!");
83                         }
84                         if (tempService) {
85                             OperationResult deleteServiceOperationResult =
86                                     this.serviceDataStoreOperations.deleteTempService(serviceName);
87                             if (!deleteServiceOperationResult.isSuccess()) {
88                                 LOG.warn("Temp Service was not removed from datastore!");
89                             }
90                         } else {
91                             OperationResult deleteServiceOperationResult =
92                                     this.serviceDataStoreOperations.deleteService(serviceName);
93                             if (!deleteServiceOperationResult.isSuccess()) {
94                                 LOG.warn("Service was not removed from datastore!");
95                             }
96                         }
97                     }
98                     break;
99                 /* service-delete. */
100                 case 4 :
101                     if (serviceRpcResultSp.getStatus() == RpcStatusEx.Successful) {
102                         LOG.info("Service '{}' deleted !", serviceName);
103                         if (this.input != null) {
104                             LOG.info("sending PCE cancel resource reserve for '{}'",  this.input.getServiceName());
105                             this.pceServiceWrapper.cancelPCEResource(this.input.getServiceName(),
106                                     ServiceNotificationTypes.ServiceDeleteResult);
107                         } else {
108                             LOG.error("ServiceInput parameter is null !");
109                         }
110                     } else if (serviceRpcResultSp.getStatus() == RpcStatusEx.Failed) {
111                         LOG.error("Renderer service delete failed !");
112                         return;
113                     }
114                     break;
115                 default:
116                     break;
117             }
118         } else {
119             LOG.warn("ServiceRpcResultSp already wired !");
120         }
121     }
122
123     @SuppressFBWarnings(
124         value = "ES_COMPARING_STRINGS_WITH_EQ",
125         justification = "false positives, not strings but real object references comparisons")
126     private Boolean compareServiceRpcResultSp(ServiceRpcResultSp notification) {
127         Boolean result = true;
128         if (serviceRpcResultSp == null) {
129             result = false;
130         } else {
131             if (serviceRpcResultSp.getNotificationType() != notification.getNotificationType()) {
132                 result = false;
133             }
134             if (serviceRpcResultSp.getServiceName() != notification.getServiceName()) {
135                 result = false;
136             }
137             if (serviceRpcResultSp.getStatus() != notification.getStatus()) {
138                 result = false;
139             }
140             if (serviceRpcResultSp.getStatusMessage() != notification.getStatusMessage()) {
141                 result = false;
142             }
143         }
144         return result;
145     }
146
147     public void setServiceInput(ServiceInput serviceInput) {
148         this.input = serviceInput;
149     }
150
151     public void setserviceDataStoreOperations(ServiceDataStoreOperations serviceData) {
152         this.serviceDataStoreOperations = serviceData;
153     }
154
155     public void setTempService(Boolean tempService) {
156         this.tempService = tempService;
157     }
158 }