/** * Normalizes the type of the parameter to the one allowed for configurations. * * The conversion is performed 'best-effort' (e.g. "3" will always end up being a BigDecimal, never a String). * Use {@link #normalizeType(Object, ConfigDescriptionParameter)} to make sure your field type ends up as intended. * * @param value the value to return as normalized type * @return corresponding value as a valid type */ public static Object normalizeType(Object value) { return normalizeType(value, null); }
/** * Create a new configuration. * * @param properties the properties to initialize (may be null) * @param alreadyNormalized flag if the properties are already normalized */ private Configuration(final @Nullable Map<String, Object> properties, final boolean alreadyNormalized) { if (properties == null) { this.properties = new HashMap<>(); } else { if (alreadyNormalized) { this.properties = new HashMap<>(properties); } else { this.properties = ConfigUtil.normalizeTypes(properties); } } }
/** * Normalizes the types to the ones allowed for configurations. * * @param configuration the configuration that needs to be normalzed * @return normalized configuration */ public static Map<String, Object> normalizeTypes(Map<String, Object> configuration) { Map<String, Object> convertedConfiguration = new HashMap<>(configuration.size()); for (Entry<String, Object> parameter : configuration.entrySet()) { String name = parameter.getKey(); Object value = parameter.getValue(); if (!isOSGiConfigParameter(name)) { convertedConfiguration.put(name, normalizeType(value, null)); } } return convertedConfiguration; }
/** * Normalizes the type of the parameter to the one allowed for configurations. * * @param value the value to return as normalized type * @param configDescriptionParameter the parameter that needs to be normalized * @return corresponding value as a valid type * @throws IllegalArgumentException if a invalid type has been given */ public static Object normalizeType(Object value, @Nullable ConfigDescriptionParameter configDescriptionParameter) { if (configDescriptionParameter != null) { Normalizer normalizer = NormalizerFactory.getNormalizer(configDescriptionParameter); return normalizer.normalize(value); } else if (value == null || value instanceof Boolean || value instanceof String || value instanceof BigDecimal) { return value; } else if (value instanceof Number) { return new BigDecimal(value.toString()); } else if (value instanceof Collection) { return normalizeCollection((Collection<?>) value); } throw new IllegalArgumentException( "Invalid type '{" + value.getClass().getCanonicalName() + "}' of configuration value!"); }
public Object put(String key, Object value) { synchronized (this) { return properties.put(key, ConfigUtil.normalizeType(value, null)); } }
String name = parameter.getKey(); Object value = parameter.getValue(); if (!isOSGiConfigParameter(name)) { ConfigDescriptionParameter configDescriptionParameter = configParams.get(name); convertedConfiguration.put(name, normalizeType(value, configDescriptionParameter));
private Map<String, Object> normalizeConfiguration(Map<String, Object> properties, String serviceId) { if (properties == null || properties.isEmpty()) { return properties; } ConfigurableServiceDTO service = getServiceById(serviceId); if (service == null) { return properties; } URI uri; try { uri = new URI(service.configDescriptionURI); } catch (URISyntaxException e) { logger.warn("Not a valid URI: {}", service.configDescriptionURI); return properties; } ConfigDescription configDesc = configDescRegistry.getConfigDescription(uri); if (configDesc == null) { return properties; } return ConfigUtil.normalizeTypes(properties, Collections.singletonList(configDesc)); }
/** * Normalizes a collection. * * @param collection the collection that entries should be normalized * @return a collection that contains the normalized entries * @throws IllegalArgumentException if the type of the normalized values differ or an invalid type has been given */ private static Collection<Object> normalizeCollection(Collection<?> collection) throws IllegalArgumentException { if (collection.size() == 0) { return Collections.emptyList(); } else { final List<Object> lst = new ArrayList<>(collection.size()); for (final Object it : collection) { final Object normalized = normalizeType(it, null); lst.add(normalized); if (normalized.getClass() != lst.get(0).getClass()) { throw new IllegalArgumentException( "Invalid configuration property. Heterogeneous collection value!"); } } return lst; } }
@PUT @Path("/{ruleUID}/config") @Consumes(MediaType.APPLICATION_JSON) @ApiOperation(value = "Sets the rule configuration values.") @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 404, message = "Rule corresponding to the given UID does not found.") }) public Response updateConfiguration( @PathParam("ruleUID") @ApiParam(value = "ruleUID", required = true) String ruleUID, @ApiParam(value = "config") Map<String, Object> configurationParameters) throws IOException { Map<String, Object> config = ConfigUtil.normalizeTypes(configurationParameters); Rule rule = ruleRegistry.get(ruleUID); if (rule == null) { logger.info("Received HTTP PUT request for update config at '{}' for the unknown rule '{}'.", uriInfo.getPath(), ruleUID); return Response.status(Status.NOT_FOUND).build(); } else { rule = RuleBuilder.create(rule).withConfiguration(new Configuration(config)).build(); ruleRegistry.update(rule); return Response.ok(null, MediaType.TEXT_PLAIN).build(); } }
/** * Get the properties and configuration parameters for the thing with the given {@link DiscoveryResult}. * * @param discoveryResult the DiscoveryResult * @param props the location the properties should be stored to. * @param configParams the location the configuration parameters should be stored to. */ private void getPropsAndConfigParams(final DiscoveryResult discoveryResult, final Map<String, String> props, final Map<String, Object> configParams) { final List<ConfigDescriptionParameter> configDescParams = getConfigDescParams(discoveryResult); final Set<String> paramNames = getConfigDescParamNames(configDescParams); final Map<String, Object> resultProps = discoveryResult.getProperties(); for (String resultKey : resultProps.keySet()) { if (paramNames.contains(resultKey)) { ConfigDescriptionParameter param = getConfigDescriptionParam(configDescParams, resultKey); Object normalizedValue = ConfigUtil.normalizeType(resultProps.get(resultKey), param); configParams.put(resultKey, normalizedValue); } else { props.put(resultKey, String.valueOf(resultProps.get(resultKey))); } } }
private Map<String, Object> normalizeConfiguration(Map<String, Object> properties, String bindingId) { if (properties == null || properties.isEmpty()) { return properties; } BindingInfo bindingInfo = this.bindingInfoRegistry.getBindingInfo(bindingId); if (bindingInfo == null || bindingInfo.getConfigDescriptionURI() == null) { return properties; } ConfigDescription configDesc = configDescRegistry.getConfigDescription(bindingInfo.getConfigDescriptionURI()); if (configDesc == null) { return properties; } return ConfigUtil.normalizeTypes(properties, Collections.singletonList(configDesc)); }
/** * Get the properties and configuration parameters for the thing with the given {@link DiscoveryResult}. * * @param discoveryResult the DiscoveryResult * @param props the location the properties should be stored to. * @param configParams the location the configuration parameters should be stored to. */ private void getPropsAndConfigParams(final DiscoveryResult discoveryResult, final Map<String, String> props, final Map<String, Object> configParams) { final List<ConfigDescriptionParameter> configDescParams = getConfigDescParams(discoveryResult); final Set<String> paramNames = getConfigDescParamNames(configDescParams); final Map<String, Object> resultProps = discoveryResult.getProperties(); for (String resultKey : resultProps.keySet()) { if (paramNames.contains(resultKey)) { ConfigDescriptionParameter param = getConfigDescriptionParam(configDescParams, resultKey); Object normalizedValue = ConfigUtil.normalizeType(resultProps.get(resultKey), param); configParams.put(resultKey, normalizedValue); } else { props.put(resultKey, String.valueOf(resultProps.get(resultKey))); } } }
private Map<String, Object> normalizeConfiguration(Map<String, Object> properties, ThingTypeUID thingTypeUID, ThingUID thingUID) { if (properties == null || properties.isEmpty()) { return properties; } ThingType thingType = thingTypeRegistry.getThingType(thingTypeUID); if (thingType == null) { return properties; } List<ConfigDescription> configDescriptions = new ArrayList<>(2); if (thingType.getConfigDescriptionURI() != null) { ConfigDescription typeConfigDesc = configDescRegistry .getConfigDescription(thingType.getConfigDescriptionURI()); if (typeConfigDesc != null) { configDescriptions.add(typeConfigDesc); } } if (getConfigDescriptionURI(thingUID) != null) { ConfigDescription thingConfigDesc = configDescRegistry .getConfigDescription(getConfigDescriptionURI(thingUID)); if (thingConfigDesc != null) { configDescriptions.add(thingConfigDesc); } } if (configDescriptions.isEmpty()) { return properties; } return ConfigUtil.normalizeTypes(properties, configDescriptions); }
@PUT @Path("/{ruleUID}/{moduleCategory}/{id}/config/{param}") @ApiOperation(value = "Sets the module's configuration parameter value.") @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 404, message = "Rule corresponding to the given UID does not found or does not have a module with such Category and ID.") }) @Consumes(MediaType.TEXT_PLAIN) public Response setModuleConfigParam( @PathParam("ruleUID") @ApiParam(value = "ruleUID", required = true) String ruleUID, @PathParam("moduleCategory") @ApiParam(value = "moduleCategory", required = true) String moduleCategory, @PathParam("id") @ApiParam(value = "id", required = true) String id, @PathParam("param") @ApiParam(value = "param", required = true) String param, @ApiParam(value = "value", required = true) String value) { Rule rule = ruleRegistry.get(ruleUID); if (rule != null) { Module module = getModule(rule, moduleCategory, id); if (module != null) { Configuration configuration = module.getConfiguration(); configuration.put(param, ConfigUtil.normalizeType(value)); module = ModuleBuilder.create(module).withConfiguration(configuration).build(); ruleRegistry.update(rule); return Response.ok(null, MediaType.TEXT_PLAIN).build(); } } return Response.status(Status.NOT_FOUND).build(); }
private Map<String, Object> normalizeConfiguration(Map<String, Object> properties, ChannelTypeUID channelTypeUID, ChannelUID channelUID) { if (properties == null || properties.isEmpty()) { return properties; } ChannelType channelType = channelTypeRegistry.getChannelType(channelTypeUID); if (channelType == null) { return properties; } List<ConfigDescription> configDescriptions = new ArrayList<>(2); if (channelType.getConfigDescriptionURI() != null) { ConfigDescription typeConfigDesc = configDescRegistry .getConfigDescription(channelType.getConfigDescriptionURI()); if (typeConfigDesc != null) { configDescriptions.add(typeConfigDesc); } } if (getConfigDescriptionURI(channelUID) != null) { ConfigDescription channelConfigDesc = configDescRegistry .getConfigDescription(getConfigDescriptionURI(channelUID)); if (channelConfigDesc != null) { configDescriptions.add(channelConfigDesc); } } if (configDescriptions.isEmpty()) { return properties; } return ConfigUtil.normalizeTypes(properties, configDescriptions); }
configuration.remove(parameterName); } else { configuration.put(parameterName, ConfigUtil.normalizeType(defaultValue, parameter)); configuration.put(parameterName, ConfigUtil.normalizeType(value, parameter));
private boolean synchronizeConfiguration(ThingTypeUID thingTypeUID, Map<String, Object> properties, Configuration config) { boolean configUpdated = false; final Set<Map.Entry<String, Object>> propertySet = properties.entrySet(); final ThingType thingType = thingTypeRegistry.getThingType(thingTypeUID); final List<ConfigDescriptionParameter> configDescParams = getConfigDescParams(thingType); for (Map.Entry<String, Object> propertyEntry : propertySet) { final String propertyKey = propertyEntry.getKey(); final Object propertyValue = propertyEntry.getValue(); // Check if the key is present in the configuration. if (!config.containsKey(propertyKey)) { continue; } // Normalize first ConfigDescriptionParameter configDescParam = getConfigDescriptionParam(configDescParams, propertyKey); Object normalizedValue = ConfigUtil.normalizeType(propertyValue, configDescParam); // If the value is equal to the one of the configuration, there is nothing to do. if (Objects.equals(normalizedValue, config.get(propertyKey))) { continue; } // - the given key is part of the configuration // - the values differ // update value config.put(propertyKey, normalizedValue); configUpdated = true; } return configUpdated; }
private boolean synchronizeConfiguration(ThingTypeUID thingTypeUID, Map<String, Object> properties, Configuration config) { boolean configUpdated = false; final Set<Map.Entry<String, Object>> propertySet = properties.entrySet(); final ThingType thingType = thingTypeRegistry.getThingType(thingTypeUID); final List<ConfigDescriptionParameter> configDescParams = getConfigDescParams(thingType); for (Map.Entry<String, Object> propertyEntry : propertySet) { final String propertyKey = propertyEntry.getKey(); final Object propertyValue = propertyEntry.getValue(); // Check if the key is present in the configuration. if (!config.containsKey(propertyKey)) { continue; } // Normalize first ConfigDescriptionParameter configDescParam = getConfigDescriptionParam(configDescParams, propertyKey); Object normalizedValue = ConfigUtil.normalizeType(propertyValue, configDescParam); // If the value is equal to the one of the configuration, there is nothing to do. if (Objects.equals(normalizedValue, config.get(propertyKey))) { continue; } // - the given key is part of the configuration // - the values differ // update value config.put(propertyKey, normalizedValue); configUpdated = true; } return configUpdated; }
Object value = ConfigUtil.normalizeType(config.get(propName), cd); if (value != null) { config.put(propName, value);