NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / neutronvpn / impl / src / main / java / org / opendaylight / netvirt / neutronvpn / NeutronHostConfigChangeListener.java
index f54b007ed3aecdc00abd070ebc6c6d6a0d949391..cb118ce9c7be83261d3f05ae6df2543feeb1ad24 100644 (file)
@@ -10,14 +10,16 @@ package org.opendaylight.netvirt.neutronvpn;
 import java.util.HashMap;
 import java.util.Locale;
 import java.util.Map;
-import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import javax.inject.Singleton;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.infrautils.utils.concurrent.Executors;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
+import org.opendaylight.serviceutils.tools.listener.AbstractAsyncDataTreeChangeListener;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.Hostconfigs;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.Hostconfig;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.HostconfigBuilder;
@@ -33,8 +35,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Singleton
-public class NeutronHostConfigChangeListener
-        extends AsyncDataTreeChangeListenerBase<Node,NeutronHostConfigChangeListener> {
+public class NeutronHostConfigChangeListener extends AbstractAsyncDataTreeChangeListener<Node> {
     private static final Logger LOG = LoggerFactory.getLogger(NeutronHostConfigChangeListener.class);
     private static final String OS_HOST_CONFIG_HOST_ID_KEY = "odl_os_hostconfig_hostid";
     private static final String OS_HOST_CONFIG_CONFIG_KEY_PREFIX = "odl_os_hostconfig_config_odl_";
@@ -52,45 +53,37 @@ public class NeutronHostConfigChangeListener
 
     @Inject
     public NeutronHostConfigChangeListener(final DataBroker dataBroker) {
-        super(Node.class,NeutronHostConfigChangeListener.class);
+        super(dataBroker, LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(NetworkTopology.class)
+                .child(Topology.class,new TopologyKey(SouthboundUtils.OVSDB_TOPOLOGY_ID)).child(Node.class),
+                Executors.newSingleThreadExecutor("NeutronHostConfigChangeListener", LOG));
         this.dataBroker = dataBroker;
         this.mdsalUtils = new MdsalUtils(dataBroker);
         this.southboundUtils = new SouthboundUtils(mdsalUtils);
     }
 
-    @Override
-    @PostConstruct
     public void init() {
         LOG.info("{} init", getClass().getSimpleName());
-        registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
     }
 
     @Override
-    protected InstanceIdentifier<Node> getWildCardPath() {
-        return InstanceIdentifier
-                .create(NetworkTopology.class)
-                .child(Topology.class,new TopologyKey(SouthboundUtils.OVSDB_TOPOLOGY_ID))
-                .child(Node.class);
+    @PreDestroy
+    public void close() {
+        super.close();
+        Executors.shutdownAndAwaitTermination(getExecutorService());
     }
 
     @Override
-    protected NeutronHostConfigChangeListener getDataTreeChangeListener() {
-        return NeutronHostConfigChangeListener.this;
-    }
-
-
-    @Override
-    protected void remove(InstanceIdentifier<Node> identifier, Node del) {
+    public void remove(InstanceIdentifier<Node> identifier, Node del) {
         updateHostConfig(del, Action.DELETE);
     }
 
     @Override
-    protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
+    public void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
         updateHostConfig(update, Action.UPDATE);
     }
 
     @Override
-    protected void add(InstanceIdentifier<Node> identifier, Node add) {
+    public void add(InstanceIdentifier<Node> identifier, Node add) {
         updateHostConfig(add, Action.ADD);
 
     }
@@ -150,7 +143,7 @@ public class NeutronHostConfigChangeListener
                 LOG.trace("Delete Node: result: {}", result);
                 break;
             default:
-                LOG.warn("Invalid action: %s", action);
+                LOG.warn("Invalid action: {}", action);
                 break;
         }
     }
@@ -163,6 +156,7 @@ public class NeutronHostConfigChangeListener
         return hostconfigBuilder.build();
     }
 
+    @Nullable
     private String getExternalId(Node node, String key) {
         OvsdbNodeAugmentation ovsdbNode = getOvsdbNodeAugmentation(node);
         if (ovsdbNode != null && ovsdbNode.getOpenvswitchExternalIds() != null) {
@@ -175,6 +169,7 @@ public class NeutronHostConfigChangeListener
         return null;
     }
 
+    @Nullable
     private OvsdbNodeAugmentation getOvsdbNodeAugmentation(Node node) {
         OvsdbNodeAugmentation ovsdbNode = southboundUtils.extractOvsdbNode(node);
         if (ovsdbNode == null) {
@@ -189,6 +184,6 @@ public class NeutronHostConfigChangeListener
     private InstanceIdentifier<Hostconfig> createInstanceIdentifier(Hostconfig hostconfig) {
         return InstanceIdentifier.create(Neutron.class)
                 .child(Hostconfigs.class)
-                .child(Hostconfig.class, hostconfig.getKey());
+                .child(Hostconfig.class, hostconfig.key());
     }
 }