Use Java declarations instead of Google Collections
[netvirt.git] / vpnservice / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / listeners / HwvtepTerminationPointListener.java
1 /*
2  * Copyright (c) 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 package org.opendaylight.netvirt.elan.l2gw.listeners;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.concurrent.ConcurrentHashMap;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
18 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.genius.datastoreutils.hwvtep.HwvtepClusteredDataTreeChangeListener;
21 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
22 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
23 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
24 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
25 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayConnectionUtils;
26 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
27 import org.opendaylight.netvirt.elan.utils.ElanUtils;
28 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
29 import org.opendaylight.netvirt.neutronvpn.api.l2gw.utils.L2GatewayCacheUtils;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.devices.Interfaces;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gateway;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
42 import org.opendaylight.yangtools.concepts.ListenerRegistration;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47
48 /**
49  * Listener for physical locator presence in operational datastore.
50  */
51 public class HwvtepTerminationPointListener
52         extends HwvtepClusteredDataTreeChangeListener<TerminationPoint, HwvtepTerminationPointListener>
53         implements AutoCloseable {
54
55     private static final Logger LOG = LoggerFactory.getLogger(HwvtepTerminationPointListener.class);
56
57     private DataBroker broker;
58     private ListenerRegistration<DataChangeListener> lstnerRegistration;
59     private final ElanL2GatewayUtils elanL2GatewayUtils;
60     private final EntityOwnershipService entityOwnershipService;
61
62     public HwvtepTerminationPointListener(DataBroker broker, ElanUtils elanUtils,
63                                           EntityOwnershipService entityOwnershipService) {
64         super(TerminationPoint.class, HwvtepTerminationPointListener.class);
65
66         this.broker = broker;
67         this.elanL2GatewayUtils = elanUtils.getElanL2GatewayUtils();
68         this.entityOwnershipService = entityOwnershipService;
69         registerListener(LogicalDatastoreType.OPERATIONAL, broker);
70         LOG.debug("created HwvtepTerminationPointListener");
71     }
72
73     static Map<InstanceIdentifier<TerminationPoint>, List<Runnable>> waitingJobsList = new ConcurrentHashMap<>();
74     static Map<InstanceIdentifier<TerminationPoint>, Boolean> teps = new ConcurrentHashMap<>();
75
76     public static void runJobAfterPhysicalLocatorIsAvialable(InstanceIdentifier<TerminationPoint> key,
77             Runnable runnable) {
78         if (teps.get(key) != null) {
79             LOG.debug("physical locator already available {} running job ", key);
80             runnable.run();
81             return;
82         }
83         synchronized (HwvtepTerminationPointListener.class) {
84             List<Runnable> list = waitingJobsList.get(key);
85             if (list == null) {
86                 list = new ArrayList<>();
87                 waitingJobsList.put(key, list);
88             }
89             list.add(runnable);
90             LOG.debug("added the job to wait list of physical locator {}", key);
91         }
92     }
93
94     @Override
95     @SuppressWarnings("checkstyle:IllegalCatch")
96     public void close() throws Exception {
97         if (lstnerRegistration != null) {
98             try {
99                 // TODO use https://git.opendaylight.org/gerrit/#/c/44145/ when merged, and remove @SuppressWarnings
100                 lstnerRegistration.close();
101             } catch (final Exception e) {
102                 LOG.error("Error when cleaning up DataChangeListener.", e);
103             }
104             lstnerRegistration = null;
105         }
106     }
107
108     @Override
109     protected void removed(InstanceIdentifier<TerminationPoint> identifier, TerminationPoint del) {
110         LOG.trace("physical locator removed {}", identifier);
111         teps.remove(identifier);
112     }
113
114     @Override
115     protected void updated(InstanceIdentifier<TerminationPoint> identifier, TerminationPoint original,
116             TerminationPoint update) {
117         LOG.trace("physical locator available {}", identifier);
118     }
119
120     @Override
121     protected void added(InstanceIdentifier<TerminationPoint> identifier, final TerminationPoint add) {
122         final HwvtepPhysicalPortAugmentation portAugmentation =
123                 add.getAugmentation(HwvtepPhysicalPortAugmentation.class);
124         if (portAugmentation != null) {
125             final NodeId nodeId = identifier.firstIdentifierOf(Node.class).firstKeyOf(Node.class).getNodeId();
126             ElanClusterUtils.runOnlyInLeaderNode(entityOwnershipService, HwvtepSouthboundConstants.ELAN_ENTITY_NAME,
127                     "handling Physical Switch add", () -> handlePortAdded(portAugmentation, add, nodeId));
128             return;
129         }
130
131         LOG.trace("physical locator available {}", identifier);
132         teps.put(identifier, true);
133         List<Runnable> runnableList;
134         synchronized (HwvtepTerminationPointListener.class) {
135             runnableList = waitingJobsList.get(identifier);
136             waitingJobsList.remove(identifier);
137         }
138         if (runnableList != null) {
139             LOG.debug("physical locator available {} running jobs ", identifier);
140             runnableList.forEach(Runnable::run);
141         } else {
142             LOG.debug("no jobs are waiting for physical locator {}", identifier);
143         }
144     }
145
146     @Override
147     protected InstanceIdentifier<TerminationPoint> getWildCardPath() {
148         return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class).child(Node.class)
149                 .child(TerminationPoint.class);
150     }
151
152     @Override
153     protected HwvtepTerminationPointListener getDataTreeChangeListener() {
154         return this;
155     }
156
157     private List<ListenableFuture<Void>> handlePortAdded(HwvtepPhysicalPortAugmentation portAugmentation,
158             TerminationPoint portAdded, NodeId psNodeId) {
159         Node psNode = HwvtepUtils.getHwVtepNode(broker, LogicalDatastoreType.OPERATIONAL, psNodeId);
160         if (psNode != null) {
161             String psName = psNode.getAugmentation(PhysicalSwitchAugmentation.class).getHwvtepNodeName().getValue();
162             L2GatewayDevice l2GwDevice = L2GatewayCacheUtils.getL2DeviceFromCache(psName);
163             if (l2GwDevice != null) {
164                 if (isL2GatewayConfigured(l2GwDevice)) {
165                     List<L2gatewayConnection> l2GwConns = L2GatewayConnectionUtils.getAssociatedL2GwConnections(broker,
166                             l2GwDevice.getL2GatewayIds());
167                     if (l2GwConns != null) {
168                         String newPortId = portAdded.getTpId().getValue();
169                         NodeId hwvtepNodeId = new NodeId(l2GwDevice.getHwvtepNodeId());
170                         List<VlanBindings> vlanBindings = getVlanBindings(l2GwConns, hwvtepNodeId, psName, newPortId);
171                         List<ListenableFuture<Void>> futures = new ArrayList<>();
172                         futures.add(elanL2GatewayUtils.updateVlanBindingsInL2GatewayDevice(hwvtepNodeId, psName,
173                                 newPortId, vlanBindings));
174                         return futures;
175                     }
176                 }
177             } else {
178                 LOG.error("{} details are not present in L2Gateway Cache", psName);
179             }
180         } else {
181             LOG.error("{} entry not in config datastore", psNodeId);
182         }
183         return Collections.emptyList();
184     }
185
186     private List<VlanBindings> getVlanBindings(List<L2gatewayConnection> l2GwConns, NodeId hwvtepNodeId, String psName,
187             String newPortId) {
188         List<VlanBindings> vlanBindings = new ArrayList<>();
189         for (L2gatewayConnection l2GwConn : l2GwConns) {
190             L2gateway l2Gateway = L2GatewayConnectionUtils.getNeutronL2gateway(broker, l2GwConn.getL2gatewayId());
191             if (l2Gateway == null) {
192                 LOG.error("L2Gateway with id {} is not present", l2GwConn.getL2gatewayId().getValue());
193             } else {
194                 String logicalSwitchName = ElanL2GatewayUtils.getLogicalSwitchFromElan(
195                         l2GwConn.getNetworkId().getValue());
196                 List<Devices> l2Devices = l2Gateway.getDevices();
197                 for (Devices l2Device : l2Devices) {
198                     String l2DeviceName = l2Device.getDeviceName();
199                     if (l2DeviceName != null && l2DeviceName.equals(psName)) {
200                         for (Interfaces deviceInterface : l2Device.getInterfaces()) {
201                             if (deviceInterface.getInterfaceName().equals(newPortId)) {
202                                 if (deviceInterface.getSegmentationIds() != null
203                                         && !deviceInterface.getSegmentationIds().isEmpty()) {
204                                     for (Integer vlanId : deviceInterface.getSegmentationIds()) {
205                                         vlanBindings.add(HwvtepSouthboundUtils.createVlanBinding(hwvtepNodeId, vlanId,
206                                                 logicalSwitchName));
207                                     }
208                                 } else {
209                                     // Use defaultVlanId (specified in L2GatewayConnection) if Vlan
210                                     // ID not specified at interface level.
211                                     Integer segmentationId = l2GwConn.getSegmentId();
212                                     int defaultVlanId = segmentationId != null ? segmentationId : 0;
213                                     vlanBindings.add(HwvtepSouthboundUtils.createVlanBinding(hwvtepNodeId,
214                                             defaultVlanId, logicalSwitchName));
215                                 }
216                             }
217                         }
218                     }
219                 }
220             }
221         }
222         return vlanBindings;
223     }
224
225     private boolean isL2GatewayConfigured(L2GatewayDevice l2GwDevice) {
226         return l2GwDevice.getHwvtepNodeId() != null && l2GwDevice.isConnected()
227                 && l2GwDevice.getL2GatewayIds().size() > 0 && l2GwDevice.getTunnelIp() != null;
228     }
229 }