ELAN: skip remote unicast MACs
[netvirt.git] / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / listeners / HwvtepTerminationPointListener.java
1 /*
2  * Copyright © 2016, 2017 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.base.Optional;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import com.google.common.util.concurrent.SettableFuture;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.List;
19 import javax.annotation.Nonnull;
20 import javax.inject.Inject;
21 import javax.inject.Singleton;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
26 import org.opendaylight.genius.datastoreutils.hwvtep.HwvtepClusteredDataTreeChangeListener;
27 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
28 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
29 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
30 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
31 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayConnectionUtils;
32 import org.opendaylight.netvirt.elan.l2gw.utils.SettableFutureCallback;
33 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
34 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache;
35 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.devices.Interfaces;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gateway;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53
54 /**
55  * Listener for physical locator presence in operational datastore.
56  */
57 @Singleton
58 public class HwvtepTerminationPointListener
59         extends HwvtepClusteredDataTreeChangeListener<TerminationPoint, HwvtepTerminationPointListener> {
60
61     private static final Logger LOG = LoggerFactory.getLogger(HwvtepTerminationPointListener.class);
62
63     private final DataBroker broker;
64     private final ElanL2GatewayUtils elanL2GatewayUtils;
65     private final ElanClusterUtils elanClusterUtils;
66     private final L2GatewayCache l2GatewayCache;
67
68     @Inject
69     public HwvtepTerminationPointListener(DataBroker broker, ElanL2GatewayUtils elanL2GatewayUtils,
70             ElanClusterUtils elanClusterUtils, L2GatewayCache l2GatewayCache) {
71         super(TerminationPoint.class, HwvtepTerminationPointListener.class);
72
73         this.broker = broker;
74         this.elanL2GatewayUtils = elanL2GatewayUtils;
75         this.elanClusterUtils = elanClusterUtils;
76         this.l2GatewayCache = l2GatewayCache;
77         //No longer needed as port reconciliation is added in plugin
78         //registerListener(LogicalDatastoreType.OPERATIONAL, broker);
79         LOG.debug("created HwvtepTerminationPointListener");
80     }
81
82     @Override
83     protected void removed(InstanceIdentifier<TerminationPoint> identifier, TerminationPoint del) {
84         LOG.trace("physical locator removed {}", identifier);
85         final HwvtepPhysicalPortAugmentation portAugmentation =
86                 del.getAugmentation(HwvtepPhysicalPortAugmentation.class);
87         if (portAugmentation != null) {
88             final NodeId nodeId = identifier.firstIdentifierOf(Node.class).firstKeyOf(Node.class).getNodeId();
89             elanClusterUtils.runOnlyInOwnerNode(HwvtepSouthboundConstants.ELAN_ENTITY_NAME,
90                 "Handling Physical port delete",
91                 () -> handlePortDeleted(identifier, portAugmentation, del, nodeId));
92             return;
93         }
94     }
95
96     @Override
97     protected void updated(InstanceIdentifier<TerminationPoint> identifier, TerminationPoint original,
98                            TerminationPoint update) {
99         LOG.trace("physical locator available {}", identifier);
100     }
101
102     @Override
103     protected void added(InstanceIdentifier<TerminationPoint> identifier, final TerminationPoint add) {
104         final HwvtepPhysicalPortAugmentation portAugmentation =
105                 add.getAugmentation(HwvtepPhysicalPortAugmentation.class);
106         if (portAugmentation != null) {
107             final NodeId nodeId = identifier.firstIdentifierOf(Node.class).firstKeyOf(Node.class).getNodeId();
108             elanClusterUtils.runOnlyInOwnerNode(HwvtepSouthboundConstants.ELAN_ENTITY_NAME,
109                 () -> handlePortAdded(portAugmentation, add, nodeId));
110             return;
111         }
112
113         LOG.trace("physical locator available {}", identifier);
114     }
115
116     @Override
117     protected InstanceIdentifier<TerminationPoint> getWildCardPath() {
118         return InstanceIdentifier.create(NetworkTopology.class)
119                 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID)).child(Node.class)
120                 .child(TerminationPoint.class);
121     }
122
123     @Override
124     protected HwvtepTerminationPointListener getDataTreeChangeListener() {
125         return this;
126     }
127
128     private List<ListenableFuture<Void>> handlePortAdded(HwvtepPhysicalPortAugmentation portAugmentation,
129                                                          TerminationPoint portAdded, NodeId psNodeId) {
130         Node psNode = HwvtepUtils.getHwVtepNode(broker, LogicalDatastoreType.OPERATIONAL, psNodeId);
131         if (psNode != null) {
132             String psName = psNode.getAugmentation(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,
143                                     vlanBindings));
144                 }
145             } else {
146                 LOG.error("{} details are not present in L2Gateway Cache", psName);
147             }
148         } else {
149             LOG.error("{} entry not in config datastore", psNodeId);
150         }
151         return Collections.emptyList();
152     }
153
154     private List<ListenableFuture<Void>> handlePortDeleted(InstanceIdentifier<TerminationPoint> identifier,
155                                                            HwvtepPhysicalPortAugmentation portAugmentation,
156                                                            TerminationPoint portDeleted,
157                                                            NodeId psNodeId) throws ReadFailedException {
158         InstanceIdentifier<Node> psNodeIid = identifier.firstIdentifierOf(Node.class);
159         final ReadWriteTransaction tx = broker.newReadWriteTransaction();
160         final SettableFuture<Void> settableFuture = SettableFuture.create();
161         List<ListenableFuture<Void>> futures = Collections.singletonList(settableFuture);
162         Futures.addCallback(tx.read(LogicalDatastoreType.CONFIGURATION, psNodeIid),
163             new FutureCallback<Optional<Node>>() {
164                 @Override
165                 public void onSuccess(@Nonnull Optional<Node> nodeOptional) {
166                     if (nodeOptional.isPresent()) {
167                         //case of port deleted
168                         tx.delete(LogicalDatastoreType.CONFIGURATION, identifier);
169                         Futures.addCallback(tx.submit(), new SettableFutureCallback<>(settableFuture),
170                                 MoreExecutors.directExecutor());
171                     }
172                 }
173
174                 @Override
175                 public void onFailure(Throwable failure) {
176                     LOG.error("Read of {} failed", psNodeIid, failure);
177                     tx.cancel();
178                 }
179             }, MoreExecutors.directExecutor());
180         return futures;
181     }
182
183     private List<VlanBindings> getVlanBindings(List<L2gatewayConnection> l2GwConns, NodeId hwvtepNodeId, String psName,
184                                                String newPortId) {
185         List<VlanBindings> vlanBindings = new ArrayList<>();
186         for (L2gatewayConnection l2GwConn : l2GwConns) {
187             L2gateway l2Gateway = L2GatewayConnectionUtils.getNeutronL2gateway(broker, l2GwConn.getL2gatewayId());
188             if (l2Gateway == null) {
189                 LOG.error("L2Gateway with id {} is not present", l2GwConn.getL2gatewayId().getValue());
190             } else {
191                 String logicalSwitchName = ElanL2GatewayUtils.getLogicalSwitchFromElan(
192                         l2GwConn.getNetworkId().getValue());
193                 List<Devices> l2Devices = l2Gateway.getDevices();
194                 for (Devices l2Device : l2Devices) {
195                     String l2DeviceName = l2Device.getDeviceName();
196                     if (l2DeviceName != null && l2DeviceName.equals(psName)) {
197                         for (Interfaces deviceInterface : l2Device.getInterfaces()) {
198                             if (deviceInterface.getInterfaceName().equals(newPortId)) {
199                                 if (deviceInterface.getSegmentationIds() != null
200                                         && !deviceInterface.getSegmentationIds().isEmpty()) {
201                                     for (Integer vlanId : deviceInterface.getSegmentationIds()) {
202                                         vlanBindings.add(HwvtepSouthboundUtils.createVlanBinding(hwvtepNodeId, vlanId,
203                                                 logicalSwitchName));
204                                     }
205                                 } else {
206                                     // Use defaultVlanId (specified in L2GatewayConnection) if Vlan
207                                     // ID not specified at interface level.
208                                     Integer segmentationId = l2GwConn.getSegmentId();
209                                     int defaultVlanId = segmentationId != null ? segmentationId : 0;
210                                     vlanBindings.add(HwvtepSouthboundUtils.createVlanBinding(hwvtepNodeId,
211                                             defaultVlanId, logicalSwitchName));
212                                 }
213                             }
214                         }
215                     }
216                 }
217             }
218         }
219         return vlanBindings;
220     }
221
222     private boolean isL2GatewayConfigured(L2GatewayDevice l2GwDevice) {
223         return l2GwDevice.getHwvtepNodeId() != null
224                 && !l2GwDevice.getL2GatewayIds().isEmpty() && l2GwDevice.getTunnelIp() != null;
225     }
226 }