diff options
author | andre.schmid <andre.schmid@est.tech> | 2021-05-14 20:38:45 +0100 |
---|---|---|
committer | Christophe Closset <christophe.closset@intl.att.com> | 2021-06-14 08:16:08 +0000 |
commit | c82aebcde26e34c4151531b4d7a8f6e7689734ba (patch) | |
tree | fe14e6fadded7f43f9e1634b89d1fb9358b44253 /catalog-model/src/main | |
parent | ab6a90df6444ef7282fe9de8fe8107641bf7082f (diff) |
Add models imports endpoint and persistence structure
Create the structure to persist the model imports.
Changed create model API, allowing to create a model along its TOSCA
descriptor import structure.
Introduced an endpoint to update the imports of a model.
Change-Id: Ic775ef544051c29c721cacc20b37c2fb20338be9
Issue-ID: SDC-3614
Signed-off-by: André Schmid <andre.schmid@est.tech>
Diffstat (limited to 'catalog-model/src/main')
2 files changed, 133 insertions, 10 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 new file mode 100644 index 0000000000..c2ad071d13 --- /dev/null +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/exception/ModelOperationExceptionSupplier.java @@ -0,0 +1,50 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception; + +import java.util.function.Supplier; +import org.openecomp.sdc.be.dao.api.ActionStatus; + +/** + * Supplies operation exception needed by the the Model logic + */ +public class ModelOperationExceptionSupplier { + + private ModelOperationExceptionSupplier() { + + } + + public static Supplier<OperationException> invalidModel(final String modelName) { + return () -> new OperationException(ActionStatus.INVALID_MODEL, modelName); + } + + public static Supplier<OperationException> emptyModelImports() { + return () -> new OperationException(ActionStatus.MODEL_IMPORTS_IS_EMPTY); + } + + public static Supplier<OperationException> couldNotReadImports() { + return () -> new OperationException(ActionStatus.COULD_NOT_READ_MODEL_IMPORTS); + } + + public static Supplier<OperationException> modelAlreadyExists(final String modelName) { + return () -> new OperationException(ActionStatus.MODEL_ALREADY_EXISTS, modelName); + } + +} 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 c604df6dde..ccc18e57dd 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 @@ -19,12 +19,27 @@ package org.openecomp.sdc.be.model.operations.impl; import fj.data.Either; +import java.nio.charset.StandardCharsets; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; import java.util.Objects; +import java.util.Optional; +import java.util.stream.Collectors; +import org.apache.commons.collections.MapUtils; +import org.apache.commons.lang3.StringUtils; import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.dao.cassandra.ToscaModelImportCassandraDao; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; +import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao; +import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; +import org.openecomp.sdc.be.data.model.ToscaImportByModel; +import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; import org.openecomp.sdc.be.model.Model; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.ModelOperationExceptionSupplier; import org.openecomp.sdc.be.resources.data.ModelData; import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode; import org.openecomp.sdc.common.log.wrappers.Logger; @@ -32,28 +47,34 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component("model-operation") -public class ModelOperation extends AbstractOperation { +public class ModelOperation { private static final Logger log = Logger.getLogger(ModelOperation.class); - private final JanusGraphGenericDao genericDao; + private final JanusGraphGenericDao janusGraphGenericDao; + private final JanusGraphDao janusGraphDao; + private final ToscaModelImportCassandraDao toscaModelImportCassandraDao; @Autowired - public ModelOperation(final JanusGraphGenericDao janusGraphGenericDao) { - this.genericDao = janusGraphGenericDao; + public ModelOperation(final JanusGraphGenericDao janusGraphGenericDao, + final JanusGraphDao janusGraphDao, + final ToscaModelImportCassandraDao toscaModelImportCassandraDao) { + this.janusGraphGenericDao = janusGraphGenericDao; + this.janusGraphDao = janusGraphDao; + this.toscaModelImportCassandraDao = toscaModelImportCassandraDao; } public Model createModel(final Model model, final boolean inTransaction) { Model result = null; - final ModelData modelData = new ModelData(model.getName(), UniqueIdBuilder.buildModelUid(model.getName())); + final var modelData = new ModelData(model.getName(), UniqueIdBuilder.buildModelUid(model.getName())); try { - final Either<ModelData, JanusGraphOperationStatus> createNode = genericDao.createNode(modelData, ModelData.class); + final Either<ModelData, JanusGraphOperationStatus> createNode = janusGraphGenericDao.createNode(modelData, ModelData.class); if (createNode.isRight()) { - final JanusGraphOperationStatus janusGraphOperationStatus = createNode.right().value(); + final var janusGraphOperationStatus = createNode.right().value(); log.error(EcompLoggerErrorCode.DATA_ERROR, ModelOperation.class.getName(), "Problem while creating model, reason {}", janusGraphOperationStatus); if (janusGraphOperationStatus == JanusGraphOperationStatus.JANUSGRAPH_SCHEMA_VIOLATION) { - throw new OperationException(ActionStatus.MODEL_ALREADY_EXISTS, model.getName()); + throw ModelOperationExceptionSupplier.modelAlreadyExists(model.getName()).get(); } throw new OperationException(ActionStatus.GENERAL_ERROR, String.format("Failed to create model %s on JanusGraph with %s error", model, janusGraphOperationStatus)); @@ -63,14 +84,66 @@ public class ModelOperation extends AbstractOperation { } finally { if (!inTransaction) { if (Objects.nonNull(result)) { - genericDao.commit(); + janusGraphGenericDao.commit(); } else { - genericDao.rollback(); + janusGraphGenericDao.rollback(); } } } } + public Optional<GraphVertex> findModelVertexByName(final String name) { + if (StringUtils.isEmpty(name)) { + return Optional.empty(); + } + 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)); + } + return Optional.ofNullable(result.left().value().get(0)); + } + + public Optional<Model> findModelByName(final String name) { + if (StringUtils.isEmpty(name)) { + return Optional.empty(); + } + final Optional<GraphVertex> modelVertexOpt = findModelVertexByName(name); + if (modelVertexOpt.isEmpty()) { + return Optional.empty(); + } + + final GraphVertex graphVertex = modelVertexOpt.get(); + final var model = new Model((String) graphVertex.getMetadataProperty(GraphPropertyEnum.NAME)); + return Optional.of(model); + } + + public void createModelImports(final String modelId, final Map<String, byte[]> zipContent) { + if (MapUtils.isEmpty(zipContent)) { + return; + } + final List<ToscaImportByModel> toscaImportByModelList = zipContent.entrySet().stream() + .map(entry -> { + final String path = entry.getKey(); + final byte[] bytes = entry.getValue(); + final String content = new String(bytes, StandardCharsets.UTF_8); + final var toscaImportByModel = new ToscaImportByModel(); + toscaImportByModel.setModelId(modelId); + toscaImportByModel.setFullPath(path); + toscaImportByModel.setContent(content); + return toscaImportByModel; + }).collect(Collectors.toList()); + toscaModelImportCassandraDao.importAll(modelId, toscaImportByModelList); + } } |