1. nexthopMgr intg with MdSalUtil & IdMgr
[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.inventory.rev130819.NodeConnectorId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.BaseIds;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.L3tunnel;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL3tunnel;
23 import org.opendaylight.vpnservice.interfacemgr.interfaces.IInterfaceManager;
24 import org.opendaylight.vpnservice.nexthopmgr.AbstractDataChangeListener;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28
29 public class OdlInterfaceChangeListener extends AbstractDataChangeListener<Interface> implements AutoCloseable {
30     private static final Logger LOG = LoggerFactory.getLogger(OdlInterfaceChangeListener.class);
31
32     private ListenerRegistration<DataChangeListener> listenerRegistration;
33     private final DataBroker broker;
34     private NexthopManager nexthopManager;
35     private IInterfaceManager interfaceManager;
36
37
38     public OdlInterfaceChangeListener(final DataBroker db, NexthopManager nhm, IInterfaceManager ifManager) {
39         super(Interface.class);
40         broker = db;
41         nexthopManager = nhm;
42         interfaceManager = ifManager;
43         registerListener(db);
44     }
45
46     @Override
47     public void close() throws Exception {
48         if (listenerRegistration != null) {
49             try {
50                 listenerRegistration.close();
51             } catch (final Exception e) {
52                 LOG.error("Error when cleaning up DataChangeListener.", e);
53             }
54             listenerRegistration = null;
55         }
56         LOG.info("odlInterface listener Closed");
57     }
58
59
60     private void registerListener(final DataBroker db) {
61         try {
62             listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
63                     getWildCardPath(), OdlInterfaceChangeListener.this, DataChangeScope.SUBTREE);
64         } catch (final Exception e) {
65             LOG.error("Nexthop Manager Interfaces DataChange listener registration fail!", e);
66             throw new IllegalStateException("Nexthop Manager registration Listener failed.", e);
67         }
68     }
69
70     @Override
71     protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
72         LOG.info("key: " + identifier + ", value=" + intrf );
73
74         if (intrf.getType().equals(L3tunnel.class)) {
75             IfL3tunnel intfData = intrf.getAugmentation(IfL3tunnel.class);
76             String gwIp = intfData.getGatewayIp().toString();
77             String remoteIp = intfData.getRemoteIp().toString();
78             if (gwIp != null) {
79                 remoteIp = gwIp;
80             }
81             NodeConnectorId ofPort = intrf.getAugmentation(BaseIds.class).getOfPortId();
82             nexthopManager.createRemoteNextHop(intrf.getName(), ofPort.toString(), remoteIp);
83         }
84     }
85
86
87     private InstanceIdentifier<Interface> getWildCardPath() {
88         return InstanceIdentifier.create(Interfaces.class).child(Interface.class);
89     }
90
91     @Override
92     protected void remove(InstanceIdentifier<Interface> identifier,
93             Interface intrf) {
94         if (intrf.getType().equals(L3tunnel.class)) {
95             long dpnId = interfaceManager.getDpnForInterface(intrf.getName());
96             IfL3tunnel intfData = intrf.getAugmentation(IfL3tunnel.class);
97             String gwIp = intfData.getGatewayIp().toString();
98             String remoteIp = intfData.getRemoteIp().toString();
99             if (gwIp != null) {
100                 remoteIp = gwIp;
101             }
102             nexthopManager.removeRemoteNextHop(dpnId, remoteIp);
103         }
104     }
105
106     @Override
107     protected void update(InstanceIdentifier<Interface> identifier,
108             Interface original, Interface update) {
109         // TODO Auto-generated method stub
110
111     }
112
113 }