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