diff options
Diffstat (limited to 'asdctool')
91 files changed, 4408 insertions, 113 deletions
diff --git a/asdctool/pom.xml b/asdctool/pom.xml index da1b9160fd..a7a122f43b 100644 --- a/asdctool/pom.xml +++ b/asdctool/pom.xml @@ -48,17 +48,100 @@ <scope>compile</scope> </dependency> + <dependency> <groupId>org.openecomp.sdc</groupId> <artifactId>catalog-be</artifactId> <version>${project.version}</version> <!-- Comment Out in order to debug in eclipse --> - <classifier>classes</classifier> + <classifier>classes</classifier> + + <exclusions> + <exclusion> + <groupId>org.openecomp.ecompsdkos</groupId> + <artifactId>epsdk-fw</artifactId> + </exclusion> + + <exclusion> + <groupId>org.openecomp.sdc.common</groupId> + <artifactId>openecomp-sdc-artifact-generator-api</artifactId> + </exclusion> + <exclusion> + <groupId>org.openecomp.sdc.common</groupId> + <artifactId>openecomp-sdc-artifact-generator-core</artifactId> + </exclusion> + + <exclusion> + <groupId>org.openecomp.sdc.common</groupId> + <artifactId>openecomp-common-lib</artifactId> + </exclusion> + </exclusions> + + <scope>compile</scope> + </dependency> + + <dependency> + <groupId>org.openecomp.ecompsdkos</groupId> + <artifactId>epsdk-fw</artifactId> + <version>${ecomp.version}</version> <scope>compile</scope> + <exclusions> + <exclusion> + <groupId>com.att.nsa</groupId> + <artifactId>cambriaClient</artifactId> + </exclusion> + + <exclusion> + <artifactId>slf4j-log4j12</artifactId> + <groupId>org.slf4j</groupId> + </exclusion> + </exclusions> </dependency> + + <!--Artifact Generator--> + <dependency> + <groupId>org.openecomp.sdc.common</groupId> + <artifactId>openecomp-sdc-artifact-generator-api</artifactId> + <version>${artifact-generator-api.version}</version> + <scope>compile</scope> + <exclusions> + <exclusion> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </exclusion> + </exclusions> + </dependency> + + <dependency> + <groupId>org.openecomp.sdc.common</groupId> + <artifactId>openecomp-sdc-artifact-generator-core</artifactId> + <version>${artifact-generator-core.version}</version> + <scope>compile</scope> + <exclusions> + <exclusion> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </exclusion> + </exclusions> + </dependency> + + <dependency> + <groupId>org.openecomp.sdc.common</groupId> + <artifactId>openecomp-common-lib</artifactId> + <version>${dox-common-lib.version}</version> + <type>pom</type> + <exclusions> + <exclusion> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </exclusion> + </exclusions> + </dependency> + + <!-- ASDC dependencies end --> <dependency> <groupId>org.eclipse.jetty</groupId> @@ -76,6 +159,12 @@ </dependency> <dependency> + <groupId>commons-cli</groupId> + <artifactId>commons-cli</artifactId> + <version>1.4</version> + </dependency> + + <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlet</artifactId> <version>${jetty.version}</version> @@ -417,6 +506,20 @@ <version>${junit.version}</version> <scope>test</scope> </dependency> + + <dependency> + <groupId>org.testng</groupId> + <artifactId>testng</artifactId> + <version>${testng.version}</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-all</artifactId> + <version>${mockito.version}</version> + <scope>test</scope> + </dependency> <!-- testing end --> </dependencies> 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 2ad4faceb7..1ce976dbe5 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 @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl; 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 ace1c038f2..11d52023f8 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 @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl; import java.util.ArrayList; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/Migration1707Task.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/Migration1707Task.java index 92e185dbd7..2c19c94cff 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/Migration1707Task.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/Migration1707Task.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration; /** diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/MigrationException.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/MigrationException.java index f54fccc0c1..8e958b9ad1 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/MigrationException.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/MigrationException.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration; public class MigrationException extends RuntimeException { diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/MigrationMsg.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/MigrationMsg.java index 3f8b55b3a5..e743e5387b 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/MigrationMsg.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/MigrationMsg.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration; public enum MigrationMsg { diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/MigrationOperationUtils.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/MigrationOperationUtils.java index a4595b2ec8..7a34521d93 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/MigrationOperationUtils.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/MigrationOperationUtils.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration; import com.thinkaurelius.titan.core.PropertyKey; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1604/AppConfig.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1604/AppConfig.java index 25132caf28..3218ed262e 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1604/AppConfig.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1604/AppConfig.java @@ -27,6 +27,7 @@ import org.openecomp.sdc.asdctool.impl.migration.v1610.TitanFixUtils; import org.openecomp.sdc.asdctool.impl.migration.v1610.ToscaArtifactsAlignment; import org.openecomp.sdc.asdctool.impl.migration.v1702.DataTypesUpdate; import org.openecomp.sdc.asdctool.impl.migration.v1702.Migration1702; +import org.openecomp.sdc.asdctool.impl.migration.v1707.Migration1707ArtifactUuidFix; import org.openecomp.sdc.asdctool.impl.migration.v1707.Migration1707RelationsFix; import org.openecomp.sdc.asdctool.impl.migration.v1707.Migration1707VnfFix; import org.openecomp.sdc.be.auditing.api.IAuditingManager; @@ -588,6 +589,11 @@ public class AppConfig { public Migration1707VnfFix migration1707VnfFix() { return new Migration1707VnfFix(); } + @Bean(name = "migration1707UuidFix") + public Migration1707ArtifactUuidFix migration1707ArtifactUuidFix() { + return new Migration1707ArtifactUuidFix(); + } + // @Bean(name = "migration1707relationsFix") // public Migration1707RelationsFix migration1707RelationsFix() { diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1604/ServiceMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1604/ServiceMigration.java index ee5171d8ab..0ef435e1c3 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1604/ServiceMigration.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1604/ServiceMigration.java @@ -706,7 +706,7 @@ public class ServiceMigration { Either<ImmutablePair<ComponentInstanceData, GraphEdge>, TitanOperationStatus> reqInst = titanGenericDao.getParentNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.RelationshipInst), rel.getUniqueId(), GraphEdgeLabels.RELATIONSHIP_INST, NodeTypeEnum.ResourceInstance, ComponentInstanceData.class); if (reqInst.isRight()) { - log.debug("updateRelations : failed to fetch capabilty component instance for relation {}, error {}", rel.getUniqueId(), reqInst.right().value()); + log.debug("updateRelations : failed to fetch capability component instance for relation {}, error {}", rel.getUniqueId(), reqInst.right().value()); return false; } ComponentInstanceData requirementInstanceData = reqInst.left().value().getLeft(); @@ -917,6 +917,9 @@ public class ServiceMigration { case CP: originType = OriginTypeEnum.CP; break; + case CVFC: + originType = OriginTypeEnum.CVFC; + break; default: log.debug("updateComponentInstanceType failed, no supported resource type {} for origin resource with id {}", resourceType, originId); return false; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/DistributionStatusUpdate.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/DistributionStatusUpdate.java index 7c0b57fb1d..744965ff68 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/DistributionStatusUpdate.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/DistributionStatusUpdate.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707; import java.util.EnumMap; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707.java index 84214cc0b0..1012ebddd4 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707; import org.openecomp.sdc.asdctool.impl.migration.Migration1707Task; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707ArtifactUuidFix.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707ArtifactUuidFix.java new file mode 100644 index 0000000000..b3f138241e --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707ArtifactUuidFix.java @@ -0,0 +1,585 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.asdctool.impl.migration.v1707; + +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.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +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.GroupDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.MapGroupsDataDefinition; +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.datatypes.tosca.ToscaDataDefinition; +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.jsontitan.datamodel.TopologyTemplate; +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.StorageOperationStatus; +import org.openecomp.sdc.common.api.ArtifactTypeEnum; +import org.openecomp.sdc.common.api.Constants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; + +import fj.data.Either; + +@org.springframework.stereotype.Component("migration1707UuidFix") +public class Migration1707ArtifactUuidFix { + + @Autowired + private TitanDao titanDao; + + @Autowired + private ToscaOperationFacade toscaOperationFacade; + + private static Logger log = LoggerFactory.getLogger(Migration1707ArtifactUuidFix.class.getName()); + + public boolean migrate(String fixComponent, String runMode) { + List<Resource> vfLst = new ArrayList<>(); + List<Service> serviceList = new ArrayList<>(); + + long time = System.currentTimeMillis(); + + if (fixComponent.equals("vf_only")) { + if (fetchFaultVf(fixComponent, vfLst, time) == false) { + return false; + } + } else { + if (fetchServices(fixComponent, serviceList, time) == false) { + return false; + } + } + if (runMode.equals("service_vf") || runMode.equals("fix")) { + log.info("Mode {}. Find problem VFs", runMode); + if (fetchVf(serviceList, vfLst, time) == false) { + log.info("Mode {}. Find problem VFs finished with failure", runMode); + return false; + } + log.info("Mode {}. Find problem VFs finished with success", runMode); + } + if (runMode.equals("fix") || runMode.equals("fix_only_services")) { + log.info("Mode {}. Start fix", runMode); + if (fix(vfLst, serviceList) == false) { + log.info("Mode {}. Fix finished with failure", runMode); + return false; + } + log.info("Mode {}. Fix finished with success", runMode); + } + + return true; + } + + private boolean fetchFaultVf(String fixComponent, List<Resource> vfLst, long time) { + log.info("Find fault VF "); + Writer writer = null; + try { + String fileName = "fault_" + time + ".csv"; + writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8")); + writer.write("vf name, vf id, state, version\n"); + + Map<GraphPropertyEnum, Object> hasProps = new HashMap<>(); + hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); + hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF.name()); + + Map<GraphPropertyEnum, Object> hasNotProps = new HashMap<>(); + hasNotProps.put(GraphPropertyEnum.IS_DELETED, true); + log.info("Try to fetch resources with properties {} and not {}", hasProps, hasNotProps); + + Either<List<GraphVertex>, TitanOperationStatus> servicesByCriteria = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll); + if (servicesByCriteria.isRight()) { + log.info("Failed to fetch resources {}", servicesByCriteria.right().value()); + return false; + } + List<GraphVertex> resources = servicesByCriteria.left().value(); + for (GraphVertex gv : resources) { + ComponentParametersView filter = new ComponentParametersView(true); + filter.setIgnoreComponentInstances(false); + filter.setIgnoreArtifacts(false); + filter.setIgnoreGroups(false); + + Either<Resource, StorageOperationStatus> toscaElement = toscaOperationFacade.getToscaElement(gv.getUniqueId()); + if (toscaElement.isRight()) { + log.info("Failed to fetch resources {} {}", gv.getUniqueId(), toscaElement.right().value()); + return false; + } + + Resource resource = toscaElement.left().value(); + String resourceName = resource.getName(); + Map<String, ArtifactDefinition> deploymentArtifacts = resource.getDeploymentArtifacts(); + List<GroupDefinition> groups = resource.getGroups(); + if (groups == null || groups.isEmpty()) { + log.info("No groups for resource {} id {} ", resourceName, gv.getUniqueId()); + continue; + } + boolean isProblematic = false; + for (GroupDefinition gr : groups) { + if (gr.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) { + if (isProblematicGroup(gr, resourceName, deploymentArtifacts)) { + isProblematic = true; + break; + } + } + } + if (isProblematic) { + vfLst.add(resource); + writeModuleResultToFile(writer, resource, null); + writer.flush(); + } + titanDao.commit(); + } + + } 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 true; + } + + private boolean fetchVf(List<Service> serviceList, List<Resource> vfLst, long time) { + log.info("Find problem VF "); + if (serviceList.isEmpty()) { + log.info("No services as input"); + return true; + } + Writer writer = null; + try { + String fileName = "problemVf_" + time + ".csv"; + writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8")); + writer.write("vf name, vf id, state, version, example service name\n"); + Set<String> vfIds = new HashSet<>(); + for (Service service : serviceList) { + List<ComponentInstance> componentInstances = service.getComponentInstances(); + for (ComponentInstance ci : componentInstances) { + if (!vfIds.contains(ci.getComponentUid())) { + vfIds.add(ci.getComponentUid()); + Either<Resource, StorageOperationStatus> toscaElement = toscaOperationFacade.getToscaElement(ci.getComponentUid()); + if (toscaElement.isRight()) { + log.info("Failed to fetch resource {} {}", ci.getComponentUid(), toscaElement.right().value()); + return false; + } + Resource resource = toscaElement.left().value(); + vfLst.add(resource); + writeModuleResultToFile(writer, resource, service); + writer.flush(); + titanDao.commit(); + } + } + } + log.info("output file with list of Vf : {}", fileName); + } catch (Exception e) { + log.info("Failed to fetch services ", e); + return false; + } finally { + titanDao.commit(); + try { + writer.flush(); + writer.close(); + } catch (Exception ex) { + /* ignore */} + } + return true; + } + + private boolean fetchServices(String fixServices, List<Service> serviceList, long time) { + log.info("Find problem Services {}", fixServices); + Writer writer = null; + + try { + String fileName = "problemService_" + time + ".csv"; + writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8")); + writer.write("service name, service id, state, version\n"); + + Map<GraphPropertyEnum, Object> hasProps = new HashMap<>(); + hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name()); + if (fixServices.equals("distributed_only")) { + hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); + hasProps.put(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTED.name()); + } + + Map<GraphPropertyEnum, Object> hasNotProps = new HashMap<>(); + hasNotProps.put(GraphPropertyEnum.IS_DELETED, true); + log.info("Try to fetch services with properties {} and not {}", hasProps, hasNotProps); + + Either<List<GraphVertex>, TitanOperationStatus> servicesByCriteria = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll); + if (servicesByCriteria.isRight()) { + log.info("Failed to fetch services {}", servicesByCriteria.right().value()); + return false; + } + List<GraphVertex> services = servicesByCriteria.left().value(); + for (GraphVertex gv : services) { + ComponentParametersView filter = new ComponentParametersView(true); + filter.setIgnoreComponentInstances(false); + filter.setIgnoreArtifacts(false); + filter.setIgnoreGroups(false); + + Either<Service, StorageOperationStatus> toscaElement = toscaOperationFacade.getToscaElement(gv.getUniqueId()); + if (toscaElement.isRight()) { + log.info("Failed to fetch service {} {}", gv.getUniqueId(), toscaElement.right().value()); + return false; + } + Service service = toscaElement.left().value(); + List<ComponentInstance> componentInstances = service.getComponentInstances(); + boolean isProblematic = false; + if (componentInstances == null) { + log.info("No instances for service {} ", gv.getUniqueId()); + continue; + } + String serviceName = (String) gv.getMetadataProperty(GraphPropertyEnum.NAME); + + for (ComponentInstance ci : componentInstances) { + Map<String, ArtifactDefinition> deploymentArtifacts = ci.getDeploymentArtifacts(); + List<GroupInstance> groupInstances = ci.getGroupInstances(); + if (groupInstances == null || groupInstances.isEmpty()) { + log.info("No instance groups for instance {} in service {} id {} ", ci.getName(), serviceName, gv.getUniqueId()); + continue; + } + + for (GroupInstance gi : groupInstances) { + if (gi.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) { + if (isProblematicGroupInstance(gi, ci.getName(), serviceName, deploymentArtifacts)) { + isProblematic = true; + break; + } + } + } + if (isProblematic) { + serviceList.add(service); + writeModuleResultToFile(writer, service, null); + writer.flush(); + break; + } + } + titanDao.commit(); + } + log.info("output file with list of services : {}", fileName); + } catch (Exception e) { + log.info("Failed to fetch services ", e); + return false; + } finally { + titanDao.commit(); + try { + writer.flush(); + writer.close(); + } catch (Exception ex) { + /* ignore */} + } + return true; + } + + private boolean isProblematicGroup(GroupDefinition gr, String resourceName, Map<String, ArtifactDefinition> deploymentArtifacts) { + List<String> artifacts = gr.getArtifacts(); + List<String> artifactsUuid = gr.getArtifactsUuid(); + + if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) { + log.info("No groups in resource {} ", resourceName); + return false; + } + if (artifacts.size() < artifactsUuid.size()) { + log.info(" artifacts.size() < artifactsUuid.size() group {} in resource {} ", gr.getName(), resourceName); + return true; + } + if (artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty())) { + log.info(" artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() group {} in resource {} ", gr.getName(), resourceName); + return true; + } + if (artifactsUuid.contains(null)) { + log.info(" artifactsUuid.contains(null) group {} in resource {} ", gr.getName(), resourceName); + return true; + } + + for (String artifactId : artifacts) { + String artifactlabel = findArtifactLabelFromArtifactId(artifactId); + ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactlabel); + if (artifactDefinition == null) { + log.info(" artifactDefinition == null label {} group {} in resource {} ", artifactlabel, gr.getName(), resourceName); + return true; + } + ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactDefinition.getArtifactType()); + if (artifactType != ArtifactTypeEnum.HEAT_ENV) { + if (!artifactId.equals(artifactDefinition.getUniqueId())) { + log.info(" !artifactId.equals(artifactDefinition.getUniqueId() artifact {} artId {} group {} in resource {} ", artifactlabel, artifactId, gr.getName(), resourceName); + return true; + } + if (!artifactsUuid.contains(artifactDefinition.getArtifactUUID())) { + log.info(" artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} group {} in resource {} ", artifactlabel, gr.getName(), resourceName); + return true; + } + } + } + return false; + } + + private boolean isProblematicGroupInstance(GroupInstance gi, String instName, String servicename, Map<String, ArtifactDefinition> deploymentArtifacts) { + List<String> artifacts = gi.getArtifacts(); + List<String> artifactsUuid = gi.getArtifactsUuid(); + List<String> instArtifactsUuid = gi.getGroupInstanceArtifactsUuid(); + + if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) { + log.info("No instance groups for instance {} in service {} ", instName, servicename); + return false; + } + if (artifacts.size() < artifactsUuid.size()) { + log.info(" artifacts.size() < artifactsUuid.size() inst {} in service {} ", instName, servicename); + return true; + } + if (artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty())) { + log.info(" artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() inst {} in service {} ", instName, servicename); + return true; + } + if (artifactsUuid.contains(null)) { + log.info(" artifactsUuid.contains(null) inst {} in service {} ", instName, servicename); + return true; + } + + for (String artifactId : artifacts) { + String artifactlabel = findArtifactLabelFromArtifactId(artifactId); + ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactlabel); + if (artifactDefinition == null) { + log.info(" artifactDefinition == null label {} inst {} in service {} ", artifactlabel, instName, servicename); + return true; + } + ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactDefinition.getArtifactType()); + if (artifactType != ArtifactTypeEnum.HEAT_ENV) { + if (!artifactId.equals(artifactDefinition.getUniqueId())) { + log.info(" !artifactId.equals(artifactDefinition.getUniqueId() artifact {} artId {} inst {} in service {} ", artifactlabel, artifactId, instName, servicename); + return true; + } + if (!artifactsUuid.contains(artifactDefinition.getArtifactUUID())) { + log.info(" artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ", artifactlabel, instName, servicename); + return true; + } + } else { + if (!instArtifactsUuid.contains(artifactDefinition.getArtifactUUID())) { + log.info(" instArtifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ", artifactlabel, instName, servicename); + return true; + } + } + } + return false; + } + + private boolean fix(List<Resource> vfLst, List<Service> serviceList) { + boolean res = true; + + if (vfLst != null && !vfLst.isEmpty()) { + res = fixVf(vfLst); + if (res) { + for (Component component : vfLst) { + TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(component); + Map<String, GroupDataDefinition> groups = topologyTemplate.getGroups(); + res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.GROUPS, EdgeLabelEnum.GROUPS, groups); + } + } + } + + if (res == true && serviceList != null && !serviceList.isEmpty()) { + res = fixServices(serviceList); + if (res) { + for (Component component : serviceList) { + TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(component); + Map<String, MapGroupsDataDefinition> groups = topologyTemplate.getInstGroups(); + res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS, groups); + } + } + } + + return res; + } + + private <T extends ToscaDataDefinition> boolean fixDataOnGraph(String componentId, VertexTypeEnum vertexTypeEnum, EdgeLabelEnum edgeLabelEnum, Map<String, T> groups) { + boolean res = true; + Either<GraphVertex, TitanOperationStatus> getResponse = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); + if (getResponse.isRight()) { + log.debug("Couldn't fetch component unique id {}, error: {}", componentId, getResponse.right().value()); + res = false; + + } + if (res) { + GraphVertex componentVertex = getResponse.left().value(); + + GraphVertex toscaDataVertex = null; + Either<GraphVertex, TitanOperationStatus> groupVertexEither = titanDao.getChildVertex(componentVertex, edgeLabelEnum, JsonParseFlagEnum.ParseJson); + if (groupVertexEither.isRight()) { + res = false; + log.debug("failed to get child {} vertex for component unique id {}, error: {}", edgeLabelEnum, componentId, groupVertexEither.right().value()); + } + if (res) { + toscaDataVertex = groupVertexEither.left().value(); + toscaDataVertex.setJson(groups); + Either<GraphVertex, TitanOperationStatus> updatevertexEither = titanDao.updateVertex(toscaDataVertex); + if (updatevertexEither.isRight()) { + log.debug("failed to update vertex for component unique id {}, error: {}", componentId, updatevertexEither.right().value()); + titanDao.rollback(); + return false; + } + } + } + + titanDao.commit(); + + return res; + } + + private boolean fixServices(List<Service> serviceList) { + for (Service service : serviceList) { + log.debug("Migration1707ArtifactUuidFix fix service: id {}, name {} ", service.getUniqueId(), service.getName()); + List<ComponentInstance> instances = service.getComponentInstances(); + for (ComponentInstance instance : instances) { + Map<String, ArtifactDefinition> artifactsMap = instance.getDeploymentArtifacts(); + List<GroupInstance> groupsList = instance.getGroupInstances(); + if (groupsList != null && artifactsMap != null) { + for (GroupInstance group : groupsList) { + if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) { + log.debug("Migration1707ArtifactUuidFix fix group: resource id {}, group name {} ", service.getUniqueId(), group.getName()); + List<String> groupArtifacts = new ArrayList<String>(group.getArtifacts()); + + group.getArtifacts().clear(); + group.getArtifactsUuid().clear(); + group.getGroupInstanceArtifacts().clear(); + group.getGroupInstanceArtifactsUuid().clear(); + + for (String artifactId : groupArtifacts) { + String artifactlabel = findArtifactLabelFromArtifactId(artifactId); + log.debug("Migration1707ArtifactUuidFix fix group: group name {} artifactId for fix {} artifactlabel {} ", group.getName(), artifactId, artifactlabel); + if (!artifactlabel.isEmpty() && artifactsMap.containsKey(artifactlabel)) { + ArtifactDefinition artifact = artifactsMap.get(artifactlabel); + ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifact.getArtifactType()); + String correctArtifactId = artifact.getUniqueId(); + String correctArtifactUUID = artifact.getArtifactUUID(); + if (artifactType != ArtifactTypeEnum.HEAT_ENV) { + + log.debug("Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} ", group.getName(), correctArtifactId, correctArtifactUUID); + group.getArtifacts().add(correctArtifactId); + if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) { + group.getArtifactsUuid().add(correctArtifactUUID); + } + } else { + log.debug("Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} ", group.getName(), correctArtifactId, correctArtifactUUID); + group.getGroupInstanceArtifacts().add(correctArtifactId); + if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) { + group.getGroupInstanceArtifactsUuid().add(correctArtifactUUID); + } + } + } + } + } + } + } + } + + } + return true; + + } + + private boolean fixVf(List<Resource> vfLst) { + for (Resource resource : vfLst) { + log.debug("Migration1707ArtifactUuidFix fix resource: id {}, name {} ", resource.getUniqueId(), resource.getName()); + Map<String, ArtifactDefinition> artifactsMap = resource.getDeploymentArtifacts(); + List<GroupDefinition> groupsList = resource.getGroups(); + if (groupsList != null && artifactsMap != null) { + for (GroupDefinition group : groupsList) { + if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE) && group.getArtifacts() != null) { + log.debug("Migration1707ArtifactUuidFix fix group: resource id {}, group name {} ", resource.getUniqueId(), group.getName()); + List<String> groupArtifacts = new ArrayList<String>(group.getArtifacts()); + group.getArtifacts().clear(); + group.getArtifactsUuid().clear(); + for (String artifactId : groupArtifacts) { + String artifactlabel = findArtifactLabelFromArtifactId(artifactId); + log.debug("Migration1707ArtifactUuidFix fix group: group name {} artifactId for fix {} artifactlabel {} ", group.getName(), artifactId, artifactlabel); + if (!artifactlabel.isEmpty() && artifactsMap.containsKey(artifactlabel)) { + ArtifactDefinition artifact = artifactsMap.get(artifactlabel); + String correctArtifactId = artifact.getUniqueId(); + String correctArtifactUUID = artifact.getArtifactUUID(); + log.debug("Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} ", group.getName(), correctArtifactId, correctArtifactUUID); + group.getArtifacts().add(correctArtifactId); + if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) { + group.getArtifactsUuid().add(correctArtifactUUID); + } + + } + } + } + } + } + + } + + return true; + } + + private String findArtifactLabelFromArtifactId(String artifactId) { + String artifactLabel = ""; + + int index = artifactId.lastIndexOf("."); + if (index > 0 && index + 1 < artifactId.length()) + artifactLabel = artifactId.substring(index + 1); + return artifactLabel; + } + + private void writeModuleResultToFile(Writer writer, org.openecomp.sdc.be.model.Component component, Service service) { + try { + // "service name, service id, state, version + StringBuffer sb = new StringBuffer(component.getName()); + sb.append(",").append(component.getUniqueId()).append(",").append(component.getLifecycleState()).append(",").append(component.getVersion()); + if (service != null) { + sb.append(",").append(service.getName()); + } + sb.append("\n"); + writer.write(sb.toString()); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707Config.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707Config.java index eeb29d67c2..36919d7520 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707Config.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707Config.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707; @@ -47,60 +67,60 @@ public class Migration1707Config { return new Migration1707(migrations); } - //@Bean(name = "renameGraphPropertyKeysMigration") - //@Order(1) - //public Migration1707Task renameGraphPropertyKeysMigration() { - // return new RenameGraphPropertyKeys(); - // } + @Bean(name = "renameGraphPropertyKeysMigration") + @Order(1) + public Migration1707Task renameGraphPropertyKeysMigration() { + return new RenameGraphPropertyKeys(); + } - //@Bean(name = "toscaNamesUpdate") - //@Order(2) - //public Migration1707Task toscaNamesUpdate() { - // return new ToscaNamesUpdate(); - //} + @Bean(name = "toscaNamesUpdate") + @Order(2) + public Migration1707Task toscaNamesUpdate() { + return new ToscaNamesUpdate(); + } @Bean(name = "users-migration") - @Order(1) + @Order(3) public Migration1707Task usersMigration() { return new UsersMigration(); } @Bean(name = "resource-category-migration") - @Order(2) + @Order(4) public Migration1707Task resourceCategoriesMigration() { return new ResourcesCategoriesMigration(); } @Bean(name = "service-category-migration") - @Order(3) + @Order(5) public Migration1707Task serviceCategoriesMigration() { return new ServiceCategoriesMigration(); } @Bean(name = "normatives-migration") - @Order(4) + @Order(6) public Migration1707Task normativesMigration() { return new NormativesMigration(); } @Bean(name = "vf-migration") - @Order(5) + @Order(7) public Migration1707Task vfMigration() { return new VFResourcesMigration(); } @Bean(name = "service-migration") - @Order(6) + @Order(8) public Migration1707Task serviceMigration() { return new ServicesMigration(); } @Bean(name = "consumers-migration") - @Order(7) + @Order(9) public Migration1707Task consumersMigration() { return new ConsumersMigration(); } @Bean(name = "tosca-template-regeneration") - @Order(8) + @Order(10) public Migration1707Task ToscaTemplateRegeneration() { return new ToscaTemplateRegeneration(); } @@ -245,5 +265,15 @@ public class Migration1707Config { public VfModulesPropertiesAdding vfModulesPropertiesAdding() { return new VfModulesPropertiesAdding(); } + + @Bean(name = "node-template-missing-data-resolver") + public NodeTemplateMissingDataResolver nodeTemplateMissingDataResolver() { + return new NodeTemplateMissingDataResolver<>(); + } + + @Bean(name = "migration1707MissingInfoFix") + public Migration1707MissingInfoFix migration1707MissingInfoFix() { + return new Migration1707MissingInfoFix(); + } } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707MissingInfoFix.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707MissingInfoFix.java new file mode 100644 index 0000000000..ff41f12b8e --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707MissingInfoFix.java @@ -0,0 +1,220 @@ +package org.openecomp.sdc.asdctool.impl.migration.v1707; + +import fj.data.Either; +import org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel.NodeTemplateMissingDataResolver; +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.*; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; +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.model.*; +import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement; +import org.openecomp.sdc.be.model.jsontitan.enums.JsonConstantKeysEnum; +import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade; +import org.openecomp.sdc.be.model.operations.api.IServiceOperation; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +@org.springframework.stereotype.Component("migration1707MissingInfoFix") +public class Migration1707MissingInfoFix { + + private static final Logger LOGGER = LoggerFactory.getLogger(Migration1707MissingInfoFix.class); + + @Resource(name = "service-operation") + private IServiceOperation serviceOperation; + + @Resource(name = "node-template-missing-data-resolver") + private NodeTemplateMissingDataResolver nodeTemplateMissingDataResolver; + + @Resource(name = "tosca-operation-facade") + private ToscaOperationFacade toscaOperations; + + @Resource(name = "titan-dao") + private TitanDao titanDao; + + + public boolean migrate(){ + boolean res = updateVFs(); + if(res) + res = updateServices(); + return res; + } + + private ComponentParametersView getFilter() { + ComponentParametersView filter = new ComponentParametersView(true); + filter.setIgnoreComponentInstances(false); + filter.setIgnoreArtifacts(false); + filter.setIgnoreGroups(false); + filter.setIgnoreComponentInstancesInputs(false); + return filter; + } + + // if new service has VF instances with no groups - try to fetch them from old graph + private boolean oldServiceModelRequired(Component newService) { + Predicate<ComponentInstance> vfInstanceWithNoGroups = p -> OriginTypeEnum.VF == p.getOriginType() && (null == p.getGroupInstances() || p.getGroupInstances().isEmpty()); + return null != newService.getComponentInstances() && newService.getComponentInstances().stream() + .anyMatch(vfInstanceWithNoGroups); + } + + + + private List<GraphVertex> fetchVertices(Map<GraphPropertyEnum, Object> hasProps){ + Either<List<GraphVertex>, TitanOperationStatus> componentsByCriteria = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, JsonParseFlagEnum.ParseAll); + if (componentsByCriteria.isRight()) { + LOGGER.debug("couldn't fetch assets from sdctitan"); + return null; + } + return componentsByCriteria.left().value(); + } + + private boolean updateVFs() { + + boolean res = true; + Map<GraphPropertyEnum, Object> hasProps = new HashMap<>(); + hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); + hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF.name()); + + List<GraphVertex> resources = fetchVertices(hasProps); + if(null == resources) + return false; + ComponentParametersView filter = getFilter(); + Map<String, ToscaElement> origCompMap = new HashMap<>(); + + for (GraphVertex gv : resources) { + boolean fixed = true; + Either<Component, StorageOperationStatus> toscaElement = toscaOperations.getToscaElement(gv.getUniqueId(), filter); + if (toscaElement.isRight()) { + LOGGER.debug("Failed to fetch resource {} {}", gv.getUniqueId(), toscaElement.right().value()); + return false; + } + Component resource = toscaElement.left().value(); + Map<String, Boolean> updateMap = new HashMap<>(); + nodeTemplateMissingDataResolver.updateVFComposition(resource, origCompMap, updateMap); + if(updateMap.get(JsonConstantKeysEnum.COMPOSITION.name())){ + LOGGER.info("applying instance tosca name fix on VF {}", gv.getUniqueId()); + fixed = toscaOperations.updateComponentInstanceMetadataOfTopologyTemplate(resource).isLeft(); + } + if(updateMap.get(EdgeLabelEnum.GROUPS.name())) { + List<GroupDataDefinition> groups = new ArrayList<>(resource.getGroups()); + LOGGER.info("applying groups vertex fix on VF {}", gv.getUniqueId()); + fixed = fixed && toscaOperations.updateGroupsOnComponent(resource, ComponentTypeEnum.RESOURCE, groups).isLeft(); + } + + res = res && fixed; + titanDao.commit(); + } + return res; + } + + private Map<String, MapPropertiesDataDefinition> buildInstancesInputsMap(Component component){ + Map<String, MapPropertiesDataDefinition> instanceInputsMap = new HashMap<>(); + for (Map.Entry<String, List<ComponentInstanceInput>> entry : component.getComponentInstancesInputs().entrySet()) { + MapPropertiesDataDefinition inputsMap = new MapPropertiesDataDefinition(); + inputsMap.setMapToscaDataDefinition(entry.getValue().stream().map(e -> new PropertyDataDefinition(e)).collect(Collectors.toMap(e -> e.getName(), e -> e))); + instanceInputsMap.put(entry.getKey(), inputsMap); + } + return instanceInputsMap; + } + + + + private Map<String, MapGroupsDataDefinition> buildGroupInstanceMap(Component component) { + Map<String, MapGroupsDataDefinition> instGroupsMap = new HashMap<>(); + for (ComponentInstance instance : component.getComponentInstances()) { + if (instance.getGroupInstances() != null) { + MapGroupsDataDefinition groupsMap = new MapGroupsDataDefinition(); + groupsMap.setMapToscaDataDefinition(instance.getGroupInstances().stream().map(e -> new GroupInstanceDataDefinition(e)).collect(Collectors.toMap(e -> e.getName(), e -> e))); + instGroupsMap.put(instance.getUniqueId(), groupsMap); + } + } + return instGroupsMap; + } + + private <T extends ToscaDataDefinition> boolean updateDataVertex(GraphVertex componentVertex, VertexTypeEnum vertexType, EdgeLabelEnum edgeLabel, Map<String, T> dataMap){ + Either<GraphVertex, TitanOperationStatus> dataVertexEither = titanDao.getChildVertex(componentVertex, edgeLabel, JsonParseFlagEnum.ParseJson); + if (dataVertexEither.isRight()) { + if(TitanOperationStatus.NOT_FOUND != dataVertexEither.right().value()) + return false; + return (nodeTemplateMissingDataResolver.topologyTemplateOperation.assosiateElementToData(componentVertex, vertexType, edgeLabel, dataMap)).isLeft(); + } + GraphVertex dataVertex = dataVertexEither.left().value(); + dataVertex.setJson(dataMap); + return (titanDao.updateVertex(dataVertex)).isLeft(); + + } + + + private boolean updateServices(){ + + boolean res = true; + Map<GraphPropertyEnum, Object> hasProps = new HashMap<>(); + hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name()); + + List<GraphVertex> componentsByCriteria = fetchVertices(hasProps); + if(null == componentsByCriteria) + return false; + + ComponentParametersView filter = getFilter(); + Map<String, ToscaElement> origCompMap = new HashMap<>(); + + Predicate<ComponentInstance> containsGroupInstances = p -> null != p.getGroupInstances() && !p.getGroupInstances().isEmpty(); + + for (GraphVertex gv : componentsByCriteria) { + + boolean fixed = true; + Either<org.openecomp.sdc.be.model.Service, StorageOperationStatus> toscaElement = toscaOperations.getToscaElement(gv.getUniqueId(), filter); + if (toscaElement.isRight()) { + LOGGER.debug("Failed to fetch service {} {}", gv.getUniqueId(), toscaElement.right().value()); + return false; + } + Component service = toscaElement.left().value(); + Component oldService = null; + + if(oldServiceModelRequired(service)){ + Either<Service, StorageOperationStatus> oldServiceEither = serviceOperation.getService(gv.getUniqueId(), filter, false); + if (oldServiceEither.isRight()){ + LOGGER.debug("couldn't fetch service {} from old titan", gv.getUniqueId()); + }else { + oldService = oldServiceEither.left().value(); + oldService = oldService.getComponentInstances().stream().anyMatch(containsGroupInstances) ? oldService : null; + } + } + + Map<String, Boolean> updateMap = new HashMap<>(); + nodeTemplateMissingDataResolver.updateServiceComposition(service, origCompMap, oldService, updateMap); + if(updateMap.get(JsonConstantKeysEnum.COMPOSITION.name())) { + LOGGER.info("applying instance tosca name fix on service {}", gv.getUniqueId()); + fixed = (toscaOperations.updateComponentInstanceMetadataOfTopologyTemplate(service)).isLeft(); + } + if(updateMap.get(EdgeLabelEnum.INST_GROUPS.name())) { + Map<String, MapGroupsDataDefinition> groupsMap = buildGroupInstanceMap(service); + LOGGER.info("applying groups instances vertex fix on service {}", gv.getUniqueId()); + fixed = fixed && updateDataVertex(gv, VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS, groupsMap); + } + if(updateMap.get(EdgeLabelEnum.INST_INPUTS.name())) { + Map<String, MapPropertiesDataDefinition> instInputs = buildInstancesInputsMap(service); + LOGGER.info("applying instances inputs vertex fix on service {}", gv.getUniqueId()); + fixed = fixed && updateDataVertex(gv, VertexTypeEnum.INST_INPUTS, EdgeLabelEnum.INST_INPUTS, instInputs); + } + res = res && fixed; + titanDao.commit(); + } + return res; + } +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707RelationsFix.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707RelationsFix.java index 47bce78893..204f0a5fc8 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707RelationsFix.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707RelationsFix.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707; import java.util.EnumMap; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707VnfFix.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707VnfFix.java index d3bd494a85..5d7a887a7f 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707VnfFix.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707VnfFix.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707; import java.util.EnumMap; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/MigrationTitanStrategy.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/MigrationTitanStrategy.java index 9a0cc89dd6..0274ca425e 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/MigrationTitanStrategy.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/MigrationTitanStrategy.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707; import org.openecomp.sdc.be.config.ConfigurationManager; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/MigrationUtils.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/MigrationUtils.java index 81a00b0444..04f810af5e 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/MigrationUtils.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/MigrationUtils.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707; import org.apache.commons.lang.enums.Enum; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/RenameGraphPropertyKeys.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/RenameGraphPropertyKeys.java index a1c1b6b4cc..9f571167db 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/RenameGraphPropertyKeys.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/RenameGraphPropertyKeys.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707; import org.openecomp.sdc.asdctool.impl.migration.*; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/ToscaNamesUpdate.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/ToscaNamesUpdate.java index 5213fae619..665fe15315 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/ToscaNamesUpdate.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/ToscaNamesUpdate.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707; import java.util.ArrayList; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/ToscaTemplateRegeneration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/ToscaTemplateRegeneration.java index 09e52619f0..7d009fe158 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/ToscaTemplateRegeneration.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/ToscaTemplateRegeneration.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707; import java.util.EnumMap; @@ -72,7 +92,8 @@ public class ToscaTemplateRegeneration implements Migration1707Task { private boolean regenerateToscaTemplateArtifacts(List<GraphVertex> components) { boolean result = true; for(GraphVertex componentV : components){ - Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> getComponentsRes = toscaOperationFacade.getToscaElement(componentV); + String componentId = componentV.getUniqueId(); + Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> getComponentsRes = toscaOperationFacade.getToscaElement(componentId); if (getComponentsRes.isRight()) { result = false; break; @@ -135,7 +156,7 @@ public class ToscaTemplateRegeneration implements Migration1707Task { Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); List<GraphVertex> components = null; - Either<List<GraphVertex>, TitanOperationStatus> getVerticiesRes = toscaOperationFacade.getTitanDao().getByCriteria(null, propertiesToMatch,JsonParseFlagEnum.ParseMetadata); + Either<List<GraphVertex>, TitanOperationStatus> getVerticiesRes = toscaOperationFacade.getTitanDao().getByCriteria(null, propertiesToMatch,JsonParseFlagEnum.NoParse); if (getVerticiesRes.isRight() && getVerticiesRes.right().value() != TitanOperationStatus.NOT_FOUND) { LOGGER.debug("Failed to fetch all certified components. Status is {}", getVerticiesRes.right().value()); diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/VfModulesPropertiesAdding.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/VfModulesPropertiesAdding.java index ca1ed2b2a8..89ac6f7cb6 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/VfModulesPropertiesAdding.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/VfModulesPropertiesAdding.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707; import java.util.ArrayList; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/CategoriesUtils.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/CategoriesUtils.java index 2ba7e013f0..44d95395d0 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/CategoriesUtils.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/CategoriesUtils.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel; import org.openecomp.sdc.be.model.category.CategoryDefinition; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ComponentMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ComponentMigration.java index 63eb8879c9..7603a5704a 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ComponentMigration.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ComponentMigration.java @@ -1,18 +1,50 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel; import fj.data.Either; import org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel.relations.RequirementsCapabilitiesMigrationService; import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; +import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum; +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.GroupDefinition; +import org.openecomp.sdc.be.model.GroupInstance; +import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement; +import org.openecomp.sdc.be.model.jsontitan.operations.ToscaElementLifecycleOperation; 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.migration.MigrationMalformedDataLogger; +import org.openecomp.sdc.common.api.ArtifactTypeEnum; +import org.openecomp.sdc.common.api.Constants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; import static org.openecomp.sdc.asdctool.impl.migration.v1707.MigrationUtils.handleError; @@ -29,14 +61,16 @@ public abstract class ComponentMigration <T extends Component> extends JsonModel @Resource(name = "invariant-uuid-resolver") private InvariantUUIDResolver <T> invariantUUIDResolver; - + + @Resource(name = "node-template-missing-data-resolver") + private NodeTemplateMissingDataResolver nodeTemplateMissingDataResolver; + @Override boolean save(T element) { LOGGER.debug(String.format("creating component %s in new graph", element.getName())); return toscaOperations.createToscaComponent(element) .either(savedNode -> true, err -> handleError(String.format("failed to create component %s with id %s. reason: %s", element.getName(), element.getUniqueId(), err.name()))); - } @Override @@ -70,6 +104,29 @@ public abstract class ComponentMigration <T extends Component> extends JsonModel } return cmptsWithoutInvariant; } + + + protected void setMissingTemplateInfo(List<T> components) { + Map<String, ToscaElement> origCompMap = new HashMap<>(); + for (T component : components) { + List<ComponentInstance> instances = component.getComponentInstances(); + if(null != instances) { + for (ComponentInstance instance : instances) { + nodeTemplateMissingDataResolver.resolveNodeTemplateInfo(instance, origCompMap, component); + nodeTemplateMissingDataResolver.fixVFGroupInstances(component, instance); + } + } + nodeTemplateMissingDataResolver.fixVFGroups(component); + } + } + + + + + + + + } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ConsumersMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ConsumersMigration.java index 83078baf6e..c455f870aa 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ConsumersMigration.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ConsumersMigration.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel; import fj.data.Either; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/InvariantUUIDResolver.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/InvariantUUIDResolver.java index bf28507877..e26a01e1b4 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/InvariantUUIDResolver.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/InvariantUUIDResolver.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel; import org.openecomp.sdc.be.model.Component; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/JsonModelMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/JsonModelMigration.java index 4e62b2f497..76c66de1c9 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/JsonModelMigration.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/JsonModelMigration.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel; import fj.data.Either; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/MigrationByIdDerivedNodeTypeResolver.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/MigrationByIdDerivedNodeTypeResolver.java index 0522ed965d..066ab9a01a 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/MigrationByIdDerivedNodeTypeResolver.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/MigrationByIdDerivedNodeTypeResolver.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel; import fj.data.Either; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/NodeTemplateMissingDataResolver.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/NodeTemplateMissingDataResolver.java new file mode 100644 index 0000000000..c83421066c --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/NodeTemplateMissingDataResolver.java @@ -0,0 +1,353 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ +package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel; +import fj.data.Either; +import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum; +import fj.data.Either; +import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; +import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum; +import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; +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.enums.JsonConstantKeysEnum; +import org.openecomp.sdc.be.model.jsontitan.operations.TopologyTemplateOperation; +import org.openecomp.sdc.be.model.jsontitan.operations.ToscaElementLifecycleOperation; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.openecomp.sdc.common.api.ArtifactTypeEnum; +import org.openecomp.sdc.common.api.Constants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.*; +import java.util.stream.Collectors; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; + + +public class NodeTemplateMissingDataResolver <T extends Component> { + + private static Logger LOGGER = LoggerFactory.getLogger(NodeTemplateMissingDataResolver.class); + + @Resource(name = "tosca-element-lifecycle-operation") + private ToscaElementLifecycleOperation lifecycleOperation; + + @Resource(name = "topology-template-operation") + public TopologyTemplateOperation topologyTemplateOperation; + + public void resolveNodeTemplateInfo(ComponentInstanceDataDefinition vfInst, Map<String, ToscaElement> origCompMap, T component) { + lifecycleOperation.resolveToscaComponentName(vfInst, origCompMap); + if(OriginTypeEnum.VF == vfInst.getOriginType()) { + collectVFInstanceInputs(component, origCompMap, vfInst); + } + } + + + + + public void updateServiceComposition(Component component, Map<String, ToscaElement> origCompMap, Component oldModelService, Map<String, Boolean> updateMap){ + + boolean composition = false; + boolean instInputs = false; + boolean instGroups = false; + List<ComponentInstance> instances = component.getComponentInstances(); + if(null != instances) { + for (ComponentInstance instance : instances) { + composition = composition || lifecycleOperation.resolveToscaComponentName(instance, origCompMap); + if(OriginTypeEnum.VF == instance.getOriginType()) { + instInputs = instInputs || collectVFInstanceInputs(component, origCompMap, instance); + instGroups = instGroups || resolveInstGroupsFromOldTitanGraphAndApplyFix(component, instance, oldModelService); + } + } + } + updateMap.put(JsonConstantKeysEnum.COMPOSITION.name(), composition); + updateMap.put(EdgeLabelEnum.INST_INPUTS.name(), instInputs); + updateMap.put(EdgeLabelEnum.INST_GROUPS.name(), instGroups); + } + + + public void updateVFComposition(Component component, Map<String, ToscaElement> origCompMap, Map<String, Boolean> updateMap) { + + boolean composition = false; + boolean groups = fixVFGroups(component); + List<ComponentInstance> instances = component.getComponentInstances(); + if(null != instances) { + for (ComponentInstance instance : instances) { + composition = composition || lifecycleOperation.resolveToscaComponentName(instance, origCompMap); + } + } + updateMap.put(JsonConstantKeysEnum.COMPOSITION.name(), composition); + updateMap.put(EdgeLabelEnum.GROUPS.name(), groups); + } + + + + private boolean resolveInstGroupsFromOldTitanGraphAndApplyFix(Component component, ComponentInstance instance, Component oldService){ + + boolean res = false; + //info already exists, apply fix if needed + if(null != instance.getGroupInstances() && !instance.getGroupInstances().isEmpty()) { + res = fixVFGroupInstances(component, instance); + //get group instances from old model + }else if(null != oldService){ + ComponentInstance origInstance = oldService.getComponentInstances().stream() + .filter(p -> instance.getUniqueId().equals(p.getUniqueId())) + .findAny().orElse(null); + if(null != origInstance && null != origInstance.getGroupInstances()) { + fixVFGroupInstances(oldService, origInstance); + instance.setGroupInstances(origInstance.getGroupInstances()); + res = true; + } + } + return res; + } + + private boolean collectVFInstanceInputs(Component component, Map<String, ToscaElement> origCompMap, ComponentInstanceDataDefinition vfInst) { + boolean res = false; + String ciUid = vfInst.getUniqueId(); + String origCompUid = vfInst.getComponentUid(); + if(null == component.getComponentInstancesInputs()) + component.setComponentInstancesInputs(new HashMap<>()); + Map<String, List<ComponentInstanceInput>> componentInstInputs = component.getComponentInstancesInputs(); + Either<ToscaElement, StorageOperationStatus> origComp = fetchToscaElement(origCompMap, vfInst, origCompUid); + if(origComp.isRight()) + return false; + Map<String, PropertyDataDefinition> origVFInputs = ((TopologyTemplate)origComp.left().value()).getInputs(); + if (origVFInputs != null && !origVFInputs.isEmpty()) { + res = true; + Map<String, ComponentInstanceInput> collectedVFInputs = origVFInputs.values().stream() + .collect(Collectors.toMap(PropertyDataDefinition::getName, ComponentInstanceInput::new)); + + List<ComponentInstanceInput> instInputList = componentInstInputs.get(ciUid); + Map<String, ComponentInstanceInput> existingInstInputs = ToscaDataDefinition.listToMapByName(instInputList); + collectedVFInputs.putAll(existingInstInputs); + List<ComponentInstanceInput> mergedList = new ArrayList<>(collectedVFInputs.values()); + componentInstInputs.put(ciUid, mergedList); + } + return res; + } + + private Either<ToscaElement, StorageOperationStatus> fetchToscaElement(Map<String, ToscaElement> origCompMap, ComponentInstanceDataDefinition vfInst, String origCompUid) { + Either<ToscaElement, StorageOperationStatus> origCompEither; + if (!origCompMap.containsKey(origCompUid)) { + origCompEither = topologyTemplateOperation.getToscaElement(origCompUid); + if (origCompEither.isRight()) { + LOGGER.error("failed to fetch Tosca element {} with id {}", vfInst.getComponentName(), origCompUid); + return origCompEither; + } + origCompMap.put(origCompUid, origCompEither.left().value()); + } + return Either.left(origCompMap.get(origCompUid)); + } + + protected boolean isProblematicGroup(GroupDefinition gr, String resourceName, Map<String, ArtifactDefinition> deploymentArtifacts) { + List<String> artifacts = gr.getArtifacts(); + List<String> artifactsUuid = gr.getArtifactsUuid(); + + if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) { + LOGGER.debug("No groups in resource {} ", resourceName); + return false; + } + if (artifacts.size() < artifactsUuid.size()) { + LOGGER.debug(" artifacts.size() < artifactsUuid.size() group {} in resource {} ", gr.getName(), resourceName); + return true; + } + if (artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty())) { + LOGGER.debug(" artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() group {} in resource {} ", gr.getName(), resourceName); + return true; + } + if (artifactsUuid.contains(null)) { + LOGGER.debug(" artifactsUuid.contains(null) group {} in resource {} ", gr.getName(), resourceName); + return true; + } + + for (String artifactId : artifacts) { + String artifactlabel = findArtifactLabelFromArtifactId(artifactId); + ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactlabel); + if (artifactDefinition == null) { + LOGGER.debug(" artifactDefinition == null label {} group {} in resource {} ", artifactlabel, gr.getName(), resourceName); + return true; + } + ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactDefinition.getArtifactType()); + if (artifactType != ArtifactTypeEnum.HEAT_ENV) { + if (!artifactId.equals(artifactDefinition.getUniqueId())) { + LOGGER.debug(" !artifactId.equals(artifactDefinition.getUniqueId() artifact {} artId {} group {} in resource {} ", artifactlabel, artifactId, gr.getName(), resourceName); + return true; + } + if (!artifactsUuid.contains(artifactDefinition.getArtifactUUID())) { + LOGGER.debug(" artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} group {} in resource {} ", artifactlabel, gr.getName(), resourceName); + return true; + } + } + } + return false; + } + + protected boolean isProblematicGroupInstance(GroupInstance gi, String instName, String servicename, Map<String, ArtifactDefinition> deploymentArtifacts) { + List<String> artifacts = gi.getArtifacts(); + List<String> artifactsUuid = gi.getArtifactsUuid(); + List<String> instArtifactsUuid = gi.getGroupInstanceArtifactsUuid(); + + if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) { + LOGGER.debug("No instance groups for instance {} in service {} ", instName, servicename); + return false; + } + if (artifacts.size() < artifactsUuid.size()) { + LOGGER.debug(" artifacts.size() < artifactsUuid.size() inst {} in service {} ", instName, servicename); + return true; + } + if (artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty())) { + LOGGER.debug(" artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() inst {} in service {} ", instName, servicename); + return true; + } + if (artifactsUuid.contains(null)) { + LOGGER.debug(" artifactsUuid.contains(null) inst {} in service {} ", instName, servicename); + return true; + } + + for (String artifactId : artifacts) { + String artifactlabel = findArtifactLabelFromArtifactId(artifactId); + ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactlabel); + if (artifactDefinition == null) { + LOGGER.debug(" artifactDefinition == null label {} inst {} in service {} ", artifactlabel, instName, servicename); + return true; + } + ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactDefinition.getArtifactType()); + if (artifactType != ArtifactTypeEnum.HEAT_ENV) { + if (!artifactId.equals(artifactDefinition.getUniqueId())) { + LOGGER.debug(" !artifactId.equals(artifactDefinition.getUniqueId() artifact {} artId {} inst {} in service {} ", artifactlabel, artifactId, instName, servicename); + return true; + } + if (!artifactsUuid.contains(artifactDefinition.getArtifactUUID())) { + LOGGER.debug(" artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ", artifactlabel, instName, servicename); + return true; + } + } else { + if (!instArtifactsUuid.contains(artifactDefinition.getArtifactUUID())) { + LOGGER.debug(" instArtifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ", artifactlabel, instName, servicename); + return true; + } + } + } + return false; + } + + private String findArtifactLabelFromArtifactId(String artifactId) { + String artifactLabel = ""; + + int index = artifactId.lastIndexOf("."); + if (index > 0 && index + 1 < artifactId.length()) + artifactLabel = artifactId.substring(index + 1); + return artifactLabel; + } + + public boolean fixVFGroups(Component component){ + boolean res = false; + + Map<String, ArtifactDefinition> deploymentArtifacts = component.getDeploymentArtifacts(); + List<GroupDefinition> groups = component.getGroups(); + if (groups == null || groups.isEmpty()) { + LOGGER.debug("No groups in component {} id {} ", component.getName(), component.getUniqueId()); + return res; + } + + for (GroupDefinition group : groups) { + if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE) && deploymentArtifacts != null) { + if (isProblematicGroup(group, component.getName(), deploymentArtifacts)) { + res = true; + List<String> groupArtifacts = null == group.getArtifacts()? new ArrayList<>() : new ArrayList<>(group.getArtifacts()); + group.setArtifacts(new ArrayList<>()); + group.setArtifactsUuid(new ArrayList<>()); + for (String artifactId : groupArtifacts) { + String artifactlabel = findArtifactLabelFromArtifactId(artifactId); + LOGGER.debug("fix group: group name {} artifactId for fix {} artifactlabel {} ", group.getName(), artifactId, artifactlabel); + if (!artifactlabel.isEmpty() && deploymentArtifacts.containsKey(artifactlabel)) { + ArtifactDefinition artifact = deploymentArtifacts.get(artifactlabel); + String correctArtifactId = artifact.getUniqueId(); + String correctArtifactUUID = artifact.getArtifactUUID(); + LOGGER.debug(" fix group: group name {} correct artifactId {} artifactUUID {} ", group.getName(), correctArtifactId, correctArtifactUUID); + group.getArtifacts().add(correctArtifactId); + if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) { + group.getArtifactsUuid().add(correctArtifactUUID); + } + } + } + } + } + } + return res; + } + + public boolean fixVFGroupInstances(Component component, ComponentInstance instance){ + boolean res = false; + + Map<String, ArtifactDefinition> deploymentArtifacts = instance.getDeploymentArtifacts(); + List<GroupInstance> groupInstances = instance.getGroupInstances(); + if (groupInstances == null || groupInstances.isEmpty()) { + LOGGER.debug("No instance groups for instance {} in service {} id {} ", instance.getName(), component.getName(), component.getUniqueId()); + return res; + } + for (GroupInstance group : groupInstances) { + if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) { + if (isProblematicGroupInstance(group, instance.getName(), component.getName(), deploymentArtifacts)) { + res = true; + LOGGER.debug("Migration1707ArtifactUuidFix fix group: resource id {}, group name {} ", component.getUniqueId(), group.getName()); + List<String> groupArtifacts = null == group.getArtifacts()? new ArrayList<>() : new ArrayList<>(group.getArtifacts()); + group.setArtifacts(new ArrayList<>()); + group.setArtifactsUuid(new ArrayList<>()); + group.setGroupInstanceArtifacts(new ArrayList<>()); + group.setGroupInstanceArtifactsUuid(new ArrayList<>()); + + for (String artifactId : groupArtifacts) { + String artifactlabel = findArtifactLabelFromArtifactId(artifactId); + LOGGER.debug("Migration1707ArtifactUuidFix fix group: group name {} artifactId for fix {} artifactlabel {} ", group.getName(), artifactId, artifactlabel); + if (!artifactlabel.isEmpty() && deploymentArtifacts.containsKey(artifactlabel)) { + ArtifactDefinition artifact = deploymentArtifacts.get(artifactlabel); + ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifact.getArtifactType()); + String correctArtifactId = artifact.getUniqueId(); + String correctArtifactUUID = artifact.getArtifactUUID(); + if (artifactType != ArtifactTypeEnum.HEAT_ENV) { + + LOGGER.debug("Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} ", group.getName(), correctArtifactId, correctArtifactUUID); + group.getArtifacts().add(correctArtifactId); + if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) { + group.getArtifactsUuid().add(correctArtifactUUID); + } + } else { + LOGGER.debug("Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} ", group.getName(), correctArtifactId, correctArtifactUUID); + group.getGroupInstanceArtifacts().add(correctArtifactId); + if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) { + group.getGroupInstanceArtifactsUuid().add(correctArtifactUUID); + } + } + } + } + } + } + } + return res; + } +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/NormativesMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/NormativesMigration.java index 82d59e57b7..1717b1fa83 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/NormativesMigration.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/NormativesMigration.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel; import fj.data.Either; @@ -16,7 +36,7 @@ public class NormativesMigration extends ComponentMigration<Resource> { private static Logger LOGGER = LoggerFactory.getLogger(NormativesMigration.class); private static final String JCP_VERSION_PROPERTY = "jcp-version"; - private static final Set<String> e2eMalformedVfcs = Sets.newHashSet("71879ee1-ad63-46d0-9943-d33083a6fdbb", "e54e7c4d-6020-4c53-838b-42d34c0da5c9"); + private static final Set<String> e2eMalformedVfcs = Sets.newHashSet("59da26b4-edd0-4412-a2e6-d6711f376340"); @javax.annotation.Resource(name = "normatives-resolver") private NormativesResolver normativesResolver; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/NormativesResolver.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/NormativesResolver.java index 205faf5355..cc9e6ec972 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/NormativesResolver.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/NormativesResolver.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel; import fj.data.Either; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ResourceVersionMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ResourceVersionMigration.java index ee222a721b..aeb85a7d35 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ResourceVersionMigration.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ResourceVersionMigration.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel; import fj.data.Either; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ResourcesCategoriesMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ResourcesCategoriesMigration.java index 5285c4c406..f605d8c3fa 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ResourcesCategoriesMigration.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ResourcesCategoriesMigration.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel; import fj.data.Either; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ServiceCategoriesMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ServiceCategoriesMigration.java index f8f79ad9ba..857bcd9b43 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ServiceCategoriesMigration.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ServiceCategoriesMigration.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel; import fj.data.Either; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ServiceVersionMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ServiceVersionMigration.java index 4a96c2743d..32ef06a220 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ServiceVersionMigration.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ServiceVersionMigration.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel; import fj.data.Either; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ServicesMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ServicesMigration.java index 254a75b317..040d220912 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ServicesMigration.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/ServicesMigration.java @@ -1,6 +1,32 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel; -import fj.data.Either; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import javax.annotation.Resource; + import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import org.openecomp.sdc.be.model.ComponentInstanceProperty; import org.openecomp.sdc.be.model.Service; @@ -9,11 +35,7 @@ import org.openecomp.sdc.be.model.operations.migration.MigrationMalformedDataLog import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.annotation.Resource; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; +import fj.data.Either; public class ServicesMigration extends ComponentMigration<Service> { @@ -57,6 +79,12 @@ public class ServicesMigration extends ComponentMigration<Service> { LOGGER.info("migrating services versions"); return versionMigration.buildComponentsVersionChain(elements); } + + @Override + void doPreMigrationOperation(List<Service> elements) { + super.doPreMigrationOperation(elements); + setMissingTemplateInfo(elements); + } private void filterOutDuplicatePropsAndAttrs(Service element) { if (element.getComponentInstancesProperties() != null) { @@ -148,5 +176,5 @@ public class ServicesMigration extends ComponentMigration<Service> { // .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); // } - + } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/UserStatesMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/UserStatesMigration.java index 67d9235cee..3f62273931 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/UserStatesMigration.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/UserStatesMigration.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel; import com.thinkaurelius.titan.core.TitanVertex; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/UsersMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/UsersMigration.java index a578f066bf..6e0a8a9247 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/UsersMigration.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/UsersMigration.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel; import fj.data.Either; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/VFResourcesMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/VFResourcesMigration.java index 34c40f0aa9..82d22addf3 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/VFResourcesMigration.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/VFResourcesMigration.java @@ -1,14 +1,34 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel; -import fj.data.Either; +import java.util.List; + import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.operations.api.IResourceOperation; -import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.List; +import fj.data.Either; public class VFResourcesMigration extends ComponentMigration<Resource> { @@ -49,5 +69,11 @@ public class VFResourcesMigration extends ComponentMigration<Resource> { return versionMigration.buildComponentsVersionChain(elements); } + @Override + void doPreMigrationOperation(List<Resource> elements) { + super.doPreMigrationOperation(elements); + setMissingTemplateInfo(elements); + } + } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/VersionMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/VersionMigration.java index 145ae28c81..514760fba2 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/VersionMigration.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/VersionMigration.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel; import fj.data.Either; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/relations/FulfilledCapabilitiesMigrationService.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/relations/FulfilledCapabilitiesMigrationService.java index 4a91290dbc..23b1afec93 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/relations/FulfilledCapabilitiesMigrationService.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/relations/FulfilledCapabilitiesMigrationService.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel.relations; import fj.data.Either; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/relations/FulfilledCapabilityRequirementMigrationService.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/relations/FulfilledCapabilityRequirementMigrationService.java index aad2eb4412..33b5574408 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/relations/FulfilledCapabilityRequirementMigrationService.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/relations/FulfilledCapabilityRequirementMigrationService.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel.relations; import fj.Function; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/relations/FulfilledRequirementsMigrationService.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/relations/FulfilledRequirementsMigrationService.java index 33c6b2254e..2b720af751 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/relations/FulfilledRequirementsMigrationService.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/relations/FulfilledRequirementsMigrationService.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel.relations; import fj.data.Either; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/relations/RequirementsCapabilitiesMigrationService.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/relations/RequirementsCapabilitiesMigrationService.java index f74e40a8fc..e468dd2fc5 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/relations/RequirementsCapabilitiesMigrationService.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/jsonmodel/relations/RequirementsCapabilitiesMigrationService.java @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel.relations; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; 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 new file mode 100644 index 0000000000..a57a237e76 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/ValidationToolBL.java @@ -0,0 +1,43 @@ +package org.openecomp.sdc.asdctool.impl.validator; + +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.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.*; + +/** + * Created by chaya on 7/3/2017. + */ +@Component +public class ValidationToolBL { + + private static Logger log = LoggerFactory.getLogger(ValidationToolBL.class.getName()); + + @Autowired + protected List<ValidatorExecuter> validators; + + @Autowired + protected ReportManager reportManager; + + private boolean allValid = true; + + + public boolean validateAll() { + for (ValidatorExecuter validatorExec: validators) { + System.out.println("ValidatorExecuter "+validatorExec.getName()+" started"); + if (!validatorExec.executeValidations()) { + allValid = false; + System.out.println("ValidatorExecuter "+validatorExec.getName()+" finished with warnings"); + } + else { + System.out.println("ValidatorExecuter "+validatorExec.getName()+" finished successfully"); + } + } + return allValid; + } + +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/config/ValidationConfigManager.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/config/ValidationConfigManager.java new file mode 100644 index 0000000000..facb9d70f2 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/config/ValidationConfigManager.java @@ -0,0 +1,39 @@ +package org.openecomp.sdc.asdctool.impl.validator.config; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +/** + * Created by chaya on 7/4/2017. + */ +public class ValidationConfigManager { + + private static Properties prop = new Properties(); + + public static String getOutputFilePath() { + return outputFilePath; + } + + public static void setOutputFilePath(String outputFilePath) { + ValidationConfigManager.outputFilePath = outputFilePath; + } + + private static String outputFilePath; + + public static Properties setValidationConfiguration(String path){ + InputStream input = null; + try { + input = new FileInputStream(path); + prop.load(input); + } catch (IOException ex) { + ex.printStackTrace(); + } + return prop; + } + + public static Properties getValidationConfiguration() { + return prop; + } +} 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 new file mode 100644 index 0000000000..87b27c7369 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/config/ValidationToolConfiguration.java @@ -0,0 +1,133 @@ +package org.openecomp.sdc.asdctool.impl.validator.config; + +import org.openecomp.sdc.asdctool.impl.migration.v1707.MigrationTitanStrategy; +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; +import org.openecomp.sdc.asdctool.impl.validator.executers.ServiceValidatorExecuter; +import org.openecomp.sdc.asdctool.impl.validator.executers.VfValidatorExecuter; +import org.openecomp.sdc.asdctool.impl.validator.ValidationToolBL; +import org.openecomp.sdc.asdctool.impl.validator.utils.ReportManager; +import org.openecomp.sdc.be.dao.DAOTitanStrategy; +import org.openecomp.sdc.be.dao.TitanClientStrategy; +import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao; +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.*; +import org.openecomp.sdc.be.model.operations.api.ToscaDefinitionPathCalculator; +import org.openecomp.sdc.be.model.operations.impl.ToscaDefinitionPathCalculatorImpl; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Created by chaya on 7/3/2017. + */ +@Configuration +public class ValidationToolConfiguration { + + @Bean + public ServiceValidatorExecuter basicServiceValidator() { return new ServiceValidatorExecuter();} + + @Bean + public VfArtifactValidationTask vfArtifactValidationTask() { return new VfArtifactValidationTask(); } + + @Bean + public ServiceArtifactValidationTask serviceArtifactValidationTask() { return new ServiceArtifactValidationTask();} + + @Bean + public ValidationToolBL validationToolBL() { + return new ValidationToolBL(); + } + + @Bean + public VfValidatorExecuter basicVfValidator() { return new VfValidatorExecuter();} + + @Bean + public ReportManager reportManager() { return new ReportManager();} + + @Bean(name = "artifact-cassandra-dao") + public ArtifactCassandraDao artifactCassandraDao() { + return new ArtifactCassandraDao(); + } + + @Bean + public ArtifactValidationUtils artifactValidationUtils() { return new ArtifactValidationUtils();} + + @Bean(name = "groups-operation") + public GroupsOperation jsonGroupsOperation() { + return new GroupsOperation(); + } + + @Bean(name = "cassandra-client") + public CassandraClient cassandraClient() { + return new CassandraClient(); + } + + @Bean(name = "dao-titan-strategy") + public TitanClientStrategy daoStrategy() { + return new DAOTitanStrategy(); + } + + @Bean(name = "migration-titan-client", initMethod = "createGraph") + public TitanGraphClient titanMigrationClient(@Qualifier("dao-titan-strategy") TitanClientStrategy titanClientStrategy) { + return new TitanGraphClient(titanClientStrategy); + } + + @Bean(name = "tosca-operation-facade") + public ToscaOperationFacade toscaOperationFacade() { + return new ToscaOperationFacade(); + } + + @Bean(name = "node-type-operation") + public NodeTypeOperation nodeTypeOperation(@Qualifier("mig-derived-resolver") DerivedNodeTypeResolver migrationDerivedNodeTypeResolver) { + return new NodeTypeOperation(migrationDerivedNodeTypeResolver); + } + + @Bean(name = "topology-template-operation") + public TopologyTemplateOperation topologyTemplateOperation() { + return new TopologyTemplateOperation(); + } + + @Bean(name = "node-template-operation") + public NodeTemplateOperation nodeTemplateOperation() { + return new NodeTemplateOperation(); + } + + @Bean(name = "mig-derived-resolver") + public DerivedNodeTypeResolver migrationDerivedNodeTypeResolver() { + return new ByToscaNameDerivedNodeTypeResolver(); + } + + @Bean(name = "titan-dao") + public TitanDao titanDao(@Qualifier("migration-titan-client") TitanGraphClient titanGraphClient) { + return new TitanDao(titanGraphClient); + } + + @Bean(name = "category-operation") + public CategoryOperation categoryOperation() { + return new CategoryOperation(); + } + + @Bean(name = "tosca-path-calculator") + public ToscaDefinitionPathCalculator pathCalculator() { + return new ToscaDefinitionPathCalculatorImpl(); + } + + @Bean(name = "artifacts-operation") + public ArtifactsOperations artifactsOperation() { + return new ArtifactsOperations(); + } + + @Bean(name = "tosca-data-operation") + public ToscaDataOperation toscaDataOperation() { + return new ToscaDataOperation(); + } + + @Bean(name = "tosca-element-lifecycle-operation") + public ToscaElementLifecycleOperation toscaElementLifecycleOperation() { + return new ToscaElementLifecycleOperation(); + } +} 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 new file mode 100644 index 0000000000..817ab15418 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ServiceValidatorExecuter.java @@ -0,0 +1,36 @@ +package org.openecomp.sdc.asdctool.impl.validator.executers; + +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.*; + +/** + * Created by chaya on 7/4/2017. + */ +public class ServiceValidatorExecuter extends TopologyTemplateValidatorExecuter implements ValidatorExecuter { + + @Autowired(required = false) + List<ServiceValidationTask> tasks = new ArrayList<>(); + + private static Logger log = LoggerFactory.getLogger(VfValidatorExecuter.class.getName()); + + public ServiceValidatorExecuter() { + setName("SERVICE_VALIDATOR"); + } + + @Override + public boolean executeValidations() { + List<GraphVertex> vertices = getVerticesToValidate(ComponentTypeEnum.SERVICE); + return validate(tasks, vertices); + } + + @Override + public String getName() { + return super.getName(); + } +} 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 new file mode 100644 index 0000000000..16a479dc75 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/TopologyTemplateValidatorExecuter.java @@ -0,0 +1,92 @@ +package org.openecomp.sdc.asdctool.impl.validator.executers; + +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.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.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.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.*; + +/** + * Created by chaya on 7/3/2017. + */ +public class TopologyTemplateValidatorExecuter { + + private static Logger log = LoggerFactory.getLogger(VfValidatorExecuter.class.getName()); + + @Autowired + protected TitanDao titanDao; + + @Autowired + protected ToscaOperationFacade toscaOperationFacade; + + @Autowired + protected TopologyTemplateOperation topologyTemplateOperation; + + protected String name; + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void reportValidateTaskStatus(TopologyTemplateValidationTask validationTask, boolean success, GraphVertex vertexScanned) { + ReportManager.reportValidationTaskStatus(vertexScanned, validationTask.getTaskName(), validationTask.getTaskResultStatus(), success); + } + + protected List<GraphVertex> getVerticesToValidate(ComponentTypeEnum type) { + Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class); + props.put(GraphPropertyEnum.COMPONENT_TYPE, type.name()); + if(type.equals(ComponentTypeEnum.RESOURCE)) { + props.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF); + } + + Either<List<GraphVertex>, TitanOperationStatus> results = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, props); + if (results.isRight()) { + System.out.println("getVerticesToValidate failed "+ results.right().value()); + return new ArrayList<GraphVertex>(); + } + System.out.println("getVerticesToValidate: "+results.left().value().size()+" vertices to scan"); + return results.left().value(); + } + + protected boolean validate(List<? extends TopologyTemplateValidationTask> tasks, List<GraphVertex> vertices) { + ReportManager.reportStartValidatorRun(getName(), vertices.size()); + Set<String> failedTasks = new HashSet<>(); + Set<String> successTasks = new HashSet<>(); + boolean successAllVertices = true; + for (GraphVertex vertex: vertices) { + boolean successAllTasks = true; + for (TopologyTemplateValidationTask task: tasks) { + ReportManager.reportStartTaskRun(vertex, task.getTaskName()); + boolean success = task.validate(vertex); + if (!success) { + failedTasks.add(task.getTaskName()); + successAllVertices = false; + successAllTasks = false; + } else { + successTasks.add(task.getTaskName()); + } + reportValidateTaskStatus(task, success, vertex); + } + String componentScanStatus = successAllTasks? "success" : "failed"; + System.out.println("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/ValidatorExecuter.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ValidatorExecuter.java new file mode 100644 index 0000000000..8281d1f353 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ValidatorExecuter.java @@ -0,0 +1,15 @@ +package org.openecomp.sdc.asdctool.impl.validator.executers; + +import org.openecomp.sdc.asdctool.impl.validator.utils.ElementTypeEnum; +import org.openecomp.sdc.be.model.Component; + +import java.util.List; + +/** + * Created by chaya on 7/3/2017. + */ +public interface ValidatorExecuter { + + boolean executeValidations(); + String getName(); +} 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 new file mode 100644 index 0000000000..913a36fbd4 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/VfValidatorExecuter.java @@ -0,0 +1,32 @@ +package org.openecomp.sdc.asdctool.impl.validator.executers; + +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.*; + +/** + * Created by chaya on 7/3/2017. + */ +public class VfValidatorExecuter extends TopologyTemplateValidatorExecuter implements ValidatorExecuter { + + @Autowired(required = false) + private List<VfValidationTask> tasks = new ArrayList<>(); + + public VfValidatorExecuter() { + setName("BASIC_VF_VALIDATOR"); + } + + @Override + public boolean executeValidations() { + List<GraphVertex> vertices = getVerticesToValidate(ComponentTypeEnum.RESOURCE); + return validate(tasks, vertices); + } + + @Override + public String getName() { + return super.getName(); + } +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/ServiceValidationTask.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/ServiceValidationTask.java new file mode 100644 index 0000000000..89444768a4 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/ServiceValidationTask.java @@ -0,0 +1,25 @@ +package org.openecomp.sdc.asdctool.impl.validator.tasks; + +/** + * Created by chaya on 7/5/2017. + */ +public abstract class ServiceValidationTask implements TopologyTemplateValidationTask { + protected String name = ""; + protected String taskStatus = "NOT_STARTED"; + + @Override + public String getTaskName() { + return this.name; + } + + @Override + public String getTaskResultStatus() { + return this.taskStatus; + } + + @Override + public void setTaskResultStatus(String status) { + this.taskStatus = status; + } + +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/TopologyTemplateValidationTask.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/TopologyTemplateValidationTask.java new file mode 100644 index 0000000000..6ca012546b --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/TopologyTemplateValidationTask.java @@ -0,0 +1,13 @@ +package org.openecomp.sdc.asdctool.impl.validator.tasks; + +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; + +/** + * Created by chaya on 7/5/2017. + */ +public interface TopologyTemplateValidationTask { + boolean validate(GraphVertex vertex); + String getTaskName(); + String getTaskResultStatus(); + void setTaskResultStatus(String status); +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/VfValidationTask.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/VfValidationTask.java new file mode 100644 index 0000000000..ec621b86ae --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/VfValidationTask.java @@ -0,0 +1,25 @@ +package org.openecomp.sdc.asdctool.impl.validator.tasks; + +/** + * Created by chaya on 7/5/2017. + */ +public abstract class VfValidationTask implements TopologyTemplateValidationTask { + protected String taskStatus = "NOT_STARTED"; + protected String name = ""; + + @Override + public String getTaskName() { + return this.name; + } + + @Override + public String getTaskResultStatus() { + return taskStatus; + } + + @Override + public void setTaskResultStatus(String status) { + this.taskStatus = status; + } + +} 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 new file mode 100644 index 0000000000..dfdeec8c67 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/artifacts/ArtifactValidationUtils.java @@ -0,0 +1,106 @@ +package org.openecomp.sdc.asdctool.impl.validator.tasks.artifacts; + +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; +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; +import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition; +import org.openecomp.sdc.be.model.ComponentParametersView; +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.TopologyTemplateOperation; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.*; + +/** + * Created by chaya on 7/6/2017. + */ +public class ArtifactValidationUtils { + + @Autowired + private ArtifactCassandraDao artifactCassandraDao; + + @Autowired + private TopologyTemplateOperation topologyTemplateOperation; + + public boolean validateArtifactsAreInCassandra(GraphVertex vertex, String taskName, List<ArtifactDataDefinition> artifacts) { + boolean allArtifactsExist = true; + for(ArtifactDataDefinition artifact:artifacts) { + boolean isArtifactExist = isArtifcatInCassandra(artifact.getEsId()); + String status = isArtifactExist ? "Artifact " + artifact.getEsId() + " is in Cassandra" : + "Artifact " + artifact.getEsId() + " doesn't exist in Cassandra"; + ReportManager.writeReportLineToFile(status); + if (!isArtifactExist) { + allArtifactsExist = false; + } + } + return allArtifactsExist; + } + + public boolean isArtifcatInCassandra(String uniueId) { + Either<Long, CassandraOperationStatus> countOfArtifactsEither = + artifactCassandraDao.getCountOfArtifactById(uniueId); + if (countOfArtifactsEither.isRight()) { + // print to console + System.out.print("Failed to retrieve artifact with id: "+uniueId+" from Cassandra" ); + return false; + } + Long count = countOfArtifactsEither.left().value(); + if (count <1) { + //System.out.print("Artifact "+uniueId+" count is: "+count); + return false; + } + return true; + } + + public List<ArtifactDataDefinition> addRelevantArtifacts(Map<String, ArtifactDataDefinition> artifactsMap) { + List<ArtifactDataDefinition> artifacts = new ArrayList<>(); + Optional.ofNullable(artifactsMap).orElse(Collections.emptyMap()).forEach( (key, dataDef) -> { + if (dataDef.getEsId() != null && !dataDef.getEsId().isEmpty()) { + artifacts.add(dataDef); + } + }); + return artifacts; + } + + public boolean validateTopologyTemplateArtifacts(GraphVertex vertex, String taskName) { + ComponentParametersView paramView = new ComponentParametersView(); + paramView.disableAll(); + paramView.setIgnoreArtifacts(false); + paramView.setIgnoreComponentInstances(false); + Either<ToscaElement, StorageOperationStatus> toscaElementEither = topologyTemplateOperation.getToscaElement(vertex.getUniqueId(), paramView); + if (toscaElementEither.isRight()) { + return false; + } + TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value(); + Map<String, ArtifactDataDefinition> deploymentArtifacts = element.getDeploymentArtifacts(); + Map<String, ArtifactDataDefinition> artifacts = element.getArtifacts(); + Map<String, ArtifactDataDefinition> apiArtifacts = element.getServiceApiArtifacts(); + Map<String, MapArtifactDataDefinition> instanceArtifacts = element.getInstanceArtifacts(); + Map<String, MapArtifactDataDefinition> instanceDeploymentArtifacts = element.getInstDeploymentArtifacts(); + + List<ArtifactDataDefinition> allArtifacts = new ArrayList<>(); + + allArtifacts.addAll(addRelevantArtifacts(deploymentArtifacts)); + allArtifacts.addAll(addRelevantArtifacts(artifacts)); + allArtifacts.addAll(addRelevantArtifacts(apiArtifacts)); + + if (instanceArtifacts != null) { + instanceArtifacts.forEach((key, artifactMap) -> { + allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition())); + }); + } + + if (instanceDeploymentArtifacts != null) { + instanceDeploymentArtifacts.forEach((key, artifactMap) -> { + allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition())); + }); + } + + return validateArtifactsAreInCassandra(vertex, taskName, allArtifacts); + } +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/artifacts/ServiceArtifactValidationTask.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/artifacts/ServiceArtifactValidationTask.java new file mode 100644 index 0000000000..410691fb47 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/artifacts/ServiceArtifactValidationTask.java @@ -0,0 +1,26 @@ +package org.openecomp.sdc.asdctool.impl.validator.tasks.artifacts; + +import org.openecomp.sdc.asdctool.impl.validator.tasks.ServiceValidationTask; +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; +import org.springframework.beans.factory.annotation.Autowired; + + +/** + * Created by chaya on 7/6/2017. + */ +public class ServiceArtifactValidationTask extends ServiceValidationTask { + + @Autowired + private ArtifactValidationUtils artifactValidationUtils; + + + + public ServiceArtifactValidationTask() { + this.name = "Service Artifact Validation Task"; + } + + @Override + public boolean validate(GraphVertex vertex) { + return artifactValidationUtils.validateTopologyTemplateArtifacts(vertex, getTaskName()); + } +} 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 new file mode 100644 index 0000000000..01bc26fe2b --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/artifacts/VfArtifactValidationTask.java @@ -0,0 +1,28 @@ +package org.openecomp.sdc.asdctool.impl.validator.tasks.artifacts; + +import org.openecomp.sdc.asdctool.impl.validator.tasks.VfValidationTask; +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; +import org.openecomp.sdc.be.model.jsontitan.operations.TopologyTemplateOperation; +import org.springframework.beans.factory.annotation.Autowired; + + +/** + * Created by chaya on 7/4/2017. + */ +public class VfArtifactValidationTask extends VfValidationTask { + + @Autowired + ArtifactValidationUtils artifactValidationUtils; + + @Autowired + protected TopologyTemplateOperation topologyTemplateOperation; + + public VfArtifactValidationTask() { + this.name = "Artifact Validation Task"; + } + + @Override + public boolean validate(GraphVertex vertex) { + return artifactValidationUtils.validateTopologyTemplateArtifacts(vertex, getTaskName()); + } +} 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 new file mode 100644 index 0000000000..245d38c853 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/utils/ElementTypeEnum.java @@ -0,0 +1,59 @@ +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; + +/** + * Created by chaya on 7/4/2017. + */ +public enum ElementTypeEnum { + + VF ("vf", VfValidatorExecuter.class); + //SERVICE("service", ServiceValidatorExecuter.class) + + private String elementType; + private Class clazz; + + ElementTypeEnum(String elementType, Class clazz) { + this. elementType = elementType; + this.clazz = clazz; + } + + public static ElementTypeEnum getByType(String elementType){ + for(ElementTypeEnum currType :ElementTypeEnum.values()){ + if(currType.getElementType().equals(elementType)){ + return currType; + } + } + return null; + } + + public static List<String> getAllTypes() { + + List<String> arrayList = new ArrayList<String>(); + + for (ElementTypeEnum graphType : ElementTypeEnum.values()) { + arrayList.add(graphType.getElementType()); + } + return arrayList; + } + + + public String getElementType() { + return elementType; + } + + public void setElementType(String elementType) { + this.elementType = elementType; + } + + public Class getClazz() { + return clazz; + } + + public void setClazz(Class clazz) { + this.clazz = clazz; + } +} 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 new file mode 100644 index 0000000000..1fdde7dd80 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/utils/ReportManager.java @@ -0,0 +1,93 @@ +package org.openecomp.sdc.asdctool.impl.validator.utils; + +import org.apache.commons.lang.text.StrBuilder; +import org.openecomp.sdc.asdctool.impl.validator.tasks.TopologyTemplateValidationTask; +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.ArrayList; +import java.util.List; +import java.util.Set; + +/** + * Created by chaya on 7/5/2017. + */ +public class ReportManager { + + private static List<ValidationTaskResult> taskResults; + private static String reportOutputFilePath; + + public ReportManager() { + try { + taskResults = new ArrayList<>(); + // open file for first time + reportOutputFilePath = ValidationConfigManager.getOutputFilePath(); + StrBuilder sb = new StrBuilder(); + sb.appendln("-----------------------Validation Tool Results:-------------------------"); + Files.write(Paths.get(reportOutputFilePath), sb.toString().getBytes()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void reportValidationTaskStatus(GraphVertex vertexScanned, String taskName, String taskResultMessage, boolean success) { + taskResults.add(new ValidationTaskResult(vertexScanned, taskName, taskResultMessage, success)); + printValidationTaskStatus(vertexScanned, taskName, success); + } + + private static void printValidationTaskStatus(GraphVertex vertexScanned, String taskName, boolean success) { + String successStatus = success ? "success" : "failed"; + String line = "-----------------------Vertex: "+vertexScanned.getUniqueId()+", Task " + taskName + " " +successStatus+"-----------------------"; + StrBuilder sb = new StrBuilder(); + writeReportLineToFile(sb.appendNewLine().toString()); + sb.appendln(line); + sb.appendNewLine(); + writeReportLineToFile(line); + } + + public static void reportValidationTaskSummary(TopologyTemplateValidationTask task, int numOfFailedComponents, int numOfSuccessComponents) { + StrBuilder sb = new StrBuilder(); + sb.appendNewLine(); + sb.appendln("-----------------------Task " + task.getTaskName() + " Validation Summary-----------------------"); + sb.appendln("Num of failed components: "+ numOfFailedComponents); + sb.appendln("Num of success components: "+ numOfSuccessComponents); + sb.appendln("Total components scanned: " + numOfFailedComponents+numOfSuccessComponents); + writeReportLineToFile(sb.toString()); + } + + public static void writeReportLineToFile(String message) { + try { + Files.write(Paths.get(reportOutputFilePath), new StrBuilder().appendNewLine().toString().getBytes(), StandardOpenOption.APPEND); + Files.write(Paths.get(reportOutputFilePath), message.getBytes(), StandardOpenOption.APPEND); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void reportValidatorTypeSummary(String validatorName, Set<String> failedTasksNames, Set<String> successTasksNames){ + StrBuilder sb = new StrBuilder(); + sb.appendNewLine().appendNewLine(); + sb.appendln("-----------------------ValidatorExecuter " + validatorName + " Validation Summary-----------------------"); + sb.appendln("Failed tasks: "+ failedTasksNames); + sb.appendln("Success tasks: "+ successTasksNames); + writeReportLineToFile(sb.toString()); + } + + public static void reportStartValidatorRun(String validatorName, int componenentsNum) { + StrBuilder sb = new StrBuilder(); + sb.appendNewLine().appendNewLine(); + sb.appendln("------ValidatorExecuter " + validatorName + " Validation Started, on "+componenentsNum+" components---------"); + writeReportLineToFile(sb.toString()); + } + + public static void reportStartTaskRun(GraphVertex vertex, String taskName){ + StrBuilder sb = new StrBuilder(); + sb.appendNewLine().appendNewLine(); + sb.appendln("-----------------------Vertex: "+vertex.getUniqueId()+", Task " + taskName + " Started-----------------------"); + writeReportLineToFile(sb.toString()); + } +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/utils/ValidationTaskResult.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/utils/ValidationTaskResult.java new file mode 100644 index 0000000000..8fad932941 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/utils/ValidationTaskResult.java @@ -0,0 +1,45 @@ +package org.openecomp.sdc.asdctool.impl.validator.utils; + +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; +import org.openecomp.sdc.be.model.Component; + +/** + * Created by chaya on 7/5/2017. + */ +public class ValidationTaskResult { + public ValidationTaskResult(GraphVertex vertex, String name, String resultMessage, boolean isSuccessful) { + this.vertexScanned = vertex; + this.name = name; + this.resultMessage = resultMessage; + this.isSuccessful = isSuccessful; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getResultMessage() { + return resultMessage; + } + + public void setResultMessage(String resultMessage) { + this.resultMessage = resultMessage; + } + + public boolean isSuccessful() { + return isSuccessful; + } + + public void setSuccessful(boolean successful) { + isSuccessful = successful; + } + + private String name; + private String resultMessage; + private boolean isSuccessful; + private GraphVertex vertexScanned; +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/MigrationMenu.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/MigrationMenu.java index c3d8743b86..600ebf848a 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/MigrationMenu.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/MigrationMenu.java @@ -33,11 +33,7 @@ import org.openecomp.sdc.asdctool.impl.migration.v1607.CsarMigration; import org.openecomp.sdc.asdctool.impl.migration.v1610.TitanFixUtils; import org.openecomp.sdc.asdctool.impl.migration.v1610.ToscaArtifactsAlignment; import org.openecomp.sdc.asdctool.impl.migration.v1702.Migration1702; -import org.openecomp.sdc.asdctool.impl.migration.v1707.Migration1707; -import org.openecomp.sdc.asdctool.impl.migration.v1707.Migration1707Config; -import org.openecomp.sdc.asdctool.impl.migration.v1707.DistributionStatusUpdate; -import org.openecomp.sdc.asdctool.impl.migration.v1707.Migration1707VnfFix; -import org.openecomp.sdc.asdctool.impl.migration.v1707.VfModulesPropertiesAdding; +import org.openecomp.sdc.asdctool.impl.migration.v1707.*; import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.common.api.ConfigurationSource; import org.openecomp.sdc.common.impl.ExternalConfiguration; @@ -52,24 +48,26 @@ public class MigrationMenu { private static final String SERVICE_MIGARTION_BEAN = "serviceMigrationBean"; private static enum MigrationOperationEnum { - MIGRATION_1602_1604("migrate-1602-1604", SERVICE_MIGARTION_BEAN), - ALIGN_DERIVED_FROM_1604("align-derived-from-1604", "derivedFromAlignment"), - MIGRATE_1604_1607("migrate-1604-1607", SERVICE_MIGARTION_BEAN), - ALIGN_VFC_NAMES_1604("align-vfc-names-1604", "vfcNamingAlignmentBean"), - TEST_REMOVE_HEAT_PLACEHOLDERS("testremoveheatplaceholders", SERVICE_MIGARTION_BEAN), - TEST_ADD_GROUP_UUIDS("testaddgroupuuids", SERVICE_MIGARTION_BEAN), - ALIGN_GROUPS("align-groups", "groupsAlignment"), - CLEAN_CSAR("clean-csar", "csarMigration"), - POPULATE_COMPONENT_CACHE("populate-component-cache", "populateComponentCache"), - FIX_PROPERTIES("fix-properties", "titanFixUtils"), - ALIGN_TOSCA_ARTIFACTS("align-tosca-artifacts", "toscaArtifactsAlignment"), + MIGRATION_1602_1604("migrate-1602-1604", SERVICE_MIGARTION_BEAN), + ALIGN_DERIVED_FROM_1604("align-derived-from-1604", "derivedFromAlignment"), + MIGRATE_1604_1607("migrate-1604-1607", SERVICE_MIGARTION_BEAN), + ALIGN_VFC_NAMES_1604("align-vfc-names-1604", "vfcNamingAlignmentBean"), + TEST_REMOVE_HEAT_PLACEHOLDERS("testremoveheatplaceholders", SERVICE_MIGARTION_BEAN), + TEST_ADD_GROUP_UUIDS("testaddgroupuuids", SERVICE_MIGARTION_BEAN), + ALIGN_GROUPS("align-groups", "groupsAlignment"), + CLEAN_CSAR("clean-csar", "csarMigration"), + POPULATE_COMPONENT_CACHE("populate-component-cache", "populateComponentCache"), + FIX_PROPERTIES("fix-properties", "titanFixUtils"), + ALIGN_TOSCA_ARTIFACTS("align-tosca-artifacts", "toscaArtifactsAlignment"), FIX_ICONS("fix-icons", "titanFixUtils"), MIGRATION_1610_1702("migrate-1610-1702", "migration1702"), MIGRATION_1702_1707("migrate-1702-1707", "migration1707"), DISTRIBUTION_STATUS_UPDATE_1707("distribution-status-update-1707", "distributionStatusUpdate"), VFMODULES_PROPERTIES_ADDING("vfModules-properties-adding", "vfModulesPropertiesAdding"), MIGRATION_1707_RELATIONS_FIX("fix-relations-after-migration-1707", "migration1707relationsFix"), - MIGRATION_1707_VNF_FIX("fix-vnf-after-migration-1707", "migration1707vnfFix"); + MIGRATION_1707_VNF_FIX("fix-vnf-after-migration-1707", "migration1707vnfFix"), + MIGRATION_1707_UUID_FIX("fix-UUID-1707", "migration1707UuidFix"), + MIGRATION_1707_MISSING_INFO_FIX("fix-missing-info-1707", "migration1707MissingInfoFix"); // UPDATE_DATA_TYPES("update_data_types", "updateDataTypes"); private String value, beanName; @@ -223,7 +221,7 @@ public class MigrationMenu { ToscaArtifactsAlignment toscaArtifactsAlignment = (ToscaArtifactsAlignment) context.getBean(operationEnum.getBeanName()); boolean isSuccessful = toscaArtifactsAlignment.alignToscaArtifacts(); if (isSuccessful) { - log.info("Tosca Artifacts alignment was finished successfull"); + log.info("Tosca Artifacts alignment was finished successfully"); System.exit(0); } else { log.info("Tosca Artifacts alignment has failed"); @@ -231,81 +229,75 @@ public class MigrationMenu { } break; case MIGRATION_1610_1702: - log.info("Start ASDC migration from 1610 to 1702"); + log.info("Start SDC migration from 1610 to 1702"); Migration1702 migration = (Migration1702) context.getBean(operationEnum.getBeanName()); isSuccessful = migration.migrate(appConfigDir); if (isSuccessful) { - log.info("ASDC migration from 1610 to 1702 was finished successful"); + log.info("SDC migration from 1610 to 1702 was finished successful"); System.exit(0); } else{ - log.info("ASDC migration from 1610 to 1702 has failed"); + log.info("SDC migration from 1610 to 1702 has failed"); System.exit(2); } - + break; case MIGRATION_1702_1707://this migration is currently not needed, but will be commented out for production env -// log.info("Start ASDC migration from 1702 to 1707"); -// Migration1707 migration1707 = (Migration1707) context.getBean(operationEnum.getBeanName()); -// isSuccessful = migration1707.migrate(); -// if (isSuccessful) { -// log.info("SDC migration from 1702 to 1707 was finished successfully"); -// System.exit(0); -// } else{ -// log.info("SDC migration from 1702 to 1707 has failed"); -// System.exit(2); -// } +// log.info("Start SDC migration from 1702 to 1707"); + Migration1707 migration1707 = (Migration1707) context.getBean(operationEnum.getBeanName()); + isSuccessful = migration1707.migrate(); + if (isSuccessful) { + log.info("SDC migration from 1702 to 1707 was finished successfully"); + System.exit(0); + } else{ + log.info("SDC migration from 1702 to 1707 has failed"); + System.exit(2); + } System.exit(0); break; case VFMODULES_PROPERTIES_ADDING://this migration is currently not needed, but will be commented out for production env -// log.info("Start adding new properties to vfModules"); -// VfModulesPropertiesAdding migrationVfModulesProperties = (VfModulesPropertiesAdding) context.getBean(operationEnum.getBeanName()); -// isSuccessful = migrationVfModulesProperties.migrate(args[1]); -// if (isSuccessful) { -// log.info("Adding new properties to vfModules was finished successfully"); -// System.exit(0); -// } else{ -// log.info("Adding new properties to vfModules has failed"); -// System.exit(2); -// } - System.exit(0); - break; - case MIGRATION_1707_VNF_FIX://this migration is currently not needed, but will be commented out for production env -// log.info("Start fixing vnf after 1707 migration"); -// Migration1707VnfFix migrationVnfFix = (Migration1707VnfFix) context.getBean(operationEnum.getBeanName()); -// isSuccessful = migrationVnfFix.migrate(); -// if (isSuccessful) { -// log.info("Fixing VNFs after 1707 migration was finished successfully"); -// System.exit(0); -// } else{ -// log.info("Fixing VNFs after 1707 migration has failed"); -// System.exit(2); -// } + log.info("Start adding new properties to vfModules"); + VfModulesPropertiesAdding migrationVfModulesProperties = (VfModulesPropertiesAdding) context.getBean(operationEnum.getBeanName()); + isSuccessful = migrationVfModulesProperties.migrate(args[1]); + if (isSuccessful) { + log.info("Adding new properties to vfModules was finished successfully"); + System.exit(0); + } else{ + log.info("Adding new properties to vfModules has failed"); + System.exit(2); + } System.exit(0); break; - case DISTRIBUTION_STATUS_UPDATE_1707://not needed can be dropped -// log.info("Start Distribution status update 1707"); -// DistributionStatusUpdate distStatusUpdate = (DistributionStatusUpdate) context.getBean(operationEnum.getBeanName()); -// isSuccessful = distStatusUpdate.migrate(); -// if (isSuccessful) { -// log.info("ASDC Distribution status update 1707 was finished successful"); -// System.exit(0); -// } else{ -// log.info("ASDC Distribution status update 1707 has failed"); -// System.exit(2); -// } + case MIGRATION_1707_UUID_FIX: + if (args == null || args.length < 5) { + System.out.println("Usage: fix-UUID-1707 <configuration dir> <all/distributed_only> <services/service_vf/fix/fix_only_services>"); + System.exit(1); + } + String fixServices = args[3]; + String runMode = args[4]; + log.info("Start fixing artifact UUID after 1707 migration with arguments run with configuration [{}] , for [{}] services", runMode, fixServices); + + Migration1707ArtifactUuidFix migrationFix = (Migration1707ArtifactUuidFix) context.getBean(operationEnum.getBeanName()); + isSuccessful = migrationFix.migrate(fixServices, runMode); + if (isSuccessful) { + log.info("Fixing artifacts UUID for 1707 was finished successfully"); + } else{ + log.info("Fixing artifacts UUID for 1707 has failed"); + System.exit(2); + } System.exit(0); break; - case MIGRATION_1707_RELATIONS_FIX://not needed can be dropped -// log.info("Start fixing relations after 1707 migration"); -// Migration migrationFix = (Migration1707RelationsFix) context.getBean(operationEnum.getBeanName()); -// isSuccessful = migrationFix.migrate(); -// if (isSuccessful) { -// log.info("Fixing relations after 1707 migration was finished successfully"); -// System.exit(0); -// } else{ -// log.info("Fixing relations after 1707 migration has failed"); -// System.exit(2); -// } + case MIGRATION_1707_MISSING_INFO_FIX: + + log.info("Start fixing missing group and instance info after 1707 migration"); + + Migration1707MissingInfoFix migration1707Fix = (Migration1707MissingInfoFix) context.getBean(operationEnum.getBeanName()); + isSuccessful = migration1707Fix.migrate(); + if (isSuccessful) { + log.info("Fixing groups and node templates missing info was finished successfully"); + } else{ + log.info("Fixing groups and node templates missing info has failed"); + System.exit(2); + } System.exit(0); break; default: @@ -347,5 +339,7 @@ public class MigrationMenu { System.out.println("Usage: vfModules-properties-adding <group_types_input_file path> <configuration dir>"); System.out.println("Usage: fix-relations-after-migration-1707 <configuration dir>"); System.out.println("Usage: fix-vnf-after-migration-1707 <configuration dir>"); + System.out.println("Usage: fix-UUID-1707 <configuration dir> <all/distributed_only> <services/service_vf/fix/fix_only_services>"); + System.out.println("Usage: fix-missing-info-1707 <configuration dir>"); } } 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 edeb56787b..efc34a7bc0 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 @@ -1,3 +1,23 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + package org.openecomp.sdc.asdctool.main; import java.io.File; 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 new file mode 100644 index 0000000000..6b10615ad3 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ValidationTool.java @@ -0,0 +1,47 @@ +package org.openecomp.sdc.asdctool.main; + +import org.openecomp.sdc.asdctool.impl.validator.ValidationToolBL; +import org.openecomp.sdc.asdctool.impl.validator.config.ValidationConfigManager; +import org.openecomp.sdc.asdctool.impl.validator.config.ValidationToolConfiguration; +import org.openecomp.sdc.asdctool.impl.migration.v1604.AppConfig; +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.springframework.context.annotation.AnnotationConfigApplicationContext; + +/** + * Created by chaya on 7/3/2017. + */ +public class ValidationTool { + + private static Logger log = LoggerFactory.getLogger(ValidationTool.class.getName()); + + public static void main(String[] args) throws Exception { + + String outputFilePath = args[0]; + ValidationConfigManager.setOutputFilePath(outputFilePath); + + String appConfigDir = args[1]; + AnnotationConfigApplicationContext context = initContext(appConfigDir); + ValidationToolBL validationToolBL = context.getBean(ValidationToolBL.class); + + System.out.println("Start Validation Tool"); + if (validationToolBL.validateAll()) { + System.out.println("Validation finished successfully"); + System.exit(0); + } else { + System.out.println("Validation finished with warnings"); + System.exit(2); + } + } + + private static AnnotationConfigApplicationContext initContext(String appConfigDir) { + ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir); + ConfigurationManager configurationManager = new ConfigurationManager(configurationSource); + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ValidationToolConfiguration.class); + return context; + } +} 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 new file mode 100644 index 0000000000..0457c2136d --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/config/MigrationSpringConfig.java @@ -0,0 +1,59 @@ +package org.openecomp.sdc.asdctool.migration.config; + +import org.openecomp.sdc.asdctool.migration.core.SdcMigrationTool; +import org.openecomp.sdc.asdctool.migration.core.task.Migration; +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.dao.cassandra.CassandraClient; +import org.openecomp.sdc.be.dao.cassandra.MigrationTasksDao; +import org.openecomp.sdc.be.dao.config.DAOSpringConfig; +import org.springframework.beans.factory.annotation.Autowired; +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 java.util.ArrayList; +import java.util.List; + +@Configuration +@Import(DAOSpringConfig.class) +@ComponentScan({"org.openecomp.sdc.asdctool.migration.tasks",//migration tasks + "org.openecomp.sdc.be.model.operations.impl", + "org.openecomp.sdc.be.model.cache", + "org.openecomp.sdc.be.dao.titan", + "org.openecomp.sdc.be.dao.cassandra", + "org.openecomp.sdc.be.model.jsontitan.operations", + "org.openecomp.sdc.be.dao.jsongraph"}) +public class MigrationSpringConfig { + + @Autowired(required=false) + private List<Migration> migrations = new ArrayList<>(); + + @Bean(name = "sdc-migration-tool") + public SdcMigrationTool sdcMigrationTool(MigrationResolver migrationResolver, SdcRepoService sdcRepoService) { + return new SdcMigrationTool(migrationResolver, sdcRepoService); + } + + @Bean(name = "spring-migrations-resolver") + public SpringBeansMigrationResolver migrationResolver(SdcRepoService sdcRepoService) { + return new SpringBeansMigrationResolver(migrations, sdcRepoService); + } + + @Bean(name = "sdc-repo-service") + public SdcRepoService sdcRepoService(MigrationTasksDao migrationTasksDao) { + return new SdcRepoService(migrationTasksDao); + } + + @Bean(name = "sdc-migration-tasks-cassandra-dao") + public MigrationTasksDao migrationTasksDao() { + return new MigrationTasksDao(); + } + + @Bean(name = "cassandra-client") + public CassandraClient cassandraClient() { + return new CassandraClient(); + } + +} 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 new file mode 100644 index 0000000000..003a27a1e4 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/DBVersion.java @@ -0,0 +1,95 @@ +package org.openecomp.sdc.asdctool.migration.core; + +import java.math.BigInteger; + +public class DBVersion implements Comparable<DBVersion>{ + + private static final String VERSION_PARTS_SEPARATOR = "\\."; + private static final int MAJOR_PART_IDX = 0; + private static final int MINOR_PART_IDX = 1; + private BigInteger major; + private BigInteger minor; + + /** + * 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(1710, 0); + + private DBVersion(BigInteger major, BigInteger minor) { + this.major = major; + this.minor = minor; + } + + private DBVersion(int major, int minor) { + this.major = BigInteger.valueOf(major); + this.minor = BigInteger.valueOf(minor); + } + + public BigInteger getMajor() { + return major; + } + + public BigInteger getMinor() { + return minor; + } + + public static DBVersion from(BigInteger major, BigInteger minor) { + return new DBVersion(major, minor); + } + + public static DBVersion fromString(String version) { + String[] split = version.split(VERSION_PARTS_SEPARATOR); + if (split.length != 2) { + throw new MigrationException("version must be of pattern: <major>.<minor>"); + } + return new DBVersion(getVersionPart(split[MAJOR_PART_IDX]), + getVersionPart(split[MINOR_PART_IDX])); + + } + + private static BigInteger getVersionPart(String versionPart) { + try { + return new BigInteger(versionPart); + } catch (NumberFormatException e) { + throw new MigrationException(String.format("version part %s is non numeric", versionPart)); + } + } + + @Override + public String toString() { + return String.format("%s.%s", major, minor); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + DBVersion dbVersion = (DBVersion) o; + + return major.equals(dbVersion.major) && minor.equals(dbVersion.minor); + } + + @Override + public int hashCode() { + int result = major.hashCode(); + result = 31 * result + minor.hashCode(); + return result; + } + + @Override + public int compareTo(DBVersion o) { + if (o == null) { + return 1; + } + int majorsComparision = this.major.compareTo(o.major); + if (majorsComparision != 0) { + return majorsComparision; + } + int minorsComparision = this.minor.compareTo(o.minor); + if (minorsComparision != 0) { + return minorsComparision; + } + return 0; + } +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/MigrationException.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/MigrationException.java new file mode 100644 index 0000000000..e9e805355e --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/MigrationException.java @@ -0,0 +1,13 @@ +package org.openecomp.sdc.asdctool.migration.core; + +public class MigrationException extends RuntimeException { + + public MigrationException(String message) { + super(message); + } + + public MigrationException(String message, RuntimeException e) { + super(message, e); + } + +} 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 new file mode 100644 index 0000000000..e2691dc7f8 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/SdcMigrationTool.java @@ -0,0 +1,62 @@ +package org.openecomp.sdc.asdctool.migration.core; + +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.Migration; +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 java.util.List; + +public class SdcMigrationTool { + + private static final Logger LOGGER = LoggerFactory.getLogger(SdcMigrationTool.class); + + private MigrationResolver migrationsResolver; + + private SdcRepoService sdcRepoService; + + public SdcMigrationTool(MigrationResolver migrationsResolver, SdcRepoService sdcRepoService) { + this.migrationsResolver = migrationsResolver; + this.sdcRepoService = sdcRepoService; + } + + public SdcMigrationTool() { + } + + public boolean migrate(boolean enforceAll) { + LOGGER.info("starting migration process"); + handleEnforceMigrationFlag(enforceAll); + List<Migration> migrations = migrationsResolver.resolveMigrations(); + LOGGER.info("there are {} migrations task to execute", migrations.size()); + for (Migration migration : migrations) { + try { + MigrationExecutionResult executionResult = new MigrationExecutorImpl().execute(migration); + if (migrationHasFailed(executionResult)) { + LOGGER.error("migration {} with version {} has failed. error msg: {}", migration.getClass().getName(), migration.getVersion().toString(), executionResult.getMsg()); + return false; + } + sdcRepoService.createMigrationTask(executionResult.toMigrationTaskEntry()); + } catch (RuntimeException e) { + LOGGER.error("migration {} with version {} has failed. error msg: {}", migration.getClass().getName(), migration.getVersion().toString(), e); + return false; + } + } + return true; + } + + private boolean migrationHasFailed(MigrationExecutionResult migrationResult) { + return migrationResult.getMigrationStatus().equals(MigrationResult.MigrationStatus.FAILED); + } + + private void handleEnforceMigrationFlag(boolean enforceAll) { + if (enforceAll) { + LOGGER.info("enforcing migration for current version"); + sdcRepoService.clearTasksForCurrentMajor(); + } + } + +} 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 new file mode 100644 index 0000000000..4ebec6e440 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/execution/MigrationExecutionResult.java @@ -0,0 +1,70 @@ +package org.openecomp.sdc.asdctool.migration.core.execution; + +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; + private String msg; + private double executionTime; + private DBVersion version; + private String taskName; + + public MigrationTaskEntry toMigrationTaskEntry() { + MigrationTaskEntry migrationTaskEntry = new MigrationTaskEntry(); + migrationTaskEntry.setMajorVersion(this.getVersion().getMajor().longValue()); + migrationTaskEntry.setMinorVersion(this.getVersion().getMinor().longValue()); + migrationTaskEntry.setTimestamp(new Date()); + migrationTaskEntry.setTaskName(this.getTaskName()); + migrationTaskEntry.setTaskStatus(this.getMigrationStatus().name()); + migrationTaskEntry.setMessage(this.getMsg()); + migrationTaskEntry.setExecutionTime(this.getExecutionTime()); + return migrationTaskEntry; + } + + + public MigrationResult.MigrationStatus getMigrationStatus() { + return migrationStatus; + } + + void setMigrationStatus(MigrationResult.MigrationStatus migrationStatus) { + this.migrationStatus = migrationStatus; + } + + public String getMsg() { + return msg; + } + + void setMsg(String msg) { + this.msg = msg; + } + + double getExecutionTime() { + return executionTime; + } + + void setExecutionTime(double executionTime) { + this.executionTime = executionTime; + } + + public DBVersion getVersion() { + return version; + } + + public void setVersion(DBVersion version) { + this.version = version; + } + + String getTaskName() { + return taskName; + } + + void setTaskName(String taskName) { + this.taskName = taskName; + } + +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/execution/MigrationExecutor.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/execution/MigrationExecutor.java new file mode 100644 index 0000000000..aba5056b48 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/execution/MigrationExecutor.java @@ -0,0 +1,15 @@ +package org.openecomp.sdc.asdctool.migration.core.execution; + +import org.openecomp.sdc.asdctool.migration.core.MigrationException; +import org.openecomp.sdc.asdctool.migration.core.task.Migration; + +public interface MigrationExecutor { + + /** + * @param migration the migration to execute + * @return a {@link MigrationExecutionResult} with the relevant data on the current migration execution; + * @throws MigrationException in case there was an unexpected exception during migration + */ + MigrationExecutionResult execute(Migration migration) throws MigrationException; + +} 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 new file mode 100644 index 0000000000..2e4d3ba8b0 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/execution/MigrationExecutorImpl.java @@ -0,0 +1,47 @@ +package org.openecomp.sdc.asdctool.migration.core.execution; + +import org.openecomp.sdc.asdctool.migration.core.MigrationException; +import org.openecomp.sdc.asdctool.migration.core.task.Migration; +import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.util.StopWatch; + + +public class MigrationExecutorImpl implements MigrationExecutor { + + private static final Logger LOGGER = LoggerFactory.getLogger(MigrationExecutorImpl.class); + + @Override + public MigrationExecutionResult execute(Migration migration) throws MigrationException { + try { + LOGGER.info("starting migration {}. description: {}. version {}", migration.getClass().getName(), migration.description(), migration.getVersion().toString()); + StopWatch stopWatch = new StopWatch(); + stopWatch.start(); + MigrationResult migrationResult = migration.migrate(); + stopWatch.stop(); + double executionTime = stopWatch.getTotalTimeSeconds(); + return logAndCreateExecutionResult(migration, migrationResult, executionTime); + } catch (RuntimeException e) { + LOGGER.error("migration {} has failed!", migration.description(), e); + throw new MigrationException("migration %s failed!!!", e); + + } + } + + private MigrationExecutionResult logAndCreateExecutionResult(Migration migration, MigrationResult migrationResult, double executionTime) { + LOGGER.info("finished migration {}. with version {}. migration status: {}, migration message: {}, execution time: {}", migration.getClass().getName(), migration.getVersion().toString(), migrationResult.getMigrationStatus().name(), migrationResult.getMsg(), executionTime); + return createMigrationTask(migration, migrationResult, executionTime); + } + + private MigrationExecutionResult createMigrationTask(Migration migration, MigrationResult migrationResult, double totalTimeSeconds) { + MigrationExecutionResult migrationExecutionResult = new MigrationExecutionResult(); + migrationExecutionResult.setExecutionTime(totalTimeSeconds); + migrationExecutionResult.setMigrationStatus(migrationResult.getMigrationStatus()); + migrationExecutionResult.setMsg(migrationResult.getMsg()); + migrationExecutionResult.setTaskName(migration.getClass().getName()); + migrationExecutionResult.setVersion(migration.getVersion()); + return migrationExecutionResult; + } + +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/task/Migration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/task/Migration.java new file mode 100644 index 0000000000..58f201acf5 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/task/Migration.java @@ -0,0 +1,14 @@ +package org.openecomp.sdc.asdctool.migration.core.task; + + +import org.openecomp.sdc.asdctool.migration.core.DBVersion; + +public interface Migration { + + String description(); + + DBVersion getVersion(); + + MigrationResult migrate(); + +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/task/MigrationResult.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/task/MigrationResult.java new file mode 100644 index 0000000000..8c4c090d94 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/core/task/MigrationResult.java @@ -0,0 +1,31 @@ +package org.openecomp.sdc.asdctool.migration.core.task; + +public class MigrationResult { + + private String msg; + private MigrationStatus migrationStatus; + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public MigrationStatus getMigrationStatus() { + return migrationStatus; + } + + public void setMigrationStatus(MigrationStatus migrationStatus) { + this.migrationStatus = migrationStatus; + } + + public enum MigrationStatus { + COMPLETED, + COMPLETED_WITH_ERRORS, + FAILED + } + + +} 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 new file mode 100644 index 0000000000..1e8a533624 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/main/MigrationMenu.java @@ -0,0 +1,100 @@ +package org.openecomp.sdc.asdctool.migration.main; + +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.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.openecomp.sdc.asdctool.migration.config.MigrationSpringConfig; +import org.openecomp.sdc.asdctool.migration.core.SdcMigrationTool; +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.springframework.context.annotation.AnnotationConfigApplicationContext; + +public class MigrationMenu { + + private final static Logger LOGGER = LoggerFactory.getLogger(MigrationMenu.class); + + public static void main(String[] args) { + CommandLine commandLine = initCmdLineOptions(args); + String appConfigDir = commandLine.getOptionValue("c"); + boolean enforceAll = commandLine.hasOption("e"); + uploadConfiguration(appConfigDir); + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MigrationSpringConfig.class); + doMigrate(enforceAll, context); + + } + + private static void doMigrate(boolean enforceAll, AnnotationConfigApplicationContext context) { + SdcMigrationTool migrationTool = context.getBean(SdcMigrationTool.class); + boolean migrate = migrationTool.migrate(enforceAll); + if (migrate) { + LOGGER.info("migration completed successfully"); + System.exit(0); + } else { + LOGGER.error("migration failed"); + System.exit(1); + } + } + + private static CommandLine initCmdLineOptions(String[] args) { + Options options = buildCmdLineOptions(); + CommandLineParser parser = new DefaultParser(); + try { + // parse the command line arguments + return parser.parse( options, args ); + } + catch( ParseException exp ) { + // oops, something went wrong + System.err.println( "Parsing failed. Reason: " + exp.getMessage() ); + usageAndExit(options); + } + return null; + } + + private static void usageAndExit(Options options) { + HelpFormatter formatter = new HelpFormatter(); + formatter.printHelp( "yy", options ); + System.exit(1); + } + + private static Options buildCmdLineOptions() { + Option configPath = buildConfigPathOption(); + + Option enforceAll = buildEnforceAllOption(); + + Options options = new Options(); + options.addOption(configPath); + options.addOption(enforceAll); + return options; + } + + private static Option buildEnforceAllOption() { + return Option.builder("e") + .longOpt("enforceAll") + .desc("enforce running all migration steps for current version") + .build(); + } + + private static Option buildConfigPathOption() { + return Option.builder("c") + .longOpt("configFolderPath") + .required() + .hasArg() + .desc("path to sdc configuration folder - required") + .build(); + } + + private static void uploadConfiguration(String appConfigDir) { + ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir); + ConfigurationManager configurationManager = new ConfigurationManager(configurationSource); + } + + +} 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 new file mode 100644 index 0000000000..b272d45ff0 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/resolver/MigrationResolver.java @@ -0,0 +1,16 @@ +package org.openecomp.sdc.asdctool.migration.resolver; + + +import org.openecomp.sdc.asdctool.migration.core.task.Migration; + +import java.util.List; + +public interface MigrationResolver { + + /** + * + * @return a list of {@code T} + */ + List<Migration> resolveMigrations(); + +} 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 new file mode 100644 index 0000000000..4af5d76123 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/resolver/SpringBeansMigrationResolver.java @@ -0,0 +1,45 @@ +package org.openecomp.sdc.asdctool.migration.resolver; + + +import org.openecomp.sdc.asdctool.migration.core.DBVersion; +import org.openecomp.sdc.asdctool.migration.core.task.Migration; +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<Migration> migrations = new ArrayList<>(); + + private SdcRepoService sdcRepoService; + + public SpringBeansMigrationResolver(List<Migration> migrations, SdcRepoService sdcRepoService) { + this.migrations = migrations; + this.sdcRepoService = sdcRepoService; + } + + @Override + public List<Migration> resolveMigrations() { + migrations.sort(Comparator.comparing(Migration::getVersion)); + return resolveNonExecutedMigrations(); + } + + //package private for testing + void setMigrations(List<Migration> migrations) { + this.migrations = migrations; + } + + private List<Migration> resolveNonExecutedMigrations() { + DBVersion latestDBVersion = sdcRepoService.getLatestDBVersion(); + return migrations.stream() + .filter(mig -> isMigrationVersionGreaterThanLatestVersion(latestDBVersion, mig)) + .collect(Collectors.toList()); + } + + private boolean isMigrationVersionGreaterThanLatestVersion(DBVersion latestDBVersion, Migration mig) { + return mig.getVersion().compareTo(latestDBVersion) > 0; + } +} 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 new file mode 100644 index 0000000000..2888ecb474 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/service/SdcRepoService.java @@ -0,0 +1,34 @@ +package org.openecomp.sdc.asdctool.migration.service; + +import org.openecomp.sdc.asdctool.migration.core.DBVersion; +import org.openecomp.sdc.be.dao.cassandra.MigrationTasksDao; +import org.openecomp.sdc.be.resources.data.MigrationTaskEntry; + +import java.math.BigInteger; + +public class SdcRepoService { + + private MigrationTasksDao migrationTasksDao; + + public SdcRepoService(MigrationTasksDao migrationTasksDao) { + this.migrationTasksDao = migrationTasksDao; + } + + public DBVersion getLatestDBVersion() { + BigInteger currentMajorVersion = DBVersion.CURRENT_VERSION.getMajor(); + BigInteger latestMinorVersion = migrationTasksDao.getLatestMinorVersion(currentMajorVersion); + return latestMinorVersion == null ? DBVersion.from(currentMajorVersion, BigInteger.valueOf(Integer.MIN_VALUE)) : DBVersion.from(currentMajorVersion, latestMinorVersion); + } + + public void clearTasksForCurrentMajor() { + BigInteger currentMajorVersion = DBVersion.CURRENT_VERSION.getMajor(); + migrationTasksDao.deleteAllTasksForVersion(currentMajorVersion); + } + + public void createMigrationTask(MigrationTaskEntry migrationTaskEntry) { + migrationTasksDao.createMigrationTask(migrationTaskEntry); + } + + + +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1710/ExampleMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1710/ExampleMigration.java new file mode 100644 index 0000000000..1302dc26a8 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1710/ExampleMigration.java @@ -0,0 +1,27 @@ +//package org.openecomp.sdc.asdctool.migration.tasks.mig1710;//package org.openecomp.sdc.migration.tasks.mig1710; +// +//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.springframework.stereotype.Component; +// +//@Component +//public class ExampleMigration implements Migration { +// +// @Override +// public String description() { +// return "some description"; +// } +// +// @Override +// public DBVersion getVersion() { +// return DBVersion.fromString("1710.0"); +// } +// +// @Override +// public MigrationResult migrate() { +// MigrationResult migrationResult = new MigrationResult(); +// migrationResult.setMigrationStatus(MigrationResult.MigrationStatus.COMPLETED); +// return migrationResult; +// } +//}
\ No newline at end of file diff --git a/asdctool/src/main/resources/config/configuration.yaml b/asdctool/src/main/resources/config/configuration.yaml index b998e2cf1f..0421d656be 100644 --- a/asdctool/src/main/resources/config/configuration.yaml +++ b/asdctool/src/main/resources/config/configuration.yaml @@ -507,6 +507,7 @@ cassandraConfig: - { name: sdcaudit, replicationStrategy: SimpleStrategy, replicationInfo: ['1']} - { name: sdcartifact, replicationStrategy: SimpleStrategy, replicationInfo: ['1']} - { name: sdccomponent, replicationStrategy: SimpleStrategy, replicationInfo: ['1']} + - { name: sdcrepository, replicationStrategy: SimpleStrategy, replicationInfo: ['1']} switchoverDetector: diff --git a/asdctool/src/main/resources/config/logback.xml b/asdctool/src/main/resources/config/logback.xml index 0426a32d9e..dadc5254a5 100644 --- a/asdctool/src/main/resources/config/logback.xml +++ b/asdctool/src/main/resources/config/logback.xml @@ -4,7 +4,16 @@ <property scope="system" name="ECOMP-subcomponent-name" value="SDC-TOOL" /> <property name="default-log-pattern" value="%d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX}|%X{uuid}|%X{serviceInstanceID}|%thread||${ECOMP-subcomponent-name}|%X{userId}|%level|%X{alarmSeverity}|%X{localAddr}|${beFqdn}|%X{remoteAddr}|%logger{35}|%X{timer}|ActivityType=<%M>, Desc=<%msg>%n" /> - + + + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <layout class="ch.qos.logback.classic.PatternLayout"> + <encoder> + <pattern>${default-log-pattern}</pattern> + </encoder> + </layout> + </appender> + <appender name="DEBUG_ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${HOME}/asdctool/logs/${ECOMP-component-name}/${ECOMP-subcomponent-name}/debug.log</file> @@ -65,4 +74,8 @@ <appender-ref ref="MALFORMED_DATA"/> </logger> + <logger name="org.openecomp.sdc.asdctool.migration" level="INFO" > + <appender-ref ref="STDOUT"/> + </logger> + </configuration>
\ No newline at end of file diff --git a/asdctool/src/main/resources/scripts/UUIDFix1707.sh b/asdctool/src/main/resources/scripts/UUIDFix1707.sh new file mode 100644 index 0000000000..d6bd8e8ff8 --- /dev/null +++ b/asdctool/src/main/resources/scripts/UUIDFix1707.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.MigrationMenu" + +command="java $JVM_LOG_FILE -cp $JARS $mainClass fix-UUID-1707 1707 $@" +echo $command + +$command +result=$? + + + +echo "***********************************" +echo "***** $result *********************" +echo "***********************************" + +exit $result + + diff --git a/asdctool/src/main/resources/scripts/postMigration1707Fix.sh b/asdctool/src/main/resources/scripts/postMigration1707Fix.sh new file mode 100644 index 0000000000..56a16db70a --- /dev/null +++ b/asdctool/src/main/resources/scripts/postMigration1707Fix.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +############################## +# Data Migration 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.MigrationMenu" + +command="java $JVM_LOG_FILE -Xmx6000M -cp $JARS $mainClass fix-missing-info-1707 $@" +echo $command + +$command +result=$? + + + +echo "***********************************" +echo "***** $result *********************" +echo "***********************************" + +exit $result + + diff --git a/asdctool/src/main/resources/scripts/sdc-migration.sh b/asdctool/src/main/resources/scripts/sdc-migration.sh new file mode 100644 index 0000000000..1616890217 --- /dev/null +++ b/asdctool/src/main/resources/scripts/sdc-migration.sh @@ -0,0 +1,36 @@ +#!/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.migration.main.MigrationMenu" + +command="java $JVM_LOG_FILE -Xmx6000M -cp $JARS $mainClass $@" +echo $command + +$command +result=$? + + + +echo "***********************************" +echo "***** $result *********************" +echo "***********************************" + +exit $result + + + diff --git a/asdctool/src/main/resources/scripts/validationTool.sh b/asdctool/src/main/resources/scripts/validationTool.sh new file mode 100644 index 0000000000..fc82eee72d --- /dev/null +++ b/asdctool/src/main/resources/scripts/validationTool.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +############################## +# Validation Tool # +############################## + +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.ValidationTool" + +command="java $JVM_LOG_FILE -cp $JARS $mainClass reportOutput.txt /apps/jetty/base/be/config/catalog-be/" +echo $command + +$command +result=$? + +echo "***********************************" +echo "***** $result *********************" +echo "***********************************" + +exit $result
\ No newline at end of file diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/DummyMigrationFactory.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/DummyMigrationFactory.java new file mode 100644 index 0000000000..b16951ef02 --- /dev/null +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/DummyMigrationFactory.java @@ -0,0 +1,72 @@ +package org.openecomp.sdc.asdctool.migration; + + +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; + +public class DummyMigrationFactory { + + public static Migration SUCCESSFUL_MIGRATION = new Migration() { + @Override + public String description() { + return "success mig"; + } + + @Override + public DBVersion getVersion() { + return DBVersion.fromString("1710.22"); + } + + @Override + public MigrationResult migrate() { + MigrationResult migrationResult = new MigrationResult(); + migrationResult.setMigrationStatus(MigrationResult.MigrationStatus.COMPLETED); + migrationResult.setMsg("myMsg"); + return migrationResult; + } + }; + + public static Migration FAILED_MIGRATION = new Migration() { + @Override + public String description() { + return "failed mig"; + } + + @Override + public DBVersion getVersion() { + return DBVersion.fromString("1710.22"); + } + + @Override + public MigrationResult migrate() { + MigrationResult migrationResult = new MigrationResult(); + migrationResult.setMigrationStatus(MigrationResult.MigrationStatus.FAILED); + migrationResult.setMsg("myMsg"); + return migrationResult; + } + }; + + public static Migration getMigration(String version, MigrationResult.MigrationStatus status) { + return new Migration() { + @Override + public String description() { + return "success mig"; + } + + @Override + public DBVersion getVersion() { + return DBVersion.fromString(version); + } + + @Override + public MigrationResult migrate() { + MigrationResult migrationResult = new MigrationResult(); + migrationResult.setMigrationStatus(status); + migrationResult.setMsg("myMsg"); + return migrationResult; + } + }; + } + +} diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/core/DBVersionTest.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/core/DBVersionTest.java new file mode 100644 index 0000000000..4e5fd3ab31 --- /dev/null +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/core/DBVersionTest.java @@ -0,0 +1,57 @@ +package org.openecomp.sdc.asdctool.migration.core; + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import static org.testng.Assert.assertEquals; + +public class DBVersionTest { + + + @DataProvider(name = "invalidVersionStringsProvider") + private Object[][] invalidVersionStringsProvider() { + return new Object[][] { + {"1.1.1"}, + {"1.a"}, + {"a.1"}, + {"1"} + }; + } + + @Test(dataProvider = "invalidVersionStringsProvider", expectedExceptions = MigrationException.class) + public void testFromString_invalidVersionString(String invalidVersion) { + DBVersion.fromString(invalidVersion); + } + + @DataProvider(name = "validVersionStringsProvider") + private Object[][] validVersionStringsProvider() { + return new Object[][] { + {"1.1", "1.1"}, + {"10100.0001", "10100.1"}, + {"000.1", "0.1"}, + {"01.00001000", "1.1000"}, + }; + } + + @Test(dataProvider = "validVersionStringsProvider") + public void testFromString(String validString, String expectedVersionString) { + assertEquals(expectedVersionString, DBVersion.fromString(validString).toString()); + } + + @DataProvider(name = "versionComparisionProvider") + public static Object[][] versionComparisionProvider() { + return new Object[][] { + {"1.1", "001.00001", 0}, + {"10.1", "0010.00001", 0}, + {"1.1", "001.000010", -1}, + {"1.1", "0010.00001", -1}, + {"10.10", "0010.00001", 1}, + {"1.1", "001.00", 1}, + }; + } + + @Test(dataProvider = "versionComparisionProvider") + public void testVersionCompareTo2(String firstVersion, String otherVersion, int expectedComparisionResult) throws Exception { + assertEquals(DBVersion.fromString(firstVersion).compareTo(DBVersion.fromString(otherVersion)), expectedComparisionResult); + } +} diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/core/SdcMigrationToolTest.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/core/SdcMigrationToolTest.java new file mode 100644 index 0000000000..b1813227e2 --- /dev/null +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/core/SdcMigrationToolTest.java @@ -0,0 +1,101 @@ +package org.openecomp.sdc.asdctool.migration.core; + +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.internal.verification.Times; +import org.openecomp.sdc.asdctool.migration.core.task.Migration; +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.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.Arrays; +import java.util.Collections; + +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class SdcMigrationToolTest { + + @InjectMocks + private SdcMigrationTool testInstance = spy(SdcMigrationTool.class); + + @Mock + private MigrationResolver migrationResolverMock; + + @Mock + private SdcRepoService sdcRepoServiceMock; + + @BeforeMethod + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testMigrate_noMigrations() throws Exception { + when(migrationResolverMock.resolveMigrations()).thenReturn(Collections.emptyList()); + testInstance.migrate(false); + verify(sdcRepoServiceMock, new Times(0)).clearTasksForCurrentMajor(); + verify(sdcRepoServiceMock, new Times(0)).createMigrationTask(Mockito.any()); + } + + @Test + public void testMigrate_enforceFlag_removeAllMigrationTasksForCurrentVersion() throws Exception { + when(migrationResolverMock.resolveMigrations()).thenReturn(Collections.emptyList()); + testInstance.migrate(true); + verify(sdcRepoServiceMock, new Times(1)).clearTasksForCurrentMajor(); + } + + @Test + public void testMigrate_stopAfterFirstFailure() throws Exception { + when(migrationResolverMock.resolveMigrations()).thenReturn(Arrays.asList(new SuccessfulMigration(), new FailedMigration(), new SuccessfulMigration())); + testInstance.migrate(false); + verify(sdcRepoServiceMock, new Times(0)).clearTasksForCurrentMajor(); + verify(sdcRepoServiceMock, new Times(1)).createMigrationTask(Mockito.any()); + + } + + private class FailedMigration implements Migration { + + @Override + public String description() { + return null; + } + + @Override + public DBVersion getVersion() { + return DBVersion.fromString("1710.22"); + } + + @Override + public MigrationResult migrate() { + MigrationResult migrationResult = new MigrationResult(); + migrationResult.setMigrationStatus(MigrationResult.MigrationStatus.FAILED); + return migrationResult; + } + } + + private class SuccessfulMigration implements Migration { + + @Override + public String description() { + return null; + } + + @Override + public DBVersion getVersion() { + return DBVersion.fromString("1710.22"); + } + + @Override + public MigrationResult migrate() { + MigrationResult migrationResult = new MigrationResult(); + migrationResult.setMigrationStatus(MigrationResult.MigrationStatus.COMPLETED); + return migrationResult; + } + } +} diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/core/execution/MigrationExecutorImplTest.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/core/execution/MigrationExecutorImplTest.java new file mode 100644 index 0000000000..763d6c83d2 --- /dev/null +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/core/execution/MigrationExecutorImplTest.java @@ -0,0 +1,47 @@ +package org.openecomp.sdc.asdctool.migration.core.execution; + +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.testng.annotations.Test; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; + +public class MigrationExecutorImplTest { + + @Test + public void testExecuteMigration() throws Exception { + MigrationExecutionResult execute = new MigrationExecutorImpl().execute(new DummyMigration()); + + } + + private void assertMigrationTaskEntryByMigrationExecutionResult(MigrationExecutionResult executionResult, Migration migration, MigrationResult result) { + assertEquals(executionResult.getMsg(), result.getMsg()); + assertEquals(executionResult.getMigrationStatus().name(), result.getMigrationStatus()); + assertEquals(executionResult.getTaskName(), migration.getClass().getName()); + assertEquals(executionResult.getVersion(), migration.getVersion()); + assertNotNull(executionResult.getExecutionTime()); + } + + private class DummyMigration implements Migration { + + @Override + public String description() { + return null; + } + + @Override + public DBVersion getVersion() { + return DBVersion.fromString("1710.22"); + } + + @Override + public MigrationResult migrate() { + MigrationResult migrationResult = new MigrationResult(); + migrationResult.setMigrationStatus(MigrationResult.MigrationStatus.COMPLETED); + migrationResult.setMsg("myMsg"); + return migrationResult; + } + } +} diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/resolver/SpringBeansMigrationResolverTest.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/resolver/SpringBeansMigrationResolverTest.java new file mode 100644 index 0000000000..6084608f0e --- /dev/null +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/resolver/SpringBeansMigrationResolverTest.java @@ -0,0 +1,84 @@ +package org.openecomp.sdc.asdctool.migration.resolver; + +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +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.asdctool.migration.service.SdcRepoService; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.mockito.Mockito.when; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +public class SpringBeansMigrationResolverTest { + + @InjectMocks + private SpringBeansMigrationResolver testInstance; + + @Mock + private SdcRepoService sdcRepoServiceMock; + + private List<Migration> migrations = Arrays.asList(createMigration("1710.1"), createMigration("1710.22"), createMigration("1707.12"), createMigration("1710.3")); + + + @BeforeMethod + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + testInstance.setMigrations(migrations); + } + + @Test + public void testResolveMigrations_getMigrationsWithVersionGreaterThanLatest() throws Exception { + when(sdcRepoServiceMock.getLatestDBVersion()).thenReturn(DBVersion.fromString("1710.2")); + List<Migration> resolvedMigrations = testInstance.resolveMigrations(); + assertEquals(resolvedMigrations.size(), 2); + assertEquals(resolvedMigrations.get(0).getVersion(), DBVersion.fromString("1710.3")); + assertEquals(resolvedMigrations.get(1).getVersion(), DBVersion.fromString("1710.22")); + } + + @Test + public void testResolveMigration_noLatestVersionForCurrentMajorVersion() throws Exception { + when(sdcRepoServiceMock.getLatestDBVersion()).thenReturn(DBVersion.fromString("1710.-1")); + List<Migration> resolvedMigrations = testInstance.resolveMigrations(); + assertEquals(resolvedMigrations.size(), 3); + assertEquals(resolvedMigrations.get(0).getVersion(), DBVersion.fromString("1710.1")); + assertEquals(resolvedMigrations.get(1).getVersion(), DBVersion.fromString("1710.3")); + assertEquals(resolvedMigrations.get(2).getVersion(), DBVersion.fromString("1710.22")); + } + + @Test + public void testResolveMigrations_emptyMigrationsList() throws Exception { + testInstance.setMigrations(Collections.emptyList()); + when(sdcRepoServiceMock.getLatestDBVersion()).thenReturn(DBVersion.fromString("1710.-1")); + List<Migration> resolvedMigrations = testInstance.resolveMigrations(); + assertTrue(resolvedMigrations.isEmpty()); + } + + private Migration createMigration(String version) { + return new Migration() { + @Override + public String description() { + return null; + } + + @Override + public DBVersion getVersion() { + return DBVersion.fromString(version); + } + + @Override + public MigrationResult migrate() { + return null; + } + }; + } + +} diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/scanner/ClassScanner.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/scanner/ClassScanner.java new file mode 100644 index 0000000000..a2bf623b83 --- /dev/null +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/scanner/ClassScanner.java @@ -0,0 +1,81 @@ +package org.openecomp.sdc.asdctool.migration.scanner; + +import org.apache.commons.io.FileUtils; +import org.openecomp.sdc.asdctool.migration.core.MigrationException; + +import java.io.File; +import java.lang.reflect.Modifier; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +/** + * scan and instantiate classes of given type in the class path + */ +public class ClassScanner { + + + private ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + + public <T> List<T> getAllClassesOfType(String basePackage, Class<T> ofType) { + Collection<File> allClassesInPackage = getAllClassesInPackage(basePackage); + List<T> loadedClasses = new ArrayList<>(); + for (File clazzFile : allClassesInPackage) { + Optional<T> instance = loadAndInstantiateClass(getClassReference(clazzFile), ofType); + instance.ifPresent(loadedClasses::add); + } + return loadedClasses; + } + + private <T> Optional<T> loadAndInstantiateClass(String classReference, Class<T> ofType) { + try { + return instantiateClassOfType(classReference, ofType); + }catch (ClassNotFoundException e) { + //log + throw new MigrationException(String.format("could not find class %s of type %s. cause: %s", classReference, ofType.toGenericString(), e.getMessage())); + } catch (IllegalAccessException e1) { + //log + throw new MigrationException(String.format("could not instantiate class %s of type %s. class is not accessible. cause: %s", classReference, ofType.toGenericString(), e1.getMessage())); + } catch (InstantiationException e2) { + //log + throw new MigrationException(String.format("could not instantiate class %s of type %s. cause: %s", classReference, ofType.toGenericString(), e2.getMessage())); + } + } + + private <T> Optional<T> instantiateClassOfType(String classReference, Class<T> ofType) throws ClassNotFoundException, IllegalAccessException, InstantiationException { + String className = classReference.replaceAll(".class$", "").replaceAll(".class", ""); + Class<?> aClass = classLoader.loadClass(className); + if (ofType.isAssignableFrom(aClass) && isInstantiateAbleClass(aClass)){ + return Optional.of((T) aClass.newInstance()); + } + return Optional.empty(); + } + + private boolean isInstantiateAbleClass(Class<?> clazz) { + return !Modifier.isAbstract(clazz.getModifiers()) && !clazz.isEnum() && !clazz.isAnonymousClass() && !clazz.isInterface(); + } + + private Collection<File> getAllClassesInPackage(String fromPackage) { + String path = fromPackage.replace(".", "/"); + URL resource = classLoader.getResource(path); + if (noMigrationTasks(resource)) { + return Collections.emptyList(); + } + return FileUtils.listFiles(new File(resource.getFile()), new String[]{"class"}, true); + } + + private boolean noMigrationTasks(URL resource) { + return resource == null; + } + + private String getClassReference(File classFile) { + String asPackage = classFile.getPath().replace(File.separator, "."); + String classes = "classes."; + return asPackage.substring(asPackage.indexOf(classes) + classes.length()); + } + + +} diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/service/SdcRepoServiceTest.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/service/SdcRepoServiceTest.java new file mode 100644 index 0000000000..f8e9abe2c9 --- /dev/null +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/service/SdcRepoServiceTest.java @@ -0,0 +1,55 @@ +package org.openecomp.sdc.asdctool.migration.service; + +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.internal.verification.Times; +import org.openecomp.sdc.asdctool.migration.core.DBVersion; +import org.openecomp.sdc.be.dao.cassandra.MigrationTasksDao; +import org.openecomp.sdc.be.resources.data.MigrationTaskEntry; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.math.BigInteger; + +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.testng.Assert.assertEquals; + +public class SdcRepoServiceTest { + + @InjectMocks + private SdcRepoService testInstance; + + @Mock + private MigrationTasksDao migrationTasksDaoMock; + + @BeforeMethod + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testGetLatestVersion_noMinorVersionForCurrentVersion() throws Exception { + when(migrationTasksDaoMock.getLatestMinorVersion(DBVersion.CURRENT_VERSION.getMajor())).thenReturn(null); + DBVersion latestDBVersion = testInstance.getLatestDBVersion(); + assertEquals(latestDBVersion.getMajor(), DBVersion.CURRENT_VERSION.getMajor()); + assertEquals(latestDBVersion.getMinor(), BigInteger.valueOf(Integer.MIN_VALUE)); + } + + @Test + public void testGetLatestVersion() throws Exception { + when(migrationTasksDaoMock.getLatestMinorVersion(DBVersion.CURRENT_VERSION.getMajor())).thenReturn(BigInteger.TEN); + DBVersion latestDBVersion = testInstance.getLatestDBVersion(); + assertEquals(latestDBVersion.getMajor(), DBVersion.CURRENT_VERSION.getMajor()); + assertEquals(latestDBVersion.getMinor(), BigInteger.TEN); + } + + @Test + public void testCreateMigrationTask() throws Exception { + MigrationTaskEntry taskEntry = new MigrationTaskEntry(); + testInstance.createMigrationTask(taskEntry); + verify(migrationTasksDaoMock, new Times(1)).createMigrationTask(taskEntry); + } + +} diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/task/MigrationTasksTest.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/task/MigrationTasksTest.java new file mode 100644 index 0000000000..037d51ba12 --- /dev/null +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/task/MigrationTasksTest.java @@ -0,0 +1,56 @@ +package org.openecomp.sdc.asdctool.migration.task; + +import org.apache.commons.lang.StringUtils; +import org.openecomp.sdc.asdctool.migration.core.DBVersion; +import org.openecomp.sdc.asdctool.migration.core.task.Migration; +import org.openecomp.sdc.asdctool.migration.scanner.ClassScanner; +import org.testng.Assert; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + + +public class MigrationTasksTest { + + public static final String MIGRATIONS_BASE_PACKAGE = "org.openecomp.sdc.asdctool.migration.tasks"; + private List<Migration> migrations; + + @BeforeMethod + public void setUp() throws Exception { + ClassScanner classScanner = new ClassScanner(); + migrations = classScanner.getAllClassesOfType(MIGRATIONS_BASE_PACKAGE, Migration.class); + } + + @Test + public void testNoTasksWithSameVersion() throws Exception { + Map<DBVersion, List<Migration>> migrationsByVersion = migrations.stream().collect(Collectors.groupingBy(Migration::getVersion)); + migrationsByVersion.forEach((version, migrations) -> { + if (migrations.size() > 1) { + System.out.println(String.format("the following migration tasks have the same version %s. versions must be unique", version.toString())); + Assert.fail(String.format("migration tasks %s has same version %s. migration tasks versions must be unique.", getMigrationsNameAsString(migrations), version.toString())); + } + }); + } + + @Test + public void testNoTaskWithVersionGreaterThanCurrentVersion() throws Exception { + Set<Migration> migrationsWithVersionsGreaterThanCurrent = migrations.stream().filter(mig -> mig.getVersion().compareTo(DBVersion.CURRENT_VERSION) > 0) + .collect(Collectors.toSet()); + + if (!migrationsWithVersionsGreaterThanCurrent.isEmpty()) { + Assert.fail(String.format("migrations tasks %s have version which is greater than DBVersion.CURRENT_VERSION %s. did you forget to update current version?", + getMigrationsNameAsString(migrationsWithVersionsGreaterThanCurrent), + DBVersion.CURRENT_VERSION.toString())); + } + } + + private String getMigrationsNameAsString(Collection<Migration> migrations) { + return StringUtils.join(migrations.stream().map(mig -> mig.getClass().getName()).collect(Collectors.toList()), ","); + } +} |