d47a1f21c17ca23393f7e1c97e6520b6710f9845
[netvirt.git] /
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 import java.util.List;
12
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;
27
28 public class DhcpHwvtepListener
29         extends AsyncDataTreeChangeListenerBase<Node, DhcpHwvtepListener>
30         implements AutoCloseable {
31
32     private static final Logger LOG = LoggerFactory.getLogger(DhcpHwvtepListener.class);
33     private DhcpExternalTunnelManager dhcpExternalTunnelManager;
34     private DataBroker dataBroker;
35
36     public DhcpHwvtepListener(DataBroker dataBroker, DhcpExternalTunnelManager dhcpManager) {
37         super(Node.class, DhcpHwvtepListener.class);
38         this.dhcpExternalTunnelManager = dhcpManager;
39         this.dataBroker = dataBroker;
40     }
41
42     public void init() {
43         registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
44     }
45
46     @Override
47     public void close() throws Exception {
48         super.close();
49         LOG.info("DhcpHwvtepListener Closed");
50     }
51
52     @Override
53     protected void remove(InstanceIdentifier<Node> identifier,
54             Node del) {
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");
59             return;
60         }
61         List<LogicalSwitches> logicalSwitches = hwvtepNode.getLogicalSwitches();
62         if (logicalSwitches == null || logicalSwitches.isEmpty()) {
63             LOG.trace("LogicalSwitch is not added");
64             return;
65         }
66         for (LogicalSwitches logicalSwitch : logicalSwitches) {
67             String elanInstanceName = logicalSwitch.getHwvtepNodeName().getValue();
68             IpAddress tunnelIp = hwvtepNode.getConnectionInfo().getRemoteIp();
69             handleLogicalSwitchRemove(elanInstanceName, tunnelIp);
70         }
71     }
72
73     @Override
74     protected void update(InstanceIdentifier<Node> identifier,
75             Node original, Node update) {
76     }
77
78     @Override
79     protected void add(InstanceIdentifier<Node> identifier,
80             Node add) {
81     }
82
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);
88             return;
89         }
90         dhcpExternalTunnelManager.removeDesignatedSwitchForExternalTunnel(designatedDpnId, tunnelIp, elanInstanceName);
91     }
92
93     @Override
94     protected InstanceIdentifier<Node> getWildCardPath() {
95         return InstanceIdentifier.create(NetworkTopology.class)
96                 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID)).child(Node.class);
97     }
98
99     @Override
100     protected DhcpHwvtepListener getDataTreeChangeListener() {
101         return DhcpHwvtepListener.this;
102     }
103 }