Search in sources :

Example 1 with OCD

use of org.eclipse.kura.configuration.metatype.OCD in project kura by eclipse.

the class ComponentUtil method getObjectClassDefinition.

/**
 * Returns a Map with all the MetaType Object Class Definitions contained in the bundle.
 *
 * @param ctx
 * @param bnd
 * @return
 */
public static Map<String, OCD> getObjectClassDefinition(BundleContext ctx, Bundle bnd) {
    Map<String, OCD> bundleDefaults = new HashMap<String, OCD>();
    ServiceReference<MetaTypeService> ref = ctx.getServiceReference(MetaTypeService.class);
    MetaTypeService metaTypeService = ctx.getService(ref);
    MetaTypeInformation mti = metaTypeService.getMetaTypeInformation(bnd);
    if (mti != null) {
        String[] pids = mti.getPids();
        if (pids != null) {
            for (String pid : pids) {
                OCD ocd = null;
                try {
                    ocd = readObjectClassDefinition(bnd, pid);
                    if (ocd != null) {
                        bundleDefaults.put(pid, ocd);
                    }
                } catch (Exception e) {
                    // ignore: OCD for the specified pid is not found
                    s_logger.warn("Error loading OCD for pid " + pid, e);
                }
            }
        }
    }
    return bundleDefaults;
}
Also used : OCD(org.eclipse.kura.configuration.metatype.OCD) MetaTypeService(org.osgi.service.metatype.MetaTypeService) HashMap(java.util.HashMap) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation) KuraException(org.eclipse.kura.KuraException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException)

Example 2 with OCD

use of org.eclipse.kura.configuration.metatype.OCD in project kura by eclipse.

the class ConfigurationServiceImpl method updateConfigurationInternal.

private void updateConfigurationInternal(String pid, Map<String, Object> properties, boolean snapshotOnConfirmation) throws KuraException {
    s_logger.debug("Attempting update configuration for {}", pid);
    if (!this.m_allActivatedPids.contains(pid)) {
        s_logger.info("UpdatingConfiguration ignored as ConfigurableComponent {} is NOT tracked.", pid);
        return;
    }
    if (properties == null) {
        s_logger.info("UpdatingConfiguration ignored as properties for ConfigurableComponent {} are NULL.", pid);
        return;
    }
    // get the OCD from the registered ConfigurableComponents
    OCD registerdOCD = getRegisteredOCD(pid);
    if (registerdOCD == null) {
        s_logger.info("UpdatingConfiguration ignored as OCD for pid {} cannot be found.", pid);
        return;
    }
    Map<String, Object> mergedProperties = new HashMap<String, Object>();
    mergeWithDefaults(registerdOCD, mergedProperties);
    if (!this.m_activatedSelfConfigComponents.contains(pid)) {
        try {
            // get the current running configuration for the selected component
            Configuration config = this.m_configurationAdmin.getConfiguration(this.m_servicePidByPid.get(pid), "?");
            Map<String, Object> runningProps = CollectionsUtil.dictionaryToMap(config.getProperties(), registerdOCD);
            mergedProperties.putAll(runningProps);
        } catch (IOException e) {
            s_logger.info("merge with running failed!");
            throw new KuraException(KuraErrorCode.CONFIGURATION_UPDATE, e, pid);
        }
    }
    mergedProperties.putAll(properties);
    try {
        updateComponentConfiguration(pid, mergedProperties, snapshotOnConfirmation);
        s_logger.info("Updating Configuration of ConfigurableComponent {} ... Done.", pid);
    } catch (IOException e) {
        s_logger.warn("Error updating Configuration of ConfigurableComponent with pid {}", pid, e);
        throw new KuraException(KuraErrorCode.CONFIGURATION_UPDATE, e, pid);
    }
}
Also used : OCD(org.eclipse.kura.configuration.metatype.OCD) ComponentConfiguration(org.eclipse.kura.configuration.ComponentConfiguration) Configuration(org.osgi.service.cm.Configuration) HashMap(java.util.HashMap) KuraException(org.eclipse.kura.KuraException) IOException(java.io.IOException)

Example 3 with OCD

use of org.eclipse.kura.configuration.metatype.OCD in project kura by eclipse.

the class ConfigurationServiceImpl method validateProperties.

private void validateProperties(String pid, ObjectClassDefinition ocd, Map<String, Object> updatedProps) throws KuraException {
    if (ocd != null) {
        // build a map of all the attribute definitions
        Map<String, AttributeDefinition> attrDefs = new HashMap<String, AttributeDefinition>();
        AttributeDefinition[] defs = ocd.getAttributeDefinitions(ObjectClassDefinition.ALL);
        for (AttributeDefinition def : defs) {
            attrDefs.put(def.getID(), def);
        }
        // and validate them against the definition
        for (Entry<String, Object> property : updatedProps.entrySet()) {
            String key = property.getKey();
            AttributeDefinition attrDef = attrDefs.get(key);
            // is attribute undefined?
            if (attrDef == null) {
                // just accept them.
                continue;
            }
            // validate the attribute value
            Object objectValue = property.getValue();
            String stringValue = StringUtil.valueToString(objectValue);
            if (stringValue != null) {
                String result = attrDef.validate(stringValue);
                if (result != null && !result.isEmpty()) {
                    throw new KuraException(KuraErrorCode.CONFIGURATION_ATTRIBUTE_INVALID, attrDef.getID() + ": " + result);
                }
            }
        }
        // make sure all required properties are set
        OCD ocdFull = getOCDForPid(pid);
        if (ocdFull != null) {
            for (AD attrDef : ocdFull.getAD()) {
                // to the required attributes make sure a value is defined.
                if (attrDef.isRequired()) {
                    if (updatedProps.get(attrDef.getId()) == null) {
                        // exception.
                        throw new KuraException(KuraErrorCode.CONFIGURATION_REQUIRED_ATTRIBUTE_MISSING, attrDef.getId());
                    }
                }
            }
        }
    }
}
Also used : OCD(org.eclipse.kura.configuration.metatype.OCD) AD(org.eclipse.kura.configuration.metatype.AD) HashMap(java.util.HashMap) KuraException(org.eclipse.kura.KuraException) AttributeDefinition(org.osgi.service.metatype.AttributeDefinition)

Example 4 with OCD

use of org.eclipse.kura.configuration.metatype.OCD in project kura by eclipse.

the class ConfigurationServiceImpl method createFactoryConfiguration.

@Override
public synchronized void createFactoryConfiguration(String factoryPid, String pid, Map<String, Object> properties, boolean takeSnapshot) throws KuraException {
    if (pid == null) {
        throw new KuraException(KuraErrorCode.INVALID_PARAMETER, "pid cannot be null");
    } else if (this.m_servicePidByPid.containsKey(pid)) {
        throw new KuraException(KuraErrorCode.INVALID_PARAMETER, "pid " + pid + " already exists");
    }
    try {
        // Second argument in createFactoryConfiguration is a bundle location. If left null the new bundle location
        // will be bound to the location of the first bundle that registers a Managed Service Factory with a
        // corresponding PID
        s_logger.info("Creating new configuration for factory pid {} and pid {}", factoryPid, pid);
        String servicePid = this.m_configurationAdmin.createFactoryConfiguration(factoryPid, null).getPid();
        s_logger.info("Updating newly created configuration for pid {}", pid);
        Map<String, Object> mergedProperties = new HashMap<String, Object>();
        if (properties != null) {
            mergedProperties.putAll(properties);
        }
        OCD ocd = this.m_ocds.get(factoryPid);
        mergeWithDefaults(ocd, mergedProperties);
        mergedProperties.put(ConfigurationService.KURA_SERVICE_PID, pid);
        Dictionary<String, Object> dict = CollectionsUtil.mapToDictionary(mergedProperties);
        Configuration config = this.m_configurationAdmin.getConfiguration(servicePid, "?");
        config.update(dict);
        registerComponentConfiguration(pid, servicePid, factoryPid);
        if (takeSnapshot) {
            snapshot();
        }
    } catch (IOException e) {
        throw new KuraException(KuraErrorCode.CONFIGURATION_ERROR, e, "Cannot create component instance for factory " + factoryPid);
    }
}
Also used : OCD(org.eclipse.kura.configuration.metatype.OCD) ComponentConfiguration(org.eclipse.kura.configuration.ComponentConfiguration) Configuration(org.osgi.service.cm.Configuration) HashMap(java.util.HashMap) KuraException(org.eclipse.kura.KuraException) IOException(java.io.IOException)

Example 5 with OCD

use of org.eclipse.kura.configuration.metatype.OCD in project kura by eclipse.

the class ComponentMetaTypeBundleTracker method processBundleMetaType.

// ----------------------------------------------------------------
// 
// Private APIs
// 
// ----------------------------------------------------------------
private void processBundleMetaType(Bundle bundle) {
    // Push the latest configuration merging the properties in ConfigAdmin
    // with the default properties read from the component's meta-type.
    // This allows components to incrementally add new configuration
    // properties in the meta-type.
    // Only the new default properties are merged with the configuration
    // properties in ConfigurationAdmin.
    // Note: configuration properties in snapshots no longer present in
    // the meta-type are not purged.
    Map<String, Tmetadata> metas = ComponentUtil.getMetadata(this.m_context, bundle);
    for (String metatypePid : metas.keySet()) {
        try {
            // register the OCD for all the contained services
            Tmetadata metadata = metas.get(metatypePid);
            if (metadata != null) {
                // check if this component is a factory
                boolean isFactory = false;
                Designate designate = ComponentUtil.getDesignate(metadata, metatypePid);
                if (designate.getFactoryPid() != null && !designate.getFactoryPid().isEmpty()) {
                    isFactory = true;
                }
                // register the pid with the OCD and whether it is a factory
                OCD ocd = ComponentUtil.getOCD(metadata, metatypePid);
                this.m_configurationService.registerComponentOCD(metatypePid, (Tocd) ocd, isFactory);
            }
        } catch (Exception e) {
            s_logger.error("Error seeding configuration for pid: " + metatypePid, e);
        }
    }
}
Also used : OCD(org.eclipse.kura.configuration.metatype.OCD) Tmetadata(org.eclipse.kura.core.configuration.metatype.Tmetadata) Designate(org.eclipse.kura.configuration.metatype.Designate) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

Aggregations

OCD (org.eclipse.kura.configuration.metatype.OCD)14 HashMap (java.util.HashMap)8 ArrayList (java.util.ArrayList)6 KuraException (org.eclipse.kura.KuraException)6 AD (org.eclipse.kura.configuration.metatype.AD)6 ComponentConfiguration (org.eclipse.kura.configuration.ComponentConfiguration)5 GwtConfigParameter (org.eclipse.kura.web.shared.model.GwtConfigParameter)5 IOException (java.io.IOException)4 GwtConfigComponent (org.eclipse.kura.web.shared.model.GwtConfigComponent)4 Icon (org.eclipse.kura.configuration.metatype.Icon)3 Option (org.eclipse.kura.configuration.metatype.Option)3 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)3 ConfigurationService (org.eclipse.kura.configuration.ConfigurationService)2 ServiceReference (org.osgi.framework.ServiceReference)2 Configuration (org.osgi.service.cm.Configuration)2 HashSet (java.util.HashSet)1 JAXBException (javax.xml.bind.JAXBException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 KuraPartialSuccessException (org.eclipse.kura.KuraPartialSuccessException)1 SelfConfiguringComponent (org.eclipse.kura.configuration.SelfConfiguringComponent)1