2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.elan.l2gw.listeners;
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
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;
49 * Listener for physical locator presence in operational datastore.
51 public class HwvtepTerminationPointListener
52 extends HwvtepClusteredDataTreeChangeListener<TerminationPoint, HwvtepTerminationPointListener>
53 implements AutoCloseable {
55 private static final Logger LOG = LoggerFactory.getLogger(HwvtepTerminationPointListener.class);
57 private DataBroker broker;
58 private ListenerRegistration<DataChangeListener> lstnerRegistration;
59 private final ElanL2GatewayUtils elanL2GatewayUtils;
60 private final EntityOwnershipService entityOwnershipService;
62 public HwvtepTerminationPointListener(DataBroker broker, ElanUtils elanUtils,
63 EntityOwnershipService entityOwnershipService) {
64 super(TerminationPoint.class, HwvtepTerminationPointListener.class);
67 this.elanL2GatewayUtils = elanUtils.getElanL2GatewayUtils();
68 this.entityOwnershipService = entityOwnershipService;
69 registerListener(LogicalDatastoreType.OPERATIONAL, broker);
70 LOG.debug("created HwvtepTerminationPointListener");
73 static Map<InstanceIdentifier<TerminationPoint>, List<Runnable>> waitingJobsList = new ConcurrentHashMap<>();
74 static Map<InstanceIdentifier<TerminationPoint>, Boolean> teps = new ConcurrentHashMap<>();
76 public static void runJobAfterPhysicalLocatorIsAvialable(InstanceIdentifier<TerminationPoint> key,
78 if (teps.get(key) != null) {
79 LOG.debug("physical locator already available {} running job ", key);
83 synchronized (HwvtepTerminationPointListener.class) {
84 List<Runnable> list = waitingJobsList.get(key);
86 list = new ArrayList<>();
87 waitingJobsList.put(key, list);
90 LOG.debug("added the job to wait list of physical locator {}", key);
95 @SuppressWarnings("checkstyle:IllegalCatch")
96 public void close() throws Exception {
97 if (lstnerRegistration != null) {
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);
104 lstnerRegistration = null;
109 protected void removed(InstanceIdentifier<TerminationPoint> identifier, TerminationPoint del) {
110 LOG.trace("physical locator removed {}", identifier);
111 teps.remove(identifier);
115 protected void updated(InstanceIdentifier<TerminationPoint> identifier, TerminationPoint original,
116 TerminationPoint update) {
117 LOG.trace("physical locator available {}", identifier);
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));
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);
138 if (runnableList != null) {
139 LOG.debug("physical locator available {} running jobs ", identifier);
140 runnableList.forEach(Runnable::run);
142 LOG.debug("no jobs are waiting for physical locator {}", identifier);
147 protected InstanceIdentifier<TerminationPoint> getWildCardPath() {
148 return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class).child(Node.class)
149 .child(TerminationPoint.class);
153 protected HwvtepTerminationPointListener getDataTreeChangeListener() {
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));
178 LOG.error("{} details are not present in L2Gateway Cache", psName);
181 LOG.error("{} entry not in config datastore", psNodeId);
183 return Collections.emptyList();
186 private List<VlanBindings> getVlanBindings(List<L2gatewayConnection> l2GwConns, NodeId hwvtepNodeId, String psName,
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());
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,
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));
225 private boolean isL2GatewayConfigured(L2GatewayDevice l2GwDevice) {
226 return l2GwDevice.getHwvtepNodeId() != null && l2GwDevice.isConnected()
227 && l2GwDevice.getL2GatewayIds().size() > 0 && l2GwDevice.getTunnelIp() != null;