Search in sources :

Example 1 with ComponentConfiguration

use of org.eclipse.kura.configuration.ComponentConfiguration in project kura by eclipse.

the class ConfigurationServiceImpl method getConfigurableComponentConfiguration.

private ComponentConfiguration getConfigurableComponentConfiguration(String pid) {
    ComponentConfiguration cc = null;
    try {
        Tocd ocd = getOCDForPid(pid);
        String servicePid = this.m_servicePidByPid.get(pid);
        if (servicePid != null) {
            Configuration cfg = this.m_configurationAdmin.getConfiguration(servicePid, "?");
            Map<String, Object> props = CollectionsUtil.dictionaryToMap(cfg.getProperties(), ocd);
            cc = new ComponentConfigurationImpl(pid, ocd, props);
        }
    } catch (Exception e) {
        s_logger.error("Error getting Configuration for component: " + pid + ". Ignoring it.", e);
    }
    return cc;
}
Also used : ComponentConfiguration(org.eclipse.kura.configuration.ComponentConfiguration) ComponentConfiguration(org.eclipse.kura.configuration.ComponentConfiguration) Configuration(org.osgi.service.cm.Configuration) Tocd(org.eclipse.kura.core.configuration.metatype.Tocd) KuraException(org.eclipse.kura.KuraException) XMLStreamException(javax.xml.stream.XMLStreamException) KuraPartialSuccessException(org.eclipse.kura.KuraPartialSuccessException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ComponentException(org.osgi.service.component.ComponentException) IOException(java.io.IOException)

Example 2 with ComponentConfiguration

use of org.eclipse.kura.configuration.ComponentConfiguration in project kura by eclipse.

the class ConfigurationServiceImpl method loadLatestSnapshotInConfigAdmin.

private void loadLatestSnapshotInConfigAdmin() throws KuraException {
    // 
    // save away initial configuration
    List<ComponentConfigurationImpl> configs = loadLatestSnapshotConfigurations();
    if (configs == null) {
        return;
    }
    for (ComponentConfiguration config : configs) {
        if (config != null) {
            Map<String, Object> props = config.getConfigurationProperties();
            if (props != null) {
                String factoryPid = (String) props.get(ConfigurationAdmin.SERVICE_FACTORYPID);
                if (factoryPid != null) {
                    String pid = config.getPid();
                    s_logger.info("Creating configuration with pid: {} and factory pid: {}", pid, factoryPid);
                    try {
                        createFactoryConfiguration(factoryPid, pid, props, false);
                    } catch (KuraException e) {
                        s_logger.warn("Error creating configuration with pid: " + pid + " and factory pid: {}", factoryPid, e);
                    }
                } else {
                    try {
                        s_logger.debug("Pushing config to config admin: {}", config.getPid());
                        // push it to the ConfigAdmin
                        Configuration cfg = this.m_configurationAdmin.getConfiguration(config.getPid(), "?");
                        // set kura.service.pid if missing
                        Map<String, Object> newProperties = new HashMap<String, Object>(props);
                        if (!newProperties.containsKey(ConfigurationService.KURA_SERVICE_PID)) {
                            newProperties.put(ConfigurationService.KURA_SERVICE_PID, config.getPid());
                        }
                        cfg.update(CollectionsUtil.mapToDictionary(newProperties));
                    } catch (IOException e) {
                        s_logger.warn("Error seeding initial properties to ConfigAdmin for pid: {}", config.getPid(), e);
                    }
                }
            }
        }
    }
}
Also used : ComponentConfiguration(org.eclipse.kura.configuration.ComponentConfiguration) 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 ComponentConfiguration

use of org.eclipse.kura.configuration.ComponentConfiguration in project kura by eclipse.

the class ConfigurationServiceImpl method buildCurrentConfiguration.

private synchronized List<ComponentConfiguration> buildCurrentConfiguration(List<ComponentConfiguration> configsToUpdate) throws KuraException {
    List<ComponentConfiguration> result = new ArrayList<ComponentConfiguration>();
    // Merge the current configuration of registered components with the provided configurations.
    // It is assumed that the PIDs in the provided configurations is a subset of the registered PIDs.
    List<ComponentConfiguration> currentConfigs = getComponentConfigurationsInternal();
    if (currentConfigs != null) {
        for (ComponentConfiguration currentConfig : currentConfigs) {
            // either add this configuration or a new one obtained by merging its properties with the ones provided
            ComponentConfiguration cc = currentConfig;
            String pid = currentConfig.getPid();
            if (configsToUpdate != null) {
                for (ComponentConfiguration configToUpdate : configsToUpdate) {
                    if (configToUpdate.getPid().equals(pid)) {
                        Map<String, Object> props = new HashMap<String, Object>();
                        if (currentConfig.getConfigurationProperties() != null) {
                            props.putAll(currentConfig.getConfigurationProperties());
                        }
                        if (configToUpdate.getConfigurationProperties() != null) {
                            props.putAll(configToUpdate.getConfigurationProperties());
                        }
                        cc = new ComponentConfigurationImpl(pid, (Tocd) configToUpdate.getDefinition(), props);
                        break;
                    }
                }
            }
            result.add(cc);
        }
    }
    // complete the returned configurations adding the snapshot configurations
    // of those components not yet in the list.
    List<ComponentConfigurationImpl> snapshotConfigs = loadLatestSnapshotConfigurations();
    if (snapshotConfigs != null) {
        for (ComponentConfigurationImpl snapshotConfig : snapshotConfigs) {
            boolean found = false;
            for (ComponentConfiguration config : result) {
                if (config.getPid().equals(snapshotConfig.getPid())) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                // Add old configurations (or not yet tracked ones) present
                result.add(snapshotConfig);
            }
        }
    }
    // remove configurations being deleted
    Iterator<String> it = this.m_pendingDeletePids.iterator();
    while (it.hasNext()) {
        String deletePid = it.next();
        for (ComponentConfiguration config : result) {
            if (config.getPid().equals(deletePid)) {
                it.remove();
                result.remove(config);
                break;
            }
        }
    }
    return result;
}
Also used : ComponentConfiguration(org.eclipse.kura.configuration.ComponentConfiguration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Tocd(org.eclipse.kura.core.configuration.metatype.Tocd)

Example 4 with ComponentConfiguration

use of org.eclipse.kura.configuration.ComponentConfiguration in project kura by eclipse.

the class ConfigurationServiceImpl method updateConfigurationsInternal.

// ----------------------------------------------------------------
// 
// Private APIs
// 
// ----------------------------------------------------------------
private synchronized void updateConfigurationsInternal(List<ComponentConfiguration> configsToUpdate, boolean takeSnapshot) throws KuraException {
    boolean snapshotOnConfirmation = false;
    List<Throwable> causes = new ArrayList<Throwable>();
    List<ComponentConfiguration> configs = buildCurrentConfiguration(configsToUpdate);
    for (ComponentConfiguration config : configs) {
        for (ComponentConfiguration configToUpdate : configsToUpdate) {
            if (config.getPid().equals(configToUpdate.getPid())) {
                try {
                    updateConfigurationInternal(config.getPid(), config.getConfigurationProperties(), snapshotOnConfirmation);
                    break;
                } catch (KuraException e) {
                    s_logger.warn("Error during updateConfigurations for component " + config.getPid(), e);
                    causes.add(e);
                }
            }
        }
    }
    if (takeSnapshot && configs != null && !configs.isEmpty()) {
        saveSnapshot(configs);
    }
    if (!causes.isEmpty()) {
        throw new KuraPartialSuccessException("updateConfigurations", causes);
    }
}
Also used : ComponentConfiguration(org.eclipse.kura.configuration.ComponentConfiguration) KuraException(org.eclipse.kura.KuraException) KuraPartialSuccessException(org.eclipse.kura.KuraPartialSuccessException) ArrayList(java.util.ArrayList)

Example 5 with ComponentConfiguration

use of org.eclipse.kura.configuration.ComponentConfiguration in project kura by eclipse.

the class RollbackCallable method doGetConfigurations.

private void doGetConfigurations(CloudletTopic reqTopic, KuraPayload reqPayload, KuraResponsePayload respPayload) throws KuraException {
    String[] resources = reqTopic.getResources();
    if (resources.length > 2) {
        s_logger.error("Bad request topic: {}", reqTopic.toString());
        s_logger.error("Expected at most two resource(s) but found {}", resources.length);
        respPayload.setResponseCode(KuraResponsePayload.RESPONSE_CODE_BAD_REQUEST);
        return;
    }
    String pid = resources.length == 2 ? resources[1] : null;
    // 
    // get current configuration with descriptors
    List<ComponentConfigurationImpl> configs = new ArrayList<ComponentConfigurationImpl>();
    try {
        if (pid == null) {
            List<String> pidsToIgnore = this.m_systemService.getDeviceManagementServiceIgnore();
            // the configuration for all components has been requested
            Set<String> componentPids = this.m_configurationService.getConfigurableComponentPids();
            for (String componentPid : componentPids) {
                boolean skip = false;
                if (pidsToIgnore != null && !pidsToIgnore.isEmpty()) {
                    for (String pidToIgnore : pidsToIgnore) {
                        if (componentPid.equals(pidToIgnore)) {
                            skip = true;
                            break;
                        }
                    }
                }
                if (skip) {
                    continue;
                }
                ComponentConfiguration cc = this.m_configurationService.getComponentConfiguration(componentPid);
                // TODO: define a validate method for ComponentConfiguration
                if (cc == null) {
                    s_logger.error("null ComponentConfiguration");
                    continue;
                }
                if (cc.getPid() == null || cc.getPid().isEmpty()) {
                    s_logger.error("null or empty ComponentConfiguration PID");
                    continue;
                }
                if (cc.getDefinition() == null) {
                    s_logger.error("null OCD for ComponentConfiguration PID {}", cc.getPid());
                    continue;
                }
                if (cc.getDefinition().getId() == null || cc.getDefinition().getId().isEmpty()) {
                    s_logger.error("null or empty OCD ID for ComponentConfiguration PID {}. OCD ID: {}", cc.getPid(), cc.getDefinition().getId());
                    continue;
                }
                configs.add((ComponentConfigurationImpl) cc);
            }
        } else {
            // the configuration for a specific component has been requested.
            ComponentConfiguration cc = this.m_configurationService.getComponentConfiguration(pid);
            if (cc != null) {
                configs.add((ComponentConfigurationImpl) cc);
            }
        }
    } catch (KuraException e) {
        s_logger.error("Error getting component configurations: {}", e);
        throw new KuraException(KuraErrorCode.CONFIGURATION_ERROR, e, "Error getting component configurations");
    }
    XmlComponentConfigurations xmlConfigs = new XmlComponentConfigurations();
    xmlConfigs.setConfigurations(configs);
    // 
    // marshall
    byte[] body = toResponseBody(xmlConfigs);
    // 
    // Build response payload
    respPayload.setBody(body);
}
Also used : ComponentConfiguration(org.eclipse.kura.configuration.ComponentConfiguration) KuraException(org.eclipse.kura.KuraException) ArrayList(java.util.ArrayList)

Aggregations

ComponentConfiguration (org.eclipse.kura.configuration.ComponentConfiguration)24 ArrayList (java.util.ArrayList)19 KuraException (org.eclipse.kura.KuraException)10 ConfigurationService (org.eclipse.kura.configuration.ConfigurationService)9 HashMap (java.util.HashMap)6 SelfConfiguringComponent (org.eclipse.kura.configuration.SelfConfiguringComponent)6 IOException (java.io.IOException)5 UnknownHostException (java.net.UnknownHostException)5 NetworkConfiguration (org.eclipse.kura.core.net.NetworkConfiguration)5 GwtConfigComponent (org.eclipse.kura.web.shared.model.GwtConfigComponent)5 NetConfig (org.eclipse.kura.net.NetConfig)4 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)4 NetInterfaceConfig (org.eclipse.kura.net.NetInterfaceConfig)4 FileNotFoundException (java.io.FileNotFoundException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 KuraPartialSuccessException (org.eclipse.kura.KuraPartialSuccessException)3 AD (org.eclipse.kura.configuration.metatype.AD)3 OCD (org.eclipse.kura.configuration.metatype.OCD)3 ComponentConfigurationImpl (org.eclipse.kura.core.configuration.ComponentConfigurationImpl)3 XmlComponentConfigurations (org.eclipse.kura.core.configuration.XmlComponentConfigurations)3