2 * Copyright (c) 2016 - 2018 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.netvirt.natservice.internal;
10 import com.google.common.base.Optional;
11 import java.math.BigInteger;
12 import javax.annotation.PostConstruct;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
18 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
19 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
20 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.vpn._interface.VpnInstanceNames;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev170119.L2vlan;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.router.interfaces.RouterInterface;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn._interface.op.data.VpnInterfaceOpDataEntry;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 public class NatInterfaceStateChangeListener
32 extends AsyncDataTreeChangeListenerBase<Interface, NatInterfaceStateChangeListener> {
34 private static final Logger LOG = LoggerFactory.getLogger(NatInterfaceStateChangeListener.class);
35 private final DataBroker dataBroker;
36 private final NatSouthboundEventHandlers southboundEventHandlers;
39 public NatInterfaceStateChangeListener(final DataBroker dataBroker,
40 final NatSouthboundEventHandlers southboundEventHandlers) {
41 super(Interface.class, NatInterfaceStateChangeListener.class);
42 this.dataBroker = dataBroker;
43 this.southboundEventHandlers = southboundEventHandlers;
49 LOG.info("{} init", getClass().getSimpleName());
50 registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
54 protected InstanceIdentifier<Interface> getWildCardPath() {
55 return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
59 protected NatInterfaceStateChangeListener getDataTreeChangeListener() {
60 return NatInterfaceStateChangeListener.this;
64 // TODO Clean up the exception handling
65 @SuppressWarnings("checkstyle:IllegalCatch")
66 protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
67 LOG.trace("add : Interface {} up event received", intrf);
68 if (!L2vlan.class.equals(intrf.getType())) {
69 LOG.debug("add : Interface {} is not Vlan Interface.Ignoring", intrf.getName());
72 String interfaceName = intrf.getName();
75 intfDpnId = NatUtil.getDpIdFromInterface(intrf);
76 } catch (Exception e) {
77 LOG.error("add : Exception occured while retriving dpnid for interface {}", intrf.getName(), e);
80 if (BigInteger.ZERO.equals(intfDpnId)) {
81 LOG.warn("add : Could not retrieve dp id for interface {} ", interfaceName);
84 // We service only VM interfaces. We do not service Tunnel Interfaces here.
85 // Tunnel events are directly serviced by TunnelInterfacesStateListener present as part of
86 // VpnInterfaceManager
87 RouterInterface routerInterface = NatUtil.getConfiguredRouterInterface(dataBroker, interfaceName);
88 if (routerInterface != null) {
89 this.southboundEventHandlers.handleAdd(interfaceName, intfDpnId, routerInterface);
91 LOG.info("add : Router-Interface Mapping not found for Interface : {}", interfaceName);
96 // TODO Clean up the exception handling
97 @SuppressWarnings("checkstyle:IllegalCatch")
98 protected void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
99 LOG.trace("remove : Interface {} removed event received", intrf);
100 if (!L2vlan.class.equals(intrf.getType())) {
101 LOG.debug("remove : Interface {} is not Vlan Interface.Ignoring", intrf.getName());
104 String interfaceName = intrf.getName();
105 BigInteger intfDpnId = BigInteger.ZERO;
107 intfDpnId = NatUtil.getDpIdFromInterface(intrf);
108 } catch (Exception e) {
109 LOG.error("remove : Exception occured while retriving dpnid for interface {}", intrf.getName(), e);
110 InstanceIdentifier<VpnInterface> id = NatUtil.getVpnInterfaceIdentifier(interfaceName);
111 Optional<VpnInterface> cfgVpnInterface =
112 SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(
113 dataBroker, LogicalDatastoreType.CONFIGURATION, id);
114 if (!cfgVpnInterface.isPresent()) {
115 LOG.warn("remove : Interface {} is not a VPN Interface, ignoring.", interfaceName);
118 for (VpnInstanceNames vpnInterfaceVpnInstance : cfgVpnInterface.get().nonnullVpnInstanceNames()) {
119 String vpnName = vpnInterfaceVpnInstance.getVpnName();
120 InstanceIdentifier<VpnInterfaceOpDataEntry> idOper = NatUtil
121 .getVpnInterfaceOpDataEntryIdentifier(interfaceName, vpnName);
122 Optional<VpnInterfaceOpDataEntry> optVpnInterface =
123 SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(
124 dataBroker, LogicalDatastoreType.OPERATIONAL, idOper);
125 if (optVpnInterface.isPresent()) {
126 intfDpnId = optVpnInterface.get().getDpnId();
131 if (BigInteger.ZERO.equals(intfDpnId)) {
132 LOG.warn("remove : Could not retrieve dpnid for interface {} ", interfaceName);
135 RouterInterface routerInterface = NatUtil.getConfiguredRouterInterface(dataBroker, interfaceName);
136 if (routerInterface != null) {
137 this.southboundEventHandlers.handleRemove(intrf.getName(), intfDpnId, routerInterface);
139 LOG.info("remove : Router-Interface Mapping not found for Interface : {}", interfaceName);
144 // TODO Clean up the exception handling
145 @SuppressWarnings("checkstyle:IllegalCatch")
146 protected void update(InstanceIdentifier<Interface> identifier, Interface original, Interface update) {
147 LOG.trace("update : Operation Interface update event - Old: {}, New: {}", original, update);
148 if (!L2vlan.class.equals(update.getType())) {
149 LOG.debug("update : Interface {} is not Vlan Interface.Ignoring", update.getName());
152 BigInteger intfDpnId = BigInteger.ZERO;
153 String interfaceName = update.getName();
155 intfDpnId = NatUtil.getDpIdFromInterface(update);
156 } catch (Exception e) {
157 LOG.error("update : Exception occured while retriving dpnid for interface {}", update.getName(), e);
158 InstanceIdentifier<VpnInterface> id = NatUtil.getVpnInterfaceIdentifier(interfaceName);
159 Optional<VpnInterface> cfgVpnInterface =
160 SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(
161 dataBroker, LogicalDatastoreType.CONFIGURATION, id);
162 if (!cfgVpnInterface.isPresent()) {
163 LOG.warn("update : Interface {} is not a VPN Interface, ignoring.", interfaceName);
166 for (VpnInstanceNames vpnInterfaceVpnInstance : cfgVpnInterface.get().nonnullVpnInstanceNames()) {
167 String vpnName = vpnInterfaceVpnInstance.getVpnName();
168 InstanceIdentifier<VpnInterfaceOpDataEntry> idOper = NatUtil
169 .getVpnInterfaceOpDataEntryIdentifier(interfaceName, vpnName);
170 Optional<VpnInterfaceOpDataEntry> optVpnInterface =
171 SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(
172 dataBroker, LogicalDatastoreType.OPERATIONAL, idOper);
173 if (optVpnInterface.isPresent()) {
174 intfDpnId = optVpnInterface.get().getDpnId();
179 if (BigInteger.ZERO.equals(intfDpnId)) {
180 LOG.warn("remove : Could not retrieve dpnid for interface {} ", interfaceName);
183 RouterInterface routerInterface = NatUtil.getConfiguredRouterInterface(dataBroker, interfaceName);
184 if (routerInterface != null) {
185 this.southboundEventHandlers.handleUpdate(original, update, intfDpnId, routerInterface);
187 LOG.info("update : Router-Interface Mapping not found for Interface : {}", interfaceName);