2 * Copyright (c) 2015 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.nexthopmgr;
10 import java.util.List;
11 import com.google.common.base.Optional;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
14 import org.opendaylight.yangtools.concepts.ListenerRegistration;
15 import org.opendaylight.yangtools.yang.binding.DataObject;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
17 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.Adjacency;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.Adjacencies;
22 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInterfaces;
23 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
24 import org.opendaylight.vpnservice.nexthopmgr.AbstractDataChangeListener;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 public class VpnInterfaceChangeListener extends AbstractDataChangeListener<Adjacencies> implements AutoCloseable {
31 private static final Logger LOG = LoggerFactory.getLogger(VpnInterfaceChangeListener.class);
33 private ListenerRegistration<DataChangeListener> listenerRegistration;
34 private final DataBroker broker;
35 private NexthopManager nexthopManager;
37 public VpnInterfaceChangeListener(final DataBroker db, NexthopManager nhm) {
38 super(Adjacencies.class);
45 public void close() throws Exception {
46 if (listenerRegistration != null) {
48 listenerRegistration.close();
49 } catch (final Exception e) {
50 LOG.error("Error when cleaning up DataChangeListener.", e);
52 listenerRegistration = null;
54 LOG.info("VPN Interface Manager Closed");
58 private void registerListener(final DataBroker db) {
60 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
61 getWildCardPath(), VpnInterfaceChangeListener.this, DataChangeScope.SUBTREE);
62 } catch (final Exception e) {
63 LOG.error("Nexthop Manager DataChange listener registration fail!", e);
64 throw new IllegalStateException("Nexthop Manager registration Listener failed.", e);
69 protected void add(InstanceIdentifier<Adjacencies> identifier,
71 LOG.trace("Adding adjacencies interface : key: " + identifier + ", value=" + adjs );
72 InstanceIdentifier<VpnInterface> vpnIfId = identifier.firstIdentifierOf(VpnInterface.class);
73 Optional<VpnInterface> vpnIf = read(LogicalDatastoreType.OPERATIONAL, vpnIfId);
74 VpnInterface vpnIfData = vpnIf.get();
76 List<Adjacency> adjList = adjs.getAdjacency();
77 for (Adjacency adjacency : adjList) {
78 nexthopManager.createLocalNextHop(
80 vpnIfData.getVpnInstanceName(),
81 adjacency.getIpAddress(),
82 adjacency.getMacAddress());
88 protected void remove(InstanceIdentifier<Adjacencies> identifier,
90 // nexthop group will be removed after fib entry deletion
94 protected void update(InstanceIdentifier<Adjacencies> identifier,
95 Adjacencies original, Adjacencies update) {
96 // TODO Auto-generated method stub
100 private <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
101 InstanceIdentifier<T> path) {
103 ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
105 Optional<T> result = Optional.absent();
107 result = tx.read(datastoreType, path).get();
108 } catch (Exception e) {
109 throw new RuntimeException(e);
115 private InstanceIdentifier<Adjacencies> getWildCardPath() {
116 return InstanceIdentifier.create(VpnInterfaces.class).child(VpnInterface.class).augmentation(Adjacencies.class);