Fix build faliures due to OFPlugin checktyle fixes
[netvirt.git] / dhcpservice / dhcpservice-impl / src / main / java / org / opendaylight / netvirt / dhcpservice / DhcpUCastMacListener.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.base.Optional;
11 import java.math.BigInteger;
12 import javax.annotation.PostConstruct;
13 import javax.annotation.PreDestroy;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.apache.commons.lang3.tuple.ImmutablePair;
17 import org.apache.commons.lang3.tuple.Pair;
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.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
21 import org.opendaylight.genius.mdsalutil.MDSALUtil;
22 import org.opendaylight.netvirt.dhcpservice.api.DhcpMConstants;
23 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
24 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
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.neutron.ports.rev150712.ports.attributes.ports.Port;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dhcpservice.config.rev150710.DhcpserviceConfig;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 @Singleton
41 public class DhcpUCastMacListener
42         extends AsyncClusteredDataTreeChangeListenerBase<LocalUcastMacs, DhcpUCastMacListener> {
43
44     private static final Logger LOG = LoggerFactory.getLogger(DhcpUCastMacListener.class);
45     private final DhcpExternalTunnelManager dhcpExternalTunnelManager;
46     private final DhcpManager dhcpManager;
47     private final DataBroker broker;
48     private final DhcpserviceConfig config;
49
50     @Inject
51     public DhcpUCastMacListener(final DhcpManager dhcpManager, final DhcpExternalTunnelManager dhcpExtTunnelMgr,
52                                 final DataBroker dataBroker, final DhcpserviceConfig config) {
53         super(LocalUcastMacs.class, DhcpUCastMacListener.class);
54         this.broker = dataBroker;
55         this.dhcpExternalTunnelManager = dhcpExtTunnelMgr;
56         this.dhcpManager = dhcpManager;
57         this.config = config;
58     }
59
60     @PostConstruct
61     public void init() {
62         if (config.isControllerDhcpEnabled()) {
63             registerListener(LogicalDatastoreType.OPERATIONAL, broker);
64         }
65     }
66
67     @Override
68     protected InstanceIdentifier<LocalUcastMacs> getWildCardPath() {
69         return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class).child(Node.class)
70                 .augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class);
71     }
72
73     @Override
74     @PreDestroy
75     public void close() {
76         super.close();
77         LOG.info("DhcpUCastMacListener Closed");
78     }
79
80     @Override
81     protected void remove(InstanceIdentifier<LocalUcastMacs> identifier,
82             LocalUcastMacs del) {
83         // Flow removal for table 18 is handled in Neutron Port delete.
84         //remove the new CR-DHCP
85         NodeId torNodeId = identifier.firstKeyOf(Node.class).getNodeId();
86         LogicalSwitches logicalSwitch = getLogicalSwitches(del);
87         if (null == logicalSwitch) {
88             LOG.error("DhcpUCastMacListener remove :Logical Switch ref doesn't have data");
89             return;
90         }
91         String elanInstanceName = logicalSwitch.getHwvtepNodeName().getValue();
92         L2GatewayDevice device = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanInstanceName, torNodeId.getValue());
93         if (device == null) {
94             LOG.error("Logical Switch Device with name {} is not present in L2GWCONN cache", elanInstanceName);
95             return;
96         }
97         IpAddress tunnelIp = device.getTunnelIp();
98         Pair<IpAddress, String> tunnelIpElanName = new ImmutablePair<>(tunnelIp, elanInstanceName);
99         dhcpExternalTunnelManager.removeFromAvailableCache(tunnelIpElanName);
100     }
101
102     @Override
103     protected void update(InstanceIdentifier<LocalUcastMacs> identifier,
104             LocalUcastMacs original, LocalUcastMacs update) {
105         // TODO Auto-generated method stub
106
107     }
108
109     @Override
110     protected void add(InstanceIdentifier<LocalUcastMacs> identifier,
111             LocalUcastMacs add) {
112         NodeId torNodeId = identifier.firstKeyOf(Node.class).getNodeId();
113         InstanceIdentifier<LogicalSwitches> logicalSwitchRef =
114                 (InstanceIdentifier<LogicalSwitches>) add.getLogicalSwitchRef().getValue();
115         Optional<LogicalSwitches> logicalSwitchOptional =
116                 MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, logicalSwitchRef);
117         if (!logicalSwitchOptional.isPresent()) {
118             LOG.error("Logical Switch ref doesn't have data {}", logicalSwitchRef);
119             return;
120         }
121         LogicalSwitches logicalSwitch = logicalSwitchOptional.get();
122         String elanInstanceName = logicalSwitch.getHwvtepNodeName().getValue();
123         String macAddress = add.getMacEntryKey().getValue();
124         BigInteger vni = new BigInteger(logicalSwitch.getTunnelKey());
125         Port port = dhcpExternalTunnelManager.readVniMacToPortCache(vni, macAddress);
126         if (port == null) {
127             LOG.trace("No neutron port created for macAddress {}, tunnelKey {}", macAddress, vni);
128             return;
129         }
130         L2GatewayDevice device = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanInstanceName, torNodeId.getValue());
131         if (device == null) {
132             LOG.error("Logical Switch Device with name {} is not present in L2GWCONN cache", elanInstanceName);
133             return;
134         }
135         IpAddress tunnelIp = device.getTunnelIp();
136         Subnet subnet = dhcpManager.getNeutronSubnet(port);
137         if (null != subnet && !subnet.isEnableDhcp()) {
138             dhcpExternalTunnelManager.updateExistingVMTunnelIPCache(tunnelIp, elanInstanceName, macAddress);
139             LOG.warn("DhcpUCastMacListener add: flag for the subnetId {} is False so Table 18 entries are not added",
140                      subnet.getUuid());
141             return;
142         }
143         BigInteger designatedDpnId =
144                 dhcpExternalTunnelManager.readDesignatedSwitchesForExternalTunnel(tunnelIp, elanInstanceName);
145         if (designatedDpnId == null || designatedDpnId.equals(DhcpMConstants.INVALID_DPID)) {
146             LOG.trace("Unable to install flows for macAddress {}. TunnelIp {}, elanInstanceName {}, designatedDpn {} ",
147                     macAddress, tunnelIp, elanInstanceName, designatedDpnId);
148             dhcpExternalTunnelManager.updateLocalCache(tunnelIp, elanInstanceName, macAddress);
149             return;
150         }
151         dhcpExternalTunnelManager.installDhcpFlowsForVms(tunnelIp, elanInstanceName,
152                 DhcpServiceUtils.getListOfDpns(broker), designatedDpnId, macAddress);
153     }
154
155     private LogicalSwitches getLogicalSwitches(LocalUcastMacs ucastMacs) {
156         InstanceIdentifier<LogicalSwitches> logicalSwitchRef =
157                 (InstanceIdentifier<LogicalSwitches>)ucastMacs.getLogicalSwitchRef().getValue();
158         return MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, logicalSwitchRef).orNull();
159     }
160
161     @Override
162     protected DhcpUCastMacListener getDataTreeChangeListener() {
163         return DhcpUCastMacListener.this;
164     }
165 }