Merge "Bug 5064 - Deletion of tunnel interfaces not removing the TEP from switch"
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / listeners / VlanMemberConfigListener.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 package org.opendaylight.vpnservice.interfacemgr.listeners;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.idmanager.IdManager;
13 import org.opendaylight.vpnservice.datastoreutils.AsyncDataTreeChangeListenerBase;
14 import org.opendaylight.vpnservice.datastoreutils.DataStoreJobCoordinator;
15 import org.opendaylight.vpnservice.interfacemgr.renderer.ovs.confighelpers.OvsVlanMemberConfigAddHelper;
16 import org.opendaylight.vpnservice.interfacemgr.renderer.ovs.confighelpers.OvsVlanMemberConfigRemoveHelper;
17 import org.opendaylight.vpnservice.interfacemgr.renderer.ovs.confighelpers.OvsVlanMemberConfigUpdateHelper;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefs;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import java.util.List;
28 import java.util.concurrent.Callable;
29
30 public class VlanMemberConfigListener extends AsyncDataTreeChangeListenerBase<Interface, VlanMemberConfigListener> {
31     private static final Logger LOG = LoggerFactory.getLogger(VlanMemberConfigListener.class);
32     private DataBroker dataBroker;
33     private IdManagerService idManager;
34
35     public VlanMemberConfigListener(final DataBroker dataBroker, final IdManagerService idManager) {
36         super(Interface.class, VlanMemberConfigListener.class);
37         this.dataBroker = dataBroker;
38         this.idManager = idManager;
39     }
40
41     @Override
42     protected InstanceIdentifier<Interface> getWildCardPath() {
43         return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
44     }
45
46     @Override
47     protected void remove(InstanceIdentifier<Interface> key, Interface interfaceOld) {
48         IfL2vlan ifL2vlan = interfaceOld.getAugmentation(IfL2vlan.class);
49         if (ifL2vlan == null) {
50             return;
51         }
52
53         if (ifL2vlan.getL2vlanMode() != IfL2vlan.L2vlanMode.TrunkMember) {
54             return;
55         }
56
57         ParentRefs parentRefs = interfaceOld.getAugmentation(ParentRefs.class);
58         if (parentRefs == null) {
59             LOG.error("Attempt to remove Vlan Trunk-Member {} without a parent interface", interfaceOld);
60             return;
61         }
62
63         String lowerLayerIf = parentRefs.getParentInterface();
64         if (lowerLayerIf.equals(interfaceOld.getName())) {
65             LOG.error("Attempt to remove Vlan Trunk-Member {} with same parent interface name.", interfaceOld);
66             return;
67         }
68
69         DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
70         RendererConfigRemoveWorker removeWorker = new RendererConfigRemoveWorker(key, interfaceOld, parentRefs, ifL2vlan);
71         coordinator.enqueueJob(lowerLayerIf, removeWorker);
72     }
73
74     @Override
75     protected void update(InstanceIdentifier<Interface> key, Interface interfaceOld, Interface interfaceNew) {
76         IfL2vlan ifL2vlanNew = interfaceNew.getAugmentation(IfL2vlan.class);
77         if (ifL2vlanNew == null) {
78             return;
79         }
80         if (ifL2vlanNew.getL2vlanMode() != IfL2vlan.L2vlanMode.TrunkMember) {
81             LOG.error("Configuration Error. Attempt to modify Vlan Mode of interface: {} " +
82                     "to interface: {}", interfaceOld, interfaceNew);
83             return;
84         }
85
86         ParentRefs parentRefsNew = interfaceNew.getAugmentation(ParentRefs.class);
87         if (parentRefsNew == null) {
88             LOG.error("Configuration Error. Attempt to update Vlan Trunk-Member {} without a " +
89                     "parent interface", interfaceNew);
90             return;
91         }
92
93         String lowerLayerIf = parentRefsNew.getParentInterface();
94         if (lowerLayerIf.equals(interfaceNew.getName())) {
95             LOG.error("Configuration Error. Attempt to update Vlan Trunk-Member {} with same parent " +
96                     "interface name.", interfaceNew);
97             return;
98         }
99
100         DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
101         RendererConfigUpdateWorker updateWorker = new RendererConfigUpdateWorker(key, interfaceNew, interfaceOld,
102                 parentRefsNew, ifL2vlanNew);
103         coordinator.enqueueJob(lowerLayerIf, updateWorker);
104     }
105
106     @Override
107     protected void add(InstanceIdentifier<Interface> key, Interface interfaceNew) {
108         IfL2vlan ifL2vlan = interfaceNew.getAugmentation(IfL2vlan.class);
109         if (ifL2vlan == null) {
110             return;
111         }
112
113         if (ifL2vlan.getL2vlanMode() != IfL2vlan.L2vlanMode.TrunkMember) {
114             return;
115         }
116
117         ParentRefs parentRefs = interfaceNew.getAugmentation(ParentRefs.class);
118         if (parentRefs == null) {
119             LOG.error("Attempt to add Vlan Trunk-Member {} without a parent interface", interfaceNew);
120             return;
121         }
122
123         String lowerLayerIf = parentRefs.getParentInterface();
124         if (lowerLayerIf.equals(interfaceNew.getName())) {
125             LOG.error("Attempt to add Vlan Trunk-Member {} with same parent interface name.", interfaceNew);
126             return;
127         }
128
129         DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
130         RendererConfigAddWorker configWorker = new RendererConfigAddWorker(key, interfaceNew, parentRefs, ifL2vlan);
131         coordinator.enqueueJob(lowerLayerIf, configWorker);
132     }
133
134     @Override
135     protected VlanMemberConfigListener getDataTreeChangeListener() {
136         return VlanMemberConfigListener.this;
137     }
138
139     private class RendererConfigAddWorker implements Callable<List<ListenableFuture<Void>>> {
140         InstanceIdentifier<Interface> key;
141         Interface interfaceNew;
142         IfL2vlan ifL2vlan;
143         ParentRefs parentRefs;
144
145         public RendererConfigAddWorker(InstanceIdentifier<Interface> key, Interface interfaceNew,
146                                        ParentRefs parentRefs, IfL2vlan ifL2vlan) {
147             this.key = key;
148             this.interfaceNew = interfaceNew;
149             this.ifL2vlan = ifL2vlan;
150             this.parentRefs = parentRefs;
151         }
152
153         @Override
154         public List<ListenableFuture<Void>> call() throws Exception {
155             // If another renderer(for eg : CSS) needs to be supported, check can be performed here
156             // to call the respective helpers.
157             return OvsVlanMemberConfigAddHelper.addConfiguration(dataBroker, parentRefs, interfaceNew,
158                     ifL2vlan, idManager);
159         }
160     }
161
162     private class RendererConfigUpdateWorker implements Callable<List<ListenableFuture<Void>>> {
163         InstanceIdentifier<Interface> key;
164         Interface interfaceNew;
165         Interface interfaceOld;
166         IfL2vlan ifL2vlanNew;
167         ParentRefs parentRefsNew;
168
169         public RendererConfigUpdateWorker(InstanceIdentifier<Interface> key, Interface interfaceNew,
170                                        Interface interfaceOld, ParentRefs parentRefsNew, IfL2vlan ifL2vlanNew) {
171             this.key = key;
172             this.interfaceNew = interfaceNew;
173             this.interfaceOld = interfaceOld;
174             this.ifL2vlanNew = ifL2vlanNew;
175             this.parentRefsNew = parentRefsNew;
176         }
177
178         @Override
179         public List<ListenableFuture<Void>> call() throws Exception {
180             // If another renderer(for eg : CSS) needs to be supported, check can be performed here
181             // to call the respective helpers.
182             return OvsVlanMemberConfigUpdateHelper.updateConfiguration(dataBroker, parentRefsNew, interfaceOld,
183                     ifL2vlanNew, interfaceNew, idManager);
184         }
185     }
186
187     private class RendererConfigRemoveWorker implements Callable<List<ListenableFuture<Void>>> {
188         InstanceIdentifier<Interface> key;
189         Interface interfaceOld;
190         IfL2vlan ifL2vlan;
191         ParentRefs parentRefs;
192
193         public RendererConfigRemoveWorker(InstanceIdentifier<Interface> key, Interface interfaceOld,
194                                           ParentRefs parentRefs, IfL2vlan ifL2vlan) {
195             this.key = key;
196             this.interfaceOld = interfaceOld;
197             this.ifL2vlan = ifL2vlan;
198             this.parentRefs = parentRefs;
199         }
200
201         @Override
202         public List<ListenableFuture<Void>> call() throws Exception {
203             // If another renderer(for eg : CSS) needs to be supported, check can be performed here
204             // to call the respective helpers.
205             return OvsVlanMemberConfigRemoveHelper.removeConfiguration(dataBroker, parentRefs, interfaceOld,
206                     ifL2vlan, idManager);
207         }
208     }
209 }