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