Skip to content

Commit

Permalink
Merge pull request #3969 from elbert3/chng-forwardableEntities
Browse files Browse the repository at this point in the history
Improve/cleanup forwardable entities
  • Loading branch information
elbert3 authored Mar 8, 2024
2 parents 46e207c + c658c16 commit e839014
Show file tree
Hide file tree
Showing 44 changed files with 704 additions and 103 deletions.
2 changes: 2 additions & 0 deletions assembly/broker-artemis/descriptors/kapua-broker-artemis.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@
<include>${pom.groupId}:kapua-openid-api</include>
<include>${pom.groupId}:kapua-openid-provider</include>
<include>${pom.groupId}:kapua-service-api</include>
<include>${pom.groupId}:kapua-service-commons-utils-api</include>
<include>${pom.groupId}:kapua-service-commons-utils-internal</include>
<include>${pom.groupId}:kapua-security-authentication-api</include>
<include>${pom.groupId}:kapua-security-authorization-api</include>
<include>${pom.groupId}:kapua-security-certificate-api</include>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public abstract class AbstractKapuaConfigurableResourceLimitedService<


//TODO: make final as soon as deprecated constructors are removed
private AccountChildrenFinder accountChildrenFinder;
private AccountRelativeFinder accountRelativeFinder;
private F factory;
//TODO: remove as soon as deprecated constructors are removed
private final Class<F> factoryClass;
Expand Down Expand Up @@ -109,7 +109,7 @@ protected AbstractKapuaConfigurableResourceLimitedService(
* @param entityManagerFactory The {@link EntityManagerFactory} that handles persistence unit
* @param abstractCacheFactory The {@link CacheFactory} that handles caching of the entities
* @since 1.2.0
* @deprecated Since 2.0.0. Please use {@link #AbstractKapuaConfigurableResourceLimitedService(String, Domain, EntityManagerFactory, EntityCacheFactory, KapuaEntityFactory, PermissionFactory, AuthorizationService, AccountChildrenFinder, RootUserTester)} This constructor may be removed in a next release
* @deprecated Since 2.0.0. Please use {@link #AbstractKapuaConfigurableResourceLimitedService(String, Domain, EntityManagerFactory, EntityCacheFactory, KapuaEntityFactory, PermissionFactory, AuthorizationService, AccountRelativeFinder, RootUserTester)} This constructor may be removed in a next release
*/
@Deprecated
protected AbstractKapuaConfigurableResourceLimitedService(
Expand All @@ -127,7 +127,7 @@ protected AbstractKapuaConfigurableResourceLimitedService(
*/
this.factoryClass = factoryClass;
this.factory = null;
this.accountChildrenFinder = null;
this.accountRelativeFinder = null;
}

/**
Expand All @@ -149,12 +149,12 @@ protected AbstractKapuaConfigurableResourceLimitedService(String pid,
F factory,
PermissionFactory permissionFactory,
AuthorizationService authorizationService,
AccountChildrenFinder accountChildrenFinder,
AccountRelativeFinder accountRelativeFinder,
RootUserTester rootUserTester) {
super(pid, domain, entityManagerFactory, abstractCacheFactory, permissionFactory, authorizationService, rootUserTester);
this.factory = factory;
this.factoryClass = null; //TODO: not needed for this construction path, remove as soon as the deprecated constructor is removed
this.accountChildrenFinder = accountChildrenFinder;
this.accountRelativeFinder = accountRelativeFinder;
}

@Override
Expand Down Expand Up @@ -244,7 +244,7 @@ private long allowedChildEntities(KapuaId scopeId, KapuaId targetScopeId, Map<St
// Current used entities
long currentUsedEntities = this.count(countQuery);

final KapuaListResult<Account> childAccounts = txManager.execute(tx -> getAccountChildrenFinder().findChildren(scopeId, Optional.ofNullable(targetScopeId)));
final KapuaListResult<Account> childAccounts = txManager.execute(tx -> getAccountRelativeFinder().findChildren(scopeId, Optional.ofNullable(targetScopeId)));
// Resources assigned to children
long childCount = 0;
for (Account childAccount : childAccounts.getItems()) {
Expand Down Expand Up @@ -285,14 +285,14 @@ protected F getFactory() {
* This instance should be provided by the Locator, but in most cases when this class is instantiated through the deprecated constructor the Locator is not yet ready,
* therefore fetching of the required instance is demanded to this artificial getter.
*
* @return The instantiated (hopefully) {@link AccountChildrenFinder} instance
* @return The instantiated (hopefully) {@link AccountRelativeFinder} instance
*/
private AccountChildrenFinder getAccountChildrenFinder() {
if (accountChildrenFinder == null) {
private AccountRelativeFinder getAccountRelativeFinder() {
if (accountRelativeFinder == null) {
KapuaLocator locator = KapuaLocator.getInstance();
this.accountChildrenFinder = locator.getService(AccountChildrenFinder.class);
this.accountRelativeFinder = locator.getService(AccountRelativeFinder.class);
}

return accountChildrenFinder;
return accountRelativeFinder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
import org.eclipse.kapua.service.KapuaService;
import org.eclipse.kapua.service.account.AccountListResult;

import java.util.List;
import java.util.Optional;

/**
* Service to retrieve all child accounts for a given scope
* Service to retrieve relative accounts for a given scope
*
* @since 2.0.0
*/
public interface AccountChildrenFinder extends KapuaService {
public interface AccountRelativeFinder extends KapuaService {

/**
* @param scopeId The scope id - must be provided
Expand All @@ -33,4 +34,11 @@ public interface AccountChildrenFinder extends KapuaService {
* @throws KapuaException
*/
AccountListResult findChildren(KapuaId scopeId, Optional<KapuaId> targetScopeId) throws KapuaException;

/**
* @param accountId The id of the account to lookup
* @return The list of parent ids for the target account
* @throws KapuaException
*/
List<KapuaId> findParentIds(KapuaId accountId) throws KapuaException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ public class ResourceLimitedServiceConfigurationManagerImpl
extends ServiceConfigurationManagerImpl
implements ServiceConfigurationManager {

private final AccountChildrenFinder accountChildrenFinder;
private final AccountRelativeFinder accountRelativeFinder;
private final UsedEntitiesCounter usedEntitiesCounter;

public ResourceLimitedServiceConfigurationManagerImpl(
String pid,
ServiceConfigRepository serviceConfigRepository,
RootUserTester rootUserTester,
AccountChildrenFinder accountChildrenFinder,
AccountRelativeFinder accountRelativeFinder,
UsedEntitiesCounter usedEntitiesCounter) {
super(pid, serviceConfigRepository, rootUserTester);
this.accountChildrenFinder = accountChildrenFinder;
this.accountRelativeFinder = accountRelativeFinder;
this.usedEntitiesCounter = usedEntitiesCounter;
}

Expand Down Expand Up @@ -135,7 +135,7 @@ private long allowedChildEntities(TxContext txContext, KapuaId scopeId, Optional
// Current used entities
long currentUsedEntities = usedEntitiesCounter.countEntitiesInScope(txContext, scopeId);

final AccountListResult childAccounts = accountChildrenFinder.findChildren(scopeId, targetScopeId);
final AccountListResult childAccounts = accountRelativeFinder.findChildren(scopeId, targetScopeId);
// Resources assigned to children
long childCount = 0;
for (Account childAccount : childAccounts.getItems()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*******************************************************************************
* Copyright (c) 2024, 2022 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.commons.model.query;

import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.model.query.KapuaForwardableEntityQuery;
import org.eclipse.kapua.model.query.KapuaQuery;

public class AbstractKapuaForwardableEntityQuery extends AbstractKapuaNamedQuery implements KapuaForwardableEntityQuery {

protected Boolean includeInherited = Boolean.FALSE;

/**
* Constructor.
*
*/
public AbstractKapuaForwardableEntityQuery() {
super();
}

/**
* Constructor.
*
* @param scopeId The {@link #getScopeId()}.
*/
public AbstractKapuaForwardableEntityQuery(KapuaId scopeId) {
super(scopeId);
}

/**
* Clone constructor.
*
* @param query The {@link AbstractKapuaForwardableEntityQuery} to clone.
*/
public AbstractKapuaForwardableEntityQuery(KapuaQuery query) {
super(query);
if(query instanceof KapuaForwardableEntityQuery) {
this.includeInherited = ((KapuaForwardableEntityQuery) query).getIncludeInherited();
}
}

@Override
public Boolean getIncludeInherited() {
return includeInherited;
}

@Override
public void setIncludeInherited(Boolean includeInherited) {
this.includeInherited = includeInherited;
}
}
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,16 @@
<artifactId>kapua-service-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-service-commons-utils-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-service-commons-utils-internal</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-service-elasticsearch-client-api</artifactId>
Expand All @@ -1032,6 +1042,16 @@
<artifactId>kapua-service-storable-internal</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-service-utils-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-service-utils-internal</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-service-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import com.google.inject.Module;
import com.google.inject.Provides;
import com.google.inject.multibindings.ProvidesIntoSet;
import org.eclipse.kapua.commons.configuration.AccountChildrenFinder;
import org.eclipse.kapua.commons.configuration.AccountRelativeFinder;
import org.eclipse.kapua.commons.configuration.CachingServiceConfigRepository;
import org.eclipse.kapua.commons.configuration.ResourceLimitedServiceConfigurationManagerImpl;
import org.eclipse.kapua.commons.configuration.RootUserTester;
Expand Down Expand Up @@ -71,10 +71,10 @@ public Domain accountDomain() {

@Provides
@Singleton
AccountChildrenFinder accountChildrenFinder(
AccountRelativeFinder accountRelativeFinder(
AccountFactory accountFactory,
AccountService accountService) {
return new AccountChildrenFinderImpl(
return new AccountRelativeFinderImpl(
accountFactory,
accountService);
}
Expand Down Expand Up @@ -132,7 +132,7 @@ AccountService accountService(AccountRepository accountRepository,
ServiceConfigurationManager accountServiceConfigurationManager(
AccountFactory factory,
RootUserTester rootUserTester,
AccountChildrenFinder accountChildrenFinder,
AccountRelativeFinder accountRelativeFinder,
AccountRepository accountRepository,
KapuaJpaRepositoryConfiguration jpaRepoConfig,
EntityCacheFactory entityCacheFactory
Expand All @@ -145,7 +145,7 @@ ServiceConfigurationManager accountServiceConfigurationManager(
entityCacheFactory.createCache("AbstractKapuaConfigurableServiceCacheId")
),
rootUserTester,
accountChildrenFinder,
accountRelativeFinder,
new UsedEntitiesCounterImpl(
factory,
accountRepository)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,35 @@
*******************************************************************************/
package org.eclipse.kapua.service.account.internal;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import javax.inject.Inject;

import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.configuration.AccountChildrenFinder;
import org.eclipse.kapua.commons.configuration.AccountRelativeFinder;
import org.eclipse.kapua.commons.model.id.KapuaEid;
import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
import org.eclipse.kapua.model.KapuaEntityAttributes;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.model.query.predicate.AttributePredicate;
import org.eclipse.kapua.service.KapuaService;
import org.eclipse.kapua.service.account.Account;
import org.eclipse.kapua.service.account.AccountFactory;
import org.eclipse.kapua.service.account.AccountListResult;
import org.eclipse.kapua.service.account.AccountQuery;
import org.eclipse.kapua.service.account.AccountService;

import javax.inject.Inject;
import java.util.Optional;

public class AccountChildrenFinderImpl implements AccountChildrenFinder, KapuaService {
public class AccountRelativeFinderImpl implements AccountRelativeFinder, KapuaService {

private final AccountFactory accountFactory;
private final AccountService accountService;

@Inject
public AccountChildrenFinderImpl(AccountFactory accountFactory, AccountService accountService) {
public AccountRelativeFinderImpl(AccountFactory accountFactory, AccountService accountService) {
this.accountFactory = accountFactory;
this.accountService = accountService;
}
Expand All @@ -53,4 +60,29 @@ public AccountListResult findChildren(KapuaId scopeId, Optional<KapuaId> exclude

return KapuaSecurityUtils.doPrivileged(() -> accountService.query(childAccountsQuery));
}

@Override
public List<KapuaId> findParentIds(KapuaId accountId) throws KapuaException {
Account account = KapuaSecurityUtils.doPrivileged(() -> accountService.find(accountId));

if(account == null || account.getParentAccountPath() == null) {
return Collections.emptyList();
}

ArrayList<KapuaId> parentAccountIds = new ArrayList<KapuaId>();

String[] splitIds = account.getParentAccountPath().split("/");
String accountIdStr = accountId.getId().toString();

// Iterate in reverse order to get parent first, then grandparent, etc
for(int i = splitIds.length - 1; i >= 0; i--) {
String id = splitIds[i];

if(id != null && !id.isEmpty() && !id.equals(accountIdStr)) {
parentAccountIds.add(new KapuaEid(new BigInteger(id)));
}
}

return parentAccountIds;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.google.inject.name.Names;
import io.cucumber.java.Before;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.configuration.AccountChildrenFinder;
import org.eclipse.kapua.commons.configuration.AccountRelativeFinder;
import org.eclipse.kapua.commons.configuration.ResourceLimitedServiceConfigurationManagerImpl;
import org.eclipse.kapua.commons.configuration.RootUserTester;
import org.eclipse.kapua.commons.configuration.ServiceConfigImplJpaRepository;
Expand Down Expand Up @@ -109,7 +109,7 @@ protected void configure() {
// bind(AccountEntityManagerFactory.class).toInstance(entityManagerFactory);
final AccountFactory accountFactory = new AccountFactoryImpl();
bind(AccountFactory.class).toInstance(accountFactory);
bind(AccountChildrenFinder.class).toInstance(Mockito.mock(AccountChildrenFinder.class));
bind(AccountRelativeFinder.class).toInstance(Mockito.mock(AccountRelativeFinder.class));
final KapuaJpaRepositoryConfiguration jpaRepoConfig = new KapuaJpaRepositoryConfiguration();
final AccountRepository accountRepository = new AccountImplJpaRepository(jpaRepoConfig);
bind(AccountService.class).toInstance(new AccountServiceImpl(
Expand All @@ -121,7 +121,7 @@ protected void configure() {
AccountService.class.getName(),
new ServiceConfigImplJpaRepository(jpaRepoConfig),
Mockito.mock(RootUserTester.class),
Mockito.mock(AccountChildrenFinder.class),
Mockito.mock(AccountRelativeFinder.class),
new UsedEntitiesCounterImpl(
accountFactory,
accountRepository)
Expand Down
Loading

0 comments on commit e839014

Please sign in to comment.