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