diff --git a/commons/src/main/java/org/eclipse/kapua/commons/configuration/KapuaConfigurableServiceBase.java b/commons/src/main/java/org/eclipse/kapua/commons/configuration/KapuaConfigurableServiceBase.java index c9c98b5cc3c..be0698d338d 100644 --- a/commons/src/main/java/org/eclipse/kapua/commons/configuration/KapuaConfigurableServiceBase.java +++ b/commons/src/main/java/org/eclipse/kapua/commons/configuration/KapuaConfigurableServiceBase.java @@ -79,7 +79,7 @@ public KapuaTocd getConfigMetadata(KapuaId scopeId) throws KapuaException { //Temporary, use Optional instead return new EmptyTocd(); } - return serviceConfigurationManager.getConfigMetadata(scopeId, true); + return serviceConfigurationManager.getConfigMetadata(scopeId, true).orElse(null); } @Override diff --git a/commons/src/main/java/org/eclipse/kapua/commons/configuration/ServiceConfigurationManager.java b/commons/src/main/java/org/eclipse/kapua/commons/configuration/ServiceConfigurationManager.java index 8c34eff0f6a..bb9c4ea9436 100644 --- a/commons/src/main/java/org/eclipse/kapua/commons/configuration/ServiceConfigurationManager.java +++ b/commons/src/main/java/org/eclipse/kapua/commons/configuration/ServiceConfigurationManager.java @@ -46,7 +46,7 @@ default boolean isServiceEnabled(TxContext txContext, KapuaId scopeId) { Map getConfigValues(KapuaId scopeId, boolean excludeDisabled) throws KapuaException; - KapuaTocd getConfigMetadata(KapuaId scopeId, boolean excludeDisabled) throws KapuaException; + Optional getConfigMetadata(KapuaId scopeId, boolean excludeDisabled) throws KapuaException; - ServiceComponentConfiguration extractServiceComponentConfiguration(KapuaId scopeId) throws KapuaException; + Optional extractServiceComponentConfiguration(KapuaId scopeId) throws KapuaException; } diff --git a/commons/src/main/java/org/eclipse/kapua/commons/configuration/ServiceConfigurationManagerCachingWrapper.java b/commons/src/main/java/org/eclipse/kapua/commons/configuration/ServiceConfigurationManagerCachingWrapper.java index 8f9ec9f5947..285a7b02dd7 100644 --- a/commons/src/main/java/org/eclipse/kapua/commons/configuration/ServiceConfigurationManagerCachingWrapper.java +++ b/commons/src/main/java/org/eclipse/kapua/commons/configuration/ServiceConfigurationManagerCachingWrapper.java @@ -88,24 +88,24 @@ public Map getConfigValues(TxContext txContext, KapuaId scopeId, } @Override - public KapuaTocd getConfigMetadata(KapuaId scopeId, boolean excludeDisabled) throws KapuaException { + public Optional getConfigMetadata(KapuaId scopeId, boolean excludeDisabled) throws KapuaException { // Argument validation ArgumentValidator.notNull(scopeId, "scopeId"); // Get the Tocd // Keep distinct values for service PID, Scope ID and disabled properties included/excluded from AD - Pair cacheKey = Pair.of(scopeId, excludeDisabled); + final Pair cacheKey = Pair.of(scopeId, excludeDisabled); try { // Check if the OCD is already in cache, but not in the "empty" cache - KapuaTocd tocd; - tocd = kapuaTocdLocalCache.get(cacheKey); - if (tocd == null && !kapuaTocdEmptyLocalCache.get(cacheKey)) { + Optional tocd; + tocd = Optional.ofNullable(kapuaTocdLocalCache.get(cacheKey)); + if (!tocd.isPresent() && !kapuaTocdEmptyLocalCache.get(cacheKey)) { // If not, read metadata and process it tocd = wrapped.getConfigMetadata(scopeId, excludeDisabled); // If null, put it in the "empty" ocd cache, else put it in the "standard" cache - if (tocd != null) { + if (tocd.isPresent()) { // If the value is not null, put it in "standard" cache and remove the entry from the "empty" cache if present - kapuaTocdLocalCache.put(cacheKey, tocd); + kapuaTocdLocalCache.put(cacheKey, tocd.get()); kapuaTocdEmptyLocalCache.remove(cacheKey); } else { // If the value is null, just remember we already read it from file at least once @@ -119,7 +119,7 @@ public KapuaTocd getConfigMetadata(KapuaId scopeId, boolean excludeDisabled) thr } @Override - public ServiceComponentConfiguration extractServiceComponentConfiguration(KapuaId scopeId) throws KapuaException { + public Optional extractServiceComponentConfiguration(KapuaId scopeId) throws KapuaException { return wrapped.extractServiceComponentConfiguration(scopeId); } } diff --git a/commons/src/main/java/org/eclipse/kapua/commons/configuration/ServiceConfigurationManagerImpl.java b/commons/src/main/java/org/eclipse/kapua/commons/configuration/ServiceConfigurationManagerImpl.java index fd45b68040a..67b22bec4c1 100644 --- a/commons/src/main/java/org/eclipse/kapua/commons/configuration/ServiceConfigurationManagerImpl.java +++ b/commons/src/main/java/org/eclipse/kapua/commons/configuration/ServiceConfigurationManagerImpl.java @@ -15,6 +15,7 @@ import java.io.IOException; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -32,7 +33,6 @@ import org.eclipse.kapua.KapuaIllegalArgumentException; import org.eclipse.kapua.KapuaIllegalNullArgumentException; import org.eclipse.kapua.commons.security.KapuaSecurityUtils; -import org.eclipse.kapua.commons.service.internal.KapuaServiceDisabledException; import org.eclipse.kapua.commons.util.ArgumentValidator; import org.eclipse.kapua.commons.util.ResourceUtils; import org.eclipse.kapua.commons.util.StringUtil; @@ -131,11 +131,21 @@ public void checkAllowedEntities(TxContext txContext, KapuaId scopeId, String en @Override public void setConfigValues(KapuaId scopeId, Optional parentId, Map values) throws KapuaException { txManager.execute(tx -> { - KapuaTocd ocd = doGetConfigMetadata(tx, scopeId, false); + Optional maybeOcd = doGetConfigMetadata(tx, scopeId, false); + if (!maybeOcd.isPresent()) { + return null; + //throw? + } + + final Optional maybeKapuaTocd = doGetConfigMetadata(tx, scopeId, true); + if (!maybeKapuaTocd.isPresent()) { + return null; + //throw? + } - Map originalValues = doGetConfigValues(tx, scopeId, doGetConfigMetadata(tx, scopeId, true)); + Map originalValues = doGetConfigValues(tx, scopeId, maybeKapuaTocd.get()); - for (KapuaTad ad : ocd.getAD()) { + for (KapuaTad ad : maybeOcd.get().getAD()) { boolean allowSelfEdit = Boolean.parseBoolean(ad.getOtherAttributes().getOrDefault(new QName("allowSelfEdit"), "false")); final KapuaId currentUserId = KapuaSecurityUtils.getSession().getUserId(); @@ -155,7 +165,7 @@ public void setConfigValues(KapuaId scopeId, Optional parentId, Map getConfigValues(TxContext txContext, KapuaId scopeId, } protected Map doGetConfigValues(TxContext txContext, KapuaId scopeId, boolean excludeDisabled) throws KapuaException { - final KapuaTocd metadata = doGetConfigMetadata(txContext, scopeId, excludeDisabled); - return doGetConfigValues(txContext, scopeId, metadata); + final Optional metadata = doGetConfigMetadata(txContext, scopeId, excludeDisabled); + return metadata.map(m -> { + try { + return doGetConfigValues(txContext, scopeId, m); + } catch (KapuaException e) { + throw new RuntimeException(e); + } + }).orElse(Collections.emptyMap()); } protected Map doGetConfigValues(TxContext txContext, KapuaId scopeId, KapuaTocd metadata) throws KapuaException { @@ -414,16 +430,16 @@ private static Map toValues(@NotNull KapuaTocd ocd, Properties p * @since 1.3.0 */ @Override - public KapuaTocd getConfigMetadata(KapuaId scopeId, boolean excludeDisabled) throws KapuaException { + public Optional getConfigMetadata(KapuaId scopeId, boolean excludeDisabled) throws KapuaException { return txManager.execute(txContext -> doGetConfigMetadata(txContext, scopeId, excludeDisabled)); } - protected KapuaTocd doGetConfigMetadata(TxContext txContext, KapuaId scopeId, boolean excludeDisabled) throws KapuaException { + protected Optional doGetConfigMetadata(TxContext txContext, KapuaId scopeId, boolean excludeDisabled) throws KapuaException { // Argument validation ArgumentValidator.notNull(scopeId, "scopeId"); // Check disabled service if (!isServiceEnabled(txContext, scopeId)) { - throw new KapuaServiceDisabledException(pid); + return Optional.empty(); } // Get the Tocd try { @@ -434,15 +450,19 @@ protected KapuaTocd doGetConfigMetadata(TxContext txContext, KapuaId scopeId, bo } @Override - public ServiceComponentConfiguration extractServiceComponentConfiguration(KapuaId scopeId) throws KapuaException { + public Optional extractServiceComponentConfiguration(KapuaId scopeId) throws KapuaException { return txManager.execute(txContext -> { - final KapuaTocd metadata = this.doGetConfigMetadata(txContext, scopeId, true); + final Optional maybeMetadata = this.doGetConfigMetadata(txContext, scopeId, true); + if (!maybeMetadata.isPresent()) { + return Optional.empty(); + } + final KapuaTocd metadata = maybeMetadata.get(); final Map values = this.doGetConfigValues(txContext, scopeId, metadata); final ServiceComponentConfiguration res = new ServiceComponentConfiguration(metadata.getId()); res.setDefinition(metadata); res.setName(metadata.getName()); res.setProperties(values); - return res; + return Optional.of(res); }); } @@ -456,16 +476,16 @@ public ServiceComponentConfiguration extractServiceComponentConfiguration(KapuaI * @return The processed {@link KapuaTocd}. * @since 1.3.0 */ - private KapuaTocd processMetadata(TxContext txContext, KapuaTmetadata metadata, KapuaId scopeId, boolean excludeDisabled) { + private Optional processMetadata(TxContext txContext, KapuaTmetadata metadata, KapuaId scopeId, boolean excludeDisabled) { if (metadata != null && metadata.getOCD() != null && !metadata.getOCD().isEmpty()) { for (KapuaTocd ocd : metadata.getOCD()) { if (ocd.getId() != null && ocd.getId().equals(pid) && isServiceEnabled(txContext, scopeId)) { ocd.getAD().removeIf(ad -> excludeDisabled && !isPropertyEnabled(ad, scopeId)); - return ocd; + return Optional.of(ocd); } } } - return null; + return Optional.empty(); } /** diff --git a/commons/src/main/java/org/eclipse/kapua/commons/configuration/ServiceConfigurationsFacadeImpl.java b/commons/src/main/java/org/eclipse/kapua/commons/configuration/ServiceConfigurationsFacadeImpl.java index 33884484d2e..a686d807ad5 100644 --- a/commons/src/main/java/org/eclipse/kapua/commons/configuration/ServiceConfigurationsFacadeImpl.java +++ b/commons/src/main/java/org/eclipse/kapua/commons/configuration/ServiceConfigurationsFacadeImpl.java @@ -14,6 +14,7 @@ import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; import javax.inject.Inject; @@ -31,6 +32,7 @@ public class ServiceConfigurationsFacadeImpl implements ServiceConfigurationsFacade { private final Map, ServiceConfigurationManager> serviceConfigurationManagersByServiceClass; + private final Map serviceConfigurationManagersByServiceClassName; protected final AuthorizationService authorizationService; protected final PermissionFactory permissionFactory; protected final AccountService accountService; @@ -39,6 +41,8 @@ public class ServiceConfigurationsFacadeImpl implements ServiceConfigurationsFac public ServiceConfigurationsFacadeImpl(Map, ServiceConfigurationManager> serviceConfigurationManagersByServiceClass, AuthorizationService authorizationService, PermissionFactory permissionFactory, AccountService accountService) { this.serviceConfigurationManagersByServiceClass = serviceConfigurationManagersByServiceClass; + this.serviceConfigurationManagersByServiceClassName = + serviceConfigurationManagersByServiceClass.entrySet().stream().collect(Collectors.toMap(kv -> kv.getKey().getName(), kv -> kv.getValue())); this.authorizationService = authorizationService; this.permissionFactory = permissionFactory; this.accountService = accountService; @@ -52,19 +56,19 @@ public ServiceConfiguration fetchAllConfigurations(KapuaId scopeId) throws Kapua if (!authorizationService.isPermitted(permissionFactory.newPermission(configurableService.getDomain(), Actions.read, scopeId))) { continue; } - res.getComponentConfigurations().add(configurableService.extractServiceComponentConfiguration(scopeId)); + configurableService.extractServiceComponentConfiguration(scopeId).ifPresent(res.getComponentConfigurations()::add); } return res; } @Override public ServiceComponentConfiguration fetchConfiguration(KapuaId scopeId, String serviceId) throws KapuaException { - final ServiceConfigurationManager serviceConfigurationManager = serviceConfigurationManagersByServiceClass.get(serviceId); + final ServiceConfigurationManager serviceConfigurationManager = serviceConfigurationManagersByServiceClassName.get(serviceId); if (serviceConfigurationManager == null) { throw new KapuaIllegalArgumentException("service.pid", serviceId); } authorizationService.checkPermission(permissionFactory.newPermission(serviceConfigurationManager.getDomain(), Actions.read, scopeId)); - return serviceConfigurationManager.extractServiceComponentConfiguration(scopeId); + return serviceConfigurationManager.extractServiceComponentConfiguration(scopeId).orElse(null); } @Override @@ -72,18 +76,18 @@ public void update(KapuaId scopeId, ServiceConfiguration newServiceConfiguration final Account account = accountService.find(scopeId); for (ServiceComponentConfiguration newServiceComponentConfiguration : newServiceConfiguration.getComponentConfigurations()) { - doUpdateServiceComponentConfiguration(account, scopeId, newServiceComponentConfiguration); + doUpdateServiceComponentConfiguration(account, scopeId, newServiceComponentConfiguration.getId(), newServiceComponentConfiguration); } } @Override public void update(KapuaId scopeId, String serviceId, ServiceComponentConfiguration newServiceComponentConfiguration) throws KapuaException { final Account account = accountService.find(scopeId); - doUpdateServiceComponentConfiguration(account, scopeId, newServiceComponentConfiguration); + doUpdateServiceComponentConfiguration(account, scopeId, serviceId, newServiceComponentConfiguration); } - private void doUpdateServiceComponentConfiguration(Account account, KapuaId scopeId, ServiceComponentConfiguration newServiceComponentConfiguration) throws KapuaException { - final ServiceConfigurationManager serviceConfigurationManager = serviceConfigurationManagersByServiceClass.get(newServiceComponentConfiguration.getId()); + private void doUpdateServiceComponentConfiguration(Account account, KapuaId scopeId, String serviceId, ServiceComponentConfiguration newServiceComponentConfiguration) throws KapuaException { + final ServiceConfigurationManager serviceConfigurationManager = serviceConfigurationManagersByServiceClassName.get(serviceId); if (serviceConfigurationManager == null) { throw new KapuaIllegalArgumentException("serviceConfiguration.componentConfiguration.id", newServiceComponentConfiguration.getId()); } diff --git a/service/device/registry/internal/src/main/java/org/eclipse/kapua/service/device/registry/connection/internal/DeviceConnectionServiceConfigurationManager.java b/service/device/registry/internal/src/main/java/org/eclipse/kapua/service/device/registry/connection/internal/DeviceConnectionServiceConfigurationManager.java index de71052e7d2..20e021f1ffc 100644 --- a/service/device/registry/internal/src/main/java/org/eclipse/kapua/service/device/registry/connection/internal/DeviceConnectionServiceConfigurationManager.java +++ b/service/device/registry/internal/src/main/java/org/eclipse/kapua/service/device/registry/connection/internal/DeviceConnectionServiceConfigurationManager.java @@ -90,12 +90,12 @@ public DeviceConnectionServiceConfigurationManager( * @since 2.0.0 */ @Override - public KapuaTocd doGetConfigMetadata(TxContext txContext, KapuaId scopeId, boolean excludeDisabled) throws KapuaException { + protected Optional doGetConfigMetadata(TxContext txContext, KapuaId scopeId, boolean excludeDisabled) throws KapuaException { - KapuaTocd deviceConnectionServiceConfigDefinition = super.doGetConfigMetadata(txContext, scopeId, excludeDisabled); + Optional deviceConnectionServiceConfigDefinition = super.doGetConfigMetadata(txContext, scopeId, excludeDisabled); // Find the 'deviceConnectionAuthenticationType' KapuaTad - Optional authenticationTypeConfigDefinition = findAuthenticationTypeTad(deviceConnectionServiceConfigDefinition); + Optional authenticationTypeConfigDefinition = deviceConnectionServiceConfigDefinition.flatMap(this::findAuthenticationTypeTad); // Add the KapuaToption to the KapuaTad authenticationTypeConfigDefinition.ifPresent(tad -> {