ELAN: skip remote unicast MACs
[netvirt.git] / dhcpservice / dhcpservice-impl / src / main / java / org / opendaylight / netvirt / dhcpservice / DhcpDesignatedDpnListener.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 java.math.BigInteger;
11 import javax.annotation.PostConstruct;
12 import javax.annotation.PreDestroy;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
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.AsyncClusteredDataTreeChangeListenerBase;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.dhcp.rev160428.DesignatedSwitchesForExternalTunnels;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.dhcp.rev160428.designated.switches._for.external.tunnels.DesignatedSwitchForTunnel;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dhcpservice.api.rev150710.subnet.dhcp.port.data.SubnetToDhcpPort;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dhcpservice.config.rev150710.DhcpserviceConfig;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 @Singleton
28 public class DhcpDesignatedDpnListener
29         extends AsyncClusteredDataTreeChangeListenerBase<DesignatedSwitchForTunnel, DhcpDesignatedDpnListener> {
30
31     private static final Logger LOG = LoggerFactory.getLogger(DhcpDesignatedDpnListener.class);
32     private final DhcpExternalTunnelManager dhcpExternalTunnelManager;
33     private final DataBroker broker;
34     private final DhcpserviceConfig config;
35
36     @Inject
37     public DhcpDesignatedDpnListener(final DhcpExternalTunnelManager dhcpExternalTunnelManager,
38                                      final DataBroker broker,
39                                      final DhcpserviceConfig config) {
40         super(DesignatedSwitchForTunnel.class, DhcpDesignatedDpnListener.class);
41         this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
42         this.broker = broker;
43         this.config = config;
44     }
45
46     @PostConstruct
47     public void init() {
48         if (config.isControllerDhcpEnabled()) {
49             registerListener(LogicalDatastoreType.CONFIGURATION, broker);
50         }
51     }
52
53     @Override
54     @PreDestroy
55     public void close() {
56         super.close();
57         LOG.debug("DhcpDesignatedDpnListener Listener Closed");
58     }
59
60     @Override
61     protected void remove(InstanceIdentifier<DesignatedSwitchForTunnel> identifier, DesignatedSwitchForTunnel del) {
62         LOG.debug("Remove for DesignatedSwitchForTunnel : {}", del);
63         dhcpExternalTunnelManager.removeFromLocalCache(BigInteger.valueOf(del.getDpId()),
64                 del.getTunnelRemoteIpAddress(), del.getElanInstanceName());
65         dhcpExternalTunnelManager.unInstallDhcpFlowsForVms(del.getElanInstanceName(),
66                 del.getTunnelRemoteIpAddress(), DhcpServiceUtils.getListOfDpns(broker));
67         LOG.trace("Removing designated DPN {} DHCP Arp Flows for Elan {}.", del.getDpId(), del.getElanInstanceName());
68         java.util.Optional<SubnetToDhcpPort> subnetDhcpData = dhcpExternalTunnelManager
69                 .getSubnetDhcpPortData(del.getElanInstanceName());
70         if (subnetDhcpData.isPresent()) {
71             dhcpExternalTunnelManager.configureDhcpArpRequestResponseFlow(BigInteger.valueOf(del.getDpId()),
72                     del.getElanInstanceName(), false, del.getTunnelRemoteIpAddress(),
73                     subnetDhcpData.get().getPortFixedip(), subnetDhcpData.get().getPortMacaddress());
74         }
75
76     }
77
78     @Override
79     protected void update(InstanceIdentifier<DesignatedSwitchForTunnel> identifier, DesignatedSwitchForTunnel original,
80             DesignatedSwitchForTunnel update) {
81         LOG.debug("Update for DesignatedSwitchForTunnel original {}, update {}", original, update);
82         dhcpExternalTunnelManager.removeFromLocalCache(BigInteger.valueOf(original.getDpId()),
83                 original.getTunnelRemoteIpAddress(), original.getElanInstanceName());
84         BigInteger designatedDpnId = BigInteger.valueOf(update.getDpId());
85         IpAddress tunnelRemoteIpAddress = update.getTunnelRemoteIpAddress();
86         String elanInstanceName = update.getElanInstanceName();
87         dhcpExternalTunnelManager.updateLocalCache(designatedDpnId, tunnelRemoteIpAddress, elanInstanceName);
88         dhcpExternalTunnelManager.installRemoteMcastMac(designatedDpnId, tunnelRemoteIpAddress, elanInstanceName);
89         java.util.Optional<SubnetToDhcpPort> subnetDhcpData = dhcpExternalTunnelManager
90                 .getSubnetDhcpPortData(elanInstanceName);
91         if (subnetDhcpData.isPresent()) {
92             LOG.trace("Removing Designated DPN {} DHCP Arp Flows for Elan {}.", original.getDpId(),
93                     original.getElanInstanceName());
94             dhcpExternalTunnelManager.configureDhcpArpRequestResponseFlow(BigInteger.valueOf(original.getDpId()),
95                     original.getElanInstanceName(), false, original.getTunnelRemoteIpAddress(),
96                     subnetDhcpData.get().getPortFixedip(), subnetDhcpData.get().getPortMacaddress());
97             LOG.trace("Configuring DHCP Arp Flows for Designated dpn {} Elan {}", designatedDpnId, elanInstanceName);
98             dhcpExternalTunnelManager.configureDhcpArpRequestResponseFlow(designatedDpnId, elanInstanceName,
99                     true, tunnelRemoteIpAddress, subnetDhcpData.get().getPortFixedip(),
100                     subnetDhcpData.get().getPortMacaddress());
101         }
102     }
103
104     @Override
105     protected void add(InstanceIdentifier<DesignatedSwitchForTunnel> identifier, DesignatedSwitchForTunnel add) {
106         LOG.debug("Add for DesignatedSwitchForTunnel : {}", add);
107         BigInteger designatedDpnId = BigInteger.valueOf(add.getDpId());
108         IpAddress tunnelRemoteIpAddress = add.getTunnelRemoteIpAddress();
109         String elanInstanceName = add.getElanInstanceName();
110         dhcpExternalTunnelManager.updateLocalCache(designatedDpnId, tunnelRemoteIpAddress, elanInstanceName);
111         dhcpExternalTunnelManager.installRemoteMcastMac(designatedDpnId, tunnelRemoteIpAddress, elanInstanceName);
112         LOG.trace("Configuring DHCP Arp Flows for Designated dpn {} Elan {}", designatedDpnId, elanInstanceName);
113         java.util.Optional<SubnetToDhcpPort> subnetDhcpData = dhcpExternalTunnelManager
114                 .getSubnetDhcpPortData(elanInstanceName);
115         if (subnetDhcpData.isPresent()) {
116             dhcpExternalTunnelManager.configureDhcpArpRequestResponseFlow(designatedDpnId, elanInstanceName,
117                     true, tunnelRemoteIpAddress, subnetDhcpData.get().getPortFixedip(),
118                     subnetDhcpData.get().getPortMacaddress());
119         }
120     }
121
122     @Override
123     protected InstanceIdentifier<DesignatedSwitchForTunnel> getWildCardPath() {
124         return InstanceIdentifier.create(DesignatedSwitchesForExternalTunnels.class)
125                 .child(DesignatedSwitchForTunnel.class);
126     }
127
128     @Override
129     protected DhcpDesignatedDpnListener getDataTreeChangeListener() {
130         return DhcpDesignatedDpnListener.this;
131     }
132 }