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;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
13 import org.opendaylight.yangtools.concepts.ListenerRegistration;
14 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.L3tunnel;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL3tunnel;
22 import org.opendaylight.vpnservice.nexthopmgr.AbstractDataChangeListener;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 public class OdlInterfaceChangeListener extends AbstractDataChangeListener<Interface> implements AutoCloseable {
29 private static final Logger LOG = LoggerFactory.getLogger(OdlInterfaceChangeListener.class);
31 private ListenerRegistration<DataChangeListener> listenerRegistration;
32 private final DataBroker broker;
33 private NexthopManager nexthopManager;
35 public OdlInterfaceChangeListener(final DataBroker db, NexthopManager nhm) {
36 super(Interface.class);
43 public void close() throws Exception {
44 if (listenerRegistration != null) {
46 listenerRegistration.close();
47 } catch (final Exception e) {
48 LOG.error("Error when cleaning up DataChangeListener.", e);
50 listenerRegistration = null;
52 LOG.info("odlInterface listener Closed");
56 private void registerListener(final DataBroker db) {
58 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
59 getWildCardPath(), OdlInterfaceChangeListener.this, DataChangeScope.SUBTREE);
60 } catch (final Exception e) {
61 LOG.error("Nexthop Manager Interfaces DataChange listener registration fail!", e);
62 throw new IllegalStateException("Nexthop Manager registration Listener failed.", e);
68 protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
69 LOG.info("key: " + identifier + ", value=" + intrf );
71 if (intrf.getType().equals(L3tunnel.class)) {
72 IfL3tunnel intfData = intrf.getAugmentation(IfL3tunnel.class);
73 String gwIp = intfData.getGatewayIp().toString();
74 String remoteIp = intfData.getRemoteIp().toString();
78 nexthopManager.createRemoteNextHop(intrf.getName(), remoteIp);
83 private InstanceIdentifier<Interface> getWildCardPath() {
84 return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
88 protected void remove(InstanceIdentifier<Interface> identifier,
90 if (intrf.getType().equals(L3tunnel.class)) {
91 IfL3tunnel intfData = intrf.getAugmentation(IfL3tunnel.class);
92 String gwIp = intfData.getGatewayIp().toString();
93 String remoteIp = intfData.getRemoteIp().toString();
97 nexthopManager.removeRemoteNextHop(intrf.getName(), remoteIp);
102 protected void update(InstanceIdentifier<Interface> identifier,
103 Interface original, Interface update) {
104 // TODO Auto-generated method stub