188f9461ce1bd64f204aa8c2b3f67b95d94d932c
[netvirt.git] / 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 javax.annotation.PostConstruct;
11 import javax.annotation.PreDestroy;
12 import javax.inject.Inject;
13 import javax.inject.Singleton;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.dhcp.rev160428.DesignatedSwitchesForExternalTunnels;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.dhcp.rev160428.designated.switches._for.external.tunnels.DesignatedSwitchForTunnel;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dhcpservice.api.rev150710.subnet.dhcp.port.data.SubnetToDhcpPort;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dhcpservice.config.rev150710.DhcpserviceConfig;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.opendaylight.yangtools.yang.common.Uint64;
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(Uint64.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(Uint64.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(Uint64.valueOf(original.getDpId()),
83                 original.getTunnelRemoteIpAddress(), original.getElanInstanceName());
84         Uint64 designatedDpnId = Uint64.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(Uint64.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.toString(),
98                 elanInstanceName);
99             dhcpExternalTunnelManager.configureDhcpArpRequestResponseFlow(designatedDpnId, elanInstanceName,
100                     true, tunnelRemoteIpAddress, subnetDhcpData.get().getPortFixedip(),
101                     subnetDhcpData.get().getPortMacaddress());
102         }
103     }
104
105     @Override
106     protected void add(InstanceIdentifier<DesignatedSwitchForTunnel> identifier, DesignatedSwitchForTunnel add) {
107         LOG.debug("Add for DesignatedSwitchForTunnel : {}", add);
108         Uint64 designatedDpnId = Uint64.valueOf(add.getDpId());
109         IpAddress tunnelRemoteIpAddress = add.getTunnelRemoteIpAddress();
110         String elanInstanceName = add.getElanInstanceName();
111         dhcpExternalTunnelManager.updateLocalCache(designatedDpnId, tunnelRemoteIpAddress, elanInstanceName);
112         dhcpExternalTunnelManager.installRemoteMcastMac(designatedDpnId, tunnelRemoteIpAddress, elanInstanceName);
113         LOG.trace("Configuring DHCP Arp Flows for Designated dpn {} Elan {}", designatedDpnId, elanInstanceName);
114         java.util.Optional<SubnetToDhcpPort> subnetDhcpData = dhcpExternalTunnelManager
115                 .getSubnetDhcpPortData(elanInstanceName);
116         if (subnetDhcpData.isPresent()) {
117             dhcpExternalTunnelManager.configureDhcpArpRequestResponseFlow(designatedDpnId, elanInstanceName,
118                     true, tunnelRemoteIpAddress, subnetDhcpData.get().getPortFixedip(),
119                     subnetDhcpData.get().getPortMacaddress());
120         }
121     }
122
123     @Override
124     protected InstanceIdentifier<DesignatedSwitchForTunnel> getWildCardPath() {
125         return InstanceIdentifier.create(DesignatedSwitchesForExternalTunnels.class)
126                 .child(DesignatedSwitchForTunnel.class);
127     }
128
129     @Override
130     protected DhcpDesignatedDpnListener getDataTreeChangeListener() {
131         return DhcpDesignatedDpnListener.this;
132     }
133 }