Search in sources :

Example 11 with ConfigProperty

use of org.onosproject.cfg.ConfigProperty in project onos by opennetworkinglab.

the class ComponentConfigManager method checkValidity.

// Checks whether the value of the specified configuration property is a valid one or not.
private void checkValidity(String componentName, String name, String newValue) {
    Map<String, ConfigProperty> map = properties.get(componentName);
    if (map == null) {
        return;
    }
    ConfigProperty prop = map.get(name);
    ConfigProperty.Type type = prop.type();
    try {
        switch(type) {
            case INTEGER:
                Integer.parseInt(newValue);
                break;
            case LONG:
                Long.parseLong(newValue);
                break;
            case FLOAT:
                Float.parseFloat(newValue);
                break;
            case DOUBLE:
                Double.parseDouble(newValue);
                break;
            case BOOLEAN:
                if (!((newValue != null) && (newValue.equalsIgnoreCase("true") || newValue.equalsIgnoreCase("false")))) {
                    throw new IllegalArgumentException("Invalid " + type + " value");
                }
                break;
            case STRING:
            case BYTE:
                // do nothing
                break;
            default:
                log.warn("Not a valid config property type");
                break;
        }
    } catch (NumberFormatException e) {
        throw new NumberFormatException("Invalid " + type + " value");
    }
}
Also used : ConfigProperty(org.onosproject.cfg.ConfigProperty)

Example 12 with ConfigProperty

use of org.onosproject.cfg.ConfigProperty in project onos by opennetworkinglab.

the class ComponentConfigManager method registerProperties.

@Override
public void registerProperties(Class<?> componentClass) {
    checkPermission(CONFIG_WRITE);
    String componentName = componentClass.getName();
    String resourceName = componentClass.getSimpleName() + RESOURCE_EXT;
    if (componentClass.getResource(resourceName) == null) {
        // Try looking in my/package/name/MyClass.cfgdef
        resourceName = componentClass.getCanonicalName().replace('.', '/') + RESOURCE_EXT;
    }
    try (InputStream ris = componentClass.getResourceAsStream(resourceName)) {
        checkArgument(ris != null, "Property definitions not found at resource %s", resourceName);
        // Read the definitions
        Set<ConfigProperty> defs = ConfigPropertyDefinitions.read(ris);
        // Produce a new map of the properties and register it.
        Map<String, ConfigProperty> map = Maps.newConcurrentMap();
        defs.forEach(p -> map.put(p.name(), p));
        properties.put(componentName, map);
        loadExistingValues(componentName, map);
    } catch (IOException e) {
        log.error("Unable to read property definitions from resource " + resourceName, e);
    }
}
Also used : InputStream(java.io.InputStream) ConfigProperty(org.onosproject.cfg.ConfigProperty) IOException(java.io.IOException)

Aggregations

ConfigProperty (org.onosproject.cfg.ConfigProperty)12 ComponentConfigService (org.onosproject.cfg.ComponentConfigService)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 IOException (java.io.IOException)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 InputStream (java.io.InputStream)2 Set (java.util.Set)2 Configuration (org.osgi.service.cm.Configuration)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Strings.isNullOrEmpty (com.google.common.base.Strings.isNullOrEmpty)1 Maps (com.google.common.collect.Maps)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 Dictionary (java.util.Dictionary)1