BUG:5042 LLDP Tunnel Monitoring should update the interface operational
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / listeners / InterfaceConfigListener.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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
9 package org.opendaylight.vpnservice.interfacemgr.listeners;
10
11 import com.google.common.util.concurrent.ListenableFuture;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.idmanager.IdManager;
14 import org.opendaylight.vpnservice.datastoreutils.AsyncDataTreeChangeListenerBase;
15 import org.opendaylight.vpnservice.datastoreutils.DataStoreJobCoordinator;
16 import org.opendaylight.vpnservice.interfacemgr.renderer.ovs.confighelpers.OvsInterfaceConfigAddHelper;
17 import org.opendaylight.vpnservice.interfacemgr.renderer.ovs.confighelpers.OvsInterfaceConfigRemoveHelper;
18 import org.opendaylight.vpnservice.interfacemgr.renderer.ovs.confighelpers.OvsInterfaceConfigUpdateHelper;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.AlivenessMonitorService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefs;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import java.util.List;
29 import java.util.concurrent.Callable;
30
31 /**
32  * This class listens for interface creation/removal/update in Configuration DS.
33  * This is used to handle interfaces for base of-ports.
34  */
35 public class InterfaceConfigListener extends AsyncDataTreeChangeListenerBase<Interface, InterfaceConfigListener> {
36     private static final Logger LOG = LoggerFactory.getLogger(InterfaceConfigListener.class);
37     private DataBroker dataBroker;
38     private IdManagerService idManager;
39     private AlivenessMonitorService alivenessMonitorService;
40
41     public InterfaceConfigListener(final DataBroker dataBroker, final IdManagerService idManager, final AlivenessMonitorService alivenessMonitorService) {
42         super(Interface.class, InterfaceConfigListener.class);
43         this.dataBroker = dataBroker;
44         this.idManager = idManager;
45         this.alivenessMonitorService = alivenessMonitorService;
46     }
47
48     @Override
49     protected InstanceIdentifier<Interface> getWildCardPath() {
50         return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
51     }
52
53     @Override
54     protected InterfaceConfigListener getDataTreeChangeListener() {
55         return InterfaceConfigListener.this;
56     }
57
58     @Override
59     protected void remove(InstanceIdentifier<Interface> key, Interface interfaceOld) {
60         LOG.debug("Received Interface Remove Event: {}, {}", key, interfaceOld);
61         String ifName = interfaceOld.getName();
62         String parentInterface = null;
63
64         ParentRefs parentRefs = interfaceOld.getAugmentation(ParentRefs.class);
65         if (parentRefs != null) {
66             parentInterface = parentRefs.getParentInterface();
67             if (parentInterface != null && !parentInterface.equals(ifName)) {
68                 return;
69             }
70             if (parentRefs.getDatapathNodeIdentifier() == null) {
71                 return;
72             }
73         }
74
75         DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
76         RendererConfigRemoveWorker configWorker = new RendererConfigRemoveWorker(key, interfaceOld, ifName, parentRefs);
77         coordinator.enqueueJob(ifName, configWorker);
78     }
79
80     @Override
81     protected void update(InstanceIdentifier<Interface> key, Interface interfaceOld, Interface interfaceNew) {
82         LOG.debug("Received Interface Update Event: {}, {}, {}", key, interfaceOld, interfaceNew);
83         String ifNameNew = interfaceNew.getName();
84         String parentInterface = null;
85
86         ParentRefs parentRefs = interfaceNew.getAugmentation(ParentRefs.class);
87         if (parentRefs != null) {
88             parentInterface = parentRefs.getParentInterface();
89         }
90
91         if (parentInterface != null && !parentInterface.equals(ifNameNew)) {
92             return;
93         }
94
95         DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
96         RendererConfigUpdateWorker worker = new RendererConfigUpdateWorker(key, interfaceOld, interfaceNew, ifNameNew);
97         coordinator.enqueueJob(ifNameNew, worker);
98     }
99
100     @Override
101     protected void add(InstanceIdentifier<Interface> key, Interface interfaceNew) {
102         LOG.debug("Received Interface Add Event: {}, {}", key, interfaceNew);
103         String ifName = interfaceNew.getName();
104         String parentInterface = null;
105
106         ParentRefs parentRefs = interfaceNew.getAugmentation(ParentRefs.class);
107         if (parentRefs != null) {
108             parentInterface = parentRefs.getParentInterface();
109         }
110
111         if (parentInterface != null && !parentInterface.equals(ifName)) {
112             return;
113         }
114
115         DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
116         RendererConfigAddWorker configWorker = new RendererConfigAddWorker(key, interfaceNew, parentRefs, ifName);
117         coordinator.enqueueJob(ifName, configWorker);
118     }
119
120     private class RendererConfigAddWorker implements Callable<List<ListenableFuture<Void>>> {
121         InstanceIdentifier<Interface> key;
122         Interface interfaceNew;
123         String portName;
124         ParentRefs parentRefs;
125
126         public RendererConfigAddWorker(InstanceIdentifier<Interface> key, Interface interfaceNew,
127                                        ParentRefs parentRefs, String portName) {
128             this.key = key;
129             this.interfaceNew = interfaceNew;
130             this.portName = portName;
131             this.parentRefs = parentRefs;
132         }
133
134         @Override
135         public List<ListenableFuture<Void>> call() throws Exception {
136             // If another renderer(for eg : CSS) needs to be supported, check can be performed here
137             // to call the respective helpers.
138             return OvsInterfaceConfigAddHelper.addConfiguration(dataBroker, parentRefs, interfaceNew,
139                     idManager);
140         }
141
142         @Override
143         public String toString() {
144             return "RendererConfigAddWorker{" +
145                     "key=" + key +
146                     ", interfaceNew=" + interfaceNew +
147                     ", portName='" + portName + '\'' +
148                     '}';
149         }
150     }
151
152     /**
153      *
154      */
155     private class RendererConfigUpdateWorker implements Callable {
156         InstanceIdentifier<Interface> key;
157         Interface interfaceOld;
158         Interface interfaceNew;
159         String portNameNew;
160
161         public RendererConfigUpdateWorker(InstanceIdentifier<Interface> key, Interface interfaceOld,
162                                           Interface interfaceNew, String portNameNew) {
163             this.key = key;
164             this.interfaceOld = interfaceOld;
165             this.interfaceNew = interfaceNew;
166             this.portNameNew = portNameNew;
167         }
168
169         @Override
170         public List<ListenableFuture<Void>> call() throws Exception {
171             // If another renderer(for eg : CSS) needs to be supported, check can be performed here
172             // to call the respective helpers.
173             return OvsInterfaceConfigUpdateHelper.updateConfiguration(dataBroker, alivenessMonitorService, idManager,
174                     interfaceNew, interfaceOld);
175         }
176
177         @Override
178         public String toString() {
179             return "RendererConfigUpdateWorker{" +
180                     "key=" + key +
181                     ", interfaceOld=" + interfaceOld +
182                     ", interfaceNew=" + interfaceNew +
183                     ", portNameNew='" + portNameNew + '\'' +
184                     '}';
185         }
186     }
187
188     /**
189      *
190      */
191     private class RendererConfigRemoveWorker implements Callable {
192         InstanceIdentifier<Interface> key;
193         Interface interfaceOld;
194         String portName;
195         ParentRefs parentRefs;
196
197         public RendererConfigRemoveWorker(InstanceIdentifier<Interface> key, Interface interfaceOld, String portName,
198                                           ParentRefs parentRefs) {
199             this.key = key;
200             this.interfaceOld = interfaceOld;
201             this.portName = portName;
202             this.parentRefs = parentRefs;
203         }
204
205         @Override
206         public List<ListenableFuture<Void>> call() throws Exception {
207             // If another renderer(for eg : CSS) needs to be supported, check can be performed here
208             // to call the respective helpers.
209             return OvsInterfaceConfigRemoveHelper.removeConfiguration(dataBroker, alivenessMonitorService,
210                     interfaceOld, idManager, parentRefs);
211         }
212
213         @Override
214         public String toString() {
215             return "RendererConfigRemoveWorker{" +
216                     "key=" + key +
217                     ", interfaceOld=" + interfaceOld +
218                     ", portName='" + portName + '\'' +
219                     '}';
220         }
221     }
222 }