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.vpnservice.datastoreutils.AsyncClusteredDataChangeListenerBase;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.dhcp.rev160428.DesignatedSwitchesForExternalTunnels;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.dhcp.rev160428.designated.switches._for.external.tunnels.DesignatedSwitchForTunnel;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class DhcpDesignatedDpnListener extends AsyncClusteredDataChangeListenerBase<DesignatedSwitchForTunnel, DhcpDesignatedDpnListener> implements AutoCloseable {
29 private static final Logger LOG = LoggerFactory.getLogger(DhcpDesignatedDpnListener.class);
30 private ListenerRegistration<DataChangeListener> listenerRegistration;
31 private DhcpExternalTunnelManager dhcpExternalTunnelManager;
32 private DataBroker broker;
34 public DhcpDesignatedDpnListener(final DhcpExternalTunnelManager dhcpExternalTunnelManager, final DataBroker broker) {
35 super(DesignatedSwitchForTunnel.class, DhcpDesignatedDpnListener.class);
36 this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
41 public void close() throws Exception {
42 if (listenerRegistration != null) {
44 listenerRegistration.close();
45 } catch (final Exception e) {
46 LOG.error("Error when cleaning up DhcpDesignatedDpnListener.", e);
48 listenerRegistration = null;
50 LOG.debug("DhcpDesignatedDpnListener Listener Closed");
54 protected void remove(InstanceIdentifier<DesignatedSwitchForTunnel> identifier, DesignatedSwitchForTunnel del) {
55 dhcpExternalTunnelManager.removeFromLocalCache(BigInteger.valueOf(del.getDpId()), del.getTunnelRemoteIpAddress(), del.getElanInstanceName());
56 dhcpExternalTunnelManager.unInstallDhcpFlowsForVms(del.getElanInstanceName(), del.getTunnelRemoteIpAddress(), DhcpServiceUtils.getListOfDpns(broker));
60 protected void update(InstanceIdentifier<DesignatedSwitchForTunnel> identifier, DesignatedSwitchForTunnel original,
61 DesignatedSwitchForTunnel update) {
62 BigInteger designatedDpnId = BigInteger.valueOf(update.getDpId());
63 IpAddress tunnelRemoteIpAddress = update.getTunnelRemoteIpAddress();
64 String elanInstanceName = update.getElanInstanceName();
65 dhcpExternalTunnelManager.removeFromLocalCache(BigInteger.valueOf(original.getDpId()), original.getTunnelRemoteIpAddress(), original.getElanInstanceName());
66 dhcpExternalTunnelManager.updateLocalCache(designatedDpnId, tunnelRemoteIpAddress, elanInstanceName);
67 /* List<BigInteger> elanDpns = DhcpServiceUtils.getDpnsForElan(elanInstanceName, broker);
68 if (elanDpns == null || elanDpns.isEmpty()) {
69 dhcpExternalTunnelManager.installRemoteMcastMac(designatedDpnId, tunnelRemoteIpAddress, elanInstanceName);
74 protected void add(InstanceIdentifier<DesignatedSwitchForTunnel> identifier, DesignatedSwitchForTunnel add) {
75 BigInteger designatedDpnId = BigInteger.valueOf(add.getDpId());
76 IpAddress tunnelRemoteIpAddress = add.getTunnelRemoteIpAddress();
77 String elanInstanceName = add.getElanInstanceName();
78 dhcpExternalTunnelManager.updateLocalCache(designatedDpnId, tunnelRemoteIpAddress, elanInstanceName);
79 /* List<BigInteger> elanDpns = DhcpServiceUtils.getDpnsForElan(elanInstanceName, broker);
80 if (elanDpns == null || elanDpns.isEmpty()) {
81 dhcpExternalTunnelManager.installRemoteMcastMac(designatedDpnId, tunnelRemoteIpAddress, elanInstanceName);
86 protected InstanceIdentifier<DesignatedSwitchForTunnel> getWildCardPath() {
87 return InstanceIdentifier.create(DesignatedSwitchesForExternalTunnels.class).child(DesignatedSwitchForTunnel.class);
91 protected ClusteredDataChangeListener getDataChangeListener() {
92 return DhcpDesignatedDpnListener.this;
96 protected DataChangeScope getDataChangeScope() {
97 return AsyncDataBroker.DataChangeScope.SUBTREE;