2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.dhcpservice;
10 import java.math.BigInteger;
11 import java.util.List;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
16 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
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.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class DhcpHwvtepListener
29 extends AsyncDataTreeChangeListenerBase<Node, DhcpHwvtepListener>
30 implements AutoCloseable {
32 private static final Logger LOG = LoggerFactory.getLogger(DhcpHwvtepListener.class);
33 private DhcpExternalTunnelManager dhcpExternalTunnelManager;
34 private DataBroker dataBroker;
36 public DhcpHwvtepListener(DataBroker dataBroker, DhcpExternalTunnelManager dhcpManager) {
37 super(Node.class, DhcpHwvtepListener.class);
38 this.dhcpExternalTunnelManager = dhcpManager;
39 this.dataBroker = dataBroker;
43 registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
47 public void close() throws Exception {
49 LOG.info("DhcpHwvtepListener Closed");
53 protected void remove(InstanceIdentifier<Node> identifier,
55 LOG.trace("Received Hwvtep remove DCN {}", del);
56 HwvtepGlobalAugmentation hwvtepNode = del.getAugmentation(HwvtepGlobalAugmentation.class);
57 if (hwvtepNode == null) {
58 LOG.trace("LogicalSwitch is not present");
61 List<LogicalSwitches> logicalSwitches = hwvtepNode.getLogicalSwitches();
62 if (logicalSwitches == null || logicalSwitches.isEmpty()) {
63 LOG.trace("LogicalSwitch is not added");
66 for (LogicalSwitches logicalSwitch : logicalSwitches) {
67 String elanInstanceName = logicalSwitch.getHwvtepNodeName().getValue();
68 IpAddress tunnelIp = hwvtepNode.getConnectionInfo().getRemoteIp();
69 handleLogicalSwitchRemove(elanInstanceName, tunnelIp);
74 protected void update(InstanceIdentifier<Node> identifier,
75 Node original, Node update) {
79 protected void add(InstanceIdentifier<Node> identifier,
83 private void handleLogicalSwitchRemove(String elanInstanceName, IpAddress tunnelIp) {
84 BigInteger designatedDpnId;
85 designatedDpnId = dhcpExternalTunnelManager.readDesignatedSwitchesForExternalTunnel(tunnelIp, elanInstanceName);
86 if (designatedDpnId == null) {
87 LOG.info("Could not find designated DPN ID elanInstanceName {}, tunnelIp {}", elanInstanceName, tunnelIp);
90 dhcpExternalTunnelManager.removeDesignatedSwitchForExternalTunnel(designatedDpnId, tunnelIp, elanInstanceName);
94 protected InstanceIdentifier<Node> getWildCardPath() {
95 return InstanceIdentifier.create(NetworkTopology.class)
96 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID)).child(Node.class);
100 protected DhcpHwvtepListener getDataTreeChangeListener() {
101 return DhcpHwvtepListener.this;