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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
15 import org.opendaylight.yangtools.concepts.ListenerRegistration;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.BaseIds;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.L3tunnel;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL3tunnel;
25 import org.opendaylight.vpnservice.interfacemgr.interfaces.IInterfaceManager;
26 import org.opendaylight.vpnservice.nexthopmgr.AbstractDataChangeListener;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 public class OdlInterfaceChangeListener extends AbstractDataChangeListener<Interface> implements AutoCloseable {
32 private static final Logger LOG = LoggerFactory.getLogger(OdlInterfaceChangeListener.class);
34 private ListenerRegistration<DataChangeListener> listenerRegistration;
35 private final DataBroker broker;
36 private NexthopManager nexthopManager;
37 private IInterfaceManager interfaceManager;
40 public OdlInterfaceChangeListener(final DataBroker db, NexthopManager nhm, IInterfaceManager ifManager) {
41 super(Interface.class);
44 interfaceManager = ifManager;
49 public void close() throws Exception {
50 if (listenerRegistration != null) {
52 listenerRegistration.close();
53 } catch (final Exception e) {
54 LOG.error("Error when cleaning up DataChangeListener.", e);
56 listenerRegistration = null;
58 LOG.info("odlInterface listener Closed");
62 private void registerListener(final DataBroker db) {
64 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
65 getWildCardPath(), OdlInterfaceChangeListener.this, DataChangeScope.SUBTREE);
66 } catch (final Exception e) {
67 LOG.error("Nexthop Manager Interfaces DataChange listener registration fail!", e);
68 throw new IllegalStateException("Nexthop Manager registration Listener failed.", e);
73 protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
74 LOG.trace("key: " + identifier + ", value=" + intrf );
76 if (intrf.getType().equals(L3tunnel.class)) {
77 IfL3tunnel intfData = intrf.getAugmentation(IfL3tunnel.class);
78 IpAddress gatewayIp = intfData.getGatewayIp();
79 String gwIp = (gatewayIp == null) ? null : gatewayIp.toString();
80 String remoteIp = null;
84 IpAddress remIp = intfData.getRemoteIp();
85 remoteIp = (remIp == null) ? null : remIp.toString();
87 NodeConnectorId ofPort = intrf.getAugmentation(BaseIds.class).getOfPortId();
88 nexthopManager.createRemoteNextHop(intrf.getName(), ofPort.toString(), remoteIp);
93 private InstanceIdentifier<Interface> getWildCardPath() {
94 return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
98 protected void remove(InstanceIdentifier<Interface> identifier,
100 if (intrf.getType().equals(L3tunnel.class)) {
101 long dpnId = interfaceManager.getDpnForInterface(intrf.getName());
102 IfL3tunnel intfData = intrf.getAugmentation(IfL3tunnel.class);
103 String gwIp = intfData.getGatewayIp().toString();
104 String remoteIp = intfData.getRemoteIp().toString();
108 nexthopManager.removeRemoteNextHop(dpnId, remoteIp);
113 protected void update(InstanceIdentifier<Interface> identifier,
114 Interface original, Interface update) {
115 // TODO Auto-generated method stub