BUG:5042 LLDP Tunnel Monitoring should update the interface operational
[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.alivenessmonitor.rev150629.AlivenessMonitorService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
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 public class VlanMemberConfigListener extends AsyncDataTreeChangeListenerBase<Interface, VlanMemberConfigListener> {
32     private static final Logger LOG = LoggerFactory.getLogger(VlanMemberConfigListener.class);
33     private DataBroker dataBroker;
34     private IdManagerService idManager;
35     private AlivenessMonitorService alivenessMonitorService;
36
37     public VlanMemberConfigListener(final DataBroker dataBroker, final IdManagerService idManager,
38                                     final AlivenessMonitorService alivenessMonitorService) {
39         super(Interface.class, VlanMemberConfigListener.class);
40         this.dataBroker = dataBroker;
41         this.idManager = idManager;
42         this.alivenessMonitorService = alivenessMonitorService;
43     }
44
45     @Override
46     protected InstanceIdentifier<Interface> getWildCardPath() {
47         return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
48     }
49
50     @Override
51     protected void remove(InstanceIdentifier<Interface> key, Interface interfaceOld) {
52         IfL2vlan ifL2vlan = interfaceOld.getAugmentation(IfL2vlan.class);
53         if (ifL2vlan == null) {
54             return;
55         }
56
57         if (ifL2vlan.getL2vlanMode() != IfL2vlan.L2vlanMode.TrunkMember) {
58             return;
59         }
60
61         ParentRefs parentRefs = interfaceOld.getAugmentation(ParentRefs.class);
62         if (parentRefs == null) {
63             LOG.error("Attempt to remove Vlan Trunk-Member {} without a parent interface", interfaceOld);
64             return;
65         }
66
67         String lowerLayerIf = parentRefs.getParentInterface();
68         if (lowerLayerIf.equals(interfaceOld.getName())) {
69             LOG.error("Attempt to remove Vlan Trunk-Member {} with same parent interface name.", interfaceOld);
70             return;
71         }
72
73         DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
74         RendererConfigRemoveWorker removeWorker = new RendererConfigRemoveWorker(key, interfaceOld, parentRefs, ifL2vlan);
75         coordinator.enqueueJob(lowerLayerIf, removeWorker);
76     }
77
78     @Override
79     protected void update(InstanceIdentifier<Interface> key, Interface interfaceOld, Interface interfaceNew) {
80         IfL2vlan ifL2vlanNew = interfaceNew.getAugmentation(IfL2vlan.class);
81         if (ifL2vlanNew == null) {
82             return;
83         }
84         if (ifL2vlanNew.getL2vlanMode() != IfL2vlan.L2vlanMode.TrunkMember) {
85             LOG.error("Configuration Error. Attempt to modify Vlan Mode of interface: {} " +
86                     "to interface: {}", interfaceOld, interfaceNew);
87             return;
88         }
89
90         ParentRefs parentRefsNew = interfaceNew.getAugmentation(ParentRefs.class);
91         if (parentRefsNew == null) {
92             LOG.error("Configuration Error. Attempt to update Vlan Trunk-Member {} without a " +
93                     "parent interface", interfaceNew);
94             return;
95         }
96
97         String lowerLayerIf = parentRefsNew.getParentInterface();
98         if (lowerLayerIf.equals(interfaceNew.getName())) {
99             LOG.error("Configuration Error. Attempt to update Vlan Trunk-Member {} with same parent " +
100                     "interface name.", interfaceNew);
101             return;
102         }
103
104         DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
105         RendererConfigUpdateWorker updateWorker = new RendererConfigUpdateWorker(key, interfaceNew, interfaceOld,
106                 parentRefsNew, ifL2vlanNew);
107         coordinator.enqueueJob(lowerLayerIf, updateWorker);
108     }
109
110     @Override
111     protected void add(InstanceIdentifier<Interface> key, Interface interfaceNew) {
112         IfL2vlan ifL2vlan = interfaceNew.getAugmentation(IfL2vlan.class);
113         if (ifL2vlan == null) {
114             return;
115         }
116
117         if (ifL2vlan.getL2vlanMode() != IfL2vlan.L2vlanMode.TrunkMember) {
118             return;
119         }
120
121         ParentRefs parentRefs = interfaceNew.getAugmentation(ParentRefs.class);
122         if (parentRefs == null) {
123             LOG.error("Attempt to add Vlan Trunk-Member {} without a parent interface", interfaceNew);
124             return;
125         }
126
127         String lowerLayerIf = parentRefs.getParentInterface();
128         if (lowerLayerIf.equals(interfaceNew.getName())) {
129             LOG.error("Attempt to add Vlan Trunk-Member {} with same parent interface name.", interfaceNew);
130             return;
131         }
132
133         DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
134         RendererConfigAddWorker configWorker = new RendererConfigAddWorker(key, interfaceNew, parentRefs, ifL2vlan);
135         coordinator.enqueueJob(lowerLayerIf, configWorker);
136     }
137
138     @Override
139     protected VlanMemberConfigListener getDataTreeChangeListener() {
140         return VlanMemberConfigListener.this;
141     }
142
143     private class RendererConfigAddWorker implements Callable<List<ListenableFuture<Void>>> {
144         InstanceIdentifier<Interface> key;
145         Interface interfaceNew;
146         IfL2vlan ifL2vlan;
147         ParentRefs parentRefs;
148
149         public RendererConfigAddWorker(InstanceIdentifier<Interface> key, Interface interfaceNew,
150                                        ParentRefs parentRefs, IfL2vlan ifL2vlan) {
151             this.key = key;
152             this.interfaceNew = interfaceNew;
153             this.ifL2vlan = ifL2vlan;
154             this.parentRefs = parentRefs;
155         }
156
157         @Override
158         public List<ListenableFuture<Void>> call() throws Exception {
159             // If another renderer(for eg : CSS) needs to be supported, check can be performed here
160             // to call the respective helpers.
161             return OvsVlanMemberConfigAddHelper.addConfiguration(dataBroker, parentRefs, interfaceNew,
162                     ifL2vlan, idManager);
163         }
164     }
165
166     private class RendererConfigUpdateWorker implements Callable<List<ListenableFuture<Void>>> {
167         InstanceIdentifier<Interface> key;
168         Interface interfaceNew;
169         Interface interfaceOld;
170         IfL2vlan ifL2vlanNew;
171         ParentRefs parentRefsNew;
172
173         public RendererConfigUpdateWorker(InstanceIdentifier<Interface> key, Interface interfaceNew,
174                                        Interface interfaceOld, ParentRefs parentRefsNew, IfL2vlan ifL2vlanNew) {
175             this.key = key;
176             this.interfaceNew = interfaceNew;
177             this.interfaceOld = interfaceOld;
178             this.ifL2vlanNew = ifL2vlanNew;
179             this.parentRefsNew = parentRefsNew;
180         }
181
182         @Override
183         public List<ListenableFuture<Void>> call() throws Exception {
184             // If another renderer(for eg : CSS) needs to be supported, check can be performed here
185             // to call the respective helpers.
186             return OvsVlanMemberConfigUpdateHelper.updateConfiguration(dataBroker, alivenessMonitorService,
187                     parentRefsNew, interfaceOld, ifL2vlanNew, interfaceNew, idManager);
188         }
189     }
190
191     private class RendererConfigRemoveWorker implements Callable<List<ListenableFuture<Void>>> {
192         InstanceIdentifier<Interface> key;
193         Interface interfaceOld;
194         IfL2vlan ifL2vlan;
195         ParentRefs parentRefs;
196
197         public RendererConfigRemoveWorker(InstanceIdentifier<Interface> key, Interface interfaceOld,
198                                           ParentRefs parentRefs, IfL2vlan ifL2vlan) {
199             this.key = key;
200             this.interfaceOld = interfaceOld;
201             this.ifL2vlan = ifL2vlan;
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 OvsVlanMemberConfigRemoveHelper.removeConfiguration(dataBroker, parentRefs, interfaceOld,
210                     ifL2vlan, idManager);
211         }
212     }
213 }