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