Comment 3 for bug 398867

Revision history for this message
robb1e (robert-clutton) wrote :

@Chris,

In the Cluster.java constructor:

    public Cluster(ClusterInfo clusterInfo) {
        this.eucalyptusConfigurationFileParser = new EucalyptusConfigurationFileParser();
        String minVlanId = eucalyptusConfigurationFileParser.getValue("MIN_VLAN_ID", "10");
        String maxVlanId = eucalyptusConfigurationFileParser.getValue("MAX_VLAN_ID", "4096");

        this.clusterInfo = clusterInfo;
        this.state = new ClusterState(this, Integer.parseInt(minVlanId), Integer.parseInt(maxVlanId));
        this.nodeState = new ClusterNodeState(this);
        this.messageQueue = new ClusterMessageQueue(this);
        this.rscUpdater = new ResourceUpdateCallback(this);
        this.addrUpdater = new AddressUpdateCallback(this);
        this.vmUpdater = new VmUpdateCallback(this);
        this.nodeLogUpdater = new NodeLogCallback(this);
        this.nodeCertUpdater = new NodeCertCallback(this);
        this.nodeMap = new ConcurrentSkipListMap<String, NodeInfo>();
    }

Then in ClusterState constructor:

    public ClusterState(Cluster parent, int minVlanId, int maxVlanId) {
        this.parent = parent;

        LOG.info(String.format("Using min vlan id of %d, and max vlan id of %d", minVlanId, maxVlanId));
        this.availableVlans = new ConcurrentSkipListSet<Integer>();
        for (int i = minVlanId; i < maxVlanId; i++)
            this.availableVlans.add(i);
    }

Then a new configuration file reader:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class EucalyptusConfigurationFileParser {
    private static Log LOG = LogFactory.getLog(EucalyptusConfigurationFileParser.class);
    private static final String DEFAULT_RELATIVE_CONFIG_FILE_PATH = "/etc/eucalyptus/eucalyptus.conf";
    private static final String eucaHome = System.getProperty("euca.home");
    private String absoluteConfigurationFilePath;
    private Map<String, String> configMap = new HashMap<String, String>();

    public EucalyptusConfigurationFileParser() {
        this(eucaHome + DEFAULT_RELATIVE_CONFIG_FILE_PATH);
    }

    public EucalyptusConfigurationFileParser(String absoluteConfigurationFilePath) {
        this.absoluteConfigurationFilePath = absoluteConfigurationFilePath;
        LOG.info(String.format("Set euca config file absolute path to %s", this.absoluteConfigurationFilePath));

        loadConfig();
    }

    public String getValue(String key) {
        return getValue(key, null);
    }

    public String getValue(String key, String defaultValue) {
        String value = configMap.get(key);
        if (value == null)
            return defaultValue;
        return value;
    }

    protected void loadConfig() {
        LOG.info(String.format("Loading euca config from %s", absoluteConfigurationFilePath));
        try {
            FileInputStream fileInputStream = new FileInputStream(new File(absoluteConfigurationFilePath));
            BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream));
            String line;
            while ((line = reader.readLine()) != null) {
                String trimmedLine = line.trim();
                if (trimmedLine.startsWith("#") || trimmedLine.length() < 1)
                    continue;

                String[] parts = line.split("=");
                if (parts.length > 1) {
                    String key = parts[0];
                    String value = parts[1].replaceAll("\"", "");
                    configMap.put(key, value);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(String.format("Error parsing config file %s", absoluteConfigurationFilePath), e);
        }
    }
}

With tests:

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.net.URL;

import org.junit.Before;
import org.junit.Test;

public class EucalyptusConfigurationFileParserTest {
    private EucalyptusConfigurationFileParser eucalyptusConfigurationFileParser;

    @Before
    public void before() {
        URL url = this.getClass().getClassLoader().getResource("com/bt/koala/eucalyptus/conf/test.euca.conf");

        eucalyptusConfigurationFileParser = new EucalyptusConfigurationFileParser(url.getPath());
    }

    /**
     * Should read quoted value from config file
     */
    @Test
    public void shouldRealQuotedValueFromConfigFile() {
        // act
        String res = eucalyptusConfigurationFileParser.getValue("START_CLOUD");

        // assert
        assertEquals("N", res);
    }

    /**
     * Should read non-quoted value from config file
     */
    @Test
    public void shouldRealNonQuotedValueFromConfigFile() {
        // act
        String res = eucalyptusConfigurationFileParser.getValue("NC_PORT");

        // assert
        assertEquals("8775", res);
    }

    /**
     * Should read key with blank value
     */
    @Test
    public void shouldRealKeyWithBlankValue() {
        // act
        String res = eucalyptusConfigurationFileParser.getValue("NODES");

        // assert
        assertEquals("", res);
    }

    /**
     * Should return null for unknonwn key
     */
    @Test
    public void shouldReturnNullForUnknownKey() {
        // act
        String res = eucalyptusConfigurationFileParser.getValue("UNKNOWN");

        // assert
        assertNull(res);
    }

    /**
     * Should return default value for unknonwn key with default value
     */
    @Test
    public void shouldReturnDefaultValueForUnknownKeyWithDefaultValue() {
        // act
        String res = eucalyptusConfigurationFileParser.getValue("UNKNOWN", "dflt");

        // assert
        assertEquals("dflt", res);
    }

    /**
     * Should fail for bad path
     */
    @Test(expected = RuntimeException.class)
    public void shouldFailForBadPath() {
        // setup
        eucalyptusConfigurationFileParser = new EucalyptusConfigurationFileParser("moo");

        // act
        eucalyptusConfigurationFileParser.getValue("NC_PORT");
    }
}