From 16e6457cf3b996f1172183b225bf987400846cc5 Mon Sep 17 00:00:00 2001 From: vasraz Date: Sun, 14 Mar 2021 13:38:29 +0000 Subject: Remove unused code Signed-off-by: Vasyl Razinkov Change-Id: Iaf6ddfe041a16ac7c853ae8446ce62c14aa89784 Issue-ID: SDC-3428 --- .../openecomp/sdc/asdctool/cli/CLIToolData.java | 9 +- .../sdc/asdctool/enums/DistributionStatusEnum.java | 36 - .../asdctool/enums/LifeCycleTransitionEnum.java | 42 - .../sdc/asdctool/impl/ArtifactUuidFix.java | 2570 ++++++++++---------- .../asdctool/impl/internal/tool/CsarGenerator.java | 71 +- .../impl/validator/utils/ValidationTaskResult.java | 64 - .../sdc/asdctool/main/DeleteComponentTool.java | 18 +- .../sdc/asdctool/main/ValidationTool.java | 8 +- .../migration/tasks/InstanceMigrationBase.java | 162 -- .../migration/tasks/handlers/OutputHandler.java | 14 +- .../tasks/mig1710/UpgradeMigration1710.java | 1190 --------- .../tasks/mig1902/SdcGroupsMigration.java | 186 -- .../tasks/mig1902/SdcResourceIconMigration.java | 187 -- .../validator/utils/ValidationTaskResultTest.java | 98 - .../tasks/mig1710/UpgradeMigration1710Test.java | 648 ----- .../tasks/mig1902/SdcGroupsMigrationTest.java | 231 -- .../mig1902/SdcResourceIconMigrationTest.java | 212 -- 17 files changed, 1340 insertions(+), 4406 deletions(-) delete mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/enums/DistributionStatusEnum.java delete mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/enums/LifeCycleTransitionEnum.java delete mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/utils/ValidationTaskResult.java delete mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/InstanceMigrationBase.java delete mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1710/UpgradeMigration1710.java delete mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1902/SdcGroupsMigration.java delete mode 100644 asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1902/SdcResourceIconMigration.java delete mode 100644 asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/utils/ValidationTaskResultTest.java delete mode 100644 asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/tasks/mig1710/UpgradeMigration1710Test.java delete mode 100644 asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/tasks/mig1902/SdcGroupsMigrationTest.java delete mode 100644 asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/tasks/mig1902/SdcResourceIconMigrationTest.java (limited to 'asdctool/src') diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/cli/CLIToolData.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/cli/CLIToolData.java index c490b6d4fb..e75f866ccc 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/cli/CLIToolData.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/cli/CLIToolData.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -32,11 +32,6 @@ public class CLIToolData { this.commandLine = commandLine; } - public CLIToolData(CommandLine commandLine, AbstractApplicationContext springApplicationContext) { - this.commandLine = commandLine; - this.springApplicationContext = springApplicationContext; - } - public CommandLine getCommandLine() { return commandLine; } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/enums/DistributionStatusEnum.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/enums/DistributionStatusEnum.java deleted file mode 100644 index 5e601a0c37..0000000000 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/enums/DistributionStatusEnum.java +++ /dev/null @@ -1,36 +0,0 @@ -/*- - * ============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.enums; - -import lombok.AllArgsConstructor; -import lombok.Getter; - -@Getter -@AllArgsConstructor -public enum DistributionStatusEnum { - DISTRIBUTION_NOT_APPROVED("Distribution not approved"), - DISTRIBUTION_APPROVED("Distribution approved"), - DISTRIBUTED("Distributed"), - DISTRIBUTION_REJECTED("Distribution rejected"); - - private final String value; - -} \ No newline at end of file diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/enums/LifeCycleTransitionEnum.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/enums/LifeCycleTransitionEnum.java deleted file mode 100644 index 6f52734f2f..0000000000 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/enums/LifeCycleTransitionEnum.java +++ /dev/null @@ -1,42 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2020 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.enums; - -import lombok.AllArgsConstructor; -import lombok.Getter; - -@Getter -@AllArgsConstructor -public enum LifeCycleTransitionEnum { - - CHECKOUT("checkout"), - CHECKIN("checkin"), - CERTIFICATION_REQUEST("certificationRequest"), - UNDO_CHECKOUT("undoCheckout"), - CANCEL_CERTIFICATION("cancelCertification"), - START_CERTIFICATION("startCertification"), - FAIL_CERTIFICATION("failCertification"), - CERTIFY("certify"), - DISTRIBUTE("distribute"); - - private final String displayName; - -} \ No newline at end of file diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/ArtifactUuidFix.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/ArtifactUuidFix.java index 579bf35324..2a77b25aa4 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/ArtifactUuidFix.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/ArtifactUuidFix.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -20,10 +20,27 @@ package org.openecomp.sdc.asdctool.impl; +import static java.nio.charset.StandardCharsets.UTF_8; + import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; import fj.data.Either; +import java.io.BufferedWriter; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.util.ArrayList; +import java.util.Collections; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; import org.openecomp.sdc.asdctool.impl.validator.utils.VfModuleArtifactPayloadEx; import org.openecomp.sdc.be.components.distribution.engine.VfModuleArtifactPayload; import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao; @@ -74,1305 +91,1288 @@ import org.openecomp.sdc.common.util.GeneralUtility; import org.openecomp.sdc.exception.ResponseFormat; import org.springframework.beans.factory.annotation.Autowired; -import java.io.BufferedWriter; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.Writer; -import java.util.ArrayList; -import java.util.Collections; -import java.util.EnumMap; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.stream.Collectors; - @org.springframework.stereotype.Component("artifactUuidFix") public class ArtifactUuidFix { - private static final String MIGRATION1707_ARTIFACT_UUID_FIX = "Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} "; - - private static final String FAILED_TO_FETCH_VF_RESOURCES = "Failed to fetch vf resources "; - - private static final String UTF8 = "utf-8"; - - private JanusGraphDao janusGraphDao; - private ToscaOperationFacade toscaOperationFacade; - private ToscaExportHandler toscaExportUtils; - private ArtifactCassandraDao artifactCassandraDao; - private CsarUtils csarUtils; - - private static Logger log = Logger.getLogger(ArtifactUuidFix.class.getName()); - - @Autowired - public ArtifactUuidFix(JanusGraphDao janusGraphDao, - ToscaOperationFacade toscaOperationFacade, ToscaExportHandler toscaExportUtils, - ArtifactCassandraDao artifactCassandraDao, CsarUtils csarUtils) { - this.janusGraphDao = janusGraphDao; - this.toscaOperationFacade = toscaOperationFacade; - this.toscaExportUtils = toscaExportUtils; - this.artifactCassandraDao = artifactCassandraDao; - this.csarUtils = csarUtils; - } - - public boolean doFix(String fixComponent, String runMode) { - List vfLst = new ArrayList<>(); - List serviceList = new ArrayList<>(); - Map> nodeToFixTosca = new HashMap<>(); - Map> vfToFixTosca = new HashMap<>(); - Map> serviceToFixTosca = new HashMap<>(); - - long time = System.currentTimeMillis(); - - doFixTosca(nodeToFixTosca, vfToFixTosca, serviceToFixTosca); - - if ("vf_only".equals(fixComponent)) { - if (!fetchFaultVf(fixComponent, vfLst, time)) { - return false; - } - } else { - if (!fetchServices(fixComponent, serviceList, time)) { - return false; - } - } - if ("service_vf".equals(runMode) || "fix".equals(runMode)) { - log.info("Mode {}. Find problem VFs", runMode); - if (!fetchVf(serviceList, vfLst, time)) { - log.info("Mode {}. Find problem VFs finished with failure", runMode); - return false; - } - log.info("Mode {}. Find problem VFs finished with success", runMode); - } - if ("fix".equals(runMode) || "fix_only_services".equals(runMode)) { - log.info("Mode {}. Start fix", runMode); - if (!fix(vfLst, serviceList, nodeToFixTosca, vfToFixTosca, serviceToFixTosca)) { - 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 vfLst, long time) { - log.info("Find fault VF "); - String fileName = "fault_" + time + ".csv"; - try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF8))) { - writer.write("vf name, vf id, state, version\n"); - - Map hasProps = new EnumMap<>(GraphPropertyEnum.class); - hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); - hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF.name()); - - Map hasNotProps = new EnumMap<>(GraphPropertyEnum.class); - hasNotProps.put(GraphPropertyEnum.IS_DELETED, true); - log.info("Try to fetch resources with properties {} and not {}", hasProps, hasNotProps); - - Either, JanusGraphOperationStatus> servicesByCriteria = janusGraphDao - .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll); - if (servicesByCriteria.isRight()) { - log.info("Failed to fetch resources {}", servicesByCriteria.right().value()); - return false; - } - List resources = servicesByCriteria.left().value(); - for (GraphVertex gv : resources) { - ComponentParametersView filter = new ComponentParametersView(true); - filter.setIgnoreComponentInstances(false); - filter.setIgnoreArtifacts(false); - filter.setIgnoreGroups(false); - - Either toscaElement = toscaOperationFacade - .getToscaElement(gv.getUniqueId()); - if (toscaElement.isRight()) { - log.info("Failed to fetch resources {} {}", gv.getUniqueId(), toscaElement.right().value()); - continue; - } - - Resource resource = toscaElement.left().value(); - String resourceName = resource.getName(); - Map deploymentArtifacts = resource.getDeploymentArtifacts(); - List 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(); - } - janusGraphDao.commit(); - } - - } catch (Exception e) { - log.info(FAILED_TO_FETCH_VF_RESOURCES, e); - return false; - } finally { - janusGraphDao.commit(); - } - return true; - } - - private boolean fetchVf(List serviceList, List vfLst, long time) { - log.info("Find problem VF "); - if (serviceList.isEmpty()) { - log.info("No services as input"); - return true; - } - String fileName = "problemVf_" + time + ".csv"; - try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF8))) { - writer.write("vf name, vf id, state, version, example service name\n"); - Set vfIds = new HashSet<>(); - for (Service service : serviceList) { - List componentInstances = service.getComponentInstances().stream() - .filter(ci -> ci.getOriginType().equals(OriginTypeEnum.VF)).collect(Collectors.toList()); - for (ComponentInstance ci : componentInstances) { - if (!vfIds.contains(ci.getComponentUid())) { - vfIds.add(ci.getComponentUid()); - ComponentParametersView filter = new ComponentParametersView(true); - filter.setIgnoreComponentInstances(false); - filter.setIgnoreArtifacts(false); - filter.setIgnoreGroups(false); - Either toscaElement = toscaOperationFacade - .getToscaElement(ci.getComponentUid(), filter); - if (toscaElement.isRight()) { - log.info("Failed to fetch resource {} {}", ci.getComponentUid(), - toscaElement.right().value()); - continue; - } - Resource resource = toscaElement.left().value(); - if (resource.getResourceType().equals(ResourceTypeEnum.VF)) { - vfLst.add(resource); - writeModuleResultToFile(writer, resource, service); - writer.flush(); - - } - janusGraphDao.commit(); - } - } - } - log.info("output file with list of Vf : {}", fileName); - } catch (Exception e) { - log.info("Failed to fetch services ", e); - return false; - } finally { - janusGraphDao.commit(); - } - return true; - } - - private boolean fetchServices(String fixServices, List serviceList, long time) { - log.info("Find problem Services {}", fixServices); - String fileName = "problemService_" + time + ".csv"; - try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF8))) { - writer.write("service name, service id, state, version\n"); - - Map hasProps = new EnumMap<>(GraphPropertyEnum.class); - hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name()); - if ("distributed_only".equals(fixServices)) { - hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); - hasProps.put(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTED.name()); - } - - Map hasNotProps = new EnumMap<>(GraphPropertyEnum.class); - hasNotProps.put(GraphPropertyEnum.IS_DELETED, true); - log.info("Try to fetch services with properties {} and not {}", hasProps, hasNotProps); - - Either, JanusGraphOperationStatus> servicesByCriteria = janusGraphDao - .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll); - if (servicesByCriteria.isRight()) { - log.info("Failed to fetch services {}", servicesByCriteria.right().value()); - return false; - } - List services = servicesByCriteria.left().value(); - for (GraphVertex gv : services) { - ComponentParametersView filter = new ComponentParametersView(true); - filter.setIgnoreComponentInstances(false); - filter.setIgnoreArtifacts(false); - filter.setIgnoreGroups(false); - - Either toscaElement = toscaOperationFacade - .getToscaElement(gv.getUniqueId(), filter); - if (toscaElement.isRight()) { - log.info("Failed to fetch service {} {}", gv.getUniqueId(), toscaElement.right().value()); - continue; - } - Service service = toscaElement.left().value(); - - String serviceName = (String) gv.getMetadataProperty(GraphPropertyEnum.NAME); - - boolean isProblematic = isProblematicService(service, serviceName); - if (isProblematic) { - serviceList.add(service); - writeModuleResultToFile(writer, service, null); - writer.flush(); - - } - - janusGraphDao.commit(); - } - log.info("output file with list of services : {}", fileName); - } catch (Exception e) { - log.info("Failed to fetch services ", e); - return false; - } finally { - janusGraphDao.commit(); - } - return true; - } - - private boolean isProblematicService( Service service, String serviceName) { - - List componentInstances = service.getComponentInstances(); - - if (componentInstances == null) { - log.info("No instances for service {} ", service.getUniqueId()); - return false; - } - boolean isCheckVFModules = true; - if(service.getLifecycleState() == LifecycleStateEnum.NOT_CERTIFIED_CHECKIN || - service.getLifecycleState() == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT){ - isCheckVFModules = false; - } - for (ComponentInstance ci : componentInstances) { - Map deploymentArtifacts = ci.getDeploymentArtifacts(); - List groupInstances = ci.getGroupInstances(); - if (groupInstances == null || groupInstances.isEmpty()) { - log.info("No instance groups for instance {} in service {} id {} ", ci.getName(), serviceName, - service.getUniqueId()); - continue; - } - List vfModules = null; - if(isCheckVFModules){ - Optional optionalVfModuleArtifact = deploymentArtifacts.values().stream() - .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.VF_MODULES_METADATA.getType())).findAny(); - - if(!optionalVfModuleArtifact.isPresent()) - return true; - - ArtifactDefinition vfModuleArtifact = optionalVfModuleArtifact.get(); - Either, StorageOperationStatus> vfModulesEither = parseVFModuleJson(vfModuleArtifact); - if(vfModulesEither.isRight()){ - log.error("Failed to parse vfModule for service {} status is {}", service.getUniqueId(), vfModulesEither.right().value()); - return true; - } - vfModules = vfModulesEither.left().value(); - if(vfModules == null || vfModules.isEmpty() ){ - log.info("vfModules empty for service {}", service.getUniqueId()); - return true; - } - } - - for (GroupInstance gi : groupInstances) { - if (gi.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) { - VfModuleArtifactPayloadEx vfModule= null; - if(isCheckVFModules && vfModules != null){ - Optional op = vfModules.stream().filter(vf -> vf.getVfModuleModelName().equals(gi.getGroupName())).findAny(); - if(!op.isPresent()){ - log.error("Failed to find vfModule for group {}", gi.getGroupName()); - return true; - } - vfModule = op.get(); - } - if (isProblematicGroupInstance(gi, ci.getName(), serviceName, deploymentArtifacts, vfModule)) { - return true; - } - } - } - - } - return false; - } - - private boolean isProblematicGroup(GroupDefinition gr, String resourceName, - Map deploymentArtifacts) { - List artifacts = gr.getArtifacts(); - List artifactsUuid = gr.getArtifactsUuid(); - Set artifactsSet = new HashSet<>(); - - if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) { - log.info("No groups in resource {} ", resourceName); - return true; - } - artifactsSet.addAll(artifacts); - if (artifactsSet.size() < artifacts.size()) { - log.info(" artifactsSet.size() < artifacts.size() group {} in resource {} ", gr.getName(), resourceName); - return true; - } - - if ((artifactsUuid != null) && (artifacts.size() < artifactsUuid.size())) { - log.info(" artifacts.size() < artifactsUuid.size() group {} in resource {} ", gr.getName(), resourceName); - return true; - } - if (!artifacts.isEmpty() && (artifactsUuid == null || artifactsUuid.isEmpty())) { - log.info( - " artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() group {} in resource {} ", - gr.getName(), resourceName); - return true; - } - if (artifactsUuid != null && 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.parse(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 != null && !artifactsUuid.contains(artifactDefinition.getArtifactUUID())) { - log.info( - " artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} group {} in resource {} ", - artifactlabel, gr.getName(), resourceName); - return true; - } - } - } - for (String artifactUUID : artifactsUuid) { - String label = findArtifactLabelFromArtifactId(artifactUUID); - if (label != null && !label.isEmpty() && !label.equals("")) { - return true; - } - } - - return false; - } - - private boolean isProblematicGroupInstance(GroupInstance gi, String instName, String servicename, - Map deploymentArtifacts, VfModuleArtifactPayloadEx vfModule) { - List artifacts = gi.getArtifacts(); - List artifactsUuid = gi.getArtifactsUuid(); - List instArtifactsUuid = gi.getGroupInstanceArtifactsUuid(); - List instArtifactsId = gi.getGroupInstanceArtifacts(); - Set instArtifatIdSet = new HashSet<>(); - Set artifactsSet = new HashSet<>(); - - log.info("check group {} for instance {} ", gi.getGroupName(), instName); - if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) { - log.info("No instance groups for instance {} in service {} ", instName, servicename); - return true; - } - artifactsSet.addAll(artifacts); - if (artifactsSet.size() < artifacts.size()) { - log.info(" artifactsSet.size() < artifacts.size() group {} in resource {} ", instName, servicename); - return true; - } - - if (instArtifactsId != null && !instArtifactsId.isEmpty()) { - instArtifatIdSet.addAll(instArtifactsId); - } - - if ((artifactsUuid != null) && (artifacts.size() < artifactsUuid.size())) { - log.info(" artifacts.size() < artifactsUuid.size() inst {} in service {} ", instName, servicename); - return true; - } - if (!artifacts.isEmpty() && (artifactsUuid == null || artifactsUuid.isEmpty())) { - log.info( - " artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() inst {} in service {} ", - instName, servicename); - return true; - } - if (artifactsUuid != null && artifactsUuid.contains(null)) { - log.info(" artifactsUuid.contains(null) inst {} in service {} ", instName, servicename); - return true; - } - if (instArtifactsId != null && instArtifatIdSet.size() < instArtifactsId.size()) { - log.info(" instArtifatIdSet.size() < instArtifactsId.size() inst {} in service {} ", instName, servicename); - return true; - } - - if ((instArtifactsId != null && instArtifactsUuid != null) - && instArtifactsId.size() != instArtifactsUuid.size()) { - log.info(" instArtifactsId.size() != instArtifactsUuid.size() 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.parse(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 != null && !artifactsUuid.contains(artifactDefinition.getArtifactUUID())) { - log.info( - " artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ", - artifactlabel, instName, servicename); - return true; - } - } else { - if (instArtifactsUuid == null || instArtifactsUuid.isEmpty()) { - log.info(" instArtifactsUuid empty. label {} inst {} in service {} ", artifactlabel, instName, - servicename); - return true; - } - if (!instArtifactsUuid.contains(artifactDefinition.getArtifactUUID())) { - log.info( - " instArtifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ", - artifactlabel, instName, servicename); - return true; - } - } - } - for (String artifactUUID : artifactsUuid) { - String label = findArtifactLabelFromArtifactId(artifactUUID); - if (label != null && !label.isEmpty() && !"".equals(label)) { - return true; - } - } - if(vfModule != null && artifactsUuid != null){ - return isProblematicVFModule(vfModule, artifactsUuid, instArtifactsUuid); - } - - return false; - } - - private boolean isProblematicVFModule(VfModuleArtifactPayloadEx vfModule, List artifactsUuid, - List instArtifactsUuid) { - log.info(" isProblematicVFModule {} ", vfModule.getVfModuleModelName()); - List vfModuleArtifacts = vfModule.getArtifacts(); - List allArtifacts = new ArrayList<>(); - allArtifacts.addAll(artifactsUuid); - if(instArtifactsUuid != null) - allArtifacts.addAll(instArtifactsUuid); - if((vfModuleArtifacts == null || vfModuleArtifacts.isEmpty()) && !artifactsUuid.isEmpty()){ - log.error(" vfModuleArtifacts == null || vfModuleArtifacts.isEmpty()) && !artifactsUuid.isEmpty()"); - return true; - } - if(vfModuleArtifacts!= null){ - if( vfModuleArtifacts.size() != allArtifacts.size()){ - log.error(" vfModuleArtifacts.size() != allArtifacts.size()"); - return true; - } - for(String vfModuleArtifact: vfModuleArtifacts){ - Optional op = allArtifacts.stream().filter(a -> a.equals(vfModuleArtifact)).findAny(); - if(!op.isPresent()){ - log.error("failed to find artifact {} in group artifacts {}", vfModuleArtifact, allArtifacts); - return true; - } - } - } - return false; - } - - - - private boolean fix(List vfLst, List serviceList, Map> nodesToFixTosca, - Map> vfToFixTosca, Map> servicesToFixTosca) { - boolean res = true; - log.info(" Fix started ***** "); - if (vfLst != null && !vfLst.isEmpty()) { - res = fixVf(vfLst); - - } - - if (res && serviceList != null && !serviceList.isEmpty()) { - res = fixServices(serviceList); - - } - - Set fixedIds = new HashSet<>(); - - long time = System.currentTimeMillis(); - String fileName = "FailedGenerateTosca" + "_" + time + ".csv"; - - try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF8))) { - writer.write("componentType, name, version, UID, UUID, invariantUUID, state\n"); - List failedList = new ArrayList<>(); - - if (res && nodesToFixTosca != null && !nodesToFixTosca.isEmpty()) { - - generateAndSaveToscaArtifacts(nodesToFixTosca, fixedIds, null, failedList); - - } - if (vfToFixTosca != null && !vfToFixTosca.isEmpty()) { - - generateAndSaveToscaArtifacts(vfToFixTosca, fixedIds, vfLst, failedList); - - } - - for (Component component : vfLst) { - res = generateToscaPerComponent(fixedIds, component); - if (res) { - TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(component); - Map groups = topologyTemplate.getGroups(); - res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.GROUPS, EdgeLabelEnum.GROUPS, groups); - if (res) { - Map arifacts = topologyTemplate.getDeploymentArtifacts(); - res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.DEPLOYMENT_ARTIFACTS, - EdgeLabelEnum.DEPLOYMENT_ARTIFACTS, arifacts); - } - if (res) { - Map arifacts = topologyTemplate.getToscaArtifacts(); - res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS, - EdgeLabelEnum.TOSCA_ARTIFACTS, arifacts); - } - janusGraphDao.commit(); - } else { - failedList.add(component); - } - } - - if (servicesToFixTosca != null && !servicesToFixTosca.isEmpty()) { - generateAndSaveToscaArtifacts(servicesToFixTosca, fixedIds, serviceList, failedList); - - } - - - for (Component component : serviceList) { - res = generateToscaPerComponent(fixedIds, component); - if (res) { - TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(component); - Map groups = topologyTemplate.getInstGroups(); - res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS, - groups); - - if (res) { - Map artifacts = topologyTemplate - .getInstDeploymentArtifacts(); - res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, - EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, artifacts); - } - if (res) { - Map arifacts = topologyTemplate.getToscaArtifacts(); - res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS, - EdgeLabelEnum.TOSCA_ARTIFACTS, arifacts); - } - janusGraphDao.commit(); - } else { - failedList.add(component); - } - - } - if (!failedList.isEmpty()) { - for (Component component : failedList) { - StringBuilder sb = new StringBuilder(component.getComponentType().getValue()); - sb.append(",").append(component.getName()).append(",").append(component.getVersion()).append(",") - .append(component.getUniqueId()).append(",").append(component.getUUID()).append(",") - .append(component.getInvariantUUID()).append(",").append(component.getLifecycleState()); - - sb.append("\n"); - writer.write(sb.toString()); - } - writer.flush(); - } - } catch (IOException e) { - log.error(e.getMessage()); - } finally { - janusGraphDao.commit(); - } - log.info(" Fix finished with res {} ***** ", res); - return res; - } - - private boolean generateAndSaveToscaArtifacts(Map> nodesToFixTosca, Set fixedIds, - List componentsWithFailedGroups, List failedList) { - boolean res = true; - log.debug("Migration1707ArtifactUuidFix generateAndSaveToscaArtifacts started "); - for (Map.Entry> entry : nodesToFixTosca.entrySet()) { - - List component = entry.getValue(); - for (Component c : component) { - log.debug("Migration1707ArtifactUuidFix fix tosca on component : id {}, name {} ", c.getUniqueId(), - c.getName()); - if (componentsWithFailedGroups != null) { - Optional op = (Optional) componentsWithFailedGroups.stream() - .filter(cg -> cg.getUniqueId().equals(c.getUniqueId())).findAny(); - if (!op.isPresent()) - res = generateToscaPerComponent(fixedIds, c); - } else - res = generateToscaPerComponent(fixedIds, c); - if (res) { - ToscaElement topologyTemplate = ModelConverter.convertToToscaElement(c); - Map arifacts = topologyTemplate.getToscaArtifacts(); - res = fixDataOnGraph(c.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS, EdgeLabelEnum.TOSCA_ARTIFACTS, - arifacts); - janusGraphDao.commit(); - } else { - failedList.add(c); - } - - } - } - log.debug("Migration1707ArtifactUuidFix generateAndSaveToscaArtifacts finished with res {} ", res); - return res; - } - - private boolean generateToscaPerComponent(Set fixedIds, Component c) { - boolean res = true; - log.debug("Migration1707ArtifactUuidFix generateToscaPerComponent started component name {} id {}", - c.getName(), c.getUniqueId()); - try { - Either toscaElement = toscaOperationFacade - .getToscaFullElement(c.getUniqueId()); - if (toscaElement.isRight()) { - log.info("Failed to fetch resources {} {}", c.getUniqueId(), toscaElement.right().value()); - return false; - } - Component toscaElementFull = toscaElement.left().value(); - toscaElementFull.setGroups(c.getGroups()); - List ciListFull = toscaElementFull.getComponentInstances(); - List ciList = c.getComponentInstances(); - if (ciListFull != null && !ciListFull.isEmpty()) { - ciListFull.forEach(ciFull -> { - ComponentInstance compInst = ciList.stream() - .filter(ci -> ci.getUniqueId().equals(ciFull.getUniqueId())).findAny().get(); - ciFull.setGroupInstances(compInst.getGroupInstances()); - }); - } - - Either either = generateToscaArtifact(toscaElementFull); - - if (either.isRight()) { - log.error("Couldn't generate and save tosca template component unique id {}, name {} error: {}", - toscaElementFull.getUniqueId(), toscaElementFull.getName(), either.right().value()); - res = false; - - } - - if (res) { - c.setToscaArtifacts(either.left().value().getToscaArtifacts()); - fixedIds.add(toscaElementFull.getUniqueId()); - } - } finally { - if (res) - janusGraphDao.commit(); - else - janusGraphDao.rollback(); - } - log.debug("Migration1707ArtifactUuidFix generateToscaPerComponent finished component name {} id {} res {}", - c.getName(), c.getUniqueId(), res); - return res; - } - - private boolean fixDataOnGraph(String componentId, VertexTypeEnum vertexTypeEnum, - EdgeLabelEnum edgeLabelEnum, Map groups) { - log.debug("amount groups to update: VertexTypeEnum {} EdgeLabelEnum {} data size {}", vertexTypeEnum.getName(), - edgeLabelEnum, groups.size()); - boolean res = true; - Either getResponse = janusGraphDao.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 groupVertexEither = janusGraphDao.getChildVertex(componentVertex, - edgeLabelEnum, JsonParseFlagEnum.ParseJson); - if (groupVertexEither.isRight() && groupVertexEither.right().value() == JanusGraphOperationStatus.NOT_FOUND) { - log.debug("no child {} vertex for component unique id {}, error: {}", edgeLabelEnum, componentId, - groupVertexEither.right().value()); - return true; - } - 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 updatevertexEither = janusGraphDao.updateVertex(toscaDataVertex); - if (updatevertexEither.isRight()) { - log.debug("failed to update vertex for component unique id {}, error: {}", componentId, - updatevertexEither.right().value()); - janusGraphDao.rollback(); - return false; - } - } - } - log.debug("Fix data on graph finished: VertexTypeEnum {} EdgeLabelEnum {} res {}", vertexTypeEnum.getName(), - res); - return res; - } - - private boolean fixServices(List serviceList) { - for (Service service : serviceList) { - log.debug("Migration1707ArtifactUuidFix fix service: id {}, name {} ", service.getUniqueId(), - service.getName()); - List instances = service.getComponentInstances(); - for (ComponentInstance instance : instances) { - fixComponentInstances(service, instance); - } - - } - return true; - - } - - private void fixComponentInstances(Service service, ComponentInstance instance) { - Map artifactsMap = instance.getDeploymentArtifacts(); - List groupsList = instance.getGroupInstances(); - if (groupsList != null && artifactsMap != null) { - List groupsToDelete = new ArrayList<>(); - for (GroupInstance group : groupsList) { - fixGroupInstances(service, artifactsMap, groupsToDelete, group); - - } - - if (!groupsToDelete.isEmpty()) { - log.debug("Migration1707ArtifactUuidFix delete group: resource id {}, group instance to delete {} ", - service.getUniqueId(), groupsToDelete); - groupsList.removeAll(groupsToDelete); - - } - - Optional optionalVfModuleArtifact = artifactsMap.values().stream() - .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.VF_MODULES_METADATA.getType())).findAny(); - ArtifactDefinition vfModuleArtifact; - if(!optionalVfModuleArtifact.isPresent()){ - vfModuleArtifact = createVfModuleArtifact(instance, service); - artifactsMap.put(vfModuleArtifact.getArtifactLabel(), vfModuleArtifact); - } - else { - vfModuleArtifact = optionalVfModuleArtifact.get(); - } - fillVfModuleInstHeatEnvPayload(service, instance, groupsList, vfModuleArtifact); - } - } - - private void fixGroupInstances(Service service, Map artifactsMap, - List groupsToDelete, GroupInstance group) { - if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) { - log.debug("Migration1707ArtifactUuidFix fix group: resource id {}, group name {} ", service.getUniqueId(), - group.getName()); - if(group.getArtifacts() != null){ - Set groupArtifactsSet = new HashSet<>(group.getArtifacts()); - if(group.getGroupInstanceArtifacts() != null){ - List groupInsArtifacts = new ArrayList<>(group.getGroupInstanceArtifacts()); - groupArtifactsSet.addAll(groupInsArtifacts); - } - List groupArtifacts = new ArrayList<>(groupArtifactsSet); - - clearGroupInstanceArtifacts(group); - - for (String artifactId : groupArtifacts) { - fixArtifactUndergroupInstances(artifactsMap, group, groupArtifacts, artifactId); - } - } - if (group.getArtifacts() == null || group.getArtifacts().isEmpty()) { - log.debug( - "Migration1707ArtifactUuidFix fix groupInstance add to delete list: resource id {} name {} , group name {} ", - service.getUniqueId(), service.getName(), group.getName()); - groupsToDelete.add(group); - } - } - } - - private void clearGroupInstanceArtifacts(GroupInstance group) { - if(group.getArtifacts() != null) - group.getArtifacts().clear(); - else - group.setArtifacts(new ArrayList<>()); - if(group.getArtifactsUuid() != null) - group.getArtifactsUuid().clear(); - else{ - group.setArtifactsUuid(new ArrayList<>()); - } - if(group.getGroupInstanceArtifacts() != null) - group.getGroupInstanceArtifacts().clear(); - else{ - group.setGroupInstanceArtifacts(new ArrayList<>()); - } - if(group.getGroupInstanceArtifactsUuid() != null ) - group.getGroupInstanceArtifactsUuid().clear(); - else - group.setGroupInstanceArtifactsUuid(new ArrayList<>()); - } - - private void fixArtifactUndergroupInstances(Map artifactsMap, GroupInstance group, - List groupArtifacts, String artifactId) { - 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.parse(artifact.getArtifactType()); - String correctArtifactId = artifact.getUniqueId(); - String correctArtifactUUID = artifact.getArtifactUUID(); - if (artifactType != ArtifactTypeEnum.HEAT_ENV) { - boolean isAddToGroup = true; - if (groupArtifacts.size() == 1) { - if (artifactType == ArtifactTypeEnum.HEAT_ARTIFACT) { - isAddToGroup = false; - artifact.setArtifactType(ArtifactTypeEnum.OTHER.getType()); - } - } - if (isAddToGroup) { - log.debug( - MIGRATION1707_ARTIFACT_UUID_FIX, - group.getName(), correctArtifactId, correctArtifactUUID); - group.getArtifacts().add(correctArtifactId); - if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) { - group.getArtifactsUuid().add(correctArtifactUUID); - } - } - } else { - log.debug( - MIGRATION1707_ARTIFACT_UUID_FIX, - group.getName(), correctArtifactId, correctArtifactUUID); - Set tmpSet = new HashSet<>(group.getGroupInstanceArtifacts()); - tmpSet.add(correctArtifactId); - group.setGroupInstanceArtifacts(new ArrayList<>(tmpSet)); - if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) { - Set tmpSetUUID = new HashSet<>(group.getGroupInstanceArtifactsUuid()); - tmpSetUUID.add(correctArtifactUUID); - group.setGroupInstanceArtifactsUuid(new ArrayList<>(tmpSetUUID)); - } - } - } - } - - private boolean fixVf(List vfLst) { - for (Resource resource : vfLst) { - log.debug("Migration1707ArtifactUuidFix fix resource: id {}, name {} ", resource.getUniqueId(), - resource.getName()); - Map artifactsMap = resource.getDeploymentArtifacts(); - List groupsList = resource.getGroups(); - List groupsToDelete = new ArrayList<>(); - if (groupsList != null && artifactsMap != null) { - for (GroupDefinition group : groupsList) { - if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE) && group.getArtifacts() != null) { - fixVfGroup(resource, artifactsMap, group); - } - if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE) - && (group.getArtifacts() == null || group.getArtifacts().isEmpty())) { - log.debug( - "Migration1707ArtifactUuidFix add group to delete list fix resource: id {}, name {} ", - resource.getUniqueId(), resource.getName(), group.getName()); - groupsToDelete.add(group); - } - } - - if (!groupsToDelete.isEmpty()) { - groupsList.removeAll(groupsToDelete); - - } - } - - } - - return true; - } - - private void fixVfGroup(Resource resource, Map artifactsMap, GroupDefinition group) { - log.debug("Migration1707ArtifactUuidFix fix group: resource id {}, group name {} ", resource.getUniqueId(), - group.getName()); - Set groupArtifactsSet = new HashSet<>(group.getArtifacts()); - List groupArtifacts = new ArrayList<>(groupArtifactsSet); - group.getArtifacts().clear(); - group.getArtifactsUuid().clear(); - - for (String artifactId : groupArtifacts) { - fixArtifactUnderGroup(artifactsMap, group, groupArtifacts, artifactId); - } - } - - private void fixArtifactUnderGroup(Map artifactsMap, GroupDefinition group, - List groupArtifacts, String artifactId) { - - 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(); - boolean isAddToGroup = true; - if (groupArtifacts.size() == 1) { - ArtifactTypeEnum artifactType = ArtifactTypeEnum.parse(artifact.getArtifactType()); - if (artifactType == ArtifactTypeEnum.HEAT_ARTIFACT) { - isAddToGroup = false; - artifact.setArtifactType(ArtifactTypeEnum.OTHER.getType()); - } - } - if (isAddToGroup) { - log.debug( - MIGRATION1707_ARTIFACT_UUID_FIX, - group.getName(), correctArtifactId, correctArtifactUUID); - group.getArtifacts().add(correctArtifactId); - if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) { - group.getArtifactsUuid().add(correctArtifactUUID); - } - } - - } - } - - 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 - StringBuilder sb = new StringBuilder(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) { - log.error(e.getMessage()); - } - } - - private void writeModuleResultToFile(Writer writer, List components) { - try { - // "service name, service id, state, version - for (Component component : components) { - StringBuilder sb = new StringBuilder(component.getName()); - sb.append(",").append(component.getUniqueId()).append(",").append(component.getInvariantUUID()) - .append(",").append(component.getLifecycleState()).append(",").append(component.getVersion()); - - sb.append("\n"); - writer.write(sb.toString()); - } - } catch (IOException e) { - - log.error(e.getMessage()); - } - } - - public boolean doFixTosca(Map> nodeToFix, Map> vfToFix, - Map> serviceToFix) { - - Map hasProps = new EnumMap<>(GraphPropertyEnum.class); - hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); - hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); - - Map> vertices = getVerticesToValidate(VertexTypeEnum.NODE_TYPE, hasProps); - validateTosca(vertices, nodeToFix, "RESOURCE_TOSCA_ARTIFACTS");// - - hasProps.clear(); - hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); - hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF); - hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); - - vertices = getVerticesToValidate(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps); - validateTosca(vertices, vfToFix, "VF_TOSCA_ARTIFACTS"); - - hasProps.clear(); - hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name()); - hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); - - vertices = getVerticesToValidate(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps); - return validateTosca(vertices, serviceToFix, "SERVICE_TOSCA_ARTIFACTS"); - } - - public Map> getVerticesToValidate(VertexTypeEnum type, - Map hasProps) { - - Map> result = new HashMap<>(); - try { - - Either, JanusGraphOperationStatus> resultsEither = janusGraphDao - .getByCriteria(type, hasProps); - if (resultsEither.isRight()) { - log.error("getVerticesToValidate failed {} ",resultsEither.right().value()); - return result; - } - log.info("getVerticesToValidate: {} vertices to scan", resultsEither.left().value().size()); - List componentsList = resultsEither.left().value(); - componentsList.forEach(vertex -> { - String ivariantUuid = (String) vertex.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID); - if (!result.containsKey(ivariantUuid)) { - List compList = new ArrayList<>(); - result.put(ivariantUuid, compList); - } - List compList = result.get(ivariantUuid); - - ComponentParametersView filter = new ComponentParametersView(true); - filter.setIgnoreArtifacts(false); - - Either toscaElement = toscaOperationFacade - .getToscaElement(vertex.getUniqueId(), filter); - if (toscaElement.isRight()) { - log.error("getVerticesToValidate: failed to find element {} staus is {}", vertex.getUniqueId() - ,toscaElement.right().value()); - } else { - compList.add(toscaElement.left().value()); - } - janusGraphDao.commit(); - - }); - - } catch (Exception e) { - log.info(FAILED_TO_FETCH_VF_RESOURCES, e); - - } finally { - janusGraphDao.commit(); - - } - return result; - } - - public boolean validateTosca(Map> vertices, Map> compToFix, - String name) { - boolean result = true; - long time = System.currentTimeMillis(); - String fileName = name + "_" + time + ".csv"; - try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF8))) { - writer.write("name, UUID, invariantUUID, state, version\n"); - for (Map.Entry> entry : vertices.entrySet()) { - List compList = entry.getValue(); - Set artifactEsId = new HashSet<>(); - for (Component component : compList) { - Map toscaArtifacts = component.getToscaArtifacts(); - Optional op = toscaArtifacts.values().stream() - .filter(a -> artifactEsId.contains(a.getEsId()) && a.getEsId() != null).findAny(); - if (op.isPresent()) { - result = false; - writeModuleResultToFile(writer, compList); - writer.flush(); - break; - } else { - artifactEsId.addAll(toscaArtifacts.values().stream().map(ArtifactDefinition::getEsId) - .collect(Collectors.toList())); - } - } - if (!result) { - List compListfull = new ArrayList<>(); - for (Component c : compList) { - ComponentParametersView filter = new ComponentParametersView(true); - filter.setIgnoreComponentInstances(false); - filter.setIgnoreArtifacts(false); - filter.setIgnoreGroups(false); - - Either toscaElement = toscaOperationFacade - .getToscaElement(c.getUniqueId(), filter); - if (toscaElement.isRight()) { - log.debug("getVerticesToValidate: failed to find element" + c.getUniqueId() - + " status is" + toscaElement.right().value()); - } else { - compListfull.add(toscaElement.left().value()); - } - this.janusGraphDao.commit(); - } - - compToFix.put(entry.getKey(), compListfull); - result = true; - } - - } - - } catch (Exception e) { - log.info(FAILED_TO_FETCH_VF_RESOURCES, e); - return false; - } finally { - janusGraphDao.commit(); - } - return result; - } - - private Either generateToscaArtifact(Component parent) { - log.debug("tosca artifact generation"); - try { - Map toscaArtifacts = parent.getToscaArtifacts(); - - ArtifactDefinition toscaArtifact = null; - Optional op = toscaArtifacts.values().stream() - .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.TOSCA_TEMPLATE.getType())).findAny(); - - if (op.isPresent()) { - toscaArtifact = op.get(); - } - if (toscaArtifact != null) { - log.debug("Migration1707ArtifactUuidFix generateToscaPerComponent artifact name {} id {} esId {}", - toscaArtifact.getArtifactName(), toscaArtifact.getUniqueId(), toscaArtifact.getEsId()); - - Either exportComponent = toscaExportUtils.exportComponent(parent); - if (exportComponent.isRight()) { - log.debug("Failed export tosca yaml for component {} error {}", parent.getUniqueId(), - exportComponent.right().value()); - - return Either.right(exportComponent.right().value()); - } - log.debug("Tosca yaml exported for component {} ", parent.getUniqueId()); - - toscaArtifact.setPayload(exportComponent.left().value().getMainYaml()); - byte[] decodedPayload = toscaArtifact.getPayloadData(); - - String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(parent.getUniqueId(), - toscaArtifact.getArtifactLabel()); - toscaArtifact.setUniqueId(uniqueId); - toscaArtifact.setEsId(toscaArtifact.getUniqueId()); - - toscaArtifact.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(decodedPayload)); - DAOArtifactData artifactData = new DAOArtifactData(toscaArtifact.getEsId(), decodedPayload); - artifactCassandraDao.saveArtifact(artifactData); - - log.debug("Tosca yaml artifact esId {} ", toscaArtifact.getEsId()); - } - ArtifactDefinition csarArtifact = null; - op = toscaArtifacts.values().stream() - .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.TOSCA_CSAR.getType())).findAny(); - - if (op.isPresent()) { - csarArtifact = op.get(); - } - - if (csarArtifact != null) { - Either generated = csarUtils.createCsar(parent, true, true); - - if (generated.isRight()) { - log.debug("Failed to export tosca csar for component {} error {}", parent.getUniqueId(), - generated.right().value()); - - return Either.right(ToscaError.GENERAL_ERROR); - } - byte[] value = generated.left().value(); - csarArtifact.setPayload(value); - byte[] decodedPayload = csarArtifact.getPayloadData(); - - String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(parent.getUniqueId(), - csarArtifact.getArtifactLabel()); - csarArtifact.setUniqueId(uniqueId); - csarArtifact.setEsId(csarArtifact.getUniqueId()); - - csarArtifact.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(decodedPayload)); - DAOArtifactData artifactData = new DAOArtifactData(csarArtifact.getEsId(), decodedPayload); - artifactCassandraDao.saveArtifact(artifactData); - log.debug("Tosca csar artifact esId {} ", csarArtifact.getEsId()); - - } - - } catch (Exception ex) { - log.error("Failed to generate tosca atifact component id {} component name {} error {}", - parent.getUniqueId(), parent.getName(), ex.getMessage()); - - return Either.right(ToscaError.GENERAL_ERROR); - } - - return Either.left(parent); - } - - private ArtifactDefinition createVfModuleArtifact(ComponentInstance currVF, Service service) { - - ArtifactDefinition vfModuleArtifactDefinition = new ArtifactDefinition(); - - vfModuleArtifactDefinition.setDescription("Auto-generated VF Modules information artifact"); - vfModuleArtifactDefinition.setArtifactDisplayName("Vf Modules Metadata"); - vfModuleArtifactDefinition.setArtifactType(ArtifactTypeEnum.VF_MODULES_METADATA.getType()); - vfModuleArtifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT); - vfModuleArtifactDefinition.setArtifactLabel("vfModulesMetadata"); - vfModuleArtifactDefinition.setTimeout(0); - vfModuleArtifactDefinition.setArtifactName(currVF.getNormalizedName() + "_modules.json"); - - return vfModuleArtifactDefinition; - } - - - private void fillVfModuleInstHeatEnvPayload(Component parent, ComponentInstance instance, List groupsForCurrVF, - ArtifactDefinition vfModuleArtifact) { - log.debug("generate new vf module for component. name {}, id {}, Version {}", instance.getName(), instance.getUniqueId()); - - String uniqueId = UniqueIdBuilder.buildInstanceArtifactUniqueId(parent.getUniqueId(), instance.getUniqueId(), vfModuleArtifact.getArtifactLabel()); + private static final String MIGRATION1707_ARTIFACT_UUID_FIX = "Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} "; + + private static final String FAILED_TO_FETCH_VF_RESOURCES = "Failed to fetch vf resources "; + + private static Logger log = Logger.getLogger(ArtifactUuidFix.class.getName()); + private JanusGraphDao janusGraphDao; + private ToscaOperationFacade toscaOperationFacade; + private ToscaExportHandler toscaExportUtils; + private ArtifactCassandraDao artifactCassandraDao; + private CsarUtils csarUtils; + + @Autowired + public ArtifactUuidFix(JanusGraphDao janusGraphDao, + ToscaOperationFacade toscaOperationFacade, ToscaExportHandler toscaExportUtils, + ArtifactCassandraDao artifactCassandraDao, CsarUtils csarUtils) { + this.janusGraphDao = janusGraphDao; + this.toscaOperationFacade = toscaOperationFacade; + this.toscaExportUtils = toscaExportUtils; + this.artifactCassandraDao = artifactCassandraDao; + this.csarUtils = csarUtils; + } + + public boolean doFix(String fixComponent, String runMode) { + List vfLst = new ArrayList<>(); + List serviceList = new ArrayList<>(); + Map> nodeToFixTosca = new HashMap<>(); + Map> vfToFixTosca = new HashMap<>(); + Map> serviceToFixTosca = new HashMap<>(); + + long time = System.currentTimeMillis(); + + doFixTosca(nodeToFixTosca, vfToFixTosca, serviceToFixTosca); + + if ("vf_only".equals(fixComponent)) { + if (!fetchFaultVf(vfLst, time)) { + return false; + } + } else { + if (!fetchServices(fixComponent, serviceList, time)) { + return false; + } + } + if ("service_vf".equals(runMode) || "fix".equals(runMode)) { + log.info("Mode {}. Find problem VFs", runMode); + if (!fetchVf(serviceList, vfLst, time)) { + log.info("Mode {}. Find problem VFs finished with failure", runMode); + return false; + } + log.info("Mode {}. Find problem VFs finished with success", runMode); + } + if ("fix".equals(runMode) || "fix_only_services".equals(runMode)) { + log.info("Mode {}. Start fix", runMode); + if (!fix(vfLst, serviceList, nodeToFixTosca, vfToFixTosca, serviceToFixTosca)) { + log.info("Mode {}. Fix finished with failure", runMode); + return false; + } + log.info("Mode {}. Fix finished with success", runMode); + } + + return true; + } + + private boolean fetchFaultVf(List vfLst, long time) { + log.info("Find fault VF "); + String fileName = "fault_" + time + ".csv"; + try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF_8))) { + writer.write("vf name, vf id, state, version\n"); + + Map hasProps = new EnumMap<>(GraphPropertyEnum.class); + hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); + hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF.name()); + + Map hasNotProps = new EnumMap<>(GraphPropertyEnum.class); + hasNotProps.put(GraphPropertyEnum.IS_DELETED, true); + log.info("Try to fetch resources with properties {} and not {}", hasProps, hasNotProps); + + Either, JanusGraphOperationStatus> servicesByCriteria = janusGraphDao + .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll); + if (servicesByCriteria.isRight()) { + log.info("Failed to fetch resources {}", servicesByCriteria.right().value()); + return false; + } + List resources = servicesByCriteria.left().value(); + for (GraphVertex gv : resources) { + ComponentParametersView filter = new ComponentParametersView(true); + filter.setIgnoreComponentInstances(false); + filter.setIgnoreArtifacts(false); + filter.setIgnoreGroups(false); + + Either toscaElement = toscaOperationFacade + .getToscaElement(gv.getUniqueId()); + if (toscaElement.isRight()) { + log.info("Failed to fetch resources {} {}", gv.getUniqueId(), toscaElement.right().value()); + continue; + } + + Resource resource = toscaElement.left().value(); + String resourceName = resource.getName(); + Map deploymentArtifacts = resource.getDeploymentArtifacts(); + List 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) && isProblematicGroup(gr, resourceName, deploymentArtifacts)) { + isProblematic = true; + break; + } + } + if (isProblematic) { + vfLst.add(resource); + writeModuleResultToFile(writer, resource, null); + writer.flush(); + } + janusGraphDao.commit(); + } + + } catch (Exception e) { + log.info(FAILED_TO_FETCH_VF_RESOURCES, e); + return false; + } finally { + janusGraphDao.commit(); + } + return true; + } + + private boolean fetchVf(List serviceList, List vfLst, long time) { + log.info("Find problem VF "); + if (serviceList.isEmpty()) { + log.info("No services as input"); + return true; + } + String fileName = "problemVf_" + time + ".csv"; + try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF_8))) { + writer.write("vf name, vf id, state, version, example service name\n"); + Set vfIds = new HashSet<>(); + for (Service service : serviceList) { + List componentInstances = service.getComponentInstances().stream() + .filter(ci -> ci.getOriginType().equals(OriginTypeEnum.VF)).collect(Collectors.toList()); + for (ComponentInstance ci : componentInstances) { + if (!vfIds.contains(ci.getComponentUid())) { + vfIds.add(ci.getComponentUid()); + ComponentParametersView filter = new ComponentParametersView(true); + filter.setIgnoreComponentInstances(false); + filter.setIgnoreArtifacts(false); + filter.setIgnoreGroups(false); + Either toscaElement = toscaOperationFacade + .getToscaElement(ci.getComponentUid(), filter); + if (toscaElement.isRight()) { + log.info("Failed to fetch resource {} {}", ci.getComponentUid(), + toscaElement.right().value()); + continue; + } + Resource resource = toscaElement.left().value(); + if (resource.getResourceType().equals(ResourceTypeEnum.VF)) { + vfLst.add(resource); + writeModuleResultToFile(writer, resource, service); + writer.flush(); + + } + janusGraphDao.commit(); + } + } + } + log.info("output file with list of Vf : {}", fileName); + } catch (Exception e) { + log.info("Failed to fetch services ", e); + return false; + } finally { + janusGraphDao.commit(); + } + return true; + } + + private boolean fetchServices(String fixServices, List serviceList, long time) { + log.info("Find problem Services {}", fixServices); + String fileName = "problemService_" + time + ".csv"; + try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF_8))) { + writer.write("service name, service id, state, version\n"); + + Map hasProps = new EnumMap<>(GraphPropertyEnum.class); + hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name()); + if ("distributed_only".equals(fixServices)) { + hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); + hasProps.put(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTED.name()); + } + + Map hasNotProps = new EnumMap<>(GraphPropertyEnum.class); + hasNotProps.put(GraphPropertyEnum.IS_DELETED, true); + log.info("Try to fetch services with properties {} and not {}", hasProps, hasNotProps); + + Either, JanusGraphOperationStatus> servicesByCriteria = janusGraphDao + .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll); + if (servicesByCriteria.isRight()) { + log.info("Failed to fetch services {}", servicesByCriteria.right().value()); + return false; + } + List services = servicesByCriteria.left().value(); + for (GraphVertex gv : services) { + ComponentParametersView filter = new ComponentParametersView(true); + filter.setIgnoreComponentInstances(false); + filter.setIgnoreArtifacts(false); + filter.setIgnoreGroups(false); + + Either toscaElement = toscaOperationFacade + .getToscaElement(gv.getUniqueId(), filter); + if (toscaElement.isRight()) { + log.info("Failed to fetch service {} {}", gv.getUniqueId(), toscaElement.right().value()); + continue; + } + Service service = toscaElement.left().value(); + + String serviceName = (String) gv.getMetadataProperty(GraphPropertyEnum.NAME); + + boolean isProblematic = isProblematicService(service, serviceName); + if (isProblematic) { + serviceList.add(service); + writeModuleResultToFile(writer, service, null); + writer.flush(); + + } + + janusGraphDao.commit(); + } + log.info("output file with list of services : {}", fileName); + } catch (Exception e) { + log.info("Failed to fetch services ", e); + return false; + } finally { + janusGraphDao.commit(); + } + return true; + } + + private boolean isProblematicService(Service service, String serviceName) { + + List componentInstances = service.getComponentInstances(); + + if (componentInstances == null) { + log.info("No instances for service {} ", service.getUniqueId()); + return false; + } + boolean isCheckVFModules = true; + if (service.getLifecycleState() == LifecycleStateEnum.NOT_CERTIFIED_CHECKIN || + service.getLifecycleState() == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) { + isCheckVFModules = false; + } + for (ComponentInstance ci : componentInstances) { + Map deploymentArtifacts = ci.getDeploymentArtifacts(); + List groupInstances = ci.getGroupInstances(); + if (groupInstances == null || groupInstances.isEmpty()) { + log.info("No instance groups for instance {} in service {} id {} ", ci.getName(), serviceName, + service.getUniqueId()); + continue; + } + List vfModules = null; + if (isCheckVFModules) { + Optional optionalVfModuleArtifact = deploymentArtifacts.values().stream() + .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.VF_MODULES_METADATA.getType())).findAny(); + + if (!optionalVfModuleArtifact.isPresent()) { + return true; + } + + ArtifactDefinition vfModuleArtifact = optionalVfModuleArtifact.get(); + Either, StorageOperationStatus> vfModulesEither = parseVFModuleJson(vfModuleArtifact); + if (vfModulesEither.isRight()) { + log.error("Failed to parse vfModule for service {} status is {}", service.getUniqueId(), vfModulesEither.right().value()); + return true; + } + vfModules = vfModulesEither.left().value(); + if (vfModules == null || vfModules.isEmpty()) { + log.info("vfModules empty for service {}", service.getUniqueId()); + return true; + } + } + + for (GroupInstance gi : groupInstances) { + if (gi.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) { + VfModuleArtifactPayloadEx vfModule = null; + if (isCheckVFModules && vfModules != null) { + Optional op = vfModules.stream().filter(vf -> vf.getVfModuleModelName().equals(gi.getGroupName())) + .findAny(); + if (!op.isPresent()) { + log.error("Failed to find vfModule for group {}", gi.getGroupName()); + return true; + } + vfModule = op.get(); + } + if (isProblematicGroupInstance(gi, ci.getName(), serviceName, deploymentArtifacts, vfModule)) { + return true; + } + } + } + + } + return false; + } + + private boolean isProblematicGroup(GroupDefinition gr, String resourceName, + Map deploymentArtifacts) { + List artifacts = gr.getArtifacts(); + List artifactsUuid = gr.getArtifactsUuid(); + Set artifactsSet = new HashSet<>(); + + if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) { + log.info("No groups in resource {} ", resourceName); + return true; + } + artifactsSet.addAll(artifacts); + if (artifactsSet.size() < artifacts.size()) { + log.info(" artifactsSet.size() < artifacts.size() group {} in resource {} ", gr.getName(), resourceName); + return true; + } + + if ((artifactsUuid != null) && (artifacts.size() < artifactsUuid.size())) { + log.info(" artifacts.size() < artifactsUuid.size() group {} in resource {} ", gr.getName(), resourceName); + return true; + } + if (!artifacts.isEmpty() && (artifactsUuid == null || artifactsUuid.isEmpty())) { + log.info( + " artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() group {} in resource {} ", + gr.getName(), resourceName); + return true; + } + if (artifactsUuid != null && 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.parse(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 != null && !artifactsUuid.contains(artifactDefinition.getArtifactUUID())) { + log.info( + " artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} group {} in resource {} ", + artifactlabel, gr.getName(), resourceName); + return true; + } + } + } + for (String artifactUUID : artifactsUuid) { + String label = findArtifactLabelFromArtifactId(artifactUUID); + if (label != null && !label.isEmpty() && !label.equals("")) { + return true; + } + } + + return false; + } + + private boolean isProblematicGroupInstance(GroupInstance gi, String instName, String servicename, + Map deploymentArtifacts, VfModuleArtifactPayloadEx vfModule) { + List artifacts = gi.getArtifacts(); + List artifactsUuid = gi.getArtifactsUuid(); + List instArtifactsUuid = gi.getGroupInstanceArtifactsUuid(); + List instArtifactsId = gi.getGroupInstanceArtifacts(); + Set instArtifatIdSet = new HashSet<>(); + Set artifactsSet = new HashSet<>(); + + log.info("check group {} for instance {} ", gi.getGroupName(), instName); + if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) { + log.info("No instance groups for instance {} in service {} ", instName, servicename); + return true; + } + artifactsSet.addAll(artifacts); + if (artifactsSet.size() < artifacts.size()) { + log.info(" artifactsSet.size() < artifacts.size() group {} in resource {} ", instName, servicename); + return true; + } + + if (instArtifactsId != null && !instArtifactsId.isEmpty()) { + instArtifatIdSet.addAll(instArtifactsId); + } + + if ((artifactsUuid != null) && (artifacts.size() < artifactsUuid.size())) { + log.info(" artifacts.size() < artifactsUuid.size() inst {} in service {} ", instName, servicename); + return true; + } + if (!artifacts.isEmpty() && (artifactsUuid == null || artifactsUuid.isEmpty())) { + log.info( + " artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() inst {} in service {} ", + instName, servicename); + return true; + } + if (artifactsUuid != null && artifactsUuid.contains(null)) { + log.info(" artifactsUuid.contains(null) inst {} in service {} ", instName, servicename); + return true; + } + if (instArtifactsId != null && instArtifatIdSet.size() < instArtifactsId.size()) { + log.info(" instArtifatIdSet.size() < instArtifactsId.size() inst {} in service {} ", instName, servicename); + return true; + } + + if ((instArtifactsId != null && instArtifactsUuid != null) + && instArtifactsId.size() != instArtifactsUuid.size()) { + log.info(" instArtifactsId.size() != instArtifactsUuid.size() 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.parse(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 != null && !artifactsUuid.contains(artifactDefinition.getArtifactUUID())) { + log.info( + " artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ", + artifactlabel, instName, servicename); + return true; + } + } else { + if (instArtifactsUuid == null || instArtifactsUuid.isEmpty()) { + log.info(" instArtifactsUuid empty. label {} inst {} in service {} ", artifactlabel, instName, + servicename); + return true; + } + if (!instArtifactsUuid.contains(artifactDefinition.getArtifactUUID())) { + log.info( + " instArtifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ", + artifactlabel, instName, servicename); + return true; + } + } + } + for (String artifactUUID : artifactsUuid) { + String label = findArtifactLabelFromArtifactId(artifactUUID); + if (label != null && !label.isEmpty() && !"".equals(label)) { + return true; + } + } + if (vfModule != null && artifactsUuid != null) { + return isProblematicVFModule(vfModule, artifactsUuid, instArtifactsUuid); + } + + return false; + } + + private boolean isProblematicVFModule(VfModuleArtifactPayloadEx vfModule, List artifactsUuid, + List instArtifactsUuid) { + log.info(" isProblematicVFModule {} ", vfModule.getVfModuleModelName()); + List vfModuleArtifacts = vfModule.getArtifacts(); + List allArtifacts = new ArrayList<>(); + allArtifacts.addAll(artifactsUuid); + if (instArtifactsUuid != null) { + allArtifacts.addAll(instArtifactsUuid); + } + if ((vfModuleArtifacts == null || vfModuleArtifacts.isEmpty()) && !artifactsUuid.isEmpty()) { + log.error(" vfModuleArtifacts == null || vfModuleArtifacts.isEmpty()) && !artifactsUuid.isEmpty()"); + return true; + } + if (vfModuleArtifacts != null) { + if (vfModuleArtifacts.size() != allArtifacts.size()) { + log.error(" vfModuleArtifacts.size() != allArtifacts.size()"); + return true; + } + for (String vfModuleArtifact : vfModuleArtifacts) { + Optional op = allArtifacts.stream().filter(a -> a.equals(vfModuleArtifact)).findAny(); + if (!op.isPresent()) { + log.error("failed to find artifact {} in group artifacts {}", vfModuleArtifact, allArtifacts); + return true; + } + } + } + return false; + } + + + private boolean fix(List vfLst, List serviceList, Map> nodesToFixTosca, + Map> vfToFixTosca, Map> servicesToFixTosca) { + boolean res = true; + log.info(" Fix started ***** "); + if (vfLst != null && !vfLst.isEmpty()) { + res = fixVf(vfLst); + + } + + if (res && serviceList != null && !serviceList.isEmpty()) { + res = fixServices(serviceList); + + } + + Set fixedIds = new HashSet<>(); + + long time = System.currentTimeMillis(); + String fileName = "FailedGenerateTosca" + "_" + time + ".csv"; + + try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF_8))) { + writer.write("componentType, name, version, UID, UUID, invariantUUID, state\n"); + List failedList = new ArrayList<>(); + + if (res && nodesToFixTosca != null && !nodesToFixTosca.isEmpty()) { + + generateAndSaveToscaArtifacts(nodesToFixTosca, fixedIds, null, failedList); + + } + if (vfToFixTosca != null && !vfToFixTosca.isEmpty()) { + + generateAndSaveToscaArtifacts(vfToFixTosca, fixedIds, vfLst, failedList); + + } + + for (Component component : vfLst) { + res = generateToscaPerComponent(fixedIds, component); + if (res) { + TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(component); + Map groups = topologyTemplate.getGroups(); + res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.GROUPS, EdgeLabelEnum.GROUPS, groups); + if (res) { + Map arifacts = topologyTemplate.getDeploymentArtifacts(); + res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.DEPLOYMENT_ARTIFACTS, + EdgeLabelEnum.DEPLOYMENT_ARTIFACTS, arifacts); + } + if (res) { + Map arifacts = topologyTemplate.getToscaArtifacts(); + res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS, + EdgeLabelEnum.TOSCA_ARTIFACTS, arifacts); + } + janusGraphDao.commit(); + } else { + failedList.add(component); + } + } + + if (servicesToFixTosca != null && !servicesToFixTosca.isEmpty()) { + generateAndSaveToscaArtifacts(servicesToFixTosca, fixedIds, serviceList, failedList); + + } + + for (Component component : serviceList) { + res = generateToscaPerComponent(fixedIds, component); + if (res) { + TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(component); + Map groups = topologyTemplate.getInstGroups(); + res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS, + groups); + + if (res) { + Map artifacts = topologyTemplate + .getInstDeploymentArtifacts(); + res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, + EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, artifacts); + } + if (res) { + Map arifacts = topologyTemplate.getToscaArtifacts(); + res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS, + EdgeLabelEnum.TOSCA_ARTIFACTS, arifacts); + } + janusGraphDao.commit(); + } else { + failedList.add(component); + } + + } + if (!failedList.isEmpty()) { + for (Component component : failedList) { + StringBuilder sb = new StringBuilder(component.getComponentType().getValue()); + sb.append(",").append(component.getName()).append(",").append(component.getVersion()).append(",") + .append(component.getUniqueId()).append(",").append(component.getUUID()).append(",") + .append(component.getInvariantUUID()).append(",").append(component.getLifecycleState()); + + sb.append("\n"); + writer.write(sb.toString()); + } + writer.flush(); + } + } catch (IOException e) { + log.error(e.getMessage()); + } finally { + janusGraphDao.commit(); + } + log.info(" Fix finished with res {} ***** ", res); + return res; + } + + private boolean generateAndSaveToscaArtifacts(Map> nodesToFixTosca, Set fixedIds, + List componentsWithFailedGroups, List failedList) { + boolean res = true; + log.debug("Migration1707ArtifactUuidFix generateAndSaveToscaArtifacts started "); + for (Map.Entry> entry : nodesToFixTosca.entrySet()) { + + List component = entry.getValue(); + for (Component c : component) { + log.debug("Migration1707ArtifactUuidFix fix tosca on component : id {}, name {} ", c.getUniqueId(), + c.getName()); + if (componentsWithFailedGroups != null) { + Optional op = (Optional) componentsWithFailedGroups.stream() + .filter(cg -> cg.getUniqueId().equals(c.getUniqueId())).findAny(); + if (!op.isPresent()) { + res = generateToscaPerComponent(fixedIds, c); + } + } else { + res = generateToscaPerComponent(fixedIds, c); + } + if (res) { + ToscaElement topologyTemplate = ModelConverter.convertToToscaElement(c); + Map arifacts = topologyTemplate.getToscaArtifacts(); + res = fixDataOnGraph(c.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS, EdgeLabelEnum.TOSCA_ARTIFACTS, + arifacts); + janusGraphDao.commit(); + } else { + failedList.add(c); + } + + } + } + log.debug("Migration1707ArtifactUuidFix generateAndSaveToscaArtifacts finished with res {} ", res); + return res; + } + + private boolean generateToscaPerComponent(Set fixedIds, Component c) { + boolean res = true; + log.debug("Migration1707ArtifactUuidFix generateToscaPerComponent started component name {} id {}", + c.getName(), c.getUniqueId()); + try { + Either toscaElement = toscaOperationFacade + .getToscaFullElement(c.getUniqueId()); + if (toscaElement.isRight()) { + log.info("Failed to fetch resources {} {}", c.getUniqueId(), toscaElement.right().value()); + return false; + } + Component toscaElementFull = toscaElement.left().value(); + toscaElementFull.setGroups(c.getGroups()); + List ciListFull = toscaElementFull.getComponentInstances(); + List ciList = c.getComponentInstances(); + if (ciListFull != null && !ciListFull.isEmpty()) { + ciListFull.forEach(ciFull -> { + ComponentInstance compInst = ciList.stream() + .filter(ci -> ci.getUniqueId().equals(ciFull.getUniqueId())).findAny().get(); + ciFull.setGroupInstances(compInst.getGroupInstances()); + }); + } + + Either either = generateToscaArtifact(toscaElementFull); + + if (either.isRight()) { + log.error("Couldn't generate and save tosca template component unique id {}, name {} error: {}", + toscaElementFull.getUniqueId(), toscaElementFull.getName(), either.right().value()); + res = false; + + } + + if (res) { + c.setToscaArtifacts(either.left().value().getToscaArtifacts()); + fixedIds.add(toscaElementFull.getUniqueId()); + } + } finally { + if (res) { + janusGraphDao.commit(); + } else { + janusGraphDao.rollback(); + } + } + log.debug("Migration1707ArtifactUuidFix generateToscaPerComponent finished component name {} id {} res {}", + c.getName(), c.getUniqueId(), res); + return res; + } + + private boolean fixDataOnGraph(String componentId, VertexTypeEnum vertexTypeEnum, + EdgeLabelEnum edgeLabelEnum, Map groups) { + log.debug("amount groups to update: VertexTypeEnum {} EdgeLabelEnum {} data size {}", vertexTypeEnum.getName(), + edgeLabelEnum, groups.size()); + boolean res = true; + Either getResponse = janusGraphDao.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 groupVertexEither = janusGraphDao.getChildVertex(componentVertex, + edgeLabelEnum, JsonParseFlagEnum.ParseJson); + if (groupVertexEither.isRight() && groupVertexEither.right().value() == JanusGraphOperationStatus.NOT_FOUND) { + log.debug("no child {} vertex for component unique id {}, error: {}", edgeLabelEnum, componentId, + groupVertexEither.right().value()); + return true; + } + 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 updatevertexEither = janusGraphDao.updateVertex(toscaDataVertex); + if (updatevertexEither.isRight()) { + log.debug("failed to update vertex for component unique id {}, error: {}", componentId, + updatevertexEither.right().value()); + janusGraphDao.rollback(); + return false; + } + } + } + log.debug("Fix data on graph finished: VertexTypeEnum {} EdgeLabelEnum {} res {}", vertexTypeEnum.getName(), + res); + return res; + } + + private boolean fixServices(List serviceList) { + for (Service service : serviceList) { + log.debug("Migration1707ArtifactUuidFix fix service: id {}, name {} ", service.getUniqueId(), + service.getName()); + List instances = service.getComponentInstances(); + for (ComponentInstance instance : instances) { + fixComponentInstances(service, instance); + } + + } + return true; + + } + + private void fixComponentInstances(Service service, ComponentInstance instance) { + Map artifactsMap = instance.getDeploymentArtifacts(); + List groupsList = instance.getGroupInstances(); + if (groupsList != null && artifactsMap != null) { + List groupsToDelete = new ArrayList<>(); + for (GroupInstance group : groupsList) { + fixGroupInstances(service, artifactsMap, groupsToDelete, group); + + } + + if (!groupsToDelete.isEmpty()) { + log.debug("Migration1707ArtifactUuidFix delete group: resource id {}, group instance to delete {} ", + service.getUniqueId(), groupsToDelete); + groupsList.removeAll(groupsToDelete); + + } + + Optional optionalVfModuleArtifact = artifactsMap.values().stream() + .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.VF_MODULES_METADATA.getType())).findAny(); + ArtifactDefinition vfModuleArtifact; + if (!optionalVfModuleArtifact.isPresent()) { + vfModuleArtifact = createVfModuleArtifact(instance); + artifactsMap.put(vfModuleArtifact.getArtifactLabel(), vfModuleArtifact); + } else { + vfModuleArtifact = optionalVfModuleArtifact.get(); + } + fillVfModuleInstHeatEnvPayload(service, instance, groupsList, vfModuleArtifact); + } + } + + private void fixGroupInstances(Service service, Map artifactsMap, + List groupsToDelete, GroupInstance group) { + if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) { + log.debug("Migration1707ArtifactUuidFix fix group: resource id {}, group name {} ", service.getUniqueId(), + group.getName()); + if (group.getArtifacts() != null) { + Set groupArtifactsSet = new HashSet<>(group.getArtifacts()); + if (group.getGroupInstanceArtifacts() != null) { + List groupInsArtifacts = new ArrayList<>(group.getGroupInstanceArtifacts()); + groupArtifactsSet.addAll(groupInsArtifacts); + } + List groupArtifacts = new ArrayList<>(groupArtifactsSet); + + clearGroupInstanceArtifacts(group); + + for (String artifactId : groupArtifacts) { + fixArtifactUndergroupInstances(artifactsMap, group, groupArtifacts, artifactId); + } + } + if (group.getArtifacts() == null || group.getArtifacts().isEmpty()) { + log.debug( + "Migration1707ArtifactUuidFix fix groupInstance add to delete list: resource id {} name {} , group name {} ", + service.getUniqueId(), service.getName(), group.getName()); + groupsToDelete.add(group); + } + } + } + + private void clearGroupInstanceArtifacts(GroupInstance group) { + if (group.getArtifacts() != null) { + group.getArtifacts().clear(); + } else { + group.setArtifacts(new ArrayList<>()); + } + if (group.getArtifactsUuid() != null) { + group.getArtifactsUuid().clear(); + } else { + group.setArtifactsUuid(new ArrayList<>()); + } + if (group.getGroupInstanceArtifacts() != null) { + group.getGroupInstanceArtifacts().clear(); + } else { + group.setGroupInstanceArtifacts(new ArrayList<>()); + } + if (group.getGroupInstanceArtifactsUuid() != null) { + group.getGroupInstanceArtifactsUuid().clear(); + } else { + group.setGroupInstanceArtifactsUuid(new ArrayList<>()); + } + } + + private void fixArtifactUndergroupInstances(Map artifactsMap, GroupInstance group, + List groupArtifacts, String artifactId) { + 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.parse(artifact.getArtifactType()); + String correctArtifactId = artifact.getUniqueId(); + String correctArtifactUUID = artifact.getArtifactUUID(); + if (artifactType != ArtifactTypeEnum.HEAT_ENV) { + boolean isAddToGroup = true; + if (groupArtifacts.size() == 1 && artifactType == ArtifactTypeEnum.HEAT_ARTIFACT) { + isAddToGroup = false; + artifact.setArtifactType(ArtifactTypeEnum.OTHER.getType()); + } + if (isAddToGroup) { + log.debug( + MIGRATION1707_ARTIFACT_UUID_FIX, + group.getName(), correctArtifactId, correctArtifactUUID); + group.getArtifacts().add(correctArtifactId); + if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) { + group.getArtifactsUuid().add(correctArtifactUUID); + } + } + } else { + log.debug( + MIGRATION1707_ARTIFACT_UUID_FIX, + group.getName(), correctArtifactId, correctArtifactUUID); + Set tmpSet = new HashSet<>(group.getGroupInstanceArtifacts()); + tmpSet.add(correctArtifactId); + group.setGroupInstanceArtifacts(new ArrayList<>(tmpSet)); + if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) { + Set tmpSetUUID = new HashSet<>(group.getGroupInstanceArtifactsUuid()); + tmpSetUUID.add(correctArtifactUUID); + group.setGroupInstanceArtifactsUuid(new ArrayList<>(tmpSetUUID)); + } + } + } + } + + private boolean fixVf(List vfLst) { + for (Resource resource : vfLst) { + log.debug("Migration1707ArtifactUuidFix fix resource: id {}, name {} ", resource.getUniqueId(), + resource.getName()); + Map artifactsMap = resource.getDeploymentArtifacts(); + List groupsList = resource.getGroups(); + List groupsToDelete = new ArrayList<>(); + if (groupsList != null && artifactsMap != null) { + for (GroupDefinition group : groupsList) { + if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE) && group.getArtifacts() != null) { + fixVfGroup(resource, artifactsMap, group); + } + if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE) + && (group.getArtifacts() == null || group.getArtifacts().isEmpty())) { + log.debug( + "Migration1707ArtifactUuidFix add group to delete list fix resource: id {}, name {} ", + resource.getUniqueId(), resource.getName(), group.getName()); + groupsToDelete.add(group); + } + } + + if (!groupsToDelete.isEmpty()) { + groupsList.removeAll(groupsToDelete); + + } + } + + } + + return true; + } + + private void fixVfGroup(Resource resource, Map artifactsMap, GroupDefinition group) { + log.debug("Migration1707ArtifactUuidFix fix group: resource id {}, group name {} ", resource.getUniqueId(), + group.getName()); + Set groupArtifactsSet = new HashSet<>(group.getArtifacts()); + List groupArtifacts = new ArrayList<>(groupArtifactsSet); + group.getArtifacts().clear(); + group.getArtifactsUuid().clear(); + + for (String artifactId : groupArtifacts) { + fixArtifactUnderGroup(artifactsMap, group, groupArtifacts, artifactId); + } + } + + private void fixArtifactUnderGroup(Map artifactsMap, GroupDefinition group, + List groupArtifacts, String artifactId) { + + 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(); + boolean isAddToGroup = true; + if (groupArtifacts.size() == 1) { + ArtifactTypeEnum artifactType = ArtifactTypeEnum.parse(artifact.getArtifactType()); + if (artifactType == ArtifactTypeEnum.HEAT_ARTIFACT) { + isAddToGroup = false; + artifact.setArtifactType(ArtifactTypeEnum.OTHER.getType()); + } + } + if (isAddToGroup) { + log.debug( + MIGRATION1707_ARTIFACT_UUID_FIX, + group.getName(), correctArtifactId, correctArtifactUUID); + group.getArtifacts().add(correctArtifactId); + if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) { + group.getArtifactsUuid().add(correctArtifactUUID); + } + } + + } + } + + 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 + StringBuilder sb = new StringBuilder(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) { + log.error(e.getMessage()); + } + } + + private void writeModuleResultToFile(Writer writer, List components) { + try { + // "service name, service id, state, version + for (Component component : components) { + StringBuilder sb = new StringBuilder(component.getName()); + sb.append(",").append(component.getUniqueId()).append(",").append(component.getInvariantUUID()) + .append(",").append(component.getLifecycleState()).append(",").append(component.getVersion()); + + sb.append("\n"); + writer.write(sb.toString()); + } + } catch (IOException e) { + + log.error(e.getMessage()); + } + } + + public boolean doFixTosca(Map> nodeToFix, Map> vfToFix, + Map> serviceToFix) { + + Map hasProps = new EnumMap<>(GraphPropertyEnum.class); + hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); + hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); + + Map> vertices = getVerticesToValidate(VertexTypeEnum.NODE_TYPE, hasProps); + validateTosca(vertices, nodeToFix, "RESOURCE_TOSCA_ARTIFACTS");// + + hasProps.clear(); + hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); + hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF); + hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); + + vertices = getVerticesToValidate(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps); + validateTosca(vertices, vfToFix, "VF_TOSCA_ARTIFACTS"); + + hasProps.clear(); + hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name()); + hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); + + vertices = getVerticesToValidate(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps); + return validateTosca(vertices, serviceToFix, "SERVICE_TOSCA_ARTIFACTS"); + } + + public Map> getVerticesToValidate(VertexTypeEnum type, + Map hasProps) { + + Map> result = new HashMap<>(); + try { + + Either, JanusGraphOperationStatus> resultsEither = janusGraphDao + .getByCriteria(type, hasProps); + if (resultsEither.isRight()) { + log.error("getVerticesToValidate failed {} ", resultsEither.right().value()); + return result; + } + log.info("getVerticesToValidate: {} vertices to scan", resultsEither.left().value().size()); + List componentsList = resultsEither.left().value(); + componentsList.forEach(vertex -> { + String ivariantUuid = (String) vertex.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID); + if (!result.containsKey(ivariantUuid)) { + List compList = new ArrayList<>(); + result.put(ivariantUuid, compList); + } + List compList = result.get(ivariantUuid); + + ComponentParametersView filter = new ComponentParametersView(true); + filter.setIgnoreArtifacts(false); + + Either toscaElement = toscaOperationFacade + .getToscaElement(vertex.getUniqueId(), filter); + if (toscaElement.isRight()) { + log.error("getVerticesToValidate: failed to find element {} staus is {}", vertex.getUniqueId() + , toscaElement.right().value()); + } else { + compList.add(toscaElement.left().value()); + } + janusGraphDao.commit(); + + }); + + } catch (Exception e) { + log.info(FAILED_TO_FETCH_VF_RESOURCES, e); + + } finally { + janusGraphDao.commit(); + + } + return result; + } + + public boolean validateTosca(Map> vertices, Map> compToFix, + String name) { + boolean result = true; + long time = System.currentTimeMillis(); + String fileName = name + "_" + time + ".csv"; + try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF_8))) { + writer.write("name, UUID, invariantUUID, state, version\n"); + for (Map.Entry> entry : vertices.entrySet()) { + List compList = entry.getValue(); + Set artifactEsId = new HashSet<>(); + for (Component component : compList) { + Map toscaArtifacts = component.getToscaArtifacts(); + Optional op = toscaArtifacts.values().stream() + .filter(a -> artifactEsId.contains(a.getEsId()) && a.getEsId() != null).findAny(); + if (op.isPresent()) { + result = false; + writeModuleResultToFile(writer, compList); + writer.flush(); + break; + } else { + artifactEsId.addAll(toscaArtifacts.values().stream().map(ArtifactDefinition::getEsId) + .collect(Collectors.toList())); + } + } + if (!result) { + List compListfull = new ArrayList<>(); + for (Component c : compList) { + ComponentParametersView filter = new ComponentParametersView(true); + filter.setIgnoreComponentInstances(false); + filter.setIgnoreArtifacts(false); + filter.setIgnoreGroups(false); + + Either toscaElement = toscaOperationFacade + .getToscaElement(c.getUniqueId(), filter); + if (toscaElement.isRight()) { + log.debug("getVerticesToValidate: failed to find element {} status is {}", c.getUniqueId(), toscaElement.right().value()); + } else { + compListfull.add(toscaElement.left().value()); + } + this.janusGraphDao.commit(); + } + + compToFix.put(entry.getKey(), compListfull); + result = true; + } + + } + + } catch (Exception e) { + log.info(FAILED_TO_FETCH_VF_RESOURCES, e); + return false; + } finally { + janusGraphDao.commit(); + } + return result; + } + + private Either generateToscaArtifact(Component parent) { + log.debug("tosca artifact generation"); + try { + Map toscaArtifacts = parent.getToscaArtifacts(); + + ArtifactDefinition toscaArtifact = null; + Optional op = toscaArtifacts.values().stream() + .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.TOSCA_TEMPLATE.getType())).findAny(); + + if (op.isPresent()) { + toscaArtifact = op.get(); + } + if (toscaArtifact != null) { + log.debug("Migration1707ArtifactUuidFix generateToscaPerComponent artifact name {} id {} esId {}", + toscaArtifact.getArtifactName(), toscaArtifact.getUniqueId(), toscaArtifact.getEsId()); + + Either exportComponent = toscaExportUtils.exportComponent(parent); + if (exportComponent.isRight()) { + log.debug("Failed export tosca yaml for component {} error {}", parent.getUniqueId(), + exportComponent.right().value()); + + return Either.right(exportComponent.right().value()); + } + log.debug("Tosca yaml exported for component {} ", parent.getUniqueId()); + + toscaArtifact.setPayload(exportComponent.left().value().getMainYaml()); + byte[] decodedPayload = toscaArtifact.getPayloadData(); + + String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(parent.getUniqueId(), + toscaArtifact.getArtifactLabel()); + toscaArtifact.setUniqueId(uniqueId); + toscaArtifact.setEsId(toscaArtifact.getUniqueId()); + + toscaArtifact.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(decodedPayload)); + DAOArtifactData artifactData = new DAOArtifactData(toscaArtifact.getEsId(), decodedPayload); + artifactCassandraDao.saveArtifact(artifactData); + + log.debug("Tosca yaml artifact esId {} ", toscaArtifact.getEsId()); + } + ArtifactDefinition csarArtifact = null; + op = toscaArtifacts.values().stream() + .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.TOSCA_CSAR.getType())).findAny(); + + if (op.isPresent()) { + csarArtifact = op.get(); + } + + if (csarArtifact != null) { + Either generated = csarUtils.createCsar(parent, true, true); + + if (generated.isRight()) { + log.debug("Failed to export tosca csar for component {} error {}", parent.getUniqueId(), + generated.right().value()); + + return Either.right(ToscaError.GENERAL_ERROR); + } + byte[] value = generated.left().value(); + csarArtifact.setPayload(value); + byte[] decodedPayload = csarArtifact.getPayloadData(); + + String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(parent.getUniqueId(), + csarArtifact.getArtifactLabel()); + csarArtifact.setUniqueId(uniqueId); + csarArtifact.setEsId(csarArtifact.getUniqueId()); + + csarArtifact.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(decodedPayload)); + DAOArtifactData artifactData = new DAOArtifactData(csarArtifact.getEsId(), decodedPayload); + artifactCassandraDao.saveArtifact(artifactData); + log.debug("Tosca csar artifact esId {} ", csarArtifact.getEsId()); + + } + + } catch (Exception ex) { + log.error("Failed to generate tosca atifact component id {} component name {} error {}", + parent.getUniqueId(), parent.getName(), ex.getMessage()); + + return Either.right(ToscaError.GENERAL_ERROR); + } + + return Either.left(parent); + } + + private ArtifactDefinition createVfModuleArtifact(ComponentInstance currVF) { + + ArtifactDefinition vfModuleArtifactDefinition = new ArtifactDefinition(); + + vfModuleArtifactDefinition.setDescription("Auto-generated VF Modules information artifact"); + vfModuleArtifactDefinition.setArtifactDisplayName("Vf Modules Metadata"); + vfModuleArtifactDefinition.setArtifactType(ArtifactTypeEnum.VF_MODULES_METADATA.getType()); + vfModuleArtifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT); + vfModuleArtifactDefinition.setArtifactLabel("vfModulesMetadata"); + vfModuleArtifactDefinition.setTimeout(0); + vfModuleArtifactDefinition.setArtifactName(currVF.getNormalizedName() + "_modules.json"); + + return vfModuleArtifactDefinition; + } + + + private void fillVfModuleInstHeatEnvPayload(Component parent, ComponentInstance instance, List groupsForCurrVF, + ArtifactDefinition vfModuleArtifact) { + log.debug("generate new vf module for component. name {}, id {}, Version {}", instance.getName(), instance.getUniqueId()); + + String uniqueId = UniqueIdBuilder + .buildInstanceArtifactUniqueId(parent.getUniqueId(), instance.getUniqueId(), vfModuleArtifact.getArtifactLabel()); - vfModuleArtifact.setUniqueId(uniqueId); - vfModuleArtifact.setEsId(vfModuleArtifact.getUniqueId()); - - List vfModulePayloadForCurrVF = new ArrayList<>(); - if (groupsForCurrVF != null) { - for (GroupInstance groupInstance : groupsForCurrVF) { - VfModuleArtifactPayload modulePayload = new VfModuleArtifactPayload(groupInstance); - vfModulePayloadForCurrVF.add(modulePayload); - } - Collections.sort(vfModulePayloadForCurrVF, - (art1, art2) -> VfModuleArtifactPayload.compareByGroupName(art1, art2)); + vfModuleArtifact.setUniqueId(uniqueId); + vfModuleArtifact.setEsId(vfModuleArtifact.getUniqueId()); + + List vfModulePayloadForCurrVF = new ArrayList<>(); + if (groupsForCurrVF != null) { + for (GroupInstance groupInstance : groupsForCurrVF) { + VfModuleArtifactPayload modulePayload = new VfModuleArtifactPayload(groupInstance); + vfModulePayloadForCurrVF.add(modulePayload); + } + Collections.sort(vfModulePayloadForCurrVF, + (art1, art2) -> VfModuleArtifactPayload.compareByGroupName(art1, art2)); - final Gson gson = new GsonBuilder().setPrettyPrinting().create(); + final Gson gson = new GsonBuilder().setPrettyPrinting().create(); - String vfModulePayloadString = gson.toJson(vfModulePayloadForCurrVF); - log.debug("vfModulePayloadString {}", vfModulePayloadString); - if (vfModulePayloadString != null) { - String newCheckSum = GeneralUtility - .calculateMD5Base64EncodedByByteArray(vfModulePayloadString.getBytes()); - vfModuleArtifact.setArtifactChecksum(newCheckSum); + String vfModulePayloadString = gson.toJson(vfModulePayloadForCurrVF); + log.debug("vfModulePayloadString {}", vfModulePayloadString); + if (vfModulePayloadString != null) { + String newCheckSum = GeneralUtility + .calculateMD5Base64EncodedByByteArray(vfModulePayloadString.getBytes()); + vfModuleArtifact.setArtifactChecksum(newCheckSum); - DAOArtifactData artifactData = new DAOArtifactData(vfModuleArtifact.getEsId(), - vfModulePayloadString.getBytes()); - artifactCassandraDao.saveArtifact(artifactData); + DAOArtifactData artifactData = new DAOArtifactData(vfModuleArtifact.getEsId(), + vfModulePayloadString.getBytes()); + artifactCassandraDao.saveArtifact(artifactData); - } + } - } + } - } + } - private Either, StorageOperationStatus> parseVFModuleJson(ArtifactDefinition vfModuleArtifact) { - log.info("Try to get vfModule json from cassandra {}", vfModuleArtifact.getEsId()); - Either vfModuleData = artifactCassandraDao.getArtifact(vfModuleArtifact.getEsId()); + private Either, StorageOperationStatus> parseVFModuleJson(ArtifactDefinition vfModuleArtifact) { + log.info("Try to get vfModule json from cassandra {}", vfModuleArtifact.getEsId()); + Either vfModuleData = artifactCassandraDao.getArtifact(vfModuleArtifact.getEsId()); - if (vfModuleData.isRight()) { - CassandraOperationStatus resourceUploadStatus = vfModuleData.right().value(); - StorageOperationStatus storageResponse = DaoStatusConverter.convertCassandraStatusToStorageStatus(resourceUploadStatus); - log.error("failed to fetch vfModule json {} from cassandra. Status is {}", vfModuleArtifact.getEsId(), storageResponse); - return Either.right(storageResponse); + if (vfModuleData.isRight()) { + CassandraOperationStatus resourceUploadStatus = vfModuleData.right().value(); + StorageOperationStatus storageResponse = DaoStatusConverter.convertCassandraStatusToStorageStatus(resourceUploadStatus); + log.error("failed to fetch vfModule json {} from cassandra. Status is {}", vfModuleArtifact.getEsId(), storageResponse); + return Either.right(storageResponse); - } + } - DAOArtifactData DAOArtifactData = vfModuleData.left().value(); - String gsonData = new String( DAOArtifactData.getDataAsArray()); - final Gson gson = new GsonBuilder().setPrettyPrinting().create(); - JsonArray jsonElement = new JsonArray(); - jsonElement = gson.fromJson(gsonData, jsonElement.getClass()); - List vfModules = new ArrayList<>(); - jsonElement.forEach(je ->{ - VfModuleArtifactPayloadEx vfModule = ComponentsUtils.parseJsonToObject(je.toString(), VfModuleArtifactPayloadEx.class); - vfModules.add(vfModule); - }); + DAOArtifactData daoArtifactData = vfModuleData.left().value(); + String gsonData = new String(daoArtifactData.getDataAsArray()); + final Gson gson = new GsonBuilder().setPrettyPrinting().create(); + JsonArray jsonElement = new JsonArray(); + jsonElement = gson.fromJson(gsonData, jsonElement.getClass()); + List vfModules = new ArrayList<>(); + jsonElement.forEach(je -> { + VfModuleArtifactPayloadEx vfModule = ComponentsUtils.parseJsonToObject(je.toString(), VfModuleArtifactPayloadEx.class); + vfModules.add(vfModule); + }); - log.debug ("parse vf module finish {}", gsonData); - return Either.left(vfModules); + log.debug("parse vf module finish {}", gsonData); + return Either.left(vfModules); - } + } } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/internal/tool/CsarGenerator.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/internal/tool/CsarGenerator.java index 6c5ffb7be6..751801f4f2 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/internal/tool/CsarGenerator.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/internal/tool/CsarGenerator.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -19,6 +19,15 @@ */ package org.openecomp.sdc.asdctool.impl.internal.tool; +import java.io.IOException; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Scanner; +import java.util.function.Supplier; +import java.util.stream.Collectors; import org.openecomp.sdc.asdctool.utils.ConsoleWriter; import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; @@ -40,20 +49,9 @@ import org.openecomp.sdc.be.tosca.CsarUtils; import org.openecomp.sdc.be.tosca.ToscaExportHandler; import org.openecomp.sdc.be.tosca.ToscaRepresentation; import org.openecomp.sdc.common.api.ArtifactTypeEnum; -import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.common.util.GeneralUtility; import org.springframework.beans.factory.annotation.Autowired; -import java.io.IOException; -import java.util.EnumMap; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Scanner; -import java.util.function.Supplier; -import java.util.stream.Collectors; - @org.springframework.stereotype.Component("csarGenerator") public class CsarGenerator extends CommonInternalTool { @@ -65,8 +63,8 @@ public class CsarGenerator extends CommonInternalTool { @Autowired public CsarGenerator(JanusGraphDao janusGraphDao, CsarUtils csarUtils, - ToscaOperationFacade toscaOperationFacade, - ArtifactCassandraDao artifactCassandraDao, ToscaExportHandler toscaExportHandler) { + ToscaOperationFacade toscaOperationFacade, + ArtifactCassandraDao artifactCassandraDao, ToscaExportHandler toscaExportHandler) { super("generate"); this.janusGraphDao = janusGraphDao; this.csarUtils = csarUtils; @@ -75,8 +73,6 @@ public class CsarGenerator extends CommonInternalTool { this.toscaExportHandler = toscaExportHandler; } - private static Logger log = Logger.getLogger(CsarGenerator.class.getName()); - public void generateCsar(String uuid, Scanner scanner) { JanusGraphOperationStatus status = JanusGraphOperationStatus.OK; @@ -98,7 +94,7 @@ public class CsarGenerator extends CommonInternalTool { ConsoleWriter.dataLine("\nGenerate CSAR (yes/no)?"); String input = scanner.nextLine(); if (input.equalsIgnoreCase("yes")) { - + status = handleService(metadataV, uuid); } } @@ -119,47 +115,51 @@ public class CsarGenerator extends CommonInternalTool { Supplier supplier = () -> generateToscaPayload(component); generateArtifact(component, ArtifactTypeEnum.TOSCA_TEMPLATE, supplier); - + supplier = () -> generateCsarPayload(component); generateArtifact(component, ArtifactTypeEnum.TOSCA_CSAR, supplier); - + GraphVertex toscaArtifactV = janusGraphDao - .getChildVertex(metadataV, EdgeLabelEnum.TOSCA_ARTIFACTS, JsonParseFlagEnum.ParseJson).either(l->l, r->null); - if ( toscaArtifactV != null ){ - Map copy = component.getToscaArtifacts().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue()))); + .getChildVertex(metadataV, EdgeLabelEnum.TOSCA_ARTIFACTS, JsonParseFlagEnum.ParseJson).either(l -> l, r -> null); + if (toscaArtifactV != null) { + Map copy = component.getToscaArtifacts().entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue()))); toscaArtifactV.setJson(copy); janusGraphDao.updateVertex(toscaArtifactV); } - + } else { ConsoleWriter.dataLine("Failed to fetch certified service with UUID ", uuid); } return status; } - private JanusGraphOperationStatus generateArtifact(Component component, ArtifactTypeEnum artifactType, Supplier supplier){ + private JanusGraphOperationStatus generateArtifact(Component component, ArtifactTypeEnum artifactType, Supplier supplier) { JanusGraphOperationStatus status = JanusGraphOperationStatus.GENERAL_ERROR; ArtifactDefinition csarArtifact; - Optional op = component.getToscaArtifacts().values().stream().filter(p -> p.getArtifactType().equals(artifactType.getType())).findAny(); + Optional op = component.getToscaArtifacts().values().stream() + .filter(p -> p.getArtifactType().equals(artifactType.getType())).findAny(); if (op.isPresent()) { csarArtifact = op.get(); - + status = savePayload(component, csarArtifact, supplier); } return status; } - + private byte[] generateCsarPayload(org.openecomp.sdc.be.model.Component component) { - return csarUtils.createCsar(component, true, true).either( l -> l, r -> null); + return csarUtils.createCsar(component, true, true).either(l -> l, r -> null); } - private byte[] generateToscaPayload(Component component){ - return toscaExportHandler.exportComponent(component).either(ToscaRepresentation::getMainYaml, r -> null); + + private byte[] generateToscaPayload(Component component) { + return toscaExportHandler.exportComponent(component).either(ToscaRepresentation::getMainYaml, r -> null); } - private JanusGraphOperationStatus savePayload(org.openecomp.sdc.be.model.Component component, ArtifactDefinition csarArtifact, Supplier supplier) { + private JanusGraphOperationStatus savePayload(org.openecomp.sdc.be.model.Component component, ArtifactDefinition csarArtifact, + Supplier supplier) { byte[] payload = supplier.get(); - if ( payload == null ) { + if (payload == null) { ConsoleWriter.dataLine("create artifact failed ", csarArtifact.getArtifactLabel()); return JanusGraphOperationStatus.GENERAL_ERROR; } @@ -170,10 +170,9 @@ public class CsarGenerator extends CommonInternalTool { String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(component.getUniqueId(), csarArtifact.getArtifactLabel()); csarArtifact.setUniqueId(uniqueId); csarArtifact.setEsId(csarArtifact.getUniqueId()); - + ConsoleWriter.dataLine("create artifact unique id ", uniqueId); - - + csarArtifact.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(decodedPayload)); DAOArtifactData artifactData = new DAOArtifactData(csarArtifact.getEsId(), decodedPayload); artifactCassandraDao.saveArtifact(artifactData); 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 deleted file mode 100644 index 2dcc0ae4db..0000000000 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/utils/ValidationTaskResult.java +++ /dev/null @@ -1,64 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2019 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.validator.utils; - -import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; - -/** - * 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/DeleteComponentTool.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/DeleteComponentTool.java index 309d23ca14..77b21fdf0c 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/DeleteComponentTool.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/DeleteComponentTool.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -20,16 +20,14 @@ package org.openecomp.sdc.asdctool.main; +import java.util.Scanner; import org.openecomp.sdc.asdctool.configuration.ConfigurationUploader; import org.openecomp.sdc.asdctool.configuration.InternalToolConfiguration; import org.openecomp.sdc.asdctool.impl.internal.tool.DeleteComponentHandler; import org.openecomp.sdc.asdctool.utils.ConsoleWriter; import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import java.util.Scanner; - -public class DeleteComponentTool extends SdcInternalTool{ - private static final String PSW = "ItIsTimeToDelete"; +public class DeleteComponentTool extends SdcInternalTool { public static void main(String[] args) { if (args == null || args.length < 2) { @@ -37,8 +35,7 @@ public class DeleteComponentTool extends SdcInternalTool{ System.exit(1); } String appConfigDir = args[0]; - String password = args[1]; - + disableConsole(); ConsoleWriter.dataLine("STARTED... "); @@ -46,7 +43,6 @@ public class DeleteComponentTool extends SdcInternalTool{ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(InternalToolConfiguration.class); DeleteComponentHandler deleteComponentHandler = context.getBean(DeleteComponentHandler.class); - String input = ""; Scanner scanner = new Scanner(System.in); do { @@ -54,9 +50,9 @@ public class DeleteComponentTool extends SdcInternalTool{ input = scanner.nextLine(); if (!input.equals("exit")) { if (!input.isEmpty()) { - ConsoleWriter.dataLine("Your id is " ,input); + ConsoleWriter.dataLine("Your id is ", input); deleteComponentHandler.deleteComponent(input, scanner); - }else{ + } else { ConsoleWriter.dataLine("Your id is empty. Try again."); } } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ValidationTool.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ValidationTool.java index e5cd6f8c14..e68c9dd5af 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ValidationTool.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ValidationTool.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -32,7 +32,6 @@ import org.openecomp.sdc.asdctool.impl.validator.report.ReportFile.CSVFile; import org.openecomp.sdc.asdctool.impl.validator.report.ReportFile.TXTFile; import org.openecomp.sdc.asdctool.impl.validator.report.ReportFileWriter; import org.openecomp.sdc.be.config.ConfigurationManager; -import org.openecomp.sdc.common.api.ConfigurationSource; import org.openecomp.sdc.common.impl.ExternalConfiguration; import org.openecomp.sdc.common.impl.FSConfigurationSource; import org.openecomp.sdc.common.log.wrappers.Logger; @@ -76,8 +75,7 @@ public class ValidationTool { } private static AnnotationConfigApplicationContext initContext(String appConfigDir) { - ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir); - ConfigurationManager configurationManager = new ConfigurationManager(configurationSource); + new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir)); return new AnnotationConfigApplicationContext(ValidationToolConfiguration.class); } } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/InstanceMigrationBase.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/InstanceMigrationBase.java deleted file mode 100644 index 5876898d7e..0000000000 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/InstanceMigrationBase.java +++ /dev/null @@ -1,162 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2020 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.migration.tasks; - -import fj.data.Either; -import org.apache.tinkerpop.gremlin.structure.Direction; -import org.apache.tinkerpop.gremlin.structure.Edge; -import org.janusgraph.core.JanusGraphVertex; -import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; -import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; -import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao; -import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum; -import org.openecomp.sdc.be.dao.jsongraph.types.EdgePropertyEnum; -import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; -import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; -import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; -import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; -import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; -import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.EnumMap; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -public abstract class InstanceMigrationBase { - - private static final Logger log = LoggerFactory.getLogger(InstanceMigrationBase.class); - protected JanusGraphDao janusGraphDao; - - public InstanceMigrationBase(JanusGraphDao janusGraphDao) { - this.janusGraphDao = janusGraphDao; - } - - protected StorageOperationStatus upgradeTopologyTemplates() { - Map hasNotProps = new EnumMap<>(GraphPropertyEnum.class); - hasNotProps.put(GraphPropertyEnum.IS_DELETED, true); - hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.CVFC); - - return janusGraphDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, null, hasNotProps, JsonParseFlagEnum.ParseAll) - .either(this::proceed, this::handleError); - } - - protected abstract StorageOperationStatus handleOneContainer(GraphVertex containerV); - - protected StorageOperationStatus proceed(List containersV) { - int failureCounter = 0; - log.info("found {} vertices to migrate ", containersV.size()); - for (GraphVertex container : containersV) { - StorageOperationStatus storageOperationStatus = handleOneContainer(container); - if (storageOperationStatus != StorageOperationStatus.OK) { - failureCounter++; - } - } - - if (failureCounter > 0) { - log.info("Failed to update {} vertices", failureCounter); - } else { - log.info("All vertices were successfully updated"); - } - - return StorageOperationStatus.OK; - } - - protected GraphVertex getVertexById(String vertexId) { - Either vertexById = janusGraphDao.getVertexById(vertexId); - if (vertexById.isRight()) { - log.info("Exception occurred while query vertexId: {} exception: {} " + vertexId + vertexById.right().value()); - return null; - } - else return vertexById.left().value(); - } - - protected StorageOperationStatus updateVertexAndCommit(GraphVertex graphVertex) { - StorageOperationStatus status; - if ((status = janusGraphDao.updateVertex(graphVertex) - .either(v -> StorageOperationStatus.OK, this::handleError)) != StorageOperationStatus.OK) { - return status; - } - return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(janusGraphDao.commit()); - } - - protected StorageOperationStatus handleError(JanusGraphOperationStatus err) { - return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(JanusGraphOperationStatus.NOT_FOUND == err ? JanusGraphOperationStatus.OK : err); - } - - protected void removeEdges(Iterator edges) { - - while (edges.hasNext()) { - Edge edge = edges.next(); - edge.remove(); - } - } - - protected void removeEdgesInState(Iterator edges, String state) { - - while (edges.hasNext()) { - Edge edge = edges.next(); - String edgeState = (String) janusGraphDao.getProperty(edge, EdgePropertyEnum.STATE); - if (edgeState.equals(state)) { - edge.remove(); - } - } - } - - - protected void updateEdgeProperty(EdgePropertyEnum property, String value, Iterator edges) throws IOException { - while (edges.hasNext()) { - Edge edge = edges.next(); - Map prop = new HashMap<>(); - prop.put(property, value); - janusGraphDao.setEdgeProperties(edge, prop); - } - - } - - - // check if user has both edges state and last_state - protected boolean sameUser(List stateList, List lastStateList) { - - for (JanusGraphVertex lsVertex : lastStateList) { - String idLs = (String) janusGraphDao.getProperty(lsVertex, GraphPropertyEnum.USERID.getProperty()); - String idSt = (String) janusGraphDao.getProperty(stateList.get(0), GraphPropertyEnum.USERID.getProperty()); - if (idLs.equals(idSt)) { - return true; - } - } - return false; - } - - protected Iterator getVertexEdge(GraphVertex containerV, Direction direction, EdgeLabelEnum edgeLabel) { - return containerV.getVertex().edges(direction, edgeLabel.name()); - } - - public enum EdgeSide { - IN, OUT; - } -} - diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/handlers/OutputHandler.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/handlers/OutputHandler.java index b6dc27ff04..f7474ee4eb 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/handlers/OutputHandler.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/handlers/OutputHandler.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -22,8 +22,10 @@ package org.openecomp.sdc.asdctool.migration.tasks.handlers; public interface OutputHandler { - public void initiate(String name, Object... title); - public void addRecord(Object... record); - public boolean writeOutputAndCloseFile(); - + void initiate(String name, Object... title); + + void addRecord(Object... record); + + boolean writeOutputAndCloseFile(); + } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1710/UpgradeMigration1710.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1710/UpgradeMigration1710.java deleted file mode 100644 index 8eda8640c6..0000000000 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1710/UpgradeMigration1710.java +++ /dev/null @@ -1,1190 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2019 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.migration.tasks.mig1710; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.collect.Lists; -import fj.data.Either; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.collections.MapUtils; -import org.apache.commons.lang3.StringUtils; -import org.openecomp.sdc.asdctool.enums.LifecycleStateEnum; -import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult; -import org.openecomp.sdc.asdctool.migration.core.task.PostMigration; -import org.openecomp.sdc.asdctool.migration.tasks.handlers.XlsOutputHandler; -import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic; -import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic; -import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic; -import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException; -import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException; -import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; -import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic; -import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction; -import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction.LifecycleChanceActionEnum; -import org.openecomp.sdc.be.components.scheduledtasks.ComponentsCleanBusinessLogic; -import org.openecomp.sdc.be.config.Configuration; -import org.openecomp.sdc.be.config.ConfigurationManager; -import org.openecomp.sdc.be.dao.api.ActionStatus; -import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; -import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; -import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao; -import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum; -import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; -import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; -import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; -import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; -import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; -import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; -import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum; -import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; -import org.openecomp.sdc.be.impl.ComponentsUtils; -import org.openecomp.sdc.be.model.ComponentInstance; -import org.openecomp.sdc.be.model.ComponentInstanceProperty; -import org.openecomp.sdc.be.model.ComponentParametersView; -import org.openecomp.sdc.be.model.LifeCycleTransitionEnum; -import org.openecomp.sdc.be.model.Resource; -import org.openecomp.sdc.be.model.User; -import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; -import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter; -import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; -import org.openecomp.sdc.be.model.operations.impl.CsarOperation; -import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter; -import org.openecomp.sdc.be.model.operations.impl.UserAdminOperation; -import org.openecomp.sdc.common.log.wrappers.Logger; -import org.openecomp.sdc.exception.ResponseFormat; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import javax.annotation.PostConstruct; -import java.util.ArrayList; -import java.util.EnumMap; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.function.Predicate; -import java.util.stream.Collectors; - -@Component -public class UpgradeMigration1710 implements PostMigration { - - private static final String SERVICE_UUID_RPOPERTY = "providing_service_uuid"; - - private static final String SERVICE_INVARIANT_UUID_RPOPERTY = "providing_service_invariant_uuid"; - - private static final String UNKNOWN = "UNKNOWN"; - - private static final String CHECKOUT_MESSAGE = "checkout upon upgrade migration"; - - private static final String FAILED_TO_CHANGE_STATE_OF_COMPONENT = "Failed to change state of component with name {}, invariantUUID {}, version {} to {}. "; - - private static final String FAILED_TO_UPGRADE_COMPONENT = "Failed to upgrade {} with name {}, invariantUUID {}, version {}. Operation {}. The reason for failure: {}. "; - - private static final String UPGRADE_COMPONENT_SUCCEEDED = "Upgrade of {} with name {}, invariantUUID {}, version {} finished successfully. "; - - private static final String UPGRADE_VFS_FAILED = "Upgrade VFs upon upgrade migration 1710 process failed. "; - - private static final Logger log = Logger.getLogger(UpgradeMigration1710.class); - - private static final String ALLOTTED_RESOURCE_NAME = "Allotted Resource"; - - //as per US 397775, only node type upgrade should be enabled, - // to support resource and service upgrade, this flag should be reverted - private boolean isNodeTypesSupportOnly = true; - - @Autowired - private JanusGraphDao janusGraphDao; - - @Autowired - private ToscaOperationFacade toscaOperationFacade; - - @Autowired - private LifecycleBusinessLogic lifecycleBusinessLogic; - - @Autowired - private UserAdminOperation userAdminOperation; - - @Autowired - private ResourceBusinessLogic resourceBusinessLogic; - - @Autowired - private ServiceBusinessLogic serviceBusinessLogic; - - @Autowired - private CsarOperation csarOperation; - - @Autowired - private ComponentInstanceBusinessLogic componentInstanceBusinessLogic; - - @Autowired - private ComponentsUtils componentsUtils; - - @Autowired - private ComponentsCleanBusinessLogic componentsCleanBusinessLogic; - - private XlsOutputHandler outputHandler = new XlsOutputHandler(null, "UpgradeMigration1710report","COMPONENT TYPE", "COMPONENT NAME", "COMPONENT UUID", "COMPONENT UNIQUE_ID", "UPGRADE STATUS", "DESCRIPTION"); - - private User user = null; - - private final LifecycleChangeInfoWithAction changeInfo = new LifecycleChangeInfoWithAction(CHECKOUT_MESSAGE, LifecycleChanceActionEnum.UPGRADE_MIGRATION); - - private final Map latestGenericTypes = new HashMap<>(); - - private final Map latestOriginResourceVersions = new HashMap<>(); - - private final Map upgradedNodeTypesMap = new HashMap<>(); - - private List nodeTypes; - - private List proxyServiceContainers = new ArrayList<>(); - - private List vfAllottedResources = new ArrayList<>(); - - private List allottedVfContainers = new ArrayList<>(); - - private boolean isVfcUpgradeRequired = false; - - private boolean skipIfUpgradeVfFailed = true; - - private boolean isAllottedAndProxySupported = true; - - private String userId; - - private boolean isCleanupLocked = false; - - private int markedAsDeletedResourcesCnt = 0; - - private int markedAsDeletedServicesCnt = 0; - - //how many components can be deleted once - private int maxDeleteComponents = 10; - - private boolean enableAutoHealing = true; - - //map for tracing checked out resources that keep in place after upgrade failure - private HashMap certifiedToNextCheckedOutUniqueId = new HashMap<>(); - - private int deleteLockTimeoutInSeconds = 60; - - private boolean isLockSucceeded = false; - - /***********************************************/ - - @VisibleForTesting - void setNodeTypesSupportOnly(boolean nodeTypesSupportOnly) { - isNodeTypesSupportOnly = nodeTypesSupportOnly; - } - - @VisibleForTesting - void setUser(User user) { - this.user = user; - } - - @VisibleForTesting - void setMarkedAsDeletedResourcesCnt(int markedAsDeletedResourcesCnt) { - this.markedAsDeletedResourcesCnt = markedAsDeletedResourcesCnt; - } - - @VisibleForTesting - void setMarkedAsDeletedServicesCnt(int markedAsDeletedServicesCnt) { - this.markedAsDeletedServicesCnt = markedAsDeletedServicesCnt; - } - - @PostConstruct - void init() { - Configuration config = ConfigurationManager.getConfigurationManager().getConfiguration(); - isVfcUpgradeRequired = !config.getSkipUpgradeVSPsFlag(); - skipIfUpgradeVfFailed = config.getSkipUpgradeFailedVfs(); - isAllottedAndProxySupported = config.getSupportAllottedResourcesAndProxyFlag(); - deleteLockTimeoutInSeconds = config.getDeleteLockTimeoutInSeconds(); - maxDeleteComponents = config.getMaxDeleteComponents(); - - String toscaConformanceLevel = config.getToscaConformanceLevel(); - Map> resourcesForUpgrade = config.getResourcesForUpgrade(); - nodeTypes = resourcesForUpgrade.get(toscaConformanceLevel); - enableAutoHealing = config.isEnableAutoHealing(); - userId = config.getAutoHealingOwner(); - isNodeTypesSupportOnly = true; - } - - @Override - public String description() { - return "Upgrade migration 1710 - post migration task, which is dedicated to upgrade all latest certified (and not checked out) Node types, VFs and Services. "; - } - - enum UpgradeStatus { - UPGRADED, - UPGRADED_AS_INSTANCE, - NOT_UPGRADED - } - - @Override - public MigrationResult migrate() { - MigrationResult migrationResult = new MigrationResult(); - //stop the upgrade if this ask is disabled - if (!enableAutoHealing) { - log.warn("Upgrade migration 1710 task is disabled"); - migrationResult.setMigrationStatus(MigrationResult.MigrationStatus.COMPLETED); - return migrationResult ; - } - log.info("Starting upgrade migration 1710 process. "); - boolean result = true; - - try { - //lock cleanup node to avoid BE to delete marked components - //while the auto-healing process is running - isLockSucceeded = isNodeTypesSupportOnly ? true : isLockDeleteOperationSucceeded(); - - if (!isLockSucceeded) { - result = false; - log.error("Cleanup node can't be locked. Upgrade migration failed"); - } - else { - Either userReq = userAdminOperation.getUserData(userId, false); - if (userReq.isRight()) { - result = false; - log.error("Upgrade migration failed. User {} resolve failed: {} ", userId, userReq.right().value()); - } else { - user = userReq.left().value(); - log.info("User {} will perform upgrade operation", user.getUserId()); - } - } - if (result) { - result = upgradeNodeTypes(); - } - if (!isNodeTypesSupportOnly && result) { - result = upgradeTopologyTemplates(); - } - } - catch (Exception e) { - result = false; - log.error("Error occurred during the migration: ", e); - } finally { - MigrationResult.MigrationStatus status = result ? - MigrationResult.MigrationStatus.COMPLETED : MigrationResult.MigrationStatus.FAILED; - cleanup(status); - migrationResult.setMigrationStatus(status); - } - return migrationResult; - } - - private boolean upgradeTopologyTemplates() { - if (upgradeVFs()) { - upgradeServices(); - upgradeProxyServiceContainers(); - upgradeAllottedVFs(); - upgradeAllottedVfContainers(); - return true; - } - return false; - } - - private void cleanup(MigrationResult.MigrationStatus status) { - if (status == MigrationResult.MigrationStatus.COMPLETED ) { - log.info("Upgrade migration 1710 has been successfully finished. "); - janusGraphDao.commit(); - } else { - log.info("Upgrade migration 1710 was failed. "); - janusGraphDao.rollback(); - } - outputHandler.writeOutputAndCloseFile(); - if (!isNodeTypesSupportOnly && isLockSucceeded) { - //delete rest of components if their upgrade failed - markedAsDeletedResourcesCnt = maxDeleteComponents; - deleteResourcesIfLimitIsReached(); - markedAsDeletedServicesCnt = maxDeleteComponents; - deleteServicesIfLimitIsReached(); - unlockDeleteOperation(); - } - } - - void upgradeServices(List uniqueIDs, Predicate shouldUpgrade, final String containerName) { - log.info("Starting upgrade {} upon upgrade migration 1710 process. ", containerName); - for (String currUid : uniqueIDs) { - upgradeServiceAndCommitIfNeeded(currUid, shouldUpgrade); - } - log.info("Upgrade {} upon upgrade migration 1710 process is finished. ", containerName); - } - - private void upgradeServiceAndCommitIfNeeded(String currUid, Predicate shouldUpgrade) { - boolean result = true; - try { - result = handleService(currUid, shouldUpgrade); - } catch (Exception e) { - result = false; - log.error("Failed to upgrade service with uniqueId {} due to a reason {}. ", currUid, e.getMessage()); - log.debug("Failed to upgrade service with uniqueId {}", currUid, e); - } - finally { - if (result) { - log.info("Service upgrade finished successfully: uniqueId {} ", currUid); - janusGraphDao.commit(); - } - else { - log.error("Failed to upgrade service with uniqueId {} ", currUid); - janusGraphDao.rollback(); - } - markCheckedOutServiceAsDeletedIfUpgradeFailed(currUid, result); - } - } - - private void upgradeAllottedVfContainers() { - upgradeServices(allottedVfContainers, component -> true, "proxy " + allottedVfContainers.size() + " service containers"); - } - - private void upgradeServices() { - Either, JanusGraphOperationStatus> getServicesRes = getAllLatestCertifiedComponentUids(VertexTypeEnum.TOPOLOGY_TEMPLATE, ComponentTypeEnum.SERVICE); - if (getServicesRes.isRight()) { - log.error("Failed to retrieve the latest certified service versions"); - return; - } - upgradeServices(getServicesRes.left().value(), this::shouldUpgrade, "services"); - } - - private void upgradeProxyServiceContainers() { - upgradeServices(proxyServiceContainers, component -> true, "proxy service containers"); - } - - private boolean handleService(String uniqueId, Predicate shouldUpgrade) { - log.info("Starting upgrade Service with uniqueId {} upon upgrade migration 1710 process. ", uniqueId); - Either getServiceRes = toscaOperationFacade.getToscaElement(uniqueId); - if(getServiceRes.isRight()){ - log.error("Failed to upgrade service with uniqueId {} due to {}. ", uniqueId, getServiceRes.right().value()); - outputHandler.addRecord(ComponentTypeEnum.SERVICE.name(), UNKNOWN, UNKNOWN, uniqueId, MigrationResult.MigrationStatus.FAILED.name(), getServiceRes.right().value()); - return false; - } - String derivedFromGenericType = getServiceRes.left().value().getDerivedFromGenericType(); - log.debug("derivedFromGenericType: {}", derivedFromGenericType ); - if (derivedFromGenericType == null) { - //malformed field value, upgrade required - return upgradeService(getServiceRes.left().value()); - } - if(!latestGenericTypes.containsKey(derivedFromGenericType)){ - Either, JanusGraphOperationStatus> getDerivedRes = findDerivedResources(derivedFromGenericType); - if(getDerivedRes.isRight()){ - log.error(FAILED_TO_UPGRADE_COMPONENT, getServiceRes.left().value().getComponentType().getValue(), getServiceRes.left().value().getName(), getServiceRes.left().value().getInvariantUUID(), getServiceRes.left().value().getVersion(), "findDerivedResources", getDerivedRes.right().value()); - outputHandler.addRecord( getServiceRes.left().value().getComponentType().name(),getServiceRes.left().value().getName(), getServiceRes.left().value().getInvariantUUID(), getServiceRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), getDerivedRes.right().value()); - return false; - } - latestGenericTypes.put(derivedFromGenericType, getDerivedRes.left().value().get(0)); - } - if(latestVersionExists(latestGenericTypes.get(derivedFromGenericType), getServiceRes.left().value().getDerivedFromGenericVersion())){ - return upgradeService(getServiceRes.left().value()); - } - if(!collectLatestOriginResourceVersions(getServiceRes.left().value())){ - return false; - } - if(shouldUpgrade.test(getServiceRes.left().value())){ - return upgradeService(getServiceRes.left().value()); - } - outputHandler.addRecord(getServiceRes.left().value().getComponentType().name(), getServiceRes.left().value().getName(), getServiceRes.left().value().getInvariantUUID(), getServiceRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.COMPLETED.name(), UpgradeStatus.NOT_UPGRADED); - return true; - } - - private boolean collectLatestOriginResourceVersions(org.openecomp.sdc.be.model.Component component) { - if (CollectionUtils.isNotEmpty(component.getComponentInstances())) { - for (ComponentInstance instance : component.getComponentInstances()) { - if (instance.getOriginType() != OriginTypeEnum.ServiceProxy && !latestOriginResourceVersions.containsKey(instance.getToscaComponentName()) && !addComponent(component, instance)) { - return false; - } - } - } - return true; - } - - private boolean addComponent(org.openecomp.sdc.be.model.Component component, ComponentInstance instance) { - VertexTypeEnum vertexType = ModelConverter.getVertexType(instance.getOriginType().name()); - Either getOriginRes = toscaOperationFacade.getLatestCertifiedByToscaResourceName(instance.getToscaComponentName(), vertexType, JsonParseFlagEnum.ParseMetadata); - if (getOriginRes.isRight()) { - log.error(FAILED_TO_UPGRADE_COMPONENT, component.getComponentType().getValue(), component.getName(), component.getInvariantUUID(), component.getVersion(), "toscaOperationFacade.getLatestCertifiedByToscaResourceName", getOriginRes.right().value()); - outputHandler.addRecord(component.getComponentType().name(), component.getName(), component.getInvariantUUID(), component.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), getOriginRes.right().value()); - return false; - } - latestOriginResourceVersions.put(instance.getToscaComponentName(), getOriginRes.left().value().getVersion()); - return true; - } - - private boolean shouldUpgrade(org.openecomp.sdc.be.model.Component component) { - if(CollectionUtils.isNotEmpty(component.getComponentInstances())) { - if (containsProxyOrAllottedVF(component)) { - return false; - } - for(ComponentInstance instance : component.getComponentInstances()){ - if(isGreater(latestOriginResourceVersions.get(instance.getToscaComponentName()), instance.getComponentVersion())){ - log.info("The service with name {}, invariantUUID {}, version {}, contains instance {} from outdated version of origin {} {} , than the service should be upgraded. ", component.getName(), component.getInvariantUUID(), component.getVersion(), instance.getName(), instance.getComponentName(), instance.getComponentVersion()); - return true; - } - } - } - return false; - } - - private boolean containsProxyOrAllottedVF(org.openecomp.sdc.be.model.Component component) { - return !component.getComponentInstances() - .stream() - .filter(i->isProxyOrAllottedVF(i, component.getUniqueId())) - .collect(Collectors.toList()).isEmpty(); - } - - private boolean isProxyOrAllottedVF(ComponentInstance instance, String uniqueId) { - if (instance.getOriginType() == OriginTypeEnum.ServiceProxy) { - keepProxyServiceContainerIfSupported(uniqueId); - return true; - } - if (isAllottedResource(instance.getActualComponentUid())) { - keepAllottedVfContainerIfSupported(uniqueId); - return true; - } - return false; - } - - private void keepAllottedVfContainerIfSupported(final String uniqueId) { - if (isAllottedAndProxySupported && !allottedVfContainers.contains(uniqueId)) { - log.info("Add a service with uniqueId {} to allotted VF containers container list", uniqueId); - allottedVfContainers.add(uniqueId); - } - } - - private void keepProxyServiceContainerIfSupported(final String uniqueId) { - if (isAllottedAndProxySupported && !proxyServiceContainers.contains(uniqueId)) { - log.info("Add a service with uniqueId {} to proxy service container list", uniqueId); - proxyServiceContainers.add(uniqueId); - } - } - - private boolean upgradeService(org.openecomp.sdc.be.model.Component service) { - String serviceName = service.getName(); - String serviceUuid = service.getUUID(); - log.info("Starting upgrade Service with name {}, invariantUUID {}, version {} upon upgrade migration 1710 process. ", serviceName, service.getInvariantUUID(), service.getVersion()); - Either checkouRes = checkOutComponent(service); - if (checkouRes.isRight()) { - log.error(FAILED_TO_UPGRADE_COMPONENT, service.getComponentType().getValue(), serviceName, service.getInvariantUUID(), service.getVersion(), "lifecycleBusinessLogic.changeComponentState", checkouRes.right().value().getFormattedMessage()); - outputHandler.addRecord(service.getComponentType().name(), serviceName, serviceUuid, service.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), checkouRes.right().value().getFormattedMessage()); - return false; - } - Either updateCompositionRes = updateComposition(checkouRes.left().value()); - if (updateCompositionRes.isRight()) { - log.error(FAILED_TO_UPGRADE_COMPONENT, service.getComponentType().getValue(), serviceName, service.getInvariantUUID(), service.getVersion(), "updateComposition", updateCompositionRes.right().value().getFormattedMessage()); - outputHandler.addRecord(checkouRes.left().value().getComponentType().name(), checkouRes.left().value().getName(), checkouRes.left().value().getUUID(), checkouRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), updateCompositionRes.right().value().getFormattedMessage()); - return false; - } - Either certifyRes = performFullCertification(checkouRes.left().value()); - if (certifyRes.isRight()) { - log.error(FAILED_TO_UPGRADE_COMPONENT, service.getComponentType().getValue(), serviceName, service.getInvariantUUID(), service.getVersion(), "performFullCertification", certifyRes.right().value().getFormattedMessage()); - outputHandler.addRecord(checkouRes.left().value().getComponentType().name(), checkouRes.left().value().getName(), checkouRes.left().value().getInvariantUUID(), checkouRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), certifyRes.right().value().getFormattedMessage()); - return false; - } - outputHandler.addRecord(checkouRes.left().value().getComponentType().name(), checkouRes.left().value().getName(), serviceUuid, checkouRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.COMPLETED.name(), UpgradeStatus.UPGRADED); - return true; - } - - private Either updateComposition(org.openecomp.sdc.be.model.Component component) { - if (component != null && component.getComponentInstances() != null) { - for (ComponentInstance instance : component.getComponentInstances()) { - try { - upgradeInstance(component, instance); - }catch (ComponentException e){ - ResponseFormat responseFormat = e.getResponseFormat(); - log.error(FAILED_TO_UPGRADE_COMPONENT, component.getComponentType().getValue(), component.getName(), - component.getInvariantUUID(), component.getVersion(), "upgradeInstance", - responseFormat.getFormattedMessage()); - outputHandler.addRecord(component.getComponentType().name(), component.getName(), component.getUUID(), - component.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), - responseFormat.getFormattedMessage()); - return Either.right(responseFormat); - } - } - } - return Either.left(component); - } - - private ComponentInstance upgradeInstance(org.openecomp.sdc.be.model.Component component, ComponentInstance instance) { - log.info("Starting upgrade {} instance {} upon upgrade migration 1710 process. ", component.getComponentType().getValue(), instance.getName()); - ComponentInstance newComponentInstance = new ComponentInstance(instance); - if (instance.getOriginType() == OriginTypeEnum.ServiceProxy) { - return upgradeServiceProxyInstance(component, instance, newComponentInstance); - } - return upgradeResourceInstance(component, instance, newComponentInstance); - } - - private ComponentInstance upgradeResourceInstance(org.openecomp.sdc.be.model.Component component, ComponentInstance instance, ComponentInstance newComponentInstance) { - - log.info("Starting upgrade {} instance {} upon upgrade migration 1710 process. ", component.getComponentType().getValue(), instance.getName()); - ComponentInstance upgradeInstanceRes = null; - VertexTypeEnum vertexType = ModelConverter.getVertexType(instance.getOriginType().name()); - Either getOriginRes = toscaOperationFacade.getLatestCertifiedByToscaResourceName(instance.getToscaComponentName(), vertexType, JsonParseFlagEnum.ParseMetadata); - if(getOriginRes.isRight()){ - log.info("Upgrade of {} instance {} upon upgrade migration 1710 process failed due to a reason {}. ", - component.getComponentType().getValue(), instance.getName(), getOriginRes.right().value()); - throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(getOriginRes.right().value(), instance.getOriginType().getComponentType())); - } - copyComponentNameAndVersionToNewInstance(newComponentInstance, getOriginRes.left().value()); - - if(isGreater(getOriginRes.left().value().getVersion(), instance.getComponentVersion())){ - upgradeInstanceRes = changeAssetVersion(component, instance, newComponentInstance); - } - if(isAllottedResource(instance.getComponentUid()) && MapUtils.isNotEmpty(component.getComponentInstancesProperties())){ - ComponentInstance instanceToUpdate = upgradeInstanceRes == null ? instance : upgradeInstanceRes; - upgradeInstanceRes = updateServiceUuidProperty(component, instanceToUpdate, component.getComponentInstancesProperties().get(instance.getUniqueId())); - } - //upgrade nodes contained by CVFC - if(upgradeInstanceRes == null && isVfcUpgradeRequired && newComponentInstance.getOriginType() == OriginTypeEnum.CVFC && - !upgradeVf(getOriginRes.left().value().getUniqueId(), false, true)) { - throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR); - } - if(upgradeInstanceRes == null){ - upgradeInstanceRes = instance; - } - log.info("Upgrade of {} instance {} upon upgrade migration 1710 process finished successfully. ", - component.getComponentType().getValue(), instance.getName()); - return upgradeInstanceRes; - } - - private void copyComponentNameAndVersionToNewInstance(ComponentInstance newComponentInstance, Resource originResource) { - newComponentInstance.setComponentName(originResource.getName()); - newComponentInstance.setComponentUid(originResource.getUniqueId()); - newComponentInstance.setComponentVersion(originResource.getVersion()); - newComponentInstance.setToscaComponentName(originResource.getToscaResourceName()); - } - - private ComponentInstance updateServiceUuidProperty(org.openecomp.sdc.be.model.Component component, ComponentInstance instance, List instanceProperties){ - if(isAllottedResource(instance.getComponentUid()) && instanceProperties != null){ - Optional propertyUuid = instanceProperties.stream().filter(p->p.getName().equals(SERVICE_UUID_RPOPERTY)).findFirst(); - Optional propertyInvariantUuid = instanceProperties.stream().filter(p->p.getName().equals(SERVICE_INVARIANT_UUID_RPOPERTY)).findFirst(); - if(propertyUuid.isPresent() && propertyInvariantUuid.isPresent()){ - String serviceInvariantUUID = propertyInvariantUuid.get().getValue(); - Either, JanusGraphOperationStatus> getLatestOriginServiceRes = getLatestCertifiedService(serviceInvariantUUID); - if (getLatestOriginServiceRes.isRight()) { - return instance; - } - propertyUuid.get().setValue((String) getLatestOriginServiceRes.left().value().get(0).getJsonMetadataField(JsonPresentationFields.UUID)); - componentInstanceBusinessLogic.createOrUpdatePropertiesValues(component.getComponentType(), component.getUniqueId(), instance.getUniqueId(), Lists.newArrayList(propertyUuid.get()), user.getUserId()) - .right() - .forEach(e -> log.debug("Failed to update property {} of the instance {} of the component {}. ", SERVICE_UUID_RPOPERTY, instance.getUniqueId(), component.getName())); - } - } - return instance; - } - - private boolean isAllottedResource(String uniqueId){ - ComponentParametersView filters = new ComponentParametersView(true); - filters.setIgnoreCategories(false); - Either getResourceRes = toscaOperationFacade.getToscaElement(uniqueId, filters); - if(getResourceRes.isRight()){ - return false; - } - if(getResourceRes.left().value().getCategories() != null && getResourceRes.left().value().getCategories().get(0)!= null){ - return ALLOTTED_RESOURCE_NAME.equals(getResourceRes.left().value().getCategories().get(0).getName()); - } - return false; - } - - private boolean isAllottedVf(org.openecomp.sdc.be.model.Component component){ - if(component.getComponentType() != ComponentTypeEnum.RESOURCE || ((Resource)component).getResourceType() != ResourceTypeEnum.VF){ - return false; - } - return isAllottedResource(component.getUniqueId()); - } - - private ComponentInstance upgradeServiceProxyInstance(org.openecomp.sdc.be.model.Component component, ComponentInstance instance, ComponentInstance newComponentInstance) { - Either, JanusGraphOperationStatus> getLatestOriginServiceRes = getLatestCertifiedService(instance.getSourceModelInvariant()); - if (getLatestOriginServiceRes.isRight()) { - throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getLatestOriginServiceRes.right().value()), instance.getOriginType().getComponentType())); - } - ModelConverter.getVertexType(instance.getOriginType().name()); - Either getOriginRes = toscaOperationFacade.getLatestByName(instance.getComponentName()); - if(getOriginRes.isRight()){ - log.info("Upgrade of {} instance {} upon upgrade migration 1710 process failed due to a reason {}. ", - component.getComponentType().getValue(), instance.getName(), getOriginRes.right().value()); - throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(getOriginRes.right().value(), instance.getOriginType().getComponentType())); - } - newComponentInstance.setComponentUid((String) getLatestOriginServiceRes.left().value().get(0).getJsonMetadataField(JsonPresentationFields.UNIQUE_ID)); - return changeAssetVersion(component, instance, newComponentInstance); - } - - private Either, JanusGraphOperationStatus> getLatestCertifiedService(String invariantUUID) { - - Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); - propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name()); - propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); - propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); - propertiesToMatch.put(GraphPropertyEnum.INVARIANT_UUID, invariantUUID); - Map propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class); - propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); - return janusGraphDao - .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata); - } - - private ComponentInstance changeAssetVersion(org.openecomp.sdc.be.model.Component containerComponent, ComponentInstance instance, ComponentInstance newComponentInstance) { - return componentInstanceBusinessLogic.changeComponentInstanceVersion(ComponentTypeEnum.SERVICE_PARAM_NAME, containerComponent.getUniqueId(), instance.getUniqueId(), user.getUserId(), newComponentInstance); - } - - private boolean upgradeNodeTypes() { - log.info("Starting upgrade node types upon upgrade migration 1710 process. "); - if (nodeTypes != null && !nodeTypes.isEmpty()) { - Either, JanusGraphOperationStatus> getRes = getAllLatestCertifiedComponentUids(VertexTypeEnum.NODE_TYPE, ComponentTypeEnum.RESOURCE); - if (getRes.isRight()) { - return false; - } - for (String toscaResourceName : nodeTypes) { - if (!upgradeNodeType(toscaResourceName, getRes.left().value())) { - return false; - } - } - } - else { - log.info("No node types for upgrade are configured"); - } - return true; - } - - private boolean upgradeNodeType(String toscaResourceName, List allNodeTypes) { - Either, StorageOperationStatus> status = getLatestByName(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName); - if (status.isRight()) { - log.error("Failed to find node type {} ", toscaResourceName); - return false; - } - List vList = status.left().value(); - for (GraphVertex vertex : vList) { - StorageOperationStatus updateRes = upgradeNodeType(vertex, allNodeTypes); - if (updateRes != StorageOperationStatus.OK) { - return false; - } - } - return true; - } - - private boolean upgradeVFs() { - log.info("Starting upgrade VFs upon upgrade migration 1710 process. "); - Either, JanusGraphOperationStatus> getVfsRes = getAllLatestCertifiedComponentUids(VertexTypeEnum.TOPOLOGY_TEMPLATE, ComponentTypeEnum.RESOURCE); - if (getVfsRes.isRight()) { - log.info(UPGRADE_VFS_FAILED); - return false; - } - return upgradeVFs(getVfsRes.left().value(), false); - } - - private boolean upgradeAllottedVFs() { - log.info("Starting upgrade {} allotted Vfs with upon upgrade migration 1710 process. ", vfAllottedResources.size()); - return upgradeVFs(vfAllottedResources, true); - } - - boolean upgradeVFs(List resourceList, boolean isAllottedVfsUpgrade) { - for (String currUid : resourceList) { - boolean result = true; - try { - result = upgradeVf(currUid, isAllottedVfsUpgrade, false); - if (!result && !skipIfUpgradeVfFailed) { - return false; - } - } catch (Exception e) { - log.error("The exception {} occurred upon upgrade VFs. ", e.getMessage()); - log.debug("The exception occurred upon upgrade VFs:", e); - result = false; - if (!skipIfUpgradeVfFailed) { - return false; - } - } - finally { - if (result) { - log.info("Resource upgrade finished successfully: uniqueId {} ", currUid); - janusGraphDao.commit(); - } - else { - log.error("Failed to upgrade resource with uniqueId {} ", currUid); - janusGraphDao.rollback(); - } - markCheckedOutResourceAsDeletedIfUpgradeFailed(currUid, result); - } - } - log.info("Upgrade VFs upon upgrade migration 1710 process finished successfully. "); - return true; - } - - private boolean upgradeVf(String uniqueId, boolean allottedVfsUpgrade, boolean isInstance) { - log.info("Starting upgrade VF with uniqueId {} upon upgrade migration 1710 process. ", uniqueId); - Either getRes = toscaOperationFacade.getToscaElement(uniqueId); - if (getRes.isRight()) { - log.debug("Failed to fetch VF with uniqueId {} upon upgrade migration 1710 process. ", uniqueId); - outputHandler.addRecord(ComponentTypeEnum.RESOURCE.name(), UNKNOWN, UNKNOWN, uniqueId, MigrationResult.MigrationStatus.FAILED.name(), getRes.right().value()); - return false; - } - if(!allottedVfsUpgrade && isAllottedVf(getRes.left().value())){ - keepAllottedResourceIfSupported(uniqueId); - return true; - } - if (StringUtils.isNotEmpty(getRes.left().value().getCsarUUID())) { - log.info("Going to fetch the latest version of VSP with csarUUID {} upon upgrade migration 1710 process. ", getRes.left().value().getCsarUUID()); - Either latestVersionRes = csarOperation.getCsarLatestVersion(getRes.left().value().getCsarUUID(), user); - if (latestVersionRes.isRight()) { - log.debug("Failed to fetch the latest version of VSP with csarUUID {} upon upgrade migration 1710 process. ", getRes.left().value().getCsarUUID()); - outputHandler.addRecord(getRes.left().value().getComponentType().name(), getRes.left().value().getName(), getRes.left().value().getUUID(), getRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), latestVersionRes.right().value()); - return false; - } - if (isGreater(latestVersionRes.left().value(), getRes.left().value().getCsarVersion())) { - return upgradeVfWithLatestVsp(getRes.left().value(), latestVersionRes.left().value(), isInstance); - } - if (isVfcUpgradeRequired) { - return upgradeComponentWithLatestGeneric(getRes.left().value(), isInstance); - } - log.warn("Warning: No need to upgrade VF with name {}, invariantUUID {}, version {} and VSP version {}. No new version of VSP. ", getRes.left().value().getName(), getRes.left().value().getInvariantUUID(), getRes.left().value().getVersion(), getRes.left().value().getCsarVersion()); - return true; - } - else { - return upgradeComponentWithLatestGeneric(getRes.left().value(), isInstance); - } - } - - private void keepAllottedResourceIfSupported(final String uniqueId) { - if (isAllottedAndProxySupported && !vfAllottedResources.contains(uniqueId)) { - log.info("Add a resource with uniqueId {} to allotted resource list", uniqueId); - vfAllottedResources.add(uniqueId); - } - } - - private boolean upgradeVfWithLatestVsp(org.openecomp.sdc.be.model.Component vf, String latestVersion, boolean isInstance) { - log.info("Starting upgrade vf with name {}, invariantUUID {}, version {} and latest VSP version {} upon upgrade migration 1710 process. ", vf.getName(), vf.getInvariantUUID(), vf.getVersion(), latestVersion); - Either checkouRes = checkOutComponent(vf); - if (checkouRes.isRight()) { - outputHandler.addRecord(vf.getComponentType().name(), vf.getName(), vf.getUUID(), vf.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), checkouRes.right().value().getFormattedMessage()); - return false; - } - Resource resourceToUpdate = new Resource(((Resource) checkouRes.left().value()).getComponentMetadataDefinition()); - resourceToUpdate.setDerivedFromGenericType(((Resource) checkouRes.left().value()).getDerivedFromGenericType()); - resourceToUpdate.setDerivedFromGenericVersion(((Resource) checkouRes.left().value()).getDerivedFromGenericVersion()); - resourceToUpdate.setCsarVersion(Double.toString(Double.parseDouble(latestVersion))); - resourceToUpdate.setCategories(((Resource)checkouRes.left().value()).getCategories()); - try { - Resource updateResourceFromCsarRes = resourceBusinessLogic.validateAndUpdateResourceFromCsar(resourceToUpdate, user, null, null, resourceToUpdate.getUniqueId()); - } catch(ByResponseFormatComponentException e){ - outputHandler.addRecord(resourceToUpdate.getComponentType().name(), resourceToUpdate.getName(), resourceToUpdate.getUUID(), resourceToUpdate.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), e.getResponseFormat().getFormattedMessage()); - log.info("Failed to update vf with name {}, invariantUUID {}, version {} and latest VSP {}. ", vf.getName(), vf.getInvariantUUID(), vf.getVersion(), latestVersion); - return false; - } - Either certifyRes = performFullCertification(checkouRes.left().value()); - if (certifyRes.isRight()) { - log.info(FAILED_TO_CHANGE_STATE_OF_COMPONENT, checkouRes.left().value().getName(), checkouRes.left().value().getInvariantUUID(), checkouRes.left().value().getVersion(), LifeCycleTransitionEnum.CERTIFY); - outputHandler.addRecord(checkouRes.left().value().getComponentType().name(), checkouRes.left().value().getName(), checkouRes.left().value().getInvariantUUID(), checkouRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), certifyRes.right().value().getFormattedMessage()); - return false; - } - log.info("Full certification of vf with name {}, invariantUUID {}, version {} finished . ", vf.getName(), vf.getInvariantUUID(), vf.getVersion(), latestVersion); - outputHandler.addRecord(certifyRes.left().value().getComponentType().name(), certifyRes.left().value().getName(), certifyRes.left().value().getUUID(), certifyRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.COMPLETED.name(), getVfUpgradeStatus(true, isInstance)); - return true; - } - - private boolean upgradeComponentWithLatestGeneric(org.openecomp.sdc.be.model.Component component, boolean isInstance) { - String derivedFromGenericType = component.getDerivedFromGenericType(); - String derivedFromGenericVersion = component.getDerivedFromGenericVersion(); - org.openecomp.sdc.be.model.Component updatedComponent = component; - if (failedToFindDerivedResourcesOfNodeType(component, derivedFromGenericType, derivedFromGenericVersion)) { - return false; - } - if (StringUtils.isEmpty(derivedFromGenericType) || - latestVersionExists(latestGenericTypes.get(derivedFromGenericType), derivedFromGenericVersion) || - isVfcUpgradeRequired || - isAllottedAndProxySupported) { - if (StringUtils.isNotEmpty(derivedFromGenericType)) { - log.info("Newer version {} of derived from generic type {} exists. ", latestGenericTypes.get(derivedFromGenericType).getJsonMetadataField(JsonPresentationFields.VERSION), derivedFromGenericType); - } - else { - log.info("The vf resource with name {}, invariantUUID {}, version {}, has an empty derivedFromGenericType field. ", component.getName(), component.getInvariantUUID(), component.getVersion()); - } - updatedComponent = checkOutAndCertifyComponent(component); - } else { - log.info("The version {} of derived from generic type {} is up to date. No need to upgrade component with name {}, invariantUUID {} and version {}. ", latestGenericTypes.get(derivedFromGenericType), derivedFromGenericType, component.getName(), component.getInvariantUUID(), component.getVersion()); - } - if (updatedComponent != null) { - log.info(UPGRADE_COMPONENT_SUCCEEDED, component.getComponentType().getValue(), component.getName(), component.getInvariantUUID(), component.getVersion()); - outputHandler.addRecord(updatedComponent.getComponentType().name(), updatedComponent.getName(), updatedComponent.getUUID(), updatedComponent.getUniqueId(), MigrationResult.MigrationStatus.COMPLETED.name(), - getVfUpgradeStatus(!updatedComponent.equals(component), isInstance)); - } - return true; - } - - private org.openecomp.sdc.be.model.Component checkOutAndCertifyComponent(org.openecomp.sdc.be.model.Component component) { - - log.info("Starting to perform check out of vf with name {}, invariantUUID {}, version {}. ", component.getName(), component.getInvariantUUID(), component.getVersion()); - Either checkoutRes = checkOutComponent(component); - if (checkoutRes.isRight()) { - log.error(FAILED_TO_CHANGE_STATE_OF_COMPONENT, component.getName(), component.getInvariantUUID(), component.getVersion(), LifeCycleTransitionEnum.CHECKOUT); - outputHandler.addRecord(component.getComponentType().name(), component.getName(), component.getInvariantUUID(), component.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), checkoutRes.right().value().getFormattedMessage()); - return null; - } - - if (!updateCompositionFailed(component, checkoutRes.left().value())) { - return null; - } - Either certifyRes = performFullCertification(checkoutRes.left().value()); - if (certifyRes.isRight()) { - log.error(FAILED_TO_UPGRADE_COMPONENT, component.getComponentType().getValue(), component.getName(), component.getInvariantUUID(), component.getVersion(), "performFullCertification", certifyRes.right().value()); - outputHandler.addRecord(checkoutRes.left().value().getComponentType().name(), checkoutRes.left().value().getName(), checkoutRes.left().value().getInvariantUUID(), checkoutRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), certifyRes.right().value().getFormattedMessage()); - return null; - } - return certifyRes.left().value(); - } - - private boolean failedToFindDerivedResourcesOfNodeType(org.openecomp.sdc.be.model.Component component, String derivedFromGenericType, String derivedFromGenericVersion) { - if (StringUtils.isNotEmpty(derivedFromGenericType) && !latestGenericTypes.containsKey(derivedFromGenericType)) { - log.info("Starting upgrade vf with name {}, invariantUUID {}, version {}, latest derived from generic type {}, latest derived from generic version {}. ", component.getName(), component.getInvariantUUID(), component.getVersion(), derivedFromGenericType, derivedFromGenericVersion); - log.info("Starting to fetch latest generic node type {}. ", derivedFromGenericType); - Either, JanusGraphOperationStatus> getDerivedRes = findDerivedResources(derivedFromGenericType); - if (getDerivedRes.isRight()) { - outputHandler.addRecord(component.getComponentType().name(), component.getName(), component.getInvariantUUID(), component.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), getDerivedRes.right().value()); - log.info("Failed to upgrade component with name {}, invariantUUID {}, version {} and latest generic. Status is {}. ", component.getName(), component.getInvariantUUID(), component.getVersion(), derivedFromGenericType); - return true; - } - latestGenericTypes.put(derivedFromGenericType, getDerivedRes.left().value().get(0)); - } - return false; - } - - private boolean updateCompositionFailed(org.openecomp.sdc.be.model.Component component, org.openecomp.sdc.be.model.Component checkoutResource) { - //try to update included VFCs, if it is either required as per configuration or an allotted resource - if ((isVfcUpgradeRequired && CollectionUtils.isNotEmpty(checkoutResource.getComponentInstances())) || isAllottedAndProxySupported) { - log.info("VFC upgrade is required: updating components of vf with name {}, invariantUUID {}, version {}. ", component.getName(), component.getInvariantUUID(), component.getVersion()); - Either updateCompositionRes = updateComposition(checkoutResource); - if (updateCompositionRes.isRight()) { - if (log.isErrorEnabled()) { - log.error(FAILED_TO_UPGRADE_COMPONENT, checkoutResource.getComponentType().name(), checkoutResource.getName(), checkoutResource.getInvariantUUID(), checkoutResource.getVersion(), "updateComposition", updateCompositionRes.right().value().getFormattedMessage()); - } - outputHandler.addRecord(checkoutResource.getComponentType().name(), checkoutResource.getName(), checkoutResource.getUUID(), checkoutResource.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), updateCompositionRes.right().value().getFormattedMessage()); - return true; - } - } - return false; - } - - private StorageOperationStatus upgradeNodeType(GraphVertex nodeTypeV, List allCertifiedUids) { - StorageOperationStatus result = StorageOperationStatus.OK; - log.info("Starting upgrade node type with name {}, invariantUUID {}, version{}. ", nodeTypeV.getMetadataProperty(GraphPropertyEnum.NAME), nodeTypeV.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID), nodeTypeV.getMetadataProperty(GraphPropertyEnum.VERSION)); - log.info("Starting to find derived to for node type with name {}, invariantUUID {}, version{}. ", nodeTypeV.getMetadataProperty(GraphPropertyEnum.NAME), nodeTypeV.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID), nodeTypeV.getMetadataProperty(GraphPropertyEnum.VERSION)); - Either, JanusGraphOperationStatus> parentResourceRes = janusGraphDao - .getParentVertices(nodeTypeV, EdgeLabelEnum.DERIVED_FROM, JsonParseFlagEnum.ParseMetadata); - if (parentResourceRes.isRight() && parentResourceRes.right().value() != JanusGraphOperationStatus.NOT_FOUND) { - return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(parentResourceRes.right().value()); - - } - List derivedResourcesUid = getAllDerivedGraphVertices(allCertifiedUids, parentResourceRes); - String uniqueId = (String) nodeTypeV.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID); - - Either getRes = toscaOperationFacade.getToscaElement(uniqueId); - if (getRes.isRight()) { - log.info("failed to fetch element with uniqueId {} ", uniqueId); - return getRes.right().value(); - } - - Resource nodeType = (Resource)getRes.left().value(); - if (!upgradedNodeTypesMap.containsKey(nodeType.getToscaResourceName()) && !nodeTypes.stream().anyMatch(p -> p.equals(nodeType.getToscaResourceName())) - && !isNodeTypeUpgradeSucceeded((Resource) getRes.left().value())) { - return StorageOperationStatus.GENERAL_ERROR; - } - for (GraphVertex chV : derivedResourcesUid) { - result = upgradeNodeType(chV, allCertifiedUids); - log.info("Upgrade node type with name {}, invariantUUID {}, version {} has been finished with the status {}", chV.getMetadataProperty(GraphPropertyEnum.NAME), chV.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID), chV.getMetadataProperty(GraphPropertyEnum.VERSION), result); - } - return result; - } - - private boolean isNodeTypeUpgradeSucceeded(Resource nodeType) { - log.info("Starting to perform check out of node type with name {}, invariantUUID {}, version {}. ", nodeType.getName(), nodeType.getInvariantUUID(), nodeType.getVersion()); - Either checkouRes = - lifecycleBusinessLogic.changeComponentState(nodeType.getComponentType(), nodeType.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT, changeInfo, true, false); - if (checkouRes.isRight()) { - log.info("Failed to check out node type with name {}, invariantUUID {} due to {}", nodeType.getName(), nodeType.getInvariantUUID(), checkouRes.right().value()); - return false; - } - if (performFullCertification(checkouRes.left().value()).isLeft()) { - upgradedNodeTypesMap.put(nodeType.getToscaResourceName(), checkouRes.left().value()); - janusGraphDao.commit(); - return true; - } - return false; - } - - private List getAllDerivedGraphVertices(List allCertifiedUids, Either, JanusGraphOperationStatus> parentResources) { - List derivedResourcesUid = new ArrayList<>(); - - if (parentResources.isLeft()) { - for (GraphVertex chV : parentResources.left().value()) { - Optional op = allCertifiedUids.stream().filter(id -> id.equals((String) chV.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID))).findAny(); - if (op.isPresent()) { - derivedResourcesUid.add(chV); - } - } - } - return derivedResourcesUid; - } - - private Either performFullCertification(org.openecomp.sdc.be.model.Component component) { - log.info("Starting to perform full certification of {} with name {}, invariantUUID {}, version {}. ", - component.getComponentType().getValue(), component.getName(), component.getInvariantUUID(), component.getVersion()); - org.openecomp.sdc.be.model.Component updatedComponent = component; - Either changeStateEither; - changeStateEither = lifecycleBusinessLogic.changeComponentState(component.getComponentType(), updatedComponent.getUniqueId(), user, LifeCycleTransitionEnum.CERTIFY, changeInfo, true, false); - if (changeStateEither.isRight()) { - log.info(FAILED_TO_CHANGE_STATE_OF_COMPONENT, component.getName(), component.getInvariantUUID(), component.getVersion(), LifeCycleTransitionEnum.CERTIFY); - } else { - log.info("Full certification of {} with name {}, invariantUUID {}, version {} finished successfully", - changeStateEither.left().value().getComponentType().getValue(), changeStateEither.left().value().getName(), - changeStateEither.left().value().getInvariantUUID(), changeStateEither.left().value().getVersion()); - } - return changeStateEither; - } - - private Either, JanusGraphOperationStatus> findDerivedResources(String parentResource) { - Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); - propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); - - propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, parentResource); - propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); - - return janusGraphDao - .getByCriteria(VertexTypeEnum.NODE_TYPE, propertiesToMatch, JsonParseFlagEnum.ParseMetadata); - } - - private boolean latestVersionExists(GraphVertex latestDerivedFrom, String currentVersion) { - return isGreater((String) latestDerivedFrom.getJsonMetadataField(JsonPresentationFields.VERSION), currentVersion); - } - - private boolean isGreater(String latestVersion, String currentVersion) { - if (latestVersion != null && currentVersion == null) { - return true; - } - if (latestVersion == null) { - return false; - } - return Double.parseDouble(latestVersion) > Double.parseDouble(currentVersion); - } - - private Either, JanusGraphOperationStatus> getAllLatestCertifiedComponentUids(VertexTypeEnum vertexType, ComponentTypeEnum componentType) { - log.info("Starting to fetch all latest certified not checked out components with type {} upon upgrade migration 1710 process", componentType); - Either, JanusGraphOperationStatus> result = null; - Map latestCertifiedMap = new HashMap<>(); - Map latestNotCertifiedMap = new HashMap<>(); - - Either, JanusGraphOperationStatus> getComponentsRes = getAllLatestComponents(vertexType, componentType); - if (getComponentsRes.isRight() && getComponentsRes.right().value() != JanusGraphOperationStatus.NOT_FOUND) { - log.error("Failed to fetch all latest certified not checked out components with type {}. Status is {}. ", componentType, getComponentsRes.right().value()); - result = Either.right(getComponentsRes.right().value()); - } - if (getComponentsRes.isRight()) { - result = Either.left(new ArrayList<>()); - } - if (result == null) { - for (GraphVertex component : getComponentsRes.left().value()) { - String invariantUUID = (String) component.getJsonMetadataField(JsonPresentationFields.INVARIANT_UUID); - if (((String) component.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals(LifecycleStateEnum.CERTIFIED.name())) { - latestCertifiedMap.put(invariantUUID, (String) component.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID)); - } else { - latestNotCertifiedMap.put(invariantUUID, (String) component.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID)); - } - } - result = Either.left(latestCertifiedMap.entrySet().stream().filter(e -> !latestNotCertifiedMap.containsKey(e.getKey())).map(Map.Entry::getValue).collect(Collectors.toList())); - } - return result; - } - - private Either, JanusGraphOperationStatus> getAllLatestComponents(VertexTypeEnum vertexType, ComponentTypeEnum componentType) { - - Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); - propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name()); - propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); - - - Map propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class); - propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); - if (vertexType == VertexTypeEnum.TOPOLOGY_TEMPLATE && componentType == ComponentTypeEnum.RESOURCE) { - propertiesNotToMatch.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.CVFC.name()); - } - return janusGraphDao - .getByCriteria(vertexType, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata); - } - - private Either, StorageOperationStatus> getLatestByName(GraphPropertyEnum property, String nodeName) { - - Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); - Map propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class); - - propertiesToMatch.put(property, nodeName); - propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); - - Either, JanusGraphOperationStatus> highestResources = janusGraphDao - .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata); - if (highestResources.isRight()) { - JanusGraphOperationStatus status = highestResources.right().value(); - log.debug("Failed to fetch resource with name {}. Status is {} ", nodeName, status); - return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)); - } - List resources = highestResources.left().value(); - List result = new ArrayList<>(); - for (GraphVertex component : resources) { - if (((String) component.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals(LifecycleStateEnum.CERTIFIED.name())) { - result.add(component); - } - } - return Either.left(result); - } - - private void deleteMarkedComponents(NodeTypeEnum componentType, int toBeDeleted) { - Map, ResponseFormat>> cleanComponentsResult; - List cleanComponents = new ArrayList<>(); - cleanComponents.add(componentType); - try { - log.info("Trying to delete {} components of type {} marked as deleted", toBeDeleted, componentType); - cleanComponentsResult = componentsCleanBusinessLogic.cleanComponents(cleanComponents, true); - logDeleteResult(componentType, cleanComponentsResult.get(componentType)); - } - catch (Exception e) { - log.error("Exception occurred {}", e.getMessage()); - log.debug("Exception occurred", e); - } - } - - private void logDeleteResult(NodeTypeEnum type, Either, ResponseFormat> deleteResult) { - if (deleteResult == null) { - return; - } - if (deleteResult.isLeft()) { - log.info("Checked out {} versions are deleted successfully", type.getName()); - } - else { - log.info("Cleanup of checked out {} versions failed due to the error: {}", type.getName(), deleteResult.right().value().getFormattedMessage()); - } - } - - private void markCheckedOutResourceAsDeletedIfUpgradeFailed(String certUid, boolean isNotFailed) { - String checkedOutUniqueId = certifiedToNextCheckedOutUniqueId.remove(certUid); - if (!isNotFailed && checkedOutUniqueId != null) { - try { - //mark as deleted the checked out resource as this upgrade failed - ResponseFormat respFormat = resourceBusinessLogic.deleteResource(checkedOutUniqueId.toLowerCase(), user); - log.info("Checked out resource uniqueId = {} is marked as deleted, status: {}", checkedOutUniqueId, respFormat.getFormattedMessage()); - deleteResourcesIfLimitIsReached(); - } - catch (Exception e) { - log.error("Error occurred:", e); - } - } - } - - private void markCheckedOutServiceAsDeletedIfUpgradeFailed(String certUid, boolean isNotFailed) { - String checkedOutUniqueId = certifiedToNextCheckedOutUniqueId.remove(certUid); - if (!isNotFailed && checkedOutUniqueId != null) { - try { - //delete the checked out resource as this upgrade failed - ResponseFormat respFormat = serviceBusinessLogic.deleteService(checkedOutUniqueId.toLowerCase(), user); - log.info("Checked out service uniqueId = {} is marked as deleted, status: {}", checkedOutUniqueId, respFormat.getFormattedMessage()); - deleteServicesIfLimitIsReached(); - } catch (Exception e) { - log.error("Error occurred:", e); - } - } - - } - - void deleteResourcesIfLimitIsReached() { - markedAsDeletedResourcesCnt++; - if (markedAsDeletedResourcesCnt >= maxDeleteComponents) { - deleteMarkedComponents(NodeTypeEnum.Resource, markedAsDeletedResourcesCnt); - markedAsDeletedResourcesCnt = 0; - } - } - - void deleteServicesIfLimitIsReached() { - markedAsDeletedServicesCnt++; - if (markedAsDeletedServicesCnt >= maxDeleteComponents) { - deleteMarkedComponents(NodeTypeEnum.Service, markedAsDeletedServicesCnt); - markedAsDeletedServicesCnt = 0; - } - } - - boolean isLockDeleteOperationSucceeded() { - StorageOperationStatus status = componentsCleanBusinessLogic.lockDeleteOperation(); - - switch(status) { - case OK: - log.info("Lock delete operation succeeded"); - isCleanupLocked = true; - break; - case FAILED_TO_LOCK_ELEMENT: - log.info("Delete operation node is already locked"); - isCleanupLocked = isLockRetrySucceeded(); - break; - default: - log.error("Lock delete operation failed due to the error: {}", status); - isCleanupLocked = false; - break; - } - return isCleanupLocked; - } - - private boolean isLockRetrySucceeded() { - long startTime = System.currentTimeMillis(); - //try to lock the cleanup resource until configurable time interval is finished - while (System.currentTimeMillis() - startTime <= deleteLockTimeoutInSeconds * 1000) { - try { - //sleep one second and try lock again - Thread.sleep(1000); - if (componentsCleanBusinessLogic.lockDeleteOperation() == StorageOperationStatus.OK) { - return true; - } - } catch (InterruptedException e) { - log.error("Error occurred: {}", e.getMessage()); - Thread.currentThread().interrupt(); - } - } - return false; - } - - void unlockDeleteOperation() { - if (isCleanupLocked) { - try { - componentsCleanBusinessLogic.unlockDeleteOperation(); - log.info("Lock delete operation is canceled"); - isCleanupLocked = false; - } - catch (Exception e) { - log.debug("Failed to unlock delete operation", e); - log.error("Failed to unlock delete operation due to the error {}", e.getMessage()); - } - } - } - - private Either checkOutComponent(org.openecomp.sdc.be.model.Component component) { - log.info("Starting to perform check out of {} {}, uniqueId = {}", component.getComponentType(), component.getName(), component.getUniqueId()); - Either checkoutRes = - lifecycleBusinessLogic.changeComponentState(component.getComponentType(), component.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT, changeInfo, true, false); - if (checkoutRes.isLeft()) { - //add the uniqueId from "upgradeVf(String uniqueId)" and checkouRes's uniqueUID to the new map - certifiedToNextCheckedOutUniqueId.put(component.getUniqueId(), checkoutRes.left().value().getUniqueId()); - log.debug("Add checked out component uniqueId = {} produced from certified component uniqueId = {} to the checked out map", checkoutRes.left().value().getUniqueId(), component.getUniqueId()); - } - return checkoutRes; - } - - UpgradeStatus getVfUpgradeStatus(boolean isUpgraded, boolean isInstance) { - if (isUpgraded) { - return isInstance ? UpgradeStatus.UPGRADED_AS_INSTANCE : UpgradeStatus.UPGRADED; - } - return UpgradeStatus.NOT_UPGRADED; - } - -} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1902/SdcGroupsMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1902/SdcGroupsMigration.java deleted file mode 100644 index 1045634ef9..0000000000 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1902/SdcGroupsMigration.java +++ /dev/null @@ -1,186 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2020 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.migration.tasks.mig1902; - -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.tasks.InstanceMigrationBase; -import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; -import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao; -import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum; -import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; -import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; -import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; -import org.openecomp.sdc.be.model.GroupTypeDefinition; -import org.openecomp.sdc.be.model.PropertyDefinition; -import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; -import org.openecomp.sdc.be.model.operations.impl.GroupTypeOperation; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - -import java.math.BigInteger; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -@Component -public class SdcGroupsMigration extends InstanceMigrationBase implements Migration { - - private static final Logger log = LoggerFactory.getLogger(SdcGroupsMigration.class); - - private final GroupTypeOperation groupTypeOperation; - - private Map latestGroupTypeMap = new HashMap<>(); - - public enum GroupsForUpgrade { - NW_COLLECTION_GROUP_NAME("org.openecomp.groups.NetworkCollection"), - VFC_INSTANCE_GROUP_NAME("org.openecomp.groups.VfcInstanceGroup"); - - private String toscaType; - - GroupsForUpgrade(String toscaType) { - this.toscaType = toscaType; - } - - public static boolean containsToscaType(String type) { - try { - return Arrays.stream(values()).anyMatch(g->g.getToscaType().equals(type)); - } - catch (IllegalArgumentException ex) { - return false; - } - } - - public String getToscaType() { - return toscaType; - } - - } - public SdcGroupsMigration(JanusGraphDao janusGraphDao, GroupTypeOperation groupTypeOperation) { - super(janusGraphDao); - this.groupTypeOperation = groupTypeOperation; - } - - @Override - public String description() { - return "update derived from field value for NetworkCollection and VfcInstanceGroup group instances "; - } - - @Override - public DBVersion getVersion() { - return DBVersion.from(BigInteger.valueOf(1902), BigInteger.valueOf(0)); - } - - @Override - public MigrationResult migrate() { - loadLatestGroupTypeDefinitions(); - StorageOperationStatus status = upgradeTopologyTemplates(); - return status == StorageOperationStatus.OK ? - MigrationResult.success() : MigrationResult.error("failed to update derived from value for NetworkCollection and VfcInstanceGroup group instances. Error : " + status); - } - - void loadLatestGroupTypeDefinitions() { - Arrays.stream(GroupsForUpgrade.values()).forEach(this::getLatestGroupTypeDefinition); - } - - @Override - protected StorageOperationStatus handleOneContainer(GraphVertex containerVorig) { - StorageOperationStatus status = StorageOperationStatus.NOT_FOUND; - GraphVertex containerV = getVertexById(containerVorig.getUniqueId()); - - try { - status = janusGraphDao.getChildVertex(containerV, EdgeLabelEnum.GROUPS, JsonParseFlagEnum.ParseAll) - .either(this::updateGroupPropertiesIfRequired, this::handleError); - } - catch (Exception e) { - log.error("Exception occurred:", e); - status = StorageOperationStatus.GENERAL_ERROR; - } - finally { - if (status != StorageOperationStatus.OK) { - janusGraphDao.rollback(); - if (status == StorageOperationStatus.NOT_FOUND) { - //it is happy flow as well - status = StorageOperationStatus.OK; - } - } - if (log.isInfoEnabled()) { - log.info("Upgrade status is <{}> for topology template <{}> uniqueId <{}>", - status.name(), containerV.getMetadataProperties().get(GraphPropertyEnum.NAME), - containerV.getMetadataProperties().get(GraphPropertyEnum.UNIQUE_ID)); - } - } - return status; - } - - private StorageOperationStatus updateGroupPropertiesIfRequired(GraphVertex vertex) { - StorageOperationStatus status = StorageOperationStatus.NOT_FOUND; - boolean isUpdated = false; - Map groupDefinitionMap = (Map) vertex.getJson(); - for (GroupDataDefinition groupDef : groupDefinitionMap.values()) { - if (GroupsForUpgrade.containsToscaType(groupDef.getType())) { - if (log.isDebugEnabled()) { - log.debug("Group instance named <{}> of type <{}> is supposed to be updated on vertex <{}>", - groupDef.getName(), groupDef.getType(), vertex.getUniqueId()); - } - isUpdated = isGroupPropertiesUpdateDone(groupDef.getProperties(), latestGroupTypeMap.get(groupDef.getType()).getProperties()); - if (log.isDebugEnabled()) { - String result = isUpdated ? "has been updated" : "is up to date "; - log.debug("Group instance named <{}> of type <{}> uniqueID <{}> {} on vertex <{}>", - groupDef.getName(), groupDef.getType(), groupDef.getUniqueId(), result, vertex.getUniqueId()); - } - } - } - if (isUpdated) { - vertex.setJson(groupDefinitionMap); - status = updateVertexAndCommit(vertex); - if (status == StorageOperationStatus.OK && log.isDebugEnabled()) { - log.debug("Group properties change is committed on vertex <{}>", vertex.getUniqueId()); - } - } - return status; - } - - private boolean isGroupPropertiesUpdateDone(List curPropDefList, List latestGroupDefList) { - boolean isUpdated = false; - for (PropertyDefinition prop: latestGroupDefList) { - if (curPropDefList.stream().noneMatch(l->l.getName().equals(prop.getName()))) { - curPropDefList.add(prop); - isUpdated = true; - } - } - return isUpdated; - } - - StorageOperationStatus getLatestGroupTypeDefinition(GroupsForUpgrade groupsForUpgrade) { - return groupTypeOperation.getLatestGroupTypeByType(groupsForUpgrade.getToscaType(), false) - .either(g-> { - latestGroupTypeMap.put(groupsForUpgrade.getToscaType(), g); - return StorageOperationStatus.OK; - }, err->err); - } - - -} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1902/SdcResourceIconMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1902/SdcResourceIconMigration.java deleted file mode 100644 index 837abf1c08..0000000000 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/tasks/mig1902/SdcResourceIconMigration.java +++ /dev/null @@ -1,187 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2020 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.migration.tasks.mig1902; - -import com.google.common.annotations.VisibleForTesting; -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.tasks.InstanceMigrationBase; -import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; -import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao; -import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; -import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; -import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.CompositionDataDefinition; -import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; -import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; -import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; -import org.openecomp.sdc.be.model.jsonjanusgraph.enums.JsonConstantKeysEnum; -import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; -import org.openecomp.sdc.common.log.wrappers.Logger; -import org.springframework.stereotype.Component; - -import java.math.BigInteger; -import java.util.EnumMap; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -@Component -public class SdcResourceIconMigration extends InstanceMigrationBase implements Migration { - - private static final Logger log = Logger.getLogger(SdcResourceIconMigration.class); - - private Map resourceTypeToIconMap = new HashMap<>(); - - @VisibleForTesting - SdcResourceIconMigration(JanusGraphDao janusGraphDao) { - super(janusGraphDao); - } - - - @Override - public String description() { - return "update iconPath for VL and CP nodes"; - } - - @Override - public DBVersion getVersion() { - return DBVersion.from(BigInteger.valueOf(1902), BigInteger.valueOf(0)); - } - - @Override - public MigrationResult migrate() { - StorageOperationStatus status; - try { - updateNodeTypeIconAndStoreInMap(ResourceTypeEnum.VL); - updateNodeTypeIconAndStoreInMap(ResourceTypeEnum.CP); - - if (!resourceTypeToIconMap.isEmpty()) { - status = upgradeTopologyTemplates(); - } else { - log.error("No VL and CP node definitions found"); - status = StorageOperationStatus.NOT_FOUND; - } - } - catch(Exception e) { - log.error("Exception thrown: {}", e); - status = StorageOperationStatus.GENERAL_ERROR; - } - return status == StorageOperationStatus.OK ? - MigrationResult.success() : MigrationResult.error("failed to update iconPath for VL and CP nodes. Error : " + status); - } - - @Override - protected StorageOperationStatus handleOneContainer(GraphVertex containerVorig) { - StorageOperationStatus status = StorageOperationStatus.OK; - GraphVertex containerV = getVertexById(containerVorig.getUniqueId()); - - Map jsonComposition = (Map)containerV.getJson(); - if (jsonComposition != null && !jsonComposition.isEmpty()) { - CompositionDataDefinition compositionDataDefinition = jsonComposition.get(JsonConstantKeysEnum.COMPOSITION.getValue()); - Map componentInstances = compositionDataDefinition.getComponentInstances(); - - long updateCount = componentInstances.values() - .stream() - .filter(this::updateIconInsideInstance).count(); - if (updateCount > 0) { - status = updateVertexAndCommit(containerV); - } - } - else { - log.warn("No json found for template <{}> uniqueId <{}>", - containerV.getMetadataProperties().get(GraphPropertyEnum.NAME), - containerV.getMetadataProperties().get(GraphPropertyEnum.UNIQUE_ID)); - } - if (log.isInfoEnabled()) { - log.info("Upgrade status is <{}> for topology template <{}> uniqueId <{}>", - status.name(), containerV.getMetadataProperties().get(GraphPropertyEnum.NAME), - containerV.getMetadataProperties().get(GraphPropertyEnum.UNIQUE_ID)); - } - return status; - } - - - @VisibleForTesting - boolean updateIconInsideInstance(ComponentInstanceDataDefinition componentInstanceDataDefinition) { - String iconPath = resourceTypeToIconMap.get(componentInstanceDataDefinition.getComponentName()); - if (iconPath != null) { - componentInstanceDataDefinition.setIcon(iconPath); - if (log.isDebugEnabled()) { - log.debug("Icon of component {} is set to {}", componentInstanceDataDefinition.getComponentName(), iconPath); - } - return true; - } - return false; - } - - @VisibleForTesting - void updateNodeTypeIconAndStoreInMap(ResourceTypeEnum resourceType) { - Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); - Map propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class); - - propertiesToMatch.put(GraphPropertyEnum.RESOURCE_TYPE, resourceType.name()); - propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); - - propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); - - String iconPath = String.valueOf(resourceType.getValue()).toLowerCase(); - - Map resourceNameToIconMap = janusGraphDao.getByCriteria(VertexTypeEnum.NODE_TYPE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseAll) - .either(vl-> updateIconResource(vl, iconPath), status->null); - - if (resourceNameToIconMap != null) { - resourceTypeToIconMap.putAll(resourceNameToIconMap); - } - else { - log.warn("Failed to get resources of type <{}>", resourceType.name()); - } - } - - private Map updateIconResource(List vertexList, String iconPath) { - if (vertexList.isEmpty()) { - return null; - } - Map nameToIconMap = new HashMap<>(); - vertexList.forEach(v->{ - StorageOperationStatus status = updateIconOnVertex(v, iconPath); - if (status == StorageOperationStatus.OK) { - if (log.isDebugEnabled()) { - log.debug("Node type's {} icon is updated to {}", v.getMetadataProperty(GraphPropertyEnum.NAME), iconPath); - } - nameToIconMap.put(String.valueOf(v.getMetadataProperty(GraphPropertyEnum.NAME)), iconPath); - } - else { - log.error("Failed to update node type {} icon due to a reason: {}", - v.getMetadataProperty(GraphPropertyEnum.NAME), status); - throw new RuntimeException("Node update failure"); - } - }); - return nameToIconMap; - } - - private StorageOperationStatus updateIconOnVertex(GraphVertex vertex, String iconPath) { - vertex.setJsonMetadataField(JsonPresentationFields.ICON, iconPath); - return updateVertexAndCommit(vertex); - } - -} diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/utils/ValidationTaskResultTest.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/utils/ValidationTaskResultTest.java deleted file mode 100644 index afbda6e098..0000000000 --- a/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/validator/utils/ValidationTaskResultTest.java +++ /dev/null @@ -1,98 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2019 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.validator.utils; - -import org.junit.Test; -import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; - - -public class ValidationTaskResultTest { - - private ValidationTaskResult createTestSubject() { - return new ValidationTaskResult(new GraphVertex(), "", "", false); - } - - - @Test - public void testGetName() { - ValidationTaskResult testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getName(); - } - - - @Test - public void testSetName() { - ValidationTaskResult testSubject; - String name = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setName(name); - } - - - @Test - public void testGetResultMessage() { - ValidationTaskResult testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getResultMessage(); - } - - - @Test - public void testSetResultMessage() { - ValidationTaskResult testSubject; - String resultMessage = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setResultMessage(resultMessage); - } - - - @Test - public void testIsSuccessful() { - ValidationTaskResult testSubject; - boolean result; - - // default test - testSubject = createTestSubject(); - result = testSubject.isSuccessful(); - } - - - @Test - public void testSetSuccessful() { - ValidationTaskResult testSubject; - boolean successful = false; - - // default test - testSubject = createTestSubject(); - testSubject.setSuccessful(successful); - } -} diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/tasks/mig1710/UpgradeMigration1710Test.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/tasks/mig1710/UpgradeMigration1710Test.java deleted file mode 100644 index 8763a1bf68..0000000000 --- a/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/tasks/mig1710/UpgradeMigration1710Test.java +++ /dev/null @@ -1,648 +0,0 @@ -/*- - * ============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========================================================= - * Modifications copyright (c) 2019 Nokia - * ================================================================================ - */ -package org.openecomp.sdc.asdctool.migration.tasks.mig1710; - -import com.google.common.collect.Lists; -import fj.data.Either; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; -import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult; -import org.openecomp.sdc.asdctool.migration.tasks.handlers.XlsOutputHandler; -import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic; -import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic; -import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException; -import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic; -import org.openecomp.sdc.be.components.scheduledtasks.ComponentsCleanBusinessLogic; -import org.openecomp.sdc.be.config.Configuration; -import org.openecomp.sdc.be.config.ConfigurationManager; -import org.openecomp.sdc.be.dao.api.ActionStatus; -import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; -import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; -import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao; -import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum; -import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; -import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; -import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; -import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; -import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum; -import org.openecomp.sdc.be.impl.ComponentsUtils; -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.LifecycleStateEnum; -import org.openecomp.sdc.be.model.Resource; -import org.openecomp.sdc.be.model.Service; -import org.openecomp.sdc.be.model.User; -import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; -import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; -import org.openecomp.sdc.be.model.operations.impl.CsarOperation; -import org.openecomp.sdc.be.model.operations.impl.UserAdminOperation; -import org.openecomp.sdc.common.api.ConfigurationSource; -import org.openecomp.sdc.common.http.client.api.HttpRequestHandler; -import org.openecomp.sdc.exception.ResponseFormat; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import static org.junit.Assert.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@RunWith(MockitoJUnitRunner.class) -public class UpgradeMigration1710Test { - - private static final String USER = "jh0003"; - private static final String CONF_LEVEL = "5.0"; - private static final String COMPONENT_UNIQUE_ID = "12345"; - private static final String OLD_VERSION = "1.0"; - private static final String UPDATED_VERSION = "2.0"; - private static final String CSAR_UUID = "1234578"; - private static HttpRequestHandler originHandler; - - private final User user = new User(); - - @InjectMocks - private UpgradeMigration1710 migration = new UpgradeMigration1710(); - @Mock - private UserAdminOperation userAdminOperation; - @Mock - private ToscaOperationFacade toscaOperationFacade; - @Mock - private LifecycleBusinessLogic lifecycleBusinessLogic; - @Mock - private JanusGraphDao janusGraphDao; - @Mock - private ComponentsUtils componentUtils; - @Mock - private CsarOperation csarOperation; - @Mock - private ConfigurationSource configurationSource; - //don't remove - it is intended to avoid the xls file generating - @Mock - private XlsOutputHandler outputHandler; - @Mock - private ResourceBusinessLogic resourceBusinessLogic; - @Mock - private ServiceBusinessLogic serviceBusinessLogic; - @Mock - private ResponseFormat responseFormat; - @Mock - private ComponentsCleanBusinessLogic componentsCleanBusinessLogic; - - private static ConfigurationManager configurationManager; - private static List resources = Stream.of("org.openecomp.resource.cp.extCP").collect(Collectors.toList()); - private static Map> resourcesForUpgrade; - private static Configuration.EnvironmentContext environmentContext = new Configuration.EnvironmentContext(); - - private Resource resource; - private Service service; - private List vfList = new ArrayList<>(); - - @BeforeClass - public static void setUpClass() { - resourcesForUpgrade = new HashMap<>(); - resourcesForUpgrade.put(CONF_LEVEL, resources); - originHandler = HttpRequestHandler.get(); - } - - @AfterClass - public static void tearDownClass() { - //put the origin handler back - HttpRequestHandler.setTestInstance(originHandler); - } - - @Before - public void setUp() { - user.setUserId(USER); - configurationManager = new ConfigurationManager(configurationSource); - configurationManager.setConfiguration(new Configuration()); - configurationManager.getConfiguration().setSkipUpgradeVSPs(true); - configurationManager.getConfiguration().setSkipUpgradeFailedVfs(true); - configurationManager.getConfiguration().setAutoHealingOwner(USER); - configurationManager.getConfiguration().setSupportAllottedResourcesAndProxy(true); - configurationManager.getConfiguration().setDeleteLockTimeoutInSeconds(10); - configurationManager.getConfiguration().setMaxDeleteComponents(5); - configurationManager.getConfiguration().setEnableAutoHealing(true); - configurationManager.getConfiguration().setToscaConformanceLevel("5.0"); - environmentContext.setDefaultValue("General_Revenue-Bearing"); - configurationManager.getConfiguration().setEnvironmentContext(environmentContext); - HashMap> resourcesForUpgrade = new HashMap(); - resourcesForUpgrade.put("5.0", Lists.newArrayList("port")); - configurationManager.getConfiguration().setResourcesForUpgrade(resourcesForUpgrade); - - migration.init(); - migration.setNodeTypesSupportOnly(false); - when(componentsCleanBusinessLogic.lockDeleteOperation()).thenReturn(StorageOperationStatus.OK); - - resource = new Resource(); - resource.setCsarUUID(CSAR_UUID); - resource.setVersion(OLD_VERSION); - resource.setUniqueId(COMPONENT_UNIQUE_ID); - - service = new Service(); - service.setVersion(OLD_VERSION); - service.setUniqueId(COMPONENT_UNIQUE_ID); - - vfList.add(COMPONENT_UNIQUE_ID); - - when(responseFormat.getFormattedMessage()) - .thenReturn(""); - when(componentUtils.convertFromStorageResponse(any(), any())).thenCallRealMethod(); - mockChangeComponentState(); - } - - @Test - public void nodeTypesUpgradeFailed() { - migration.setNodeTypesSupportOnly(true); - resolveUserAndDefineUpgradeLevel(); - when(janusGraphDao.getByCriteria(any(), any(), any(), any())) - .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)); - assertEquals(MigrationResult.MigrationStatus.FAILED, migration.migrate().getMigrationStatus()); - } - - @Test - public void migrationDisabled() { - configurationManager.getConfiguration().setEnableAutoHealing(false); - migration.init(); - assertEquals(MigrationResult.MigrationStatus.COMPLETED, migration.migrate().getMigrationStatus()); - verify(janusGraphDao, times(0)).commit(); - verify(janusGraphDao, times(0)).rollback(); - } - - @Test - public void migrationFailedIfDeleteNodeLockFailed() { - when(componentsCleanBusinessLogic.lockDeleteOperation()) - .thenReturn(StorageOperationStatus.BAD_REQUEST); - assertEquals(MigrationResult.MigrationStatus.FAILED, migration.migrate().getMigrationStatus()); - } - - @Test - public void migrationFailedIfDeleteNodeLockRetryFailed() { - when(componentsCleanBusinessLogic.lockDeleteOperation()) - .thenReturn(StorageOperationStatus.FAILED_TO_LOCK_ELEMENT) - .thenReturn(StorageOperationStatus.BAD_REQUEST); - assertEquals(MigrationResult.MigrationStatus.FAILED, migration.migrate().getMigrationStatus()); - } - - @Test - public void nodeTypesOnlyUpgradePassed() { - migration.setNodeTypesSupportOnly(true); - upgradeAllScenario(false); - assertEquals(MigrationResult.MigrationStatus.COMPLETED, migration.migrate().getMigrationStatus()); - verify(janusGraphDao, times(2)).commit(); - verify(janusGraphDao, times(0)).rollback(); - } - - @Test - public void nodeTypesUpgradePassedAndVFsUpgradeFailedWhenSkipFailedVFsIsNotSupported() { - final boolean failOnVfUpgrade = true; - final boolean upgradeServices = false; - final boolean exceptionOnVfUpgrade = false; - final boolean upgradeVFC = false; - final boolean isFailed = true; - configurationManager.getConfiguration().setSkipUpgradeFailedVfs(false); - migration.init(); - migration.setNodeTypesSupportOnly(false); - resolveUserAndDefineUpgradeLevel(); - upgradeRules(failOnVfUpgrade, exceptionOnVfUpgrade, upgradeServices, upgradeVFC, isFailed); - assertEquals(MigrationResult.MigrationStatus.FAILED, migration.migrate().getMigrationStatus()); - verify(janusGraphDao, times(1)).commit(); - verify(janusGraphDao, times(2)).rollback(); - } - - - @Test - public void upgradeAllVFsUpgradeFailedOnExceptionWhenSkipFailedVFsIsNotSupported() { - final boolean failOnVfUpgrade = false; - final boolean upgradeServices = false; - final boolean exceptionOnVfUpgrade = true; - final boolean upgradeVFC = false; - final boolean isFailed = true; - configurationManager.getConfiguration().setSkipUpgradeFailedVfs(false); - resolveUserAndDefineUpgradeLevel(); - upgradeRules(failOnVfUpgrade, exceptionOnVfUpgrade, upgradeServices, upgradeVFC, isFailed); - migration.init(); - assertEquals(MigrationResult.MigrationStatus.COMPLETED, migration.migrate().getMigrationStatus()); - verify(janusGraphDao, times(2)).commit(); - verify(janusGraphDao, times(0)).rollback(); - } - - @Test - public void upgradeAllIfVFsUpgradeFailedOnExceptionWhenSkipFailedVFsIsSupported() { - final boolean failOnVfUpgrade = false; - final boolean upgradeServices = true; - final boolean exceptionOnFvUpgrade = true; - final boolean upgradeVFC = false; - final boolean isFailed = false; - configurationManager.getConfiguration().setSkipUpgradeFailedVfs(true); - resolveUserAndDefineUpgradeLevel(); - upgradeRules(failOnVfUpgrade, exceptionOnFvUpgrade, upgradeServices, upgradeVFC, isFailed); - assertEquals(MigrationResult.MigrationStatus.COMPLETED, migration.migrate().getMigrationStatus()); - verify(janusGraphDao, times(3)).commit(); - verify(janusGraphDao, times(1)).rollback(); - } - - - @Test - public void upgradeAll() { - upgradeAllScenario(true); - assertEquals(MigrationResult.MigrationStatus.COMPLETED, migration.migrate().getMigrationStatus()); - verify(janusGraphDao, times(4)).commit(); - verify(janusGraphDao, times(0)).rollback(); - } - - @Test - public void upgradeAllWhenDeleteLockRetrySucceeded() { - when(componentsCleanBusinessLogic.lockDeleteOperation()) - .thenReturn(StorageOperationStatus.FAILED_TO_LOCK_ELEMENT) - .thenReturn(StorageOperationStatus.OK); - upgradeAllScenario(true); - assertEquals(MigrationResult.MigrationStatus.COMPLETED, migration.migrate().getMigrationStatus()); - verify(janusGraphDao, times(4)).commit(); - verify(janusGraphDao, times(0)).rollback(); - } - - @Test - public void upgradeAllWhenVspUpgradeIsRequired() { - final boolean failOnVfUpgrade = false; - final boolean upgradeServices = true; - final boolean exceptionOnFvUpgrade = false; - final boolean upgradeVFC = true; - final boolean isFailed = true; - resolveUserAndDefineUpgradeLevel(); - upgradeRules(failOnVfUpgrade, exceptionOnFvUpgrade, upgradeServices, upgradeVFC, isFailed); - configurationManager.getConfiguration().setSkipUpgradeVSPs(false); - assertEquals(MigrationResult.MigrationStatus.COMPLETED, migration.migrate().getMigrationStatus()); - } - - @Test - public void migrationFailedWhenUserNotResolved() { - when(userAdminOperation.getUserData(anyString(), eq(false))).thenReturn(Either.right(ActionStatus.MISSING_INFORMATION)); - when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK); - assertEquals(MigrationResult.MigrationStatus.FAILED, migration.migrate().getMigrationStatus()); - } - - @Test - public void verifyThatCheckedOutResourcesMarkedAsDeletedIfUpgradeFailed() { - mockCheckoutFlow(); - when(resourceBusinessLogic.validateAndUpdateResourceFromCsar(any(Resource.class), any(), any(), any(), - any())) - .thenThrow(new ByResponseFormatComponentException(responseFormat)); - when(resourceBusinessLogic.deleteResource(anyString(), any())) - .thenReturn(responseFormat); - mockChangeComponentState(); - migration.upgradeVFs(vfList, false); - verify(resourceBusinessLogic).deleteResource(anyString(), any()); - } - - @Test - public void verifyThatCheckedOutAllottedResourcesMarkedAsDeletedIfUpgradeFailed() { - mockCheckoutFlow(); - when(resourceBusinessLogic.validateAndUpdateResourceFromCsar(any(Resource.class), any(), any(), any(), - any())) - .thenThrow(new ByResponseFormatComponentException(responseFormat)); - when(resourceBusinessLogic.deleteResource(anyString(), any())) - .thenReturn(responseFormat); - mockChangeComponentState(); - migration.upgradeVFs(vfList, true); - verify(resourceBusinessLogic).deleteResource(anyString(), any()); - } - - @Test - public void verifyThatCheckedOutResourceIsNotMarkedAsDeletedIfUpgradeSucceeded() { - mockCheckoutFlow(); - resource.setVersion(UPDATED_VERSION); - when(resourceBusinessLogic.validateAndUpdateResourceFromCsar(any(Resource.class), any(), any(), any(), - any())) - .thenReturn(resource); - mockChangeComponentState(); - migration.upgradeVFs(vfList, true); - verify(resourceBusinessLogic, times(0)).deleteResource(anyString(), any()); - } - - @Test - public void verifyThatCheckedOutServicesMarkedAsDeletedIfUpgradeFailed() { - List servicesForUpgrade = new ArrayList<>(); - servicesForUpgrade.add(COMPONENT_UNIQUE_ID); - - Either foundServices = Either.left(resource); - mockCheckoutFlow(); - when(toscaOperationFacade.getToscaElement(any(), any(ComponentParametersView.class))) - .thenReturn(Either.left(service)); - when(toscaOperationFacade.getLatestCertifiedByToscaResourceName(any(), any(), any())) - .thenReturn(foundServices); - migration.upgradeServices(servicesForUpgrade, component -> true, "services"); - verify(serviceBusinessLogic, times(0)).deleteService(anyString(), any()); - } - - @Test - public void verifyThatCheckedOutServicesIsNotMarkedAsDeletedIfUpgradeSucceeded() { - List servicesForUpgrade = new ArrayList<>(); - servicesForUpgrade.add(COMPONENT_UNIQUE_ID); - - mockCheckoutFlow(); - when(toscaOperationFacade.getLatestCertifiedByToscaResourceName(anyString(), any(VertexTypeEnum.class), any(JsonParseFlagEnum.class))) - .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); - when(serviceBusinessLogic.deleteService(anyString(), any())) - .thenReturn(responseFormat); - migration.upgradeServices(servicesForUpgrade, component -> true, "services"); - verify(serviceBusinessLogic).deleteService(anyString(), any()); - } - - - @Test - public void unlockDeleteOperationIsPerformedIfItWasLocked() { - migration.isLockDeleteOperationSucceeded(); - migration.unlockDeleteOperation(); - verify(componentsCleanBusinessLogic).unlockDeleteOperation(); - } - - @Test - public void unlockDeleteOperationIsNotPerformedIfItWasNotLocked() { - when(componentsCleanBusinessLogic.lockDeleteOperation()).thenReturn(StorageOperationStatus.GENERAL_ERROR); - migration.isLockDeleteOperationSucceeded(); - migration.unlockDeleteOperation(); - verify(componentsCleanBusinessLogic, times(0)).unlockDeleteOperation(); - } - - @Test - public void deleteLockSucceededAfterRetry() { - when(componentsCleanBusinessLogic.lockDeleteOperation()) - .thenReturn(StorageOperationStatus.FAILED_TO_LOCK_ELEMENT) - .thenReturn(StorageOperationStatus.FAILED_TO_LOCK_ELEMENT) - .thenReturn(StorageOperationStatus.FAILED_TO_LOCK_ELEMENT) - .thenReturn(StorageOperationStatus.OK); - migration.isLockDeleteOperationSucceeded(); - migration.unlockDeleteOperation(); - verify(componentsCleanBusinessLogic).unlockDeleteOperation(); - } - - @Test - public void deleteLockFailedAfterRetry() { - when(componentsCleanBusinessLogic.lockDeleteOperation()) - .thenReturn(StorageOperationStatus.FAILED_TO_LOCK_ELEMENT); - migration.isLockDeleteOperationSucceeded(); - migration.unlockDeleteOperation(); - verify(componentsCleanBusinessLogic, times(0)).unlockDeleteOperation(); - } - - @Test - public void deleteMarkedResourcesWhenLimitIsReached() { - ArrayList componentsToClean = new ArrayList<>(); - componentsToClean.add(NodeTypeEnum.Resource); - migration.setUser(user); - migration.setMarkedAsDeletedResourcesCnt(5); - migration.deleteResourcesIfLimitIsReached(); - verify(componentsCleanBusinessLogic).cleanComponents(componentsToClean, true); - } - - @Test - public void deleteMarkedResourcesNotCalledWhenLimitIsNotReached() { - ArrayList componentsToClean = new ArrayList<>(); - componentsToClean.add(NodeTypeEnum.Resource); - migration.setUser(user); - migration.setMarkedAsDeletedResourcesCnt(3); - migration.deleteResourcesIfLimitIsReached(); - verify(componentsCleanBusinessLogic, times(0)).cleanComponents(componentsToClean, true); - } - - @Test - public void deleteMarkedServicesWhenLimitIsReached() { - ArrayList componentsToClean = new ArrayList<>(); - componentsToClean.add(NodeTypeEnum.Service); - migration.setUser(user); - migration.setMarkedAsDeletedServicesCnt(5); - migration.deleteServicesIfLimitIsReached(); - verify(componentsCleanBusinessLogic).cleanComponents(componentsToClean, true); - } - - @Test - public void deleteMarkedServicesNotCalledWhenLimitIsNotReached() { - ArrayList componentsToClean = new ArrayList<>(); - componentsToClean.add(NodeTypeEnum.Service); - migration.setUser(user); - migration.setMarkedAsDeletedServicesCnt(2); - migration.deleteServicesIfLimitIsReached(); - verify(componentsCleanBusinessLogic, times(0)).cleanComponents(componentsToClean, true); - } - - @Test - public void getVfUpgradeStatusWhenUpgradeFailedAndItIsInstance() { - assertEquals(UpgradeMigration1710.UpgradeStatus.NOT_UPGRADED, migration.getVfUpgradeStatus(false, true)); - } - - @Test - public void getVfUpgradeStatusWhenUpgradeFailedAndItIsNotInstance() { - assertEquals(UpgradeMigration1710.UpgradeStatus.NOT_UPGRADED, migration.getVfUpgradeStatus(false, false)); - } - - @Test - public void getVfUpgradeStatusWhenUpgradeSucceededAndItIsInstance() { - assertEquals(UpgradeMigration1710.UpgradeStatus.UPGRADED_AS_INSTANCE, migration.getVfUpgradeStatus(true, true)); - } - - @Test - public void getVfUpgradeStatusWhenUpgradeSucceededAndItIsNotInstance() { - assertEquals(UpgradeMigration1710.UpgradeStatus.UPGRADED, migration.getVfUpgradeStatus(true, false)); - } - - private void resolveUserAndDefineUpgradeLevel() { - when(userAdminOperation.getUserData(anyString(), eq(false))).thenReturn(Either.left(user)); - configurationManager.getConfiguration().setToscaConformanceLevel(CONF_LEVEL); - configurationManager.getConfiguration().setResourcesForUpgrade(resourcesForUpgrade); - } - - private void upgradeAllScenario(boolean upgradeServices) { - final boolean failOnVfUpgrade = false; - final boolean exceptionOnFvUpgrade = false; - final boolean upgradeVFC = false; - final boolean isFailed = false; - final boolean isProxy = true; - - resolveUserAndDefineUpgradeLevel(); - mockCheckoutFlow(); - when(resourceBusinessLogic.validateAndUpdateResourceFromCsar(any(Resource.class), any(), any(), any(), - any())) - .thenReturn(resource); - upgradeRules(failOnVfUpgrade, exceptionOnFvUpgrade, upgradeServices, upgradeVFC, isFailed, isProxy); - } - - private void upgradeRules(boolean failedVfUpgrade, boolean exceptionOnVfUpgrade, boolean upgradeService, - boolean upgradeVFCs, boolean isFailed) { - upgradeRules(failedVfUpgrade, exceptionOnVfUpgrade, upgradeService, upgradeVFCs, isFailed, false); - } - - private void upgradeRules(boolean failedVfUpgrade, boolean exceptionOnVfUpgrade, boolean upgradeService, - boolean upgradeVFCs, boolean isFailed, boolean isProxy) { - - mockNodeTypesUpgrade(); - Either foundResource = Either.left(resource); - - if (failedVfUpgrade) { - getToscaElementMockForVfUpgradeFailedScenario(foundResource); - } else { - if (exceptionOnVfUpgrade) { - getToscaElementMockForExceptionOnUpgradeScenario(foundResource, upgradeService); - } else { - when(toscaOperationFacade.getToscaElement(anyString())) - .thenReturn(foundResource); - } - } - //happy flow - if (upgradeService) { - mockForUpgradeServiceScenario(foundResource, upgradeVFCs, isFailed); - } - } - - private void mockNodeTypesUpgrade() { - GraphVertex component = createComponent(); - List components = Lists.newArrayList(); - components.add(component); - - when(janusGraphDao.getByCriteria(any(), any(), any(), any())) - .thenReturn(Either.left(components)); - when(janusGraphDao.getParentVertices(any(GraphVertex.class), any(EdgeLabelEnum.class), any(JsonParseFlagEnum.class))) - //1th node to upgrade - .thenReturn(Either.left(components)) - //parent of the 1th node - stop recursion - .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)); - } - - private GraphVertex createComponent() { - GraphVertex component = new GraphVertex(); - component.setJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE,LifecycleStateEnum.CERTIFIED.name()); - component.setJsonMetadataField(JsonPresentationFields.UNIQUE_ID,COMPONENT_UNIQUE_ID); - component.setJsonMetadataField(JsonPresentationFields.CI_COMPONENT_VERSION,UPDATED_VERSION); - return component; - } - - private void mockChangeComponentState() { - List instances = Lists.newArrayList(); - instances.add(createComponentInstance()); - - Resource checkedOutResource = new Resource(); - checkedOutResource.setUniqueId("123400"); - checkedOutResource.setComponentInstances(instances); - Either fromLifeCycle = Either.left(checkedOutResource); - doReturn(fromLifeCycle).when(lifecycleBusinessLogic) - .changeComponentState(any(), any(), any(), any(), any(),eq(true), eq(false)); - } - - private void getToscaElementMockForVfUpgradeFailedScenario(Either foundResource) { - when(toscaOperationFacade.getToscaElement(anyString())) - .thenReturn(foundResource) - .thenReturn(foundResource) - .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); - } - - private void mockForUpgradeServiceScenario(Either foundResource, boolean upgradeVFC, boolean isFailed) { - Either foundService = Either.left(resource); - if (upgradeVFC) { - when(toscaOperationFacade.getToscaElement(anyString())) - .thenReturn(foundResource) - .thenReturn(foundResource) - .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); - } - else if (!isFailed) { - when(toscaOperationFacade.getToscaElement(any(), any(ComponentParametersView.class))) - .thenReturn(Either.left(resource)); - when(toscaOperationFacade.getLatestCertifiedByToscaResourceName(any(), any(), any())) - .thenReturn(foundService); - } - } - - private void getToscaElementMockForExceptionOnUpgradeScenario(Either foundResource, boolean upgradeService) { - if (upgradeService) { - service.setVersion(UPDATED_VERSION); - Either foundService = Either.left(service); - when(toscaOperationFacade.getToscaElement(anyString())) - .thenReturn(foundResource) - .thenReturn(foundResource) - .thenThrow(new RuntimeException()) - .thenReturn(foundService); - } - else { - when(toscaOperationFacade.getToscaElement(anyString())) - .thenReturn(foundResource) - .thenReturn(foundResource) - .thenThrow(new RuntimeException()); - } - } - - private void mockCheckoutFlow() { - GraphVertex component = new GraphVertex(); - component.setJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE, LifecycleStateEnum.CERTIFIED.name()); - component.setJsonMetadataField(JsonPresentationFields.UNIQUE_ID, COMPONENT_UNIQUE_ID); - List components = Lists.newArrayList(); - components.add(component); - - when(toscaOperationFacade.getToscaElement(anyString())).thenReturn(Either.left(resource)); - when(janusGraphDao.getByCriteria(any(), any(), any(), any())) - .thenReturn(Either.left(components)); - when(csarOperation.getCsarLatestVersion(anyString(), any())) - .thenReturn(Either.left("2.0")); - } - - private ComponentInstance createComponentInstance() { - ComponentInstance instance = new ComponentInstance(); - instance.setIcon(""); - instance.setUniqueId(""); - instance.setName(""); - instance.setComponentUid(""); - instance.setCreationTime(1L); - instance.setModificationTime(2L); - instance.setDescription(""); - instance.setPosX(""); - instance.setPosY(""); - instance.setPropertyValueCounter(1); - instance.setNormalizedName(""); - instance.setOriginType(OriginTypeEnum.CVFC); - instance.setCustomizationUUID(""); - instance.setComponentName(""); - instance.setComponentVersion(OLD_VERSION); - instance.setToscaComponentName(""); - instance.setInvariantName(""); - instance.setSourceModelInvariant(""); - instance.setSourceModelName(""); - instance.setSourceModelUuid(""); - instance.setSourceModelUid(""); - instance.setIsProxy(false); - return instance; - } - - -} diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/tasks/mig1902/SdcGroupsMigrationTest.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/tasks/mig1902/SdcGroupsMigrationTest.java deleted file mode 100644 index 328c5f3459..0000000000 --- a/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/tasks/mig1902/SdcGroupsMigrationTest.java +++ /dev/null @@ -1,231 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2020 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.migration.tasks.mig1902; - -import fj.data.Either; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; -import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult; -import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; -import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; -import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao; -import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum; -import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; -import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; -import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; -import org.openecomp.sdc.be.model.GroupTypeDefinition; -import org.openecomp.sdc.be.model.PropertyDefinition; -import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; -import org.openecomp.sdc.be.model.operations.impl.GroupTypeOperation; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import static org.junit.Assert.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyMap; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@RunWith(MockitoJUnitRunner.class) -public class SdcGroupsMigrationTest { - - @Mock - private JanusGraphDao janusGraphDao; - @Mock - private GroupTypeOperation groupTypeOperation; - @Mock - private GraphVertex topologyTemplateVertex; - @Mock - private GraphVertex groupsVertex; - - @InjectMocks - private SdcGroupsMigration groupsMigration; - - @Before - public void setUp() { - groupsMigration = new SdcGroupsMigration(janusGraphDao, groupTypeOperation); - when(janusGraphDao.getVertexById(any())).thenReturn(Either.left(topologyTemplateVertex)); - } - - @Test - public void handleOneContainerWhenErrorHappened() { - when(janusGraphDao.getChildVertex(any(GraphVertex.class), eq(EdgeLabelEnum.GROUPS), eq(JsonParseFlagEnum.ParseAll))) - .thenReturn(Either.right(JanusGraphOperationStatus.MATCH_NOT_FOUND)); - StorageOperationStatus status = groupsMigration.handleOneContainer(topologyTemplateVertex); - assertEquals(StorageOperationStatus.MATCH_NOT_FOUND, status); - } - - @Test - public void handleOneContainerWhenNoGroups() { - when(janusGraphDao.getChildVertex(any(GraphVertex.class), eq(EdgeLabelEnum.GROUPS), eq(JsonParseFlagEnum.ParseAll))) - .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)); - StorageOperationStatus status = groupsMigration.handleOneContainer(topologyTemplateVertex); - assertEquals(StorageOperationStatus.OK, status); - } - - @Test - public void handleOneContainerWhenGroupsShouldNotBeUpdated() { - when(janusGraphDao.getChildVertex(any(GraphVertex.class), eq(EdgeLabelEnum.GROUPS), eq(JsonParseFlagEnum.ParseAll))) - .thenReturn(Either.left(groupsVertex)); - - doReturn(buildGroupInstancesMap(new String[] {"org.openecomp.groups.heat.HeatStack", "org.openecomp.groups.VfModule"}, new String[]{})) - .when(groupsVertex).getJson(); - StorageOperationStatus status = groupsMigration.handleOneContainer(topologyTemplateVertex); - assertEquals(StorageOperationStatus.OK, status); - verify(janusGraphDao, times(0)).commit(); - - } - - @Test - public void handleOneContainerWhenGroupsShouldBeUpdated() { - mockLatestGroupMapCreating(); - mockUpgradeHappyFlow(); - StorageOperationStatus status = groupsMigration.handleOneContainer(topologyTemplateVertex); - assertEquals(StorageOperationStatus.OK, status); - verify(janusGraphDao, times(1)).commit(); - verify(janusGraphDao, times(0)).rollback(); - } - - private void mockUpgradeHappyFlow() { - when(janusGraphDao.getChildVertex(any(GraphVertex.class), eq(EdgeLabelEnum.GROUPS), eq(JsonParseFlagEnum.ParseAll))) - .thenReturn(Either.left(groupsVertex)); - when(janusGraphDao.updateVertex(any(GraphVertex.class))).thenReturn(Either.left(groupsVertex)); - when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK); - doReturn(buildGroupInstancesMap(new String[] {"org.openecomp.groups.NetworkCollection", "org.openecomp.groups.VfcInstanceGroup"}, - new String[] {"old1", "old2"})) - .when(groupsVertex).getJson(); - } - - @Test - public void handleOneContainerWhenGroupsAlreadyUpdated() { - mockLatestGroupMapCreating(); - when(janusGraphDao.getChildVertex(any(GraphVertex.class), eq(EdgeLabelEnum.GROUPS), eq(JsonParseFlagEnum.ParseAll))) - .thenReturn(Either.left(groupsVertex)); - doReturn(buildGroupInstancesMap(new String[] {"org.openecomp.groups.NetworkCollection"}, - new String[] {"a", "b", "c", "d"})) - .when(groupsVertex).getJson(); - StorageOperationStatus status = groupsMigration.handleOneContainer(topologyTemplateVertex); - assertEquals(StorageOperationStatus.OK, status); - verify(janusGraphDao, times(0)).commit(); - verify(janusGraphDao, times(1)).rollback(); - } - - @Test - public void handleOneContainerWhenExceptionIsThrown() { - when(janusGraphDao.getChildVertex(any(GraphVertex.class), eq(EdgeLabelEnum.GROUPS), eq(JsonParseFlagEnum.ParseAll))) - .thenThrow(new RuntimeException()); - StorageOperationStatus status = groupsMigration.handleOneContainer(topologyTemplateVertex); - assertEquals(StorageOperationStatus.GENERAL_ERROR, status); - verify(janusGraphDao, times(0)).commit(); - verify(janusGraphDao, times(1)).rollback(); - - } - - // A temp remark for this test - following Commit hash: 08595ad21b0c409c69e3902232f5575963199e3e [ASDC-641] – Migration workaround for deployment artifact timeout. Reviewer: Lior. -// @Test -// public void migrateWhenExceptionIsThrown() { -// List vertexList = new ArrayList<>(); -// vertexList.add(topologyTemplateVertex); -// mockLatestGroupMapCreating(); -// when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.TOPOLOGY_TEMPLATE), eq(null), anyMap(), eq(JsonParseFlagEnum.ParseAll))) -// .thenReturn(Either.left(vertexList)); -// when(janusGraphDao.getChildVertex(any(GraphVertex.class), eq(EdgeLabelEnum.GROUPS), eq(JsonParseFlagEnum.ParseAll))) -// .thenThrow(new RuntimeException()); -// assertEquals(MigrationResult.MigrationStatus.FAILED, groupsMigration.migrate().getMigrationStatus()); -// verify(janusGraphDao, times(0)).commit(); -// verify(janusGraphDao, times(1)).rollback(); -// -// } - - @Test - public void migrateWhenGroupsShouldBeUpdated() { - List vertexList = new ArrayList<>(); - vertexList.add(topologyTemplateVertex); - mockLatestGroupMapCreating(); - when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.TOPOLOGY_TEMPLATE), eq(null), anyMap(), eq(JsonParseFlagEnum.ParseAll))) - .thenReturn(Either.left(vertexList)); - mockUpgradeHappyFlow(); - - assertEquals(MigrationResult.MigrationStatus.COMPLETED, groupsMigration.migrate().getMigrationStatus()); - verify(janusGraphDao, times(1)).commit(); - verify(janusGraphDao, times(0)).rollback(); - } - - - private Map buildGroupInstancesMap(String[] groupTypes, String [] propertyNames) { - Map groupsMap = new HashMap<>(); - for (String type: groupTypes) { - GroupDataDefinition gr = new GroupDataDefinition(); - gr.setType(type); - - gr.setProperties(createInstanceProperties(propertyNames)); - groupsMap.put(gr.getType(), gr); - } - return groupsMap; - } - - private void mockLatestGroupMapCreating() { - doReturn(Either.left(createTypeDefinition(new String[] {"a", "b", "c", "d"}))) - .when(groupTypeOperation).getLatestGroupTypeByType(eq(SdcGroupsMigration.GroupsForUpgrade.NW_COLLECTION_GROUP_NAME.getToscaType()), eq(false)); - doReturn(Either.left(createTypeDefinition(new String[] {"l", "m", "n", "o", "p"}))) - .when(groupTypeOperation).getLatestGroupTypeByType(eq(SdcGroupsMigration.GroupsForUpgrade.VFC_INSTANCE_GROUP_NAME.getToscaType()), eq(false)); - groupsMigration.loadLatestGroupTypeDefinitions(); - - } - - private GroupTypeDefinition createTypeDefinition(String[] propertyNames) { - GroupTypeDefinition typeDefinition = new GroupTypeDefinition(); - typeDefinition.setProperties(createTypeProperties(propertyNames)); - return typeDefinition; - } - - private List createTypeProperties(String[] propertyNames) { - List propertyDefinitionList = new ArrayList<>(); - for (String name: propertyNames) { - PropertyDefinition propertyDefinition = new PropertyDefinition(); - propertyDefinition.setName(name); - propertyDefinitionList.add(propertyDefinition); - } - return propertyDefinitionList; - } - - private List createInstanceProperties(String[] propertyNames) { - List propertyDefinitionList = new ArrayList<>(); - for (String name: propertyNames) { - PropertyDefinition propertyDefinition = new PropertyDefinition(); - propertyDefinition.setName(name); - propertyDefinitionList.add(propertyDefinition); - } - return propertyDefinitionList; - } - -} diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/tasks/mig1902/SdcResourceIconMigrationTest.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/tasks/mig1902/SdcResourceIconMigrationTest.java deleted file mode 100644 index f54592da08..0000000000 --- a/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/tasks/mig1902/SdcResourceIconMigrationTest.java +++ /dev/null @@ -1,212 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2020 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.migration.tasks.mig1902; - -import fj.data.Either; -import org.assertj.core.util.Lists; -import org.assertj.core.util.Maps; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; -import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult; -import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; -import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; -import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao; -import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; -import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; -import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.CompositionDataDefinition; -import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; -import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; -import org.openecomp.sdc.be.model.jsonjanusgraph.enums.JsonConstantKeysEnum; - -import java.util.HashMap; -import java.util.List; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.mockito.ArgumentMatchers.anyMap; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@RunWith(MockitoJUnitRunner.class) -public class SdcResourceIconMigrationTest { - @Mock - private JanusGraphDao janusGraphDao; - - @Mock - private GraphVertex graphVertex; - - @Mock - private GraphVertex topologyTemplateVertex; - - @Mock - private CompositionDataDefinition compositionDataDefinition; - - @Mock - private ComponentInstanceDataDefinition componentInstanceDataDefinition; - - @InjectMocks - private SdcResourceIconMigration iconMigration; - - @Before - public void setUp() { - iconMigration = new SdcResourceIconMigration(janusGraphDao); - when(janusGraphDao.getVertexById(any())).thenReturn(Either.left(topologyTemplateVertex)); - when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK); - } - - - @Test - public void migrationFailedWhenNoNodeTypeDefined() { - when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.NODE_TYPE), anyMap(), anyMap(), eq(JsonParseFlagEnum.ParseAll))) - .thenReturn(Either.left(Lists.emptyList())); - assertEquals(MigrationResult.MigrationStatus.FAILED, iconMigration.migrate().getMigrationStatus()); - } - - @Test - public void resourceIsNotUpdatedIfNotVL() { - //iconMigration.handleOneContainer(graphVertex); - mockInstancesNotFoundFlow(); - - iconMigration.updateNodeTypeIconAndStoreInMap(ResourceTypeEnum.VL); - assertFalse(iconMigration.updateIconInsideInstance(componentInstanceDataDefinition)); - } - - - - @Test - public void resourceIsUpdatedIfCP() { - //iconMigration.handleOneContainer(graphVertex); - mockInstancesFoundFlow(); - when(janusGraphDao.updateVertex(any(GraphVertex.class))).thenReturn(Either.left(graphVertex)); - iconMigration.updateNodeTypeIconAndStoreInMap(ResourceTypeEnum.CP); - assertTrue(iconMigration.updateIconInsideInstance(componentInstanceDataDefinition)); - } - - @Test - public void migrateWhenIconsAreUpdated() { - mockInstancesFoundFlow(); - when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.TOPOLOGY_TEMPLATE), eq(null), anyMap(), eq(JsonParseFlagEnum.ParseAll))) - .thenReturn(Either.left(Lists.newArrayList(topologyTemplateVertex))); - when(compositionDataDefinition.getComponentInstances()).thenReturn(Maps.newHashMap("a", componentInstanceDataDefinition)); - doReturn(Maps.newHashMap(JsonConstantKeysEnum.COMPOSITION.getValue(), compositionDataDefinition)).when(topologyTemplateVertex).getJson(); - when(janusGraphDao.updateVertex(any(GraphVertex.class))).thenReturn(Either.left(graphVertex)); - - assertEquals(MigrationResult.MigrationStatus.COMPLETED, iconMigration.migrate().getMigrationStatus()); - verify(janusGraphDao, times(3)).commit(); - } - - @Test - public void migrateWhenIconsNotUpdated() { - mockInstancesNotFoundFlow(); - when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.TOPOLOGY_TEMPLATE), eq(null), anyMap(), eq(JsonParseFlagEnum.ParseAll))) - .thenReturn(Either.left(Lists.newArrayList(topologyTemplateVertex))); - when(compositionDataDefinition.getComponentInstances()).thenReturn(Maps.newHashMap("a", componentInstanceDataDefinition)); - doReturn(Maps.newHashMap(JsonConstantKeysEnum.COMPOSITION.getValue(), compositionDataDefinition)).when(topologyTemplateVertex).getJson(); - - assertEquals(MigrationResult.MigrationStatus.COMPLETED, iconMigration.migrate().getMigrationStatus()); - verify(janusGraphDao, times(2)).commit(); - } - - @Test - public void migrateWhenNoInstancesFound() { - mockInstancesNotFoundFlow(); - when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.TOPOLOGY_TEMPLATE), eq(null), anyMap(), eq(JsonParseFlagEnum.ParseAll))) - .thenReturn(Either.left(Lists.newArrayList(topologyTemplateVertex))); - - assertEquals(MigrationResult.MigrationStatus.COMPLETED, iconMigration.migrate().getMigrationStatus()); - verify(janusGraphDao, times(2)).commit(); - } - - @Test - public void migrationFailedWhenInstanceVertexUpdateFailed() { - mockInstancesFoundFlow(); - when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.TOPOLOGY_TEMPLATE), eq(null), anyMap(), eq(JsonParseFlagEnum.ParseAll))) - .thenReturn(Either.left(Lists.newArrayList(topologyTemplateVertex))); - when(compositionDataDefinition.getComponentInstances()).thenReturn(Maps.newHashMap("a", componentInstanceDataDefinition)); - doReturn(Maps.newHashMap(JsonConstantKeysEnum.COMPOSITION.getValue(), compositionDataDefinition)).when(topologyTemplateVertex).getJson(); - when(janusGraphDao.updateVertex(any(GraphVertex.class))).thenReturn(Either.left(graphVertex)) - .thenReturn(Either.left(graphVertex)) - .thenReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR)); - - assertEquals(MigrationResult.MigrationStatus.COMPLETED, iconMigration.migrate().getMigrationStatus()); - verify(janusGraphDao, times(2)).commit(); - } - - @Test - public void migrationCompletedWhenVertexJsonIsEmpty() { - mockInstancesFoundFlow(); - when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.TOPOLOGY_TEMPLATE), eq(null), anyMap(), eq(JsonParseFlagEnum.ParseAll))) - .thenReturn(Either.left(Lists.newArrayList(topologyTemplateVertex))); - doReturn(new HashMap<>()).when(topologyTemplateVertex).getJson(); - when(janusGraphDao.updateVertex(any(GraphVertex.class))).thenReturn(Either.left(graphVertex)); - - assertEquals(MigrationResult.MigrationStatus.COMPLETED, iconMigration.migrate().getMigrationStatus()); - verify(janusGraphDao, times(2)).commit(); - } - - @Test - public void migrationCompletedWhenVertexJsonIsNull() { - mockInstancesFoundFlow(); - when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.TOPOLOGY_TEMPLATE), eq(null), anyMap(), eq(JsonParseFlagEnum.ParseAll))) - .thenReturn(Either.left(Lists.newArrayList(topologyTemplateVertex))); - doReturn(null).when(topologyTemplateVertex).getJson(); - when(janusGraphDao.updateVertex(any(GraphVertex.class))).thenReturn(Either.left(graphVertex)); - - assertEquals(MigrationResult.MigrationStatus.COMPLETED, iconMigration.migrate().getMigrationStatus()); - verify(janusGraphDao, times(2)).commit(); - } - - @Test - public void migrationFailedWhenTypeUpdateFailed() { - mockInstancesFoundFlow(); - when(janusGraphDao.updateVertex(any(GraphVertex.class))).thenReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR)); - assertEquals(MigrationResult.MigrationStatus.FAILED, iconMigration.migrate().getMigrationStatus()); - verify(janusGraphDao, times(0)).commit(); - } - - private void mockInstancesNotFoundFlow() { - List nodeTypeVertexList = Lists.newArrayList(graphVertex); - when(graphVertex.getMetadataProperty(GraphPropertyEnum.NAME)).thenReturn("vl1"); - when(componentInstanceDataDefinition.getComponentName()).thenReturn("other"); - when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.NODE_TYPE), anyMap(), anyMap(), eq(JsonParseFlagEnum.ParseAll))) - .thenReturn(Either.left(nodeTypeVertexList)); - when(janusGraphDao.updateVertex(any(GraphVertex.class))).thenReturn(Either.left(graphVertex)); - } - - private void mockInstancesFoundFlow() { - when(graphVertex.getMetadataProperty(GraphPropertyEnum.NAME)).thenReturn(String.valueOf("cp1")); - when(componentInstanceDataDefinition.getComponentName()).thenReturn("cp1"); - when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.NODE_TYPE), anyMap(), anyMap(), eq(JsonParseFlagEnum.ParseAll))) - .thenReturn(Either.left(Lists.newArrayList(graphVertex))); - } - - -} -- cgit 1.2.3-korg