diff options
author | 2021-10-20 23:54:04 +0200 | |
---|---|---|
committer | 2021-10-20 23:54:04 +0200 | |
commit | 44165469656ceaa53f52ebf4cf88cfeaf65c8f2a (patch) | |
tree | 39e130c70ff0b8937fd289e1db8959aaa98d0e23 | |
parent | 55ed528ac81e302dd779049cf3cb017be97eff33 (diff) |
Fix issue with limit of aai bulk process limit
Issue-ID: SO-3795
Signed-off-by: Lukasz Rajewski <lukasz.rajewski@orange.com>
Change-Id: I7bd59a64756eb81e6e1edfc294017f5b5830784a
-rw-r--r-- | so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/util/AaiRepository.java | 70 |
1 files changed, 57 insertions, 13 deletions
diff --git a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/util/AaiRepository.java b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/util/AaiRepository.java index 2ca9e3a..0531728 100644 --- a/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/util/AaiRepository.java +++ b/so-cnf-adapter-application/src/main/java/org/onap/so/adapters/cnf/util/AaiRepository.java @@ -39,6 +39,7 @@ import org.onap.so.adapters.cnf.service.aai.KubernetesResource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -49,7 +50,7 @@ public class AaiRepository implements IAaiRepository { private final AAIResourcesClient aaiClient; private final ObjectMapper objectMapper; - private AAITransactionalClient transaction; + private final AAITransactionHelper aaiTransactionHelper; public static IAaiRepository instance() { return new AaiRepository(); @@ -57,25 +58,17 @@ public class AaiRepository implements IAaiRepository { private AaiRepository() { aaiClient = new AAIResourcesClient(AAIVersion.LATEST); + aaiTransactionHelper = new AAITransactionHelper(aaiClient); this.objectMapper = new ObjectMapper(); } - private AAITransactionalClient getTransaction() { - if (transaction == null) - transaction = aaiClient.beginTransaction(); - return transaction; + private AAITransactionHelper getTransaction() { + return aaiTransactionHelper; } @Override public void commit(boolean dryrun) { - try { - if (transaction != null) - transaction.execute(dryrun); - else - logger.info("Nothing to commit in AAI"); - } catch (BulkProcessFailed bulkProcessFailed) { - throw new RuntimeException("Failed to exectute transaction", bulkProcessFailed); - } + aaiTransactionHelper.execute(dryrun); } @Override @@ -175,4 +168,55 @@ public class AaiRepository implements IAaiRepository { }).forEach(uri -> getTransaction().delete(uri)); } } + + static class AAITransactionHelper { + private List<AAITransactionalClient> transactions; + private final AAIResourcesClient aaiClient; + private int transactionCount; + + private static final int TRANSACTION_LIMIT = 30; + + public AAITransactionHelper(AAIResourcesClient aaiClient) { + this.aaiClient = aaiClient; + transactions = new ArrayList<AAITransactionalClient>(); + transactionCount = TRANSACTION_LIMIT; + } + + private AAITransactionalClient getTransaction() { + if (transactionCount == TRANSACTION_LIMIT) { + transactions.add(aaiClient.beginTransaction()); + transactionCount = 0; + } + return transactions.get(transactions.size() - 1); + } + + public void execute(boolean dryRun) { + if (transactions.size() > 0) { + transactions.forEach(transaction -> { + try { + transaction.execute(dryRun); + } catch (BulkProcessFailed e) { + throw new RuntimeException("Failed to execute transaction", e); + } + }); + transactions.clear(); + } else + logger.info("Nothing to commit in AAI"); + } + + void create(AAIResourceUri uri, String payload) { + getTransaction().create(uri, payload); + transactionCount++; + } + + void connect(AAIResourceUri uriA, AAIResourceUri uriB) { + getTransaction().connect(uriA, uriB); + transactionCount++; + } + + void delete(AAIResourceUri uri) { + getTransaction().delete(uri); + transactionCount++; + } + } } |