d97fc88efb5996640fb4bd2a9e082bfa7071f68c
[vpnservice.git] / nexthopmgr / nexthopmgr-impl / src / main / java / org / opendaylight / vpnservice / nexthopmgr / OdlInterfaceChangeListener.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.vpnservice.nexthopmgr;
9
10
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;
21
22 import org.opendaylight.vpnservice.nexthopmgr.AbstractDataChangeListener;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27
28 public class OdlInterfaceChangeListener extends AbstractDataChangeListener<Interface> implements AutoCloseable {
29     private static final Logger LOG = LoggerFactory.getLogger(OdlInterfaceChangeListener.class);
30
31     private ListenerRegistration<DataChangeListener> listenerRegistration;
32     private final DataBroker broker;
33     private NexthopManager nexthopManager;
34
35     public OdlInterfaceChangeListener(final DataBroker db, NexthopManager nhm) {
36         super(Interface.class);
37         broker = db;
38         nexthopManager = nhm;
39         registerListener(db);
40     }
41
42     @Override
43     public void close() throws Exception {
44         if (listenerRegistration != null) {
45             try {
46                 listenerRegistration.close();
47             } catch (final Exception e) {
48                 LOG.error("Error when cleaning up DataChangeListener.", e);
49             }
50             listenerRegistration = null;
51         }
52         LOG.info("odlInterface listener Closed");
53     }
54
55
56     private void registerListener(final DataBroker db) {
57         try {
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);
63         }
64     }
65     
66     @Override
67     
68         protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
69         LOG.info("key: " + identifier + ", value=" + intrf );
70
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();
75             if (gwIp != null) {
76                 remoteIp = gwIp;
77             }
78             nexthopManager.createRemoteNextHop(intrf.getName(), remoteIp);
79         }
80     }
81
82     
83     private InstanceIdentifier<Interface> getWildCardPath() {
84         return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
85     }
86
87     @Override
88     protected void remove(InstanceIdentifier<Interface> identifier,
89             Interface intrf) {
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();
94             if (gwIp != null) {
95                 remoteIp = gwIp;
96             }
97             nexthopManager.removeRemoteNextHop(intrf.getName(), remoteIp);
98         }
99     }
100
101     @Override
102     protected void update(InstanceIdentifier<Interface> identifier,
103             Interface original, Interface update) {
104         // TODO Auto-generated method stub
105
106     }
107
108 }