From 2ca2fc5c0da1eb862fcd79d1f9345aa89e62b396 Mon Sep 17 00:00:00 2001 From: ojasdubey Date: Mon, 18 Mar 2019 11:55:56 +0530 Subject: Service Consumption BE 1. Service consumption feature backend implementation 2. Operation output bug fix for delete operation not allowed for mapped operation output Change-Id: Ib2554eed4f940b003955263a0c8bf795a23cac9a Issue-ID: SDC-1990 Signed-off-by: ojasdubey --- .../tasks/mig1902/InterfaceOperationMigration.java | 167 +++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1902/InterfaceOperationMigration.java (limited to 'asdctool/src/main') diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1902/InterfaceOperationMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1902/InterfaceOperationMigration.java new file mode 100644 index 0000000000..afbd3b411a --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1902/InterfaceOperationMigration.java @@ -0,0 +1,167 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + */ + +package org.openecomp.sdc.asdctool.migration.tasks.mig1902; + +import fj.data.Either; +import java.math.BigInteger; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; +import org.apache.commons.collections.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.openecomp.sdc.asdctool.migration.core.DBVersion; +import org.openecomp.sdc.asdctool.migration.core.task.Migration; +import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult; +import org.openecomp.sdc.be.config.ConfigurationManager; +import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; +import org.openecomp.sdc.be.dao.jsongraph.TitanDao; +import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum; +import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; +import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; +import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; +import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.MapInterfaceDataDefinition; +import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; +import org.openecomp.sdc.be.model.User; +import org.openecomp.sdc.be.model.jsontitan.operations.InterfaceOperation; +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.UserAdminOperation; +import org.openecomp.sdc.common.log.wrappers.Logger; +import org.springframework.beans.factory.annotation.Autowired; + +@org.springframework.stereotype.Component +public class InterfaceOperationMigration implements Migration { + + private static final Logger LOGGER = Logger.getLogger(InterfaceOperationMigration.class); + + @Autowired + private TitanDao titanDao; + @Autowired + private UserAdminOperation userAdminOperation; + @Autowired + private InterfaceOperation interfaceOperation; + + @Override + public String description() { + return "Update interface operation data to latest data model"; + } + + @Override + public DBVersion getVersion() { + return DBVersion.from(BigInteger.valueOf(1902), BigInteger.valueOf(0)); + } + + @Override + public MigrationResult migrate() { + final String userId = ConfigurationManager.getConfigurationManager().getConfiguration().getAutoHealingOwner(); + + Either userData = userAdminOperation.getUserData(userId, false); + if (userData.isRight()) { + return MigrationResult.error( + "failed to update interface operation data. Failed to resolve user : " + + userId + " error " + userData.right().value()); + } + + StorageOperationStatus status = getAndUpdateAllComponents(); + return status == StorageOperationStatus.OK ? MigrationResult.success() + : MigrationResult.error("failed to update interface operation data . Error : " + status); + } + + private StorageOperationStatus getAndUpdateAllComponents(){ + Map hasNotProps = new EnumMap<>(GraphPropertyEnum.class); + hasNotProps.put(GraphPropertyEnum.IS_DELETED, true); + return titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, null, hasNotProps, JsonParseFlagEnum.ParseAll) + .either(this::updateComponentVertices, this::handleError); + } + + private StorageOperationStatus updateComponentVertices(List containersV) { + StorageOperationStatus status = StorageOperationStatus.OK; + for (GraphVertex container : containersV) { + status = updateDataOnGraph(container); + if (status != StorageOperationStatus.OK) { + break; + } + } + return status; + } + + private StorageOperationStatus handleError(TitanOperationStatus err) { + titanDao.rollback(); + return DaoStatusConverter.convertTitanStatusToStorageStatus( + TitanOperationStatus.NOT_FOUND == err ? TitanOperationStatus.OK : err); + } + + private StorageOperationStatus updateDataOnGraph(GraphVertex componentVertex) { + try { + Either interfaceVertexEither = + titanDao.getChildVertex(componentVertex, EdgeLabelEnum.INTERFACE, JsonParseFlagEnum.ParseJson); + if (interfaceVertexEither.isLeft()) { + GraphVertex interfaceVertex = interfaceVertexEither.left().value(); + Map interfaceDefinitions = (Map) interfaceVertex.getJson(); + if(MapUtils.isNotEmpty(interfaceDefinitions)){ + for (Map.Entry interfaceDefinition : interfaceDefinitions.entrySet()) { + if (StringUtils.isEmpty(interfaceDefinition.getValue().getType())) { + interfaceDefinition.getValue().setType(interfaceDefinition.getValue().getToscaResourceName()); + } + } + interfaceVertex.setJson(interfaceDefinitions); + Either updateInterfaceVertexEither = titanDao.updateVertex(interfaceVertex); + if(updateInterfaceVertexEither.isRight()){ + return DaoStatusConverter.convertTitanStatusToStorageStatus(updateInterfaceVertexEither.right().value()); + } + } + + StorageOperationStatus statusRes = interfaceOperation.removeToscaDataVertex( + interfaceVertex, EdgeLabelEnum.INTERFACE_OPERATION, VertexTypeEnum.INTERFACE_OPERATION); + if (statusRes != StorageOperationStatus.NOT_FOUND && statusRes != StorageOperationStatus.OK) { + return statusRes; + } + } + + Either instInterfaceVertexEither = + titanDao.getChildVertex(componentVertex, EdgeLabelEnum.INST_INTERFACES, JsonParseFlagEnum.ParseJson); + if (instInterfaceVertexEither.isLeft()) { + GraphVertex instInterfaceVertex = instInterfaceVertexEither.left().value(); + Map instInterfaceDefinitions = (Map) instInterfaceVertex.getJson(); + if(MapUtils.isNotEmpty(instInterfaceDefinitions)){ + for (Map.Entry mapInstInterfaceDataDefinitions : instInterfaceDefinitions.entrySet()) { + for (Map.Entry instInterfaceDataDefinitions : mapInstInterfaceDataDefinitions.getValue().getMapToscaDataDefinition().entrySet()) { + if (StringUtils.isEmpty(instInterfaceDataDefinitions.getValue().getType())) { + instInterfaceDataDefinitions.getValue().setType(instInterfaceDataDefinitions.getValue().getToscaResourceName()); + } + } + } + instInterfaceVertex.setJson(instInterfaceDefinitions); + Either updateInstInterfaceVertexEither = titanDao.updateVertex(instInterfaceVertex); + if(updateInstInterfaceVertexEither.isRight()){ + return DaoStatusConverter.convertTitanStatusToStorageStatus(updateInstInterfaceVertexEither.right().value()); + } + } + } + + titanDao.commit(); + } catch (Exception e) { + LOGGER.debug("Interface operation migration failed with error : ", e); + titanDao.rollback(); + return StorageOperationStatus.GENERAL_ERROR; + } + + return StorageOperationStatus.OK; + } +} \ No newline at end of file -- cgit 1.2.3-korg