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 java.util.ArrayList;
13 import com.google.common.base.Optional;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
17 import org.opendaylight.yangtools.concepts.ListenerRegistration;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
21 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.Adjacency;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.Adjacencies;
26 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInterfaces;
27 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
28 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterfaceKey;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
32 import org.opendaylight.vpnservice.nexthopmgr.AbstractDataChangeListener;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 public class VpnInterfaceChangeListener extends AbstractDataChangeListener<VpnInterface> implements AutoCloseable {
39 private static final Logger LOG = LoggerFactory.getLogger(VpnInterfaceChangeListener.class);
41 private ListenerRegistration<DataChangeListener> listenerRegistration;
42 private final DataBroker broker;
43 private NexthopManager nexthopManager;
45 public VpnInterfaceChangeListener(final DataBroker db, NexthopManager nhm) {
46 super(VpnInterface.class);
53 public void close() throws Exception {
54 if (listenerRegistration != null) {
56 listenerRegistration.close();
57 } catch (final Exception e) {
58 LOG.error("Error when cleaning up DataChangeListener.", e);
60 listenerRegistration = null;
62 LOG.info("VPN Interface Manager Closed");
66 private void registerListener(final DataBroker db) {
68 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
69 getWildCardPath(), VpnInterfaceChangeListener.this, DataChangeScope.SUBTREE);
70 } catch (final Exception e) {
71 LOG.error("Nexthop Manager DataChange listener registration fail!", e);
72 throw new IllegalStateException("Nexthop Manager registration Listener failed.", e);
77 protected void add(InstanceIdentifier<VpnInterface> identifier,
79 LOG.info("key: " + identifier + ", value=" + vpnIf );
81 String vpnName = vpnIf.getVpnInstanceName();
82 final VpnInterfaceKey key = identifier.firstKeyOf(VpnInterface.class, VpnInterfaceKey.class);
83 String interfaceName = key.getName();
84 InstanceIdentifierBuilder<Interface> idBuilder =
85 InstanceIdentifier.builder(Interfaces.class).child(Interface.class, new InterfaceKey(interfaceName));
86 InstanceIdentifier<Interface> id = idBuilder.build();
87 Optional<Interface> port = read(LogicalDatastoreType.CONFIGURATION, id);
88 if (port.isPresent()) {
89 //Interface interf = port.get();
92 InstanceIdentifier<Adjacencies> path = identifier.augmentation(Adjacencies.class);
93 Optional<Adjacencies> adjacencies = read(LogicalDatastoreType.CONFIGURATION, path);
95 if (adjacencies.isPresent()) {
96 List<Adjacency> nextHops = adjacencies.get().getAdjacency();
97 List<Adjacency> value = new ArrayList<>();
99 if (!nextHops.isEmpty()) {
100 LOG.info("NextHops are " + nextHops);
101 for (Adjacency nextHop : nextHops) {
102 nexthopManager.createLocalNextHop(interfaceName, vpnName, nextHop.getIpAddress());
110 private <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
111 InstanceIdentifier<T> path) {
113 ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
115 Optional<T> result = Optional.absent();
117 result = tx.read(datastoreType, path).get();
118 } catch (Exception e) {
119 throw new RuntimeException(e);
125 private InstanceIdentifier<VpnInterface> getWildCardPath() {
126 return InstanceIdentifier.create(VpnInterfaces.class).child(VpnInterface.class);
130 protected void remove(InstanceIdentifier<VpnInterface> identifier,
132 // TODO Auto-generated method stub
136 protected void update(InstanceIdentifier<VpnInterface> identifier,
137 VpnInterface original, VpnInterface update) {
138 // TODO Auto-generated method stub