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.inventory.rev130819.NodeConnectorId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.BaseIds;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.L3tunnel;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL3tunnel;
23 import org.opendaylight.vpnservice.interfacemgr.interfaces.IInterfaceManager;
24 import org.opendaylight.vpnservice.nexthopmgr.AbstractDataChangeListener;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 public class OdlInterfaceChangeListener extends AbstractDataChangeListener<Interface> implements AutoCloseable {
30 private static final Logger LOG = LoggerFactory.getLogger(OdlInterfaceChangeListener.class);
32 private ListenerRegistration<DataChangeListener> listenerRegistration;
33 private final DataBroker broker;
34 private NexthopManager nexthopManager;
35 private IInterfaceManager interfaceManager;
38 public OdlInterfaceChangeListener(final DataBroker db, NexthopManager nhm, IInterfaceManager ifManager) {
39 super(Interface.class);
42 interfaceManager = ifManager;
47 public void close() throws Exception {
48 if (listenerRegistration != null) {
50 listenerRegistration.close();
51 } catch (final Exception e) {
52 LOG.error("Error when cleaning up DataChangeListener.", e);
54 listenerRegistration = null;
56 LOG.info("odlInterface listener Closed");
60 private void registerListener(final DataBroker db) {
62 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
63 getWildCardPath(), OdlInterfaceChangeListener.this, DataChangeScope.SUBTREE);
64 } catch (final Exception e) {
65 LOG.error("Nexthop Manager Interfaces DataChange listener registration fail!", e);
66 throw new IllegalStateException("Nexthop Manager registration Listener failed.", e);
71 protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
72 LOG.info("key: " + identifier + ", value=" + intrf );
74 if (intrf.getType().equals(L3tunnel.class)) {
75 IfL3tunnel intfData = intrf.getAugmentation(IfL3tunnel.class);
76 String gwIp = intfData.getGatewayIp().toString();
77 String remoteIp = intfData.getRemoteIp().toString();
81 NodeConnectorId ofPort = intrf.getAugmentation(BaseIds.class).getOfPortId();
82 nexthopManager.createRemoteNextHop(intrf.getName(), ofPort.toString(), remoteIp);
87 private InstanceIdentifier<Interface> getWildCardPath() {
88 return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
92 protected void remove(InstanceIdentifier<Interface> identifier,
94 if (intrf.getType().equals(L3tunnel.class)) {
95 long dpnId = interfaceManager.getDpnForInterface(intrf.getName());
96 IfL3tunnel intfData = intrf.getAugmentation(IfL3tunnel.class);
97 String gwIp = intfData.getGatewayIp().toString();
98 String remoteIp = intfData.getRemoteIp().toString();
102 nexthopManager.removeRemoteNextHop(dpnId, remoteIp);
107 protected void update(InstanceIdentifier<Interface> identifier,
108 Interface original, Interface update) {
109 // TODO Auto-generated method stub