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