8c6c34b0f470ed05c39f76fa32c70be5f847eb4b
[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
13 import com.google.common.base.Optional;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.vpnservice.interfacemgr.interfaces.IInterfaceManager;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
26 import org.opendaylight.yangtools.concepts.ListenerRegistration;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31
32 public class OdlInterfaceChangeListener extends AbstractDataChangeListener<Interface> implements AutoCloseable {
33     private static final Logger LOG = LoggerFactory.getLogger(OdlInterfaceChangeListener.class);
34
35     private ListenerRegistration<DataChangeListener> listenerRegistration;
36     private final DataBroker broker;
37     private NexthopManager nexthopManager;
38     private IInterfaceManager interfaceManager;
39
40
41     public OdlInterfaceChangeListener(final DataBroker db, NexthopManager nhm, IInterfaceManager ifManager) {
42         super(Interface.class);
43         broker = db;
44         nexthopManager = nhm;
45         interfaceManager = ifManager;
46         registerListener(db);
47     }
48
49     @Override
50     public void close() throws Exception {
51         if (listenerRegistration != null) {
52             try {
53                 listenerRegistration.close();
54             } catch (final Exception e) {
55                 LOG.error("Error when cleaning up DataChangeListener.", e);
56             }
57             listenerRegistration = null;
58         }
59         LOG.info("odlInterface listener Closed");
60     }
61
62
63     private void registerListener(final DataBroker db) {
64         try {
65             listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
66                     getWildCardPath(), OdlInterfaceChangeListener.this, DataChangeScope.SUBTREE);
67         } catch (final Exception e) {
68             LOG.error("Nexthop Manager Interfaces DataChange listener registration fail!", e);
69             throw new IllegalStateException("Nexthop Manager registration Listener failed.", e);
70         }
71     }
72
73     @Override
74     protected void add(InstanceIdentifier<Interface> identifier, Interface interfaceInfo) {
75         LOG.trace("Adding Interface : key: " + identifier + ", value=" + interfaceInfo );
76         // READ interface config information
77         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface intrf =
78                 getInterfaceFromConfigDS(nexthopManager, new InterfaceKey(interfaceInfo.getName()));
79
80         if(intrf != null && intrf.getType().equals(Tunnel.class)){
81             IfTunnel intfData = intrf.getAugmentation(IfTunnel.class);
82             IpAddress gatewayIp = intfData.getTunnelGateway();
83             IpAddress remoteIp = (gatewayIp == null) ? intfData.getTunnelDestination() : gatewayIp;
84             nexthopManager.createRemoteNextHop(intrf.getName(), remoteIp.getIpv4Address().getValue());
85         }
86     }
87
88     private InstanceIdentifier<Interface> getWildCardPath() {
89         return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
90     }
91
92     @Override
93     protected void remove(InstanceIdentifier<Interface> identifier,
94             Interface interfaceInfo) {
95         LOG.trace("Removing interface : key: " + identifier + ", value=" + interfaceInfo );
96         // READ interface config information
97         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface intrf =
98                 getInterfaceFromConfigDS(nexthopManager, new InterfaceKey(interfaceInfo.getName()));
99
100         if (intrf != null && intrf.getType().equals(Tunnel.class)) {
101             BigInteger dpnId = interfaceManager.getDpnForInterface(intrf);
102             IfTunnel intfData = intrf.getAugmentation(IfTunnel.class);
103             IpAddress gatewayIp = intfData.getTunnelGateway();
104             IpAddress remoteIp = (gatewayIp == null) ? intfData.getTunnelDestination() : gatewayIp;
105             nexthopManager.removeRemoteNextHop(dpnId, intrf.getName(), remoteIp.getIpv4Address().getValue());
106         }
107     }
108
109     public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface getInterfaceFromConfigDS(NexthopManager nexthopManager, InterfaceKey interfaceKey) {
110         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface> interfaceId =
111                 getInterfaceIdentifier(interfaceKey);
112         Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface> interfaceOptional =
113                 nexthopManager.read(LogicalDatastoreType.CONFIGURATION, interfaceId);
114         if (!interfaceOptional.isPresent()) {
115             return null;
116         }
117
118         return interfaceOptional.get();
119     }
120
121     public static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface> getInterfaceIdentifier(InterfaceKey interfaceKey) {
122         InstanceIdentifier.InstanceIdentifierBuilder<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface> interfaceInstanceIdentifierBuilder =
123                 InstanceIdentifier.builder(Interfaces.class).child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface.class, interfaceKey);
124         return interfaceInstanceIdentifierBuilder.build();
125     }
126
127     @Override
128     protected void update(InstanceIdentifier<Interface> identifier,
129             Interface original, Interface update) {
130         // TODO Auto-generated method stub
131
132     }
133
134 }