68e03731bde59a40c4ef974062e90345329b6e8c
[netvirt.git] / vpnservice / 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.L2GatewayDevice;
35 import org.opendaylight.netvirt.neutronvpn.api.l2gw.utils.L2GatewayCacheUtils;
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
67     @Inject
68     public HwvtepTerminationPointListener(DataBroker broker, ElanL2GatewayUtils elanL2GatewayUtils,
69             ElanClusterUtils elanClusterUtils) {
70         super(TerminationPoint.class, HwvtepTerminationPointListener.class);
71
72         this.broker = broker;
73         this.elanL2GatewayUtils = elanL2GatewayUtils;
74         this.elanClusterUtils = elanClusterUtils;
75         //No longer needed as port reconciliation is added in plugin
76         //registerListener(LogicalDatastoreType.OPERATIONAL, broker);
77         LOG.debug("created HwvtepTerminationPointListener");
78     }
79
80     @Override
81     protected void removed(InstanceIdentifier<TerminationPoint> identifier, TerminationPoint del) {
82         LOG.trace("physical locator removed {}", identifier);
83         final HwvtepPhysicalPortAugmentation portAugmentation =
84                 del.getAugmentation(HwvtepPhysicalPortAugmentation.class);
85         if (portAugmentation != null) {
86             final NodeId nodeId = identifier.firstIdentifierOf(Node.class).firstKeyOf(Node.class).getNodeId();
87             elanClusterUtils.runOnlyInOwnerNode(HwvtepSouthboundConstants.ELAN_ENTITY_NAME,
88                 "Handling Physical port delete",
89                 () -> handlePortDeleted(identifier, portAugmentation, del, nodeId));
90             return;
91         }
92     }
93
94     @Override
95     protected void updated(InstanceIdentifier<TerminationPoint> identifier, TerminationPoint original,
96                            TerminationPoint update) {
97         LOG.trace("physical locator available {}", identifier);
98     }
99
100     @Override
101     protected void added(InstanceIdentifier<TerminationPoint> identifier, final TerminationPoint add) {
102         final HwvtepPhysicalPortAugmentation portAugmentation =
103                 add.getAugmentation(HwvtepPhysicalPortAugmentation.class);
104         if (portAugmentation != null) {
105             final NodeId nodeId = identifier.firstIdentifierOf(Node.class).firstKeyOf(Node.class).getNodeId();
106             elanClusterUtils.runOnlyInOwnerNode(HwvtepSouthboundConstants.ELAN_ENTITY_NAME,
107                 () -> handlePortAdded(portAugmentation, add, nodeId));
108             return;
109         }
110
111         LOG.trace("physical locator available {}", identifier);
112     }
113
114     @Override
115     protected InstanceIdentifier<TerminationPoint> getWildCardPath() {
116         return InstanceIdentifier.create(NetworkTopology.class)
117                 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID)).child(Node.class)
118                 .child(TerminationPoint.class);
119     }
120
121     @Override
122     protected HwvtepTerminationPointListener getDataTreeChangeListener() {
123         return this;
124     }
125
126     private List<ListenableFuture<Void>> handlePortAdded(HwvtepPhysicalPortAugmentation portAugmentation,
127                                                          TerminationPoint portAdded, NodeId psNodeId) {
128         Node psNode = HwvtepUtils.getHwVtepNode(broker, LogicalDatastoreType.OPERATIONAL, psNodeId);
129         if (psNode != null) {
130             String psName = psNode.getAugmentation(PhysicalSwitchAugmentation.class).getHwvtepNodeName().getValue();
131             L2GatewayDevice l2GwDevice = L2GatewayCacheUtils.getL2DeviceFromCache(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 Collections.emptyList();
150     }
151
152     private List<ListenableFuture<Void>> handlePortDeleted(InstanceIdentifier<TerminationPoint> identifier,
153                                                            HwvtepPhysicalPortAugmentation portAugmentation,
154                                                            TerminationPoint portDeleted,
155                                                            NodeId psNodeId) throws ReadFailedException {
156         InstanceIdentifier<Node> psNodeIid = identifier.firstIdentifierOf(Node.class);
157         final ReadWriteTransaction tx = broker.newReadWriteTransaction();
158         final SettableFuture<Void> settableFuture = SettableFuture.create();
159         List<ListenableFuture<Void>> futures = Collections.singletonList(settableFuture);
160         Futures.addCallback(tx.read(LogicalDatastoreType.CONFIGURATION, psNodeIid),
161             new FutureCallback<Optional<Node>>() {
162                 @Override
163                 public void onSuccess(@Nonnull Optional<Node> nodeOptional) {
164                     if (nodeOptional.isPresent()) {
165                         //case of port deleted
166                         tx.delete(LogicalDatastoreType.CONFIGURATION, identifier);
167                         Futures.addCallback(tx.submit(), new SettableFutureCallback<>(settableFuture),
168                                 MoreExecutors.directExecutor());
169                     }
170                 }
171
172                 @Override
173                 public void onFailure(Throwable failure) {
174                     LOG.error("Read of {} failed", psNodeIid, failure);
175                     tx.cancel();
176                 }
177             }, MoreExecutors.directExecutor());
178         return futures;
179     }
180
181     private List<VlanBindings> getVlanBindings(List<L2gatewayConnection> l2GwConns, NodeId hwvtepNodeId, String psName,
182                                                String newPortId) {
183         List<VlanBindings> vlanBindings = new ArrayList<>();
184         for (L2gatewayConnection l2GwConn : l2GwConns) {
185             L2gateway l2Gateway = L2GatewayConnectionUtils.getNeutronL2gateway(broker, l2GwConn.getL2gatewayId());
186             if (l2Gateway == null) {
187                 LOG.error("L2Gateway with id {} is not present", l2GwConn.getL2gatewayId().getValue());
188             } else {
189                 String logicalSwitchName = ElanL2GatewayUtils.getLogicalSwitchFromElan(
190                         l2GwConn.getNetworkId().getValue());
191                 List<Devices> l2Devices = l2Gateway.getDevices();
192                 for (Devices l2Device : l2Devices) {
193                     String l2DeviceName = l2Device.getDeviceName();
194                     if (l2DeviceName != null && l2DeviceName.equals(psName)) {
195                         for (Interfaces deviceInterface : l2Device.getInterfaces()) {
196                             if (deviceInterface.getInterfaceName().equals(newPortId)) {
197                                 if (deviceInterface.getSegmentationIds() != null
198                                         && !deviceInterface.getSegmentationIds().isEmpty()) {
199                                     for (Integer vlanId : deviceInterface.getSegmentationIds()) {
200                                         vlanBindings.add(HwvtepSouthboundUtils.createVlanBinding(hwvtepNodeId, vlanId,
201                                                 logicalSwitchName));
202                                     }
203                                 } else {
204                                     // Use defaultVlanId (specified in L2GatewayConnection) if Vlan
205                                     // ID not specified at interface level.
206                                     Integer segmentationId = l2GwConn.getSegmentId();
207                                     int defaultVlanId = segmentationId != null ? segmentationId : 0;
208                                     vlanBindings.add(HwvtepSouthboundUtils.createVlanBinding(hwvtepNodeId,
209                                             defaultVlanId, logicalSwitchName));
210                                 }
211                             }
212                         }
213                     }
214                 }
215             }
216         }
217         return vlanBindings;
218     }
219
220     private boolean isL2GatewayConfigured(L2GatewayDevice l2GwDevice) {
221         return l2GwDevice.getHwvtepNodeId() != null
222                 && !l2GwDevice.getL2GatewayIds().isEmpty() && l2GwDevice.getTunnelIp() != null;
223     }
224 }