2 * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.elan.l2gw.listeners;
10 import static java.util.stream.Collectors.groupingBy;
11 import static java.util.stream.Collectors.toList;
12 import static java.util.stream.Collectors.toMap;
13 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
15 import com.google.common.base.Optional;
16 import com.google.common.util.concurrent.FutureCallback;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.MoreExecutors;
19 import java.util.List;
22 import java.util.concurrent.TimeUnit;
23 import java.util.function.BiPredicate;
24 import java.util.function.Function;
25 import java.util.function.Predicate;
26 import javax.annotation.PostConstruct;
27 import javax.inject.Inject;
28 import javax.inject.Singleton;
30 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
31 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
32 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
33 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
34 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
35 import org.opendaylight.infrautils.metrics.Counter;
36 import org.opendaylight.infrautils.metrics.Labeled;
37 import org.opendaylight.infrautils.metrics.MetricDescriptor;
38 import org.opendaylight.infrautils.metrics.MetricProvider;
39 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
40 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayConnectionUtils;
41 import org.opendaylight.netvirt.elan.utils.Scheduler;
42 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache;
43 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelIps;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
58 public class L2GatewayConnectionListener extends AsyncClusteredDataTreeChangeListenerBase<L2gatewayConnection,
59 L2GatewayConnectionListener> {
60 private static final Logger LOG = LoggerFactory.getLogger(L2GatewayConnectionListener.class);
61 private static final int MAX_READ_TRIALS = 120;
63 private static final Function<Node, InstanceIdentifier<Node>> TO_GLOBAL_PATH =
64 HwvtepHAUtil::getGlobalNodePathFromPSNode;
66 private static final Function<Node, InstanceIdentifier<Node>> TO_NODE_PATH =
67 (node) -> HwvtepSouthboundUtils.createInstanceIdentifier(node.getNodeId());
69 private static final Function<InstanceIdentifier<Node>, String> GET_DEVICE_NAME = HwvtepHAUtil::getPsName;
71 private static final Predicate<InstanceIdentifier<Node>> IS_PS_NODE = (psIid) ->
72 HwvtepHAUtil.getPsName(psIid) != null;
74 private static final Predicate<Node> IS_HA_PARENT_NODE = (node) -> {
75 HwvtepGlobalAugmentation augmentation = node.augmentation(HwvtepGlobalAugmentation.class);
76 if (augmentation != null && augmentation.getManagers() != null) {
77 return augmentation.getManagers().stream().anyMatch(
78 manager -> manager.key().getTarget().getValue().equals(HwvtepHAUtil.MANAGER_KEY));
83 private static final BiPredicate<InstanceIdentifier<Node>, Node> PS_NODE_OF_PARENT_NODE =
84 (psIid, node) -> psIid.firstKeyOf(Node.class).getNodeId().getValue().contains(node.getNodeId().getValue());
86 private final DataBroker broker;
87 private final L2GatewayConnectionUtils l2GatewayConnectionUtils;
88 private final Scheduler scheduler;
89 private final L2GatewayCache l2GatewayCache;
90 private final Labeled<Labeled<Counter>> elanConnectionsCounter;
93 public L2GatewayConnectionListener(final DataBroker db, L2GatewayConnectionUtils l2GatewayConnectionUtils,
94 Scheduler scheduler, L2GatewayCache l2GatewayCache,
95 MetricProvider metricProvider) {
96 super(L2gatewayConnection.class, L2GatewayConnectionListener.class);
98 this.l2GatewayConnectionUtils = l2GatewayConnectionUtils;
99 this.scheduler = scheduler;
100 this.l2GatewayCache = l2GatewayCache;
101 this.elanConnectionsCounter = metricProvider.newCounter(MetricDescriptor.builder()
102 .anchor(this).project("netvirt").module("l2gw").id("connections").build(), "modification", "elan");
107 loadL2GwDeviceCache(1);
111 protected void add(final InstanceIdentifier<L2gatewayConnection> identifier, final L2gatewayConnection input) {
112 LOG.trace("Adding L2gatewayConnection: {}", input);
113 elanConnectionsCounter
114 .label(DataObjectModification.ModificationType.WRITE.name())
115 .label(input.getNetworkId().getValue()).increment();
116 // Get associated L2GwId from 'input'
117 // Create logical switch in each of the L2GwDevices part of L2Gw
118 // Logical switch name is network UUID
119 // Add L2GwDevices to ELAN
120 l2GatewayConnectionUtils.addL2GatewayConnection(input);
124 protected void remove(InstanceIdentifier<L2gatewayConnection> identifier, L2gatewayConnection input) {
125 LOG.trace("Removing L2gatewayConnection: {}", input);
126 elanConnectionsCounter
127 .label(DataObjectModification.ModificationType.DELETE.name())
128 .label(input.getNetworkId().getValue()).increment();
129 l2GatewayConnectionUtils.deleteL2GatewayConnection(input);
133 protected void update(InstanceIdentifier<L2gatewayConnection> identifier, L2gatewayConnection original,
134 L2gatewayConnection update) {
135 LOG.trace("Updating L2gatewayConnection : original value={}, updated value={}", original, update);
139 protected InstanceIdentifier<L2gatewayConnection> getWildCardPath() {
140 return InstanceIdentifier.create(Neutron.class).child(L2gatewayConnections.class)
141 .child(L2gatewayConnection.class);
145 protected L2GatewayConnectionListener getDataTreeChangeListener() {
149 private void loadL2GwDeviceCache(final int trialNo) {
150 scheduler.getScheduledExecutorService().schedule(() -> {
151 if (trialNo == MAX_READ_TRIALS) {
152 LOG.error("Failed to read config topology");
155 ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
156 InstanceIdentifier<Topology> topoIid = HwvtepSouthboundUtils.createHwvtepTopologyInstanceIdentifier();
157 Futures.addCallback(tx.read(CONFIGURATION, topoIid), new FutureCallback<Optional<Topology>>() {
159 public void onSuccess(Optional<Topology> topologyOptional) {
160 if (topologyOptional != null && topologyOptional.isPresent()) {
161 loadL2GwDeviceCache(topologyOptional.get().getNode());
163 registerListener(CONFIGURATION, broker);
167 public void onFailure(Throwable throwable) {
168 loadL2GwDeviceCache(trialNo + 1);
170 }, MoreExecutors.directExecutor());
172 }, 1, TimeUnit.SECONDS);
175 private void loadL2GwDeviceCache(List<Node> nodes) {
177 LOG.debug("No config topology nodes are present");
180 Map<InstanceIdentifier<Node>, Node> allNodes = nodes
182 .collect(toMap(TO_NODE_PATH, Function.identity()));
184 LOG.trace("Loading all config nodes");
186 Set<InstanceIdentifier<Node>> allIids = allNodes.keySet();
188 Map<String, List<InstanceIdentifier<Node>>> psNodesByDeviceName = allIids
191 .collect(groupingBy(GET_DEVICE_NAME, toList()));
194 allNodes.values().stream()
195 .filter(IS_HA_PARENT_NODE)
196 .forEach(parentNode -> allIids.stream()
198 .filter(psIid -> PS_NODE_OF_PARENT_NODE.test(psIid, parentNode))
199 .forEach(psIid -> addL2DeviceToCache(psIid, parentNode, allNodes.get(psIid))));
201 //Process non HA nodes there will be only one ps node iid for each device for non ha nodes
202 psNodesByDeviceName.values().stream()
203 .filter(psIids -> psIids.size() == 1)
204 .map(psIids -> psIids.get(0))
206 Node psNode = allNodes.get(psIid);
207 Node globalNode = allNodes.get(TO_GLOBAL_PATH.apply(psNode));
208 if (globalNode != null) {
209 addL2DeviceToCache(psIid, globalNode, psNode);
214 void addL2DeviceToCache(InstanceIdentifier<Node> psIid, Node globalNode, Node psNode) {
215 LOG.trace("Adding device to cache {}", psNode.getNodeId().getValue());
216 String deviceName = HwvtepHAUtil.getPsName(psIid);
217 L2GatewayDevice l2GwDevice = l2GatewayCache.addOrGet(deviceName);
218 l2GwDevice.setConnected(true);
219 l2GwDevice.setHwvtepNodeId(globalNode.getNodeId().getValue());
221 List<TunnelIps> tunnelIps = psNode.augmentation(PhysicalSwitchAugmentation.class) != null
222 ? psNode.augmentation(PhysicalSwitchAugmentation.class).getTunnelIps() : null;
223 if (tunnelIps != null) {
224 for (TunnelIps tunnelIp : tunnelIps) {
225 IpAddress tunnelIpAddr = tunnelIp.getTunnelIpsKey();
226 l2GwDevice.addTunnelIp(tunnelIpAddr);