Merge "ConfigurationService to create default config dir"
authorAlessandro Boch <aboch@cisco.com>
Wed, 19 Feb 2014 21:42:43 +0000 (21:42 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Wed, 19 Feb 2014 21:42:43 +0000 (21:42 +0000)
opendaylight/commons/opendaylight/pom.xml
opendaylight/configuration/api/pom.xml
opendaylight/configuration/api/src/main/java/org/opendaylight/controller/configuration/IConfigurationContainerService.java
opendaylight/configuration/implementation/pom.xml
opendaylight/configuration/implementation/src/main/java/org/opendaylight/controller/configuration/internal/ConfigurationService.java
opendaylight/configuration/implementation/src/main/java/org/opendaylight/controller/configuration/internal/ContainerConfigurationService.java

index d32be92..328dfaf 100644 (file)
@@ -98,7 +98,7 @@
     <forwardingrulesmanager.version>0.5.1-SNAPSHOT</forwardingrulesmanager.version>
     <statisticsmanager.version>0.5.1-SNAPSHOT</statisticsmanager.version>
     <clustering.services.version>0.5.1-SNAPSHOT</clustering.services.version>
-    <configuration.version>0.4.2-SNAPSHOT</configuration.version>
+    <configuration.version>0.4.3-SNAPSHOT</configuration.version>
     <topologymanager.version>0.4.2-SNAPSHOT</topologymanager.version>
     <protocol_plugin.stub.version>0.4.2-SNAPSHOT</protocol_plugin.stub.version>
     <clustering.stub.version>0.4.2-SNAPSHOT</clustering.stub.version>
index 97b2d25..8a5cd99 100644 (file)
@@ -15,7 +15,7 @@
   </scm>
 
   <artifactId>configuration</artifactId>
-  <version>0.4.2-SNAPSHOT</version>
+  <version>0.4.3-SNAPSHOT</version>
   <packaging>bundle</packaging>
 
   <build>
index ee571b8..b8461ce 100644 (file)
@@ -15,10 +15,20 @@ package org.opendaylight.controller.configuration;
 public interface IConfigurationContainerService extends IConfigurationServiceCommon {
 
     /**
-     * Bundle will call this function to ask ContainerConfigurationService to provide the
-     * directory location of container
+     * This function returns the path to the configuration directory of the
+     * current container.
      *
-     * @return The path to active container directory
+     * @return The path to active container's configuration directory
      */
-    String getConfigurationRoot();
+    public String getConfigurationRoot();
+
+    /**
+     * Function checks whether there exists a saved configuration for this
+     * container (This is essentially checking whether the container's root
+     * configuration directory exists)
+     *
+     * @return True iff container config has been saved at least once
+     */
+    public boolean hasBeenSaved();
+
 }
index 8259243..0846dc5 100644 (file)
@@ -15,7 +15,7 @@
   </scm>
 
   <artifactId>configuration.implementation</artifactId>
-  <version>0.4.2-SNAPSHOT</version>
+  <version>0.4.3-SNAPSHOT</version>
   <packaging>bundle</packaging>
 
   <build>
index 4c0f3a2..dd73675 100644 (file)
@@ -85,6 +85,9 @@ public class ConfigurationService implements IConfigurationService, ICacheUpdate
 
     public void init() {
         logger.info("ConfigurationService Manager init");
+
+        // Create the default startup directory, so that container unaware apps can initiate save
+        createContainerDirectory(ROOT + GlobalConstants.DEFAULT.toString());
     }
 
     public void start() {
@@ -112,7 +115,7 @@ public class ConfigurationService implements IConfigurationService, ICacheUpdate
         List<String> containerList = new ArrayList<String>();
         for (IConfigurationAware configurationAware : this.configurationAwareList) {
             if (configurationAware instanceof IConfigurationContainerService) {
-                String containerFilePath = ((ContainerConfigurationService)configurationAware).getConfigurationRoot();
+                String containerFilePath = ((IConfigurationContainerService)configurationAware).getConfigurationRoot();
                 containerList.add(containerFilePath);
             }
         }
@@ -120,12 +123,21 @@ public class ConfigurationService implements IConfigurationService, ICacheUpdate
     }
 
     private void createContainerDirectory(IConfigurationAware configurationAware) {
-        String containerFilePath = ((ContainerConfigurationService) configurationAware).getConfigurationRoot();
-        if (!new File(containerFilePath).exists()) {
-            boolean created = new File(containerFilePath).mkdir();
-            if (!created) {
-               logger.error("Failed to create startup config directory: {}", containerFilePath);
+        String containerFilePath = ((IConfigurationContainerService) configurationAware).getConfigurationRoot();
+        createContainerDirectory(containerFilePath);
+    }
+
+    private void createContainerDirectory(String containerFilePath) {
+
+        try {
+            if (!new File(containerFilePath).exists()) {
+                boolean created = new File(containerFilePath).mkdir();
+                if (!created) {
+                    logger.error("Failed to create config directory: {}", containerFilePath);
+                }
             }
+        } catch (Exception e) {
+            logger.error("Failed to create config directory: {} ({})", containerFilePath, e.getMessage());
         }
     }
 
index 3e06725..a36d4cc 100644 (file)
@@ -9,6 +9,7 @@
 
 package org.opendaylight.controller.configuration.internal;
 
+import java.io.File;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Dictionary;
@@ -57,6 +58,7 @@ public class ContainerConfigurationService implements IConfigurationContainerSer
             .synchronizedSet(new HashSet<IConfigurationContainerAware>());
     private ObjectReader objReader;
     private ObjectWriter objWriter;
+    private String containerName;
 
     public void addConfigurationContainerAware(
             IConfigurationContainerAware configurationAware) {
@@ -88,7 +90,7 @@ public class ContainerConfigurationService implements IConfigurationContainerSer
 
     void init(Component c) {
         Dictionary<?, ?> props = c.getServiceProperties();
-        String containerName = (props != null) ? (String) props.get("containerName") :
+        containerName = (props != null) ? (String) props.get("containerName") :
             GlobalConstants.DEFAULT.toString();
         root =  String.format("%s%s/", GlobalConstants.STARTUPHOME.toString(), containerName);
     }
@@ -127,13 +129,14 @@ public class ContainerConfigurationService implements IConfigurationContainerSer
             Status status = configurationAware.saveConfiguration();
             if (!status.isSuccess()) {
                 success = false;
-                logger.warn("Failed to save config for {}", configurationAware.getClass().getSimpleName());
+                logger.warn("Failed to save config for {} ({})", configurationAware.getClass().getSimpleName(),
+                        status.getDescription());
             }
         }
         if (success) {
             return new Status(StatusCode.SUCCESS);
         } else {
-            return new Status(StatusCode.INTERNALERROR, "Failed to Save All Configurations");
+            return new Status(StatusCode.INTERNALERROR, "Failed to save one or more configurations");
         }
     }
 
@@ -200,6 +203,10 @@ public class ContainerConfigurationService implements IConfigurationContainerSer
 
     @Override
     public Status persistConfiguration(List<ConfigurationObject> config, String fileName) {
+        if (!hasBeenSaved()) {
+            return new Status(StatusCode.NOTALLOWED,
+                    String.format("Container %s has not been saved yet", containerName));
+        }
         String destination = String.format("%s%s", root, fileName);
         return objWriter.write(config, destination);
     }
@@ -219,4 +226,15 @@ public class ContainerConfigurationService implements IConfigurationContainerSer
         }
         return (List<ConfigurationObject>) obj;
     }
+
+    @Override
+    public boolean hasBeenSaved() {
+        try {
+            File configRoot = new File(this.getConfigurationRoot());
+            return configRoot.exists();
+        } catch (Exception e) {
+            return false;
+        }
+
+    }
 }