NETVIRT-1630 migrate to md-sal APIs
[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.PreDestroy;
11 import javax.inject.Inject;
12 import javax.inject.Singleton;
13 import org.opendaylight.infrautils.utils.concurrent.Executors;
14 import org.opendaylight.mdsal.binding.api.DataBroker;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.serviceutils.tools.listener.AbstractClusteredAsyncDataTreeChangeListener;
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 extends AbstractClusteredAsyncDataTreeChangeListener<DesignatedSwitchForTunnel> {
29
30     private static final Logger LOG = LoggerFactory.getLogger(DhcpDesignatedDpnListener.class);
31     private final DhcpExternalTunnelManager dhcpExternalTunnelManager;
32     private final DataBroker broker;
33     private final DhcpserviceConfig config;
34
35     @Inject
36     public DhcpDesignatedDpnListener(final DhcpExternalTunnelManager dhcpExternalTunnelManager,
37                                      final DataBroker broker,
38                                      final DhcpserviceConfig config) {
39         super(broker, LogicalDatastoreType.CONFIGURATION,
40                 InstanceIdentifier.create(DesignatedSwitchesForExternalTunnels.class)
41                         .child(DesignatedSwitchForTunnel.class),
42                 Executors.newListeningSingleThreadExecutor("DhcpDesignatedDpnListener", LOG));
43         this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
44         this.broker = broker;
45         this.config = config;
46         init();
47     }
48
49     public void init() {
50         if (config.isControllerDhcpEnabled()) {
51             LOG.info("{} close", getClass().getSimpleName());
52         }
53     }
54
55     @Override
56     @PreDestroy
57     public void close() {
58         super.close();
59         Executors.shutdownAndAwaitTermination(getExecutorService());
60         LOG.debug("DhcpDesignatedDpnListener Listener Closed");
61     }
62
63     @Override
64     public void remove(InstanceIdentifier<DesignatedSwitchForTunnel> identifier, DesignatedSwitchForTunnel del) {
65         if (!config.isControllerDhcpEnabled()) {
66             return;
67         }
68         LOG.debug("Remove for DesignatedSwitchForTunnel : {}", del);
69         dhcpExternalTunnelManager.removeFromLocalCache(Uint64.valueOf(del.getDpId()),
70                 del.getTunnelRemoteIpAddress(), del.getElanInstanceName());
71         dhcpExternalTunnelManager.unInstallDhcpFlowsForVms(del.getElanInstanceName(),
72                 del.getTunnelRemoteIpAddress(), DhcpServiceUtils.getListOfDpns(broker));
73         LOG.trace("Removing designated DPN {} DHCP Arp Flows for Elan {}.", del.getDpId(), del.getElanInstanceName());
74         java.util.Optional<SubnetToDhcpPort> subnetDhcpData = dhcpExternalTunnelManager
75                 .getSubnetDhcpPortData(del.getElanInstanceName());
76         if (subnetDhcpData.isPresent()) {
77             dhcpExternalTunnelManager.configureDhcpArpRequestResponseFlow(Uint64.valueOf(del.getDpId()),
78                     del.getElanInstanceName(), false, del.getTunnelRemoteIpAddress(),
79                     subnetDhcpData.get().getPortFixedip(), subnetDhcpData.get().getPortMacaddress());
80         }
81
82     }
83
84     @Override
85     public void update(InstanceIdentifier<DesignatedSwitchForTunnel> identifier, DesignatedSwitchForTunnel original,
86             DesignatedSwitchForTunnel update) {
87         if (!config.isControllerDhcpEnabled()) {
88             return;
89         }
90         LOG.debug("Update for DesignatedSwitchForTunnel original {}, update {}", original, update);
91         dhcpExternalTunnelManager.removeFromLocalCache(Uint64.valueOf(original.getDpId()),
92                 original.getTunnelRemoteIpAddress(), original.getElanInstanceName());
93         Uint64 designatedDpnId = Uint64.valueOf(update.getDpId());
94         IpAddress tunnelRemoteIpAddress = update.getTunnelRemoteIpAddress();
95         String elanInstanceName = update.getElanInstanceName();
96         dhcpExternalTunnelManager.updateLocalCache(designatedDpnId, tunnelRemoteIpAddress, elanInstanceName);
97         dhcpExternalTunnelManager.installRemoteMcastMac(designatedDpnId, tunnelRemoteIpAddress, elanInstanceName);
98         java.util.Optional<SubnetToDhcpPort> subnetDhcpData = dhcpExternalTunnelManager
99                 .getSubnetDhcpPortData(elanInstanceName);
100         if (subnetDhcpData.isPresent()) {
101             LOG.trace("Removing Designated DPN {} DHCP Arp Flows for Elan {}.", original.getDpId(),
102                     original.getElanInstanceName());
103             dhcpExternalTunnelManager.configureDhcpArpRequestResponseFlow(Uint64.valueOf(original.getDpId()),
104                     original.getElanInstanceName(), false, original.getTunnelRemoteIpAddress(),
105                     subnetDhcpData.get().getPortFixedip(), subnetDhcpData.get().getPortMacaddress());
106             LOG.trace("Configuring DHCP Arp Flows for Designated dpn {} Elan {}", designatedDpnId.toString(),
107                 elanInstanceName);
108             dhcpExternalTunnelManager.configureDhcpArpRequestResponseFlow(designatedDpnId, elanInstanceName,
109                     true, tunnelRemoteIpAddress, subnetDhcpData.get().getPortFixedip(),
110                     subnetDhcpData.get().getPortMacaddress());
111         }
112     }
113
114     @Override
115     public void add(InstanceIdentifier<DesignatedSwitchForTunnel> identifier, DesignatedSwitchForTunnel add) {
116         if (!config.isControllerDhcpEnabled()) {
117             return;
118         }
119         LOG.debug("Add for DesignatedSwitchForTunnel : {}", add);
120         Uint64 designatedDpnId = Uint64.valueOf(add.getDpId());
121         IpAddress tunnelRemoteIpAddress = add.getTunnelRemoteIpAddress();
122         String elanInstanceName = add.getElanInstanceName();
123         dhcpExternalTunnelManager.updateLocalCache(designatedDpnId, tunnelRemoteIpAddress, elanInstanceName);
124         dhcpExternalTunnelManager.installRemoteMcastMac(designatedDpnId, tunnelRemoteIpAddress, elanInstanceName);
125         LOG.trace("Configuring DHCP Arp Flows for Designated dpn {} Elan {}", designatedDpnId, elanInstanceName);
126         java.util.Optional<SubnetToDhcpPort> subnetDhcpData = dhcpExternalTunnelManager
127                 .getSubnetDhcpPortData(elanInstanceName);
128         if (subnetDhcpData.isPresent()) {
129             dhcpExternalTunnelManager.configureDhcpArpRequestResponseFlow(designatedDpnId, elanInstanceName,
130                     true, tunnelRemoteIpAddress, subnetDhcpData.get().getPortFixedip(),
131                     subnetDhcpData.get().getPortMacaddress());
132         }
133     }
134 }