Search in sources :

Example 6 with ConfigProperty

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

the class ComponentConfigManager method reset.

// Locates the property in the component map and replaces it with an
// reset copy.
private void reset(String componentName, String name) {
    Map<String, ConfigProperty> map = properties.get(componentName);
    if (map != null) {
        ConfigProperty prop = map.get(name);
        if (prop != null) {
            map.put(name, ConfigProperty.resetProperty(prop));
            accumulator.add(componentName);
            return;
        }
        log.warn("Unable to reset non-existent property {} for component {}", name, componentName);
    }
}
Also used : ConfigProperty(org.onosproject.cfg.ConfigProperty)

Example 7 with ConfigProperty

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

the class ComponentConfigManager method set.

// Locates the property in the component map and replaces it with an
// updated copy.
private void set(String componentName, String name, String value) {
    Map<String, ConfigProperty> map = properties.get(componentName);
    if (map != null) {
        ConfigProperty prop = map.get(name);
        if (prop != null) {
            map.put(name, ConfigProperty.setProperty(prop, value));
            accumulator.add(componentName);
            return;
        }
    }
    // If definition doesn't exist in local catalog, cache the property.
    preSet(componentName, name, value);
}
Also used : ConfigProperty(org.onosproject.cfg.ConfigProperty)

Example 8 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 9 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)

Example 10 with ConfigProperty

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

the class ComponentConfigManager method loadExistingValues.

// Loads existing property values that may have been learned from other
// nodes in the cluster before the local property registration.
private void loadExistingValues(String componentName, Map<String, ConfigProperty> map) {
    try {
        Configuration cfg = cfgAdmin.getConfiguration(componentName, null);
        Dictionary<String, Object> localProperties = cfg.getProperties();
        // Iterate over all registered config properties...
        for (ConfigProperty p : ImmutableSet.copyOf(map.values())) {
            String name = p.name();
            String globalValue = store.getProperty(componentName, name);
            String localValue = localProperties != null ? (String) localProperties.get(name) : null;
            log.debug("Processing {} property {}; global {}; local {}", componentName, name, globalValue, localValue);
            try {
                // not the same as the default, reset local value to default.
                if (Objects.equals(globalValue, p.defaultValue()) || (globalValue == null && !Objects.equals(localValue, p.defaultValue()))) {
                    log.debug("Resetting {} property {}; value same as default", componentName, name);
                    reset(componentName, name);
                } else if (!Objects.equals(globalValue, localValue)) {
                    // If the local value is different from global value
                    // validate the global value and apply it locally.
                    String newValue;
                    if (globalValue != null) {
                        newValue = globalValue;
                    } else {
                        newValue = localValue;
                    }
                    log.debug("Setting {} property {} to {}", componentName, name, newValue);
                    checkValidity(componentName, name, newValue);
                    set(componentName, name, newValue);
                } else {
                    // Otherwise, simply update the registered property
                    // with the global value.
                    log.debug("Syncing {} property {} to {}", componentName, name, globalValue);
                    map.put(name, ConfigProperty.setProperty(p, globalValue));
                }
            } catch (IllegalArgumentException e) {
                log.warn("Value {} for property {} is not a valid {}; using default", globalValue, name, p.type());
                reset(componentName, name);
            }
        }
    } catch (IOException e) {
        log.error("Unable to get configuration for " + componentName, e);
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) 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