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