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