2 * Copyright © 2016, 2017 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 static java.util.Collections.emptyList;
11 import static org.opendaylight.mdsal.binding.util.Datastore.CONFIGURATION;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
18 import java.util.Objects;
19 import java.util.concurrent.ExecutionException;
20 import javax.inject.Inject;
21 import javax.inject.Singleton;
22 import org.opendaylight.genius.datastoreutils.hwvtep.HwvtepClusteredDataTreeChangeListener;
23 import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
24 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
25 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
26 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
27 import org.opendaylight.infrautils.utils.concurrent.Executors;
28 import org.opendaylight.mdsal.binding.api.DataBroker;
29 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
30 import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunner;
31 import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunnerImpl;
32 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
33 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
34 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayConnectionUtils;
35 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
36 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache;
37 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.DevicesKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.devices.Interfaces;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gateway;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
58 * Listener for physical locator presence in operational datastore.
61 public class HwvtepTerminationPointListener
62 extends HwvtepClusteredDataTreeChangeListener<TerminationPoint, HwvtepTerminationPointListener> {
64 private static final Logger LOG = LoggerFactory.getLogger(HwvtepTerminationPointListener.class);
66 private final DataBroker broker;
67 private final ManagedNewTransactionRunner txRunner;
68 private final ElanL2GatewayUtils elanL2GatewayUtils;
69 private final ElanClusterUtils elanClusterUtils;
70 private final L2GatewayCache l2GatewayCache;
73 public HwvtepTerminationPointListener(DataBroker broker, ElanL2GatewayUtils elanL2GatewayUtils,
74 ElanClusterUtils elanClusterUtils, L2GatewayCache l2GatewayCache,
75 HwvtepNodeHACache hwvtepNodeHACache) {
76 //super(TerminationPoint.class, HwvtepTerminationPointListener.class, hwvtepNodeHACache);
78 super(broker, DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
79 InstanceIdentifier.create(NetworkTopology.class)
80 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID)).child(Node.class)
81 .child(TerminationPoint.class)),
82 Executors.newListeningSingleThreadExecutor("HwvtepTerminationPointListener", LOG),
85 this.txRunner = new ManagedNewTransactionRunnerImpl(broker);
86 this.elanL2GatewayUtils = elanL2GatewayUtils;
87 this.elanClusterUtils = elanClusterUtils;
88 this.l2GatewayCache = l2GatewayCache;
89 //No longer needed as port reconciliation is added in plugin
90 //registerListener(LogicalDatastoreType.OPERATIONAL, broker);
91 LOG.debug("created HwvtepTerminationPointListener");
95 protected void removed(InstanceIdentifier<TerminationPoint> identifier, TerminationPoint del) {
96 LOG.trace("physical locator removed {}", identifier);
97 final HwvtepPhysicalPortAugmentation portAugmentation =
98 del.augmentation(HwvtepPhysicalPortAugmentation.class);
99 if (portAugmentation != null) {
100 elanClusterUtils.runOnlyInOwnerNode(HwvtepSouthboundConstants.ELAN_ENTITY_NAME,
101 "Handling Physical port delete",
102 () -> handlePortDeleted(identifier));
107 protected void updated(InstanceIdentifier<TerminationPoint> identifier, TerminationPoint original,
108 TerminationPoint update) {
109 LOG.trace("physical locator available {}", identifier);
113 protected void added(InstanceIdentifier<TerminationPoint> identifier, final TerminationPoint add) {
114 final HwvtepPhysicalPortAugmentation portAugmentation =
115 add.augmentation(HwvtepPhysicalPortAugmentation.class);
116 if (portAugmentation != null) {
117 final NodeId nodeId = identifier.firstIdentifierOf(Node.class).firstKeyOf(Node.class).getNodeId();
118 elanClusterUtils.runOnlyInOwnerNode(HwvtepSouthboundConstants.ELAN_ENTITY_NAME,
119 () -> handlePortAdded(add, nodeId));
123 LOG.trace("physical locator available {}", identifier);
126 private List<ListenableFuture<?>> handlePortAdded(TerminationPoint portAdded, NodeId psNodeId) {
129 psNode = HwvtepUtils.getHwVtepNode(broker, LogicalDatastoreType.OPERATIONAL, psNodeId);
130 } catch (ExecutionException | InterruptedException e) {
131 LOG.error("Exception while retriving HwVtepNode {}", psNodeId.getValue(), e);
133 if (psNode != null) {
134 String psName = psNode.augmentation(PhysicalSwitchAugmentation.class).getHwvtepNodeName().getValue();
135 L2GatewayDevice l2GwDevice = l2GatewayCache.get(psName);
136 if (l2GwDevice != null) {
137 if (isL2GatewayConfigured(l2GwDevice)) {
138 List<L2gatewayConnection> l2GwConns = L2GatewayConnectionUtils.getAssociatedL2GwConnections(broker,
139 l2GwDevice.getL2GatewayIds());
140 String newPortId = portAdded.getTpId().getValue();
141 NodeId hwvtepNodeId = new NodeId(l2GwDevice.getHwvtepNodeId());
142 List<VlanBindings> vlanBindings = getVlanBindings(l2GwConns, hwvtepNodeId, psName, newPortId);
143 return Collections.singletonList(
144 elanL2GatewayUtils.updateVlanBindingsInL2GatewayDevice(hwvtepNodeId, psName, newPortId,
148 LOG.error("{} details are not present in L2Gateway Cache", psName);
151 LOG.error("{} entry not in config datastore", psNodeId);
156 private List<ListenableFuture<?>> handlePortDeleted(InstanceIdentifier<TerminationPoint> identifier) {
157 InstanceIdentifier<Node> psNodeIid = identifier.firstIdentifierOf(Node.class);
158 return Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
159 tx -> tx.read(psNodeIid).get().ifPresent(node -> tx.delete(identifier))));
162 private List<VlanBindings> getVlanBindings(List<L2gatewayConnection> l2GwConns, NodeId hwvtepNodeId, String psName,
164 List<VlanBindings> vlanBindings = new ArrayList<>();
165 for (L2gatewayConnection l2GwConn : l2GwConns) {
166 L2gateway l2Gateway = L2GatewayConnectionUtils.getNeutronL2gateway(broker, l2GwConn.getL2gatewayId());
167 if (l2Gateway == null) {
168 LOG.error("L2Gateway with id {} is not present", l2GwConn.getL2gatewayId().getValue());
170 String logicalSwitchName = ElanL2GatewayUtils.getLogicalSwitchFromElan(
171 l2GwConn.getNetworkId().getValue());
172 Map<DevicesKey, Devices> l2Devices = l2Gateway.nonnullDevices();
173 for (Devices l2Device : l2Devices.values()) {
174 String l2DeviceName = l2Device.getDeviceName();
175 if (l2DeviceName != null && l2DeviceName.equals(psName)) {
176 for (Interfaces deviceInterface : l2Device.nonnullInterfaces().values()) {
177 if (Objects.equals(deviceInterface.getInterfaceName(), newPortId)) {
178 if (deviceInterface.getSegmentationIds() != null
179 && !deviceInterface.getSegmentationIds().isEmpty()) {
180 for (Integer vlanId : deviceInterface.getSegmentationIds()) {
181 vlanBindings.add(HwvtepSouthboundUtils.createVlanBinding(hwvtepNodeId, vlanId,
185 // Use defaultVlanId (specified in L2GatewayConnection) if Vlan
186 // ID not specified at interface level.
187 Integer segmentationId = l2GwConn.getSegmentId();
188 int defaultVlanId = segmentationId != null ? segmentationId : 0;
189 vlanBindings.add(HwvtepSouthboundUtils.createVlanBinding(hwvtepNodeId,
190 defaultVlanId, logicalSwitchName));
201 private static boolean isL2GatewayConfigured(L2GatewayDevice l2GwDevice) {
202 return l2GwDevice.getHwvtepNodeId() != null
203 && !l2GwDevice.getL2GatewayIds().isEmpty() && l2GwDevice.getTunnelIp() != null;