bdfd046b00c052f91dabf8113e6fbcdc029c3831
[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 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;
28
29
30 public class OdlInterfaceChangeListener extends AbstractDataChangeListener<Interface> implements AutoCloseable {
31     private static final Logger LOG = LoggerFactory.getLogger(OdlInterfaceChangeListener.class);
32
33     private ListenerRegistration<DataChangeListener> listenerRegistration;
34     private final DataBroker broker;
35     private NexthopManager nexthopManager;
36     private IInterfaceManager interfaceManager;
37
38
39     public OdlInterfaceChangeListener(final DataBroker db, NexthopManager nhm, IInterfaceManager ifManager) {
40         super(Interface.class);
41         broker = db;
42         nexthopManager = nhm;
43         interfaceManager = ifManager;
44         registerListener(db);
45     }
46
47     @Override
48     public void close() throws Exception {
49         if (listenerRegistration != null) {
50             try {
51                 listenerRegistration.close();
52             } catch (final Exception e) {
53                 LOG.error("Error when cleaning up DataChangeListener.", e);
54             }
55             listenerRegistration = null;
56         }
57         LOG.info("odlInterface listener Closed");
58     }
59
60
61     private void registerListener(final DataBroker db) {
62         try {
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);
68         }
69     }
70
71     @Override
72     protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
73         LOG.trace("Adding Interface : key: " + identifier + ", value=" + intrf );
74
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());
81         }
82     }
83
84
85     private InstanceIdentifier<Interface> getWildCardPath() {
86         return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
87     }
88
89     @Override
90     protected void remove(InstanceIdentifier<Interface> identifier,
91             Interface intrf) {
92         LOG.trace("Removing interface : key: " + identifier + ", value=" + intrf );
93         if (intrf.getType().equals(L3tunnel.class)) {
94             BigInteger dpnId = interfaceManager.getDpnForInterface(intrf.getName());
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());
99         }
100     }
101
102     @Override
103     protected void update(InstanceIdentifier<Interface> identifier,
104             Interface original, Interface update) {
105         // TODO Auto-generated method stub
106
107     }
108
109 }