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