From 5b593496b8f1b8e8be8d7d2dbcc223332e65a49b Mon Sep 17 00:00:00 2001 From: Michael Lando Date: Sun, 29 Jul 2018 16:13:45 +0300 Subject: re base code Change-Id: I12a5ca14a6d8a87e9316b9ff362eb131105f98a5 Issue-ID: SDC-1566 Signed-off-by: Michael Lando --- .../java/org/openecomp/sdc/asdctool/Utils.java | 25 +- .../org/openecomp/sdc/asdctool/cli/CLITool.java | 7 +- .../configuration/ConfigurationUploader.java | 4 +- .../configuration/CsarGeneratorConfiguration.java | 39 + .../configuration/InternalToolConfiguration.java | 28 + .../configuration/VrfObjectFixConfiguration.java | 36 + .../configuration/mocks/es/ESCatalogDAOMock.java | 5 +- .../sdc/asdctool/enums/SchemaZipFileEnum.java | 113 +- .../sdc/asdctool/impl/ArtifactUuidFix.java | 43 +- .../openecomp/sdc/asdctool/impl/DataMigration.java | 1523 ++++++++++---------- .../sdc/asdctool/impl/GraphJsonValidator.java | 10 +- .../sdc/asdctool/impl/GraphMLConverter.java | 39 +- .../sdc/asdctool/impl/GraphMLDataAnalyzer.java | 14 +- .../openecomp/sdc/asdctool/impl/ProductLogic.java | 18 +- .../org/openecomp/sdc/asdctool/impl/RestUtils.java | 9 +- .../sdc/asdctool/impl/TitanGraphInitializer.java | 41 +- .../sdc/asdctool/impl/UpdatePropertyOnVertex.java | 20 +- .../sdc/asdctool/impl/VrfObjectFixHandler.java | 197 +++ .../impl/internal/tool/CommonInternalTool.java | 38 + .../asdctool/impl/internal/tool/CsarGenerator.java | 187 +++ .../impl/internal/tool/DeleteComponentHandler.java | 200 +++ .../asdctool/impl/validator/ArtifactToolBL.java | 16 +- .../asdctool/impl/validator/ValidationToolBL.java | 16 +- .../config/ValidationToolConfiguration.java | 17 +- .../executers/ArtifactValidatorExecuter.java | 234 ++- .../NodeToscaArtifactsValidatorExecuter.java | 8 +- .../ServiceToscaArtifactsValidatorExecutor.java | 8 +- .../executers/ServiceValidatorExecuter.java | 10 +- .../TopologyTemplateValidatorExecuter.java | 22 +- .../VFToscaArtifactValidatorExecutor.java | 8 +- .../validator/executers/VfValidatorExecuter.java | 6 +- .../tasks/artifacts/ArtifactValidationUtils.java | 9 +- .../tasks/artifacts/ArtifactsVertexResult.java | 4 +- .../tasks/artifacts/VfArtifactValidationTask.java | 1 - .../validator/tasks/moduleJson/ModuleJsonTask.java | 12 +- .../impl/validator/utils/ElementTypeEnum.java | 4 +- .../impl/validator/utils/ReportManager.java | 14 +- .../sdc/asdctool/main/ArtifactUUIDFixMenu.java | 5 +- .../sdc/asdctool/main/ArtifactValidatorTool.java | 5 +- .../sdc/asdctool/main/CsarGeneratorTool.java | 45 + .../sdc/asdctool/main/DataSchemaMenu.java | 5 +- .../sdc/asdctool/main/DeleteComponentTool.java | 55 + .../main/EsToCassandraDataMigrationMenu.java | 11 +- .../sdc/asdctool/main/GetConsumersMenu.java | 5 +- .../sdc/asdctool/main/SdcInternalTool.java | 16 + .../sdc/asdctool/main/SdcSchemaFileImport.java | 25 +- .../sdc/asdctool/main/UpdateIsVnfMenu.java | 13 +- .../sdc/asdctool/main/ValidationTool.java | 11 +- .../sdc/asdctool/main/VrfObjectFixMenu.java | 52 + .../migration/config/MigrationSpringConfig.java | 10 +- .../config/mocks/DistributionEngineMock.java | 12 +- .../config/mocks/HealthCheckBusinessLogicMock.java | 4 +- .../sdc/asdctool/migration/core/DBVersion.java | 10 +- .../asdctool/migration/core/SdcMigrationTool.java | 9 +- .../core/execution/MigrationExecutionResult.java | 4 +- .../core/execution/MigrationExecutorImpl.java | 5 +- .../asdctool/migration/dao/MigrationTasksDao.java | 24 +- .../sdc/asdctool/migration/main/MigrationMenu.java | 5 +- .../migration/resolver/MigrationResolver.java | 4 +- .../resolver/SpringBeansMigrationResolver.java | 10 +- .../asdctool/migration/service/SdcRepoService.java | 4 +- .../migration/tasks/handlers/OutputHandler.java | 4 +- .../migration/tasks/handlers/XlsOutputHandler.java | 81 +- .../tasks/mig1710/UpgradeMigration1710.java | 1044 +++++++++----- .../tasks/mig1802/SdcCatalogMigration.java | 25 +- .../tasks/mig1806/ForwardPathMigration.java | 253 ++++ .../tasks/mig1806/ResourceLifecycleMigration.java | 138 ++ .../tasks/mig1806/SDCInstancesMigration.java | 237 +++ .../tasks/mig1806/SdcArchiveMigration.java | 76 + .../sdc/asdctool/servlets/EntryPoint.java | 7 +- .../servlets/ExportImportTitanServlet.java | 31 +- .../simulator/tenant/ImportCassandraTableTool.java | 8 +- .../simulator/tenant/OperationalEnvironment.java | 3 +- .../OperationalEvnironmentImportHandler.java | 15 +- .../sdc/asdctool/utils/ConsoleWriter.java | 37 + .../openecomp/sdc/asdctool/utils/ReportWriter.java | 50 + .../src/main/resources/application-context.xml | 4 +- .../src/main/resources/config/configuration.yaml | 44 +- .../src/main/resources/config/titan.properties | 25 +- .../main/resources/scripts/deleteComponentTool.sh | 35 + .../src/main/resources/scripts/generateCsar.sh | 35 + .../src/main/resources/scripts/vrfObjectFix.sh | 35 + 82 files changed, 3760 insertions(+), 1761 deletions(-) create mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/configuration/CsarGeneratorConfiguration.java create mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/configuration/InternalToolConfiguration.java create mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/configuration/VrfObjectFixConfiguration.java create mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/VrfObjectFixHandler.java create mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/internal/tool/CommonInternalTool.java create mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/internal/tool/CsarGenerator.java create mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/internal/tool/DeleteComponentHandler.java create mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/main/CsarGeneratorTool.java create mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/main/DeleteComponentTool.java create mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcInternalTool.java create mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/main/VrfObjectFixMenu.java create mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1806/ForwardPathMigration.java create mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1806/ResourceLifecycleMigration.java create mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1806/SDCInstancesMigration.java create mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1806/SdcArchiveMigration.java create mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/utils/ConsoleWriter.java create mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/utils/ReportWriter.java create mode 100644 asdctool/src/main/resources/scripts/deleteComponentTool.sh create mode 100644 asdctool/src/main/resources/scripts/generateCsar.sh create mode 100644 asdctool/src/main/resources/scripts/vrfObjectFix.sh (limited to 'asdctool/src/main') diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/Utils.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/Utils.java index ce8b2c4ef7..e13f40fe29 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/Utils.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/Utils.java @@ -20,30 +20,23 @@ package org.openecomp.sdc.asdctool; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; - -import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.ResponseBuilder; - +import com.thinkaurelius.titan.core.TitanFactory; +import com.thinkaurelius.titan.core.TitanGraph; import org.apache.commons.configuration.Configuration; import org.apache.tinkerpop.gremlin.structure.Element; import org.apache.tinkerpop.gremlin.structure.Property; import org.apache.tinkerpop.gremlin.structure.util.ElementHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.common.log.wrappers.Logger; -import com.thinkaurelius.titan.core.TitanFactory; -import com.thinkaurelius.titan.core.TitanGraph; - -//import org.openecomp.sdc.be.auditing.impl.AuditingManager; - -//import org.openecomp.sdc.be.info.errors.ResponseFormat; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.ResponseBuilder; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; public class Utils { - private static Logger log = LoggerFactory.getLogger(Utils.class.getName()); + private static Logger log = Logger.getLogger(Utils.class.getName()); public final static String NEW_LINE = System.getProperty("line.separator"); diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/cli/CLITool.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/cli/CLITool.java index 053596d229..2e4c2b3afa 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/cli/CLITool.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/cli/CLITool.java @@ -1,11 +1,6 @@ package org.openecomp.sdc.asdctool.cli; -import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.CommandLineParser; -import org.apache.commons.cli.DefaultParser; -import org.apache.commons.cli.HelpFormatter; -import org.apache.commons.cli.Options; -import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/configuration/ConfigurationUploader.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/configuration/ConfigurationUploader.java index 267f20904c..ac75dc8310 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/configuration/ConfigurationUploader.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/configuration/ConfigurationUploader.java @@ -1,12 +1,12 @@ package org.openecomp.sdc.asdctool.configuration; -import java.io.File; - import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.common.api.ConfigurationSource; import org.openecomp.sdc.common.impl.ExternalConfiguration; import org.openecomp.sdc.common.impl.FSConfigurationSource; +import java.io.File; + public class ConfigurationUploader { public static void uploadConfigurationFiles(String appConfigDir) { diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/configuration/CsarGeneratorConfiguration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/configuration/CsarGeneratorConfiguration.java new file mode 100644 index 0000000000..06264c669e --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/configuration/CsarGeneratorConfiguration.java @@ -0,0 +1,39 @@ +package org.openecomp.sdc.asdctool.configuration; + +import org.openecomp.sdc.asdctool.impl.internal.tool.CsarGenerator; +import org.openecomp.sdc.be.components.distribution.engine.ServiceDistributionArtifactsBuilder; +import org.openecomp.sdc.be.config.CatalogModelSpringConfig; +import org.openecomp.sdc.be.dao.config.DAOSpringConfig; +import org.openecomp.sdc.config.CatalogBESpringConfig; +import org.springframework.beans.factory.config.PropertiesFactoryBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.core.io.FileSystemResource; + +@Configuration +@Import({DAOSpringConfig.class, CatalogBESpringConfig.class, CatalogModelSpringConfig.class}) +@ComponentScan({"org.openecomp.sdc.asdctool.migration.config.mocks" + }) +public class CsarGeneratorConfiguration { + + @Bean + public CsarGenerator csarGenerator() { + return new CsarGenerator(); + } + + @Bean(name = "elasticsearchConfig") + public PropertiesFactoryBean mapper() { + String configHome = System.getProperty("config.home"); + PropertiesFactoryBean bean = new PropertiesFactoryBean(); + bean.setLocation(new FileSystemResource(configHome + "/elasticsearch.yml")); + return bean; + } + @Bean(name = "serviceDistributionArtifactsBuilder") + public ServiceDistributionArtifactsBuilder serviceDistributionArtifactsBuilder() { + return new ServiceDistributionArtifactsBuilder(); + } + + +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/configuration/InternalToolConfiguration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/configuration/InternalToolConfiguration.java new file mode 100644 index 0000000000..fb763189bf --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/configuration/InternalToolConfiguration.java @@ -0,0 +1,28 @@ +package org.openecomp.sdc.asdctool.configuration; + +import org.openecomp.sdc.asdctool.impl.internal.tool.DeleteComponentHandler; +import org.openecomp.sdc.be.config.CatalogModelSpringConfig; +import org.openecomp.sdc.be.dao.config.DAOSpringConfig; +import org.springframework.beans.factory.config.PropertiesFactoryBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.core.io.FileSystemResource; + +@Configuration +@Import({DAOSpringConfig.class, CatalogModelSpringConfig.class}) +public class InternalToolConfiguration { + @Bean(name = "elasticsearchConfig") + public PropertiesFactoryBean mapper() { + String configHome = System.getProperty("config.home"); + PropertiesFactoryBean bean = new PropertiesFactoryBean(); + bean.setLocation(new FileSystemResource(configHome + "/elasticsearch.yml")); + return bean; + } + + @Bean + public DeleteComponentHandler deleteComponentHandler() { + return new DeleteComponentHandler(); + } + +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/configuration/VrfObjectFixConfiguration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/configuration/VrfObjectFixConfiguration.java new file mode 100644 index 0000000000..0dd078d863 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/configuration/VrfObjectFixConfiguration.java @@ -0,0 +1,36 @@ +package org.openecomp.sdc.asdctool.configuration; + +import org.openecomp.sdc.asdctool.impl.VrfObjectFixHandler; +import org.openecomp.sdc.be.dao.DAOTitanStrategy; +import org.openecomp.sdc.be.dao.TitanClientStrategy; +import org.openecomp.sdc.be.dao.jsongraph.TitanDao; +import org.openecomp.sdc.be.dao.titan.TitanGraphClient; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; + +@Configuration +public class VrfObjectFixConfiguration { + + @Bean(name = "titan-dao") + public TitanDao titanDao(@Qualifier("titan-client") TitanGraphClient titanClient){ + return new TitanDao(titanClient); + } + + @Bean(name = "titan-client") + @Primary + public TitanGraphClient titanClient(@Qualifier("dao-client-strategy") TitanClientStrategy titanClientStrategy) { + return new TitanGraphClient(titanClientStrategy); + } + + @Bean(name ="dao-client-strategy") + public TitanClientStrategy titanClientStrategy() { + return new DAOTitanStrategy(); + } + + @Bean + public VrfObjectFixHandler vrfObjectFixHandler(@Qualifier("titan-dao") TitanDao titanDao){ + return new VrfObjectFixHandler(titanDao); + } +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/configuration/mocks/es/ESCatalogDAOMock.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/configuration/mocks/es/ESCatalogDAOMock.java index f4551a0264..302d20fea7 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/configuration/mocks/es/ESCatalogDAOMock.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/configuration/mocks/es/ESCatalogDAOMock.java @@ -1,12 +1,11 @@ package org.openecomp.sdc.asdctool.configuration.mocks.es; -import java.util.List; - +import fj.data.Either; import org.openecomp.sdc.be.dao.api.ICatalogDAO; import org.openecomp.sdc.be.dao.api.ResourceUploadStatus; import org.openecomp.sdc.be.resources.data.ESArtifactData; -import fj.data.Either; +import java.util.List; public class ESCatalogDAOMock implements ICatalogDAO { diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/enums/SchemaZipFileEnum.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/enums/SchemaZipFileEnum.java index 6f4c602c2c..42c5290ff8 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/enums/SchemaZipFileEnum.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/enums/SchemaZipFileEnum.java @@ -1,68 +1,57 @@ package org.openecomp.sdc.asdctool.enums; -public enum SchemaZipFileEnum { - - DATA("data", "data-types", "dataTypes", "data_types", new String[]{}), - GROUPS("groups", "group-types", "groupTypes", "group_types", new String[]{"data.yml"}), - POLICIES("policies", "policy-types", "policyTypes","policy_types", new String[]{"data.yml"}), - RELATIONSHIPS("relationships","relationship-types","relationshipTypes", "relationship_types", new String[]{"capabilities.yml", "data.yml", "interfaces.yml"}), - ARTIFACTS("artifacts", "artifact-types", "artifactTypes", "artifact_types", new String[]{"data.yml"}), - CAPABILITIES("capabilities", "capability-types", "capabilityTypes", "capability_types" ,new String[]{"data.yml"}), - INTERFACES("interfaces", "interface-lifecycle-types", "interfaceLifecycleTypes", "interface_types", new String[]{"data.yml"}); - - private String fileName; - private String sourceFolderName; - private String sourceFileName; - private String collectionTitle; - private String[] importFileList; - - private SchemaZipFileEnum(String fileName, String sourceFolderName, String sourceFileName, String collectionTitle, - String[] importFileList) { - this.fileName = fileName; - this.sourceFolderName = sourceFolderName; - this.sourceFileName = sourceFileName; - this.collectionTitle = collectionTitle; - this.importFileList = importFileList; - } - - public String getFileName() { - return fileName; - } - - public void setFileName(String fileName) { - this.fileName = fileName; - } - - public String getSourceFolderName() { - return sourceFolderName; - } +import static org.openecomp.sdc.asdctool.enums.SchemaZipFileEnum.SchemaZipConstants.*; - public void setSourceFolderName(String sourceFolderName) { - this.sourceFolderName = sourceFolderName; - } - - public String getSourceFileName() { - return sourceFileName; - } - - public void setSourceFileName(String sourceFileName) { - this.sourceFileName = sourceFileName; - } - - public String getCollectionTitle() { - return collectionTitle; - } - - public void setCollectionTitle(String collectionTitle) { - this.collectionTitle = collectionTitle; - } +public enum SchemaZipFileEnum { - public String[] getImportFileList() { - return importFileList; - } + DATA("data", "data-types", "dataTypes", "data_types", EMPTY_IMPORT_LIST), + GROUPS("groups", "group-types", "groupTypes", "group_types", DATA_IMPORT_LIST), + POLICIES("policies", "policy-types", "policyTypes", "policy_types", DATA_IMPORT_LIST), + ANNOTATIONS("annotations", "annotation-types", "annotationTypes", "annotation_types", DATA_IMPORT_LIST), + RELATIONSHIPS("relationships", "relationship-types", "relationshipTypes", "relationship_types", RELATIONSHIPS_TYPES_IMPORT_LIST), + ARTIFACTS("artifacts", "artifact-types", "artifactTypes", "artifact_types", DATA_IMPORT_LIST), + CAPABILITIES("capabilities", "capability-types", "capabilityTypes", "capability_types", DATA_IMPORT_LIST), + INTERFACES("interfaces", "interface-lifecycle-types", "interfaceLifecycleTypes", "interface_types", DATA_IMPORT_LIST); + + private String fileName; + private String sourceFolderName; + private String sourceFileName; + private String collectionTitle; + private String[] importFileList; + + SchemaZipFileEnum(String fileName, String sourceFolderName, String sourceFileName, String collectionTitle, + String[] importFileList) { + this.fileName = fileName; + this.sourceFolderName = sourceFolderName; + this.sourceFileName = sourceFileName; + this.collectionTitle = collectionTitle; + this.importFileList = importFileList; + } + + public String getFileName() { + return fileName; + } + + public String getSourceFolderName() { + return sourceFolderName; + } + + public String getSourceFileName() { + return sourceFileName; + } + + public String getCollectionTitle() { + return collectionTitle; + } + + public String[] getImportFileList() { + return importFileList; + } + + static class SchemaZipConstants { + static final String [] EMPTY_IMPORT_LIST = new String[]{}; + static final String [] DATA_IMPORT_LIST = new String[]{"data.yml"}; + static final String [] RELATIONSHIPS_TYPES_IMPORT_LIST = new String[]{"capabilities.yml", "data.yml", "interfaces.yml"}; + } - public void setImportFileList(String[] importFileList) { - this.importFileList = importFileList; - } - } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/ArtifactUuidFix.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/ArtifactUuidFix.java index fa02377623..a4ee8d8a2c 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/ArtifactUuidFix.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/ArtifactUuidFix.java @@ -1,20 +1,9 @@ package org.openecomp.sdc.asdctool.impl; -import java.io.BufferedWriter; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.Writer; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.stream.Collectors; - +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; +import fj.data.Either; import org.openecomp.sdc.asdctool.impl.validator.utils.VfModuleArtifactPayloadEx; import org.openecomp.sdc.be.components.distribution.engine.VfModuleArtifactPayload; import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao; @@ -35,16 +24,7 @@ import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; import org.openecomp.sdc.be.impl.ComponentsUtils; -import org.openecomp.sdc.be.model.ArtifactDefinition; -import org.openecomp.sdc.be.model.Component; -import org.openecomp.sdc.be.model.ComponentInstance; -import org.openecomp.sdc.be.model.ComponentParametersView; -import org.openecomp.sdc.be.model.DistributionStatusEnum; -import org.openecomp.sdc.be.model.GroupDefinition; -import org.openecomp.sdc.be.model.GroupInstance; -import org.openecomp.sdc.be.model.LifecycleStateEnum; -import org.openecomp.sdc.be.model.Resource; -import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.*; import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate; import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement; import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade; @@ -60,17 +40,14 @@ import org.openecomp.sdc.be.tosca.ToscaRepresentation; import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum; import org.openecomp.sdc.common.api.ArtifactTypeEnum; import org.openecomp.sdc.common.api.Constants; +import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.common.util.GeneralUtility; import org.openecomp.sdc.exception.ResponseFormat; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonArray; - -import fj.data.Either; +import java.io.*; +import java.util.*; +import java.util.stream.Collectors; @org.springframework.stereotype.Component("artifactUuidFix") public class ArtifactUuidFix { @@ -89,7 +66,7 @@ public class ArtifactUuidFix { @Autowired private CsarUtils csarUtils; - private static Logger log = LoggerFactory.getLogger(ArtifactUuidFix.class.getName()); + private static Logger log = Logger.getLogger(ArtifactUuidFix.class.getName()); public boolean doFix(String fixComponent, String runMode) { List vfLst = new ArrayList<>(); diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/DataMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/DataMigration.java index 8b6898d3b0..cad7a05cb2 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/DataMigration.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/DataMigration.java @@ -7,9 +7,9 @@ * 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. @@ -20,27 +20,11 @@ package org.openecomp.sdc.asdctool.impl; -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.PrintWriter; -import java.lang.reflect.Type; -import java.net.MalformedURLException; -import java.net.URISyntaxException; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.EnumMap; -import java.util.Map; -import java.util.Map.Entry; -import java.util.TimeZone; +import com.carrotsearch.hppc.cursors.ObjectCursor; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import fj.data.Either; import org.apache.commons.lang.SystemUtils; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.cluster.metadata.IndexMetaData; @@ -50,6 +34,17 @@ import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; +import org.openecomp.sdc.be.auditing.api.AuditEventFactory; +import org.openecomp.sdc.be.auditing.impl.AuditAuthRequestEventFactory; +import org.openecomp.sdc.be.auditing.impl.AuditConsumerEventFactory; +import org.openecomp.sdc.be.auditing.impl.AuditGetUebClusterEventFactory; +import org.openecomp.sdc.be.auditing.impl.category.AuditCategoryEventFactory; +import org.openecomp.sdc.be.auditing.impl.category.AuditGetCategoryHierarchyEventFactory; +import org.openecomp.sdc.be.auditing.impl.distribution.*; +import org.openecomp.sdc.be.auditing.impl.resourceadmin.AuditResourceAdminEventMigrationFactory; +import org.openecomp.sdc.be.auditing.impl.usersadmin.AuditGetUsersListEventFactory; +import org.openecomp.sdc.be.auditing.impl.usersadmin.AuditUserAccessEventFactory; +import org.openecomp.sdc.be.auditing.impl.usersadmin.AuditUserAdminEventFactory; import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao; import org.openecomp.sdc.be.dao.cassandra.AuditCassandraDao; import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus; @@ -58,769 +53,761 @@ import org.openecomp.sdc.be.dao.es.ElasticSearchClient; import org.openecomp.sdc.be.resources.data.ESArtifactData; import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; import org.openecomp.sdc.be.resources.data.auditing.AuditingGenericEvent; -import org.openecomp.sdc.be.resources.data.auditing.AuditingGetUebClusterEvent; import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants; -import org.openecomp.sdc.be.resources.data.auditing.AuthEvent; -import org.openecomp.sdc.be.resources.data.auditing.CategoryEvent; -import org.openecomp.sdc.be.resources.data.auditing.ConsumerEvent; -import org.openecomp.sdc.be.resources.data.auditing.DistributionDeployEvent; -import org.openecomp.sdc.be.resources.data.auditing.DistributionDownloadEvent; -import org.openecomp.sdc.be.resources.data.auditing.DistributionEngineEvent; -import org.openecomp.sdc.be.resources.data.auditing.DistributionNotificationEvent; -import org.openecomp.sdc.be.resources.data.auditing.DistributionStatusEvent; -import org.openecomp.sdc.be.resources.data.auditing.GetCategoryHierarchyEvent; -import org.openecomp.sdc.be.resources.data.auditing.GetUsersListEvent; -import org.openecomp.sdc.be.resources.data.auditing.ResourceAdminEvent; -import org.openecomp.sdc.be.resources.data.auditing.UserAccessEvent; -import org.openecomp.sdc.be.resources.data.auditing.UserAdminEvent; -import org.openecomp.sdc.common.datastructure.AuditingFieldsKeysEnum; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.be.resources.data.auditing.model.*; +import org.openecomp.sdc.common.datastructure.AuditingFieldsKey; +import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.beans.factory.annotation.Autowired; -import com.carrotsearch.hppc.cursors.ObjectCursor; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; - -import fj.data.Either; +import java.io.*; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.EnumMap; +import java.util.Map; /** * Created by mlando on 5/16/2016. */ public class DataMigration { - private Gson gson = new Gson(); - - private ObjectMapper jsonMapper = new ObjectMapper(); - - private static Logger log = LoggerFactory.getLogger(DataMigration.class.getName()); + private ObjectMapper jsonMapper = new ObjectMapper(); - private ElasticSearchClient elasticSearchClient; + private static Logger log = Logger.getLogger(DataMigration.class.getName()); - @Autowired - protected AuditCassandraDao auditCassandraDao; - @Autowired - protected ArtifactCassandraDao artifactCassandraDao; - - private static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS z"; - private SimpleDateFormat simpleDateFormat; + private ElasticSearchClient elasticSearchClient; + @Autowired + private AuditCassandraDao auditCassandraDao; + @Autowired + private ArtifactCassandraDao artifactCassandraDao; /** - * the method exports and imports the records from ES to cassandra the flow - * will check to see if the files are not empty if the files are not empty - * the export will be skiped and the flow will use the existing files. the - * flow will check if the tables in cassandra are empty, if the tables are - * not empty the proces will stop and exit. if the tables are empty the - * method will import the records from the files. in case of a fail the flow - * will exit and clear all the Cassandra tables. - * - * @param appConfigDir - * the location of the dir in wich the output files will be - * stored - * @param exportFromEs - * should the es be exported again and overwrite the old export - * @param importToCassandra - * should we import the data into cassandra - * @return true in case the operation was successful. - */ - public boolean migrateDataEsToCassandra(String appConfigDir, boolean exportFromEs, boolean importToCassandra) { - initFormater(); - if (!initEsClient()) { - return false; - } - Map files = createOutPutFiles(appConfigDir, exportFromEs); - if (files == null) { - return false; - } - if (exportFromEs && filesEmpty(files)) { - Map printerWritersMap = createWriters(files); - if (printerWritersMap == null) { - return false; - } - try { - ImmutableOpenMap indexData = getIndexData(); - for (ObjectCursor key : indexData.keys()) { - if (("resources".equalsIgnoreCase(key.value) && !exportArtifacts(key.value, printerWritersMap)) - || (key.value.startsWith("auditingevents") && !exportAudit(key.value, printerWritersMap))) { + * the method exports and imports the records from ES to cassandra the flow + * will check to see if the files are not empty if the files are not empty + * the export will be skiped and the flow will use the existing files. the + * flow will check if the tables in cassandra are empty, if the tables are + * not empty the proces will stop and exit. if the tables are empty the + * method will import the records from the files. in case of a fail the flow + * will exit and clear all the Cassandra tables. + * + * @param appConfigDir + * the location of the dir in wich the output files will be + * stored + * @param exportFromEs + * should the es be exported again and overwrite the old export + * @param importToCassandra + * should we import the data into cassandra + * @return true in case the operation was successful. + */ + public boolean migrateDataESToCassndra(String appConfigDir, boolean exportFromEs, boolean importToCassandra) { + if (!initEsClient()) { + return false; + } + Map files = createOutPutFiles(appConfigDir, exportFromEs); + if (files == null) { + return false; + } + if (exportFromEs && filesEmpty(files)) { + Map printerWritersMap = createWriters(files); + if (printerWritersMap == null) { + return false; + } + try { + ImmutableOpenMap indexData = getIndexData(); + for (ObjectCursor key : indexData.keys()) { + if (("resources".equalsIgnoreCase(key.value) || key.value.startsWith("auditingevents")) + && !exportArtifacts(key.value, printerWritersMap)) { return false; } - } - } finally { - if (elasticSearchClient != null) { - elasticSearchClient.close(); - } - for (PrintWriter writer : printerWritersMap.values()) { - writer.close(); - } - } - } - return !importToCassandra || importToCassndra(files); - } - - private void initFormater() { - simpleDateFormat = new SimpleDateFormat(DATE_FORMAT_PATTERN); - simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - } - - private boolean initEsClient() { - String configHome = System.getProperty("config.home"); - URL url; - Settings settings; - try { - if (SystemUtils.IS_OS_WINDOWS) { - url = new URL("file:///" + configHome + "/elasticsearch.yml"); - } else { - url = new URL("file:" + configHome + "/elasticsearch.yml"); - } - log.debug("URL {}", url); - settings = Settings.settingsBuilder().loadFromPath(Paths.get(url.toURI())).build(); - } catch (MalformedURLException | URISyntaxException e1) { - log.error("Failed to create URL in order to load elasticsearch yml", e1); - return true; - } - - this.elasticSearchClient = new ElasticSearchClient(); - this.elasticSearchClient.setClusterName(settings.get("cluster.name")); - this.elasticSearchClient.setLocal(settings.get("elasticSearch.local")); - this.elasticSearchClient.setTransportClient(settings.get("elasticSearch.transportclient")); - try { - elasticSearchClient.initialize(); - } catch (URISyntaxException e) { - log.error("Failed to initialize elasticSearchClient", e); - return false; - } - return true; - } - - /** - * the method clears all the cassandra tables. - */ - private void truncateCassandraTable() { - log.info("import failed. truncating Cassandra tables."); - artifactCassandraDao.deleteAllArtifacts(); - auditCassandraDao.deleteAllAudit(); - } - - /** - * the method imports the records from the files into cassandra. - * - * @param files - * a map of files holding - * @return true if the operation was successful - */ - private boolean importToCassndra(Map files) { - log.info("starting to import date into Cassandra."); - if (!validtaTablsNotEmpty(files)) { - return true; - } - for (Table table : files.keySet()) { - log.info("importing recordes into {}", table.getTableDescription().getTableName()); - if (!handleImport(files, table)) { - truncateCassandraTable(); - return false; - } - } - log.info("finished to import date into Cassandra."); - return true; - } - - private boolean validtaTablsNotEmpty(Map files) { - for (Table table : files.keySet()) { - Either isTableEmptyRes = checkIfTableIsEmpty(table); - if (isTableEmptyRes.isRight() || !isTableEmptyRes.left().value()) { - log.error("Cassandra table {} is not empty operation aborted.", - table.getTableDescription().getTableName()); - return false; - } - } - return true; - } - - /** - * the method retrieves the fields from the given map and praprs them for - * storage as an audit according to the table name - * - * @param map - * the map from which we will retrive the fields enum values - * @param table - * the table we are going to store the record in. - * @return a enummap representing the audit record that is going to be - * created. - */ - private EnumMap createAuditMap(Map map, Table table) { - EnumMap auditingFields = new EnumMap<>(AuditingFieldsKeysEnum.class); - switch (table) { - case USER_ADMIN_EVENT: - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP, map.get("TIMESTAMP")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_REQUEST_ID, map.get("REQUEST_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_SERVICE_INSTANCE_ID, map.get("SERVICE_INSTANCE_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_ACTION, map.get("ACTION")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DESC, map.get("DESC")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_STATUS, map.get("STATUS")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_USER_AFTER, map.get("USER_AFTER")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_USER_BEFORE, map.get("USER_BEFORE")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_MODIFIER_UID, map.get("MODIFIER")); - break; - case USER_ACCESS_EVENT: - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP, map.get("TIMESTAMP")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_REQUEST_ID, map.get("REQUEST_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_SERVICE_INSTANCE_ID, map.get("SERVICE_INSTANCE_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_ACTION, map.get("ACTION")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DESC, map.get("DESC")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_STATUS, map.get("STATUS")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_USER_UID, map.get("USER")); - break; - case RESOURCE_ADMIN_EVENT: - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP, map.get("TIMESTAMP")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_REQUEST_ID, map.get("REQUEST_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_SERVICE_INSTANCE_ID, map.get("SERVICE_INSTANCE_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_INVARIANT_UUID, map.get("INVARIANT_UUID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_ACTION, map.get("ACTION")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DESC, map.get("DESC")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_STATUS, map.get("STATUS")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_CURR_VERSION, map.get("CURR_VERSION")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_CURR_STATE, map.get("CURR_STATE")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_ID, map.get("DID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_MODIFIER_UID, map.get("MODIFIER")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_PREV_VERSION, map.get("PREV_VERSION")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_PREV_STATE, map.get("PREV_STATE")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_NAME, map.get("RESOURCE_NAME")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_TYPE, map.get("RESOURCE_TYPE")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_DPREV_STATUS, map.get("DPREV_STATUS")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_DCURR_STATUS, map.get("DCURR_STATUS")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_TOSCA_NODE_TYPE, map.get("TOSCA_NODE_TYPE")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_COMMENT, map.get("COMMENT")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_ARTIFACT_DATA, map.get("ARTIFACT_DATA")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_PREV_ARTIFACT_UUID, map.get("PREV_ARTIFACT_UUID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_CURR_ARTIFACT_UUID, map.get("CURR_ARTIFACT_UUID")); - break; - case DISTRIBUTION_DOWNLOAD_EVENT: - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP, map.get("TIMESTAMP")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_REQUEST_ID, map.get("REQUEST_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_SERVICE_INSTANCE_ID, map.get("SERVICE_INSTANCE_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_ACTION, map.get("ACTION")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DESC, map.get("DESC")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_STATUS, map.get("STATUS")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_RESOURCE_URL, map.get("RESOURCE_URL")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_CONSUMER_ID, map.get("CONSUMER_ID")); - break; - case DISTRIBUTION_ENGINE_EVENT: - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP, map.get("TIMESTAMP")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_REQUEST_ID, map.get("REQUEST_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_SERVICE_INSTANCE_ID, map.get("SERVICE_INSTANCE_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_ACTION, map.get("ACTION")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DESC, map.get("DESC")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_STATUS, map.get("STATUS")); - if (map.get("TOPIC_NAME") != null) { - if (map.get("TOPIC_NAME").contains("-STATUS-")) { - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_STATUS_TOPIC_NAME, - map.get("TOPIC_NAME")); - } else { - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_NOTIFICATION_TOPIC_NAME, - map.get("TOPIC_NAME")); + } + } finally { + if (elasticSearchClient != null) { + elasticSearchClient.close(); + } + for (PrintWriter writer : printerWritersMap.values()) { + writer.close(); + } + } + } + if (importToCassandra && !importToCassndra(files)) { + return false; + } + + return true; + } + + private boolean initEsClient() { + String configHome = System.getProperty("config.home"); + URL url = null; + Settings settings = null; + try { + if (SystemUtils.IS_OS_WINDOWS) { + url = new URL("file:///" + configHome + "/elasticsearch.yml"); + } else { + url = new URL("file:" + configHome + "/elasticsearch.yml"); + } + log.debug("URL {}", url); + settings = Settings.settingsBuilder().loadFromPath(Paths.get(url.toURI())).build(); + } catch (MalformedURLException | URISyntaxException e1) { + log.error("Failed to create URL in order to load elasticsearch yml", e1); + return true; + } + + this.elasticSearchClient = new ElasticSearchClient(); + this.elasticSearchClient.setClusterName(settings.get("cluster.name")); + this.elasticSearchClient.setLocal(settings.get("elasticSearch.local")); + this.elasticSearchClient.setTransportClient(settings.get("elasticSearch.transportclient")); + try { + elasticSearchClient.initialize(); + } catch (URISyntaxException e) { + e.printStackTrace(); + return false; + } + return true; + } + + /** + * the method clears all the cassandra tables + */ + private void truncateCassandraTable() { + log.info("import failed. truncating Cassandra tables."); + artifactCassandraDao.deleteAllArtifacts(); + auditCassandraDao.deleteAllAudit(); + } + + /** + * the method imports the records from the files into cassandra + * + * @param files + * a map of files holding + * @return true if the operation was successful + */ + private boolean importToCassndra(Map files) { + log.info("starting to import date into Cassandra."); + if (!validtaTablsNotEmpty(files)) + return true; + for (Table table : files.keySet()) { + log.info("importing recordes into {}", table.getTableDescription().getTableName()); + if (!handleImport(files, table)) { + truncateCassandraTable(); + return false; + } + } + log.info("finished to import date into Cassandra."); + return true; + } + + private boolean validtaTablsNotEmpty(Map files) { + for (Table table : files.keySet()) { + Either isTableEmptyRes = checkIfTableIsEmpty(table); + if (isTableEmptyRes.isRight() || !isTableEmptyRes.left().value()) { + log.error("Cassandra table {} is not empty operation aborted.", + table.getTableDescription().getTableName()); + return false; + } + } + return true; + } + + /** + * the method retrieves the fields from the given map and generates + * corresponding audit event according to the table name + * + * @param map + * the map from which we will retrive the fields enum values + * @param table + * the table we are going to store the record in. + * @return an AuditingGenericEvent event representing the audit record that is going to be + * created. + */ + AuditingGenericEvent createAuditEvent(Map map, Table table) { + AuditEventFactory factory = null; + switch (table) { + case USER_ADMIN_EVENT: + factory = getAuditUserAdminEventFactory(map); + break; + case USER_ACCESS_EVENT: + factory = getAuditUserAccessEventFactory(map); + break; + case RESOURCE_ADMIN_EVENT: + factory = getAuditResourceAdminEventMigrationFactory(map); + break; + case DISTRIBUTION_DOWNLOAD_EVENT: + factory = getAuditDistributionDownloadEventFactory(map); + break; + case DISTRIBUTION_ENGINE_EVENT: + factory = getAuditDistributionEngineEventMigrationFactory(map); + break; + case DISTRIBUTION_NOTIFICATION_EVENT: + factory = getAuditDistributionNotificationEventFactory(map); + break; + case DISTRIBUTION_STATUS_EVENT: + factory = getAuditDistributionStatusEventFactory(map); + break; + case DISTRIBUTION_DEPLOY_EVENT: + factory = getAuditDistributionDeployEventFactory(map); + break; + case DISTRIBUTION_GET_UEB_CLUSTER_EVENT: + factory = getAuditGetUebClusterEventFactory(map); + break; + case AUTH_EVENT: + factory = getAuditAuthRequestEventFactory(map); + break; + case CONSUMER_EVENT: + factory = getAuditConsumerEventFactory(map); + break; + case CATEGORY_EVENT: + factory = getAuditCategoryEventFactory(map); + break; + case GET_USERS_LIST_EVENT: + factory = getAuditGetUsersListEventFactory(map); + break; + case GET_CATEGORY_HIERARCHY_EVENT: + factory = getAuditGetCategoryHierarchyEventFactory(map); + break; + default: + break; + } + return factory != null ? factory.getDbEvent() : null; + } + + private AuditEventFactory getAuditGetCategoryHierarchyEventFactory(Map map) { + return new AuditGetCategoryHierarchyEventFactory( + CommonAuditData.newBuilder() + .description(map.get(AuditingFieldsKey.AUDIT_DESC)) + .status(map.get(AuditingFieldsKey.AUDIT_STATUS)) + .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID)) + .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID)) + .build(), + map.get(AuditingFieldsKey.AUDIT_MODIFIER_UID), + map.get(AuditingFieldsKey.AUDIT_DETAILS), + map.get(AuditingFieldsKey.AUDIT_TIMESTAMP)); + } + + private AuditEventFactory getAuditGetUsersListEventFactory(Map map) { + return new AuditGetUsersListEventFactory( + CommonAuditData.newBuilder() + .description(map.get(AuditingFieldsKey.AUDIT_DESC)) + .status(map.get(AuditingFieldsKey.AUDIT_STATUS)) + .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID)) + .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID)) + .build(), + map.get(AuditingFieldsKey.AUDIT_MODIFIER_UID), + map.get(AuditingFieldsKey.AUDIT_USER_DETAILS), + map.get(AuditingFieldsKey.AUDIT_TIMESTAMP)); + } + + private AuditEventFactory getAuditCategoryEventFactory(Map map) { + return new AuditCategoryEventFactory( + AuditingActionEnum.fromName(map.get(AuditingFieldsKey.AUDIT_ACTION)), + CommonAuditData.newBuilder() + .description(map.get(AuditingFieldsKey.AUDIT_DESC)) + .status(map.get(AuditingFieldsKey.AUDIT_STATUS)) + .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID)) + .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID)) + .build(), + map.get(AuditingFieldsKey.AUDIT_MODIFIER_UID), + map.get(AuditingFieldsKey.AUDIT_CATEGORY_NAME), + map.get(AuditingFieldsKey.AUDIT_SUB_CATEGORY_NAME), + map.get(AuditingFieldsKey.AUDIT_GROUPING_NAME), + map.get(AuditingFieldsKey.AUDIT_RESOURCE_TYPE), + map.get(AuditingFieldsKey.AUDIT_TIMESTAMP)); + } + + private AuditEventFactory getAuditUserAccessEventFactory(Map map) { + return new AuditUserAccessEventFactory( + CommonAuditData.newBuilder() + .description(map.get(AuditingFieldsKey.AUDIT_DESC)) + .status(map.get(AuditingFieldsKey.AUDIT_STATUS)) + .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID)) + .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID)) + .build(), + map.get(AuditingFieldsKey.AUDIT_USER_UID), + map.get(AuditingFieldsKey.AUDIT_TIMESTAMP)); + } + + private AuditEventFactory getAuditUserAdminEventFactory(Map map) { + return new AuditUserAdminEventFactory( + AuditingActionEnum.fromName(map.get(AuditingFieldsKey.AUDIT_ACTION)), + CommonAuditData.newBuilder() + .description(map.get(AuditingFieldsKey.AUDIT_DESC)) + .status(map.get(AuditingFieldsKey.AUDIT_STATUS)) + .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID)) + .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID)) + .build(), + map.get(AuditingFieldsKey.AUDIT_MODIFIER_UID), + map.get(AuditingFieldsKey.AUDIT_USER_BEFORE), + map.get(AuditingFieldsKey.AUDIT_USER_AFTER), + map.get(AuditingFieldsKey.AUDIT_TIMESTAMP)); + } + + private AuditEventFactory getAuditConsumerEventFactory(Map map) { + return new AuditConsumerEventFactory( + AuditingActionEnum.fromName(map.get(AuditingFieldsKey.AUDIT_ACTION)), + CommonAuditData.newBuilder() + .description(map.get(AuditingFieldsKey.AUDIT_DESC)) + .status(map.get(AuditingFieldsKey.AUDIT_STATUS)) + .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID)) + .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID)) + .build(), + map.get(AuditingFieldsKey.AUDIT_MODIFIER_UID), + map.get(AuditingFieldsKey.AUDIT_ECOMP_USER), + map.get(AuditingFieldsKey.AUDIT_TIMESTAMP)); + } + + private AuditEventFactory getAuditAuthRequestEventFactory(Map map) { + return new AuditAuthRequestEventFactory( + CommonAuditData.newBuilder() + .description(map.get(AuditingFieldsKey.AUDIT_DESC)) + .status(map.get(AuditingFieldsKey.AUDIT_STATUS)) + .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID)) + .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID)) + .build(), + map.get(AuditingFieldsKey.AUDIT_USER_UID), + map.get(AuditingFieldsKey.AUDIT_AUTH_URL), + map.get(AuditingFieldsKey.AUDIT_AUTH_REALM), + map.get(AuditingFieldsKey.AUDIT_AUTH_STATUS), + map.get(AuditingFieldsKey.AUDIT_TIMESTAMP)); + } + + private AuditEventFactory getAuditGetUebClusterEventFactory(Map map) { + return new AuditGetUebClusterEventFactory( + CommonAuditData.newBuilder() + .description(map.get(AuditingFieldsKey.AUDIT_DESC)) + .status(map.get(AuditingFieldsKey.AUDIT_STATUS)) + .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID)) + .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID)) + .build(), + map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_CONSUMER_ID), + map.get(AuditingFieldsKey.AUDIT_TIMESTAMP)); + } + + private AuditEventFactory getAuditResourceAdminEventMigrationFactory(Map map) { + return new AuditResourceAdminEventMigrationFactory( + AuditingActionEnum.fromName(map.get(AuditingFieldsKey.AUDIT_ACTION)), + CommonAuditData.newBuilder() + .description(map.get(AuditingFieldsKey.AUDIT_DESC)) + .status(map.get(AuditingFieldsKey.AUDIT_STATUS)) + .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID)) + .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID)) + .build(), + new ResourceCommonInfo(map.get(AuditingFieldsKey.AUDIT_RESOURCE_NAME), + map.get(AuditingFieldsKey.AUDIT_RESOURCE_TYPE)), + ResourceVersionInfo.newBuilder() + .artifactUuid(map.get(AuditingFieldsKey.AUDIT_PREV_ARTIFACT_UUID)) + .state(map.get(AuditingFieldsKey.AUDIT_RESOURCE_PREV_STATE)) + .version(map.get(AuditingFieldsKey.AUDIT_RESOURCE_PREV_VERSION)) + .distributionStatus(map.get(AuditingFieldsKey.AUDIT_RESOURCE_DPREV_STATUS)) + .build(), + ResourceVersionInfo.newBuilder() + .artifactUuid(map.get(AuditingFieldsKey.AUDIT_CURR_ARTIFACT_UUID)) + .state(map.get(AuditingFieldsKey.AUDIT_RESOURCE_CURR_STATE)) + .version(map.get(AuditingFieldsKey.AUDIT_RESOURCE_CURR_VERSION)) + .distributionStatus(map.get(AuditingFieldsKey.AUDIT_RESOURCE_DCURR_STATUS)) + .build(), + map.get(AuditingFieldsKey.AUDIT_INVARIANT_UUID), + map.get(AuditingFieldsKey.AUDIT_MODIFIER_UID), + map.get(AuditingFieldsKey.AUDIT_ARTIFACT_DATA), + map.get(AuditingFieldsKey.AUDIT_RESOURCE_COMMENT), + map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_ID), + map.get(AuditingFieldsKey.AUDIT_RESOURCE_TOSCA_NODE_TYPE), + map.get(AuditingFieldsKey.AUDIT_TIMESTAMP)); + } + + private AuditEventFactory getAuditDistributionDownloadEventFactory(Map map) { + return new AuditDistributionDownloadEventFactory( + CommonAuditData.newBuilder() + .description(map.get(AuditingFieldsKey.AUDIT_DESC)) + .status(map.get(AuditingFieldsKey.AUDIT_STATUS)) + .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID)) + .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID)) + .build(), + new DistributionData(map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_CONSUMER_ID), + map.get(AuditingFieldsKey.AUDIT_RESOURCE_URL)), + map.get(AuditingFieldsKey.AUDIT_TIMESTAMP)); + } + + private AuditEventFactory getAuditDistributionEngineEventMigrationFactory(Map map) { + return new AuditDistributionEngineEventMigrationFactory( + AuditingActionEnum.fromName(map.get(AuditingFieldsKey.AUDIT_ACTION)), + CommonAuditData.newBuilder() + .description(map.get(AuditingFieldsKey.AUDIT_DESC)) + .status(map.get(AuditingFieldsKey.AUDIT_STATUS)) + .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID)) + .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID)) + .build(), + DistributionTopicData.newBuilder() + .notificationTopic(map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_NOTIFICATION_TOPIC_NAME)) + .statusTopic(map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_STATUS_TOPIC_NAME)) + .build(), + map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_CONSUMER_ID), + map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_API_KEY), + map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_ENVRIONMENT_NAME), + map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_ROLE), + map.get(AuditingFieldsKey.AUDIT_TIMESTAMP)); + } + + private AuditEventFactory getAuditDistributionDeployEventFactory(Map map) { + return new AuditDistributionDeployEventFactory( + CommonAuditData.newBuilder() + .description(map.get(AuditingFieldsKey.AUDIT_DESC)) + .status(map.get(AuditingFieldsKey.AUDIT_STATUS)) + .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID)) + .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID)) + .build(), + new ResourceCommonInfo(map.get(AuditingFieldsKey.AUDIT_RESOURCE_NAME), + map.get(AuditingFieldsKey.AUDIT_RESOURCE_TYPE)), + map.get(AuditingFieldsKey.AUDIT_RESOURCE_CURR_VERSION), + map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_ID), + map.get(AuditingFieldsKey.AUDIT_MODIFIER_UID), + map.get(AuditingFieldsKey.AUDIT_TIMESTAMP)); + } + + private AuditEventFactory getAuditDistributionStatusEventFactory(Map map) { + return new AuditDistributionStatusEventFactory( + CommonAuditData.newBuilder() + .description(map.get(AuditingFieldsKey.AUDIT_DESC)) + .status(map.get(AuditingFieldsKey.AUDIT_STATUS)) + .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID)) + .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID)) + .build(), + new DistributionData(map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_CONSUMER_ID), + map.get(AuditingFieldsKey.AUDIT_RESOURCE_URL)), + map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_ID), + map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_TOPIC_NAME), + map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_STATUS_TIME), + map.get(AuditingFieldsKey.AUDIT_TIMESTAMP)); + } + + private AuditEventFactory getAuditDistributionNotificationEventFactory(Map map) { + return new AuditDistributionNotificationEventFactory( + CommonAuditData.newBuilder() + .description(map.get(AuditingFieldsKey.AUDIT_DESC)) + .status(map.get(AuditingFieldsKey.AUDIT_STATUS)) + .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID)) + .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID)) + .build(), + new ResourceCommonInfo(map.get(AuditingFieldsKey.AUDIT_RESOURCE_NAME), + map.get(AuditingFieldsKey.AUDIT_RESOURCE_TYPE)), + ResourceVersionInfo.newBuilder() + .state(map.get(AuditingFieldsKey.AUDIT_RESOURCE_CURR_STATE)) + .version(map.get(AuditingFieldsKey.AUDIT_RESOURCE_CURR_VERSION)) + .build(), + map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_ID), + map.get(AuditingFieldsKey.AUDIT_MODIFIER_UID), + map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_TOPIC_NAME), + new OperationalEnvAuditData(map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_ENVIRONMENT_ID), + map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_VNF_WORKLOAD_CONTEXT), + map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_TENANT)), + map.get(AuditingFieldsKey.AUDIT_TIMESTAMP)); + } + + + + /** + * the method reads the content of the file intended for a given table, and + * sores them in cassandra + * + * @param files + * a map of files from which the recordes will be retrieved. + * @param table + * the name of the table we want to look up in the files and sore + * in Cassandra // * @param store the function to call when + * storing recordes in cassndra + * @return true if the operation was successful + */ + private boolean handleImport(Map files, Table table) { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(files.get(table))); + String line = null; + while ((line = br.readLine()) != null) { + CassandraOperationStatus res = CassandraOperationStatus.GENERAL_ERROR; + if (Table.ARTIFACT.equals(table)) { + res = artifactCassandraDao.saveArtifact(jsonMapper.readValue(line, ESArtifactData.class)); + } + else { + AuditingGenericEvent recordForCassandra = createAuditRecordForCassandra(line, table); + if (recordForCassandra != null) { + res = auditCassandraDao.saveRecord(recordForCassandra); } - } else { - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_STATUS_TOPIC_NAME, - map.get("DSTATUS_TOPIC")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_NOTIFICATION_TOPIC_NAME, - map.get("DNOTIF_TOPIC")); - } - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_TOPIC_NAME, map.get("TOPIC_NAME")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_ROLE, map.get("ROLE")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_API_KEY, map.get("API_KEY")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_ENVRIONMENT_NAME, map.get("D_ENV")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_CONSUMER_ID, map.get("CONSUMER_ID")); - break; - case DISTRIBUTION_NOTIFICATION_EVENT: - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP, map.get("TIMESTAMP")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_REQUEST_ID, map.get("REQUEST_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_SERVICE_INSTANCE_ID, map.get("SERVICE_INSTANCE_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_ACTION, map.get("ACTION")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DESC, map.get("DESC")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_STATUS, map.get("STATUS")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_CURR_STATE, map.get("CURR_STATE")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_CURR_VERSION, map.get("CURR_VERSION")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_ID, map.get("DID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_MODIFIER_UID, map.get("MODIFIER")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_NAME, map.get("RESOURCE_NAME")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_TYPE, map.get("RESOURCE_TYPE")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_TOPIC_NAME, map.get("TOPIC_NAME")); - break; - case DISTRIBUTION_STATUS_EVENT: - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP, map.get("TIMESTAMP")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_REQUEST_ID, map.get("REQUEST_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_SERVICE_INSTANCE_ID, map.get("SERVICE_INSTANCE_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_ACTION, map.get("ACTION")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DESC, map.get("DESC")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_STATUS, map.get("STATUS")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_RESOURCE_URL, map.get("RESOURCE_URL")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_ID, map.get("DID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_TOPIC_NAME, map.get("TOPIC_NAME")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_CONSUMER_ID, map.get("CONSUMER_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_STATUS_TIME, map.get("STATUS_TIME")); - break; - case DISTRIBUTION_DEPLOY_EVENT: - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP, map.get("TIMESTAMP")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_REQUEST_ID, map.get("REQUEST_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_SERVICE_INSTANCE_ID, map.get("SERVICE_INSTANCE_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_ACTION, map.get("ACTION")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DESC, map.get("DESC")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_STATUS, map.get("STATUS")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_ID, map.get("DID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_NAME, map.get("RESOURCE_NAME")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_TYPE, map.get("RESOURCE_TYPE")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_MODIFIER_UID, map.get("MODIFIER")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_CURR_VERSION, map.get("CURR_VERSION")); - break; - case DISTRIBUTION_GET_UEB_CLUSTER_EVENT: - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP, map.get("TIMESTAMP")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_REQUEST_ID, map.get("REQUEST_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_SERVICE_INSTANCE_ID, map.get("SERVICE_INSTANCE_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_ACTION, map.get("ACTION")); - if (map.get("STATUS_DESC") != null) { - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DESC, map.get("STATUS_DESC")); - } else { - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DESC, map.get("DESC")); - } - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_STATUS, map.get("STATUS")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_CONSUMER_ID, map.get("CONSUMER_ID")); - break; - case AUTH_EVENT: - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP, map.get("TIMESTAMP")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_ACTION, map.get("ACTION")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DESC, map.get("DESC")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_REQUEST_ID, map.get("REQUEST_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_STATUS, map.get("STATUS")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_AUTH_USER, map.get("USER")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_AUTH_URL, map.get("URL")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_AUTH_STATUS, map.get("AUTH_STATUS")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_AUTH_REALM, map.get("REALM")); - break; - case CONSUMER_EVENT: - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP, map.get("TIMESTAMP")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_ACTION, map.get("ACTION")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DESC, map.get("DESC")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_STATUS, map.get("STATUS")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_MODIFIER_UID, map.get("MODIFIER")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_REQUEST_ID, map.get("REQUEST_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_ECOMP_USER, map.get("ECOMP_USER")); - break; - case CATEGORY_EVENT: - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP, map.get("TIMESTAMP")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_ACTION, map.get("ACTION")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DESC, map.get("DESC")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_STATUS, map.get("STATUS")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_MODIFIER_UID, map.get("MODIFIER")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_REQUEST_ID, map.get("REQUEST_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_SERVICE_INSTANCE_ID, map.get("SERVICE_INSTANCE_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_CATEGORY_NAME, map.get("CATEGORY_NAME")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_SUB_CATEGORY_NAME, map.get("SUB_CATEGORY_NAME")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_GROUPING_NAME, map.get("GROUPING_NAME")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_RESOURCE_TYPE, map.get("RESOURCE_TYPE")); - break; - case GET_USERS_LIST_EVENT: - case GET_CATEGORY_HIERARCHY_EVENT: - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP, map.get("TIMESTAMP")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_ACTION, map.get("ACTION")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DESC, map.get("DESC")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_STATUS, map.get("STATUS")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_MODIFIER_UID, map.get("MODIFIER")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_REQUEST_ID, map.get("REQUEST_ID")); - auditingFields.put(AuditingFieldsKeysEnum.AUDIT_DETAILS, map.get("DETAILS")); - break; - default: - auditingFields = null; - break; - } - return auditingFields; - } - - /** - * the method reads the content of the file intended for a given table, and - * sores them in cassandra - * - * @param files - * a map of files from which the recordes will be retrieved. - * @param table - * the name of the table we want to look up in the files and sore - * in Cassandra // * @param store the function to call when - * storing recordes in cassndra - * @return true if the operation was successful - */ - private boolean handleImport(Map files, Table table) { - try (BufferedReader br = new BufferedReader(new FileReader(files.get(table)))) { - String line; - while ((line = br.readLine()) != null) { - CassandraOperationStatus res; - if (Table.ARTIFACT.equals(table)) { - res = artifactCassandraDao.saveArtifact(jsonMapper.readValue(line, ESArtifactData.class)); - } else { - Type type = new TypeToken>() {}.getType(); - Map map = gson.fromJson(line, type); - EnumMap auditingFields = createAuditMap(map, table); - AuditingGenericEvent recordForCassandra; - try { - recordForCassandra = createAuditRecord(auditingFields); - } catch (ParseException e) { - log.error("filed to parse time stemp in recored {}", auditingFields); - return false; - } - res = auditCassandraDao.saveRecord(recordForCassandra); - } - if (!res.equals(CassandraOperationStatus.OK)) { - log.error("save recored to cassndra {} failed with status {} aborting.", - table.getTableDescription().getTableName(), res); - return false; - } - } - return true; - } catch (IOException e) { - log.error("failed to read file", e); - return false; - } - } - - /** - * the method checks if the given table is empty. - * - * @param table - * the name of the table we want to check - * @return true if the table is empty - */ - private Either checkIfTableIsEmpty(Table table) { - if (Table.ARTIFACT.equals(table)) { - return artifactCassandraDao.isTableEmpty(table.getTableDescription().getTableName()); - } else { - return auditCassandraDao.isTableEmpty(table.getTableDescription().getTableName()); - } + } + if (!res.equals(CassandraOperationStatus.OK)) { + log.error("save recored to cassndra {} failed with status {} aborting.", + table.getTableDescription().getTableName(), res); + return false; + } + } + return true; + } catch (IOException e) { + log.error("failed to read file", e); + return false; + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException e) { + log.error("failed to close file reader", e); + } + } + } + } + + AuditingGenericEvent createAuditRecordForCassandra(String json, Table table) throws IOException{ + return createAuditEvent(parseToMap(json), table); } - private boolean filesEmpty(Map files) { - for (Entry entry : files.entrySet()) { - File file = entry.getValue(); - if (file.length() != 0) { - log.info("file:{} is not empty skipping export", entry.getKey().getTableDescription().getTableName()); - return false; - } - } - return true; - } - - /** - * the method reads the records from es index of audit's into a file as - * json's. - * - * @param value - * the name of the index we want - * @param printerWritersMap - * a map of the writers we use to write to a file. - * @return true in case the export was successful. - */ - private boolean exportAudit(String value, Map printerWritersMap) { - log.info("stratng to export audit data from es index{} to file.", value); - QueryBuilder queryBuilder = QueryBuilders.matchAllQuery(); - SearchResponse scrollResp = elasticSearchClient.getClient().prepareSearch(value).setScroll(new TimeValue(60000)) - .setQuery(queryBuilder).setSize(100).execute().actionGet(); - while (true) { - for (SearchHit hit : scrollResp.getHits().getHits()) { - PrintWriter out = printerWritersMap.get(TypeToTableMapping.getTableByType(hit.getType())); - out.println(hit.getSourceAsString()); - } - scrollResp = elasticSearchClient.getClient().prepareSearchScroll(scrollResp.getScrollId()) - .setScroll(new TimeValue(60000)).execute().actionGet(); - if (scrollResp.getHits().getHits().length == 0) { - break; - - } - } - log.info("export audit data from es to file. finished succsesfully"); - return true; - } - - /** - * the method reads the records from es index of resources into a file as - * json's. - * - * @param index - * the name of the index we want to read - * @param printerWritersMap - * a map of the writers we use to write to a file. - * @return true in case the export was successful. - */ - private boolean exportArtifacts(String index, Map printerWritersMap) { - log.info("stratng to export artifact data from es to file."); - PrintWriter out = printerWritersMap.get(Table.ARTIFACT); - QueryBuilder queryBuilder = QueryBuilders.matchAllQuery(); - SearchResponse scrollResp = elasticSearchClient.getClient().prepareSearch(index).setScroll(new TimeValue(60000)) - .setQuery(queryBuilder).setSize(100).execute().actionGet(); - while (true) { - for (SearchHit hit : scrollResp.getHits().getHits()) { - out.println(hit.getSourceAsString()); - } - scrollResp = elasticSearchClient.getClient().prepareSearchScroll(scrollResp.getScrollId()) - .setScroll(new TimeValue(60000)).execute().actionGet(); - if (scrollResp.getHits().getHits().length == 0) { - break; - } - } - log.info("export artifact data from es to file. finished succsesfully"); - return true; - } - - /** - * the method retrieves all the indexes from elasticsearch. - * - * @return a map of indexes and there metadata - */ - private ImmutableOpenMap getIndexData() { - return elasticSearchClient.getClient().admin().cluster().prepareState().get().getState().getMetaData() - .getIndices(); - } - - /** - * the method creates all the files and dir which holds them. in case the - * files exist they will not be created again. - * - * @param appConfigDir - * the base path under which the output dir will be created and - * the export result files the created filesa are named according - * to the name of the table into which it will be imported. - * @param exportToEs - * if true all the export files will be recreated - * @return returns a map of tables and the files representing them them - */ - private Map createOutPutFiles(String appConfigDir, boolean exportToEs) { - Map result = new EnumMap<>(Table.class); - File outputDir = new File(appConfigDir + "/output/"); - if (!createOutPutFolder(outputDir)) { - return null; - } - for (Table table : Table.values()) { - File file = new File(outputDir + "/" + table.getTableDescription().getTableName()); - if (exportToEs) { - try { - if (file.exists()) { - Files.delete(file.toPath()); - } - } catch (IOException e) { - log.error("failed to delete output file {}", file.getAbsolutePath(), e); - return null; - } - file = new File(outputDir + "/" + table.getTableDescription().getTableName()); - } - if (!file.exists()) { - try { - file.createNewFile(); - } catch (IOException e) { - log.error("failed to create output file {}", file.getAbsolutePath(), e); - return null; - } - } - result.put(table, file); - } - return result; - } - - /** - * the method create the writers to each file - * - * @param files - * a map of the files according to table - * @return returns a map of writers according to table. - */ - private Map createWriters(Map files) { - Map printerWritersMap = new EnumMap<>(Table.class); - try { - for (Entry entry : files.entrySet()) { - log.info("creating writer for {}", entry.getKey()); - File file = entry.getValue(); - FileWriter fw = new FileWriter(file, true); - BufferedWriter bw = new BufferedWriter(fw); - PrintWriter out = new PrintWriter(bw); - printerWritersMap.put(entry.getKey(), out); - log.info("creating writer for {} was successful", entry.getKey()); - } - } catch (IOException e) { - log.error("create writer to file failed", e); - return null; - } - return printerWritersMap; - } - - /** - * the method creates the output dir in case it does not exist - * - * @param outputDir - * the path under wich the directory will be created. - * @return true in case the create was succsesful or the dir already exists - */ - private boolean createOutPutFolder(File outputDir) { - if (!outputDir.exists()) { - log.info("creating output dir {}", outputDir.getAbsolutePath()); - try { - Files.createDirectories(outputDir.toPath()); - } catch (IOException e) { - log.error("failed to create output dir {}", outputDir.getAbsolutePath(), e); - return false; - } - } - return true; - } - - public enum TypeToTableMapping { - USER_ADMIN_EVENT_TYPE(AuditingTypesConstants.USER_ADMIN_EVENT_TYPE, - Table.USER_ADMIN_EVENT), USER_ACCESS_EVENT_TYPE(AuditingTypesConstants.USER_ACCESS_EVENT_TYPE, - Table.USER_ACCESS_EVENT), RESOURCE_ADMIN_EVENT_TYPE( - AuditingTypesConstants.RESOURCE_ADMIN_EVENT_TYPE, - Table.RESOURCE_ADMIN_EVENT), DISTRIBUTION_DOWNLOAD_EVENT_TYPE( - AuditingTypesConstants.DISTRIBUTION_DOWNLOAD_EVENT_TYPE, - Table.DISTRIBUTION_DOWNLOAD_EVENT), DISTRIBUTION_ENGINE_EVENT_TYPE( - AuditingTypesConstants.DISTRIBUTION_ENGINE_EVENT_TYPE, - Table.DISTRIBUTION_ENGINE_EVENT), DISTRIBUTION_NOTIFICATION_EVENT_TYPE( - AuditingTypesConstants.DISTRIBUTION_NOTIFICATION_EVENT_TYPE, - Table.DISTRIBUTION_NOTIFICATION_EVENT), DISTRIBUTION_STATUS_EVENT_TYPE( - AuditingTypesConstants.DISTRIBUTION_STATUS_EVENT_TYPE, - Table.DISTRIBUTION_STATUS_EVENT), DISTRIBUTION_DEPLOY_EVENT_TYPE( - AuditingTypesConstants.DISTRIBUTION_DEPLOY_EVENT_TYPE, - Table.DISTRIBUTION_DEPLOY_EVENT), DISTRIBUTION_GET_UEB_CLUSTER_EVENT_TYPE( - AuditingTypesConstants.DISTRIBUTION_GET_UEB_CLUSTER_EVENT_TYPE, - Table.DISTRIBUTION_GET_UEB_CLUSTER_EVENT), AUTH_EVENT_TYPE( - AuditingTypesConstants.AUTH_EVENT_TYPE, - Table.AUTH_EVENT), CONSUMER_EVENT_TYPE( - AuditingTypesConstants.CONSUMER_EVENT_TYPE, - Table.CONSUMER_EVENT), CATEGORY_EVENT_TYPE( - AuditingTypesConstants.CATEGORY_EVENT_TYPE, - Table.CATEGORY_EVENT), GET_USERS_LIST_EVENT_TYPE( - AuditingTypesConstants.GET_USERS_LIST_EVENT_TYPE, - Table.GET_USERS_LIST_EVENT), GET_CATEGORY_HIERARCHY_EVENT_TYPE( - AuditingTypesConstants.GET_CATEGORY_HIERARCHY_EVENT_TYPE, - Table.GET_CATEGORY_HIERARCHY_EVENT); - - String typeName; - Table table; - - TypeToTableMapping(String typeName, Table table) { - this.typeName = typeName; - this.table = table; - } - - public String getTypeName() { - return typeName; - } - - public Table getTable() { - return table; - } - - public static Table getTableByType(String type) { - for (TypeToTableMapping mapping : TypeToTableMapping.values()) { - if (mapping.getTypeName().equalsIgnoreCase(type)) { - return mapping.getTable(); - } - } - return null; - } - } - - public AuditingGenericEvent createAuditRecord(EnumMap auditingFields) - throws ParseException { - AuditingActionEnum actionEnum = AuditingActionEnum - .getActionByName((String) auditingFields.get(AuditingFieldsKeysEnum.AUDIT_ACTION)); - String tableName = actionEnum.getAuditingEsType(); - AuditingGenericEvent event; - Date date; - switch (tableName) { - case AuditingTypesConstants.USER_ADMIN_EVENT_TYPE: - UserAdminEvent userAdminEvent = new UserAdminEvent(auditingFields); - date = simpleDateFormat.parse((String) auditingFields.get(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP)); - userAdminEvent.setTimestamp1(date); - event = userAdminEvent; - break; - case AuditingTypesConstants.AUTH_EVENT_TYPE: - AuthEvent authEvent = new AuthEvent(auditingFields); - date = simpleDateFormat.parse((String) auditingFields.get(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP)); - authEvent.setTimestamp1(date); - event = authEvent; - break; - case AuditingTypesConstants.CATEGORY_EVENT_TYPE: - CategoryEvent categoryEvent = new CategoryEvent(auditingFields); - date = simpleDateFormat.parse((String) auditingFields.get(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP)); - categoryEvent.setTimestamp1(date); - event = categoryEvent; - break; - case AuditingTypesConstants.RESOURCE_ADMIN_EVENT_TYPE: - ResourceAdminEvent resourceAdminEvent = new ResourceAdminEvent(auditingFields); - date = simpleDateFormat.parse((String) auditingFields.get(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP)); - resourceAdminEvent.setTimestamp1(date); - event = resourceAdminEvent; - break; - case AuditingTypesConstants.USER_ACCESS_EVENT_TYPE: - UserAccessEvent userAccessEvent = new UserAccessEvent(auditingFields); - date = simpleDateFormat.parse((String) auditingFields.get(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP)); - userAccessEvent.setTimestamp1(date); - event = userAccessEvent; - break; - case AuditingTypesConstants.DISTRIBUTION_STATUS_EVENT_TYPE: - DistributionStatusEvent distributionStatusEvent = new DistributionStatusEvent(auditingFields); - date = simpleDateFormat.parse((String) auditingFields.get(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP)); - distributionStatusEvent.setTimestamp1(date); - event = distributionStatusEvent; - break; - case AuditingTypesConstants.DISTRIBUTION_DOWNLOAD_EVENT_TYPE: - DistributionDownloadEvent distributionDownloadEvent = new DistributionDownloadEvent(auditingFields); - date = simpleDateFormat.parse((String) auditingFields.get(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP)); - distributionDownloadEvent.setTimestamp1(date); - event = distributionDownloadEvent; - break; - case AuditingTypesConstants.DISTRIBUTION_ENGINE_EVENT_TYPE: - DistributionEngineEvent distributionEngineEvent = new DistributionEngineEvent(auditingFields); - date = simpleDateFormat.parse((String) auditingFields.get(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP)); - distributionEngineEvent.setTimestamp1(date); - event = distributionEngineEvent; - break; - case AuditingTypesConstants.DISTRIBUTION_NOTIFICATION_EVENT_TYPE: - DistributionNotificationEvent distributionNotificationEvent = new DistributionNotificationEvent( - auditingFields); - date = simpleDateFormat.parse((String) auditingFields.get(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP)); - distributionNotificationEvent.setTimestamp1(date); - event = distributionNotificationEvent; - break; - case AuditingTypesConstants.DISTRIBUTION_DEPLOY_EVENT_TYPE: - DistributionDeployEvent distributionDeployEvent = new DistributionDeployEvent(auditingFields); - date = simpleDateFormat.parse((String) auditingFields.get(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP)); - distributionDeployEvent.setTimestamp1(date); - event = distributionDeployEvent; - break; - case AuditingTypesConstants.DISTRIBUTION_GET_UEB_CLUSTER_EVENT_TYPE: - AuditingGetUebClusterEvent auditingGetUebClusterEvent = new AuditingGetUebClusterEvent(auditingFields); - date = simpleDateFormat.parse((String) auditingFields.get(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP)); - auditingGetUebClusterEvent.setTimestamp1(date); - event = auditingGetUebClusterEvent; - break; - case AuditingTypesConstants.CONSUMER_EVENT_TYPE: - ConsumerEvent consumerEvent = new ConsumerEvent(auditingFields); - date = simpleDateFormat.parse((String) auditingFields.get(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP)); - consumerEvent.setTimestamp1(date); - event = consumerEvent; - break; - case AuditingTypesConstants.GET_USERS_LIST_EVENT_TYPE: - GetUsersListEvent getUsersListEvent = new GetUsersListEvent(auditingFields); - date = simpleDateFormat.parse((String) auditingFields.get(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP)); - getUsersListEvent.setTimestamp1(date); - event = getUsersListEvent; - break; - case AuditingTypesConstants.GET_CATEGORY_HIERARCHY_EVENT_TYPE: - GetCategoryHierarchyEvent getCategoryHierarchyEvent = new GetCategoryHierarchyEvent(auditingFields); - date = simpleDateFormat.parse((String) auditingFields.get(AuditingFieldsKeysEnum.AUDIT_TIMESTAMP)); - getCategoryHierarchyEvent.setTimestamp1(date); - event = getCategoryHierarchyEvent; - break; - default: - event = null; - break; - } - return event; - } + private Map parseToMap(String json) throws IOException { + return jsonMapper.readValue(json, new TypeReference>(){}); + } + + /** + * the method checks if the given table is empty + * + * @param table + * the name of the table we want to check + * @return true if the table is empty + */ + private Either checkIfTableIsEmpty(Table table) { + if (Table.ARTIFACT.equals(table)) { + return artifactCassandraDao.isTableEmpty(table.getTableDescription().getTableName()); + } else { + return auditCassandraDao.isTableEmpty(table.getTableDescription().getTableName()); + } + } + + private boolean filesEmpty(Map files) { + for (Table table : files.keySet()) { + File file = files.get(table); + if (file.length() != 0) { + log.info("file:{} is not empty skipping export", table.getTableDescription().getTableName()); + return false; + } + } + return true; + } + + /** + * the method reads the records from es index of audit's into a file as + * json's. + * + * @param value + * the name of the index we want + * @param printerWritersMap + * a map of the writers we use to write to a file. + * @return true in case the export was successful. + */ + private boolean exportAudit(String value, Map printerWritersMap) { + log.info("stratng to export audit data from es index{} to file.", value); + QueryBuilder queryBuilder = QueryBuilders.matchAllQuery(); + SearchResponse scrollResp = elasticSearchClient.getClient().prepareSearch(value).setScroll(new TimeValue(60000)) + .setQuery(queryBuilder).setSize(100).execute().actionGet(); + while (true) { + for (SearchHit hit : scrollResp.getHits().getHits()) { + PrintWriter out = printerWritersMap.get(TypeToTableMapping.getTableByType(hit.getType())); + out.println(hit.getSourceAsString()); + } + scrollResp = elasticSearchClient.getClient().prepareSearchScroll(scrollResp.getScrollId()) + .setScroll(new TimeValue(60000)).execute().actionGet(); + if (scrollResp.getHits().getHits().length == 0) { + break; + + } + } + + log.info("export audit data from es to file. finished succsesfully"); + return true; + } + + /** + * the method reads the records from es index of resources into a file as + * json's. + * + * @param index + * the name of the index we want to read + * @param printerWritersMap + * a map of the writers we use to write to a file. + * @return true in case the export was successful. + */ + private boolean exportArtifacts(String index, Map printerWritersMap) { + log.info("stratng to export artifact data from es to file."); + PrintWriter out = printerWritersMap.get(Table.ARTIFACT); + QueryBuilder queryBuilder = QueryBuilders.matchAllQuery(); + SearchResponse scrollResp = elasticSearchClient.getClient().prepareSearch(index).setScroll(new TimeValue(60000)) + .setQuery(queryBuilder).setSize(100).execute().actionGet(); + while (true) { + for (SearchHit hit : scrollResp.getHits().getHits()) { + ; + out.println(hit.getSourceAsString()); + } + scrollResp = elasticSearchClient.getClient().prepareSearchScroll(scrollResp.getScrollId()) + .setScroll(new TimeValue(60000)).execute().actionGet(); + if (scrollResp.getHits().getHits().length == 0) { + break; + + } + } + + log.info("export artifact data from es to file. finished succsesfully"); + return true; + } + + /** + * the method retrieves all the indexes from elasticsearch + * + * @return a map of indexes and there metadata + */ + private ImmutableOpenMap getIndexData() { + return elasticSearchClient.getClient().admin().cluster().prepareState().get().getState().getMetaData() + .getIndices(); + } + + /** + * the method creates all the files and dir which holds them. in case the + * files exist they will not be created again. + * + * @param appConfigDir + * the base path under which the output dir will be created and + * the export result files the created filesa are named according + * to the name of the table into which it will be imported. + * @param exportToEs + * if true all the export files will be recreated + * @returnthe returns a map of tables and the files representing them them + */ + private Map createOutPutFiles(String appConfigDir, boolean exportToEs) { + Map result = new EnumMap(Table.class); + File outputDir = new File(appConfigDir + "/output/"); + if (!createOutPutFolder(outputDir)) { + return null; + } + for (Table table : Table.values()) { + File file = new File(outputDir + "/" + table.getTableDescription().getTableName()); + if (exportToEs) { + try { + if (file.exists()) { + Files.delete(file.toPath()); + } + } catch (IOException e) { + log.error("failed to delete output file {}", file.getAbsolutePath(), e); + return null; + } + file = new File(outputDir + "/" + table.getTableDescription().getTableName()); + } + if (!file.exists()) { + try { + file.createNewFile(); + } catch (IOException e) { + log.error("failed to create output file {}", file.getAbsolutePath(), e); + return null; + } + } + result.put(table, file); + + } + return result; + } + + /** + * the method create the writers to each file + * + * @param files + * a map of the files according to table + * @return returns a map of writers according to table. + */ + private Map createWriters(Map files) { + Map printerWritersMap = new EnumMap<>(Table.class); + try { + for (Table table : files.keySet()) { + log.info("creating writer for {}", table); + File file = files.get(table); + FileWriter fw = new FileWriter(file, true); + BufferedWriter bw = new BufferedWriter(fw); + PrintWriter out = new PrintWriter(bw); + printerWritersMap.put(table, out); + log.info("creating writer for {} was successful", table); + } + } catch (IOException e) { + log.error("create writer to file failed", e); + return null; + } + return printerWritersMap; + } + + /** + * the method creates the output dir in case it does not exist + * + * @param outputDir + * the path under wich the directory will be created. + * @return true in case the create was succsesful or the dir already exists + */ + private boolean createOutPutFolder(File outputDir) { + if (!outputDir.exists()) { + log.info("creating output dir {}", outputDir.getAbsolutePath()); + try { + Files.createDirectories(outputDir.toPath()); + } catch (IOException e) { + log.error("failed to create output dir {}", outputDir.getAbsolutePath(), e); + return false; + } + } + return true; + } + + public enum TypeToTableMapping { + USER_ADMIN_EVENT_TYPE(AuditingTypesConstants.USER_ADMIN_EVENT_TYPE, + Table.USER_ADMIN_EVENT), USER_ACCESS_EVENT_TYPE(AuditingTypesConstants.USER_ACCESS_EVENT_TYPE, + Table.USER_ACCESS_EVENT), RESOURCE_ADMIN_EVENT_TYPE( + AuditingTypesConstants.RESOURCE_ADMIN_EVENT_TYPE, + Table.RESOURCE_ADMIN_EVENT), DISTRIBUTION_DOWNLOAD_EVENT_TYPE( + AuditingTypesConstants.DISTRIBUTION_DOWNLOAD_EVENT_TYPE, + Table.DISTRIBUTION_DOWNLOAD_EVENT), DISTRIBUTION_ENGINE_EVENT_TYPE( + AuditingTypesConstants.DISTRIBUTION_ENGINE_EVENT_TYPE, + Table.DISTRIBUTION_ENGINE_EVENT), DISTRIBUTION_NOTIFICATION_EVENT_TYPE( + AuditingTypesConstants.DISTRIBUTION_NOTIFICATION_EVENT_TYPE, + Table.DISTRIBUTION_NOTIFICATION_EVENT), DISTRIBUTION_STATUS_EVENT_TYPE( + AuditingTypesConstants.DISTRIBUTION_STATUS_EVENT_TYPE, + Table.DISTRIBUTION_STATUS_EVENT), DISTRIBUTION_DEPLOY_EVENT_TYPE( + AuditingTypesConstants.DISTRIBUTION_DEPLOY_EVENT_TYPE, + Table.DISTRIBUTION_DEPLOY_EVENT), DISTRIBUTION_GET_UEB_CLUSTER_EVENT_TYPE( + AuditingTypesConstants.DISTRIBUTION_GET_UEB_CLUSTER_EVENT_TYPE, + Table.DISTRIBUTION_GET_UEB_CLUSTER_EVENT), AUTH_EVENT_TYPE( + AuditingTypesConstants.AUTH_EVENT_TYPE, + Table.AUTH_EVENT), CONSUMER_EVENT_TYPE( + AuditingTypesConstants.CONSUMER_EVENT_TYPE, + Table.CONSUMER_EVENT), CATEGORY_EVENT_TYPE( + AuditingTypesConstants.CATEGORY_EVENT_TYPE, + Table.CATEGORY_EVENT), GET_USERS_LIST_EVENT_TYPE( + AuditingTypesConstants.GET_USERS_LIST_EVENT_TYPE, + Table.GET_USERS_LIST_EVENT), GET_CATEGORY_HIERARCHY_EVENT_TYPE( + AuditingTypesConstants.GET_CATEGORY_HIERARCHY_EVENT_TYPE, + Table.GET_CATEGORY_HIERARCHY_EVENT); + + String typeName; + Table table; + + TypeToTableMapping(String typeName, Table table) { + this.typeName = typeName; + this.table = table; + } + + public String getTypeName() { + return typeName; + } + + public Table getTable() { + return table; + } + + public static Table getTableByType(String type) { + for (TypeToTableMapping mapping : TypeToTableMapping.values()) { + if (mapping.getTypeName().equalsIgnoreCase(type)) { + return mapping.getTable(); + } + } + return null; + } + } } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphJsonValidator.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphJsonValidator.java index 23019b9b15..3d95de7d5c 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphJsonValidator.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphJsonValidator.java @@ -21,6 +21,9 @@ package org.openecomp.sdc.asdctool.impl; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.openecomp.sdc.common.log.wrappers.Logger; + import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; @@ -28,17 +31,12 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.fasterxml.jackson.databind.ObjectMapper; - /** * simple util class to verify that the titan export json graph is not corrupted */ public class GraphJsonValidator { - private static Logger log = LoggerFactory.getLogger(GraphJsonValidator.class.getName()); + private static Logger log = Logger.getLogger(GraphJsonValidator.class.getName()); public boolean verifyTitanJson(String filePath) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLConverter.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLConverter.java index 5fb5c7e7cf..0ec7af4906 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLConverter.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLConverter.java @@ -20,30 +20,11 @@ package org.openecomp.sdc.asdctool.impl; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - +import com.google.gson.Gson; +import com.thinkaurelius.titan.core.*; import org.apache.commons.configuration.BaseConfiguration; import org.apache.commons.lang3.tuple.ImmutablePair; -import org.apache.tinkerpop.gremlin.structure.Direction; -import org.apache.tinkerpop.gremlin.structure.Edge; -import org.apache.tinkerpop.gremlin.structure.Element; -import org.apache.tinkerpop.gremlin.structure.Graph; -import org.apache.tinkerpop.gremlin.structure.Property; -import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.apache.tinkerpop.gremlin.structure.*; import org.apache.tinkerpop.gremlin.structure.io.IoCore; import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONMapper; import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONReader; @@ -52,19 +33,15 @@ import org.apache.tinkerpop.gremlin.structure.util.ElementHelper; import org.openecomp.sdc.asdctool.Utils; import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.common.log.wrappers.Logger; -import com.google.gson.Gson; -import com.thinkaurelius.titan.core.TitanEdge; -import com.thinkaurelius.titan.core.TitanFactory; -import com.thinkaurelius.titan.core.TitanGraph; -import com.thinkaurelius.titan.core.TitanGraphQuery; -import com.thinkaurelius.titan.core.TitanVertex; +import java.io.*; +import java.util.*; +import java.util.Map.Entry; public class GraphMLConverter { - private static Logger log = LoggerFactory.getLogger(GraphMLConverter.class.getName()); + private static Logger log = Logger.getLogger(GraphMLConverter.class.getName()); private Gson gson = new Gson(); diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLDataAnalyzer.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLDataAnalyzer.java index f34feb0af6..863f920b47 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLDataAnalyzer.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLDataAnalyzer.java @@ -20,13 +20,6 @@ package org.openecomp.sdc.asdctool.impl; -import java.io.File; -import java.io.FileOutputStream; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; @@ -39,6 +32,13 @@ import org.jdom2.util.IteratorIterable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.File; +import java.io.FileOutputStream; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + public class GraphMLDataAnalyzer { private static Logger log = LoggerFactory.getLogger(GraphMLDataAnalyzer.class); diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/ProductLogic.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/ProductLogic.java index a8674f8a1b..40fd6fb3d0 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/ProductLogic.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/ProductLogic.java @@ -20,26 +20,24 @@ package org.openecomp.sdc.asdctool.impl; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - +import com.thinkaurelius.titan.core.TitanFactory; +import com.thinkaurelius.titan.core.TitanGraph; +import com.thinkaurelius.titan.core.TitanVertex; import org.apache.tinkerpop.gremlin.structure.Vertex; import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.common.log.wrappers.Logger; -import com.thinkaurelius.titan.core.TitanFactory; -import com.thinkaurelius.titan.core.TitanGraph; -import com.thinkaurelius.titan.core.TitanVertex; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; /** * Created by mlando on 2/23/2016. */ public class ProductLogic { - private static Logger log = LoggerFactory.getLogger(ProductLogic.class.getName()); + private static Logger log = Logger.getLogger(ProductLogic.class.getName()); public boolean deleteAllProducts(String titanFile, String beHost, String bePort, String adminUser) { log.debug("retrieving all products from graph"); diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/RestUtils.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/RestUtils.java index 50781f25c2..a886367561 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/RestUtils.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/RestUtils.java @@ -20,13 +20,12 @@ package org.openecomp.sdc.asdctool.impl; -import java.util.Properties; - import org.apache.http.HttpStatus; import org.openecomp.sdc.common.http.client.api.HttpRequest; import org.openecomp.sdc.common.http.client.api.HttpResponse; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.common.log.wrappers.Logger; + +import java.util.Properties; /** * Created by mlando on 2/23/2016. @@ -35,7 +34,7 @@ public class RestUtils { final static String DELETE_PRODUCT = "http://%s:%s/sdc2/rest/v1/catalog/products/%s"; - private static Logger log = LoggerFactory.getLogger(RestUtils.class.getName()); + private static Logger log = Logger.getLogger(RestUtils.class.getName()); public RestUtils() { } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/TitanGraphInitializer.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/TitanGraphInitializer.java index ae226f1a82..28a5bbdb24 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/TitanGraphInitializer.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/TitanGraphInitializer.java @@ -20,10 +20,10 @@ package org.openecomp.sdc.asdctool.impl; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; - +import com.thinkaurelius.titan.core.*; +import com.thinkaurelius.titan.core.schema.ConsistencyModifier; +import com.thinkaurelius.titan.core.schema.TitanGraphIndex; +import com.thinkaurelius.titan.core.schema.TitanManagement; import org.apache.tinkerpop.gremlin.structure.Edge; import org.apache.tinkerpop.gremlin.structure.Vertex; import org.openecomp.sdc.be.dao.graph.datatype.ActionEnum; @@ -39,14 +39,9 @@ import org.openecomp.sdc.be.resources.data.UserData; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.thinkaurelius.titan.core.PropertyKey; -import com.thinkaurelius.titan.core.TitanException; -import com.thinkaurelius.titan.core.TitanFactory; -import com.thinkaurelius.titan.core.TitanGraph; -import com.thinkaurelius.titan.core.TitanGraphQuery; -import com.thinkaurelius.titan.core.schema.ConsistencyModifier; -import com.thinkaurelius.titan.core.schema.TitanGraphIndex; -import com.thinkaurelius.titan.core.schema.TitanManagement; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; public class TitanGraphInitializer { @@ -90,6 +85,10 @@ public class TitanGraphInitializer { return false; } + private static boolean isVertexNotExist(Map properties) { + return !isVertexExist(properties); + } + private static void createDefaultAdminUser() { createUser(getDefaultUserAdmin()); graph.tx().commit(); @@ -187,13 +186,19 @@ public class TitanGraphInitializer { createVertexIndixes(); createEdgeIndixes(); createDefaultAdminUser(); - createRootCatalogVertex(); + createRootVertex(VertexTypeEnum.CATALOG_ROOT); + createRootVertex(VertexTypeEnum.ARCHIVE_ROOT); } - private static void createRootCatalogVertex() { - Vertex vertex = graph.addVertex(); - vertex.property(GraphPropertyEnum.UNIQUE_ID.getProperty(), IdBuilderUtils.generateUniqueId()); - vertex.property(GraphPropertyEnum.LABEL.getProperty(), VertexTypeEnum.CATALOG_ROOT.getName()); - graph.tx().commit(); + private static void createRootVertex(VertexTypeEnum vertexTypeEnum) { + Map checkedProperties = new HashMap<>(); + checkedProperties.put(GraphPropertiesDictionary.LABEL.getProperty(), vertexTypeEnum.getName()); + if (isVertexNotExist(checkedProperties)) { + Vertex vertex = graph.addVertex(); + vertex.property(GraphPropertyEnum.UNIQUE_ID.getProperty(), IdBuilderUtils.generateUniqueId()); + vertex.property(GraphPropertyEnum.LABEL.getProperty(), vertexTypeEnum.getName()); + graph.tx().commit(); + } } + } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/UpdatePropertyOnVertex.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/UpdatePropertyOnVertex.java index 6f0136cba3..a5e0449b6b 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/UpdatePropertyOnVertex.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/UpdatePropertyOnVertex.java @@ -20,25 +20,23 @@ package org.openecomp.sdc.asdctool.impl; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - +import com.thinkaurelius.titan.core.TitanFactory; +import com.thinkaurelius.titan.core.TitanGraph; +import com.thinkaurelius.titan.core.TitanGraphQuery; import org.apache.tinkerpop.gremlin.structure.Vertex; import org.openecomp.sdc.asdctool.Utils; import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary; import org.openecomp.sdc.be.model.LifecycleStateEnum; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.common.log.wrappers.Logger; -import com.thinkaurelius.titan.core.TitanFactory; -import com.thinkaurelius.titan.core.TitanGraph; -import com.thinkaurelius.titan.core.TitanGraphQuery; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; public class UpdatePropertyOnVertex { - private static Logger log = LoggerFactory.getLogger(UpdatePropertyOnVertex.class.getName()); + private static Logger log = Logger.getLogger(UpdatePropertyOnVertex.class.getName()); public Integer updatePropertyOnServiceAtLeastCertified(String titanFile, Map keyValueToSet, List> orCriteria) { diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/VrfObjectFixHandler.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/VrfObjectFixHandler.java new file mode 100644 index 0000000000..8eec51071f --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/VrfObjectFixHandler.java @@ -0,0 +1,197 @@ +package org.openecomp.sdc.asdctool.impl; + +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.MapUtils; +import org.apache.tinkerpop.gremlin.structure.Direction; +import org.apache.tinkerpop.gremlin.structure.Edge; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.openecomp.sdc.asdctool.migration.tasks.handlers.XlsOutputHandler; +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.EdgePropertyEnum; +import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; +import org.openecomp.sdc.be.dao.jsongraph.utils.JsonParserUtils; +import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; +import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.CompositionDataDefinition; +import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; +import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; +import org.openecomp.sdc.be.model.jsontitan.enums.JsonConstantKeysEnum; +import org.openecomp.sdc.be.model.operations.StorageException; +import org.openecomp.sdc.common.log.wrappers.Logger; + +import java.io.IOException; +import java.util.*; + +import static java.util.Collections.emptyList; +import static java.util.stream.Collectors.toList; + +@org.springframework.stereotype.Component("vrfObjectFixHandler") +public class VrfObjectFixHandler { + + private static final Logger log = Logger.getLogger(VrfObjectFixHandler.class); + private static final String VALID_TOSCA_NAME = "org.openecomp.nodes.VRFObject"; + private static final Object[] outputTableTitle = + new String[]{"VRF OBJECT VERSION", + "CONTAINER NAME", + "CONTAINER UNIQUE ID", + "INSTANCE NAME", + "INSTANCE UNIQUE ID"}; + + private XlsOutputHandler outputHandler; + private final String sheetName = this.getClass().getSimpleName() + "Report"; + + private TitanDao titanDao; + + public VrfObjectFixHandler(TitanDao titanDao) { + this.titanDao = titanDao; + } + + public boolean handle(String mode, String outputPath) { + outputHandler = new XlsOutputHandler(outputPath, sheetName, outputTableTitle); + switch (mode){ + case "detect" : + return detectCorruptedData(); + case "fix": + return fixCorruptedData(); + default : + log.debug("#handle - The invalid mode parameter has been received: {}", mode); + return false; + } + } + + private boolean fixCorruptedData(){ + try{ + Map>> corruptedData = fetchCorruptedData(); + corruptedData.forEach(this::fixCorruptedVfrObjectAndRelatedInstances); + titanDao.commit(); + writeOutput(corruptedData); + } catch (Exception e){ + titanDao.rollback(); + log.debug("#fixCorruptedData - Failed to detect corrupted data. The exception occurred: ", e); + return false; + } + return true; + } + + private boolean detectCorruptedData(){ + try{ + Map>> corruptedData = fetchCorruptedData(); + writeOutput(corruptedData); + } catch (Exception e){ + log.debug("#detectCorruptedData - Failed to detect corrupted data. The exception occurred: ", e); + return false; + } + return true; + } + + private void fixCorruptedVfrObjectAndRelatedInstances(GraphVertex vfrObjectV, Map> instances) { + fixCorruptedVfrObject(vfrObjectV); + instances.forEach(this::fixCorruptedContainerInstances); + } + + private void fixCorruptedVfrObject(GraphVertex vfrObjectV) { + vfrObjectV.getMetadataProperties().put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, VALID_TOSCA_NAME); + titanDao.updateVertex(vfrObjectV).left().on(this::rightOnUpdate); + } + + private Map>> fetchCorruptedData(){ + Map>> corruptedData = new HashMap<>(); + List vrfObjectsV = getCorruptedVrfObjects(); + vrfObjectsV.forEach(vrfObjectV-> fillCorruptedData(vrfObjectV, corruptedData)); + return corruptedData; + } + + private List getCorruptedVrfObjects() { + Map props = new EnumMap<>(GraphPropertyEnum.class); + props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, "org.openecomp.resource.configuration.VRFObject"); + return titanDao.getByCriteria(VertexTypeEnum.NODE_TYPE, props).left().on(this::rightOnGet); + } + + private void fillCorruptedData(GraphVertex vrfObjectV, Map>> findToUpdate) { + Map> corruptedInstances = new HashMap<>(); + findToUpdate.put(vrfObjectV, corruptedInstances); + Iterator instanceEdges = vrfObjectV.getVertex().edges(Direction.IN, EdgeLabelEnum.INSTANCE_OF.name()); + while(instanceEdges.hasNext()){ + Edge edge = instanceEdges.next(); + putCorruptedInstances(corruptedInstances, edge, (List) titanDao.getProperty(edge, EdgePropertyEnum.INSTANCES)); + } + } + + private void putCorruptedInstances(Map> corruptedInstances, Edge edge, List ids) { + if(CollectionUtils.isNotEmpty(ids)){ + Vertex container = edge.outVertex(); + Map jsonObj = getJsonMap(container); + CompositionDataDefinition composition = (CompositionDataDefinition)jsonObj.get(JsonConstantKeysEnum.COMPOSITION.getValue()); + corruptedInstances.put(container, composition.getComponentInstances() + .values() + .stream() + .filter(i->ids.contains(i.getUniqueId())) + .collect(toList())); + } + } + + private void fixCorruptedContainerInstances(Vertex container, List corruptedInstances){ + try { + Map jsonObj = getJsonMap(container); + fixComponentToscaName(corruptedInstances, jsonObj); + String jsonMetadataStr = JsonParserUtils.toJson(jsonObj); + container.property(GraphPropertyEnum.JSON.getProperty(), jsonMetadataStr); + } catch (IOException e) { + throw new StorageException("Failed to fix the corrupted instances of the container", e, TitanOperationStatus.GENERAL_ERROR); + } + } + + private void fixComponentToscaName(List corruptedInstances, Map jsonObj) { + List ids = corruptedInstances + .stream() + .map(ComponentInstanceDataDefinition::getUniqueId) + .collect(toList()); + + CompositionDataDefinition composition = (CompositionDataDefinition)jsonObj.get(JsonConstantKeysEnum.COMPOSITION.getValue()); + composition.getComponentInstances() + .values() + .stream() + .filter(i->ids.contains(i.getUniqueId())) + .forEach(i->i.setToscaComponentName(VALID_TOSCA_NAME)); + } + + private Map getJsonMap(Vertex container) { + String json = (String)container.property(GraphPropertyEnum.JSON.getProperty()).value(); + Map properties = titanDao.getVertexProperties(container); + VertexTypeEnum label = VertexTypeEnum.getByName((String) (properties.get(GraphPropertyEnum.LABEL))); + return JsonParserUtils.toMap(json, label != null ? label.getClassOfJson() : null); + } + + private void writeOutput(Map>> corruptedData) { + if(outputHandler.getOutputPath() != null){ + if(MapUtils.isNotEmpty(corruptedData)){ + corruptedData.forEach(this::addVrfObjectRecord); + } else { + outputHandler.addRecord("CORRUPTED VRF OBJECT NOT FOUND"); + } + outputHandler.writeOutputAndCloseFile(); + } + } + + private List rightOnGet(TitanOperationStatus status) { + if(status == TitanOperationStatus.NOT_FOUND){ + return emptyList(); + } + throw new StorageException(status); + } + private GraphVertex rightOnUpdate(TitanOperationStatus status) { + throw new StorageException(status); + } + + private void addVrfObjectRecord(GraphVertex vrfObject, Map> instances) { + outputHandler.addRecord(vrfObject.getMetadataProperties().get(GraphPropertyEnum.VERSION).toString()); + instances.forEach(this::addVrfObjectInstances); + } + + private void addVrfObjectInstances(Vertex container, List instances) { + outputHandler.addRecord("", container.property(GraphPropertyEnum.NAME.getProperty()).value().toString(), container.property(GraphPropertyEnum.UNIQUE_ID.getProperty()).value().toString()); + instances.forEach(i->outputHandler.addRecord("","","",i.getName(),i.getUniqueId())); + } +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/internal/tool/CommonInternalTool.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/internal/tool/CommonInternalTool.java new file mode 100644 index 0000000000..bdf3d3b71b --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/internal/tool/CommonInternalTool.java @@ -0,0 +1,38 @@ +package org.openecomp.sdc.asdctool.impl.internal.tool; + +import java.io.IOException; +import java.util.Map; + +import org.openecomp.sdc.asdctool.utils.ConsoleWriter; +import org.openecomp.sdc.asdctool.utils.ReportWriter; +import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; + +public abstract class CommonInternalTool { + protected ReportWriter reportWriter; + private String reportType; + + CommonInternalTool(String reportType){ + this.reportType = reportType; + } + protected ReportWriter getReportWriter() throws IOException{ + if ( reportWriter == null ){ + reportWriter = new ReportWriter(reportType); + } + return reportWriter; + } + public void closeAll() { + try { + getReportWriter().close(); + } catch (IOException e) { + ConsoleWriter.dataLine("\nFailed to close report file."); + } + } + protected void printComponentInfo(Map metadataProperties) { + ConsoleWriter.dataLine("component from type", metadataProperties.get(GraphPropertyEnum.COMPONENT_TYPE)); + ConsoleWriter.dataLine("component name", metadataProperties.get(GraphPropertyEnum.NAME)); + ConsoleWriter.dataLine("component version", metadataProperties.get(GraphPropertyEnum.VERSION)); + ConsoleWriter.dataLine("component state", metadataProperties.get(GraphPropertyEnum.STATE)); + ConsoleWriter.dataLine("component is highest", metadataProperties.get(GraphPropertyEnum.IS_HIGHEST_VERSION)); + ConsoleWriter.dataLine("component is archived", metadataProperties.get(GraphPropertyEnum.IS_ARCHIVED)); + } +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/internal/tool/CsarGenerator.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/internal/tool/CsarGenerator.java new file mode 100644 index 0000000000..21085942ac --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/internal/tool/CsarGenerator.java @@ -0,0 +1,187 @@ +package org.openecomp.sdc.asdctool.impl.internal.tool; + +import java.io.IOException; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Scanner; +import java.util.function.Supplier; +import java.util.stream.Collectors; + +import org.openecomp.sdc.asdctool.utils.ConsoleWriter; +import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao; +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.ArtifactDataDefinition; +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.model.ArtifactDefinition; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.LifecycleStateEnum; +import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade; +import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; +import org.openecomp.sdc.be.resources.data.ESArtifactData; +import org.openecomp.sdc.be.tosca.CsarUtils; +import org.openecomp.sdc.be.tosca.ToscaError; +import org.openecomp.sdc.be.tosca.ToscaExportHandler; +import org.openecomp.sdc.be.tosca.ToscaRepresentation; +import org.openecomp.sdc.common.api.ArtifactTypeEnum; +import org.openecomp.sdc.common.log.wrappers.Logger; +import org.openecomp.sdc.common.util.GeneralUtility; +import org.openecomp.sdc.exception.ResponseFormat; +import org.springframework.beans.factory.annotation.Autowired; + +import fj.data.Either; + +@org.springframework.stereotype.Component("csarGenerator") +public class CsarGenerator extends CommonInternalTool { + public CsarGenerator() { + super("generate"); + } + + @Autowired + private TitanDao titanDao; + @Autowired + private CsarUtils csarUtils; + @Autowired + private ToscaOperationFacade toscaOperationFacade; + @Autowired + private ArtifactCassandraDao artifactCassandraDao; + @Autowired + private ToscaExportHandler toscaExportHandler; + + + private static Logger log = Logger.getLogger(CsarGenerator.class.getName()); + + public void generateCsar(String uuid, Scanner scanner) { + TitanOperationStatus status = TitanOperationStatus.OK; + + Map props = new EnumMap<>(GraphPropertyEnum.class); + props.put(GraphPropertyEnum.UUID, uuid); + props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); + props.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name()); + + List byCriterria = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, props).either(l -> l, r -> null); + if (byCriterria != null && !byCriterria.isEmpty()) { + if (byCriterria.size() > 1) { + ConsoleWriter.dataLine("Warning ! More that 1 certified service with uuid", uuid); + // TBD + } else { + GraphVertex metadataV = byCriterria.get(0); + + printComponentInfo(metadataV.getMetadataProperties()); + ConsoleWriter.dataLine("\nGenerate CSAR (yes/no)?"); + String input = scanner.nextLine(); + if (input.equalsIgnoreCase("yes")) { + + status = handleService(metadataV, uuid); + } + } + } else { + ConsoleWriter.dataLine("No certified service with UUID", uuid); + } + if (status == TitanOperationStatus.OK) { + titanDao.commit(); + } else { + titanDao.rollback(); + } + } + + private TitanOperationStatus handleService(GraphVertex metadataV, String uuid) { + TitanOperationStatus status = TitanOperationStatus.OK; + org.openecomp.sdc.be.model.Component component = toscaOperationFacade.getToscaFullElement(metadataV.getUniqueId()).either(l -> l, r -> null); + if (component != null) { + + Supplier supplier = () -> generateToscaPayload(component); + generateArtifact(component, ArtifactTypeEnum.TOSCA_TEMPLATE, supplier); + + supplier = () -> generateCsarPayload(component); + generateArtifact(component, ArtifactTypeEnum.TOSCA_CSAR, supplier); + + GraphVertex toscaArtifactV = titanDao.getChildVertex(metadataV, EdgeLabelEnum.TOSCA_ARTIFACTS, JsonParseFlagEnum.ParseJson).either(l->l, r->null); + if ( toscaArtifactV != null ){ + Map copy = component.getToscaArtifacts().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue()))); + toscaArtifactV.setJson(copy); + titanDao.updateVertex(toscaArtifactV); + } + + } else { + ConsoleWriter.dataLine("Failed to fetch certified service with UUID", uuid); + } + return status; + } + + private TitanOperationStatus generateArtifact(Component component, ArtifactTypeEnum artifactType, Supplier supplier){ + TitanOperationStatus status = TitanOperationStatus.GENERAL_ERROR; + ArtifactDefinition csarArtifact = null; + Optional op = component.getToscaArtifacts().values().stream().filter(p -> p.getArtifactType().equals(artifactType.getType())).findAny(); + if (op.isPresent()) { + csarArtifact = op.get(); + + status = savePayload(component, csarArtifact, supplier); + } + return status; + } + + private byte[] generateCsarPayload(org.openecomp.sdc.be.model.Component component) { + return csarUtils.createCsar(component, true, true).either( l -> l, r -> null); + } + private byte[] generateToscaPayload(Component component){ + return toscaExportHandler.exportComponent(component).either(l -> l.getMainYaml().getBytes(), r -> null); + } + + private TitanOperationStatus savePayload(org.openecomp.sdc.be.model.Component component, ArtifactDefinition csarArtifact, Supplier supplier) { + byte[] payload = supplier.get(); + + if ( payload == null ) { + ConsoleWriter.dataLine("create artifact failed ", csarArtifact.getArtifactLabel()); + return TitanOperationStatus.GENERAL_ERROR; + } + ConsoleWriter.dataLine("createartifact success ", csarArtifact.getArtifactLabel()); + csarArtifact.setPayload(payload); + byte[] decodedPayload = csarArtifact.getPayloadData(); + + String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(component.getUniqueId(), csarArtifact.getArtifactLabel()); + csarArtifact.setUniqueId(uniqueId); + csarArtifact.setEsId(csarArtifact.getUniqueId()); + + ConsoleWriter.dataLine("create artifact unique id ", uniqueId); + + + csarArtifact.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(decodedPayload)); + ESArtifactData artifactData = new ESArtifactData(csarArtifact.getEsId(), decodedPayload); + artifactCassandraDao.saveArtifact(artifactData); + ConsoleWriter.dataLine("Artifact generated and saved into Cassandra ", csarArtifact.getArtifactLabel()); + report(component, csarArtifact); + + return TitanOperationStatus.OK; + } + + private void report(org.openecomp.sdc.be.model.Component component, ArtifactDefinition csarArtifact) { + Map dataToPrint = new HashMap<>(); + dataToPrint.put("name", component.getName()); + dataToPrint.put("type", component.getComponentType()); + dataToPrint.put("version", component.getVersion()); + dataToPrint.put("UUID", component.getUUID()); + dataToPrint.put("state", component.getLifecycleState()); + dataToPrint.put("archive", component.isArchived()); + dataToPrint.put("id", component.getUniqueId()); + dataToPrint.put("artifact name", csarArtifact.getArtifactLabel()); + dataToPrint.put("artifact id", csarArtifact.getUniqueId()); + dataToPrint.put("csar es id", csarArtifact.getEsId()); + dataToPrint.put("artifact checksum", csarArtifact.getArtifactChecksum()); + + try { + getReportWriter().report(dataToPrint); + } catch (IOException e) { + ConsoleWriter.dataLine("\nFailed to created report file."); + } + } +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/internal/tool/DeleteComponentHandler.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/internal/tool/DeleteComponentHandler.java new file mode 100644 index 0000000000..1dd6c7e10a --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/internal/tool/DeleteComponentHandler.java @@ -0,0 +1,200 @@ +package org.openecomp.sdc.asdctool.impl.internal.tool; + +import com.thinkaurelius.titan.core.TitanVertex; +import fj.data.Either; +import org.apache.tinkerpop.gremlin.structure.Direction; +import org.apache.tinkerpop.gremlin.structure.Edge; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.openecomp.sdc.asdctool.utils.ConsoleWriter; +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.VertexTypeEnum; +import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; +import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; +import org.openecomp.sdc.be.model.jsontitan.operations.NodeTypeOperation; +import org.openecomp.sdc.be.model.jsontitan.operations.TopologyTemplateOperation; +import org.openecomp.sdc.be.model.jsontitan.operations.ToscaElementOperation; +import org.openecomp.sdc.common.log.wrappers.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.util.Iterator; +import java.util.Map; +import java.util.Scanner; + +@Component("deleteComponentHandler") +public class DeleteComponentHandler extends CommonInternalTool{ + @Autowired + private TitanDao titanDao; + @Autowired + private NodeTypeOperation nodeTypeOperation; + @Autowired + private TopologyTemplateOperation topologyTemplateOperation; + + + private static Logger log = Logger.getLogger(DeleteComponentHandler.class.getName()); + + + public DeleteComponentHandler(){ + super("delete"); + } + public void deleteComponent(String id, Scanner scanner) { + TitanOperationStatus status = TitanOperationStatus.OK; + GraphVertex metadataVertex = titanDao.getVertexById(id).either(l -> l, r -> null); + if (metadataVertex != null) { + status = handleComponent(scanner, metadataVertex); + } else { + ConsoleWriter.dataLine("No vertex for id", id); + } + if (status == TitanOperationStatus.OK) { + titanDao.commit(); + } else { + titanDao.rollback(); + } + } + + private TitanOperationStatus handleComponent(Scanner scanner, GraphVertex metadataVertex) { + Map metadataProperties = metadataVertex.getMetadataProperties(); + TitanOperationStatus status = TitanOperationStatus.OK; + printComponentInfo(metadataProperties); + + Iterator edges = metadataVertex.getVertex().edges(Direction.OUT, EdgeLabelEnum.VERSION.name()); + if (edges != null && edges.hasNext()) { + ConsoleWriter.dataLine("\ncomponent is not latest version and cannot be deleted"); + } else { + ConsoleWriter.dataLine("\ncomponent is latest ."); + if (isReferenceExist(metadataVertex)) { + ConsoleWriter.dataLine("\nExist reference on component ( istance, proxy or allotted). Component cannot be deleted"); + } else { + ConsoleWriter.dataLine("\nNo references. Try to delete (yes/no)?"); + String input = scanner.nextLine(); + if (input.equalsIgnoreCase("yes")) { + status = handleComponent(metadataVertex); + } + } + } + return status; + } + + private TitanOperationStatus handleComponent(GraphVertex metadataVertex) { + ToscaElementOperation toscaElementOperation = getOperationByLabel(metadataVertex); + Iterator edges = metadataVertex.getVertex().edges(Direction.IN, EdgeLabelEnum.VERSION.name()); + if (edges != null && edges.hasNext()) { + TitanOperationStatus status = updatePreviousVersion(metadataVertex, edges); + if ( status != TitanOperationStatus.OK ){ + return status; + } + } + toscaElementOperation.deleteToscaElement(metadataVertex) + .left() + .map(l -> { + ConsoleWriter.dataLine("\nDeleted"); + report(metadataVertex); + return TitanOperationStatus.OK; + }) + .right() + .map(r-> { + ConsoleWriter.dataLine("\nFailed to delete. see log file"); + return r; + }); + return TitanOperationStatus.OK; + } + + private TitanOperationStatus updatePreviousVersion(GraphVertex metadataVertex, Iterator edges) { + Edge edge = edges.next(); + TitanVertex prevVersionVertex = (TitanVertex) edge.outVertex(); + // check if previous version is deleted + Boolean isDeleted = (Boolean) titanDao.getProperty(prevVersionVertex, GraphPropertyEnum.IS_DELETED.getProperty()); + if (isDeleted != null && isDeleted) { + ConsoleWriter.dataLine("\nPrevoius version is marked as deleted. Component cannot be deleted"); + return TitanOperationStatus.GENERAL_ERROR; + } + // update highest property for previous version + TitanOperationStatus status = updateStateOfPreviuosVersion(prevVersionVertex); + if ( TitanOperationStatus.OK != status ){ + return status; + } + + // connect to catalog or archive + return connectToCatalogAndArchive(metadataVertex, prevVersionVertex); + } + + private TitanOperationStatus updateStateOfPreviuosVersion(TitanVertex prevVersionVertex) { + String prevId = (String) titanDao.getProperty(prevVersionVertex, GraphPropertyEnum.UNIQUE_ID.getProperty()); + Either prevGraphVertex = titanDao.getVertexById(prevId); + GraphVertex prevVertex = prevGraphVertex.left().value(); + prevVertex.addMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION, true); + titanDao.updateVertex(prevVertex); + + Iterator edgesIter = prevVersionVertex.edges(Direction.IN, EdgeLabelEnum.LAST_STATE.name()); + if ( edgesIter.hasNext() ) { + Edge lastStateEdge = edgesIter.next(); + Vertex lastModifier = lastStateEdge.outVertex(); + TitanOperationStatus replaceRes = titanDao.replaceEdgeLabel(lastModifier, prevVersionVertex, lastStateEdge, EdgeLabelEnum.LAST_STATE, EdgeLabelEnum.STATE); + if (replaceRes != TitanOperationStatus.OK) { + log.info("Failed to replace label from {} to {}. status = {}", EdgeLabelEnum.LAST_STATE, EdgeLabelEnum.STATE, replaceRes); + ConsoleWriter.dataLine("\nFailed to replace LAST_STATE edge . Failed to delete"); + return TitanOperationStatus.GENERAL_ERROR; + } + } + return TitanOperationStatus.OK; + } + + + private TitanOperationStatus connectToCatalogAndArchive(GraphVertex metadataVertex, TitanVertex prevVersionVertex) { + + TitanOperationStatus status = connectByLabel(metadataVertex, prevVersionVertex, EdgeLabelEnum.CATALOG_ELEMENT, VertexTypeEnum.CATALOG_ROOT); + if ( status == TitanOperationStatus.OK ){ + status = connectByLabel(metadataVertex, prevVersionVertex, EdgeLabelEnum.ARCHIVE_ELEMENT, VertexTypeEnum.ARCHIVE_ROOT); + } + return status; + } + + private TitanOperationStatus connectByLabel(GraphVertex metadataVertex, TitanVertex prevVersionVertex, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexlabel) { + Iterator edgesToCatalog = metadataVertex.getVertex().edges(Direction.IN, edgeLabel.name()); + if ( edgesToCatalog != null && edgesToCatalog.hasNext() ){ + //exist edge move to prev version + Either catalog = titanDao.getVertexByLabel(vertexlabel); + if (catalog.isRight()) { + log.debug("Failed to fetch {} vertex, error {}", vertexlabel, catalog.right().value()); + return catalog.right().value(); + } + GraphVertex catalogV = catalog.left().value(); + Edge edge = edgesToCatalog.next(); + return titanDao.createEdge(catalogV.getVertex(), prevVersionVertex, edgeLabel, edge ); + } + return TitanOperationStatus.OK; + } + + private boolean isReferenceExist(GraphVertex metadataVertex) { + return existEdgeByLabel(metadataVertex, EdgeLabelEnum.INSTANCE_OF) || existEdgeByLabel(metadataVertex, EdgeLabelEnum.PROXY_OF) || existEdgeByLabel(metadataVertex, EdgeLabelEnum.ALLOTTED_OF); + } + + private boolean existEdgeByLabel(GraphVertex metadataVertex, EdgeLabelEnum label) { + Iterator edges = metadataVertex.getVertex().edges(Direction.IN, label.name()); + return (edges != null && edges.hasNext()); + } + + private ToscaElementOperation getOperationByLabel(GraphVertex metadataVertex) { + VertexTypeEnum label = metadataVertex.getLabel(); + if (label == VertexTypeEnum.NODE_TYPE) { + return nodeTypeOperation; + } else { + return topologyTemplateOperation; + } + } + + private void report(GraphVertex metadataVertex) { + try { + getReportWriter().report(metadataVertex.getMetadataProperties()); + } catch (IOException e) { + ConsoleWriter.dataLine("\nFailed to created report file."); + } + } + + + + +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/ArtifactToolBL.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/ArtifactToolBL.java index 16eb0be95e..937cb7c0b4 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/ArtifactToolBL.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/ArtifactToolBL.java @@ -1,16 +1,14 @@ package org.openecomp.sdc.asdctool.impl.validator; -import java.util.List; - import org.openecomp.sdc.asdctool.impl.validator.executers.IArtifactValidatorExecuter; -import org.openecomp.sdc.asdctool.impl.validator.utils.ReportManager; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.beans.factory.annotation.Autowired; +import java.util.List; + public class ArtifactToolBL { - private static Logger log = LoggerFactory.getLogger(ValidationToolBL.class.getName()); + private static Logger log = Logger.getLogger(ValidationToolBL.class.getName()); @Autowired protected List validators; @@ -19,13 +17,13 @@ public class ArtifactToolBL { public boolean validateAll() { for (IArtifactValidatorExecuter validatorExec: validators) { - System.out.println("ValidatorExecuter "+validatorExec.getName()+" started"); + log.debug("ValidatorExecuter "+validatorExec.getName()+" started"); if (!validatorExec.executeValidations()) { allValid = false; - System.out.println("ValidatorExecuter "+validatorExec.getName()+" finished with warnings"); + log.debug("ValidatorExecuter "+validatorExec.getName()+" finished with warnings"); } else { - System.out.println("ValidatorExecuter "+validatorExec.getName()+" finished successfully"); + log.debug("ValidatorExecuter "+validatorExec.getName()+" finished successfully"); } } return allValid; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/ValidationToolBL.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/ValidationToolBL.java index 9406351a6c..b8065e03f6 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/ValidationToolBL.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/ValidationToolBL.java @@ -1,21 +1,19 @@ package org.openecomp.sdc.asdctool.impl.validator; -import java.util.List; - import org.openecomp.sdc.asdctool.impl.validator.executers.ValidatorExecuter; -import org.openecomp.sdc.asdctool.impl.validator.utils.ReportManager; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.util.List; + /** * Created by chaya on 7/3/2017. */ @Component public class ValidationToolBL { - private static Logger log = LoggerFactory.getLogger(ValidationToolBL.class.getName()); + private static Logger log = Logger.getLogger(ValidationToolBL.class.getName()); @Autowired protected List validators; @@ -24,13 +22,13 @@ public class ValidationToolBL { public boolean validateAll() { for (ValidatorExecuter validatorExec: validators) { - System.out.println("ValidatorExecuter "+validatorExec.getName()+" started"); + log.debug("ValidatorExecuter "+validatorExec.getName()+" started"); if (!validatorExec.executeValidations()) { allValid = false; - System.out.println("ValidatorExecuter "+validatorExec.getName()+" finished with warnings"); + log.debug("ValidatorExecuter "+validatorExec.getName()+" finished with warnings"); } else { - System.out.println("ValidatorExecuter "+validatorExec.getName()+" finished successfully"); + log.debug("ValidatorExecuter "+validatorExec.getName()+" finished successfully"); } } return allValid; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/config/ValidationToolConfiguration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/config/ValidationToolConfiguration.java index 63e95d568b..eb2574405f 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/config/ValidationToolConfiguration.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/config/ValidationToolConfiguration.java @@ -2,11 +2,7 @@ package org.openecomp.sdc.asdctool.impl.validator.config; import org.openecomp.sdc.asdctool.impl.validator.ArtifactToolBL; import org.openecomp.sdc.asdctool.impl.validator.ValidationToolBL; -import org.openecomp.sdc.asdctool.impl.validator.executers.NodeToscaArtifactsValidatorExecuter; -import org.openecomp.sdc.asdctool.impl.validator.executers.ServiceToscaArtifactsValidatorExecutor; -import org.openecomp.sdc.asdctool.impl.validator.executers.ServiceValidatorExecuter; -import org.openecomp.sdc.asdctool.impl.validator.executers.VFToscaArtifactValidatorExecutor; -import org.openecomp.sdc.asdctool.impl.validator.executers.VfValidatorExecuter; +import org.openecomp.sdc.asdctool.impl.validator.executers.*; import org.openecomp.sdc.asdctool.impl.validator.tasks.artifacts.ArtifactValidationUtils; import org.openecomp.sdc.asdctool.impl.validator.tasks.artifacts.ServiceArtifactValidationTask; import org.openecomp.sdc.asdctool.impl.validator.tasks.artifacts.VfArtifactValidationTask; @@ -19,16 +15,7 @@ import org.openecomp.sdc.be.dao.cassandra.CassandraClient; import org.openecomp.sdc.be.dao.jsongraph.TitanDao; import org.openecomp.sdc.be.dao.titan.TitanGraphClient; import org.openecomp.sdc.be.model.DerivedNodeTypeResolver; -import org.openecomp.sdc.be.model.jsontitan.operations.ArtifactsOperations; -import org.openecomp.sdc.be.model.jsontitan.operations.ByToscaNameDerivedNodeTypeResolver; -import org.openecomp.sdc.be.model.jsontitan.operations.CategoryOperation; -import org.openecomp.sdc.be.model.jsontitan.operations.GroupsOperation; -import org.openecomp.sdc.be.model.jsontitan.operations.NodeTemplateOperation; -import org.openecomp.sdc.be.model.jsontitan.operations.NodeTypeOperation; -import org.openecomp.sdc.be.model.jsontitan.operations.TopologyTemplateOperation; -import org.openecomp.sdc.be.model.jsontitan.operations.ToscaDataOperation; -import org.openecomp.sdc.be.model.jsontitan.operations.ToscaElementLifecycleOperation; -import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade; +import org.openecomp.sdc.be.model.jsontitan.operations.*; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ArtifactValidatorExecuter.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ArtifactValidatorExecuter.java index 3585474398..089e9729d4 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ArtifactValidatorExecuter.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ArtifactValidatorExecuter.java @@ -1,20 +1,6 @@ package org.openecomp.sdc.asdctool.impl.validator.executers; -import java.io.BufferedWriter; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.Writer; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.stream.Collectors; - +import fj.data.Either; import org.openecomp.sdc.asdctool.impl.validator.config.ValidationConfigManager; import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; import org.openecomp.sdc.be.dao.jsongraph.TitanDao; @@ -26,113 +12,121 @@ import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.ComponentParametersView; import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.beans.factory.annotation.Autowired; -import fj.data.Either; - -public class ArtifactValidatorExecuter { - - @Autowired - protected TitanDao titanDao; - - @Autowired - private ToscaOperationFacade toscaOperationFacade; - private static Logger log = LoggerFactory.getLogger(ArtifactValidatorExecuter.class.getName()); - - protected String name; - - public void setName(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public Map> getVerticesToValidate(VertexTypeEnum type, - Map hasProps) { - - Map> result = new HashMap<>(); - Either, TitanOperationStatus> resultsEither = titanDao.getByCriteria(type, hasProps); - if (resultsEither.isRight()) { - System.out.println("getVerticesToValidate failed " + resultsEither.right().value()); - return result; - } - System.out.println("getVerticesToValidate: " + resultsEither.left().value().size() + " vertices to scan"); - List componentsList = resultsEither.left().value(); - componentsList.forEach(vertex -> { - String ivariantUuid = (String)vertex.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID); - if (!result.containsKey(ivariantUuid)) { - result.put(ivariantUuid, new ArrayList<>()); - } - List compList = result.get(ivariantUuid); - - ComponentParametersView filter = new ComponentParametersView(true); - filter.setIgnoreArtifacts(false); - - Either toscaElement - = toscaOperationFacade.getToscaElement(vertex.getUniqueId(), filter); - if (toscaElement.isRight()) { - System.out.println("getVerticesToValidate: failed to find element" - + vertex.getUniqueId() + " staus is" + toscaElement.right().value()); - } else { - compList.add(toscaElement.left().value()); - } - }); - return result; - } - - public boolean validate(Map> vertices) { - boolean result = true; - long time = System.currentTimeMillis(); - String fileName = ValidationConfigManager.getOutputFilePath() + this.getName() + "_" + time + ".csv"; - - try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"))) { - writer.write("name, UUID, invariantUUID, state, version\n"); - Collection> collection = vertices.values(); - for (List compList: collection) { - Set artifactEsId = new HashSet<>(); - for (Component component: compList) { - Map toscaArtifacts = component.getToscaArtifacts(); - Optional op = toscaArtifacts.values() - .stream().filter(a -> artifactEsId.contains(a.getEsId())).findAny(); - if (op.isPresent()) { - result = false; - writeModuleResultToFile(writer, compList); - writer.flush(); - break; - } else { - artifactEsId.addAll(toscaArtifacts.values() - .stream().map(ArtifactDefinition::getEsId).collect(Collectors.toList())); - } - } - } - } catch (Exception e) { - log.info("Failed to fetch vf resources ", e); - return false; - } finally { - titanDao.commit(); - } - return result; - } +import java.io.*; +import java.util.*; +import java.util.stream.Collectors; - private void writeModuleResultToFile(Writer writer, List components) { - try { - // "service name, service id, state, version - for (Component component: components ) { - StringBuilder sb = new StringBuilder(component.getName()); - sb.append(",").append(component.getUniqueId()) - .append(",").append(component.getInvariantUUID()) - .append(",").append(component.getLifecycleState()) - .append(",").append(component.getVersion()) - .append("\n"); +public class ArtifactValidatorExecuter{ + + @Autowired + protected TitanDao titanDao; + + @Autowired + private ToscaOperationFacade toscaOperationFacade; + private static Logger log = Logger.getLogger(ArtifactValidatorExecuter.class.getName()); + + protected String name; + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + + + public Map> getVerticesToValidate(VertexTypeEnum type, Map hasProps){ + Map> result = new HashMap<>(); + Either, TitanOperationStatus> resultsEither = titanDao.getByCriteria(type, hasProps); + if (resultsEither.isRight()) { + System.out.println("getVerticesToValidate failed "+ resultsEither.right().value()); + return result; + } + System.out.println("getVerticesToValidate: "+resultsEither.left().value().size()+" vertices to scan"); + List componentsList = resultsEither.left().value(); + componentsList.forEach(vertex -> { + String ivariantUuid = (String)vertex.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID); + if(!result.containsKey(ivariantUuid)){ + List compList = new ArrayList(); + result.put(ivariantUuid, compList); + } + List compList = result.get(ivariantUuid); + + ComponentParametersView filter = new ComponentParametersView(true); + filter.setIgnoreArtifacts(false); + + Either toscaElement = toscaOperationFacade.getToscaElement(vertex.getUniqueId(), filter); + if (toscaElement.isRight()) { + System.out.println("getVerticesToValidate: failed to find element"+ vertex.getUniqueId()+" staus is" + toscaElement.right().value()); + }else{ + compList.add(toscaElement.left().value()); + } + + }); + + return result; + } + + public boolean validate( Map> vertices) { + boolean result = true; + long time = System.currentTimeMillis(); + String fileName = ValidationConfigManager.getOutputFilePath() + this.getName() + "_"+ time + ".csv"; + Writer writer = null; + try { + writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8")); + writer.write("name, UUID, invariantUUID, state, version\n"); + Collection> collection = vertices.values(); + for(List compList: collection ){ + Set artifactEsId = new HashSet<>(); + for(Component component: compList ){ + Map toscaArtifacts = component.getToscaArtifacts(); + Optional op = toscaArtifacts.values(). + stream().filter(a -> artifactEsId.contains(a.getEsId())).findAny(); + if(op.isPresent()){ + result = false; + writeModuleResultToFile(writer, compList); + writer.flush(); + break; + }else{ + artifactEsId.addAll(toscaArtifacts.values().stream().map(ArtifactDefinition::getEsId).collect(Collectors.toList())) ; + } + } + + } + + } catch (Exception e) { + log.info("Failed to fetch vf resources ", e); + return false; + } finally { + titanDao.commit(); + try { + writer.flush(); + writer.close(); + } catch (Exception ex) { + /* ignore */} + } + return result; + } + + private void writeModuleResultToFile(Writer writer, List components) { + try { + // "service name, service id, state, version + for(Component component: components ){ + StringBuffer sb = new StringBuffer(component.getName()); + sb.append(",").append(component.getUniqueId()).append(",").append(component.getInvariantUUID()).append(",").append(component.getLifecycleState()).append(",").append(component.getVersion()); + + sb.append("\n"); + writer.write(sb.toString()); + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } - writer.write(sb.toString()); - } - } catch (IOException e) { - log.debug("Cannot write module result to file", e); - } - } } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/NodeToscaArtifactsValidatorExecuter.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/NodeToscaArtifactsValidatorExecuter.java index 6715c8a955..8e6ddabb7b 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/NodeToscaArtifactsValidatorExecuter.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/NodeToscaArtifactsValidatorExecuter.java @@ -1,15 +1,15 @@ package org.openecomp.sdc.asdctool.impl.validator.executers; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.LifecycleStateEnum; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + public class NodeToscaArtifactsValidatorExecuter extends ArtifactValidatorExecuter implements IArtifactValidatorExecuter{ protected String name; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ServiceToscaArtifactsValidatorExecutor.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ServiceToscaArtifactsValidatorExecutor.java index 2fe5abef3c..a6377fce50 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ServiceToscaArtifactsValidatorExecutor.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ServiceToscaArtifactsValidatorExecutor.java @@ -1,15 +1,15 @@ package org.openecomp.sdc.asdctool.impl.validator.executers; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.LifecycleStateEnum; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + public class ServiceToscaArtifactsValidatorExecutor extends ArtifactValidatorExecuter implements IArtifactValidatorExecuter{ diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ServiceValidatorExecuter.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ServiceValidatorExecuter.java index b64fdde8d7..efad4bb392 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ServiceValidatorExecuter.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ServiceValidatorExecuter.java @@ -1,15 +1,13 @@ package org.openecomp.sdc.asdctool.impl.validator.executers; -import java.util.ArrayList; -import java.util.List; - import org.openecomp.sdc.asdctool.impl.validator.tasks.ServiceValidationTask; import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import java.util.ArrayList; +import java.util.List; + /** * Created by chaya on 7/4/2017. */ @@ -18,8 +16,6 @@ public class ServiceValidatorExecuter extends TopologyTemplateValidatorExecuter @Autowired(required = false) List tasks = new ArrayList<>(); - private static Logger log = LoggerFactory.getLogger(VfValidatorExecuter.class.getName()); - public ServiceValidatorExecuter() { setName("SERVICE_VALIDATOR"); } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/TopologyTemplateValidatorExecuter.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/TopologyTemplateValidatorExecuter.java index f875aa4742..a10d3193b2 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/TopologyTemplateValidatorExecuter.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/TopologyTemplateValidatorExecuter.java @@ -1,12 +1,6 @@ package org.openecomp.sdc.asdctool.impl.validator.executers; -import java.util.ArrayList; -import java.util.EnumMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - +import fj.data.Either; import org.openecomp.sdc.asdctool.impl.validator.tasks.TopologyTemplateValidationTask; import org.openecomp.sdc.asdctool.impl.validator.utils.ReportManager; import org.openecomp.sdc.asdctool.impl.validator.utils.VertexResult; @@ -17,19 +11,17 @@ import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; -import org.openecomp.sdc.be.model.jsontitan.operations.TopologyTemplateOperation; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.beans.factory.annotation.Autowired; -import fj.data.Either; +import java.util.*; /** * Created by chaya on 7/3/2017. */ public class TopologyTemplateValidatorExecuter { - private static Logger log = LoggerFactory.getLogger(VfValidatorExecuter.class.getName()); + private static Logger log = Logger.getLogger(VfValidatorExecuter.class.getName()); @Autowired protected TitanDao titanDao; @@ -53,10 +45,10 @@ public class TopologyTemplateValidatorExecuter { Either, TitanOperationStatus> results = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, props); if (results.isRight()) { - System.out.println("getVerticesToValidate failed "+ results.right().value()); + log.error("getVerticesToValidate failed "+ results.right().value()); return new ArrayList<>(); } - System.out.println("getVerticesToValidate: "+results.left().value().size()+" vertices to scan"); + log.info("getVerticesToValidate: "+results.left().value().size()+" vertices to scan"); return results.left().value(); } @@ -85,7 +77,7 @@ public class TopologyTemplateValidatorExecuter { ReportManager.reportTaskEnd(vertex.getUniqueId(), task.getTaskName(), result); } String componentScanStatus = successAllTasks? "success" : "failed"; - System.out.println("Topology Template "+vertex.getUniqueId()+" Validation finished with "+componentScanStatus); + log.info("Topology Template "+vertex.getUniqueId()+" Validation finished with "+componentScanStatus); } ReportManager.reportValidatorTypeSummary(getName(), failedTasks, successTasks); return successAllVertices; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/VFToscaArtifactValidatorExecutor.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/VFToscaArtifactValidatorExecutor.java index bc22f2e424..5f1413f992 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/VFToscaArtifactValidatorExecutor.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/VFToscaArtifactValidatorExecutor.java @@ -1,9 +1,5 @@ package org.openecomp.sdc.asdctool.impl.validator.executers; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; @@ -11,6 +7,10 @@ import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.LifecycleStateEnum; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + public class VFToscaArtifactValidatorExecutor extends ArtifactValidatorExecuter implements IArtifactValidatorExecuter{ public VFToscaArtifactValidatorExecutor() { diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/VfValidatorExecuter.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/VfValidatorExecuter.java index b4f333dab3..221e665d4a 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/VfValidatorExecuter.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/VfValidatorExecuter.java @@ -1,13 +1,13 @@ package org.openecomp.sdc.asdctool.impl.validator.executers; -import java.util.ArrayList; -import java.util.List; - import org.openecomp.sdc.asdctool.impl.validator.tasks.VfValidationTask; import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.springframework.beans.factory.annotation.Autowired; +import java.util.ArrayList; +import java.util.List; + /** * Created by chaya on 7/3/2017. */ diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/artifacts/ArtifactValidationUtils.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/artifacts/ArtifactValidationUtils.java index 90d8f8639c..faccd647c2 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/artifacts/ArtifactValidationUtils.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/artifacts/ArtifactValidationUtils.java @@ -1,11 +1,6 @@ package org.openecomp.sdc.asdctool.impl.validator.tasks.artifacts; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Optional; - +import fj.data.Either; import org.openecomp.sdc.asdctool.impl.validator.utils.ReportManager; import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao; import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus; @@ -19,7 +14,7 @@ import org.openecomp.sdc.be.model.jsontitan.operations.TopologyTemplateOperation import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.springframework.beans.factory.annotation.Autowired; -import fj.data.Either; +import java.util.*; /** * Created by chaya on 7/6/2017. diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/artifacts/ArtifactsVertexResult.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/artifacts/ArtifactsVertexResult.java index c82a7b9d4f..c010148644 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/artifacts/ArtifactsVertexResult.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/artifacts/ArtifactsVertexResult.java @@ -1,10 +1,10 @@ package org.openecomp.sdc.asdctool.impl.validator.tasks.artifacts; +import org.openecomp.sdc.asdctool.impl.validator.utils.VertexResult; + import java.util.HashSet; import java.util.Set; -import org.openecomp.sdc.asdctool.impl.validator.utils.VertexResult; - /** * Created by chaya on 7/25/2017. */ diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/artifacts/VfArtifactValidationTask.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/artifacts/VfArtifactValidationTask.java index 1c18fb751e..ed2ceb947b 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/artifacts/VfArtifactValidationTask.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/artifacts/VfArtifactValidationTask.java @@ -3,7 +3,6 @@ package org.openecomp.sdc.asdctool.impl.validator.tasks.artifacts; import org.openecomp.sdc.asdctool.impl.validator.tasks.VfValidationTask; import org.openecomp.sdc.asdctool.impl.validator.utils.VertexResult; import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; -import org.openecomp.sdc.be.model.jsontitan.operations.TopologyTemplateOperation; import org.springframework.beans.factory.annotation.Autowired; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/moduleJson/ModuleJsonTask.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/moduleJson/ModuleJsonTask.java index 78045a56a6..272b546a5d 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/moduleJson/ModuleJsonTask.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/moduleJson/ModuleJsonTask.java @@ -1,13 +1,6 @@ package org.openecomp.sdc.asdctool.impl.validator.tasks.moduleJson; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.stream.Collectors; - +import fj.data.Either; import org.openecomp.sdc.asdctool.impl.validator.tasks.ServiceValidationTask; import org.openecomp.sdc.asdctool.impl.validator.utils.ReportManager; import org.openecomp.sdc.asdctool.impl.validator.utils.VertexResult; @@ -24,7 +17,8 @@ import org.openecomp.sdc.be.model.jsontitan.operations.TopologyTemplateOperation import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.springframework.beans.factory.annotation.Autowired; -import fj.data.Either; +import java.util.*; +import java.util.stream.Collectors; /** * Created by chaya on 7/18/2017. diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/utils/ElementTypeEnum.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/utils/ElementTypeEnum.java index e6a09ee875..245d38c853 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/utils/ElementTypeEnum.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/utils/ElementTypeEnum.java @@ -1,10 +1,10 @@ package org.openecomp.sdc.asdctool.impl.validator.utils; +import org.openecomp.sdc.asdctool.impl.validator.executers.VfValidatorExecuter; + import java.util.ArrayList; import java.util.List; -import org.openecomp.sdc.asdctool.impl.validator.executers.VfValidatorExecuter; - /** * Created by chaya on 7/4/2017. */ diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/utils/ReportManager.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/utils/ReportManager.java index ebedf0a568..88a114cb63 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/utils/ReportManager.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/utils/ReportManager.java @@ -1,18 +1,14 @@ package org.openecomp.sdc.asdctool.impl.validator.utils; +import org.apache.commons.lang.text.StrBuilder; +import org.openecomp.sdc.asdctool.impl.validator.config.ValidationConfigManager; +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; + import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Optional; -import java.util.Set; - -import org.apache.commons.lang.text.StrBuilder; -import org.openecomp.sdc.asdctool.impl.validator.config.ValidationConfigManager; -import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; +import java.util.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ArtifactUUIDFixMenu.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ArtifactUUIDFixMenu.java index a2e59464c0..def9ed2c8a 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ArtifactUUIDFixMenu.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ArtifactUUIDFixMenu.java @@ -3,13 +3,12 @@ package org.openecomp.sdc.asdctool.main; import org.openecomp.sdc.asdctool.configuration.ArtifactUUIDFixConfiguration; import org.openecomp.sdc.asdctool.configuration.ConfigurationUploader; import org.openecomp.sdc.asdctool.impl.ArtifactUuidFix; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class ArtifactUUIDFixMenu { - private static Logger log = LoggerFactory.getLogger(ArtifactUUIDFixMenu.class); + private static Logger log = Logger.getLogger(ArtifactUUIDFixMenu.class.getName()); public static void main(String[] args) { if (args == null || args.length < 3) { diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ArtifactValidatorTool.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ArtifactValidatorTool.java index ba8f3be4e9..2e84923188 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ArtifactValidatorTool.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ArtifactValidatorTool.java @@ -7,12 +7,11 @@ import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.common.api.ConfigurationSource; import org.openecomp.sdc.common.impl.ExternalConfiguration; import org.openecomp.sdc.common.impl.FSConfigurationSource; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class ArtifactValidatorTool { - private static Logger log = LoggerFactory.getLogger(ValidationTool.class.getName()); + private static Logger log = Logger.getLogger(ValidationTool.class.getName()); public static void main(String[] args) { diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/CsarGeneratorTool.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/CsarGeneratorTool.java new file mode 100644 index 0000000000..e2b35ad739 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/CsarGeneratorTool.java @@ -0,0 +1,45 @@ +package org.openecomp.sdc.asdctool.main; + +import java.util.Scanner; + +import org.openecomp.sdc.asdctool.configuration.ConfigurationUploader; +import org.openecomp.sdc.asdctool.configuration.CsarGeneratorConfiguration; +import org.openecomp.sdc.asdctool.impl.internal.tool.CsarGenerator; +import org.openecomp.sdc.asdctool.utils.ConsoleWriter; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +public class CsarGeneratorTool extends SdcInternalTool { + + public static void main(String[] args) { + if (args == null) { + ConsoleWriter.dataLine("Usage: "); + System.exit(1); + } + String appConfigDir = args[0]; + + disableConsole(); + + ConfigurationUploader.uploadConfigurationFiles(appConfigDir); + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(CsarGeneratorConfiguration.class); + CsarGenerator csarGenerator = context.getBean(CsarGenerator.class); + ConsoleWriter.dataLine("STARTED... "); + + String input = ""; + Scanner scanner = new Scanner(System.in); + do { + ConsoleWriter.dataLine("Enter next service UUID or exit: "); + input = scanner.nextLine(); + if (!input.equals("exit")) { + if (!input.isEmpty()) { + ConsoleWriter.dataLine("Your UUID is ", input); + csarGenerator.generateCsar(input, scanner); + } else { + ConsoleWriter.dataLine("Your UUID is empty. Try again."); + } + } + } while (!input.equals("exit")); + csarGenerator.closeAll(); + ConsoleWriter.dataLine("CsarGeneratorTool exit..."); + System.exit(0); + } +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/DataSchemaMenu.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/DataSchemaMenu.java index 665a9b75d1..496eb1864e 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/DataSchemaMenu.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/DataSchemaMenu.java @@ -26,12 +26,11 @@ import org.openecomp.sdc.be.dao.cassandra.schema.SdcSchemaBuilder; import org.openecomp.sdc.common.api.ConfigurationSource; import org.openecomp.sdc.common.impl.ExternalConfiguration; import org.openecomp.sdc.common.impl.FSConfigurationSource; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.common.log.wrappers.Logger; public class DataSchemaMenu { - private static Logger log = LoggerFactory.getLogger(DataSchemaMenu.class.getName()); + private static Logger log = Logger.getLogger(DataSchemaMenu.class.getName()); public static void main(String[] args) { diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/DeleteComponentTool.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/DeleteComponentTool.java new file mode 100644 index 0000000000..8ef1522f86 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/DeleteComponentTool.java @@ -0,0 +1,55 @@ +package org.openecomp.sdc.asdctool.main; + +import java.util.Scanner; + +import org.openecomp.sdc.asdctool.configuration.ConfigurationUploader; +import org.openecomp.sdc.asdctool.configuration.InternalToolConfiguration; +import org.openecomp.sdc.asdctool.impl.internal.tool.DeleteComponentHandler; +import org.openecomp.sdc.asdctool.utils.ConsoleWriter; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +public class DeleteComponentTool extends SdcInternalTool{ + private static final String PSW = "ItIsTimeToDelete"; + + public static void main(String[] args) { + if (args == null || args.length < 2) { + ConsoleWriter.dataLine("Usage: "); + System.exit(1); + } + String appConfigDir = args[0]; + String password = args[1]; + + if ( !PSW.equals(password) ){ + ConsoleWriter.dataLine("Wrong password"); + System.exit(1); + } + + disableConsole(); + ConsoleWriter.dataLine("STARTED... "); + + ConfigurationUploader.uploadConfigurationFiles(appConfigDir); + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(InternalToolConfiguration.class); + DeleteComponentHandler deleteComponentHandler = context.getBean(DeleteComponentHandler.class); + + + String input = ""; + Scanner scanner = new Scanner(System.in); + do { + ConsoleWriter.dataLine("Enter next component unique id or exit: "); + input = scanner.nextLine(); + if (!input.equals("exit")) { + if (!input.isEmpty()) { + ConsoleWriter.dataLine("Your id is " ,input); + deleteComponentHandler.deleteComponent(input, scanner); + }else{ + ConsoleWriter.dataLine("Your id is empty. Try again."); + } + } + } while (!input.equals("exit")); + deleteComponentHandler.closeAll(); + ConsoleWriter.dataLine("DeleteComponentTool exit..."); + System.exit(0); + } + + +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/EsToCassandraDataMigrationMenu.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/EsToCassandraDataMigrationMenu.java index 873bdb1496..c119d7e1ff 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/EsToCassandraDataMigrationMenu.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/EsToCassandraDataMigrationMenu.java @@ -26,13 +26,12 @@ import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.common.api.ConfigurationSource; import org.openecomp.sdc.common.impl.ExternalConfiguration; import org.openecomp.sdc.common.impl.FSConfigurationSource; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class EsToCassandraDataMigrationMenu { - private static Logger log = LoggerFactory.getLogger(EsToCassandraDataMigrationMenu.class.getName()); + private static Logger log = Logger.getLogger(EsToCassandraDataMigrationMenu.class.getName()); public static void main(String[] args) { @@ -55,7 +54,7 @@ public class EsToCassandraDataMigrationMenu { case "es-to-cassndra-migration": dataMigration = (DataMigration) context.getBean("DataMigrationBean"); log.debug("Start migration from ES to C* "); - if (dataMigration.migrateDataEsToCassandra(appConfigDir, true, true)) { + if (dataMigration.migrateDataESToCassndra(appConfigDir, true, true)) { log.debug("migration from ES to C* was finished successfull"); System.exit(0); } else { @@ -66,7 +65,7 @@ public class EsToCassandraDataMigrationMenu { case "es-to-cassndra-migration-export-only": dataMigration = (DataMigration) context.getBean("DataMigrationBean"); log.debug("Start migration export only from ES to C* "); - if (dataMigration.migrateDataEsToCassandra(appConfigDir, true, false)) { + if (dataMigration.migrateDataESToCassndra(appConfigDir, true, false)) { log.debug("migration export only from ES to C* was finished successfull"); System.exit(0); } else { @@ -77,7 +76,7 @@ public class EsToCassandraDataMigrationMenu { case "es-to-cassndra-migration-import-only": dataMigration = (DataMigration) context.getBean("DataMigrationBean"); log.debug("Start migration import only from ES to C* "); - if (dataMigration.migrateDataEsToCassandra(appConfigDir, false, true)) { + if (dataMigration.migrateDataESToCassndra(appConfigDir, false, true)) { log.debug("migration import only from ES to C* was finished successfull"); System.exit(0); } else { diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/GetConsumersMenu.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/GetConsumersMenu.java index 733cc5cc94..189348acc6 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/GetConsumersMenu.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/GetConsumersMenu.java @@ -1,7 +1,6 @@ package org.openecomp.sdc.asdctool.main; -import java.util.List; - +import fj.data.Either; import org.openecomp.sdc.asdctool.cli.CLIToolData; import org.openecomp.sdc.asdctool.cli.SpringCLITool; import org.openecomp.sdc.asdctool.configuration.GetConsumersConfiguration; @@ -11,7 +10,7 @@ import org.openecomp.sdc.be.resources.data.ConsumerData; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import fj.data.Either; +import java.util.List; public class GetConsumersMenu extends SpringCLITool { diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcInternalTool.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcInternalTool.java new file mode 100644 index 0000000000..9c2c62fb8f --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcInternalTool.java @@ -0,0 +1,16 @@ +package org.openecomp.sdc.asdctool.main; + +import org.openecomp.sdc.common.log.wrappers.Logger; +import org.slf4j.LoggerFactory; + +import ch.qos.logback.core.Appender; + +public abstract class SdcInternalTool { + protected static void disableConsole() { + org.slf4j.Logger rootLogger = LoggerFactory.getILoggerFactory().getLogger(Logger.ROOT_LOGGER_NAME); + Appender appender = ((ch.qos.logback.classic.Logger) rootLogger).getAppender("STDOUT"); + if (appender != null) { + appender.stop(); + } + } +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcSchemaFileImport.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcSchemaFileImport.java index e915d27502..d2fa3a41fc 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcSchemaFileImport.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcSchemaFileImport.java @@ -20,21 +20,6 @@ package org.openecomp.sdc.asdctool.main; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.FileSystems; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Date; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.zip.ZipEntry; -import java.util.zip.ZipOutputStream; - import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.ArrayUtils; import org.openecomp.sdc.asdctool.enums.SchemaZipFileEnum; @@ -50,6 +35,16 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; +import java.io.*; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Date; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + public class SdcSchemaFileImport { diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/UpdateIsVnfMenu.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/UpdateIsVnfMenu.java index b9a1e1741b..ff548ff71a 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/UpdateIsVnfMenu.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/UpdateIsVnfMenu.java @@ -20,20 +20,19 @@ package org.openecomp.sdc.asdctool.main; +import org.openecomp.sdc.asdctool.impl.UpdatePropertyOnVertex; +import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary; +import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; +import org.openecomp.sdc.common.log.wrappers.Logger; + import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import org.openecomp.sdc.asdctool.impl.UpdatePropertyOnVertex; -import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary; -import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - public class UpdateIsVnfMenu { - private static Logger log = LoggerFactory.getLogger(UpdateIsVnfMenu.class.getName()); + private static Logger log = Logger.getLogger(UpdateIsVnfMenu.class.getName()); private static void usageAndExit() { updateIsVnfTrueUsage(); diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ValidationTool.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ValidationTool.java index e07b3af551..f55120e37c 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ValidationTool.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ValidationTool.java @@ -8,8 +8,7 @@ import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.common.api.ConfigurationSource; import org.openecomp.sdc.common.impl.ExternalConfiguration; import org.openecomp.sdc.common.impl.FSConfigurationSource; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** @@ -17,7 +16,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext */ public class ValidationTool { - private static Logger log = LoggerFactory.getLogger(ValidationTool.class.getName()); + private static Logger log = Logger.getLogger(ValidationTool.class.getName()); public static void main(String[] args) { @@ -29,14 +28,14 @@ public class ValidationTool { AnnotationConfigApplicationContext context = initContext(appConfigDir); ValidationToolBL validationToolBL = context.getBean(ValidationToolBL.class); - System.out.println("Start Validation Tool"); + log.info("Start Validation Tool"); Boolean result = validationToolBL.validateAll(); ReportManager.reportEndOfToolRun(); if (result) { - System.out.println("Validation finished successfully"); + log.info("Validation finished successfully"); System.exit(0); } else { - System.out.println("Validation finished with warnings"); + log.info("Validation finished with warnings"); System.exit(2); } } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/VrfObjectFixMenu.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/VrfObjectFixMenu.java new file mode 100644 index 0000000000..cf159ccf24 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/VrfObjectFixMenu.java @@ -0,0 +1,52 @@ +package org.openecomp.sdc.asdctool.main; + +import org.openecomp.sdc.asdctool.configuration.VrfObjectFixConfiguration; +import org.openecomp.sdc.asdctool.impl.VrfObjectFixHandler; +import org.openecomp.sdc.be.config.ConfigurationManager; +import org.openecomp.sdc.common.api.ConfigurationSource; +import org.openecomp.sdc.common.impl.ExternalConfiguration; +import org.openecomp.sdc.common.impl.FSConfigurationSource; +import org.openecomp.sdc.common.log.wrappers.Logger; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import java.util.Arrays; + +public class VrfObjectFixMenu { + + private static final Logger log = Logger.getLogger(VrfObjectFixMenu.class); + + private VrfObjectFixMenu(){} + + public static void main(String[] args) { + if (isNotValidArguments(args)) { + log.debug("#main - The invalid array of the arguments have been received: {}", Arrays.toString(args)); + log.debug("#main - Usage: <'detect'/'fix'> "); + System.exit(1); + } + initConfig(args[0]); + VrfObjectFixHandler vrfObjectFixHandler = getVrfObjectFixHandler(); + if (vrfObjectFixHandler.handle(args[1], args.length == 3 ? args[2] : null)) { + log.info("#main - The {} operation of the corrupted VRFObject Node Types has been finished successfully", args[1]); + } else{ + log.info("#main - The {} operation of the corrupted VRFObject Node Types has been failed", args[1]); + System.exit(2); + } + System.exit(0); + } + + private static VrfObjectFixHandler getVrfObjectFixHandler() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(VrfObjectFixConfiguration.class); + return context.getBean(VrfObjectFixHandler.class); + } + + private static boolean isNotValidArguments(String[] args) { + return args == null || args.length < 2; + } + + + private static void initConfig(String configDir) { + ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), configDir); + new ConfigurationManager(configurationSource); + } + +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/config/MigrationSpringConfig.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/config/MigrationSpringConfig.java index f192d87867..a18d644f08 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/config/MigrationSpringConfig.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/config/MigrationSpringConfig.java @@ -1,8 +1,5 @@ package org.openecomp.sdc.asdctool.migration.config; -import java.util.ArrayList; -import java.util.List; - import org.openecomp.sdc.asdctool.migration.core.SdcMigrationTool; import org.openecomp.sdc.asdctool.migration.core.task.Migration; import org.openecomp.sdc.asdctool.migration.core.task.PostMigration; @@ -11,6 +8,7 @@ import org.openecomp.sdc.asdctool.migration.resolver.MigrationResolver; import org.openecomp.sdc.asdctool.migration.resolver.SpringBeansMigrationResolver; import org.openecomp.sdc.asdctool.migration.service.SdcRepoService; import org.openecomp.sdc.be.components.distribution.engine.ServiceDistributionArtifactsBuilder; +import org.openecomp.sdc.be.components.scheduledtasks.ComponentsCleanBusinessLogic; import org.openecomp.sdc.be.config.CatalogModelSpringConfig; import org.openecomp.sdc.be.dao.config.DAOSpringConfig; import org.openecomp.sdc.config.CatalogBESpringConfig; @@ -22,6 +20,9 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.core.io.FileSystemResource; +import java.util.ArrayList; +import java.util.List; + @Configuration @Import({DAOSpringConfig.class, CatalogBESpringConfig.class, CatalogModelSpringConfig.class}) @ComponentScan({"org.openecomp.sdc.asdctool.migration.tasks",//migration tasks @@ -68,4 +69,7 @@ public class MigrationSpringConfig { return bean; } + @Bean(name = "componentsCleanBusinessLogic") + public ComponentsCleanBusinessLogic componentsCleanBusinessLogic() {return new ComponentsCleanBusinessLogic(); } + } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/config/mocks/DistributionEngineMock.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/config/mocks/DistributionEngineMock.java index aa27d6e435..65e508c743 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/config/mocks/DistributionEngineMock.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/config/mocks/DistributionEngineMock.java @@ -4,6 +4,7 @@ import org.openecomp.sdc.be.components.distribution.engine.IDistributionEngine; import org.openecomp.sdc.be.components.distribution.engine.INotificationData; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.resources.data.OperationalEnvironmentEntry; import org.springframework.stereotype.Component; @@ -16,12 +17,12 @@ public class DistributionEngineMock implements IDistributionEngine { } @Override - public ActionStatus notifyService(String distributionId, Service service, INotificationData notificationData, String envName, String userId, String modifierName) { + public ActionStatus notifyService(String distributionId, Service service, INotificationData notificationData, String envName, User modifier) { return null; } @Override - public ActionStatus notifyService(String distributionId, Service service, INotificationData notificationData, String envId, String envName, String userId, String modifierName) { + public ActionStatus notifyService(String distributionId, Service service, INotificationData notificationData, String envId, String envName, User modifier) { return null; } @@ -41,7 +42,7 @@ public class DistributionEngineMock implements IDistributionEngine { } @Override - public StorageOperationStatus isReadyForDistribution(Service service, String envName) { + public StorageOperationStatus isReadyForDistribution(String envName) { return null; } @@ -50,11 +51,6 @@ public class DistributionEngineMock implements IDistributionEngine { return null; } - @Override - public StorageOperationStatus verifyServiceHasDeploymentArtifacts(Service service) { - return null; - } - @Override public OperationalEnvironmentEntry getEnvironmentById(String opEnvId) { return null; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/config/mocks/HealthCheckBusinessLogicMock.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/config/mocks/HealthCheckBusinessLogicMock.java index 5e4f15ca7e..c7ef45e712 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/config/mocks/HealthCheckBusinessLogicMock.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/config/mocks/HealthCheckBusinessLogicMock.java @@ -1,10 +1,10 @@ package org.openecomp.sdc.asdctool.migration.config.mocks; -import javax.annotation.PostConstruct; - import org.openecomp.sdc.be.components.health.HealthCheckBusinessLogic; import org.springframework.stereotype.Component; +import javax.annotation.PostConstruct; + @Component("healthCheckBusinessLogic") public class HealthCheckBusinessLogicMock extends HealthCheckBusinessLogic { diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/DBVersion.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/DBVersion.java index 03d5adfc15..003a27a1e4 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/DBVersion.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/DBVersion.java @@ -13,7 +13,7 @@ public class DBVersion implements Comparable{ /** * The current db version. should be tested against real db to verify it is compatible to the db version */ - public static final DBVersion CURRENT_VERSION = new DBVersion(1802, 0); + public static final DBVersion CURRENT_VERSION = new DBVersion(1710, 0); private DBVersion(BigInteger major, BigInteger minor) { this.major = major; @@ -62,12 +62,8 @@ public class DBVersion implements Comparable{ @Override public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; DBVersion dbVersion = (DBVersion) o; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/SdcMigrationTool.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/SdcMigrationTool.java index b8e2347970..2435601e7c 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/SdcMigrationTool.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/SdcMigrationTool.java @@ -1,7 +1,5 @@ package org.openecomp.sdc.asdctool.migration.core; -import java.util.List; - import org.openecomp.sdc.asdctool.migration.core.execution.MigrationExecutionResult; import org.openecomp.sdc.asdctool.migration.core.execution.MigrationExecutorImpl; import org.openecomp.sdc.asdctool.migration.core.task.IMigrationStage; @@ -9,12 +7,13 @@ import org.openecomp.sdc.asdctool.migration.core.task.IMigrationStage.AspectMigr import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult; import org.openecomp.sdc.asdctool.migration.resolver.MigrationResolver; import org.openecomp.sdc.asdctool.migration.service.SdcRepoService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.common.log.wrappers.Logger; + +import java.util.List; public class SdcMigrationTool { - private static final Logger LOGGER = LoggerFactory.getLogger(SdcMigrationTool.class); + private static final Logger LOGGER = Logger.getLogger(SdcMigrationTool.class); private MigrationResolver migrationsResolver; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/execution/MigrationExecutionResult.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/execution/MigrationExecutionResult.java index aa871914d6..accd9eca53 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/execution/MigrationExecutionResult.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/execution/MigrationExecutionResult.java @@ -1,11 +1,11 @@ package org.openecomp.sdc.asdctool.migration.core.execution; -import java.util.Date; - import org.openecomp.sdc.asdctool.migration.core.DBVersion; import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult; import org.openecomp.sdc.be.resources.data.MigrationTaskEntry; +import java.util.Date; + public class MigrationExecutionResult { private MigrationResult.MigrationStatus migrationStatus; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/execution/MigrationExecutorImpl.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/execution/MigrationExecutorImpl.java index 2b3b28a737..fd9ac14944 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/execution/MigrationExecutorImpl.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/execution/MigrationExecutorImpl.java @@ -3,14 +3,13 @@ package org.openecomp.sdc.asdctool.migration.core.execution; import org.openecomp.sdc.asdctool.migration.core.MigrationException; import org.openecomp.sdc.asdctool.migration.core.task.IMigrationStage; import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.util.StopWatch; public class MigrationExecutorImpl implements MigrationExecutor { - private static final Logger LOGGER = LoggerFactory.getLogger(MigrationExecutorImpl.class); + private static final Logger LOGGER = Logger.getLogger(MigrationExecutorImpl.class); @Override public MigrationExecutionResult execute(IMigrationStage migration) throws MigrationException { diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/dao/MigrationTasksDao.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/dao/MigrationTasksDao.java index b0bfabb059..e1144c14b5 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/dao/MigrationTasksDao.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/dao/MigrationTasksDao.java @@ -1,28 +1,24 @@ package org.openecomp.sdc.asdctool.migration.dao; -import java.math.BigInteger; - -import javax.annotation.PostConstruct; - -import org.apache.commons.lang3.tuple.ImmutablePair; -import org.openecomp.sdc.be.dao.cassandra.CassandraDao; -import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus; -import org.openecomp.sdc.be.resources.data.MigrationTaskEntry; -import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Row; import com.datastax.driver.core.Session; import com.datastax.driver.mapping.Mapper; import com.datastax.driver.mapping.MappingManager; - import fj.data.Either; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.openecomp.sdc.be.dao.cassandra.CassandraDao; +import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus; +import org.openecomp.sdc.be.resources.data.MigrationTaskEntry; +import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants; +import org.openecomp.sdc.common.log.wrappers.Logger; + +import javax.annotation.PostConstruct; +import java.math.BigInteger; public class MigrationTasksDao extends CassandraDao { - private static Logger logger = LoggerFactory.getLogger(MigrationTasksDao.class.getName()); + private static Logger logger = Logger.getLogger(MigrationTasksDao.class.getName()); private MigrationTasksAccessor migrationTasksAccessor; private Mapper migrationTaskMapper; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/main/MigrationMenu.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/main/MigrationMenu.java index de6c66d2d7..92c9c3f83c 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/main/MigrationMenu.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/main/MigrationMenu.java @@ -6,13 +6,12 @@ import org.openecomp.sdc.asdctool.cli.CLIToolData; import org.openecomp.sdc.asdctool.cli.SpringCLITool; import org.openecomp.sdc.asdctool.migration.config.MigrationSpringConfig; import org.openecomp.sdc.asdctool.migration.core.SdcMigrationTool; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.context.support.AbstractApplicationContext; public class MigrationMenu extends SpringCLITool { - private static final Logger LOGGER = LoggerFactory.getLogger(MigrationMenu.class); + private static final Logger LOGGER = Logger.getLogger(MigrationMenu.class); public static void main(String[] args) { MigrationMenu migrationMenu = new MigrationMenu(); diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/resolver/MigrationResolver.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/resolver/MigrationResolver.java index 22add31eb4..91511117d0 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/resolver/MigrationResolver.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/resolver/MigrationResolver.java @@ -1,10 +1,10 @@ package org.openecomp.sdc.asdctool.migration.resolver; -import java.util.List; - import org.openecomp.sdc.asdctool.migration.core.task.IMigrationStage; +import java.util.List; + public interface MigrationResolver { /** diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/resolver/SpringBeansMigrationResolver.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/resolver/SpringBeansMigrationResolver.java index 182996f5e4..b70c9b1ab7 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/resolver/SpringBeansMigrationResolver.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/resolver/SpringBeansMigrationResolver.java @@ -1,17 +1,17 @@ package org.openecomp.sdc.asdctool.migration.resolver; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.List; -import java.util.stream.Collectors; - import org.openecomp.sdc.asdctool.migration.core.DBVersion; import org.openecomp.sdc.asdctool.migration.core.task.IMigrationStage; import org.openecomp.sdc.asdctool.migration.core.task.Migration; import org.openecomp.sdc.asdctool.migration.core.task.PostMigration; import org.openecomp.sdc.asdctool.migration.service.SdcRepoService; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; + public class SpringBeansMigrationResolver implements MigrationResolver { private List migrations = new ArrayList<>(); diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/service/SdcRepoService.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/service/SdcRepoService.java index cf4affb345..20451f2c63 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/service/SdcRepoService.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/service/SdcRepoService.java @@ -1,11 +1,11 @@ package org.openecomp.sdc.asdctool.migration.service; -import java.math.BigInteger; - import org.openecomp.sdc.asdctool.migration.core.DBVersion; import org.openecomp.sdc.asdctool.migration.dao.MigrationTasksDao; import org.openecomp.sdc.be.resources.data.MigrationTaskEntry; +import java.math.BigInteger; + public class SdcRepoService { private MigrationTasksDao migrationTasksDao; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/handlers/OutputHandler.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/handlers/OutputHandler.java index 59259219db..418201984b 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/handlers/OutputHandler.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/handlers/OutputHandler.java @@ -2,8 +2,8 @@ package org.openecomp.sdc.asdctool.migration.tasks.handlers; public interface OutputHandler { - public void initiate(Object... title); + public void initiate(String name, Object... title); public void addRecord(Object... record); - public boolean writeOutput(); + public boolean writeOutputAndCloseFile(); } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/handlers/XlsOutputHandler.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/handlers/XlsOutputHandler.java index 0ce03b14e9..b5ea6dd0a9 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/handlers/XlsOutputHandler.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/handlers/XlsOutputHandler.java @@ -1,65 +1,92 @@ package org.openecomp.sdc.asdctool.migration.tasks.handlers; -import java.io.FileOutputStream; -import java.text.DateFormat; -import java.text.SimpleDateFormat; - +import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.common.log.wrappers.Logger; + +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.text.SimpleDateFormat; public class XlsOutputHandler implements OutputHandler { - private final static Logger LOGGER = LoggerFactory.getLogger(XlsOutputHandler.class); - + private static final Logger log = Logger.getLogger(XlsOutputHandler.class); private Workbook workbook; private Sheet activeSheet; - private Row currentRow; - int rowCount = 0; + private int rowCount = 0; + private String sheetName; + private String outputPath; - public XlsOutputHandler(Object... title){ - initiate(title); + public XlsOutputHandler(String outputPath, String sheetName, Object... title){ + this.outputPath = outputPath; + this.sheetName = sheetName; + initiate(sheetName, title); } @Override - public void initiate(Object... title) { - LOGGER.info("Starting to initiate xls output handler. "); + public void initiate(String sheetName, Object... title) { + log.info("#initiate - Starting to initiate XlsOutputHandler. "); workbook = new HSSFWorkbook(); - activeSheet = workbook.createSheet("Upgrade Migration 1710.0 results"); + activeSheet = workbook.createSheet(sheetName); addRecord(title); - LOGGER.info("Xls output handler has been initiated. "); + log.info("#initiate - XlsOutputHandler has been initiated. "); } @Override public void addRecord(Object... record) { - LOGGER.debug("Going to add record {} to output. ", record); - currentRow = activeSheet.createRow(rowCount++); - LOGGER.debug("A new row has been created"); + log.info("#addRecord - Going to add record {} to output. ", record); + Row currentRow = activeSheet.createRow(rowCount++); + log.info("#addRecord - A new row has been created"); int columnCount = 0; Cell cell; for(Object cellValue : record){ cell = currentRow.createCell(columnCount++); - if(cellValue != null) - cell.setCellValue(cellValue.toString()); + if (cellValue != null) { + cell.setCellValue(cellValue.toString()); + } } } @Override - public boolean writeOutput() { + public boolean writeOutputAndCloseFile() { + if (rowCount <= 1) { + return false; + } try { - DateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss"); - String fileName = "UpgradeMigration1710Results_" + df.format(System.currentTimeMillis()) + ".xls"; - LOGGER.info("Going to write xls output file {}. ", fileName); - workbook.write(new FileOutputStream(fileName)); + FileOutputStream file = getXlsFile(); + workbook.write(file); + file.close(); return true; } catch (Exception e) { - LOGGER.error("Failed to write an output file upon Upgrade migration 1710. Exception {} occured. ", e); + log.debug("#writeOutputAndCloseFile - Failed to write an output file. ", e); return false; } } + public String getOutputPath() { + return outputPath; + } + + FileOutputStream getXlsFile() throws FileNotFoundException { + String fileName = buildFileName(); + log.info("#getXlsFile - Going to write the output file {}. ", fileName); + return new FileOutputStream(fileName); + } + + private String buildFileName() { + StringBuilder fileName = new StringBuilder(); + if(StringUtils.isNotEmpty(outputPath)){ + fileName.append(outputPath); + } + return fileName.append(sheetName) + .append("_") + .append(new SimpleDateFormat("yyyyMMdd_HHmmss").format(System.currentTimeMillis())) + .append(".xls") + .toString(); + } + } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1710/UpgradeMigration1710.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1710/UpgradeMigration1710.java index 9d01ab0d9c..4b9af31092 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1710/UpgradeMigration1710.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1710/UpgradeMigration1710.java @@ -1,14 +1,8 @@ package org.openecomp.sdc.asdctool.migration.tasks.mig1710; -import java.util.ArrayList; -import java.util.EnumMap; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.function.Predicate; -import java.util.stream.Collectors; - +import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.Lists; +import fj.data.Either; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; @@ -17,9 +11,13 @@ import org.openecomp.sdc.asdctool.migration.core.task.PostMigration; import org.openecomp.sdc.asdctool.migration.tasks.handlers.XlsOutputHandler; import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic; import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic; +import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic; +import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic; import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction; import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction.LifecycleChanceActionEnum; +import org.openecomp.sdc.be.components.scheduledtasks.ComponentsCleanBusinessLogic; +import org.openecomp.sdc.be.config.Configuration; import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; @@ -28,41 +26,32 @@ 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.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.OriginTypeEnum; -import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.*; import org.openecomp.sdc.be.impl.ComponentsUtils; -import org.openecomp.sdc.be.model.ComponentInstance; -import org.openecomp.sdc.be.model.ComponentInstanceProperty; -import org.openecomp.sdc.be.model.ComponentParametersView; -import org.openecomp.sdc.be.model.LifeCycleTransitionEnum; -import org.openecomp.sdc.be.model.LifecycleStateEnum; -import org.openecomp.sdc.be.model.Resource; -import org.openecomp.sdc.be.model.User; +import org.openecomp.sdc.be.model.*; import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade; import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter; import org.openecomp.sdc.be.model.operations.api.IUserAdminOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.CsarOperation; import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter; +import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.exception.ResponseFormat; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; -import com.google.common.collect.Lists; - -import fj.data.Either; +import javax.annotation.PostConstruct; +import java.util.*; +import java.util.function.Predicate; +import java.util.stream.Collectors; -//@Component +@Component public class UpgradeMigration1710 implements PostMigration { - - private static final String SERVICE_UUID_RPOPERTY = "providing_service_uuid"; - - private static final String SERVICE_INVARIANT_UUID_RPOPERTY = "providing_service_invariant_uuid"; - + + private static final String SERVICE_UUID_RPOPERTY = "providing_service_uuid"; + + private static final String SERVICE_INVARIANT_UUID_RPOPERTY = "providing_service_invariant_uuid"; + private static final String UNKNOWN = "UNKNOWN"; private static final String CHECKOUT_MESSAGE = "checkout upon upgrade migration"; @@ -75,7 +64,13 @@ public class UpgradeMigration1710 implements PostMigration { private static final String UPGRADE_VFS_FAILED = "Upgrade VFs upon upgrade migration 1710 process failed. "; - private static final Logger LOGGER = LoggerFactory.getLogger(UpgradeMigration1710.class); + private static final Logger log = Logger.getLogger(UpgradeMigration1710.class); + + private static final String ALLOTTED_RESOURCE_NAME = "Allotted Resource"; + + //as per US 397775, only node type upgrade should be enabled, + // to support resource and service upgrade, this flag should be reverted + private boolean isNodeTypesSupportOnly = true; @Autowired private TitanDao titanDao; @@ -92,6 +87,9 @@ public class UpgradeMigration1710 implements PostMigration { @Autowired private ResourceBusinessLogic resourceBusinessLogic; + @Autowired + private ServiceBusinessLogic serviceBusinessLogic; + @Autowired private CsarOperation csarOperation; @@ -101,7 +99,10 @@ public class UpgradeMigration1710 implements PostMigration { @Autowired private ComponentsUtils componentsUtils; - private XlsOutputHandler outputHandler = new XlsOutputHandler("COMPONENT TYPE", "COMPONENT NAME", "COMPONENT UUID", "COMPONENT UNIQUE_ID", "UPGRADE STATUS", "DESCRIPTION"); + @Autowired + private ComponentsCleanBusinessLogic componentsCleanBusinessLogic; + + private XlsOutputHandler outputHandler = new XlsOutputHandler(null, "UpgradeMigration1710report","COMPONENT TYPE", "COMPONENT NAME", "COMPONENT UUID", "COMPONENT UNIQUE_ID", "UPGRADE STATUS", "DESCRIPTION"); private User user = null; @@ -110,101 +111,179 @@ public class UpgradeMigration1710 implements PostMigration { private final Map latestGenericTypes = new HashMap<>(); private final Map latestOriginResourceVersions = new HashMap<>(); - - private final List proxyServiceContainers = new ArrayList<>(); - - private final List vfAllottedResources = new ArrayList<>(); - - private final List allottedVfContainers = new ArrayList<>(); + + private final Map upgradedNodeTypesMap = new HashMap<>(); + + private List nodeTypes; + + private List proxyServiceContainers = new ArrayList<>(); + + private List vfAllottedResources = new ArrayList<>(); + + private List allottedVfContainers = new ArrayList<>(); private boolean isVfcUpgradeRequired = false; private boolean skipIfUpgradeVfFailed = true; + private boolean isAllottedAndProxySupported = true; + + private String userId; + + private boolean isCleanupLocked = false; + + private int markedAsDeletedResourcesCnt = 0; + + private int markedAsDeletedServicesCnt = 0; + + //how many components can be deleted once + private int maxDeleteComponents = 10; + + private boolean enableAutoHealing = true; + + //map for tracing checked out resources that keep in place after upgrade failure + private HashMap certifiedToNextCheckedOutUniqueId = new HashMap<>(); + + private int deleteLockTimeoutInSeconds = 60; + + private boolean isLockSucceeded = false; + /***********************************************/ + @VisibleForTesting + void setNodeTypesSupportOnly(boolean nodeTypesSupportOnly) { + isNodeTypesSupportOnly = nodeTypesSupportOnly; + } + + @VisibleForTesting + void setUser(User user) { + this.user = user; + } + + @VisibleForTesting + void setMarkedAsDeletedResourcesCnt(int markedAsDeletedResourcesCnt) { + this.markedAsDeletedResourcesCnt = markedAsDeletedResourcesCnt; + } + + @VisibleForTesting + void setMarkedAsDeletedServicesCnt(int markedAsDeletedServicesCnt) { + this.markedAsDeletedServicesCnt = markedAsDeletedServicesCnt; + } + + @PostConstruct + void init() { + Configuration config = ConfigurationManager.getConfigurationManager().getConfiguration(); + isVfcUpgradeRequired = !config.getSkipUpgradeVSPsFlag(); + skipIfUpgradeVfFailed = config.getSkipUpgradeFailedVfs(); + isAllottedAndProxySupported = config.getSupportAllottedResourcesAndProxyFlag(); + deleteLockTimeoutInSeconds = config.getDeleteLockTimeoutInSeconds(); + maxDeleteComponents = config.getMaxDeleteComponents(); + + String toscaConformanceLevel = config.getToscaConformanceLevel(); + Map> resourcesForUpgrade = config.getResourcesForUpgrade(); + nodeTypes = resourcesForUpgrade.get(toscaConformanceLevel); + enableAutoHealing = config.isEnableAutoHealing(); + userId = config.getAutoHealingOwner(); + isNodeTypesSupportOnly = true; + } + @Override public String description() { return "Upgrade migration 1710 - post migration task, which is dedicated to upgrade all latest certified (and not checked out) Node types, VFs and Services. "; } - private enum UpgradeStatus { + enum UpgradeStatus { UPGRADED, + UPGRADED_AS_INSTANCE, NOT_UPGRADED } @Override public MigrationResult migrate() { - LOGGER.info("Starting upgrade migration 1710 process. "); MigrationResult migrationResult = new MigrationResult(); - + //stop the upgrade if this ask is disabled + if (!enableAutoHealing) { + log.warn("Upgrade migration 1710 task is disabled"); + migrationResult.setMigrationStatus(MigrationResult.MigrationStatus.COMPLETED); + return migrationResult ; + } + log.info("Starting upgrade migration 1710 process. "); boolean result = true; + try { - isVfcUpgradeRequired = !ConfigurationManager.getConfigurationManager().getConfiguration().getSkipUpgradeVSPsFlag(); - skipIfUpgradeVfFailed = ConfigurationManager.getConfigurationManager().getConfiguration().getSkipUpgradeFailedVfs(); - final String userId = ConfigurationManager.getConfigurationManager().getConfiguration().getAutoHealingOwner(); + //lock cleanup node to avoid BE to delete marked components + //while the auto-healing process is running + isLockSucceeded = isNodeTypesSupportOnly ? true : isLockDeleteOperationSucceeded(); - Either userReq = userAdminOperation.getUserData(userId, false); - if (userReq.isRight()) { + if (!isLockSucceeded) { result = false; - LOGGER.error("Upgrade migration failed. User {} resolve failed: {} ", userId, userReq.right().value()); - } else { - user = userReq.left().value(); - LOGGER.info("User {} will perform upgrade operation", user.getUserId()); - } - if (result) { - result = upgradeNodeTypes(); + log.error("Cleanup node can't be locked. Upgrade migration failed"); } - if (result) { - result = upgradeVFs(); + else { + Either userReq = userAdminOperation.getUserData(userId, false); + if (userReq.isRight()) { + result = false; + log.error("Upgrade migration failed. User {} resolve failed: {} ", userId, userReq.right().value()); + } else { + user = userReq.left().value(); + log.info("User {} will perform upgrade operation", user.getUserId()); + } } if (result) { - upgradeServices(); - } - if(result){ - upgradeProxyServiceContainers(); - } - if(result){ - upgradeAllottedVFs(); + result = upgradeNodeTypes(); } - if(result){ - upgradeAllottedVfContainers(); + if (!isNodeTypesSupportOnly && result) { + result = upgradeTopologyTemplates(); } - } catch (Exception e) { + } + catch (Exception e) { result = false; - LOGGER.error("Error occurred {}. ", e); + log.error("Error occurred during the migration: ", e); } finally { - if (result) { - LOGGER.info("Upgrade migration 1710 has been successfully finished. "); - titanDao.commit(); - migrationResult.setMigrationStatus(MigrationResult.MigrationStatus.COMPLETED); - } else { - LOGGER.info("Upgrade migration 1710 was failed. "); - titanDao.rollback(); - migrationResult.setMigrationStatus(MigrationResult.MigrationStatus.FAILED); - } - outputHandler.writeOutput(); + MigrationResult.MigrationStatus status = result ? + MigrationResult.MigrationStatus.COMPLETED : MigrationResult.MigrationStatus.FAILED; + cleanup(status); + migrationResult.setMigrationStatus(status); } return migrationResult; } - private void upgradeAllottedVfContainers() { - LOGGER.info("Starting upgrade proxy {} service containers upon upgrade migration 1710 process. ", allottedVfContainers.size()); - for(String currUid : allottedVfContainers){ - upgradeServiceAndCommitIfNeeded(currUid, component -> true); + private boolean upgradeTopologyTemplates() { + if (upgradeVFs()) { + upgradeServices(); + upgradeProxyServiceContainers(); + upgradeAllottedVFs(); + upgradeAllottedVfContainers(); + return true; } - } + return false; + } - private StorageOperationStatus upgradeServices() { - LOGGER.info("Starting upgrade services upon upgrade migration 1710 process. "); - Either, TitanOperationStatus> getServicesRes = getAllLatestCertifiedComponentUids(VertexTypeEnum.TOPOLOGY_TEMPLATE, ComponentTypeEnum.SERVICE); - if (getServicesRes.isRight()) { - return StorageOperationStatus.GENERAL_ERROR; + private void cleanup(MigrationResult.MigrationStatus status) { + if (status == MigrationResult.MigrationStatus.COMPLETED ) { + log.info("Upgrade migration 1710 has been successfully finished. "); + titanDao.commit(); + } else { + log.info("Upgrade migration 1710 was failed. "); + titanDao.rollback(); + } + outputHandler.writeOutputAndCloseFile(); + if (!isNodeTypesSupportOnly && isLockSucceeded) { + //delete rest of components if their upgrade failed + markedAsDeletedResourcesCnt = maxDeleteComponents; + deleteResourcesIfLimitIsReached(); + markedAsDeletedServicesCnt = maxDeleteComponents; + deleteServicesIfLimitIsReached(); + unlockDeleteOperation(); } - for (String currUid : getServicesRes.left().value()) { - upgradeServiceAndCommitIfNeeded(currUid, this::shouldUpgrade); + } + + void upgradeServices(List uniqueIDs, Predicate shouldUpgrade, final String containerName) { + log.info("Starting upgrade {} upon upgrade migration 1710 process. ", containerName); + for (String currUid : uniqueIDs) { + upgradeServiceAndCommitIfNeeded(currUid, shouldUpgrade); } - return StorageOperationStatus.OK; + log.info("Upgrade {} upon upgrade migration 1710 process is finished. ", containerName); } private void upgradeServiceAndCommitIfNeeded(String currUid, Predicate shouldUpgrade) { @@ -213,35 +292,49 @@ public class UpgradeMigration1710 implements PostMigration { result = handleService(currUid, shouldUpgrade); } catch (Exception e) { result = false; - LOGGER.error("Failed to upgrade Service with uniqueId {} due to a reason {}. ", currUid, e); + log.error("Failed to upgrade service with uniqueId {} due to a reason {}. ", currUid, e.getMessage()); + log.debug("Failed to upgrade service with uniqueId {}", currUid, e); } finally { if (result) { + log.info("Service upgrade finished successfully: uniqueId {} ", currUid); titanDao.commit(); } else { + log.error("Failed to upgrade service with uniqueId {} ", currUid); titanDao.rollback(); } + markCheckedOutServiceAsDeletedIfUpgradeFailed(currUid, result); } } - - private void upgradeProxyServiceContainers() { - LOGGER.info("Starting upgrade proxy service containers upon upgrade migration 1710 process. "); - for(String currUid : proxyServiceContainers){ - upgradeServiceAndCommitIfNeeded(currUid, component -> true); + + private void upgradeAllottedVfContainers() { + upgradeServices(allottedVfContainers, component -> true, "proxy " + allottedVfContainers.size() + " service containers"); + } + + private void upgradeServices() { + Either, TitanOperationStatus> getServicesRes = getAllLatestCertifiedComponentUids(VertexTypeEnum.TOPOLOGY_TEMPLATE, ComponentTypeEnum.SERVICE); + if (getServicesRes.isRight()) { + log.error("Failed to retrieve the latest certified service versions"); + return; } - } + upgradeServices(getServicesRes.left().value(), this::shouldUpgrade, "services"); + } + + private void upgradeProxyServiceContainers() { + upgradeServices(proxyServiceContainers, component -> true, "proxy service containers"); + } private boolean handleService(String uniqueId, Predicate shouldUpgrade) { - LOGGER.info("Starting upgrade Service with uniqueId {} upon upgrade migration 1710 process. ", uniqueId); + log.info("Starting upgrade Service with uniqueId {} upon upgrade migration 1710 process. ", uniqueId); Either getServiceRes = toscaOperationFacade.getToscaElement(uniqueId); if(getServiceRes.isRight()){ - LOGGER.error("Failed to upgrade service with uniqueId {} due to {}. ", uniqueId, getServiceRes.right().value()); + log.error("Failed to upgrade service with uniqueId {} due to {}. ", uniqueId, getServiceRes.right().value()); outputHandler.addRecord(ComponentTypeEnum.SERVICE.name(), UNKNOWN, UNKNOWN, uniqueId, MigrationResult.MigrationStatus.FAILED.name(), getServiceRes.right().value()); return false; } String derivedFromGenericType = getServiceRes.left().value().getDerivedFromGenericType(); - LOGGER.debug("derivedFromGenericType: {}", derivedFromGenericType ); + log.debug("derivedFromGenericType: {}", derivedFromGenericType ); if (derivedFromGenericType == null) { //malformed field value, upgrade required return upgradeService(getServiceRes.left().value()); @@ -249,7 +342,7 @@ public class UpgradeMigration1710 implements PostMigration { if(!latestGenericTypes.containsKey(derivedFromGenericType)){ Either, TitanOperationStatus> getDerivedRes = findDerivedResources(derivedFromGenericType); if(getDerivedRes.isRight()){ - LOGGER.error(FAILED_TO_UPGRADE_COMPONENT, getServiceRes.left().value().getComponentType().getValue(), getServiceRes.left().value().getName(), getServiceRes.left().value().getInvariantUUID(), getServiceRes.left().value().getVersion(), "findDerivedResources", getDerivedRes.right().value()); + log.error(FAILED_TO_UPGRADE_COMPONENT, getServiceRes.left().value().getComponentType().getValue(), getServiceRes.left().value().getName(), getServiceRes.left().value().getInvariantUUID(), getServiceRes.left().value().getVersion(), "findDerivedResources", getDerivedRes.right().value()); outputHandler.addRecord( getServiceRes.left().value().getComponentType().name(),getServiceRes.left().value().getName(), getServiceRes.left().value().getInvariantUUID(), getServiceRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), getDerivedRes.right().value()); return false; } @@ -279,60 +372,85 @@ public class UpgradeMigration1710 implements PostMigration { return true; } - private boolean addComponent(org.openecomp.sdc.be.model.Component component, ComponentInstance instance) { - VertexTypeEnum vertexType = ModelConverter.getVertexType(instance.getOriginType().name()); - Either getOriginRes = toscaOperationFacade.getLatestCertifiedByToscaResourceName(instance.getToscaComponentName(), vertexType, JsonParseFlagEnum.ParseMetadata); - if (getOriginRes.isRight()) { - LOGGER.error(FAILED_TO_UPGRADE_COMPONENT, component.getComponentType().getValue(), component.getName(), component.getInvariantUUID(), component.getVersion(), "toscaOperationFacade.getLatestCertifiedByToscaResourceName", getOriginRes.right().value()); - outputHandler.addRecord(component.getComponentType().name(), component.getName(), component.getInvariantUUID(), component.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), getOriginRes.right().value()); - return false; - } - latestOriginResourceVersions.put(instance.getToscaComponentName(), getOriginRes.left().value().getVersion()); - return true; - } + private boolean addComponent(org.openecomp.sdc.be.model.Component component, ComponentInstance instance) { + VertexTypeEnum vertexType = ModelConverter.getVertexType(instance.getOriginType().name()); + Either getOriginRes = toscaOperationFacade.getLatestCertifiedByToscaResourceName(instance.getToscaComponentName(), vertexType, JsonParseFlagEnum.ParseMetadata); + if (getOriginRes.isRight()) { + log.error(FAILED_TO_UPGRADE_COMPONENT, component.getComponentType().getValue(), component.getName(), component.getInvariantUUID(), component.getVersion(), "toscaOperationFacade.getLatestCertifiedByToscaResourceName", getOriginRes.right().value()); + outputHandler.addRecord(component.getComponentType().name(), component.getName(), component.getInvariantUUID(), component.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), getOriginRes.right().value()); + return false; + } + latestOriginResourceVersions.put(instance.getToscaComponentName(), getOriginRes.left().value().getVersion()); + return true; + } private boolean shouldUpgrade(org.openecomp.sdc.be.model.Component component) { - boolean shouldUpgrade = false; - if(CollectionUtils.isNotEmpty(component.getComponentInstances())){ + if(CollectionUtils.isNotEmpty(component.getComponentInstances())) { + if (containsProxyOrAllottedVF(component)) { + return false; + } for(ComponentInstance instance : component.getComponentInstances()){ - if(instance.getOriginType() == OriginTypeEnum.ServiceProxy){ - LOGGER.info("The service with name {}, invariantUUID {}, version {}, contains Service proxy instance {}, than the service should be upgraded in the end of the upgrading proccess. ", component.getName(), component.getInvariantUUID(), component.getVersion(), instance.getName()); - proxyServiceContainers.add(component.getUniqueId()); - shouldUpgrade = false; - break; - } - if(isAllottedResource(instance.getActualComponentUid())){ - allottedVfContainers.add(component.getUniqueId()); - } if(isGreater(latestOriginResourceVersions.get(instance.getToscaComponentName()), instance.getComponentVersion())){ - LOGGER.info("The service with name {}, invariantUUID {}, version {}, contains instance {} from outdated version of origin {} {} , than the service should be upgraded. ", component.getName(), component.getInvariantUUID(), component.getVersion(), instance.getName(), instance.getComponentName(), instance.getComponentVersion()); - shouldUpgrade = true; + log.info("The service with name {}, invariantUUID {}, version {}, contains instance {} from outdated version of origin {} {} , than the service should be upgraded. ", component.getName(), component.getInvariantUUID(), component.getVersion(), instance.getName(), instance.getComponentName(), instance.getComponentVersion()); + return true; } } } - return shouldUpgrade; + return false; + } + + private boolean containsProxyOrAllottedVF(org.openecomp.sdc.be.model.Component component) { + return !component.getComponentInstances() + .stream() + .filter(i->isProxyOrAllottedVF(i, component.getUniqueId())) + .collect(Collectors.toList()).isEmpty(); + } + + private boolean isProxyOrAllottedVF(ComponentInstance instance, String uniqueId) { + if (instance.getOriginType() == OriginTypeEnum.ServiceProxy) { + keepProxyServiceContainerIfSupported(uniqueId); + return true; + } + if (isAllottedResource(instance.getActualComponentUid())) { + keepAllottedVfContainerIfSupported(uniqueId); + return true; + } + return false; + } + + private void keepAllottedVfContainerIfSupported(final String uniqueId) { + if (isAllottedAndProxySupported && !allottedVfContainers.contains(uniqueId)) { + log.info("Add a service with uniqueId {} to allotted VF containers container list", uniqueId); + allottedVfContainers.add(uniqueId); + } + } + + private void keepProxyServiceContainerIfSupported(final String uniqueId) { + if (isAllottedAndProxySupported && !proxyServiceContainers.contains(uniqueId)) { + log.info("Add a service with uniqueId {} to proxy service container list", uniqueId); + proxyServiceContainers.add(uniqueId); + } } private boolean upgradeService(org.openecomp.sdc.be.model.Component service) { String serviceName = service.getName(); String serviceUuid = service.getUUID(); - LOGGER.info("Starting upgrade Service with name {}, invariantUUID {}, version {} upon upgrade migration 1710 process. ", serviceName, service.getInvariantUUID(), service.getVersion()); - LOGGER.info("Starting to perform check out of service {}. ", serviceName); - Either checkouRes = lifecycleBusinessLogic.changeComponentState(service.getComponentType(), service.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT, changeInfo, true, false); + log.info("Starting upgrade Service with name {}, invariantUUID {}, version {} upon upgrade migration 1710 process. ", serviceName, service.getInvariantUUID(), service.getVersion()); + Either checkouRes = checkOutComponent(service); if (checkouRes.isRight()) { - LOGGER.error(FAILED_TO_UPGRADE_COMPONENT, service.getComponentType().getValue(), serviceName, service.getInvariantUUID(), service.getVersion(), "lifecycleBusinessLogic.changeComponentState", checkouRes.right().value().getFormattedMessage()); + log.error(FAILED_TO_UPGRADE_COMPONENT, service.getComponentType().getValue(), serviceName, service.getInvariantUUID(), service.getVersion(), "lifecycleBusinessLogic.changeComponentState", checkouRes.right().value().getFormattedMessage()); outputHandler.addRecord(service.getComponentType().name(), serviceName, serviceUuid, service.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), checkouRes.right().value().getFormattedMessage()); return false; } Either updateCompositionRes = updateComposition(checkouRes.left().value()); if (updateCompositionRes.isRight()) { - LOGGER.error(FAILED_TO_UPGRADE_COMPONENT, service.getComponentType().getValue(), serviceName, service.getInvariantUUID(), service.getVersion(), "updateComposition", updateCompositionRes.right().value().getFormattedMessage()); + log.error(FAILED_TO_UPGRADE_COMPONENT, service.getComponentType().getValue(), serviceName, service.getInvariantUUID(), service.getVersion(), "updateComposition", updateCompositionRes.right().value().getFormattedMessage()); outputHandler.addRecord(checkouRes.left().value().getComponentType().name(), checkouRes.left().value().getName(), checkouRes.left().value().getUUID(), checkouRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), updateCompositionRes.right().value().getFormattedMessage()); return false; } Either certifyRes = performFullCertification(checkouRes.left().value()); if (certifyRes.isRight()) { - LOGGER.error(FAILED_TO_UPGRADE_COMPONENT, service.getComponentType().getValue(), serviceName, service.getInvariantUUID(), service.getVersion(), "performFullCertification", certifyRes.right().value().getFormattedMessage()); + log.error(FAILED_TO_UPGRADE_COMPONENT, service.getComponentType().getValue(), serviceName, service.getInvariantUUID(), service.getVersion(), "performFullCertification", certifyRes.right().value().getFormattedMessage()); outputHandler.addRecord(checkouRes.left().value().getComponentType().name(), checkouRes.left().value().getName(), checkouRes.left().value().getInvariantUUID(), checkouRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), certifyRes.right().value().getFormattedMessage()); return false; } @@ -341,20 +459,22 @@ public class UpgradeMigration1710 implements PostMigration { } private Either updateComposition(org.openecomp.sdc.be.model.Component component) { - Either upgradeInstanceRes; - for (ComponentInstance instance : component.getComponentInstances()) { - upgradeInstanceRes = upgradeInstance(component, instance); - if (upgradeInstanceRes.isRight()) { - LOGGER.error(FAILED_TO_UPGRADE_COMPONENT, component.getComponentType().getValue(), component.getName(), component.getInvariantUUID(), component.getVersion(), "upgradeInstance", upgradeInstanceRes.right().value().getFormattedMessage()); - outputHandler.addRecord(component.getComponentType().name(), component.getName(), component.getUUID(), component.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), upgradeInstanceRes.right().value().getFormattedMessage()); - return Either.right(upgradeInstanceRes.right().value()); + if (component != null && component.getComponentInstances() != null) { + Either upgradeInstanceRes; + for (ComponentInstance instance : component.getComponentInstances()) { + upgradeInstanceRes = upgradeInstance(component, instance); + if (upgradeInstanceRes.isRight()) { + log.error(FAILED_TO_UPGRADE_COMPONENT, component.getComponentType().getValue(), component.getName(), component.getInvariantUUID(), component.getVersion(), "upgradeInstance", upgradeInstanceRes.right().value().getFormattedMessage()); + outputHandler.addRecord(component.getComponentType().name(), component.getName(), component.getUUID(), component.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), upgradeInstanceRes.right().value().getFormattedMessage()); + return Either.right(upgradeInstanceRes.right().value()); + } } } return Either.left(component); } private Either upgradeInstance(org.openecomp.sdc.be.model.Component component, ComponentInstance instance) { - LOGGER.info("Starting upgrade {} instance {} upon upgrade migration 1710 process. ", component.getComponentType().getValue(), instance.getName()); + log.info("Starting upgrade {} instance {} upon upgrade migration 1710 process. ", component.getComponentType().getValue(), instance.getName()); ComponentInstance newComponentInstance = new ComponentInstance(instance); if (instance.getOriginType() == OriginTypeEnum.ServiceProxy) { return upgradeServiceProxyInstance(component, instance, newComponentInstance); @@ -364,92 +484,98 @@ public class UpgradeMigration1710 implements PostMigration { private Either upgradeResourceInstance(org.openecomp.sdc.be.model.Component component, ComponentInstance instance, ComponentInstance newComponentInstance) { - LOGGER.info("Starting upgrade {} instance {} upon upgrade migration 1710 process. ", component.getComponentType().getValue(), instance.getName()); + log.info("Starting upgrade {} instance {} upon upgrade migration 1710 process. ", component.getComponentType().getValue(), instance.getName()); Either upgradeInstanceRes = null; VertexTypeEnum vertexType = ModelConverter.getVertexType(instance.getOriginType().name()); Either getOriginRes = toscaOperationFacade.getLatestCertifiedByToscaResourceName(instance.getToscaComponentName(), vertexType, JsonParseFlagEnum.ParseMetadata); if(getOriginRes.isRight()){ - LOGGER.info("Upgrade of {} instance {} upon upgrade migration 1710 process failed due to a reason {}. ", + log.info("Upgrade of {} instance {} upon upgrade migration 1710 process failed due to a reason {}. ", component.getComponentType().getValue(), instance.getName(), getOriginRes.right().value()); upgradeInstanceRes = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(getOriginRes.right().value(), instance.getOriginType().getComponentType()))); } - if(upgradeInstanceRes == null){ - newComponentInstance.setComponentName(getOriginRes.left().value().getName()); - newComponentInstance.setComponentUid(getOriginRes.left().value().getUniqueId()); - newComponentInstance.setComponentVersion(getOriginRes.left().value().getVersion()); - newComponentInstance.setToscaComponentName(((Resource)getOriginRes.left().value()).getToscaResourceName()); - if(isGreater(getOriginRes.left().value().getVersion(), instance.getComponentVersion())){ - upgradeInstanceRes = changeAssetVersion(component, instance, newComponentInstance); - } - if((upgradeInstanceRes == null || upgradeInstanceRes.isLeft()) && isAllottedResource(instance.getComponentUid()) && MapUtils.isNotEmpty(component.getComponentInstancesProperties())){ - ComponentInstance instanceToUpdate = upgradeInstanceRes == null ? instance : upgradeInstanceRes.left().value(); - upgradeInstanceRes = Either.left(updateServiceUuidProperty(component, instanceToUpdate, component.getComponentInstancesProperties().get(instance.getUniqueId()))); - } + if(upgradeInstanceRes == null) { + copyComponentNameAndVersionToNewInstance(newComponentInstance, getOriginRes.left().value()); + + if(isGreater(getOriginRes.left().value().getVersion(), instance.getComponentVersion())){ + upgradeInstanceRes = changeAssetVersion(component, instance, newComponentInstance); + } + if((upgradeInstanceRes == null || upgradeInstanceRes.isLeft()) && isAllottedResource(instance.getComponentUid()) && MapUtils.isNotEmpty(component.getComponentInstancesProperties())){ + ComponentInstance instanceToUpdate = upgradeInstanceRes == null ? instance : upgradeInstanceRes.left().value(); + upgradeInstanceRes = Either.left(updateServiceUuidProperty(component, instanceToUpdate, component.getComponentInstancesProperties().get(instance.getUniqueId()))); + } } //upgrade nodes contained by CVFC if(upgradeInstanceRes == null && isVfcUpgradeRequired && newComponentInstance.getOriginType() == OriginTypeEnum.CVFC && - !upgradeVf(getOriginRes.left().value().getUniqueId(), false)) { - upgradeInstanceRes = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); + !upgradeVf(getOriginRes.left().value().getUniqueId(), false, true)) { + upgradeInstanceRes = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); } if(upgradeInstanceRes == null){ - upgradeInstanceRes = Either.left(instance); + upgradeInstanceRes = Either.left(instance); } - LOGGER.info("Upgrade of {} instance {} upon upgrade migration 1710 process finished successfully. ", - component.getComponentType().getValue(), instance.getName()); + log.info("Upgrade of {} instance {} upon upgrade migration 1710 process finished successfully. ", + component.getComponentType().getValue(), instance.getName()); return upgradeInstanceRes; } + private void copyComponentNameAndVersionToNewInstance(ComponentInstance newComponentInstance, Resource originResource) { + newComponentInstance.setComponentName(originResource.getName()); + newComponentInstance.setComponentUid(originResource.getUniqueId()); + newComponentInstance.setComponentVersion(originResource.getVersion()); + newComponentInstance.setToscaComponentName(originResource.getToscaResourceName()); + } + private ComponentInstance updateServiceUuidProperty(org.openecomp.sdc.be.model.Component component, ComponentInstance instance, List instanceProperties){ - if(isAllottedResource(instance.getComponentUid()) && instanceProperties != null){ - Optional propertyUuid = instanceProperties.stream().filter(p->p.getName().equals(SERVICE_UUID_RPOPERTY)).findFirst(); - Optional propertyInvariantUuid = instanceProperties.stream().filter(p->p.getName().equals(SERVICE_INVARIANT_UUID_RPOPERTY)).findFirst(); - if(propertyUuid.isPresent() && propertyInvariantUuid.isPresent()){ - String serviceInvariantUUID = propertyInvariantUuid.get().getValue(); - Either, TitanOperationStatus> getLatestOriginServiceRes = getLatestCertifiedService(serviceInvariantUUID); - if (getLatestOriginServiceRes.isRight()) { - return instance; - } - propertyUuid.get().setValue((String) getLatestOriginServiceRes.left().value().get(0).getJsonMetadataField(JsonPresentationFields.UUID)); - componentInstanceBusinessLogic.createOrUpdatePropertiesValues(component.getComponentType(), component.getUniqueId(), instance.getUniqueId(), Lists.newArrayList(propertyUuid.get()), user.getUserId()) - .right() - .forEach(e -> LOGGER.debug("Failed to update property {} of the instance {} of the component {}. ", SERVICE_UUID_RPOPERTY, instance.getUniqueId(), component.getName())); - } - } - return instance; - } - + if(isAllottedResource(instance.getComponentUid()) && instanceProperties != null){ + Optional propertyUuid = instanceProperties.stream().filter(p->p.getName().equals(SERVICE_UUID_RPOPERTY)).findFirst(); + Optional propertyInvariantUuid = instanceProperties.stream().filter(p->p.getName().equals(SERVICE_INVARIANT_UUID_RPOPERTY)).findFirst(); + if(propertyUuid.isPresent() && propertyInvariantUuid.isPresent()){ + String serviceInvariantUUID = propertyInvariantUuid.get().getValue(); + Either, TitanOperationStatus> getLatestOriginServiceRes = getLatestCertifiedService(serviceInvariantUUID); + if (getLatestOriginServiceRes.isRight()) { + return instance; + } + propertyUuid.get().setValue((String) getLatestOriginServiceRes.left().value().get(0).getJsonMetadataField(JsonPresentationFields.UUID)); + componentInstanceBusinessLogic.createOrUpdatePropertiesValues(component.getComponentType(), component.getUniqueId(), instance.getUniqueId(), Lists.newArrayList(propertyUuid.get()), user.getUserId()) + .right() + .forEach(e -> log.debug("Failed to update property {} of the instance {} of the component {}. ", SERVICE_UUID_RPOPERTY, instance.getUniqueId(), component.getName())); + } + } + return instance; + } + private boolean isAllottedResource(String uniqueId){ - ComponentParametersView filters = new ComponentParametersView(true); - filters.setIgnoreCategories(false); - Either getResourceRes = toscaOperationFacade.getToscaElement(uniqueId, filters); - if(getResourceRes.isRight()){ - return false; - } - if(getResourceRes.left().value().getCategories() != null && getResourceRes.left().value().getCategories().get(0)!= null){ - return "Allotted Resource".equals(getResourceRes.left().value().getCategories().get(0).getName()); - } - return false; - } - + ComponentParametersView filters = new ComponentParametersView(true); + filters.setIgnoreCategories(false); + Either getResourceRes = toscaOperationFacade.getToscaElement(uniqueId, filters); + if(getResourceRes.isRight()){ + return false; + } + if(getResourceRes.left().value().getCategories() != null && getResourceRes.left().value().getCategories().get(0)!= null){ + return ALLOTTED_RESOURCE_NAME.equals(getResourceRes.left().value().getCategories().get(0).getName()); + } + return false; + } + private boolean isAllottedVf(org.openecomp.sdc.be.model.Component component){ - if(component.getComponentType() != ComponentTypeEnum.RESOURCE){ - return false; - } - if(((Resource)component).getResourceType() != ResourceTypeEnum.VF){ - return false; - } - return isAllottedResource(component.getUniqueId()); - } - + if(component.getComponentType() != ComponentTypeEnum.RESOURCE || ((Resource)component).getResourceType() != ResourceTypeEnum.VF){ + return false; + } + return isAllottedResource(component.getUniqueId()); + } + private Either upgradeServiceProxyInstance(org.openecomp.sdc.be.model.Component component, ComponentInstance instance, ComponentInstance newComponentInstance) { Either, TitanOperationStatus> getLatestOriginServiceRes = getLatestCertifiedService(instance.getSourceModelInvariant()); if (getLatestOriginServiceRes.isRight()) { return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertTitanStatusToStorageStatus(getLatestOriginServiceRes.right().value()), instance.getOriginType().getComponentType()))); } - newComponentInstance.setComponentVersion((String) getLatestOriginServiceRes.left().value().get(0).getJsonMetadataField(JsonPresentationFields.VERSION)); - newComponentInstance.setSourceModelUid((String) getLatestOriginServiceRes.left().value().get(0).getJsonMetadataField(JsonPresentationFields.UNIQUE_ID)); - newComponentInstance.setSourceModelName((String) getLatestOriginServiceRes.left().value().get(0).getJsonMetadataField(JsonPresentationFields.NAME)); - newComponentInstance.setSourceModelUuid((String) getLatestOriginServiceRes.left().value().get(0).getJsonMetadataField(JsonPresentationFields.UUID)); + ModelConverter.getVertexType(instance.getOriginType().name()); + Either getOriginRes = toscaOperationFacade.getLatestByName(instance.getComponentName()); + if(getOriginRes.isRight()){ + log.info("Upgrade of {} instance {} upon upgrade migration 1710 process failed due to a reason {}. ", + component.getComponentType().getValue(), instance.getName(), getOriginRes.right().value()); + return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(getOriginRes.right().value(), instance.getOriginType().getComponentType()))); + } + newComponentInstance.setComponentUid((String) getLatestOriginServiceRes.left().value().get(0).getJsonMetadataField(JsonPresentationFields.UNIQUE_ID)); return changeAssetVersion(component, instance, newComponentInstance); } @@ -470,271 +596,319 @@ public class UpgradeMigration1710 implements PostMigration { } private boolean upgradeNodeTypes() { - LOGGER.info("Starting upgrade node types upon upgrade migration 1710 process. "); - String toscaConformanceLevel = ConfigurationManager.getConfigurationManager().getConfiguration().getToscaConformanceLevel(); - Map> resourcesForUpgrade = ConfigurationManager.getConfigurationManager().getConfiguration().getResourcesForUpgrade(); - Map upgradedNodeTypesMap = new HashMap<>(); - List nodeTypes; - if (resourcesForUpgrade.containsKey(toscaConformanceLevel)) { - nodeTypes = resourcesForUpgrade.get(toscaConformanceLevel); - if (nodeTypes != null && !nodeTypes.isEmpty()) { - Either, TitanOperationStatus> getRes = getAllLatestCertifiedComponentUids(VertexTypeEnum.NODE_TYPE, ComponentTypeEnum.RESOURCE); - if (getRes.isRight()) { + log.info("Starting upgrade node types upon upgrade migration 1710 process. "); + if (nodeTypes != null && !nodeTypes.isEmpty()) { + Either, TitanOperationStatus> getRes = getAllLatestCertifiedComponentUids(VertexTypeEnum.NODE_TYPE, ComponentTypeEnum.RESOURCE); + if (getRes.isRight()) { + return false; + } + for (String toscaResourceName : nodeTypes) { + if (!upgradeNodeType(toscaResourceName, getRes.left().value())) { return false; } - List allNodeTypes = getRes.left().value(); - - for (String toscaResourceName : nodeTypes) { - Either, StorageOperationStatus> status = getLatestByName(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName); - if (status.isRight()) { - LOGGER.error("Failed to find node type {} ", toscaResourceName); - return false; - } - List vList = status.left().value(); - for (GraphVertex vertex : vList) { - StorageOperationStatus updateRes = upgradeNodeType(vertex, upgradedNodeTypesMap, allNodeTypes, nodeTypes); - if (updateRes != StorageOperationStatus.OK) { - return false; - } - } - } } } + else { + log.info("No node types for upgrade are configured"); + } return true; } - private boolean upgradeVFs() { - return upgradeVFs(false); - } - - private boolean upgradeAllottedVFs() { - LOGGER.info("Starting upgrade {} allotted Vfs with upon upgrade migration 1710 process. ", vfAllottedResources.size()); - return upgradeVFs(true); + private boolean upgradeNodeType(String toscaResourceName, List allNodeTypes) { + Either, StorageOperationStatus> status = getLatestByName(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName); + if (status.isRight()) { + log.error("Failed to find node type {} ", toscaResourceName); + return false; + } + List vList = status.left().value(); + for (GraphVertex vertex : vList) { + StorageOperationStatus updateRes = upgradeNodeType(vertex, allNodeTypes); + if (updateRes != StorageOperationStatus.OK) { + return false; + } + } + return true; } - - private boolean upgradeVFs(boolean allottedVfsUpgrade) { - LOGGER.info("Starting upgrade VFs upon upgrade migration 1710 process. "); + + private boolean upgradeVFs() { + log.info("Starting upgrade VFs upon upgrade migration 1710 process. "); Either, TitanOperationStatus> getVfsRes = getAllLatestCertifiedComponentUids(VertexTypeEnum.TOPOLOGY_TEMPLATE, ComponentTypeEnum.RESOURCE); if (getVfsRes.isRight()) { - LOGGER.info(UPGRADE_VFS_FAILED); + log.info(UPGRADE_VFS_FAILED); return false; } - for (String currUid : getVfsRes.left().value()) { + return upgradeVFs(getVfsRes.left().value(), false); + } + + private boolean upgradeAllottedVFs() { + log.info("Starting upgrade {} allotted Vfs with upon upgrade migration 1710 process. ", vfAllottedResources.size()); + return upgradeVFs(vfAllottedResources, true); + } + + boolean upgradeVFs(List resourceList, boolean isAllottedVfsUpgrade) { + for (String currUid : resourceList) { boolean result = true; try { - result = upgradeVf(currUid, allottedVfsUpgrade); + result = upgradeVf(currUid, isAllottedVfsUpgrade, false); if (!result && !skipIfUpgradeVfFailed) { return false; } } catch (Exception e) { - LOGGER.error("The exception {} occured upon upgrade VFs. ", e); + log.error("The exception {} occurred upon upgrade VFs. ", e.getMessage()); + log.debug("The exception occurred upon upgrade VFs:", e); result = false; if (!skipIfUpgradeVfFailed) { - return false; + return false; } } finally { - if (!result) { - LOGGER.error("Failed to upgrade RESOURCE with uniqueId {} ", currUid); - titanDao.rollback(); + if (result) { + log.info("Resource upgrade finished successfully: uniqueId {} ", currUid); + titanDao.commit(); } else { - LOGGER.info("RESOURCE upgrade finished successfully: uniqueId {} ", currUid); - titanDao.commit(); + log.error("Failed to upgrade resource with uniqueId {} ", currUid); + titanDao.rollback(); } + markCheckedOutResourceAsDeletedIfUpgradeFailed(currUid, result); } } - LOGGER.info("Upgrade VFs upon upgrade migration 1710 process finished successfully. "); + log.info("Upgrade VFs upon upgrade migration 1710 process finished successfully. "); return true; } - private boolean upgradeVf(String uniqueId, boolean allottedVfsUpgrade) { - LOGGER.info("Starting upgrade VF with uniqueId {} upon upgrade migration 1710 process. ", uniqueId); - Either latestVersionRes; + private boolean upgradeVf(String uniqueId, boolean allottedVfsUpgrade, boolean isInstance) { + log.info("Starting upgrade VF with uniqueId {} upon upgrade migration 1710 process. ", uniqueId); Either getRes = toscaOperationFacade.getToscaElement(uniqueId); if (getRes.isRight()) { - LOGGER.debug("Failed to fetch VF with uniqueId {} upon upgrade migration 1710 process. ", uniqueId); + log.debug("Failed to fetch VF with uniqueId {} upon upgrade migration 1710 process. ", uniqueId); outputHandler.addRecord(ComponentTypeEnum.RESOURCE.name(), UNKNOWN, UNKNOWN, uniqueId, MigrationResult.MigrationStatus.FAILED.name(), getRes.right().value()); return false; } if(!allottedVfsUpgrade && isAllottedVf(getRes.left().value())){ - vfAllottedResources.add(uniqueId); - return true; + keepAllottedResourceIfSupported(uniqueId); + return true; } if (StringUtils.isNotEmpty(getRes.left().value().getCsarUUID())) { - LOGGER.info("Going to fetch the latest version of VSP with csarUUID {} upon upgrade migration 1710 process. ", getRes.left().value().getCsarUUID()); - latestVersionRes = csarOperation.getCsarLatestVersion(getRes.left().value().getCsarUUID(), user); + log.info("Going to fetch the latest version of VSP with csarUUID {} upon upgrade migration 1710 process. ", getRes.left().value().getCsarUUID()); + Either latestVersionRes = csarOperation.getCsarLatestVersion(getRes.left().value().getCsarUUID(), user); if (latestVersionRes.isRight()) { - LOGGER.debug("Failed to fetch the latest version of VSP with csarUUID {} upon upgrade migration 1710 process. ", getRes.left().value().getCsarUUID()); + log.debug("Failed to fetch the latest version of VSP with csarUUID {} upon upgrade migration 1710 process. ", getRes.left().value().getCsarUUID()); outputHandler.addRecord(getRes.left().value().getComponentType().name(), getRes.left().value().getName(), getRes.left().value().getUUID(), getRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), latestVersionRes.right().value()); return false; } if (isGreater(latestVersionRes.left().value(), getRes.left().value().getCsarVersion())) { - return upgradeVfWithLatestVsp(getRes.left().value(), latestVersionRes); + return upgradeVfWithLatestVsp(getRes.left().value(), latestVersionRes.left().value(), isInstance); } - if (!isVfcUpgradeRequired) { - LOGGER.warn("Warning: No need to upgrade VF with name {}, invariantUUID {}, version {} and VSP version {}. No new version of VSP. ", getRes.left().value().getName(), getRes.left().value().getInvariantUUID(), getRes.left().value().getVersion(), getRes.left().value().getCsarVersion()); + if (isVfcUpgradeRequired) { + return upgradeComponentWithLatestGeneric(getRes.left().value(), isInstance); } + log.warn("Warning: No need to upgrade VF with name {}, invariantUUID {}, version {} and VSP version {}. No new version of VSP. ", getRes.left().value().getName(), getRes.left().value().getInvariantUUID(), getRes.left().value().getVersion(), getRes.left().value().getCsarVersion()); + return true; + } + else { + return upgradeComponentWithLatestGeneric(getRes.left().value(), isInstance); + } + } + + private void keepAllottedResourceIfSupported(final String uniqueId) { + if (isAllottedAndProxySupported && !vfAllottedResources.contains(uniqueId)) { + log.info("Add a resource with uniqueId {} to allotted resource list", uniqueId); + vfAllottedResources.add(uniqueId); } - return upgradeComponentWithLatestGeneric(getRes.left().value()); } - private boolean upgradeVfWithLatestVsp(org.openecomp.sdc.be.model.Component vf, Either latestVersionRes) { - LOGGER.info("Starting upgrade vf with name {}, invariantUUID {}, version {} and latest VSP version {} upon upgrade migration 1710 process. ", vf.getName(), vf.getInvariantUUID(), vf.getVersion(), latestVersionRes.left().value()); - LOGGER.info("Starting to perform check out of vf with name {}, invariantUUID {}, version {}. ", vf.getName(), vf.getInvariantUUID(), vf.getVersion()); - Either checkouRes = lifecycleBusinessLogic.changeComponentState(vf.getComponentType(), vf.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT, changeInfo, true, false); + private boolean upgradeVfWithLatestVsp(org.openecomp.sdc.be.model.Component vf, String latestVersion, boolean isInstance) { + log.info("Starting upgrade vf with name {}, invariantUUID {}, version {} and latest VSP version {} upon upgrade migration 1710 process. ", vf.getName(), vf.getInvariantUUID(), vf.getVersion(), latestVersion); + Either checkouRes = checkOutComponent(vf); if (checkouRes.isRight()) { outputHandler.addRecord(vf.getComponentType().name(), vf.getName(), vf.getUUID(), vf.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), checkouRes.right().value().getFormattedMessage()); return false; } - LOGGER.info("Starting update vf with name {}, invariantUUID {}, version {} and latest VSP {}. ", vf.getName(), vf.getInvariantUUID(), vf.getVersion(), latestVersionRes.left().value()); Resource resourceToUpdate = new Resource(((Resource) checkouRes.left().value()).getComponentMetadataDefinition()); resourceToUpdate.setDerivedFromGenericType(((Resource) checkouRes.left().value()).getDerivedFromGenericType()); resourceToUpdate.setDerivedFromGenericVersion(((Resource) checkouRes.left().value()).getDerivedFromGenericVersion()); - resourceToUpdate.setCsarVersion(Double.toString(Double.parseDouble(latestVersionRes.left().value()))); - Either updateResourceFromCsarRes = resourceBusinessLogic.validateAndUpdateResourceFromCsar(resourceToUpdate, user, null, null, resourceToUpdate.getUniqueId()); - if (updateResourceFromCsarRes.isRight()) { - outputHandler.addRecord(resourceToUpdate.getComponentType().name(), resourceToUpdate.getName(), resourceToUpdate.getUUID(), resourceToUpdate.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), updateResourceFromCsarRes.right().value().getFormattedMessage()); - LOGGER.info("Failed to update vf with name {}, invariantUUID {}, version {} and latest VSP {}. ", vf.getName(), vf.getInvariantUUID(), vf.getVersion(), latestVersionRes.left().value()); + resourceToUpdate.setCsarVersion(Double.toString(Double.parseDouble(latestVersion))); + resourceToUpdate.setCategories(((Resource)checkouRes.left().value()).getCategories()); + try { + Resource updateResourceFromCsarRes = resourceBusinessLogic.validateAndUpdateResourceFromCsar(resourceToUpdate, user, null, null, resourceToUpdate.getUniqueId()); + } catch(ComponentException e){ + outputHandler.addRecord(resourceToUpdate.getComponentType().name(), resourceToUpdate.getName(), resourceToUpdate.getUUID(), resourceToUpdate.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), e.getResponseFormat().getFormattedMessage()); + log.info("Failed to update vf with name {}, invariantUUID {}, version {} and latest VSP {}. ", vf.getName(), vf.getInvariantUUID(), vf.getVersion(), latestVersion); return false; } Either certifyRes = performFullCertification(checkouRes.left().value()); if (certifyRes.isRight()) { - LOGGER.info(FAILED_TO_CHANGE_STATE_OF_COMPONENT, checkouRes.left().value().getName(), checkouRes.left().value().getInvariantUUID(), checkouRes.left().value().getVersion(), LifeCycleTransitionEnum.CERTIFY); + log.info(FAILED_TO_CHANGE_STATE_OF_COMPONENT, checkouRes.left().value().getName(), checkouRes.left().value().getInvariantUUID(), checkouRes.left().value().getVersion(), LifeCycleTransitionEnum.CERTIFY); outputHandler.addRecord(checkouRes.left().value().getComponentType().name(), checkouRes.left().value().getName(), checkouRes.left().value().getInvariantUUID(), checkouRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), certifyRes.right().value().getFormattedMessage()); return false; } - LOGGER.info("Full certification of vf with name {}, invariantUUID {}, version {} finished . ", vf.getName(), vf.getInvariantUUID(), vf.getVersion(), latestVersionRes.left().value()); - outputHandler.addRecord(certifyRes.left().value().getComponentType().name(), certifyRes.left().value().getName(), certifyRes.left().value().getUUID(), certifyRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.COMPLETED.name(), UpgradeStatus.UPGRADED); + log.info("Full certification of vf with name {}, invariantUUID {}, version {} finished . ", vf.getName(), vf.getInvariantUUID(), vf.getVersion(), latestVersion); + outputHandler.addRecord(certifyRes.left().value().getComponentType().name(), certifyRes.left().value().getName(), certifyRes.left().value().getUUID(), certifyRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.COMPLETED.name(), getVfUpgradeStatus(true, isInstance)); return true; } - private boolean upgradeComponentWithLatestGeneric(org.openecomp.sdc.be.model.Component component) { + private boolean upgradeComponentWithLatestGeneric(org.openecomp.sdc.be.model.Component component, boolean isInstance) { String derivedFromGenericType = component.getDerivedFromGenericType(); String derivedFromGenericVersion = component.getDerivedFromGenericVersion(); org.openecomp.sdc.be.model.Component updatedComponent = component; + if (failedToFindDerivedResourcesOfNodeType(component, derivedFromGenericType, derivedFromGenericVersion)) { + return false; + } + if (StringUtils.isEmpty(derivedFromGenericType) || + latestVersionExists(latestGenericTypes.get(derivedFromGenericType), derivedFromGenericVersion) || + isVfcUpgradeRequired || + isAllottedAndProxySupported) { + if (StringUtils.isNotEmpty(derivedFromGenericType)) { + log.info("Newer version {} of derived from generic type {} exists. ", latestGenericTypes.get(derivedFromGenericType).getJsonMetadataField(JsonPresentationFields.VERSION), derivedFromGenericType); + } + else { + log.info("The vf resource with name {}, invariantUUID {}, version {}, has an empty derivedFromGenericType field. ", component.getName(), component.getInvariantUUID(), component.getVersion()); + } + updatedComponent = checkOutAndCertifyComponent(component); + } else { + log.info("The version {} of derived from generic type {} is up to date. No need to upgrade component with name {}, invariantUUID {} and version {}. ", latestGenericTypes.get(derivedFromGenericType), derivedFromGenericType, component.getName(), component.getInvariantUUID(), component.getVersion()); + } + if (updatedComponent != null) { + log.info(UPGRADE_COMPONENT_SUCCEEDED, component.getComponentType().getValue(), component.getName(), component.getInvariantUUID(), component.getVersion()); + outputHandler.addRecord(updatedComponent.getComponentType().name(), updatedComponent.getName(), updatedComponent.getUUID(), updatedComponent.getUniqueId(), MigrationResult.MigrationStatus.COMPLETED.name(), + getVfUpgradeStatus(!updatedComponent.equals(component), isInstance)); + } + return true; + } + + private org.openecomp.sdc.be.model.Component checkOutAndCertifyComponent(org.openecomp.sdc.be.model.Component component) { + + log.info("Starting to perform check out of vf with name {}, invariantUUID {}, version {}. ", component.getName(), component.getInvariantUUID(), component.getVersion()); + Either checkoutRes = checkOutComponent(component); + if (checkoutRes.isRight()) { + log.error(FAILED_TO_CHANGE_STATE_OF_COMPONENT, component.getName(), component.getInvariantUUID(), component.getVersion(), LifeCycleTransitionEnum.CHECKOUT); + outputHandler.addRecord(component.getComponentType().name(), component.getName(), component.getInvariantUUID(), component.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), checkoutRes.right().value().getFormattedMessage()); + return null; + } + + if (!updateCompositionFailed(component, checkoutRes.left().value())) { + return null; + } + Either certifyRes = performFullCertification(checkoutRes.left().value()); + if (certifyRes.isRight()) { + log.error(FAILED_TO_UPGRADE_COMPONENT, component.getComponentType().getValue(), component.getName(), component.getInvariantUUID(), component.getVersion(), "performFullCertification", certifyRes.right().value()); + outputHandler.addRecord(checkoutRes.left().value().getComponentType().name(), checkoutRes.left().value().getName(), checkoutRes.left().value().getInvariantUUID(), checkoutRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), certifyRes.right().value().getFormattedMessage()); + return null; + } + return certifyRes.left().value(); + } + + private boolean failedToFindDerivedResourcesOfNodeType(org.openecomp.sdc.be.model.Component component, String derivedFromGenericType, String derivedFromGenericVersion) { if (StringUtils.isNotEmpty(derivedFromGenericType) && !latestGenericTypes.containsKey(derivedFromGenericType)) { - LOGGER.info("Starting upgrade vf with name {}, invariantUUID {}, version {}, latest derived from generic type {}, latest derived from generic version {}. ", component.getName(), component.getInvariantUUID(), component.getVersion(), derivedFromGenericType, derivedFromGenericVersion); - LOGGER.info("Starting to fetch latest generic node type {}. ", derivedFromGenericType); + log.info("Starting upgrade vf with name {}, invariantUUID {}, version {}, latest derived from generic type {}, latest derived from generic version {}. ", component.getName(), component.getInvariantUUID(), component.getVersion(), derivedFromGenericType, derivedFromGenericVersion); + log.info("Starting to fetch latest generic node type {}. ", derivedFromGenericType); Either, TitanOperationStatus> getDerivedRes = findDerivedResources(derivedFromGenericType); if (getDerivedRes.isRight()) { outputHandler.addRecord(component.getComponentType().name(), component.getName(), component.getInvariantUUID(), component.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), getDerivedRes.right().value()); - LOGGER.info("Failed to upgrade component with name {}, invariantUUID {}, version {} and latest generic. Status is {}. ", component.getName(), component.getInvariantUUID(), component.getVersion(), derivedFromGenericType); - return false; + log.info("Failed to upgrade component with name {}, invariantUUID {}, version {} and latest generic. Status is {}. ", component.getName(), component.getInvariantUUID(), component.getVersion(), derivedFromGenericType); + return true; } latestGenericTypes.put(derivedFromGenericType, getDerivedRes.left().value().get(0)); } - if (StringUtils.isEmpty(derivedFromGenericType) || - latestVersionExists(latestGenericTypes.get(derivedFromGenericType), derivedFromGenericVersion) || - isVfcUpgradeRequired) { - if (StringUtils.isNotEmpty(derivedFromGenericType)) - LOGGER.info("Newer version {} of derived from generic type {} exists. ", latestGenericTypes.get(derivedFromGenericType).getJsonMetadataField(JsonPresentationFields.VERSION), derivedFromGenericType); - else - LOGGER.info("The vf resource with name {}, invariantUUID {}, version {}, has an empty derivedFromGenericType field. ", component.getName(), component.getInvariantUUID(), component.getVersion()); - - LOGGER.info("Starting to perform check out of vf with name {}, invariantUUID {}, version {}. ", component.getName(), component.getInvariantUUID(), component.getVersion()); - Either checkouRes = lifecycleBusinessLogic.changeComponentState(component.getComponentType(), component.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT, changeInfo, true, false); - if (checkouRes.isRight()) { - LOGGER.info(FAILED_TO_CHANGE_STATE_OF_COMPONENT, component.getName(), component.getInvariantUUID(), component.getVersion(), LifeCycleTransitionEnum.CHECKOUT); - outputHandler.addRecord(component.getComponentType().name(), component.getName(), component.getInvariantUUID(), component.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), checkouRes.right().value().getFormattedMessage()); - return false; - } - //update included VFCs, if it is required as per configuration - if (CollectionUtils.isNotEmpty(checkouRes.left().value().getComponentInstances())) { - LOGGER.info("VFC upgrade is required: updating components of vf with name {}, invariantUUID {}, version {}. ", component.getName(), component.getInvariantUUID(), component.getVersion()); - Either updateCompositionRes = - updateComposition(checkouRes.left().value()); - if (updateCompositionRes.isRight()) { - LOGGER.error(FAILED_TO_UPGRADE_COMPONENT, checkouRes.left().value().getComponentType().name(), checkouRes.left().value().getName(), checkouRes.left().value().getInvariantUUID(), checkouRes.left().value().getVersion(), "updateComposition", updateCompositionRes.right().value().getFormattedMessage()); - outputHandler.addRecord(checkouRes.left().value().getComponentType().name(), checkouRes.left().value().getName(), checkouRes.left().value().getUUID(), checkouRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), updateCompositionRes.right().value().getFormattedMessage()); - return false; + return false; + } + + private boolean updateCompositionFailed(org.openecomp.sdc.be.model.Component component, org.openecomp.sdc.be.model.Component checkoutResource) { + //try to update included VFCs, if it is either required as per configuration or an allotted resource + if ((isVfcUpgradeRequired && CollectionUtils.isNotEmpty(checkoutResource.getComponentInstances())) || isAllottedAndProxySupported) { + log.info("VFC upgrade is required: updating components of vf with name {}, invariantUUID {}, version {}. ", component.getName(), component.getInvariantUUID(), component.getVersion()); + Either updateCompositionRes = updateComposition(checkoutResource); + if (updateCompositionRes.isRight()) { + if (log.isErrorEnabled()) { + log.error(FAILED_TO_UPGRADE_COMPONENT, checkoutResource.getComponentType().name(), checkoutResource.getName(), checkoutResource.getInvariantUUID(), checkoutResource.getVersion(), "updateComposition", updateCompositionRes.right().value().getFormattedMessage()); } + outputHandler.addRecord(checkoutResource.getComponentType().name(), checkoutResource.getName(), checkoutResource.getUUID(), checkoutResource.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), updateCompositionRes.right().value().getFormattedMessage()); + return true; } - Either certifyRes = performFullCertification(checkouRes.left().value()); - if (certifyRes.isRight()) { - LOGGER.info(FAILED_TO_CHANGE_STATE_OF_COMPONENT, component.getName(), component.getInvariantUUID(), component.getVersion(), LifeCycleTransitionEnum.CERTIFY); - outputHandler.addRecord(checkouRes.left().value().getComponentType().name(), checkouRes.left().value().getName(), checkouRes.left().value().getInvariantUUID(), checkouRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), certifyRes.right().value().getFormattedMessage()); - return false; - } - updatedComponent = certifyRes.left().value(); - } else { - LOGGER.info("The version {} of derived from generic type {} is up to date. No need to upgrade component with name {}, invariantUUID {} and version {}. ", latestGenericTypes.get(derivedFromGenericType), derivedFromGenericType, component.getName(), component.getInvariantUUID(), component.getVersion()); } - LOGGER.info(UPGRADE_COMPONENT_SUCCEEDED, component.getComponentType().getValue(), component.getName(), component.getInvariantUUID(), component.getVersion()); - outputHandler.addRecord(updatedComponent.getComponentType().name(), updatedComponent.getName(), updatedComponent.getUUID(), updatedComponent.getUniqueId(), MigrationResult.MigrationStatus.COMPLETED.name(), updatedComponent.equals(component) ? UpgradeStatus.NOT_UPGRADED : UpgradeStatus.UPGRADED); - return true; + return false; } - private StorageOperationStatus upgradeNodeType(GraphVertex nodeTypeV, Map upgradedNodeTypesMap, List allCertifiedUids, List nodeTypes) { + private StorageOperationStatus upgradeNodeType(GraphVertex nodeTypeV, List allCertifiedUids) { StorageOperationStatus result = StorageOperationStatus.OK; - LOGGER.info("Starting upgrade node type with name {}, invariantUUID {}, version{}. ", nodeTypeV.getMetadataProperty(GraphPropertyEnum.NAME), nodeTypeV.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID), nodeTypeV.getMetadataProperty(GraphPropertyEnum.VERSION)); - LOGGER.info("Starting to find derived to for node type with name {}, invariantUUID {}, version{}. ", nodeTypeV.getMetadataProperty(GraphPropertyEnum.NAME), nodeTypeV.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID), nodeTypeV.getMetadataProperty(GraphPropertyEnum.VERSION)); + log.info("Starting upgrade node type with name {}, invariantUUID {}, version{}. ", nodeTypeV.getMetadataProperty(GraphPropertyEnum.NAME), nodeTypeV.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID), nodeTypeV.getMetadataProperty(GraphPropertyEnum.VERSION)); + log.info("Starting to find derived to for node type with name {}, invariantUUID {}, version{}. ", nodeTypeV.getMetadataProperty(GraphPropertyEnum.NAME), nodeTypeV.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID), nodeTypeV.getMetadataProperty(GraphPropertyEnum.VERSION)); Either, TitanOperationStatus> parentResourceRes = titanDao.getParentVertecies(nodeTypeV, EdgeLabelEnum.DERIVED_FROM, JsonParseFlagEnum.ParseMetadata); if (parentResourceRes.isRight() && parentResourceRes.right().value() != TitanOperationStatus.NOT_FOUND) { return DaoStatusConverter.convertTitanStatusToStorageStatus(parentResourceRes.right().value()); } - List derivedResourcesUid = new ArrayList<>(); - if (parentResourceRes.isLeft()) { - for (GraphVertex chV : parentResourceRes.left().value()) { - Optional op = allCertifiedUids.stream().filter(id -> id.equals((String) chV.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID))).findAny(); - if (op.isPresent()) { - derivedResourcesUid.add(chV); - } - } - } + List derivedResourcesUid = getAllDerivedGraphVertices(allCertifiedUids, parentResourceRes); String uniqueId = (String) nodeTypeV.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID); + Either getRes = toscaOperationFacade.getToscaElement(uniqueId); if (getRes.isRight()) { - LOGGER.info("failed to fetch element with uniqueId {} ", uniqueId); + log.info("failed to fetch element with uniqueId {} ", uniqueId); return getRes.right().value(); } - org.openecomp.sdc.be.model.Resource nt = (Resource) getRes.left().value(); - boolean isNeedToUpgrade = true; - if (upgradedNodeTypesMap.containsKey(nt.getToscaResourceName()) || nodeTypes.stream().anyMatch(p -> p.equals(nt.getToscaResourceName()))) { - isNeedToUpgrade = false; - } - if (isNeedToUpgrade) { - LOGGER.info("Starting to perform check out of node type with name {}, invariantUUID {}, version {}. ", nt.getName(), nt.getInvariantUUID(), nt.getVersion()); - Either checkouRes = lifecycleBusinessLogic.changeComponentState(nt.getComponentType(), nt.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT, changeInfo, true, false); - if (checkouRes.isRight()) { - return StorageOperationStatus.GENERAL_ERROR; - } - org.openecomp.sdc.be.model.Component upgradetComp = checkouRes.left().value(); - boolean res = performFullCertification(upgradetComp).isLeft(); - if (!res) { + Resource nodeType = (Resource)getRes.left().value(); + if (!upgradedNodeTypesMap.containsKey(nodeType.getToscaResourceName()) && !nodeTypes.stream().anyMatch(p -> p.equals(nodeType.getToscaResourceName())) + && !isNodeTypeUpgradeSucceeded((Resource) getRes.left().value())) { return StorageOperationStatus.GENERAL_ERROR; - } - upgradedNodeTypesMap.put(nt.getToscaResourceName(), upgradetComp); - titanDao.commit(); } for (GraphVertex chV : derivedResourcesUid) { - result = upgradeNodeType(chV, upgradedNodeTypesMap, allCertifiedUids, nodeTypes); - LOGGER.info("Upgrade node type with name {}, invariantUUID {}, version {} has been finished with the status {}", chV.getMetadataProperty(GraphPropertyEnum.NAME), chV.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID), chV.getMetadataProperty(GraphPropertyEnum.VERSION), result); + result = upgradeNodeType(chV, allCertifiedUids); + log.info("Upgrade node type with name {}, invariantUUID {}, version {} has been finished with the status {}", chV.getMetadataProperty(GraphPropertyEnum.NAME), chV.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID), chV.getMetadataProperty(GraphPropertyEnum.VERSION), result); } return result; } + private boolean isNodeTypeUpgradeSucceeded(Resource nodeType) { + log.info("Starting to perform check out of node type with name {}, invariantUUID {}, version {}. ", nodeType.getName(), nodeType.getInvariantUUID(), nodeType.getVersion()); + Either checkouRes = + lifecycleBusinessLogic.changeComponentState(nodeType.getComponentType(), nodeType.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT, changeInfo, true, false); + if (checkouRes.isRight()) { + log.info("Failed to check out node type with name {}, invariantUUID {} due to {}", nodeType.getName(), nodeType.getInvariantUUID(), checkouRes.right().value()); + return false; + } + if (performFullCertification(checkouRes.left().value()).isLeft()) { + upgradedNodeTypesMap.put(nodeType.getToscaResourceName(), checkouRes.left().value()); + titanDao.commit(); + return true; + } + return false; + } + + private List getAllDerivedGraphVertices(List allCertifiedUids, Either, TitanOperationStatus> parentResources) { + List derivedResourcesUid = new ArrayList<>(); + + if (parentResources.isLeft()) { + for (GraphVertex chV : parentResources.left().value()) { + Optional op = allCertifiedUids.stream().filter(id -> id.equals((String) chV.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID))).findAny(); + if (op.isPresent()) { + derivedResourcesUid.add(chV); + } + } + } + return derivedResourcesUid; + } + private Either performFullCertification(org.openecomp.sdc.be.model.Component component) { - LOGGER.info("Starting to perform full certification of {} with name {}, invariantUUID {}, version {}. ", + log.info("Starting to perform full certification of {} with name {}, invariantUUID {}, version {}. ", component.getComponentType().getValue(), component.getName(), component.getInvariantUUID(), component.getVersion()); Either changeStateEither = lifecycleBusinessLogic.changeComponentState(component.getComponentType(), component.getUniqueId(), user, LifeCycleTransitionEnum.CERTIFICATION_REQUEST, changeInfo, true, false); if (changeStateEither.isRight()) { - LOGGER.info(FAILED_TO_CHANGE_STATE_OF_COMPONENT, component.getName(), component.getInvariantUUID(), component.getVersion(), LifeCycleTransitionEnum.CERTIFICATION_REQUEST); + log.info(FAILED_TO_CHANGE_STATE_OF_COMPONENT, component.getName(), component.getInvariantUUID(), component.getVersion(), LifeCycleTransitionEnum.CERTIFICATION_REQUEST); return changeStateEither; } changeStateEither = lifecycleBusinessLogic.changeComponentState(component.getComponentType(), changeStateEither.left().value().getUniqueId(), user, LifeCycleTransitionEnum.START_CERTIFICATION, changeInfo, true, false); if (changeStateEither.isRight()) { - LOGGER.info(FAILED_TO_CHANGE_STATE_OF_COMPONENT, component.getName(), component.getInvariantUUID(), component.getVersion(), LifeCycleTransitionEnum.START_CERTIFICATION); + log.info(FAILED_TO_CHANGE_STATE_OF_COMPONENT, component.getName(), component.getInvariantUUID(), component.getVersion(), LifeCycleTransitionEnum.START_CERTIFICATION); return changeStateEither; } changeStateEither = lifecycleBusinessLogic.changeComponentState(component.getComponentType(), changeStateEither.left().value().getUniqueId(), user, LifeCycleTransitionEnum.CERTIFY, changeInfo, true, false); if (changeStateEither.isRight()) { - LOGGER.info(FAILED_TO_CHANGE_STATE_OF_COMPONENT, component.getName(), component.getInvariantUUID(), component.getVersion(), LifeCycleTransitionEnum.CERTIFY); + log.info(FAILED_TO_CHANGE_STATE_OF_COMPONENT, component.getName(), component.getInvariantUUID(), component.getVersion(), LifeCycleTransitionEnum.CERTIFY); } else { - LOGGER.info("Full certification of {} with name {}, invariantUUID {}, version {} finished successfully", + log.info("Full certification of {} with name {}, invariantUUID {}, version {} finished successfully", changeStateEither.left().value().getComponentType().getValue(), changeStateEither.left().value().getName(), changeStateEither.left().value().getInvariantUUID(), changeStateEither.left().value().getVersion()); } @@ -756,22 +930,24 @@ public class UpgradeMigration1710 implements PostMigration { } private boolean isGreater(String latestVersion, String currentVersion) { - if (latestVersion != null && currentVersion == null) + if (latestVersion != null && currentVersion == null) { return true; - if (latestVersion == null) + } + if (latestVersion == null) { return false; + } return Double.parseDouble(latestVersion) > Double.parseDouble(currentVersion); } private Either, TitanOperationStatus> getAllLatestCertifiedComponentUids(VertexTypeEnum vertexType, ComponentTypeEnum componentType) { - LOGGER.info("Starting to fetch all latest certified not checked out components with type {} upon upgrade migration 1710 process", componentType); + log.info("Starting to fetch all latest certified not checked out components with type {} upon upgrade migration 1710 process", componentType); Either, TitanOperationStatus> result = null; Map latestCertifiedMap = new HashMap<>(); Map latestNotCertifiedMap = new HashMap<>(); - Either, TitanOperationStatus> getComponentsRes = getAllLatestCertifiedComponents(vertexType, componentType); + Either, TitanOperationStatus> getComponentsRes = getAllLatestComponents(vertexType, componentType); if (getComponentsRes.isRight() && getComponentsRes.right().value() != TitanOperationStatus.NOT_FOUND) { - LOGGER.error("Failed to fetch all latest certified not checked out components with type {}. Status is {}. ", componentType, getComponentsRes.right().value()); + log.error("Failed to fetch all latest certified not checked out components with type {}. Status is {}. ", componentType, getComponentsRes.right().value()); result = Either.right(getComponentsRes.right().value()); } if (getComponentsRes.isRight()) { @@ -791,7 +967,7 @@ public class UpgradeMigration1710 implements PostMigration { return result; } - private Either, TitanOperationStatus> getAllLatestCertifiedComponents(VertexTypeEnum vertexType, ComponentTypeEnum componentType) { + private Either, TitanOperationStatus> getAllLatestComponents(VertexTypeEnum vertexType, ComponentTypeEnum componentType) { Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name()); @@ -799,8 +975,9 @@ public class UpgradeMigration1710 implements PostMigration { Map propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class); propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); - if (vertexType == VertexTypeEnum.TOPOLOGY_TEMPLATE && componentType == ComponentTypeEnum.RESOURCE) + if (vertexType == VertexTypeEnum.TOPOLOGY_TEMPLATE && componentType == ComponentTypeEnum.RESOURCE) { propertiesNotToMatch.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.CVFC.name()); + } return titanDao.getByCriteria(vertexType, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata); } @@ -815,7 +992,7 @@ public class UpgradeMigration1710 implements PostMigration { Either, TitanOperationStatus> highestResources = titanDao.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata); if (highestResources.isRight()) { TitanOperationStatus status = highestResources.right().value(); - LOGGER.debug("Failed to fetch resource with name {}. Status is {} ", nodeName, status); + log.debug("Failed to fetch resource with name {}. Status is {} ", nodeName, status); return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status)); } List resources = highestResources.left().value(); @@ -828,4 +1005,147 @@ public class UpgradeMigration1710 implements PostMigration { return Either.left(result); } + private void deleteMarkedComponents(NodeTypeEnum componentType, int toBeDeleted) { + Map, ResponseFormat>> cleanComponentsResult; + List cleanComponents = new ArrayList<>(); + cleanComponents.add(componentType); + try { + log.info("Trying to delete {} components of type {} marked as deleted", toBeDeleted, componentType); + cleanComponentsResult = componentsCleanBusinessLogic.cleanComponents(cleanComponents, true); + logDeleteResult(componentType, cleanComponentsResult.get(componentType)); + } + catch (Exception e) { + log.error("Exception occurred {}", e.getMessage()); + log.debug("Exception occurred", e); + } + } + + private void logDeleteResult(NodeTypeEnum type, Either, ResponseFormat> deleteResult) { + if (deleteResult == null) { + return; + } + if (deleteResult.isLeft()) { + log.info("Checked out {} versions are deleted successfully", type.getName()); + } + else { + log.info("Cleanup of checked out {} versions failed due to the error: {}", type.getName(), deleteResult.right().value().getFormattedMessage()); + } + } + + private void markCheckedOutResourceAsDeletedIfUpgradeFailed(String certUid, boolean isNotFailed) { + String checkedOutUniqueId = certifiedToNextCheckedOutUniqueId.remove(certUid); + if (!isNotFailed && checkedOutUniqueId != null) { + try { + //mark as deleted the checked out resource as this upgrade failed + ResponseFormat respFormat = resourceBusinessLogic.deleteResource(checkedOutUniqueId.toLowerCase(), user); + log.info("Checked out resource uniqueId = {} is marked as deleted, status: {}", checkedOutUniqueId, respFormat.getFormattedMessage()); + deleteResourcesIfLimitIsReached(); + } + catch (Exception e) { + log.error("Error occurred:", e); + } + } + } + + private void markCheckedOutServiceAsDeletedIfUpgradeFailed(String certUid, boolean isNotFailed) { + String checkedOutUniqueId = certifiedToNextCheckedOutUniqueId.remove(certUid); + if (!isNotFailed && checkedOutUniqueId != null) { + try { + //delete the checked out resource as this upgrade failed + ResponseFormat respFormat = serviceBusinessLogic.deleteService(checkedOutUniqueId.toLowerCase(), user); + log.info("Checked out service uniqueId = {} is marked as deleted, status: {}", checkedOutUniqueId, respFormat.getFormattedMessage()); + deleteServicesIfLimitIsReached(); + } catch (Exception e) { + log.error("Error occurred:", e); + } + } + + } + + void deleteResourcesIfLimitIsReached() { + markedAsDeletedResourcesCnt++; + if (markedAsDeletedResourcesCnt >= maxDeleteComponents) { + deleteMarkedComponents(NodeTypeEnum.Resource, markedAsDeletedResourcesCnt); + markedAsDeletedResourcesCnt = 0; + } + } + + void deleteServicesIfLimitIsReached() { + markedAsDeletedServicesCnt++; + if (markedAsDeletedServicesCnt >= maxDeleteComponents) { + deleteMarkedComponents(NodeTypeEnum.Service, markedAsDeletedServicesCnt); + markedAsDeletedServicesCnt = 0; + } + } + + boolean isLockDeleteOperationSucceeded() { + StorageOperationStatus status = componentsCleanBusinessLogic.lockDeleteOperation(); + + switch(status) { + case OK: + log.info("Lock delete operation succeeded"); + isCleanupLocked = true; + break; + case FAILED_TO_LOCK_ELEMENT: + log.info("Delete operation node is already locked"); + isCleanupLocked = isLockRetrySucceeded(); + break; + default: + log.error("Lock delete operation failed due to the error: {}", status); + isCleanupLocked = false; + break; + } + return isCleanupLocked; + } + + private boolean isLockRetrySucceeded() { + long startTime = System.currentTimeMillis(); + //try to lock the cleanup resource until configurable time interval is finished + while (System.currentTimeMillis() - startTime <= deleteLockTimeoutInSeconds * 1000) { + try { + //sleep one second and try lock again + Thread.sleep(1000); + if (componentsCleanBusinessLogic.lockDeleteOperation() == StorageOperationStatus.OK) { + return true; + } + } catch (InterruptedException e) { + log.error("Error occurred: {}", e.getMessage()); + } + } + return false; + } + + void unlockDeleteOperation() { + if (isCleanupLocked) { + try { + componentsCleanBusinessLogic.unlockDeleteOperation(); + log.info("Lock delete operation is canceled"); + isCleanupLocked = false; + } + catch (Exception e) { + log.debug("Failed to unlock delete operation", e); + log.error("Failed to unlock delete operation due to the error {}", e.getMessage()); + } + } + } + + private Either checkOutComponent(org.openecomp.sdc.be.model.Component component) { + log.info("Starting to perform check out of {} {}, uniqueId = {}", component.getComponentType(), component.getName(), component.getUniqueId()); + Either checkoutRes = + lifecycleBusinessLogic.changeComponentState(component.getComponentType(), component.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT, changeInfo, true, false); + if (checkoutRes.isLeft()) { + //add the uniqueId from "upgradeVf(String uniqueId)" and checkouRes's uniqueUID to the new map + certifiedToNextCheckedOutUniqueId.put(component.getUniqueId(), checkoutRes.left().value().getUniqueId()); + log.debug("Add checked out component uniqueId = {} produced from certified component uniqueId = {} to the checked out map", checkoutRes.left().value().getUniqueId(), component.getUniqueId()); + } + return checkoutRes; + } + + UpgradeStatus getVfUpgradeStatus(boolean isUpgraded, boolean isInstance) { + if (isUpgraded) { + return isInstance ? UpgradeStatus.UPGRADED_AS_INSTANCE : UpgradeStatus.UPGRADED; + } + return UpgradeStatus.NOT_UPGRADED; + } + } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1802/SdcCatalogMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1802/SdcCatalogMigration.java index 542db17425..d520eee1b8 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1802/SdcCatalogMigration.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1802/SdcCatalogMigration.java @@ -1,10 +1,6 @@ package org.openecomp.sdc.asdctool.migration.tasks.mig1802; -import java.math.BigInteger; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - +import fj.data.Either; import org.apache.commons.collections.ListUtils; import org.apache.tinkerpop.gremlin.structure.Direction; import org.openecomp.sdc.asdctool.migration.core.DBVersion; @@ -21,15 +17,18 @@ import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; import org.openecomp.sdc.be.model.jsontitan.operations.TopologyTemplateOperation; import org.openecomp.sdc.be.model.jsontitan.operations.ToscaElementOperation; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.stereotype.Component; -import fj.data.Either; +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; @Component public class SdcCatalogMigration implements Migration { - private static final Logger LOGGER = LoggerFactory.getLogger(SdcCatalogMigration.class); + private static final Logger LOGGER = Logger.getLogger(SdcCatalogMigration.class); private static final List EXCLUDE_TYPES = Arrays.asList(ResourceTypeEnum.VFCMT, ResourceTypeEnum.Configuration); private ToscaElementOperation toscaElementOperation; @@ -93,15 +92,23 @@ public class SdcCatalogMigration implements Migration { private Either, TitanOperationStatus> getAllCatalogVertices() { LOGGER.info("fetching all catalog resources"); return toscaElementOperation.getListOfHighestComponents(ComponentTypeEnum.RESOURCE, EXCLUDE_TYPES, JsonParseFlagEnum.ParseMetadata) + .right() + .bind(this::errOrEmptyListIfNotFound) .left() .bind(this::getAllCatalogVertices); } + private Either, TitanOperationStatus> errOrEmptyListIfNotFound(TitanOperationStatus err) { + return TitanOperationStatus.NOT_FOUND.equals(err) ? Either.left(new ArrayList<>()) : Either.right(err); + } + @SuppressWarnings("unchecked") private Either, TitanOperationStatus> getAllCatalogVertices(List allResourceCatalogVertices) { LOGGER.info("number of resources: {}", allResourceCatalogVertices.size()); LOGGER.info("fetching all catalog services"); return toscaElementOperation.getListOfHighestComponents(ComponentTypeEnum.SERVICE, EXCLUDE_TYPES, JsonParseFlagEnum.ParseMetadata) + .right() + .bind(this::errOrEmptyListIfNotFound) .left() .map(allServiceVertices -> ListUtils.union(allServiceVertices, allResourceCatalogVertices)); } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1806/ForwardPathMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1806/ForwardPathMigration.java new file mode 100644 index 0000000000..9634025867 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1806/ForwardPathMigration.java @@ -0,0 +1,253 @@ +/* + * 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.mig1806; + +import com.google.common.collect.ImmutableSet; +import com.thinkaurelius.titan.core.TitanVertex; +import fj.data.Either; +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.tinkerpop.gremlin.structure.Direction; +import org.apache.tinkerpop.gremlin.structure.Edge; +import org.apache.tinkerpop.gremlin.structure.Vertex; +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.jsongraph.utils.IdBuilderUtils; +import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; +import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; +import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.ComponentParametersView; +import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.User; +import org.openecomp.sdc.be.model.jsontitan.operations.ForwardingPathOperation; +import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade; +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; + +@org.springframework.stereotype.Component +public class ForwardPathMigration implements Migration { + + private TitanDao titanDao; + private UserAdminOperation userAdminOperation; + private ToscaOperationFacade toscaOperationFacade; + private User user = null; + + public ForwardPathMigration(TitanDao titanDao, + UserAdminOperation userAdminOperation, ToscaOperationFacade toscaOperationFacade) { + this.titanDao = titanDao; + this.userAdminOperation = userAdminOperation; + this.toscaOperationFacade = toscaOperationFacade; + } + + @Override + public String description() { + return "remove corrupted forwarding paths "; + } + + @Override + public DBVersion getVersion() { + return DBVersion.from(BigInteger.valueOf(1806), BigInteger.valueOf(0)); + } + + @Override + public MigrationResult migrate() { + final String userId = ConfigurationManager.getConfigurationManager().getConfiguration().getAutoHealingOwner(); + + Either userData = (Either) userAdminOperation + .getUserData(userId, false); + if (userData.isRight()) { + return MigrationResult.error( + "failed to delete unused forwarding paths. Failed to resolve user : " + userId + " error " + userData + .right().value()); + } else { + user = userData.left().value(); + } + StorageOperationStatus status = cleanAllServices(); + + return status == StorageOperationStatus.OK ? MigrationResult.success() + : MigrationResult.error("failed to remove corrupted forwarding paths . Error : " + status); + + } + + private StorageOperationStatus cleanAllServices() { + StorageOperationStatus status; + + Map hasProps = new EnumMap<>(GraphPropertyEnum.class); + hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name()); + Map hasNotProps = new HashMap<>(); + hasNotProps.put(GraphPropertyEnum.IS_DELETED, true); + status = titanDao + .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll) + .either(this::cleanServices, this::handleError); + return status; + } + + private StorageOperationStatus cleanServices(List containersV) { + StorageOperationStatus status = StorageOperationStatus.OK; + for (GraphVertex container : containersV) { + ComponentParametersView componentParametersView = new ComponentParametersView(); + componentParametersView.setIgnoreComponentInstances(false); + componentParametersView.setIgnoreCapabilities(false); + componentParametersView.setIgnoreRequirements(false); + componentParametersView.setIgnoreForwardingPath(false); + Either toscaElement = toscaOperationFacade + .getToscaElement(container.getUniqueId(), componentParametersView); + if (toscaElement.isRight()) { + return toscaElement.right().value(); + } + status = fixDataOnGraph(toscaElement.left().value()); + 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 fixDataOnGraph(Component component) { + if (!(component instanceof Service)){ + return StorageOperationStatus.OK; + } + Service service = (Service) component; + Either getResponse = titanDao.getVertexById(service.getUniqueId(), + JsonParseFlagEnum.NoParse); + if (getResponse.isRight()) { + return DaoStatusConverter.convertTitanStatusToStorageStatus(getResponse.right().value()); + + } + Set ciNames = new HashSet<>(); + if (service.getComponentInstances() != null && !service.getComponentInstances().isEmpty()) { + ciNames = service.getComponentInstances().stream().map(ci -> ci.getName()) + .collect(Collectors.toSet()); + } + GraphVertex componentVertex = getResponse.left().value(); + + GraphVertex toscaDataVertex; + Either groupVertexEither = titanDao.getChildVertex(componentVertex, + EdgeLabelEnum.FORWARDING_PATH, JsonParseFlagEnum.ParseJson); + if (groupVertexEither.isRight() && groupVertexEither.right().value() == TitanOperationStatus.NOT_FOUND) { + return StorageOperationStatus.OK; + } + if (groupVertexEither.isRight()) { + return DaoStatusConverter.convertTitanStatusToStorageStatus(groupVertexEither.right().value()); + } + toscaDataVertex = groupVertexEither.left().value(); + Map forwardingPaths = new HashMap<>( + (Map) toscaDataVertex.getJson()); + List toBeDeletedFP = new ArrayList<>(); + for (Map.Entry forwardingPathDataDefinition : forwardingPaths + .entrySet()) { + Set nodeNames = forwardingPathDataDefinition.getValue().getPathElements() + .getListToscaDataDefinition() + .stream().map(element -> ImmutableSet.of(element.getFromNode(), element.getToNode())) + .flatMap(set -> set.stream()).collect(Collectors.toSet()); + if (!ciNames.containsAll(nodeNames)) { + toBeDeletedFP.add(forwardingPathDataDefinition.getKey()); + } + } + if (toBeDeletedFP.isEmpty()) { + titanDao.rollback(); + return StorageOperationStatus.OK; + } + toBeDeletedFP.stream().forEach(fpKey -> forwardingPaths.remove(fpKey)); + toscaDataVertex.setJson(forwardingPaths); + Either updatevertexEither = updateOrCopyOnUpdate( + toscaDataVertex, componentVertex); + if (updatevertexEither.isRight()) { + titanDao.rollback(); + return DaoStatusConverter.convertTitanStatusToStorageStatus(updatevertexEither.right().value()); + } + titanDao.commit(); + return StorageOperationStatus.OK; + } + + private Either cloneDataVertex(GraphVertex dataVertex, GraphVertex toscaElementVertex, Edge edgeToRemove) { + EdgeLabelEnum label = EdgeLabelEnum.FORWARDING_PATH; + GraphVertex newDataVertex = new GraphVertex(dataVertex.getLabel()); + String id = IdBuilderUtils.generateChildId(toscaElementVertex.getUniqueId(), dataVertex.getLabel()); + newDataVertex.cloneData(dataVertex); + newDataVertex.setUniqueId(id); + + Either createVertex = titanDao.createVertex(newDataVertex); + if (createVertex.isRight()) { + return createVertex; + } + newDataVertex = createVertex.left().value(); + TitanOperationStatus createEdge = titanDao.createEdge(toscaElementVertex, newDataVertex, label, titanDao.getEdgeProperties(edgeToRemove)); + if (createEdge != TitanOperationStatus.OK) { + return Either.right(createEdge); + } + edgeToRemove.remove(); + return Either.left(newDataVertex); + } + + private Either updateOrCopyOnUpdate(GraphVertex dataVertex, GraphVertex toscaElementVertex ) { + EdgeLabelEnum label = EdgeLabelEnum.FORWARDING_PATH; + Iterator edges = dataVertex.getVertex().edges(Direction.IN, label.name()); + int edgeCount = 0; + Edge edgeToRemove = null; + while (edges.hasNext()) { + Edge edge = edges.next(); + ++edgeCount; + Vertex outVertex = edge.outVertex(); + String outId = (String) titanDao.getProperty((TitanVertex) outVertex, GraphPropertyEnum.UNIQUE_ID.getProperty()); + if (toscaElementVertex.getUniqueId().equals(outId)) { + edgeToRemove = edge; + } + } + if (edgeToRemove == null) { + return Either.right(TitanOperationStatus.GENERAL_ERROR); + } + switch (edgeCount) { + case 0: + // error + return Either.right(TitanOperationStatus.GENERAL_ERROR); + case 1: + // update + return titanDao.updateVertex(dataVertex); + default: + // copy on update + return cloneDataVertex(dataVertex, toscaElementVertex, edgeToRemove); + } + } +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1806/ResourceLifecycleMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1806/ResourceLifecycleMigration.java new file mode 100644 index 0000000000..7d741624c2 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1806/ResourceLifecycleMigration.java @@ -0,0 +1,138 @@ +package org.openecomp.sdc.asdctool.migration.tasks.mig1806; + +import fj.data.Either; +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.components.lifecycle.LifecycleBusinessLogic; +import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction; +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.JsonParseFlagEnum; +import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; +import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; +import org.openecomp.sdc.be.model.LifeCycleTransitionEnum; +import org.openecomp.sdc.be.model.LifecycleStateEnum; +import org.openecomp.sdc.be.model.User; +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.openecomp.sdc.exception.ResponseFormat; +import org.springframework.stereotype.Component; + +import java.math.BigInteger; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; + +@Component +public class ResourceLifecycleMigration implements Migration { + + private TitanDao titanDao; + private LifecycleBusinessLogic lifecycleBusinessLogic; + private UserAdminOperation userAdminOperation; + + private User user = null; + + private static final Logger log = Logger.getLogger(ResourceLifecycleMigration.class); + + public ResourceLifecycleMigration(TitanDao titanDao, LifecycleBusinessLogic lifecycleBusinessLogic, UserAdminOperation userAdminOperation) { + this.titanDao = titanDao; + this.lifecycleBusinessLogic = lifecycleBusinessLogic; + this.userAdminOperation = userAdminOperation; + } + + @Override + public String description() { + return "change resource lifecycle state from testing to certified"; + } + + @Override + public DBVersion getVersion() { + return DBVersion.from(BigInteger.valueOf(1806), BigInteger.valueOf(0)); + } + + @Override + public MigrationResult migrate() { + log.info("start change resource lifecycle states migration"); + final String userId = ConfigurationManager.getConfigurationManager().getConfiguration().getAutoHealingOwner(); + + Either userReq = userAdminOperation.getUserData(userId, false); + if (userReq.isRight()) { + log.error("Upgrade migration failed. User {} resolve failed: {} ", userId, userReq.right().value()); + return MigrationResult.error("failed to change lifecycle state of resources. Failed to resolve user : " + userId + " error " + userReq.right().value()); + } else { + user = userReq.left().value(); + log.info("User {} will perform upgrade operation with role {}", user.getUserId(), user.getRole()); + } + + StorageOperationStatus status = changeResourceLifecycleState(); + + return status == StorageOperationStatus.OK ? MigrationResult.success() : MigrationResult.error("failed to change lifecycle state of resources. Error : " + status); + } + + private StorageOperationStatus changeResourceLifecycleState() { + StorageOperationStatus status; + status = findResourcesAndChangeStatus(VertexTypeEnum.NODE_TYPE); + if (StorageOperationStatus.OK == status) { + status = findResourcesAndChangeStatus(VertexTypeEnum.TOPOLOGY_TEMPLATE); + } + titanDao.commit(); + return status; + } + + private StorageOperationStatus findResourcesAndChangeStatus(VertexTypeEnum type) { + StorageOperationStatus status; + Map props = new EnumMap<>(GraphPropertyEnum.class); + props.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); + props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.READY_FOR_CERTIFICATION.name()); + props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); + + Map hasNot = new EnumMap<>(GraphPropertyEnum.class); + hasNot.put(GraphPropertyEnum.IS_DELETED, true); + + log.info("findResourcesAndChangeStatus for type {} and state {}", type ,LifecycleStateEnum.READY_FOR_CERTIFICATION); + status = titanDao.getByCriteria(type, props, hasNot, JsonParseFlagEnum.ParseAll).either(this::changeState, this::handleError); + log.info("status {} for type {} and state {}", status, type ,LifecycleStateEnum.READY_FOR_CERTIFICATION); + + log.info("findResourcesAndChangeStatus for type {} and state {}", type ,LifecycleStateEnum.CERTIFICATION_IN_PROGRESS); + props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFICATION_IN_PROGRESS.name()); + status = titanDao.getByCriteria(type, props, hasNot, JsonParseFlagEnum.ParseAll).either(this::changeState, this::handleError); + log.info("status {} for type {} and state {}", status, type ,LifecycleStateEnum.CERTIFICATION_IN_PROGRESS); + + + return status; + } + + private StorageOperationStatus changeState(List resourcesV) { + StorageOperationStatus status = StorageOperationStatus.OK; + + for (GraphVertex resourceV : resourcesV) { + status = changeResourceState(resourceV); + if (status != StorageOperationStatus.OK) { + log.info("Failed to change state to certified of resource with id {} , continue to next, reset status", resourceV.getUniqueId() ); + status = StorageOperationStatus.OK; + } + } + return status; + } + + private StorageOperationStatus changeResourceState(GraphVertex resourceV) { + log.debug("Change state to certified to resource with id {} ", resourceV.getUniqueId() ); + + LifecycleChangeInfoWithAction changeInfo = new LifecycleChangeInfoWithAction("change resource state by migration"); + final Either changeComponentState = lifecycleBusinessLogic.changeComponentState(ComponentTypeEnum.RESOURCE, resourceV.getUniqueId(), user, LifeCycleTransitionEnum.CERTIFY, changeInfo, false, true); + return changeComponentState.isLeft() ? StorageOperationStatus.OK : StorageOperationStatus.GENERAL_ERROR; + } + + private StorageOperationStatus handleError(TitanOperationStatus err) { + log.debug("receive titan error {}", err); + return DaoStatusConverter.convertTitanStatusToStorageStatus(TitanOperationStatus.NOT_FOUND == err ? TitanOperationStatus.OK : err); + } + +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1806/SDCInstancesMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1806/SDCInstancesMigration.java new file mode 100644 index 0000000000..4786839250 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1806/SDCInstancesMigration.java @@ -0,0 +1,237 @@ +package org.openecomp.sdc.asdctool.migration.tasks.mig1806; + +import fj.data.Either; +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.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.ComponentInstanceDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.CompositionDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition; +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.ResourceTypeEnum; +import org.openecomp.sdc.be.model.jsontitan.enums.JsonConstantKeysEnum; +import org.openecomp.sdc.be.model.jsontitan.operations.NodeTemplateOperation; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter; +import org.openecomp.sdc.common.log.wrappers.Logger; +import org.springframework.stereotype.Component; + +import java.math.BigInteger; +import java.util.*; +import java.util.Map.Entry; + +@Component +public class SDCInstancesMigration implements Migration { + + private TitanDao titanDao; + private NodeTemplateOperation nodeTemplateOperation; + + private static final Logger log = Logger.getLogger(SDCInstancesMigration.class); + + private static final String ALLOTTED_CATEGORY = "Allotted Resource"; + + private static final List UUID_PROPS_NAMES = Arrays.asList("providing_service_uuid", "providing_service_uuid"); + + + public SDCInstancesMigration(TitanDao titanDao, NodeTemplateOperation nodeTemplateOperation) { + this.titanDao = titanDao; + this.nodeTemplateOperation = nodeTemplateOperation; + } + + @Override + public String description() { + return "connect instances in container to its origins"; + } + + @Override + public DBVersion getVersion() { + return DBVersion.from(BigInteger.valueOf(1806), BigInteger.valueOf(0)); + } + + @Override + public MigrationResult migrate() { + StorageOperationStatus status = connectAllContainers(); + + return status == StorageOperationStatus.OK ? MigrationResult.success() : MigrationResult.error("failed to create connection between instances and origins. Error : " + status); + } + + private StorageOperationStatus connectAllContainers() { + StorageOperationStatus status; + Map hasNotProps = new EnumMap<>(GraphPropertyEnum.class); + hasNotProps.put(GraphPropertyEnum.IS_DELETED, true); + hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.CVFC); + + status = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, null, hasNotProps, JsonParseFlagEnum.ParseAll) + .either(this::connectAll, this::handleError); + return status; + } + + private StorageOperationStatus handleError(TitanOperationStatus err) { + return DaoStatusConverter.convertTitanStatusToStorageStatus(TitanOperationStatus.NOT_FOUND == err ? TitanOperationStatus.OK : err); + } + + private StorageOperationStatus connectAll(List containersV) { + StorageOperationStatus status = StorageOperationStatus.OK; + for (GraphVertex container : containersV) { + status = handleOneContainer(container); + if (status != StorageOperationStatus.OK) { + break; + } + } + return status; + } + + private StorageOperationStatus handleOneContainer(GraphVertex containerV) { + StorageOperationStatus status = StorageOperationStatus.OK; + + boolean needConnectAllotted = false; + ComponentTypeEnum componentType = containerV.getType(); + Map instanceProperties = null; + if (componentType == ComponentTypeEnum.RESOURCE) { + Either subcategoryV = titanDao.getChildVertex(containerV, EdgeLabelEnum.CATEGORY, JsonParseFlagEnum.NoParse); + if (subcategoryV.isRight()) { + log.debug("Failed to fetch category vertex for resource {} error {} ", containerV.getUniqueId(), subcategoryV.right().value()); + return StorageOperationStatus.GENERAL_ERROR; + } + GraphVertex catV = subcategoryV.left().value(); + Map metadataProperties = catV.getMetadataProperties(); + + String name = (String) metadataProperties.get(GraphPropertyEnum.NAME); + if (name.equals(ALLOTTED_CATEGORY)) { + log.debug("Find allotted resource {}.", containerV.getUniqueId()); + needConnectAllotted = true; + Either, StorageOperationStatus> instProperties = getInstProperties(containerV); + if ( instProperties.isRight() ){ + return instProperties.right().value(); + } + instanceProperties = instProperties.left().value(); + } + } + Map jsonComposition = (Map) containerV.getJson(); + if (jsonComposition != null && !jsonComposition.isEmpty()) { + try { + status = connectInstances(containerV, needConnectAllotted, instanceProperties, jsonComposition); + + } finally { + if (status == StorageOperationStatus.OK) { + titanDao.commit(); + } else { + titanDao.rollback(); + } + } + } + return status; + } + + private Either, StorageOperationStatus> getInstProperties(GraphVertex containerV) { + Map instanceProperties; + Either instProps = titanDao.getChildVertex(containerV, EdgeLabelEnum.INST_PROPERTIES, JsonParseFlagEnum.ParseAll); + + if (instProps.isRight()) { + if (instProps.right().value() == TitanOperationStatus.NOT_FOUND) { + instanceProperties = new HashMap<>(); + } else { + log.debug("Failed to fetch instance properties vertex for resource {} error {} ", containerV.getUniqueId(), instProps.right().value()); + return Either.right(StorageOperationStatus.GENERAL_ERROR); + } + } else { + instanceProperties = (Map) instProps.left().value().getJson(); + } + return Either.left(instanceProperties); + } + + private StorageOperationStatus connectInstances(GraphVertex containerV, boolean needConnectAllotted, Map instanceProperties, + Map jsonComposition) { + StorageOperationStatus status = StorageOperationStatus.OK; + CompositionDataDefinition compositionDataDefinition = jsonComposition.get(JsonConstantKeysEnum.COMPOSITION.getValue()); + Map componentInstances = compositionDataDefinition.getComponentInstances(); + for (Map.Entry entry : componentInstances.entrySet()) { + status = handleInstance(containerV, needConnectAllotted, instanceProperties, entry); + if ( status != StorageOperationStatus.OK){ + if ( status == StorageOperationStatus.NOT_FOUND ){ + log.debug("reset status and continue"); + status = StorageOperationStatus.OK; + }else{ + log.debug("Failed handle instance. exit"); + break; + } + } + } + return status; + } + + private StorageOperationStatus handleInstance(GraphVertex containerV, boolean needConnectAllotted, Map instanceProperties, Map.Entry entry) { + ComponentInstanceDataDefinition instance = entry.getValue(); + StorageOperationStatus status = nodeTemplateOperation.createInstanceEdge(containerV, instance); + if (status != StorageOperationStatus.OK) { + if ( status == StorageOperationStatus.NOT_FOUND ){ + Boolean highest = (Boolean) containerV.getMetadataProperties().get(GraphPropertyEnum.IS_HIGHEST_VERSION); + log.debug("No origin for instance {} with ID {}. The component is highest ={}, Reset status and continue.. ", instance.getUniqueId(), instance.getComponentUid(), highest); + status = StorageOperationStatus.OK; + }else{ + log.debug("Failed to connect in container {} instance {} to origin {} error {} ", containerV.getUniqueId(), instance.getUniqueId(), instance.getComponentUid(), status); + return status; + } + } + if (needConnectAllotted) { + status = connectAllotedInstances(containerV, instanceProperties, instance); + } + return status; + } + + private StorageOperationStatus connectAllotedInstances(GraphVertex containerV, Map instanceProperties, ComponentInstanceDataDefinition instance) { + StorageOperationStatus status = StorageOperationStatus.OK; + if ( instanceProperties != null ){ + MapPropertiesDataDefinition mapPropertiesDataDefinition = instanceProperties.get(instance.getUniqueId()); + if ( mapPropertiesDataDefinition != null ){ + status = checkAllottedPropertyAndConnect(containerV, instance, mapPropertiesDataDefinition); + }else{ + log.debug("No isntances properties for instance {}", instance.getUniqueId()); + } + } + return status; + } + + private StorageOperationStatus checkAllottedPropertyAndConnect(GraphVertex containerV, ComponentInstanceDataDefinition instance, MapPropertiesDataDefinition mapPropertiesDataDefinition) { + Map mapToscaDataDefinition = mapPropertiesDataDefinition.getMapToscaDataDefinition(); + StorageOperationStatus status = StorageOperationStatus.OK; + Optional> findFirst = mapToscaDataDefinition + .entrySet() + .stream() + .filter(e -> UUID_PROPS_NAMES.contains(e.getKey())) + .findFirst(); + + if ( findFirst.isPresent() ){ + PropertyDataDefinition property = findFirst.get().getValue(); + String serviceUUID = property.getValue(); + if ( serviceUUID != null ){ + log.debug("Defined reference service on property {} value {} on instance {}", property.getName(), property.getValue(), instance.getUniqueId() ); + status = nodeTemplateOperation.createAllottedOfEdge(containerV.getUniqueId(), instance.getUniqueId(), serviceUUID); + if ( status != StorageOperationStatus.OK ){ + if ( status == StorageOperationStatus.NOT_FOUND ){ + Boolean highest = (Boolean) containerV.getMetadataProperties().get(GraphPropertyEnum.IS_HIGHEST_VERSION); + log.debug("No origin for allotted reference {} with UUID {}. the component highest = {}, Reset status and continue.. ", instance.getUniqueId(), serviceUUID, highest); + status = StorageOperationStatus.OK; + }else{ + log.debug("Failed to connect in container {} instance {} to allotted service {} error {} ", containerV.getUniqueId(), instance.getUniqueId(), instance.getComponentUid(), status); + return status; + } + } + }else{ + log.debug("No value for property {} on instance {}", property.getName(),instance.getUniqueId() ); + } + }else{ + log.debug("No sercific properties of dependencies for instance {}", instance.getUniqueId()); + } + return status; + } + +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1806/SdcArchiveMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1806/SdcArchiveMigration.java new file mode 100644 index 0000000000..bdab2cee2b --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1806/SdcArchiveMigration.java @@ -0,0 +1,76 @@ +package org.openecomp.sdc.asdctool.migration.tasks.mig1806; + +import fj.data.Either; +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.dao.jsongraph.GraphVertex; +import org.openecomp.sdc.be.dao.jsongraph.TitanDao; +import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; +import org.openecomp.sdc.be.dao.jsongraph.utils.IdBuilderUtils; +import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; +import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; +import org.openecomp.sdc.common.log.wrappers.Logger; +import org.springframework.stereotype.Component; + +import java.math.BigInteger; + +@Component +public class SdcArchiveMigration implements Migration { + private static final Logger logger = Logger.getLogger(SdcArchiveMigration.class); + + private TitanDao titanDao; + + public SdcArchiveMigration(TitanDao titanDao) { + this.titanDao = titanDao; + } + + @Override + public String description() { + return "add archive node for archiving/restoring components "; + } + + @Override + public DBVersion getVersion() { + return DBVersion.from(BigInteger.valueOf(1806), BigInteger.valueOf(0)); + } + + @Override + public MigrationResult migrate() { + TitanOperationStatus status = null; + try { + status = getOrCreateArchiveRoot(); + return status == TitanOperationStatus.OK ? MigrationResult.success() : MigrationResult.error("failed to create archive root node. error: " + status); + } finally { + commitOrRollBack(status); + } + } + + private void commitOrRollBack(TitanOperationStatus status) { + if (status == TitanOperationStatus.OK) { + titanDao.commit(); + } else { + titanDao.rollback(); + } + } + + private TitanOperationStatus getOrCreateArchiveRoot() { + logger.info("creating or getting catalog archive vertex"); + return titanDao.getVertexByLabel(VertexTypeEnum.ARCHIVE_ROOT) + .either(v -> TitanOperationStatus.OK, s -> this.createRootArchiveVertex()); + } + + private TitanOperationStatus createRootArchiveVertex() { + GraphVertex archiveRootVertex = new GraphVertex(VertexTypeEnum.ARCHIVE_ROOT); + archiveRootVertex.setUniqueId(IdBuilderUtils.generateUniqueId()); + archiveRootVertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.ARCHIVE_ROOT); + archiveRootVertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, archiveRootVertex.getUniqueId()); + + logger.info("Creating root archive vertex {}", archiveRootVertex.getUniqueId()); + + final Either vertexE = titanDao.createVertex(archiveRootVertex); + + return vertexE.isLeft() ? TitanOperationStatus.OK : vertexE.right().value(); + } + +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/servlets/EntryPoint.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/servlets/EntryPoint.java index 8899aa1e5e..e5ea76ad56 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/servlets/EntryPoint.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/servlets/EntryPoint.java @@ -20,18 +20,17 @@ package org.openecomp.sdc.asdctool.servlets; +import org.openecomp.sdc.common.log.wrappers.Logger; + import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - @Path("/entrypoint") public class EntryPoint { - private static Logger log = LoggerFactory.getLogger(EntryPoint.class.getName()); + private static Logger log = Logger.getLogger(EntryPoint.class.getName()); @GET @Path("test") diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/servlets/ExportImportTitanServlet.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/servlets/ExportImportTitanServlet.java index 62b59f13f3..c1f9335d59 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/servlets/ExportImportTitanServlet.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/servlets/ExportImportTitanServlet.java @@ -20,15 +20,13 @@ package org.openecomp.sdc.asdctool.servlets; -import java.io.BufferedOutputStream; -import java.io.BufferedReader; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.OutputStream; -import java.util.Map.Entry; -import java.util.Properties; +import com.thinkaurelius.titan.core.TitanGraph; +import org.apache.commons.configuration.BaseConfiguration; +import org.apache.commons.configuration.Configuration; +import org.apache.tinkerpop.gremlin.structure.io.graphml.GraphMLWriter; +import org.glassfish.jersey.media.multipart.FormDataParam; +import org.openecomp.sdc.asdctool.Utils; +import org.openecomp.sdc.common.log.wrappers.Logger; import javax.ws.rs.Consumes; import javax.ws.rs.GET; @@ -36,22 +34,15 @@ import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; - -import org.apache.commons.configuration.BaseConfiguration; -import org.apache.commons.configuration.Configuration; -import org.apache.tinkerpop.gremlin.structure.io.graphml.GraphMLWriter; -import org.glassfish.jersey.media.multipart.FormDataParam; -import org.openecomp.sdc.asdctool.Utils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.thinkaurelius.titan.core.TitanGraph; +import java.io.*; +import java.util.Map.Entry; +import java.util.Properties; //import com.tinkerpop.blueprints.util.io.graphml.GraphMLWriter; @Path("/titan") public class ExportImportTitanServlet { - private static Logger log = LoggerFactory.getLogger(ExportImportTitanServlet.class.getName()); + private static Logger log = Logger.getLogger(ExportImportTitanServlet.class.getName()); @GET @Path("export") diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/simulator/tenant/ImportCassandraTableTool.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/simulator/tenant/ImportCassandraTableTool.java index 9ea2d5ad49..045ac5cafd 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/simulator/tenant/ImportCassandraTableTool.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/simulator/tenant/ImportCassandraTableTool.java @@ -1,9 +1,5 @@ package org.openecomp.sdc.asdctool.simulator.tenant; -import java.util.HashMap; -import java.util.Map; -import java.util.function.Consumer; - import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.common.api.ConfigurationSource; import org.openecomp.sdc.common.impl.ExternalConfiguration; @@ -11,6 +7,10 @@ import org.openecomp.sdc.common.impl.FSConfigurationSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; + /** * Main class of utility imports CSV file into the specified table * The old stuff of the table is removed. diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/simulator/tenant/OperationalEnvironment.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/simulator/tenant/OperationalEnvironment.java index 1caf073c79..bf192f352b 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/simulator/tenant/OperationalEnvironment.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/simulator/tenant/OperationalEnvironment.java @@ -1,8 +1,7 @@ package org.openecomp.sdc.asdctool.simulator.tenant; -import org.openecomp.sdc.be.datatypes.enums.EnvironmentStatusEnum; - import com.opencsv.bean.CsvBindByPosition; +import org.openecomp.sdc.be.datatypes.enums.EnvironmentStatusEnum; /** * Represents line in CSV file should be imported into "operationalenvironment" table. diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/simulator/tenant/OperationalEvnironmentImportHandler.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/simulator/tenant/OperationalEvnironmentImportHandler.java index d4dbddde3f..baee8aa060 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/simulator/tenant/OperationalEvnironmentImportHandler.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/simulator/tenant/OperationalEvnironmentImportHandler.java @@ -1,12 +1,6 @@ package org.openecomp.sdc.asdctool.simulator.tenant; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.List; -import java.util.stream.Collectors; - +import com.opencsv.bean.CsvToBeanBuilder; import org.openecomp.sdc.be.dao.cassandra.OperationalEnvironmentDao; import org.openecomp.sdc.be.dao.cassandra.schema.Table; import org.openecomp.sdc.be.resources.data.OperationalEnvironmentEntry; @@ -14,7 +8,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import com.opencsv.bean.CsvToBeanBuilder; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.List; +import java.util.stream.Collectors; /** * Imports CSV file into diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/utils/ConsoleWriter.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/utils/ConsoleWriter.java new file mode 100644 index 0000000000..934f462ca8 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/utils/ConsoleWriter.java @@ -0,0 +1,37 @@ +package org.openecomp.sdc.asdctool.utils; + +public class ConsoleWriter { + private static String tabbedData(String data, int min) { + // System.out.println(); //for debug + + int tabcount = 0; + int len = 8 * min; + + while (data.length() < len) { + tabcount++; + len = len - 8; + } + + // System.out.println("debug: tabcount=" + tabcount); + // System.out.print("debug adding tabs... "); + for (int x = 0; x < tabcount; x++) { + // System.out.print("tab "); + data = data + "\t"; + } + // System.out.println(); //for debug + + return data; + } + + public static void dataLine(String name) { + dataLine(name, null); + } + + public static void dataLine(String name, Object data) { + if (data != null) { + System.out.println(tabbedData(name, 3) + data); + } else { + System.out.println(tabbedData(name, 3)); + } + } +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/utils/ReportWriter.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/utils/ReportWriter.java new file mode 100644 index 0000000000..64e9290f72 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/utils/ReportWriter.java @@ -0,0 +1,50 @@ +package org.openecomp.sdc.asdctool.utils; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import org.openecomp.sdc.be.dao.jsongraph.utils.JsonParserUtils; + +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class ReportWriter { + FileWriter file; + public ReportWriter(String reportName) { + + StringBuilder sb = new StringBuilder(); + Path path = Paths.get("/var/tmp/"); + if ( path.toFile().exists() ) { + sb.append("/var/tmp/"); + } + sb.append("report_").append(reportName).append("_").append(System.currentTimeMillis()).append(".json"); + String fileName = sb.toString(); + try { + file = new FileWriter(fileName); + } catch (IOException e) { + System.out.println("Failed to create report file. " + e.getMessage()); + } + } + + public void report(Object objectToWrite) throws IOException { + if (file != null) { + JsonParser parser = new JsonParser(); + JsonObject json = parser.parse(JsonParserUtils.toJson(objectToWrite)).getAsJsonObject(); + + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + String prettyJson = gson.toJson(json); + + file.write(prettyJson); + file.flush(); + } + } + + public void close() throws IOException { + if (file != null) { + file.close(); + } + } +} diff --git a/asdctool/src/main/resources/application-context.xml b/asdctool/src/main/resources/application-context.xml index cf5ae5a767..c9a13df44a 100644 --- a/asdctool/src/main/resources/application-context.xml +++ b/asdctool/src/main/resources/application-context.xml @@ -1,7 +1,7 @@ diff --git a/asdctool/src/main/resources/config/configuration.yaml b/asdctool/src/main/resources/config/configuration.yaml index c92827e558..14fd1c5123 100644 --- a/asdctool/src/main/resources/config/configuration.yaml +++ b/asdctool/src/main/resources/config/configuration.yaml @@ -6,7 +6,7 @@ identificationHeaderFields: - HTTP_CSP_WSTYPE # catalog backend hostname -beFqdn: localhost +beFqdn: 192.168.33.10 # sdccatalog.att.com # catalog backend http port @@ -23,18 +23,25 @@ beSslPort: 8443 version: 1.0 released: 2012-11-30 -toscaConformanceLevel: 5.0 +toscaConformanceLevel: 8.0 minToscaConformanceLevel: 3.0 # These values are necessary for running upgrade migration 1710.0 process +enableAutoHealing: false appVersion: 1.1.0 artifactGeneratorConfig: Artifact-Generator.properties resourcesForUpgrade: - 5.0: - - tosca.nodes.Root + 8.0: + - org.openecomp.resource.cp.extCP + - tosca.nodes.network.Network + - tosca.nodes.network.Port + - org.openecomp.resource.cp.nodes.network.SubInterface skipUpgradeFailedVfs: true skipUpgradeVSPs: true autoHealingOwner: jh0003 +supportAllottedResourcesAndProxy: false +deleteLockTimeoutInSeconds: 60 +maxDeleteComponents: 10 titanCfgFile: src\main\resources\config\titan.properties titanMigrationKeySpaceCfgFile: src\main\resources\config\titan-migration.properties @@ -84,20 +91,22 @@ neo4j: password: "12345" cassandraConfig: - cassandraHosts: ['localhost'] - localDataCenter: datacenter1 + cassandraHosts: [192.168.33.10] + localDataCenter: DC-AIO-Ubuntu1 reconnectTimeout : 30000 - authenticate: false - username: koko - password: bobo - ssl: false - truststorePath : /path/path - truststorePassword : 123123 + authenticate: true + username: asdc_user + password: Aa1234%^! + ssl: true + truststorePath : C:/Users/im453s/git/vagrant-sdc-all-in-one/tmp.trust + truststorePassword : Aa123456 keySpaces: - - { name: sdcaudit, replicationStrategy: SimpleStrategy, replicationInfo: ['1']} - - { name: sdcartifact, replicationStrategy: SimpleStrategy, replicationInfo: ['1']} - - { name: sdccomponent, replicationStrategy: SimpleStrategy, replicationInfo: ['1']} - - { name: sdcrepository, replicationStrategy: SimpleStrategy, replicationInfo: ['1']} + - { name: dox, replicationStrategy: NetworkTopologyStrategy, replicationInfo: ['DC-AIO-Ubuntu1', '1']} + - { name: sdcaudit, replicationStrategy: NetworkTopologyStrategy, replicationInfo: ['DC-AIO-Ubuntu1', '1']} + - { name: sdcartifact, replicationStrategy: NetworkTopologyStrategy, replicationInfo: ['DC-AIO-Ubuntu1', '1']} + - { name: sdccomponent, replicationStrategy: NetworkTopologyStrategy, replicationInfo: ['DC-AIO-Ubuntu1', '1']} + - { name: sdcrepository, replicationStrategy: NetworkTopologyStrategy, replicationInfo: ['DC-AIO-Ubuntu1', '1']} + #Application-specific settings of ES elasticSearch: @@ -203,6 +212,7 @@ toscaArtifacts: type: TOSCA_CSAR description: TOSCA definition package of the asset + #Informational artifacts placeHolder excludeResourceCategory: - Generic @@ -329,7 +339,7 @@ serviceDeploymentArtifacts: - xml AAI_VF_INSTANCE_MODEL: acceptedTypes: - - xml + - xml OTHER: acceptedTypes: diff --git a/asdctool/src/main/resources/config/titan.properties b/asdctool/src/main/resources/config/titan.properties index 5411a44224..5f22a08837 100644 --- a/asdctool/src/main/resources/config/titan.properties +++ b/asdctool/src/main/resources/config/titan.properties @@ -1,11 +1,28 @@ -storage.backend=cassandra -storage.hostname=localhost +storage.backend=cassandrathrift +storage.hostname=192.168.33.10 storage.port=9160 +storage.username=asdc_user +storage.password=Aa1234%^! +storage.connection-timeout=10000 +storage.cassandra.keyspace=sdctitan + +storage.cassandra.ssl.enabled=true +storage.cassandra.ssl.truststore.location=C:\\gitWork\\vagrant-sdc-all-in-one\\mytmp.trust +storage.cassandra.ssl.truststore.password=Aa123456 + +storage.cassandra.read-consistency-level=LOCAL_QUORUM +storage.cassandra.write-consistency-level=LOCAL_QUORUM +storage.cassandra.replication-strategy-class=org.apache.cassandra.locator.NetworkTopologyStrategy +storage.cassandra.replication-strategy-options=DC-sdc-iltlv633,1 +storage.cassandra.astyanax.local-datacenter=DC-sdc-iltlv633 + cache.db-cache = false cache.db-cache-clean-wait = 20 cache.db-cache-time = 180000 cache.db-cache-size = 0.5 -cache.tx-cache-size = 500000 -storage.cassandra.keyspace=sdctitan +cache.tx-cache-size = 1000000 + +storage.lock.retries=5 +storage.lock.wait-time=500 diff --git a/asdctool/src/main/resources/scripts/deleteComponentTool.sh b/asdctool/src/main/resources/scripts/deleteComponentTool.sh new file mode 100644 index 0000000000..afdf8166f5 --- /dev/null +++ b/asdctool/src/main/resources/scripts/deleteComponentTool.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +############################## +# Distribution Status Update 1707 +############################## + +CURRENT_DIR=`pwd` +BASEDIR=$(dirname $0) + +if [ ${BASEDIR:0:1} = "/" ] +then + FULL_PATH=$BASEDIR +else + FULL_PATH=$CURRENT_DIR/$BASEDIR +fi + +source ${FULL_PATH}/baseOperation.sh + +mainClass="org.openecomp.sdc.asdctool.main.DeleteComponentTool" + +command="java $JVM_LOG_FILE -cp $JARS $mainClass $@" +echo $command + +$command +result=$? + + + +echo "***********************************" +echo "***** $result *********************" +echo "***********************************" + +exit $result + + diff --git a/asdctool/src/main/resources/scripts/generateCsar.sh b/asdctool/src/main/resources/scripts/generateCsar.sh new file mode 100644 index 0000000000..c74168823c --- /dev/null +++ b/asdctool/src/main/resources/scripts/generateCsar.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +############################## +# Distribution Status Update 1707 +############################## + +CURRENT_DIR=`pwd` +BASEDIR=$(dirname $0) + +if [ ${BASEDIR:0:1} = "/" ] +then + FULL_PATH=$BASEDIR +else + FULL_PATH=$CURRENT_DIR/$BASEDIR +fi + +source ${FULL_PATH}/baseOperation.sh + +mainClass="org.openecomp.sdc.asdctool.main.CsarGeneratorTool" + +command="java $JVM_LOG_FILE -cp $JARS $mainClass $@" +echo $command + +$command +result=$? + + + +echo "***********************************" +echo "***** $result *********************" +echo "***********************************" + +exit $result + + diff --git a/asdctool/src/main/resources/scripts/vrfObjectFix.sh b/asdctool/src/main/resources/scripts/vrfObjectFix.sh new file mode 100644 index 0000000000..3f875fa136 --- /dev/null +++ b/asdctool/src/main/resources/scripts/vrfObjectFix.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +############################## +# Data Migration +############################## + +CURRENT_DIR=`pwd` +BASEDIR=$(dirname $0) + +if [ ${BASEDIR:0:1} = "/" ] +then + FULL_PATH=$BASEDIR +else + FULL_PATH=$CURRENT_DIR/$BASEDIR +fi + +source ${FULL_PATH}/baseOperation.sh + +mainClass="org.openecomp.sdc.asdctool.main.VrfObjectFixMenu" + +command="java $JVM_LOG_FILE -Xmx6000M -cp $JARS $mainClass $@" +echo $command + +$command +result=$? + + + +echo "***********************************" +echo "***** $result *********************" +echo "***********************************" + +exit $result + + -- cgit 1.2.3-korg