move vpnservice and cleanup poms
[netvirt.git] / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / listeners / HwvtepRemoteMcastMacListener.java
1 /*
2  * Copyright (c) 2016 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.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;
32
33 /**
34  * The listener class for listening to {@code RemoteMcastMacs}
35  * add/delete/update.
36  *
37  * @see RemoteMcastMacs
38  */
39 public class HwvtepRemoteMcastMacListener
40         extends HwvtepClusteredDataTreeChangeListener<RemoteMcastMacs, HwvtepRemoteMcastMacListener> {
41
42     /** The Constant LOG. */
43     private static final Logger LOG = LoggerFactory.getLogger(HwvtepRemoteMcastMacListener.class);
44
45     /** The node id. */
46     private final NodeId nodeId;
47
48     private final List<IpAddress> expectedPhyLocatorIps;
49
50     private final ElanUtils elanUtils;
51
52     String logicalSwitchName;
53
54     AtomicBoolean executeTask = new AtomicBoolean(true);
55
56     Callable<List<ListenableFuture<Void>>> taskToRun;
57
58     private final JobCoordinator jobCoordinator;
59
60     /**
61      * Instantiates a new remote mcast mac listener.
62      */
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)) {
81                 runTask();
82             }
83         }
84     }
85
86     private boolean isDataPresentInOpDs(InstanceIdentifier<RemoteMcastMacs> path) throws Exception {
87         Optional<RemoteMcastMacs> mac = elanUtils.read2(LogicalDatastoreType.OPERATIONAL, path);
88         if (!mac.isPresent()) {
89             return false;
90         }
91         if (this.expectedPhyLocatorIps != null && !this.expectedPhyLocatorIps.isEmpty()) {
92             RemoteMcastMacs remoteMcastMac = mac.get();
93             if (remoteMcastMac.getLocatorSet() == null || remoteMcastMac.getLocatorSet().isEmpty()) {
94                 return false;
95             }
96             for (IpAddress ip : this.expectedPhyLocatorIps) {
97                 boolean ipExists = ElanL2GatewayUtils.checkIfPhyLocatorAlreadyExistsInRemoteMcastEntry(this.nodeId,
98                         remoteMcastMac, ip);
99                 if (!ipExists) {
100                     LOG.trace("IP [{}] not found in RemoteMcastMacs for node [{}]", String.valueOf(ip.getValue()),
101                             this.nodeId.getValue());
102                     return false;
103                 }
104             }
105         }
106         return true;
107     }
108
109     @Override
110     public InstanceIdentifier<RemoteMcastMacs> getWildCardPath() {
111         return HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
112                 logicalSwitchName, new MacAddress(ElanConstants.UNKNOWN_DMAC));
113     }
114
115     @Override
116     protected HwvtepRemoteMcastMacListener getDataTreeChangeListener() {
117         return this;
118     }
119
120     @Override
121     protected void removed(InstanceIdentifier<RemoteMcastMacs> identifier, RemoteMcastMacs deleted) {
122         LOG.trace("Received Remove DataChange Notification for identifier: {}, RemoteMcastMacs: {}", identifier,
123                 deleted);
124     }
125
126     @Override
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);
131     }
132
133     @Override
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)) {
139             runTask();
140         }
141     }
142
143     @SuppressWarnings("checkstyle:IllegalCatch") // TODO remove when using AutoCloseables
144     void runTask() {
145         try {
146             String jobKey = ElanL2GatewayUtils.getL2GatewayConnectionJobKey(nodeId.getValue(), nodeId.getValue());
147             jobCoordinator.enqueueJob(jobKey, taskToRun,
148                     SystemPropertyReader.getDataStoreJobCoordinatorMaxRetries());
149         } finally {
150             // TODO https://git.opendaylight.org/gerrit/#/c/44145/
151             // AutoCloseables.closeAndLog(this);
152             try {
153                 close();
154             } catch (Exception e) {
155                 LOG.warn("Failed to close McastMacSwitchListener", e);
156             }
157         }
158     }
159 }