Natservice module bug fixes
[vpnservice.git] / vpnmanager / vpnmanager-impl / src / main / java / org / opendaylight / vpnservice / RouterInterfaceListener.java
1 /*\r
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.vpnservice;\r
9 \r
10 import org.opendaylight.controller.md.sal.binding.api.DataBroker;\r
11 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;\r
12 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;\r
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;\r
14 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;\r
15 import org.opendaylight.vpnservice.utilities.InterfaceUtils;\r
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.RouterInterfacesMap;\r
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.router.interfaces.map.RouterInterfaces;\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.router.interfaces.map.router.interfaces.Interfaces;\r
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;\r
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;\r
21 import org.slf4j.Logger;\r
22 import org.slf4j.LoggerFactory;\r
23 \r
24 public class RouterInterfaceListener extends AbstractDataChangeListener<Interfaces> {\r
25     private static final Logger LOG = LoggerFactory.getLogger(RouterInterfaceListener.class);\r
26     private ListenerRegistration<DataChangeListener> listenerRegistration;\r
27     private DataBroker broker;\r
28     private VpnInterfaceManager vpnInterfaceManager;\r
29 \r
30     public RouterInterfaceListener(final DataBroker db) {\r
31         super(Interfaces.class);\r
32         broker = db;\r
33         registerListener(db);\r
34     }\r
35 \r
36     void setVpnInterfaceManager(VpnInterfaceManager vpnInterfaceManager) {\r
37         this.vpnInterfaceManager = vpnInterfaceManager;\r
38     }\r
39 \r
40     private void registerListener(final DataBroker db) {\r
41         try {\r
42             listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,\r
43                     getWildCardPath(), RouterInterfaceListener.this, DataChangeScope.SUBTREE);\r
44         } catch (final Exception e) {\r
45             LOG.error("Router interface DataChange listener registration fail !", e);\r
46         }\r
47     }\r
48 \r
49     private InstanceIdentifier<?> getWildCardPath() {\r
50         return InstanceIdentifier.create(RouterInterfacesMap.class).child(RouterInterfaces.class).child(Interfaces.class);\r
51     }\r
52 \r
53     @Override\r
54     protected void add(InstanceIdentifier<Interfaces> identifier, Interfaces interfaceInfo) {\r
55         LOG.trace("Add event - key: {}, value: {}", identifier, interfaceInfo);\r
56         final String routerId = identifier.firstKeyOf(RouterInterfaces.class).getRouterId().getValue();\r
57         String interfaceName = interfaceInfo.getInterfaceId();\r
58 \r
59         MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, \r
60                 VpnUtil.getRouterInterfaceId(interfaceName), VpnUtil.getRouterInterface(interfaceName, routerId));\r
61 \r
62         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface interfaceState =\r
63                 InterfaceUtils.getInterfaceStateFromOperDS(broker, interfaceName);\r
64         if (interfaceState != null) {\r
65             LOG.debug("Handling interface {} in router {} add scenario", interfaceName, routerId);\r
66             vpnInterfaceManager.addToNeutronRouterDpnsMap(routerId, interfaceName);\r
67         } else {\r
68             LOG.warn("Interface {} not yet operational to handle router interface add event in router {}", interfaceName, routerId);\r
69         }\r
70     }\r
71 \r
72     @Override\r
73     protected void remove(InstanceIdentifier<Interfaces> identifier, Interfaces interfaceInfo) {\r
74         LOG.trace("Remove event - key: {}, value: {}", identifier, interfaceInfo);\r
75         final String routerId = identifier.firstKeyOf(RouterInterfaces.class).getRouterId().getValue();\r
76         String interfaceName = interfaceInfo.getInterfaceId();\r
77         vpnInterfaceManager.removeFromNeutronRouterDpnsMap(routerId, interfaceName);\r
78     }\r
79 \r
80     @Override\r
81     protected void update(InstanceIdentifier<Interfaces> identifier, Interfaces original, Interfaces update) {\r
82         LOG.trace("Update event - key: {}, original: {}, update: {}", identifier, original, update);\r
83     }\r
84 \r
85 }\r