2 * Copyright (c) 2016 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 com.google.common.base.Optional;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.util.List;
13 import java.util.concurrent.Callable;
14 import java.util.concurrent.atomic.AtomicBoolean;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.genius.datastoreutils.hwvtep.HwvtepClusteredDataTreeChangeListener;
18 import org.opendaylight.genius.utils.SystemPropertyReader;
19 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
20 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
21 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
22 import org.opendaylight.netvirt.elan.utils.ElanConstants;
23 import org.opendaylight.netvirt.elan.utils.ElanUtils;
24 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * The listener class for listening to {@code RemoteMcastMacs}
37 * @see RemoteMcastMacs
39 public class HwvtepRemoteMcastMacListener
40 extends HwvtepClusteredDataTreeChangeListener<RemoteMcastMacs, HwvtepRemoteMcastMacListener> {
42 /** The Constant LOG. */
43 private static final Logger LOG = LoggerFactory.getLogger(HwvtepRemoteMcastMacListener.class);
46 private final NodeId nodeId;
48 private final List<IpAddress> expectedPhyLocatorIps;
50 private final ElanUtils elanUtils;
52 String logicalSwitchName;
54 AtomicBoolean executeTask = new AtomicBoolean(true);
56 Callable<List<ListenableFuture<Void>>> taskToRun;
58 private final JobCoordinator jobCoordinator;
61 * Instantiates a new remote mcast mac listener.
63 public HwvtepRemoteMcastMacListener(DataBroker broker, ElanUtils elanUtils, String logicalSwitchName,
64 L2GatewayDevice l2GatewayDevice,
65 List<IpAddress> expectedPhyLocatorIps,
66 Callable<List<ListenableFuture<Void>>> task,
67 JobCoordinator jobCoordinator) throws Exception {
68 super(RemoteMcastMacs.class, HwvtepRemoteMcastMacListener.class);
69 this.elanUtils = elanUtils;
70 this.nodeId = new NodeId(l2GatewayDevice.getHwvtepNodeId());
71 this.taskToRun = task;
72 this.logicalSwitchName = logicalSwitchName;
73 this.expectedPhyLocatorIps = expectedPhyLocatorIps;
74 this.jobCoordinator = jobCoordinator;
75 LOG.info("registering the listener for mcast mac ");
76 registerListener(LogicalDatastoreType.OPERATIONAL, broker);
77 LOG.info("registered the listener for mcast mac ");
78 if (isDataPresentInOpDs(getWildCardPath())) {
79 LOG.info("mcast mac already present running the task ");
80 if (executeTask.compareAndSet(true, false)) {
86 private boolean isDataPresentInOpDs(InstanceIdentifier<RemoteMcastMacs> path) throws Exception {
87 Optional<RemoteMcastMacs> mac = elanUtils.read2(LogicalDatastoreType.OPERATIONAL, path);
88 if (!mac.isPresent()) {
91 if (this.expectedPhyLocatorIps != null && !this.expectedPhyLocatorIps.isEmpty()) {
92 RemoteMcastMacs remoteMcastMac = mac.get();
93 if (remoteMcastMac.getLocatorSet() == null || remoteMcastMac.getLocatorSet().isEmpty()) {
96 for (IpAddress ip : this.expectedPhyLocatorIps) {
97 boolean ipExists = ElanL2GatewayUtils.checkIfPhyLocatorAlreadyExistsInRemoteMcastEntry(this.nodeId,
100 LOG.trace("IP [{}] not found in RemoteMcastMacs for node [{}]", String.valueOf(ip.getValue()),
101 this.nodeId.getValue());
110 public InstanceIdentifier<RemoteMcastMacs> getWildCardPath() {
111 return HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
112 logicalSwitchName, new MacAddress(ElanConstants.UNKNOWN_DMAC));
116 protected HwvtepRemoteMcastMacListener getDataTreeChangeListener() {
121 protected void removed(InstanceIdentifier<RemoteMcastMacs> identifier, RemoteMcastMacs deleted) {
122 LOG.trace("Received Remove DataChange Notification for identifier: {}, RemoteMcastMacs: {}", identifier,
127 protected void updated(InstanceIdentifier<RemoteMcastMacs> identifier, RemoteMcastMacs old,
128 RemoteMcastMacs newdata) {
129 LOG.trace("Received Update DataChange Notification for identifier: {}, RemoteMcastMacs old: {}, new: {}."
130 + "No Action Performed.", identifier, old, newdata);
134 protected void added(InstanceIdentifier<RemoteMcastMacs> identifier, RemoteMcastMacs mcastMac) {
135 LOG.debug("Received Add DataChange Notification for identifier: {}, RemoteMcastMacs: {}", identifier, mcastMac);
136 // No isDataPresentInOpDs check is done as assuming all the expected phy
137 // locator ips will be available during add
138 if (executeTask.compareAndSet(true, false)) {
143 @SuppressWarnings("checkstyle:IllegalCatch") // TODO remove when using AutoCloseables
146 String jobKey = ElanL2GatewayUtils.getL2GatewayConnectionJobKey(nodeId.getValue(), nodeId.getValue());
147 jobCoordinator.enqueueJob(jobKey, taskToRun,
148 SystemPropertyReader.getDataStoreJobCoordinatorMaxRetries());
150 // TODO https://git.opendaylight.org/gerrit/#/c/44145/
151 // AutoCloseables.closeAndLog(this);
154 } catch (Exception e) {
155 LOG.warn("Failed to close McastMacSwitchListener", e);