47bb1e90b048ff64db6cd07a5fe273e314386414
[netvirt.git] / dhcpservice / impl / src / main / java / org / opendaylight / netvirt / dhcpservice / DhcpMcastMacListener.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.dhcpservice;
9
10 import com.google.common.util.concurrent.MoreExecutors;
11 import java.math.BigInteger;
12 import java.util.List;
13 import java.util.concurrent.TimeUnit;
14 import javax.annotation.Nonnull;
15 import javax.annotation.PreDestroy;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
21 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
22 import org.opendaylight.infrautils.utils.concurrent.Executors;
23 import org.opendaylight.netvirt.dhcpservice.api.DhcpMConstants;
24 import org.opendaylight.serviceutils.tools.mdsal.listener.AbstractAsyncDataTreeChangeListener;
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.opendaylight.params.xml.ns.yang.dhcpservice.config.rev150710.DhcpserviceConfig;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 @Singleton
39 public class DhcpMcastMacListener
40         extends AbstractAsyncDataTreeChangeListener<RemoteMcastMacs> {
41
42     private static final long TIMEOUT_FOR_SHUTDOWN = 30;
43
44     private static final Logger LOG = LoggerFactory.getLogger(DhcpMcastMacListener.class);
45
46     private final DataBroker dataBroker;
47     private final DhcpExternalTunnelManager externalTunnelManager;
48     private final DhcpL2GwUtil dhcpL2GwUtil;
49     private final DhcpserviceConfig config;
50
51     @Inject
52     public DhcpMcastMacListener(DhcpExternalTunnelManager dhcpManager, DhcpL2GwUtil dhcpL2GwUtil, DataBroker dataBroker,
53                                 final DhcpserviceConfig config, HwvtepNodeHACache hwvtepNodeHACache) {
54         super(dataBroker, LogicalDatastoreType.CONFIGURATION,
55               InstanceIdentifier.create(NetworkTopology.class).child(Topology.class,
56                                      new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID)).child(Node.class)
57                       .augmentation(HwvtepGlobalAugmentation.class).child(RemoteMcastMacs.class),
58               Executors.newSingleThreadExecutor("IdPoolListener", LOG));
59         this.externalTunnelManager = dhcpManager;
60         this.dataBroker = dataBroker;
61         this.dhcpL2GwUtil = dhcpL2GwUtil;
62         this.config = config;
63     }
64
65     @Override
66     public void update(@Nonnull InstanceIdentifier<RemoteMcastMacs> identifier, @Nonnull RemoteMcastMacs original,
67                        @Nonnull RemoteMcastMacs update) {
68         // NOOP
69     }
70
71     @Override
72     public void add(@Nonnull InstanceIdentifier<RemoteMcastMacs> identifier, @Nonnull RemoteMcastMacs remoteMcastMacs) {
73         String elanInstanceName = getElanName(remoteMcastMacs);
74         IpAddress tunnelIp = dhcpL2GwUtil.getHwvtepNodeTunnelIp(identifier.firstIdentifierOf(Node.class));
75         if (tunnelIp == null) {
76             LOG.error("Could not find tunnelIp for {}", identifier);
77             return;
78         }
79         List<BigInteger> dpns = DhcpServiceUtils.getListOfDpns(dataBroker);
80         BigInteger designatedDpnId = externalTunnelManager.designateDpnId(tunnelIp, elanInstanceName, dpns);
81         if (designatedDpnId == null || designatedDpnId.equals(DhcpMConstants.INVALID_DPID)) {
82             LOG.error("Unable to designate a DPN for {}", identifier);
83         }
84     }
85
86     @Override
87     public void remove(@Nonnull InstanceIdentifier<RemoteMcastMacs> identifier,
88                        @Nonnull RemoteMcastMacs remoteMcastMacs) {
89         String elanInstanceName = getElanName(remoteMcastMacs);
90         IpAddress tunnelIp = dhcpL2GwUtil.getHwvtepNodeTunnelIp(identifier.firstIdentifierOf(Node.class));
91         if (tunnelIp == null) {
92             LOG.error("Could not find tunnelIp for {}", identifier);
93             return;
94         }
95         BigInteger designatedDpnId = externalTunnelManager.readDesignatedSwitchesForExternalTunnel(
96                 tunnelIp, elanInstanceName);
97         if (designatedDpnId == null) {
98             LOG.error("Could not find designated DPN ID elanInstanceName {}, tunnelIp {}", elanInstanceName, tunnelIp);
99             return;
100         }
101         externalTunnelManager.removeDesignatedSwitchForExternalTunnel(designatedDpnId, tunnelIp, elanInstanceName);
102     }
103
104     @Override
105     @PreDestroy
106     public void close() {
107         if (config.isControllerDhcpEnabled()) {
108             super.close();
109         }
110         MoreExecutors.shutdownAndAwaitTermination(getExecutorService(), TIMEOUT_FOR_SHUTDOWN, TimeUnit.SECONDS);
111     }
112
113     private String getElanName(RemoteMcastMacs mac) {
114         InstanceIdentifier<LogicalSwitches> logicalSwitchIid = (InstanceIdentifier<LogicalSwitches>)
115                 mac.getLogicalSwitchRef().getValue();
116         return logicalSwitchIid.firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue();
117     }
118 }