69cce9f9f0b78e4728a49f62c51196558aec17d0
[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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
12
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;
29
30
31 public class OdlInterfaceChangeListener extends AbstractDataChangeListener<Interface> implements AutoCloseable {
32     private static final Logger LOG = LoggerFactory.getLogger(OdlInterfaceChangeListener.class);
33
34     private ListenerRegistration<DataChangeListener> listenerRegistration;
35     private final DataBroker broker;
36     private NexthopManager nexthopManager;
37     private IInterfaceManager interfaceManager;
38
39
40     public OdlInterfaceChangeListener(final DataBroker db, NexthopManager nhm, IInterfaceManager ifManager) {
41         super(Interface.class);
42         broker = db;
43         nexthopManager = nhm;
44         interfaceManager = ifManager;
45         registerListener(db);
46     }
47
48     @Override
49     public void close() throws Exception {
50         if (listenerRegistration != null) {
51             try {
52                 listenerRegistration.close();
53             } catch (final Exception e) {
54                 LOG.error("Error when cleaning up DataChangeListener.", e);
55             }
56             listenerRegistration = null;
57         }
58         LOG.info("odlInterface listener Closed");
59     }
60
61
62     private void registerListener(final DataBroker db) {
63         try {
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);
69         }
70     }
71
72     @Override
73     protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
74         LOG.trace("key: " + identifier + ", value=" + intrf );
75
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;
81             if (gwIp != null) {
82                 remoteIp = gwIp;
83             } else {
84                 IpAddress remIp = intfData.getRemoteIp();
85                 remoteIp = (remIp == null) ? null : remIp.toString();
86             }
87             NodeConnectorId ofPort = intrf.getAugmentation(BaseIds.class).getOfPortId();
88             nexthopManager.createRemoteNextHop(intrf.getName(), ofPort.toString(), remoteIp);
89         }
90     }
91
92
93     private InstanceIdentifier<Interface> getWildCardPath() {
94         return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
95     }
96
97     @Override
98     protected void remove(InstanceIdentifier<Interface> identifier,
99             Interface intrf) {
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();
105             if (gwIp != null) {
106                 remoteIp = gwIp;
107             }
108             nexthopManager.removeRemoteNextHop(dpnId, remoteIp);
109         }
110     }
111
112     @Override
113     protected void update(InstanceIdentifier<Interface> identifier,
114             Interface original, Interface update) {
115         // TODO Auto-generated method stub
116
117     }
118
119 }