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.HwvtepHACache;
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;
57 * Listener for physical locator presence in operational datastore.
60 public class HwvtepTerminationPointListener
61 extends HwvtepClusteredDataTreeChangeListener<TerminationPoint, HwvtepTerminationPointListener> {
63 private static final Logger LOG = LoggerFactory.getLogger(HwvtepTerminationPointListener.class);
65 private final DataBroker broker;
66 private final ManagedNewTransactionRunner txRunner;
67 private final ElanL2GatewayUtils elanL2GatewayUtils;
68 private final ElanClusterUtils elanClusterUtils;
69 private final L2GatewayCache l2GatewayCache;
72 public HwvtepTerminationPointListener(DataBroker broker, ElanL2GatewayUtils elanL2GatewayUtils,
73 ElanClusterUtils elanClusterUtils, L2GatewayCache l2GatewayCache) {
74 //super(TerminationPoint.class, HwvtepTerminationPointListener.class, hwvtepNodeHACache);
76 super(broker, DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
77 InstanceIdentifier.create(NetworkTopology.class)
78 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID)).child(Node.class)
79 .child(TerminationPoint.class)),
80 Executors.newListeningSingleThreadExecutor("HwvtepTerminationPointListener", LOG),
81 HwvtepHACache.getInstance());
83 this.txRunner = new ManagedNewTransactionRunnerImpl(broker);
84 this.elanL2GatewayUtils = elanL2GatewayUtils;
85 this.elanClusterUtils = elanClusterUtils;
86 this.l2GatewayCache = l2GatewayCache;
87 //No longer needed as port reconciliation is added in plugin
88 //registerListener(LogicalDatastoreType.OPERATIONAL, broker);
89 LOG.debug("created HwvtepTerminationPointListener");
93 protected void removed(InstanceIdentifier<TerminationPoint> identifier, TerminationPoint del) {
94 LOG.trace("physical locator removed {}", identifier);
95 final HwvtepPhysicalPortAugmentation portAugmentation =
96 del.augmentation(HwvtepPhysicalPortAugmentation.class);
97 if (portAugmentation != null) {
98 elanClusterUtils.runOnlyInOwnerNode(HwvtepSouthboundConstants.ELAN_ENTITY_NAME,
99 "Handling Physical port delete",
100 () -> handlePortDeleted(identifier));
105 protected void updated(InstanceIdentifier<TerminationPoint> identifier, TerminationPoint original,
106 TerminationPoint update) {
107 LOG.trace("physical locator available {}", identifier);
111 protected void added(InstanceIdentifier<TerminationPoint> identifier, final TerminationPoint add) {
112 final HwvtepPhysicalPortAugmentation portAugmentation =
113 add.augmentation(HwvtepPhysicalPortAugmentation.class);
114 if (portAugmentation != null) {
115 final NodeId nodeId = identifier.firstIdentifierOf(Node.class).firstKeyOf(Node.class).getNodeId();
116 elanClusterUtils.runOnlyInOwnerNode(HwvtepSouthboundConstants.ELAN_ENTITY_NAME,
117 () -> handlePortAdded(add, nodeId));
121 LOG.trace("physical locator available {}", identifier);
124 private List<ListenableFuture<?>> handlePortAdded(TerminationPoint portAdded, NodeId psNodeId) {
127 psNode = HwvtepUtils.getHwVtepNode(broker, LogicalDatastoreType.OPERATIONAL, psNodeId);
128 } catch (ExecutionException | InterruptedException e) {
129 LOG.error("Exception while retriving HwVtepNode {}", psNodeId.getValue(), e);
131 if (psNode != null) {
132 String psName = psNode.augmentation(PhysicalSwitchAugmentation.class).getHwvtepNodeName().getValue();
133 L2GatewayDevice l2GwDevice = l2GatewayCache.get(psName);
134 if (l2GwDevice != null) {
135 if (isL2GatewayConfigured(l2GwDevice)) {
136 List<L2gatewayConnection> l2GwConns = L2GatewayConnectionUtils.getAssociatedL2GwConnections(broker,
137 l2GwDevice.getL2GatewayIds());
138 String newPortId = portAdded.getTpId().getValue();
139 NodeId hwvtepNodeId = new NodeId(l2GwDevice.getHwvtepNodeId());
140 List<VlanBindings> vlanBindings = getVlanBindings(l2GwConns, hwvtepNodeId, psName, newPortId);
141 return Collections.singletonList(
142 elanL2GatewayUtils.updateVlanBindingsInL2GatewayDevice(hwvtepNodeId, psName, newPortId,
146 LOG.error("{} details are not present in L2Gateway Cache", psName);
149 LOG.error("{} entry not in config datastore", psNodeId);
154 private List<ListenableFuture<?>> handlePortDeleted(InstanceIdentifier<TerminationPoint> identifier) {
155 InstanceIdentifier<Node> psNodeIid = identifier.firstIdentifierOf(Node.class);
156 return Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
157 tx -> tx.read(psNodeIid).get().ifPresent(node -> tx.delete(identifier))));
160 private List<VlanBindings> getVlanBindings(List<L2gatewayConnection> l2GwConns, NodeId hwvtepNodeId, String psName,
162 List<VlanBindings> vlanBindings = new ArrayList<>();
163 for (L2gatewayConnection l2GwConn : l2GwConns) {
164 L2gateway l2Gateway = L2GatewayConnectionUtils.getNeutronL2gateway(broker, l2GwConn.getL2gatewayId());
165 if (l2Gateway == null) {
166 LOG.error("L2Gateway with id {} is not present", l2GwConn.getL2gatewayId().getValue());
168 String logicalSwitchName = ElanL2GatewayUtils.getLogicalSwitchFromElan(
169 l2GwConn.getNetworkId().getValue());
170 Map<DevicesKey, Devices> l2Devices = l2Gateway.nonnullDevices();
171 for (Devices l2Device : l2Devices.values()) {
172 String l2DeviceName = l2Device.getDeviceName();
173 if (l2DeviceName != null && l2DeviceName.equals(psName)) {
174 for (Interfaces deviceInterface : l2Device.nonnullInterfaces().values()) {
175 if (Objects.equals(deviceInterface.getInterfaceName(), newPortId)) {
176 if (deviceInterface.getSegmentationIds() != null
177 && !deviceInterface.getSegmentationIds().isEmpty()) {
178 for (Integer vlanId : deviceInterface.getSegmentationIds()) {
179 vlanBindings.add(HwvtepSouthboundUtils.createVlanBinding(hwvtepNodeId, vlanId,
183 // Use defaultVlanId (specified in L2GatewayConnection) if Vlan
184 // ID not specified at interface level.
185 Integer segmentationId = l2GwConn.getSegmentId();
186 int defaultVlanId = segmentationId != null ? segmentationId : 0;
187 vlanBindings.add(HwvtepSouthboundUtils.createVlanBinding(hwvtepNodeId,
188 defaultVlanId, logicalSwitchName));
199 private static boolean isL2GatewayConfigured(L2GatewayDevice l2GwDevice) {
200 return l2GwDevice.getHwvtepNodeId() != null
201 && !l2GwDevice.getL2GatewayIds().isEmpty() && l2GwDevice.getTunnelIp() != null;