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);
}
}
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);
}
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");
}
}
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);
}
}
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);
}
}
Aggregations