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