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 java.math.BigInteger;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.vpnservice.interfacemgr.interfaces.IInterfaceManager;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.BaseIds;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL3tunnel;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.L3tunnel;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 public class OdlInterfaceChangeListener extends AbstractDataChangeListener<Interface> implements AutoCloseable {
31 private static final Logger LOG = LoggerFactory.getLogger(OdlInterfaceChangeListener.class);
33 private ListenerRegistration<DataChangeListener> listenerRegistration;
34 private final DataBroker broker;
35 private NexthopManager nexthopManager;
36 private IInterfaceManager interfaceManager;
39 public OdlInterfaceChangeListener(final DataBroker db, NexthopManager nhm, IInterfaceManager ifManager) {
40 super(Interface.class);
43 interfaceManager = ifManager;
48 public void close() throws Exception {
49 if (listenerRegistration != null) {
51 listenerRegistration.close();
52 } catch (final Exception e) {
53 LOG.error("Error when cleaning up DataChangeListener.", e);
55 listenerRegistration = null;
57 LOG.info("odlInterface listener Closed");
61 private void registerListener(final DataBroker db) {
63 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
64 getWildCardPath(), OdlInterfaceChangeListener.this, DataChangeScope.SUBTREE);
65 } catch (final Exception e) {
66 LOG.error("Nexthop Manager Interfaces DataChange listener registration fail!", e);
67 throw new IllegalStateException("Nexthop Manager registration Listener failed.", e);
72 protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
73 LOG.trace("Adding Interface : key: " + identifier + ", value=" + intrf );
75 if (intrf.getType().equals(L3tunnel.class)) {
76 IfL3tunnel intfData = intrf.getAugmentation(IfL3tunnel.class);
77 IpAddress gatewayIp = intfData.getGatewayIp();
78 IpAddress remoteIp = (gatewayIp == null) ? intfData.getRemoteIp() : gatewayIp;
79 NodeConnectorId ofPort = intrf.getAugmentation(BaseIds.class).getOfPortId();
80 nexthopManager.createRemoteNextHop(intrf.getName(), ofPort.toString(), remoteIp.getIpv4Address().getValue());
85 private InstanceIdentifier<Interface> getWildCardPath() {
86 return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
90 protected void remove(InstanceIdentifier<Interface> identifier,
92 LOG.trace("Removing interface : key: " + identifier + ", value=" + intrf );
93 if (intrf.getType().equals(L3tunnel.class)) {
94 BigInteger dpnId = interfaceManager.getDpnForInterface(intrf);
95 IfL3tunnel intfData = intrf.getAugmentation(IfL3tunnel.class);
96 IpAddress gatewayIp = intfData.getGatewayIp();
97 IpAddress remoteIp = (gatewayIp == null) ? intfData.getRemoteIp() : gatewayIp;
98 nexthopManager.removeRemoteNextHop(dpnId, intrf.getName(), remoteIp.getIpv4Address().getValue());
103 protected void update(InstanceIdentifier<Interface> identifier,
104 Interface original, Interface update) {
105 // TODO Auto-generated method stub