Natservice module bug fixes
[vpnservice.git] / vpnmanager / vpnmanager-impl / src / main / java / org / opendaylight / vpnservice / RouterInterfaceListener.java
diff --git a/vpnmanager/vpnmanager-impl/src/main/java/org/opendaylight/vpnservice/RouterInterfaceListener.java b/vpnmanager/vpnmanager-impl/src/main/java/org/opendaylight/vpnservice/RouterInterfaceListener.java
new file mode 100644 (file)
index 0000000..d799303
--- /dev/null
@@ -0,0 +1,85 @@
+/*\r
+ * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.\r
+ *\r
+ * This program and the accompanying materials are made available under the\r
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
+ * and is available at http://www.eclipse.org/legal/epl-v10.html\r
+ */\r
+package org.opendaylight.vpnservice;\r
+\r
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;\r
+import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;\r
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;\r
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;\r
+import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;\r
+import org.opendaylight.vpnservice.utilities.InterfaceUtils;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.RouterInterfacesMap;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.router.interfaces.map.RouterInterfaces;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.router.interfaces.map.router.interfaces.Interfaces;\r
+import org.opendaylight.yangtools.concepts.ListenerRegistration;\r
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+public class RouterInterfaceListener extends AbstractDataChangeListener<Interfaces> {\r
+    private static final Logger LOG = LoggerFactory.getLogger(RouterInterfaceListener.class);\r
+    private ListenerRegistration<DataChangeListener> listenerRegistration;\r
+    private DataBroker broker;\r
+    private VpnInterfaceManager vpnInterfaceManager;\r
+\r
+    public RouterInterfaceListener(final DataBroker db) {\r
+        super(Interfaces.class);\r
+        broker = db;\r
+        registerListener(db);\r
+    }\r
+\r
+    void setVpnInterfaceManager(VpnInterfaceManager vpnInterfaceManager) {\r
+        this.vpnInterfaceManager = vpnInterfaceManager;\r
+    }\r
+\r
+    private void registerListener(final DataBroker db) {\r
+        try {\r
+            listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,\r
+                    getWildCardPath(), RouterInterfaceListener.this, DataChangeScope.SUBTREE);\r
+        } catch (final Exception e) {\r
+            LOG.error("Router interface DataChange listener registration fail !", e);\r
+        }\r
+    }\r
+\r
+    private InstanceIdentifier<?> getWildCardPath() {\r
+        return InstanceIdentifier.create(RouterInterfacesMap.class).child(RouterInterfaces.class).child(Interfaces.class);\r
+    }\r
+\r
+    @Override\r
+    protected void add(InstanceIdentifier<Interfaces> identifier, Interfaces interfaceInfo) {\r
+        LOG.trace("Add event - key: {}, value: {}", identifier, interfaceInfo);\r
+        final String routerId = identifier.firstKeyOf(RouterInterfaces.class).getRouterId().getValue();\r
+        String interfaceName = interfaceInfo.getInterfaceId();\r
+\r
+        MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, \r
+                VpnUtil.getRouterInterfaceId(interfaceName), VpnUtil.getRouterInterface(interfaceName, routerId));\r
+\r
+        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface interfaceState =\r
+                InterfaceUtils.getInterfaceStateFromOperDS(broker, interfaceName);\r
+        if (interfaceState != null) {\r
+            LOG.debug("Handling interface {} in router {} add scenario", interfaceName, routerId);\r
+            vpnInterfaceManager.addToNeutronRouterDpnsMap(routerId, interfaceName);\r
+        } else {\r
+            LOG.warn("Interface {} not yet operational to handle router interface add event in router {}", interfaceName, routerId);\r
+        }\r
+    }\r
+\r
+    @Override\r
+    protected void remove(InstanceIdentifier<Interfaces> identifier, Interfaces interfaceInfo) {\r
+        LOG.trace("Remove event - key: {}, value: {}", identifier, interfaceInfo);\r
+        final String routerId = identifier.firstKeyOf(RouterInterfaces.class).getRouterId().getValue();\r
+        String interfaceName = interfaceInfo.getInterfaceId();\r
+        vpnInterfaceManager.removeFromNeutronRouterDpnsMap(routerId, interfaceName);\r
+    }\r
+\r
+    @Override\r
+    protected void update(InstanceIdentifier<Interfaces> identifier, Interfaces original, Interfaces update) {\r
+        LOG.trace("Update event - key: {}, original: {}, update: {}", identifier, original, update);\r
+    }\r
+\r
+}\r