DHCP Handling for TOR VM
[vpnservice.git] / dhcpservice / dhcpservice-impl / src / main / java / org / opendaylight / vpnservice / dhcpservice / DhcpL2GatewayConnectionListener.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.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.vpnservice.datastoreutils.AsyncClusteredDataChangeListenerBase;
20 import org.opendaylight.vpnservice.dhcpservice.api.DHCPMConstants;
21 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
22 import org.opendaylight.vpnservice.neutronvpn.api.l2gw.L2GatewayDevice;
23 import org.opendaylight.vpnservice.neutronvpn.api.l2gw.utils.L2GatewayCacheUtils;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.L2gateways;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gateway;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gatewayKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import com.google.common.base.Optional;
39
40 public class DhcpL2GatewayConnectionListener extends AsyncClusteredDataChangeListenerBase<L2gatewayConnection,DhcpL2GatewayConnectionListener> implements AutoCloseable {
41
42     private static final Logger logger = LoggerFactory.getLogger(DhcpInterfaceEventListener.class);
43     private ListenerRegistration<DataChangeListener> listenerRegistration;
44     private final DhcpExternalTunnelManager dhcpExternalTunnelManager;
45     private final DataBroker dataBroker;
46
47     public DhcpL2GatewayConnectionListener(DataBroker dataBroker, DhcpExternalTunnelManager dhcpExternalTunnelManager) {
48         super(L2gatewayConnection.class, DhcpL2GatewayConnectionListener.class);
49         this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
50         this.dataBroker = dataBroker;
51     }
52
53     @Override
54     protected void remove(InstanceIdentifier<L2gatewayConnection> identifier,
55             L2gatewayConnection del) {
56         Uuid gatewayId = del.getL2gatewayId();
57         InstanceIdentifier<L2gateway> inst = InstanceIdentifier.create(Neutron.class).child(L2gateways.class)
58                 .child(L2gateway.class, new L2gatewayKey(gatewayId));
59         Optional<L2gateway> l2Gateway = MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, inst);
60         if (!l2Gateway.isPresent()) {
61             return;
62         }
63         Uuid networkUuid = del.getNetworkId();
64         boolean isLastConnection = true;
65         InstanceIdentifier<L2gatewayConnections> l2gatewayConnectionIdentifier = InstanceIdentifier.create(Neutron.class).child(L2gatewayConnections.class);
66         Optional<L2gatewayConnections> l2GwConnection = MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, l2gatewayConnectionIdentifier);
67         List<L2gatewayConnection> l2GatewayConnectionList = l2GwConnection.get().getL2gatewayConnection();
68         for (L2gatewayConnection l2gatewayConnection : l2GatewayConnectionList) {
69             if (networkUuid.equals(l2gatewayConnection.getNetworkId())) {
70                 isLastConnection = false;
71                 break;
72             }
73         }
74         if (!isLastConnection) {
75             return;
76         }
77         List<Devices> l2Devices = l2Gateway.get().getDevices();
78         for (Devices l2Device : l2Devices) {
79             String l2DeviceName = l2Device.getDeviceName();
80             L2GatewayDevice l2GatewayDevice = L2GatewayCacheUtils.getL2DeviceFromCache(l2DeviceName);
81             IpAddress tunnelIp = l2GatewayDevice.getTunnelIp();
82             BigInteger designatedDpnId = dhcpExternalTunnelManager.readDesignatedSwitchesForExternalTunnel(tunnelIp, del.getNetworkId().getValue());
83             if (designatedDpnId == null || designatedDpnId.equals(DHCPMConstants.INVALID_DPID)) {
84                 logger.error("Could not find desiganted DPN ID");
85                 return;
86             }
87             dhcpExternalTunnelManager.removeDesignatedSwitchForExternalTunnel(designatedDpnId, tunnelIp, del.getNetworkId().getValue());
88         }
89     }
90
91     @Override
92     protected void update(InstanceIdentifier<L2gatewayConnection> identifier,
93             L2gatewayConnection original, L2gatewayConnection update) {
94
95     }
96
97     @Override
98     protected void add(InstanceIdentifier<L2gatewayConnection> identifier,
99             L2gatewayConnection add) {
100
101     }
102
103     @Override
104     protected InstanceIdentifier<L2gatewayConnection> getWildCardPath() {
105         return InstanceIdentifier.create(Neutron.class).child(L2gatewayConnections.class)
106                 .child(L2gatewayConnection.class);
107     }
108
109     @Override
110     protected ClusteredDataChangeListener getDataChangeListener() {
111         return DhcpL2GatewayConnectionListener.this;
112     }
113
114     @Override
115     protected DataChangeScope getDataChangeScope() {
116         return AsyncDataBroker.DataChangeScope.SUBTREE;
117     }
118
119     @Override
120     public void close() throws Exception {
121         if (listenerRegistration != null) {
122             try {
123                 listenerRegistration.close();
124             } catch (final Exception e) {
125                 logger.error("Error when cleaning up DataChangeListener.", e);
126             }
127             listenerRegistration = null;
128         }
129         logger.info("DhcpL2GatewayConnection listener Closed");
130     }
131 }