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.netvirt.natservice.internal;
10 import java.math.BigInteger;
11 import javax.annotation.PostConstruct;
12 import javax.inject.Inject;
13 import javax.inject.Singleton;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
18 import org.opendaylight.genius.mdsalutil.MDSALUtil;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.router.interfaces.RouterInterface;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.router.interfaces.RouterInterfaceBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.router.interfaces.RouterInterfaceKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterInterfacesMap;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.RouterInterfaces;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.router.interfaces.Interfaces;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 public class NatRouterInterfaceListener
32 extends AsyncDataTreeChangeListenerBase<Interfaces, NatRouterInterfaceListener> {
34 private static final Logger LOG = LoggerFactory.getLogger(NatRouterInterfaceListener.class);
35 private final DataBroker dataBroker;
36 private final OdlInterfaceRpcService interfaceManager;
39 public NatRouterInterfaceListener(final DataBroker dataBroker, final OdlInterfaceRpcService interfaceManager) {
40 super(Interfaces.class, NatRouterInterfaceListener.class);
41 this.dataBroker = dataBroker;
42 this.interfaceManager = interfaceManager;
48 LOG.info("{} init", getClass().getSimpleName());
49 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
53 protected NatRouterInterfaceListener getDataTreeChangeListener() {
54 return NatRouterInterfaceListener.this;
58 protected InstanceIdentifier<Interfaces> getWildCardPath() {
59 return InstanceIdentifier.create(RouterInterfacesMap.class)
60 .child(RouterInterfaces.class).child(Interfaces.class);
64 // TODO Clean up the exception handling
65 @SuppressWarnings("checkstyle:IllegalCatch")
66 protected void add(InstanceIdentifier<Interfaces> identifier, Interfaces interfaceInfo) {
67 LOG.trace("add : Add event - key: {}, value: {}", interfaceInfo.getKey(), interfaceInfo);
68 final String routerId = identifier.firstKeyOf(RouterInterfaces.class).getRouterId().getValue();
69 final String interfaceName = interfaceInfo.getInterfaceId();
72 MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION,
73 NatUtil.getRouterInterfaceId(interfaceName), getRouterInterface(interfaceName, routerId));
74 } catch (Exception e) {
75 LOG.error("add: Unable to write data in RouterInterface model", e);
78 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces
79 .state.Interface interfaceState = NatUtil.getInterfaceStateFromOperDS(dataBroker, interfaceName);
80 WriteTransaction writeOperTxn = dataBroker.newWriteOnlyTransaction();
81 if (interfaceState != null) {
82 BigInteger dpId = NatUtil.getDpnForInterface(interfaceManager, interfaceName);
83 if (dpId.equals(BigInteger.ZERO)) {
84 LOG.warn("ADD : Could not retrieve dp id for interface {} to handle router {} association model",
85 interfaceName, routerId);
88 NatUtil.addToNeutronRouterDpnsMap(dataBroker, routerId, interfaceName, dpId, writeOperTxn);
89 NatUtil.addToDpnRoutersMap(dataBroker, routerId, interfaceName, dpId, writeOperTxn);
91 LOG.info("add : Interface {} not yet operational to handle router interface add event in router {}",
92 interfaceName, routerId);
95 writeOperTxn.submit();
99 protected void remove(InstanceIdentifier<Interfaces> identifier, Interfaces interfaceInfo) {
100 LOG.trace("remove : Remove event - key: {}, value: {}", interfaceInfo.getKey(), interfaceInfo);
101 final String routerId = identifier.firstKeyOf(RouterInterfaces.class).getRouterId().getValue();
102 final String interfaceName = interfaceInfo.getInterfaceId();
104 //Delete the RouterInterfaces maintained in the ODL:L3VPN configuration model
105 WriteTransaction writeTxn = dataBroker.newWriteOnlyTransaction();
106 writeTxn.delete(LogicalDatastoreType.CONFIGURATION, NatUtil.getRouterInterfaceId(interfaceName));
108 //Delete the NeutronRouterDpnMap from the ODL:L3VPN operational model
109 NatUtil.removeFromNeutronRouterDpnsMap(dataBroker, routerId, interfaceName, interfaceManager, writeTxn);
111 //Delete the DpnRouterMap from the ODL:L3VPN operational model
112 NatUtil.removeFromDpnRoutersMap(dataBroker, routerId, interfaceName, interfaceManager, writeTxn);
117 protected void update(InstanceIdentifier<Interfaces> identifier, Interfaces original, Interfaces update) {
118 LOG.trace("update key: {}, original: {}, update: {}", update.getKey(), original, update);
121 static RouterInterface getRouterInterface(String interfaceName, String routerName) {
122 return new RouterInterfaceBuilder().setKey(new RouterInterfaceKey(interfaceName))
123 .setInterfaceName(interfaceName).setRouterName(routerName).build();