Config save support for networkconfiguration.neutron
[controller.git] / opendaylight / networkconfiguration / neutron / implementation / src / main / java / org / opendaylight / controller / networkconfig / neutron / implementation / NeutronSubnetInterface.java
index 62ef64c74c705fe8d0fb0f92c913d750157ece9c..f908a95c313390e25b340f862d4f63e65999c07c 100644 (file)
@@ -8,6 +8,9 @@
 
 package org.opendaylight.controller.networkconfig.neutron.implementation;
 
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.ObjectInputStream;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Dictionary;
@@ -16,6 +19,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 import java.util.Map.Entry;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
 import org.apache.felix.dm.Component;
@@ -23,16 +27,27 @@ import org.opendaylight.controller.clustering.services.CacheConfigException;
 import org.opendaylight.controller.clustering.services.CacheExistException;
 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
 import org.opendaylight.controller.clustering.services.IClusterServices;
+import org.opendaylight.controller.configuration.IConfigurationContainerAware;
 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
 import org.opendaylight.controller.networkconfig.neutron.INeutronSubnetCRUD;
 import org.opendaylight.controller.networkconfig.neutron.NeutronCRUDInterfaces;
 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
 import org.opendaylight.controller.networkconfig.neutron.NeutronSubnet;
+import org.opendaylight.controller.sal.utils.GlobalConstants;
+import org.opendaylight.controller.sal.utils.IObjectReader;
+import org.opendaylight.controller.sal.utils.ObjectReader;
+import org.opendaylight.controller.sal.utils.ObjectWriter;
+import org.opendaylight.controller.sal.utils.Status;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class NeutronSubnetInterface implements INeutronSubnetCRUD {
+public class NeutronSubnetInterface implements INeutronSubnetCRUD, IConfigurationContainerAware,
+                                               IObjectReader {
     private static final Logger logger = LoggerFactory.getLogger(NeutronSubnetInterface.class);
+    private static String ROOT = GlobalConstants.STARTUPHOME.toString();
+    private static final String FILENAME ="neutron.subnet";
+    private static String fileName;
+
     private String containerName = null;
 
     private IClusterContainerServices clusterContainerService = null;
@@ -100,6 +115,10 @@ public class NeutronSubnetInterface implements INeutronSubnetCRUD {
     private void startUp() {
         allocateCache();
         retrieveCache();
+        if (subnetDB.isEmpty()) {
+            loadConfiguration();
+        }
+
     }
 
     /**
@@ -116,6 +135,7 @@ public class NeutronSubnetInterface implements INeutronSubnetCRUD {
             // In the Global instance case the containerName is empty
             this.containerName = "";
         }
+        fileName = ROOT + FILENAME + "_" + containerName + ".conf";
         startUp();
     }
 
@@ -176,16 +196,19 @@ public class NeutronSubnetInterface implements INeutronSubnetCRUD {
 
     // IfNBSubnetCRUD methods
 
+    @Override
     public boolean subnetExists(String uuid) {
         return subnetDB.containsKey(uuid);
     }
 
+    @Override
     public NeutronSubnet getSubnet(String uuid) {
         if (!subnetExists(uuid))
             return null;
         return subnetDB.get(uuid);
     }
 
+    @Override
     public List<NeutronSubnet> getAllSubnets() {
         Set<NeutronSubnet> allSubnets = new HashSet<NeutronSubnet>();
         for (Entry<String, NeutronSubnet> entry : subnetDB.entrySet()) {
@@ -198,6 +221,7 @@ public class NeutronSubnetInterface implements INeutronSubnetCRUD {
         return ans;
     }
 
+    @Override
     public boolean addSubnet(NeutronSubnet input) {
         String id = input.getID();
         if (subnetExists(id))
@@ -210,6 +234,7 @@ public class NeutronSubnetInterface implements INeutronSubnetCRUD {
         return true;
     }
 
+    @Override
     public boolean removeSubnet(String uuid) {
         if (!subnetExists(uuid))
             return false;
@@ -222,6 +247,7 @@ public class NeutronSubnetInterface implements INeutronSubnetCRUD {
         return true;
     }
 
+    @Override
     public boolean updateSubnet(String uuid, NeutronSubnet delta) {
         if (!subnetExists(uuid))
             return false;
@@ -229,10 +255,38 @@ public class NeutronSubnetInterface implements INeutronSubnetCRUD {
         return overwrite(target, delta);
     }
 
+    @Override
     public boolean subnetInUse(String subnetUUID) {
         if (!subnetExists(subnetUUID))
             return true;
         NeutronSubnet target = subnetDB.get(subnetUUID);
         return (target.getPortsInSubnet().size() > 0);
     }
+
+    @SuppressWarnings("unchecked")
+    private void loadConfiguration() {
+        ObjectReader objReader = new ObjectReader();
+        ConcurrentMap<String, NeutronSubnet> confList = (ConcurrentMap<String, NeutronSubnet>)
+                                                            objReader.read(this, fileName);
+
+        if (confList == null) {
+            return;
+        }
+
+        for (String key : confList.keySet()) {
+            subnetDB.put(key, confList.get(key));
+        }
+    }
+
+    @Override
+    public Status saveConfiguration() {
+        ObjectWriter objWriter = new ObjectWriter();
+        return objWriter.write(new ConcurrentHashMap<String, NeutronSubnet>(subnetDB), fileName);
+    }
+
+    @Override
+    public Object readObject(ObjectInputStream ois) throws FileNotFoundException, IOException, ClassNotFoundException {
+        return ois.readObject();
+    }
+
 }