2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
8 package org.opendaylight.netvirt.vpnmanager;
\r
10 import com.google.common.util.concurrent.ListenableFuture;
\r
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
\r
12 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
\r
13 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
\r
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
\r
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
\r
16 import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
\r
17 import org.opendaylight.genius.mdsalutil.MDSALUtil;
\r
18 import org.opendaylight.netvirt.vpnmanager.utilities.InterfaceUtils;
\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterInterfacesMap;
\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.RouterInterfaces;
\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.router.interfaces.Interfaces;
\r
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
\r
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
\r
24 import org.slf4j.Logger;
\r
25 import org.slf4j.LoggerFactory;
\r
27 import java.util.ArrayList;
\r
28 import java.util.List;
\r
29 import java.util.concurrent.Callable;
\r
31 public class RouterInterfaceListener extends AbstractDataChangeListener<Interfaces> {
\r
32 private static final Logger LOG = LoggerFactory.getLogger(RouterInterfaceListener.class);
\r
33 private ListenerRegistration<DataChangeListener> listenerRegistration;
\r
34 private DataBroker broker;
\r
35 private VpnInterfaceManager vpnInterfaceManager;
\r
37 public RouterInterfaceListener(final DataBroker db) {
\r
38 super(Interfaces.class);
\r
40 registerListener(db);
\r
43 void setVpnInterfaceManager(VpnInterfaceManager vpnInterfaceManager) {
\r
44 this.vpnInterfaceManager = vpnInterfaceManager;
\r
47 private void registerListener(final DataBroker db) {
\r
49 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
\r
50 getWildCardPath(), RouterInterfaceListener.this, DataChangeScope.SUBTREE);
\r
51 } catch (final Exception e) {
\r
52 LOG.error("Router interface DataChange listener registration fail !", e);
\r
56 private InstanceIdentifier<?> getWildCardPath() {
\r
57 return InstanceIdentifier.create(RouterInterfacesMap.class).child(RouterInterfaces.class).child(Interfaces.class);
\r
61 protected void add(InstanceIdentifier<Interfaces> identifier, Interfaces interfaceInfo) {
\r
62 LOG.trace("Add event - key: {}, value: {}", identifier, interfaceInfo);
\r
63 final String routerId = identifier.firstKeyOf(RouterInterfaces.class).getRouterId().getValue();
\r
64 final String interfaceName = interfaceInfo.getInterfaceId();
\r
65 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface interfaceState =
\r
66 InterfaceUtils.getInterfaceStateFromOperDS(broker, interfaceName);
\r
67 if (interfaceState != null) {
\r
68 DataStoreJobCoordinator dataStoreCoordinator = DataStoreJobCoordinator.getInstance();
\r
69 dataStoreCoordinator.enqueueJob(interfaceName,
\r
70 new Callable<List<ListenableFuture<Void>>>() {
\r
72 public List<ListenableFuture<Void>> call() throws Exception {
\r
73 WriteTransaction writeTxn = broker.newWriteOnlyTransaction();
\r
74 LOG.debug("Handling interface {} in router {} add scenario", interfaceName, routerId);
\r
75 writeTxn.put(LogicalDatastoreType.CONFIGURATION,
\r
76 VpnUtil.getRouterInterfaceId(interfaceName),
\r
77 VpnUtil.getRouterInterface(interfaceName, routerId), true);
\r
78 vpnInterfaceManager.addToNeutronRouterDpnsMap(routerId, interfaceName, writeTxn);
\r
79 List<ListenableFuture<Void>> futures = new ArrayList<>();
\r
80 futures.add(writeTxn.submit());
\r
85 LOG.warn("Interface {} not yet operational to handle router interface add event in router {}", interfaceName, routerId);
\r
90 protected void remove(InstanceIdentifier<Interfaces> identifier, Interfaces interfaceInfo) {
\r
91 LOG.trace("Remove event - key: {}, value: {}", identifier, interfaceInfo);
\r
92 final String routerId = identifier.firstKeyOf(RouterInterfaces.class).getRouterId().getValue();
\r
93 final String interfaceName = interfaceInfo.getInterfaceId();
\r
94 DataStoreJobCoordinator dataStoreCoordinator = DataStoreJobCoordinator.getInstance();
\r
95 dataStoreCoordinator.enqueueJob(interfaceName,
\r
96 new Callable<List<ListenableFuture<Void>>>() {
\r
98 public List<ListenableFuture<Void>> call() throws Exception {
\r
99 WriteTransaction writeTxn = broker.newWriteOnlyTransaction();
\r
100 vpnInterfaceManager.removeFromNeutronRouterDpnsMap(routerId, interfaceName, writeTxn);
\r
101 List<ListenableFuture<Void>> futures = new ArrayList<>();
\r
102 futures.add(writeTxn.submit());
\r
109 protected void update(InstanceIdentifier<Interfaces> identifier, Interfaces original, Interfaces update) {
\r
110 LOG.trace("Update event - key: {}, original: {}, update: {}", identifier, original, update);
\r