DHCP Handling for TOR VM
[vpnservice.git] / dhcpservice / dhcpservice-impl / src / main / java / org / opendaylight / vpnservice / dhcpservice / DhcpNeutronPortListener.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.vpnservice.dhcpservice;
9
10 import java.math.BigInteger;
11 import java.util.List;
12
13 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataChangeListener;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
18 import org.opendaylight.vpnservice.datastoreutils.AsyncClusteredDataChangeListenerBase;
19 import org.opendaylight.vpnservice.neutronvpn.api.utils.NeutronUtils;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class DhcpNeutronPortListener extends AsyncClusteredDataChangeListenerBase<Port, DhcpNeutronPortListener> implements AutoCloseable {
30
31     private static final Logger LOG = LoggerFactory.getLogger(DhcpNeutronPortListener.class);
32
33     private ListenerRegistration<DataChangeListener> listenerRegistration;
34     private DhcpExternalTunnelManager dhcpExternalTunnelManager;
35     private DataBroker broker;
36
37     public DhcpNeutronPortListener(final DataBroker db, final DhcpExternalTunnelManager dhcpExternalTunnelManager) {
38         super(Port.class, DhcpNeutronPortListener.class);
39         this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
40         this.broker = db;
41     }
42
43     @Override
44     protected InstanceIdentifier<Port> getWildCardPath() {
45         return InstanceIdentifier.create(Neutron.class).child(Ports.class).child(Port.class);
46     }
47
48     @Override
49     public void close() throws Exception {
50         if (listenerRegistration != null) {
51             try {
52                 listenerRegistration.close();
53             } catch (final Exception e) {
54                 LOG.error("Error when cleaning up DhcpNeutronPortListener.", e);
55             }
56             listenerRegistration = null;
57         }
58         LOG.debug("DhcpNeutronPortListener Listener Closed");
59     }
60
61     @Override
62     protected void remove(InstanceIdentifier<Port> identifier, Port del) {
63         LOG.trace("Port removed: {}", del);
64         if(NeutronUtils.isPortVnicTypeNormal(del)) {
65             return;
66         }
67         String macAddress = del.getMacAddress();
68         Uuid networkId = del.getNetworkId();
69         String segmentationId = DhcpServiceUtils.getSegmentationId(networkId, broker);
70         if (segmentationId == null) {
71             return;
72         }
73         List<BigInteger> listOfDpns = DhcpServiceUtils.getListOfDpns(broker);
74         dhcpExternalTunnelManager.unInstallDhcpFlowsForVms(networkId.getValue(), listOfDpns, macAddress);
75         dhcpExternalTunnelManager.removeVniMacToPortCache(new BigInteger(segmentationId), macAddress);
76     }
77
78     @Override
79     protected void update(InstanceIdentifier<Port> identifier, Port original, Port update) {
80         LOG.trace("Port changed to {}", update);
81     }
82
83     @Override
84     protected void add(InstanceIdentifier<Port> identifier, Port add) {
85         LOG.trace("Port added {}", add);
86         if(NeutronUtils.isPortVnicTypeNormal(add)) {
87             LOG.trace("Port is normal {}", add);
88             return;
89         }
90         String macAddress = add.getMacAddress();
91         Uuid networkId = add.getNetworkId();
92         String segmentationId = DhcpServiceUtils.getSegmentationId(networkId, broker);
93         if (segmentationId == null) {
94             LOG.trace("segmentation id is null");
95             return;
96         }
97         dhcpExternalTunnelManager.updateVniMacToPortCache(new BigInteger(segmentationId), macAddress, add);
98     }
99
100     @Override
101     protected ClusteredDataChangeListener getDataChangeListener() {
102         return DhcpNeutronPortListener.this;
103     }
104
105     @Override
106     protected DataChangeScope getDataChangeScope() {
107         return AsyncDataBroker.DataChangeScope.SUBTREE;
108     }
109 }