diff options
author | KrupaNagabhushan <krupa.nagabhushan@est.tech> | 2021-09-02 13:15:51 +0100 |
---|---|---|
committer | Michael Morris <michael.morris@est.tech> | 2021-09-20 13:35:20 +0000 |
commit | 3608fa7d186a3acffda0f3040abaad068a081410 (patch) | |
tree | f7591a69f107b42a2b82f8ff923f136eea4ea024 /catalog-model/src/main/java/org/openecomp | |
parent | 79165dcada554b625553e982b39d817c7f9d1747 (diff) |
Error re-importing VSP
Issue-ID: SDC-3728
Signed-off-by: KrupaNagabhushan <krupa.nagabhushan@est.tech>
Signed-off-by: André Schmid <andre.schmid@est.tech>
Change-Id: I22d6186b8e6922511a7ede584d009cfae041fabd
Diffstat (limited to 'catalog-model/src/main/java/org/openecomp')
7 files changed, 213 insertions, 22 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementLifecycleOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementLifecycleOperation.java index 963458b71a..d46d4a554e 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementLifecycleOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementLifecycleOperation.java @@ -60,10 +60,12 @@ import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; +import org.openecomp.sdc.be.datatypes.enums.ModelTypeEnum; import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum; import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; import org.openecomp.sdc.be.model.DistributionStatusEnum; import org.openecomp.sdc.be.model.LifecycleStateEnum; +import org.openecomp.sdc.be.model.Model; import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate; import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement; @@ -71,10 +73,12 @@ import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum; import org.openecomp.sdc.be.model.jsonjanusgraph.enums.JsonConstantKeysEnum; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter; +import org.openecomp.sdc.be.model.operations.impl.ModelOperation; import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; import org.openecomp.sdc.common.jsongraph.util.CommonUtility; import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum; import org.openecomp.sdc.common.log.wrappers.Logger; +import org.springframework.beans.factory.annotation.Autowired; @org.springframework.stereotype.Component("tosca-element-lifecycle-operation") /** @@ -87,6 +91,12 @@ public class ToscaElementLifecycleOperation extends BaseOperation { private static final String FAILED_TO_DELETE_LAST_STATE_EDGE_STATUS_IS = "Failed to delete last state edge. Status is {}. "; private static final String FAILED_TO_GET_VERTICES = "Failed to get vertices by id {}. Status is {}. "; private static final Logger log = Logger.getLogger(ToscaElementLifecycleOperation.class); + private final ModelOperation modelOperation; + + @Autowired + public ToscaElementLifecycleOperation(ModelOperation modelOperation) { + this.modelOperation = modelOperation; + } static StorageOperationStatus handleFailureToPrepareParameters(final JanusGraphOperationStatus status, final String toscaElementId) { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId); @@ -223,12 +233,12 @@ public class ToscaElementLifecycleOperation extends BaseOperation { * @param toscaElementId * @return */ - public Either<ToscaElement, StorageOperationStatus> undoCheckout(String toscaElementId) { + public Either<ToscaElement, StorageOperationStatus> undoCheckout(String toscaElementId, String model) { try { return janusGraphDao.getVertexById(toscaElementId, JsonParseFlagEnum.ParseMetadata).right().map(errorStatus -> { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId); return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(errorStatus); - }).left().bind(this::retrieveAndUpdatePreviousVersion).left().bind(this::updateEdgeToCatalogRootAndReturnPreVersionElement); + }).left().bind(this::retrieveAndUpdatePreviousVersion).left().bind(tuple -> updateEdgeToCatalogRootAndReturnPreVersionElement(tuple, model)); } catch (Exception e) { CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Exception occurred during undo checkout tosca element {}. {}", toscaElementId, e.getMessage()); @@ -260,13 +270,17 @@ public class ToscaElementLifecycleOperation extends BaseOperation { } private Either<ToscaElement, StorageOperationStatus> updateEdgeToCatalogRootAndReturnPreVersionElement( - final P2<GraphVertex, JanusGraphVertex> tuple) { + final P2<GraphVertex, JanusGraphVertex> tuple, final String model) { final GraphVertex currVersionV = tuple._1(); final JanusGraphVertex preVersionVertex = tuple._2(); StorageOperationStatus updateCatalogRes = updateEdgeToCatalogRootByUndoCheckout(preVersionVertex, currVersionV); if (updateCatalogRes != StorageOperationStatus.OK) { return Either.right(updateCatalogRes); } else { + final Optional<Model> modelOptional = modelOperation.findModelByName(model); + if (modelOptional.isPresent() && modelOptional.get().getModelType() == ModelTypeEnum.NORMATIVE_EXTENSION) { + modelOperation.deleteModel(modelOptional.get(), false); + } final ToscaElementOperation operation = getToscaElementOperation(currVersionV.getLabel()); return operation.deleteToscaElement(currVersionV).left().bind(discarded -> getUpdatedPreVersionElement(operation, preVersionVertex)); } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/exception/OperationException.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/exception/OperationException.java index 30323af521..763151683f 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/exception/OperationException.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/exception/OperationException.java @@ -35,7 +35,13 @@ public class OperationException extends BusinessException { params = new String[0]; } - public OperationException(final ActionStatus actionStatus, String... params) { + public OperationException(final ActionStatus actionStatus, final String... params) { + this.actionStatus = actionStatus; + this.params = params; + } + + public OperationException(final Throwable cause, final ActionStatus actionStatus, final String... params) { + super(cause); this.actionStatus = actionStatus; this.params = params; } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperation.java index d6cca3f824..a0963c9030 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperation.java @@ -24,15 +24,17 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.collections4.MapUtils; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.janusgraph.core.JanusGraph; import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity; import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphGenericDao; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; +import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels; +import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; -import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.resources.data.DataTypeData; import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode; import org.slf4j.Logger; @@ -45,12 +47,16 @@ public class DataTypeOperation extends AbstractOperation { private static final Logger LOGGER = LoggerFactory.getLogger(DataTypeOperation.class); - private final ModelOperation modelOperation; + private ModelOperation modelOperation; @Autowired - public DataTypeOperation(final HealingJanusGraphGenericDao janusGraphGenericDao, - final ModelOperation modelOperation) { + public DataTypeOperation(final HealingJanusGraphGenericDao janusGraphGenericDao) { this.janusGraphGenericDao = janusGraphGenericDao; + } + + //circular dependency ModelOperation->ModelElementOperation->DataTypeOperation + @Autowired + public void setModelOperation(final ModelOperation modelOperation) { this.modelOperation = modelOperation; } @@ -132,4 +138,18 @@ public class DataTypeOperation extends AbstractOperation { return getDataTypes.left().value(); } + public void deleteDataTypesByModelId(final String modelId) { + final JanusGraph janusGraph = janusGraphGenericDao.getJanusGraph(); + final GraphTraversalSource traversal = janusGraph.traversal(); + final List<Vertex> dataTypeList = traversal.V() + .has(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), modelId) + .out(GraphEdgeLabels.MODEL_ELEMENT.getProperty()) + .has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.DataType.getName()) + .toList(); + dataTypeList.forEach(dataTypeVertex -> { + traversal.V(dataTypeVertex).out(GraphEdgeLabels.PROPERTY.getProperty()).drop().iterate(); + dataTypeVertex.remove(); + }); + } + } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelElementOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelElementOperation.java new file mode 100644 index 0000000000..048fbfd067 --- /dev/null +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelElementOperation.java @@ -0,0 +1,74 @@ +/* + * - + * ============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.operations.impl; + +import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao; +import org.openecomp.sdc.be.model.Model; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component("model-element-operation") +public class ModelElementOperation { + + private final JanusGraphGenericDao janusGraphGenericDao; + private final DataTypeOperation dataTypeOperation; + private final PolicyTypeOperation policyTypeOperation; + + @Autowired + public ModelElementOperation(final JanusGraphGenericDao janusGraphGenericDao, + final DataTypeOperation dataTypeOperation, + final PolicyTypeOperation policyTypeOperation) { + this.janusGraphGenericDao = janusGraphGenericDao; + this.dataTypeOperation = dataTypeOperation; + this.policyTypeOperation = policyTypeOperation; + } + + /** + * Deletes the given model if it exists, along with its MODEL_ELEMENT edges and import files. + * + * @param model the model + * @param inTransaction if the operation is called in the middle of a janusgraph transaction + */ + public void deleteModelElements(final Model model, final boolean inTransaction) { + boolean rollback = false; + + try { + final String modelId = UniqueIdBuilder.buildModelUid(model.getName()); + dataTypeOperation.deleteDataTypesByModelId(modelId); + policyTypeOperation.deletePolicyTypesByModelId(modelId); + } catch (final Exception e) { + rollback = true; + throw new OperationException(e, ActionStatus.COULD_NOT_DELETE_MODEL_ELEMENTS, model.getName()); + } finally { + if (!inTransaction) { + if (rollback) { + janusGraphGenericDao.rollback(); + } else { + janusGraphGenericDao.commit(); + } + } + } + } + +} 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 095b4e1cef..b5cb9d98c0 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 @@ -38,10 +38,10 @@ import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.dao.cassandra.ToscaModelImportCassandraDao; import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge; import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao; 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.janusgraph.JanusGraphDao; import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels; import org.openecomp.sdc.be.data.model.ToscaImportByModel; @@ -71,6 +71,7 @@ public class ModelOperation { private final JanusGraphDao janusGraphDao; private final ToscaModelImportCassandraDao toscaModelImportCassandraDao; private final DerivedFromOperation derivedFromOperation; + private ModelElementOperation modelElementOperation; @Autowired public ModelOperation(final JanusGraphGenericDao janusGraphGenericDao, @@ -322,4 +323,54 @@ public class ModelOperation { + "description: Auto-generated file that contains package custom types or types added after system installation." + "\n"; } + /** + * Deletes the given model if it exists, along with its MODEL_ELEMENT edges and import files. + * + * @param model the model + * @param inTransaction if the operation is called in the middle of a janusgraph transaction + */ + public void deleteModel(final Model model, final boolean inTransaction) { + boolean rollback = false; + + try { + final GraphVertex modelVertexByName = findModelVertexByName(model.getName()).orElse(null); + if (modelVertexByName == null) { + return; + } + toscaModelImportCassandraDao.deleteAllByModel(model.getName()); + modelElementOperation.deleteModelElements(model, inTransaction); + deleteModel(model); + } catch (final OperationException e) { + rollback = true; + throw e; + } catch (final Exception e) { + rollback = true; + throw new OperationException(e, ActionStatus.COULD_NOT_DELETE_MODEL, model.getName()); + } finally { + if (!inTransaction) { + if (rollback) { + janusGraphGenericDao.rollback(); + } else { + janusGraphGenericDao.commit(); + } + } + } + } + + private void deleteModel(final Model model) { + final var modelData = new ModelData(model.getName(), UniqueIdBuilder.buildModelUid(model.getName()), model.getModelType()); + final Either<ModelData, JanusGraphOperationStatus> deleteParentNodeByModel = janusGraphGenericDao.deleteNode(modelData, ModelData.class); + if (deleteParentNodeByModel.isRight()) { + final var janusGraphOperationStatus = deleteParentNodeByModel.right().value(); + log.error(EcompLoggerErrorCode.DATA_ERROR, ModelOperation.class.getName(), + "Failed to delete model {} on JanusGraph with status {}", new Object[] {model.getName(), janusGraphOperationStatus}); + throw new OperationException(ActionStatus.COULD_NOT_DELETE_MODEL, model.getName()); + } + } + + @Autowired + public void setModelElementOperation(final ModelElementOperation modelElementOperation) { + this.modelElementOperation = modelElementOperation; + } + } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PolicyTypeOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PolicyTypeOperation.java index 99a284168e..db3c5574b4 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PolicyTypeOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PolicyTypeOperation.java @@ -18,8 +18,8 @@ * ============LICENSE_END========================================================= */ package org.openecomp.sdc.be.model.operations.impl; + import static org.openecomp.sdc.be.dao.janusgraph.JanusGraphUtils.buildNotInPredicate; -import static org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR; import fj.data.Either; import java.util.ArrayList; @@ -31,6 +31,9 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.janusgraph.core.JanusGraph; import org.janusgraph.graphdb.query.JanusGraphPredicate; import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge; @@ -60,13 +63,17 @@ public class PolicyTypeOperation extends AbstractOperation implements IPolicyTyp private static final Logger log = Logger.getLogger(PolicyTypeOperation.class.getName()); private static final String CREATE_FLOW_CONTEXT = "CreatePolicyType"; private static final String GET_FLOW_CONTEXT = "GetPolicyType"; - @Autowired - private PropertyOperation propertyOperation; - @Autowired - private DerivedFromOperation derivedFromOperation; - @Autowired - private OperationUtils operationUtils; + private final PropertyOperation propertyOperation; + private final DerivedFromOperation derivedFromOperation; + private final OperationUtils operationUtils; + @Autowired + public PolicyTypeOperation(final PropertyOperation propertyOperation, final DerivedFromOperation derivedFromOperation, + final OperationUtils operationUtils) { + this.propertyOperation = propertyOperation; + this.derivedFromOperation = derivedFromOperation; + this.operationUtils = operationUtils; + } @Override public Either<PolicyTypeDefinition, StorageOperationStatus> getLatestPolicyTypeByType(String type, String model) { @@ -328,4 +335,18 @@ public class PolicyTypeOperation extends AbstractOperation implements IPolicyTyp updatedTypeDefinition.setCreationTime(currTypeDefinition.getCreationTime()); updatedTypeDefinition.setModificationTime(System.currentTimeMillis()); } + + public void deletePolicyTypesByModelId(final String modelId) { + final JanusGraph janusGraph = janusGraphGenericDao.getJanusGraph(); + final GraphTraversalSource traversal = janusGraph.traversal(); + final List<Vertex> policyTypeList = traversal.V() + .has(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), modelId) + .out(GraphEdgeLabels.MODEL_ELEMENT.getProperty()) + .has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.PolicyType.getName()) + .toList(); + policyTypeList.forEach(policyTypeVertex -> { + traversal.V(policyTypeVertex).out(GraphEdgeLabels.PROPERTY.getProperty()).drop().iterate(); + policyTypeVertex.remove(); + }); + } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java index 01dbb07fb4..bf8d6aed15 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java @@ -119,15 +119,19 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe private static final String THE_VALUE_OF_PROPERTY_FROM_TYPE_IS_INVALID = "The value {} of property from type {} is invalid"; private static final String PROPERTY = "Property"; private static final String UPDATE_DATA_TYPE = "UpdateDataType"; - private static Logger log = Logger.getLogger(PropertyOperation.class.getName()); - private DerivedFromOperation derivedFromOperation; + private static final Logger log = Logger.getLogger(PropertyOperation.class.getName()); + private final DerivedFromOperation derivedFromOperation; private DataTypeOperation dataTypeOperation; @Autowired - public PropertyOperation(HealingJanusGraphGenericDao janusGraphGenericDao, DerivedFromOperation derivedFromOperation, - DataTypeOperation dataTypeOperation) { + public PropertyOperation(final HealingJanusGraphGenericDao janusGraphGenericDao, final DerivedFromOperation derivedFromOperation) { this.janusGraphGenericDao = janusGraphGenericDao; this.derivedFromOperation = derivedFromOperation; + } + + //circular dependency DataTypeOperation->ModelOperation->ModelElementOperation->PropertyOperation + @Autowired + public void setDataTypeOperation(DataTypeOperation dataTypeOperation) { this.dataTypeOperation = dataTypeOperation; } @@ -2264,4 +2268,5 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe return null; } } + } |