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.vpnservice.dhcpservice;
10 import java.math.BigInteger;
11 import java.util.List;
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.rev130715.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;
38 import com.google.common.base.Optional;
40 public class DhcpL2GatewayConnectionListener extends AsyncClusteredDataChangeListenerBase<L2gatewayConnection,DhcpL2GatewayConnectionListener> implements AutoCloseable {
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;
47 public DhcpL2GatewayConnectionListener(DataBroker dataBroker, DhcpExternalTunnelManager dhcpExternalTunnelManager) {
48 super(L2gatewayConnection.class, DhcpL2GatewayConnectionListener.class);
49 this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
50 this.dataBroker = dataBroker;
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 logger.trace("L2Gw not present id {}", gatewayId);
64 Uuid networkUuid = del.getNetworkId();
65 boolean isLastConnection = isLastGatewayConnection(networkUuid);
66 if (!isLastConnection) {
67 logger.trace("Not the last L2GatewayConnection. Not removing flows.");
70 List<Devices> l2Devices = l2Gateway.get().getDevices();
71 for (Devices l2Device : l2Devices) {
72 String l2DeviceName = l2Device.getDeviceName();
73 L2GatewayDevice l2GatewayDevice = L2GatewayCacheUtils.getL2DeviceFromCache(l2DeviceName);
74 IpAddress tunnelIp = l2GatewayDevice.getTunnelIp();
75 BigInteger designatedDpnId = dhcpExternalTunnelManager.readDesignatedSwitchesForExternalTunnel(tunnelIp, del.getNetworkId().getValue());
76 if (designatedDpnId == null || designatedDpnId.equals(DHCPMConstants.INVALID_DPID)) {
77 logger.error("Could not find desiganted DPN ID");
80 dhcpExternalTunnelManager.removeDesignatedSwitchForExternalTunnel(designatedDpnId, tunnelIp, del.getNetworkId().getValue());
84 private boolean isLastGatewayConnection(Uuid networkUuid) {
85 boolean isLastConnection = true;
86 InstanceIdentifier<L2gatewayConnections> l2gatewayConnectionIdentifier = InstanceIdentifier.create(Neutron.class).child(L2gatewayConnections.class);
87 Optional<L2gatewayConnections> l2GwConnection = MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, l2gatewayConnectionIdentifier);
88 List<L2gatewayConnection> l2GatewayConnectionList = l2GwConnection.get().getL2gatewayConnection();
89 for (L2gatewayConnection l2gatewayConnection : l2GatewayConnectionList) {
90 if (networkUuid.equals(l2gatewayConnection.getNetworkId())) {
91 isLastConnection = false;
95 return isLastConnection;
99 protected void update(InstanceIdentifier<L2gatewayConnection> identifier,
100 L2gatewayConnection original, L2gatewayConnection update) {
105 protected void add(InstanceIdentifier<L2gatewayConnection> identifier,
106 L2gatewayConnection add) {
111 protected InstanceIdentifier<L2gatewayConnection> getWildCardPath() {
112 return InstanceIdentifier.create(Neutron.class).child(L2gatewayConnections.class)
113 .child(L2gatewayConnection.class);
117 protected ClusteredDataChangeListener getDataChangeListener() {
118 return DhcpL2GatewayConnectionListener.this;
122 protected DataChangeScope getDataChangeScope() {
123 return AsyncDataBroker.DataChangeScope.SUBTREE;
127 public void close() throws Exception {
128 if (listenerRegistration != null) {
130 listenerRegistration.close();
131 } catch (final Exception e) {
132 logger.error("Error when cleaning up DataChangeListener.", e);
134 listenerRegistration = null;
136 logger.info("DhcpL2GatewayConnection listener Closed");