summaryrefslogtreecommitdiffstats
path: root/catalog-model/src/main
diff options
context:
space:
mode:
authorandre.schmid <andre.schmid@est.tech>2021-06-21 22:25:28 +0100
committerChristophe Closset <christophe.closset@intl.att.com>2021-07-05 09:21:57 +0000
commitb835031b83230c36649c6e77787867a465e0ac47 (patch)
treec988e23a137da1f2a757693f56ab1a457394444f /catalog-model/src/main
parent70975c212b58b793d3893e131a3d23d8144f5df9 (diff)
Create REST endpoint to retrieve models
Change-Id: Ifca0095d84d5da4ab4b055942d893e9c6a259eb7 Issue-ID: SDC-3622 Signed-off-by: André Schmid <andre.schmid@est.tech>
Diffstat (limited to 'catalog-model/src/main')
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/exception/ModelOperationExceptionSupplier.java6
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java55
2 files changed, 48 insertions, 13 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/exception/ModelOperationExceptionSupplier.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/exception/ModelOperationExceptionSupplier.java
index c2ad071d13..6622bf5bd8 100644
--- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/exception/ModelOperationExceptionSupplier.java
+++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/exception/ModelOperationExceptionSupplier.java
@@ -21,6 +21,7 @@ package org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception;
import java.util.function.Supplier;
import org.openecomp.sdc.be.dao.api.ActionStatus;
+import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
/**
* Supplies operation exception needed by the the Model logic
@@ -47,4 +48,9 @@ public class ModelOperationExceptionSupplier {
return () -> new OperationException(ActionStatus.MODEL_ALREADY_EXISTS, modelName);
}
+ public static Supplier<OperationException> failedToRetrieveModels(final JanusGraphOperationStatus janusGraphOperationStatus) {
+ var errorMsg = String.format("Failed to retrieve models. Status '%s'", janusGraphOperationStatus);
+ return () -> new OperationException(ActionStatus.GENERAL_ERROR, errorMsg);
+ }
+
}
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java
index ccc18e57dd..ddc0367a44 100644
--- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java
+++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java
@@ -20,6 +20,7 @@ package org.openecomp.sdc.be.model.operations.impl;
import fj.data.Either;
import java.nio.charset.StandardCharsets;
+import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
@@ -99,18 +100,11 @@ public class ModelOperation {
final Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
props.put(GraphPropertyEnum.NAME, name);
props.put(GraphPropertyEnum.UNIQUE_ID, UniqueIdBuilder.buildModelUid(name));
- final Either<List<GraphVertex>, JanusGraphOperationStatus> result = janusGraphDao.getByCriteria(VertexTypeEnum.MODEL, props);
- if (result.isRight()) {
- final JanusGraphOperationStatus janusGraphOperationStatus = result.right().value();
- if (janusGraphOperationStatus == JanusGraphOperationStatus.NOT_FOUND) {
- return Optional.empty();
- }
- log.error(EcompLoggerErrorCode.DATA_ERROR, this.getClass().getName(),
- String.format("Problem while getting model %s. reason %s", name, janusGraphOperationStatus));
- throw new OperationException(ActionStatus.GENERAL_ERROR,
- String.format("Failed to get model %s on JanusGraph with %s error", name, janusGraphOperationStatus));
+ final List<GraphVertex> modelVerticesList = findModelVerticesByCriteria(props);
+ if (modelVerticesList.isEmpty()) {
+ return Optional.empty();
}
- return Optional.ofNullable(result.left().value().get(0));
+ return Optional.ofNullable(modelVerticesList.get(0));
}
public Optional<Model> findModelByName(final String name) {
@@ -123,8 +117,7 @@ public class ModelOperation {
}
final GraphVertex graphVertex = modelVertexOpt.get();
- final var model = new Model((String) graphVertex.getMetadataProperty(GraphPropertyEnum.NAME));
- return Optional.of(model);
+ return Optional.of(convertToModel(graphVertex));
}
public void createModelImports(final String modelId, final Map<String, byte[]> zipContent) {
@@ -144,6 +137,42 @@ public class ModelOperation {
}).collect(Collectors.toList());
toscaModelImportCassandraDao.importAll(modelId, toscaImportByModelList);
}
+
+ /**
+ * Finds all the models.
+ *
+ * @return the list of models
+ */
+ public List<Model> findAllModels() {
+ return findModelsByCriteria(Collections.emptyMap());
+ }
+
+ private List<Model> findModelsByCriteria(final Map<GraphPropertyEnum, Object> propertyCriteria) {
+ final List<GraphVertex> modelVerticesByCriteria = findModelVerticesByCriteria(propertyCriteria);
+ if (modelVerticesByCriteria.isEmpty()) {
+ return Collections.emptyList();
+ }
+
+ return modelVerticesByCriteria.stream().map(this::convertToModel).collect(Collectors.toList());
+ }
+
+ private List<GraphVertex> findModelVerticesByCriteria(final Map<GraphPropertyEnum, Object> propertyCriteria) {
+ final Either<List<GraphVertex>, JanusGraphOperationStatus> result = janusGraphDao.getByCriteria(VertexTypeEnum.MODEL, propertyCriteria);
+ if (result.isRight()) {
+ final var janusGraphOperationStatus = result.right().value();
+ if (janusGraphOperationStatus == JanusGraphOperationStatus.NOT_FOUND) {
+ return Collections.emptyList();
+ }
+ final var operationException = ModelOperationExceptionSupplier.failedToRetrieveModels(janusGraphOperationStatus).get();
+ log.error(EcompLoggerErrorCode.DATA_ERROR, this.getClass().getName(), operationException.getMessage());
+ throw operationException;
+ }
+ return result.left().value();
+ }
+
+ private Model convertToModel(final GraphVertex modelGraphVertex) {
+ return new Model((String) modelGraphVertex.getMetadataProperty(GraphPropertyEnum.NAME));
+ }
}