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