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.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;
29 public class DhcpNeutronPortListener extends AsyncClusteredDataChangeListenerBase<Port, DhcpNeutronPortListener> implements AutoCloseable {
31 private static final Logger LOG = LoggerFactory.getLogger(DhcpNeutronPortListener.class);
33 private ListenerRegistration<DataChangeListener> listenerRegistration;
34 private DhcpExternalTunnelManager dhcpExternalTunnelManager;
35 private DataBroker broker;
37 public DhcpNeutronPortListener(final DataBroker db, final DhcpExternalTunnelManager dhcpExternalTunnelManager) {
38 super(Port.class, DhcpNeutronPortListener.class);
39 this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
44 protected InstanceIdentifier<Port> getWildCardPath() {
45 return InstanceIdentifier.create(Neutron.class).child(Ports.class).child(Port.class);
49 public void close() throws Exception {
50 if (listenerRegistration != null) {
52 listenerRegistration.close();
53 } catch (final Exception e) {
54 LOG.error("Error when cleaning up DhcpNeutronPortListener.", e);
56 listenerRegistration = null;
58 LOG.debug("DhcpNeutronPortListener Listener Closed");
62 protected void remove(InstanceIdentifier<Port> identifier, Port del) {
63 LOG.trace("Port removed: {}", del);
64 if(NeutronUtils.isPortVnicTypeNormal(del)) {
67 String macAddress = del.getMacAddress();
68 Uuid networkId = del.getNetworkId();
69 String segmentationId = DhcpServiceUtils.getSegmentationId(networkId, broker);
70 if (segmentationId == null) {
73 List<BigInteger> listOfDpns = DhcpServiceUtils.getListOfDpns(broker);
74 dhcpExternalTunnelManager.unInstallDhcpFlowsForVms(networkId.getValue(), listOfDpns, macAddress);
75 dhcpExternalTunnelManager.removeVniMacToPortCache(new BigInteger(segmentationId), macAddress);
79 protected void update(InstanceIdentifier<Port> identifier, Port original, Port update) {
80 LOG.trace("Port changed to {}", update);
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);
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");
97 dhcpExternalTunnelManager.updateVniMacToPortCache(new BigInteger(segmentationId), macAddress, add);
101 protected ClusteredDataChangeListener getDataChangeListener() {
102 return DhcpNeutronPortListener.this;
106 protected DataChangeScope getDataChangeScope() {
107 return AsyncDataBroker.DataChangeScope.SUBTREE;