1 package org.openecomp.sdc.asdctool.impl;
3 import java.io.BufferedWriter;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.OutputStreamWriter;
8 import java.util.ArrayList;
9 import java.util.Collection;
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.List;
15 import java.util.Optional;
17 import java.util.stream.Collectors;
19 import org.openecomp.sdc.asdctool.impl.validator.utils.VfModuleArtifactPayloadEx;
20 import org.openecomp.sdc.be.components.distribution.engine.VfModuleArtifactPayload;
21 import org.openecomp.sdc.be.dao.api.ActionStatus;
22 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
23 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
24 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
25 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
26 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
27 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
28 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
29 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
30 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
31 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
32 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
33 import org.openecomp.sdc.be.datatypes.elements.MapGroupsDataDefinition;
34 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
35 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
36 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
37 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
38 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
39 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
40 import org.openecomp.sdc.be.impl.ComponentsUtils;
41 import org.openecomp.sdc.be.model.*;
42 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
43 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
44 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
45 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
46 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
47 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
48 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
49 import org.openecomp.sdc.be.resources.data.ESArtifactData;
50 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
51 import org.openecomp.sdc.be.tosca.CsarUtils;
52 import org.openecomp.sdc.be.tosca.ToscaError;
53 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
54 import org.openecomp.sdc.be.tosca.ToscaRepresentation;
55 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
56 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
57 import org.openecomp.sdc.common.api.Constants;
59 import org.openecomp.sdc.common.util.GeneralUtility;
60 import org.openecomp.sdc.exception.ResponseFormat;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63 import org.springframework.beans.factory.annotation.Autowired;
64 import org.springframework.util.StringUtils;
66 import com.google.gson.Gson;
67 import com.google.gson.GsonBuilder;
68 import com.google.gson.JsonArray;
69 import com.google.gson.JsonObject;
71 import fj.data.Either;
73 @org.springframework.stereotype.Component("artifactUuidFix")
74 public class ArtifactUuidFix {
77 private TitanDao titanDao;
80 private ToscaOperationFacade toscaOperationFacade;
82 private ToscaExportHandler toscaExportUtils;
84 private ArtifactCassandraDao artifactCassandraDao;
88 private CsarUtils csarUtils;
90 private static Logger log = LoggerFactory.getLogger(ArtifactUuidFix.class.getName());
92 public boolean doFix(String fixComponent, String runMode) {
93 List<Resource> vfLst = new ArrayList<>();
94 List<Service> serviceList = new ArrayList<>();
95 Map<String, List<Component>> nodeToFixTosca = new HashMap<>();
96 Map<String, List<Component>> vfToFixTosca = new HashMap<>();
97 Map<String, List<Component>> serviceToFixTosca = new HashMap<>();
99 long time = System.currentTimeMillis();
101 doFixTosca(nodeToFixTosca, vfToFixTosca, serviceToFixTosca);
103 if (fixComponent.equals("vf_only")) {
104 if (fetchFaultVf(fixComponent, vfLst, time) == false) {
108 if (fetchServices(fixComponent, serviceList, time) == false) {
112 if (runMode.equals("service_vf") || runMode.equals("fix")) {
113 log.info("Mode {}. Find problem VFs", runMode);
114 if (fetchVf(serviceList, vfLst, time) == false) {
115 log.info("Mode {}. Find problem VFs finished with failure", runMode);
118 log.info("Mode {}. Find problem VFs finished with success", runMode);
120 if (runMode.equals("fix") || runMode.equals("fix_only_services")) {
121 log.info("Mode {}. Start fix", runMode);
122 if (fix(vfLst, serviceList, nodeToFixTosca, vfToFixTosca, serviceToFixTosca) == false) {
123 log.info("Mode {}. Fix finished with failure", runMode);
126 log.info("Mode {}. Fix finished with success", runMode);
132 private boolean fetchFaultVf(String fixComponent, List<Resource> vfLst, long time) {
133 log.info("Find fault VF ");
134 Writer writer = null;
136 String fileName = "fault_" + time + ".csv";
137 writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"));
138 writer.write("vf name, vf id, state, version\n");
140 Map<GraphPropertyEnum, Object> hasProps = new HashMap<>();
141 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
142 hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF.name());
144 Map<GraphPropertyEnum, Object> hasNotProps = new HashMap<>();
145 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
146 log.info("Try to fetch resources with properties {} and not {}", hasProps, hasNotProps);
148 Either<List<GraphVertex>, TitanOperationStatus> servicesByCriteria = titanDao
149 .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll);
150 if (servicesByCriteria.isRight()) {
151 log.info("Failed to fetch resources {}", servicesByCriteria.right().value());
154 List<GraphVertex> resources = servicesByCriteria.left().value();
155 for (GraphVertex gv : resources) {
156 ComponentParametersView filter = new ComponentParametersView(true);
157 filter.setIgnoreComponentInstances(false);
158 filter.setIgnoreArtifacts(false);
159 filter.setIgnoreGroups(false);
161 Either<Resource, StorageOperationStatus> toscaElement = toscaOperationFacade
162 .getToscaElement(gv.getUniqueId());
163 if (toscaElement.isRight()) {
164 log.info("Failed to fetch resources {} {}", gv.getUniqueId(), toscaElement.right().value());
168 Resource resource = toscaElement.left().value();
169 String resourceName = resource.getName();
170 Map<String, ArtifactDefinition> deploymentArtifacts = resource.getDeploymentArtifacts();
171 List<GroupDefinition> groups = resource.getGroups();
172 if (groups == null || groups.isEmpty()) {
173 log.info("No groups for resource {} id {} ", resourceName, gv.getUniqueId());
176 boolean isProblematic = false;
177 for (GroupDefinition gr : groups) {
178 if (gr.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
179 if (isProblematicGroup(gr, resourceName, deploymentArtifacts)) {
180 isProblematic = true;
187 writeModuleResultToFile(writer, resource, null);
193 } catch (Exception e) {
194 log.info("Failed to fetch vf resources ", e);
201 } catch (Exception ex) {
208 private boolean fetchVf(List<Service> serviceList, List<Resource> vfLst, long time) {
209 log.info("Find problem VF ");
210 if (serviceList.isEmpty()) {
211 log.info("No services as input");
214 Writer writer = null;
216 String fileName = "problemVf_" + time + ".csv";
217 writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"));
218 writer.write("vf name, vf id, state, version, example service name\n");
219 Set<String> vfIds = new HashSet<>();
220 for (Service service : serviceList) {
221 List<ComponentInstance> componentInstances = service.getComponentInstances().stream()
222 .filter(ci -> ci.getOriginType().equals(OriginTypeEnum.VF)).collect(Collectors.toList());
223 for (ComponentInstance ci : componentInstances) {
224 if (!vfIds.contains(ci.getComponentUid())) {
225 vfIds.add(ci.getComponentUid());
226 ComponentParametersView filter = new ComponentParametersView(true);
227 filter.setIgnoreComponentInstances(false);
228 filter.setIgnoreArtifacts(false);
229 filter.setIgnoreGroups(false);
230 Either<Resource, StorageOperationStatus> toscaElement = toscaOperationFacade
231 .getToscaElement(ci.getComponentUid(), filter);
232 if (toscaElement.isRight()) {
233 log.info("Failed to fetch resource {} {}", ci.getComponentUid(),
234 toscaElement.right().value());
237 Resource resource = toscaElement.left().value();
238 if (resource.getResourceType().equals(ResourceTypeEnum.VF)) {
240 writeModuleResultToFile(writer, resource, service);
248 log.info("output file with list of Vf : {}", fileName);
249 } catch (Exception e) {
250 log.info("Failed to fetch services ", e);
257 } catch (Exception ex) {
264 private boolean fetchServices(String fixServices, List<Service> serviceList, long time) {
265 log.info("Find problem Services {}", fixServices);
266 Writer writer = null;
269 String fileName = "problemService_" + time + ".csv";
270 writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"));
271 writer.write("service name, service id, state, version\n");
273 Map<GraphPropertyEnum, Object> hasProps = new HashMap<>();
274 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
275 if (fixServices.equals("distributed_only")) {
276 hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
277 hasProps.put(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTED.name());
280 Map<GraphPropertyEnum, Object> hasNotProps = new HashMap<>();
281 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
282 log.info("Try to fetch services with properties {} and not {}", hasProps, hasNotProps);
284 Either<List<GraphVertex>, TitanOperationStatus> servicesByCriteria = titanDao
285 .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll);
286 if (servicesByCriteria.isRight()) {
287 log.info("Failed to fetch services {}", servicesByCriteria.right().value());
290 List<GraphVertex> services = servicesByCriteria.left().value();
291 for (GraphVertex gv : services) {
292 ComponentParametersView filter = new ComponentParametersView(true);
293 filter.setIgnoreComponentInstances(false);
294 filter.setIgnoreArtifacts(false);
295 filter.setIgnoreGroups(false);
297 Either<Service, StorageOperationStatus> toscaElement = toscaOperationFacade
298 .getToscaElement(gv.getUniqueId(), filter);
299 if (toscaElement.isRight()) {
300 log.info("Failed to fetch service {} {}", gv.getUniqueId(), toscaElement.right().value());
303 Service service = toscaElement.left().value();
305 String serviceName = (String) gv.getMetadataProperty(GraphPropertyEnum.NAME);
307 boolean isProblematic = isProblematicService(service, serviceName);
309 serviceList.add(service);
310 writeModuleResultToFile(writer, service, null);
317 log.info("output file with list of services : {}", fileName);
318 } catch (Exception e) {
319 log.info("Failed to fetch services ", e);
326 } catch (Exception ex) {
333 private boolean isProblematicService( Service service, String serviceName) throws IOException {
335 List<ComponentInstance> componentInstances = service.getComponentInstances();
337 if (componentInstances == null) {
338 log.info("No instances for service {} ", service.getUniqueId());
341 boolean isCheckVFModules = true;
342 if(service.getLifecycleState() == LifecycleStateEnum.NOT_CERTIFIED_CHECKIN ||
343 service.getLifecycleState() == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT){
344 isCheckVFModules = false;
346 for (ComponentInstance ci : componentInstances) {
347 Map<String, ArtifactDefinition> deploymentArtifacts = ci.getDeploymentArtifacts();
348 List<GroupInstance> groupInstances = ci.getGroupInstances();
349 if (groupInstances == null || groupInstances.isEmpty()) {
350 log.info("No instance groups for instance {} in service {} id {} ", ci.getName(), serviceName,
351 service.getUniqueId());
354 List<VfModuleArtifactPayloadEx> vfModules = null;
355 if(isCheckVFModules){
356 Optional<ArtifactDefinition> optionalVfModuleArtifact = deploymentArtifacts.values().stream()
357 .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.VF_MODULES_METADATA.name())).findAny();
359 if(!optionalVfModuleArtifact.isPresent())
362 ArtifactDefinition vfModuleArtifact = optionalVfModuleArtifact.get();
363 Either<List<VfModuleArtifactPayloadEx>, StorageOperationStatus> vfModulesEither = parseVFModuleJson(vfModuleArtifact);
364 if(vfModulesEither.isRight()){
365 log.error("Failed to parse vfModule for service {} status is {}", service.getUniqueId(), vfModulesEither.right().value());
368 vfModules = vfModulesEither.left().value();
369 if(vfModules == null || vfModules.isEmpty() ){
370 log.info("vfModules empty for service {}", service.getUniqueId());
375 for (GroupInstance gi : groupInstances) {
376 if (gi.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
377 VfModuleArtifactPayloadEx vfModule= null;
378 if(isCheckVFModules && vfModules != null){
379 Optional<VfModuleArtifactPayloadEx> op = vfModules.stream().filter(vf -> vf.getVfModuleModelName().equals(gi.getGroupName())).findAny();
381 log.error("Failed to find vfModule for group {}", gi.getGroupName());
386 if (isProblematicGroupInstance(gi, ci.getName(), serviceName, deploymentArtifacts, vfModule)) {
396 private boolean isProblematicGroup(GroupDefinition gr, String resourceName,
397 Map<String, ArtifactDefinition> deploymentArtifacts) {
398 List<String> artifacts = gr.getArtifacts();
399 List<String> artifactsUuid = gr.getArtifactsUuid();
400 Set<String> artifactsSet = new HashSet<>();
402 if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) {
403 log.info("No groups in resource {} ", resourceName);
406 artifactsSet.addAll(artifacts);
407 if (artifactsSet.size() < artifacts.size()) {
408 log.info(" artifactsSet.size() < artifacts.size() group {} in resource {} ", gr.getName(), resourceName);
412 if (artifacts.size() < artifactsUuid.size()) {
413 log.info(" artifacts.size() < artifactsUuid.size() group {} in resource {} ", gr.getName(), resourceName);
416 if (artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty())) {
418 " artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() group {} in resource {} ",
419 gr.getName(), resourceName);
422 if (artifactsUuid.contains(null)) {
423 log.info(" artifactsUuid.contains(null) group {} in resource {} ", gr.getName(), resourceName);
427 for (String artifactId : artifacts) {
428 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
429 ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactlabel);
430 if (artifactDefinition == null) {
431 log.info(" artifactDefinition == null label {} group {} in resource {} ", artifactlabel, gr.getName(),
435 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactDefinition.getArtifactType());
436 if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
437 if (!artifactId.equals(artifactDefinition.getUniqueId())) {
439 " !artifactId.equals(artifactDefinition.getUniqueId() artifact {} artId {} group {} in resource {} ",
440 artifactlabel, artifactId, gr.getName(), resourceName);
443 if (!artifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
445 " artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} group {} in resource {} ",
446 artifactlabel, gr.getName(), resourceName);
451 for (String artifactUUID : artifactsUuid) {
452 String label = findArtifactLabelFromArtifactId(artifactUUID);
453 if (label != null && !label.isEmpty() && !label.equals("")) {
461 private boolean isProblematicGroupInstance(GroupInstance gi, String instName, String servicename,
462 Map<String, ArtifactDefinition> deploymentArtifacts, VfModuleArtifactPayloadEx vfModule) {
463 List<String> artifacts = gi.getArtifacts();
464 List<String> artifactsUuid = gi.getArtifactsUuid();
465 List<String> instArtifactsUuid = gi.getGroupInstanceArtifactsUuid();
466 List<String> instArtifactsId = gi.getGroupInstanceArtifacts();
467 Set<String> instArtifatIdSet = new HashSet<>();
468 Set<String> artifactsSet = new HashSet<>();
470 log.info("check group {} for instance {} ", gi.getGroupName(), instName);
471 if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) {
472 log.info("No instance groups for instance {} in service {} ", instName, servicename);
475 artifactsSet.addAll(artifacts);
476 if (artifactsSet.size() < artifacts.size()) {
477 log.info(" artifactsSet.size() < artifacts.size() group {} in resource {} ", instName, servicename);
481 if (instArtifactsId != null && !instArtifactsId.isEmpty()) {
482 instArtifatIdSet.addAll(instArtifactsId);
485 if (artifacts.size() < artifactsUuid.size()) {
486 log.info(" artifacts.size() < artifactsUuid.size() inst {} in service {} ", instName, servicename);
489 if (!artifacts.isEmpty() && (artifactsUuid == null || artifactsUuid.isEmpty())) {
491 " artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() inst {} in service {} ",
492 instName, servicename);
495 if (artifactsUuid.contains(null)) {
496 log.info(" artifactsUuid.contains(null) inst {} in service {} ", instName, servicename);
499 if (instArtifactsId != null && instArtifatIdSet.size() < instArtifactsId.size()) {
500 log.info(" instArtifatIdSet.size() < instArtifactsId.size() inst {} in service {} ", instName, servicename);
504 if ((instArtifactsId != null && instArtifactsUuid != null)
505 && instArtifactsId.size() != instArtifactsUuid.size()) {
506 log.info(" instArtifactsId.size() != instArtifactsUuid.size() inst {} in service {} ", instName,
511 for (String artifactId : artifacts) {
512 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
513 ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactlabel);
514 if (artifactDefinition == null) {
515 log.info(" artifactDefinition == null label {} inst {} in service {} ", artifactlabel, instName,
519 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactDefinition.getArtifactType());
520 if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
521 if (!artifactId.equals(artifactDefinition.getUniqueId())) {
523 " !artifactId.equals(artifactDefinition.getUniqueId() artifact {} artId {} inst {} in service {} ",
524 artifactlabel, artifactId, instName, servicename);
527 if (!artifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
529 " artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ",
530 artifactlabel, instName, servicename);
534 if (instArtifactsUuid == null || instArtifactsUuid.isEmpty()) {
535 log.info(" instArtifactsUuid empty. label {} inst {} in service {} ", artifactlabel, instName,
539 if (!instArtifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
541 " instArtifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ",
542 artifactlabel, instName, servicename);
547 for (String artifactUUID : artifactsUuid) {
548 String label = findArtifactLabelFromArtifactId(artifactUUID);
549 if (label != null && !label.isEmpty() && !label.equals("")) {
553 if(vfModule != null ){
554 return isProblematicVFModule(vfModule, artifactsUuid, instArtifactsUuid);
560 private boolean isProblematicVFModule(VfModuleArtifactPayloadEx vfModule, List<String> artifactsUuid,
561 List<String> instArtifactsUuid) {
562 log.info(" isProblematicVFModule {} ", vfModule.getVfModuleModelName());
563 List<String> vfModuleArtifacts = vfModule.getArtifacts();
564 List<String> allArtifacts = new ArrayList<>();
565 allArtifacts.addAll(artifactsUuid);
566 if(instArtifactsUuid != null)
567 allArtifacts.addAll(instArtifactsUuid);
568 if((vfModuleArtifacts == null || vfModuleArtifacts.isEmpty()) && !artifactsUuid.isEmpty()){
569 log.error(" vfModuleArtifacts == null || vfModuleArtifacts.isEmpty()) && !artifactsUuid.isEmpty()");
572 if(vfModuleArtifacts!= null){
573 if( vfModuleArtifacts.size() != allArtifacts.size()){
574 log.error(" vfModuleArtifacts.size() != allArtifacts.size()");
577 for(String vfModuleArtifact: vfModuleArtifacts){
578 Optional<String> op = allArtifacts.stream().filter(a -> a.equals(vfModuleArtifact)).findAny();
580 log.error("failed to find artifact {} in group artifacts {}", vfModuleArtifact, allArtifacts);
590 private boolean fix(List<Resource> vfLst, List<Service> serviceList, Map<String, List<Component>> nodesToFixTosca,
591 Map<String, List<Component>> vfToFixTosca, Map<String, List<Component>> servicesToFixTosca) {
593 log.info(" Fix started ***** ");
594 if (vfLst != null && !vfLst.isEmpty()) {
599 if (res && serviceList != null && !serviceList.isEmpty()) {
600 res = fixServices(serviceList);
604 Set<String> fixedIds = new HashSet<>();
606 long time = System.currentTimeMillis();
607 String fileName = "FailedGenerateTosca" + "_" + time + ".csv";
608 Writer writer = null;
610 writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"));
611 writer.write("componentType, name, version, UID, UUID, invariantUUID, state\n");
612 List<Component> failedList = new ArrayList<>();
614 if (res && nodesToFixTosca != null && !nodesToFixTosca.isEmpty()) {
616 generateAndSaveToscaArtifacts(nodesToFixTosca, fixedIds, null, failedList);
619 if (vfToFixTosca != null && !vfToFixTosca.isEmpty()) {
621 generateAndSaveToscaArtifacts(vfToFixTosca, fixedIds, vfLst, failedList);
625 for (Component component : vfLst) {
626 res = generateToscaPerComponent(fixedIds, component);
628 TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(component);
629 Map<String, GroupDataDefinition> groups = topologyTemplate.getGroups();
630 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.GROUPS, EdgeLabelEnum.GROUPS, groups);
632 Map<String, ArtifactDataDefinition> arifacts = topologyTemplate.getDeploymentArtifacts();
633 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.DEPLOYMENT_ARTIFACTS,
634 EdgeLabelEnum.DEPLOYMENT_ARTIFACTS, arifacts);
637 Map<String, ArtifactDataDefinition> arifacts = topologyTemplate.getToscaArtifacts();
638 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS,
639 EdgeLabelEnum.TOSCA_ARTIFACTS, arifacts);
643 failedList.add(component);
647 if (servicesToFixTosca != null && !servicesToFixTosca.isEmpty()) {
648 generateAndSaveToscaArtifacts(servicesToFixTosca, fixedIds, serviceList, failedList);
653 for (Component component : serviceList) {
654 res = generateToscaPerComponent(fixedIds, component);
656 TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(component);
657 Map<String, MapGroupsDataDefinition> groups = topologyTemplate.getInstGroups();
658 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS,
662 Map<String, MapArtifactDataDefinition> artifacts = topologyTemplate
663 .getInstDeploymentArtifacts();
664 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS,
665 EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, artifacts);
668 Map<String, ArtifactDataDefinition> arifacts = topologyTemplate.getToscaArtifacts();
669 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS,
670 EdgeLabelEnum.TOSCA_ARTIFACTS, arifacts);
674 failedList.add(component);
678 if (!failedList.isEmpty()) {
679 for (Component component : failedList) {
680 StringBuilder sb = new StringBuilder(component.getComponentType().getValue());
681 sb.append(",").append(component.getName()).append(",").append(component.getVersion()).append(",")
682 .append(component.getUniqueId()).append(",").append(component.getUUID()).append(",")
683 .append(component.getInvariantUUID()).append(",").append(component.getLifecycleState());
686 writer.write(sb.toString());
690 } catch (IOException e) {
691 log.error(e.getMessage());
697 } catch (Exception ex) {
700 log.info(" Fix finished with res {} ***** ", res);
704 private boolean generateAndSaveToscaArtifacts(Map<String, List<Component>> nodesToFixTosca, Set<String> fixedIds,
705 List<? extends Component> componentsWithFailedGroups, List<Component> failedList) {
707 log.debug("Migration1707ArtifactUuidFix generateAndSaveToscaArtifacts started ");
708 for (Map.Entry<String, List<Component>> entry : nodesToFixTosca.entrySet()) {
710 List<Component> component = entry.getValue();
711 for (Component c : component) {
712 log.debug("Migration1707ArtifactUuidFix fix tosca on component : id {}, name {} ", c.getUniqueId(),
714 if (componentsWithFailedGroups != null) {
715 Optional<Component> op = (Optional<Component>) componentsWithFailedGroups.stream()
716 .filter(cg -> cg.getUniqueId().equals(c.getUniqueId())).findAny();
718 res = generateToscaPerComponent(fixedIds, c);
720 res = generateToscaPerComponent(fixedIds, c);
722 ToscaElement topologyTemplate = ModelConverter.convertToToscaElement(c);
723 Map<String, ArtifactDataDefinition> arifacts = topologyTemplate.getToscaArtifacts();
724 res = fixDataOnGraph(c.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS, EdgeLabelEnum.TOSCA_ARTIFACTS,
733 log.debug("Migration1707ArtifactUuidFix generateAndSaveToscaArtifacts finished with res {} ", res);
737 private boolean generateToscaPerComponent(Set<String> fixedIds, Component c) {
739 log.debug("Migration1707ArtifactUuidFix generateToscaPerComponent started component name {} id {}",
740 c.getName(), c.getUniqueId());
742 Either<Component, StorageOperationStatus> toscaElement = toscaOperationFacade
743 .getToscaFullElement(c.getUniqueId());
744 if (toscaElement.isRight()) {
745 log.info("Failed to fetch resources {} {}", c.getUniqueId(), toscaElement.right().value());
748 Component toscaElementFull = toscaElement.left().value();
749 toscaElementFull.setGroups(c.getGroups());
750 List<ComponentInstance> ciListFull = toscaElementFull.getComponentInstances();
751 List<ComponentInstance> ciList = c.getComponentInstances();
752 if (ciListFull != null && !ciListFull.isEmpty()) {
753 ciListFull.forEach(ciFull -> {
754 ComponentInstance compInst = ciList.stream()
755 .filter(ci -> ci.getUniqueId().equals(ciFull.getUniqueId())).findAny().get();
756 ciFull.setGroupInstances(compInst.getGroupInstances());
760 Either<Component, ToscaError> either = generateToscaArtifact(toscaElementFull);
762 if (either.isRight()) {
763 log.error("Couldn't generate and save tosca template component unique id {}, name {} error: {}",
764 toscaElementFull.getUniqueId(), toscaElementFull.getName(), either.right().value());
770 c.setToscaArtifacts(either.left().value().getToscaArtifacts());
771 fixedIds.add(toscaElementFull.getUniqueId());
779 log.debug("Migration1707ArtifactUuidFix generateToscaPerComponent finished component name {} id {} res {}",
780 c.getName(), c.getUniqueId(), res);
784 private <T extends ToscaDataDefinition> boolean fixDataOnGraph(String componentId, VertexTypeEnum vertexTypeEnum,
785 EdgeLabelEnum edgeLabelEnum, Map<String, T> groups) {
786 log.debug("amount groups to update: VertexTypeEnum {} EdgeLabelEnum {} data size {}", vertexTypeEnum.getName(),
787 edgeLabelEnum, groups.size());
789 Either<GraphVertex, TitanOperationStatus> getResponse = titanDao.getVertexById(componentId,
790 JsonParseFlagEnum.NoParse);
791 if (getResponse.isRight()) {
792 log.debug("Couldn't fetch component unique id {}, error: {}", componentId, getResponse.right().value());
797 GraphVertex componentVertex = getResponse.left().value();
799 GraphVertex toscaDataVertex = null;
800 Either<GraphVertex, TitanOperationStatus> groupVertexEither = titanDao.getChildVertex(componentVertex,
801 edgeLabelEnum, JsonParseFlagEnum.ParseJson);
802 if (groupVertexEither.isRight() && groupVertexEither.right().value() == TitanOperationStatus.NOT_FOUND) {
803 log.debug("no child {} vertex for component unique id {}, error: {}", edgeLabelEnum, componentId,
804 groupVertexEither.right().value());
807 if (groupVertexEither.isRight()) {
809 log.debug("failed to get child {} vertex for component unique id {}, error: {}", edgeLabelEnum,
810 componentId, groupVertexEither.right().value());
813 toscaDataVertex = groupVertexEither.left().value();
814 toscaDataVertex.setJson(groups);
815 Either<GraphVertex, TitanOperationStatus> updatevertexEither = titanDao.updateVertex(toscaDataVertex);
816 if (updatevertexEither.isRight()) {
817 log.debug("failed to update vertex for component unique id {}, error: {}", componentId,
818 updatevertexEither.right().value());
824 log.debug("Fix data on graph finished: VertexTypeEnum {} EdgeLabelEnum {} res {}", vertexTypeEnum.getName(),
829 private boolean fixServices(List<Service> serviceList) {
830 for (Service service : serviceList) {
831 log.debug("Migration1707ArtifactUuidFix fix service: id {}, name {} ", service.getUniqueId(),
833 List<ComponentInstance> instances = service.getComponentInstances();
834 for (ComponentInstance instance : instances) {
835 fixComponentInstances(service, instance);
843 private void fixComponentInstances(Service service, ComponentInstance instance) {
844 Map<String, ArtifactDefinition> artifactsMap = instance.getDeploymentArtifacts();
845 List<GroupInstance> groupsList = instance.getGroupInstances();
846 if (groupsList != null && artifactsMap != null) {
847 List<GroupInstance> groupsToDelete = new ArrayList<>();
848 for (GroupInstance group : groupsList) {
849 fixGroupInstances(service, artifactsMap, groupsToDelete, group);
853 if (!groupsToDelete.isEmpty()) {
854 log.debug("Migration1707ArtifactUuidFix delete group: resource id {}, group instance to delete {} ",
855 service.getUniqueId(), groupsToDelete);
856 groupsList.removeAll(groupsToDelete);
860 Optional<ArtifactDefinition> optionalVfModuleArtifact = artifactsMap.values().stream()
861 .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.VF_MODULES_METADATA.name())).findAny();
862 ArtifactDefinition vfModuleAertifact = null;
863 if(!optionalVfModuleArtifact.isPresent()){
864 vfModuleAertifact = createVfModuleArtifact(instance, service);
865 artifactsMap.put(vfModuleAertifact.getArtifactLabel(), vfModuleAertifact);
868 vfModuleAertifact = optionalVfModuleArtifact.get();
870 fillVfModuleInstHeatEnvPayload(service, instance, groupsList, vfModuleAertifact);
874 private void fixGroupInstances(Service service, Map<String, ArtifactDefinition> artifactsMap,
875 List<GroupInstance> groupsToDelete, GroupInstance group) {
876 if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
877 log.debug("Migration1707ArtifactUuidFix fix group: resource id {}, group name {} ", service.getUniqueId(),
879 if(group.getArtifacts() != null){
880 Set<String> groupArtifactsSet = new HashSet<>(group.getArtifacts());
881 if(group.getGroupInstanceArtifacts() != null){
882 List<String> groupInsArtifacts = new ArrayList<>(group.getGroupInstanceArtifacts());
883 groupArtifactsSet.addAll(groupInsArtifacts);
885 List<String> groupArtifacts = new ArrayList<>(groupArtifactsSet);
887 clearGroupInstanceArtifacts(group);
889 for (String artifactId : groupArtifacts) {
890 fixArtifactUndergroupInstances(artifactsMap, group, groupArtifacts, artifactId);
893 if (group.getArtifacts() == null || group.getArtifacts().isEmpty()) {
895 "Migration1707ArtifactUuidFix fix groupInstance add to delete list: resource id {} name {} , group name {} ",
896 service.getUniqueId(), service.getName(), group.getName());
897 groupsToDelete.add(group);
902 private void clearGroupInstanceArtifacts(GroupInstance group) {
903 if(group.getArtifacts() != null)
904 group.getArtifacts().clear();
906 group.setArtifacts(new ArrayList<>());
907 if(group.getArtifactsUuid() != null)
908 group.getArtifactsUuid().clear();
910 group.setArtifactsUuid(new ArrayList<>());
912 if(group.getGroupInstanceArtifacts() != null)
913 group.getGroupInstanceArtifacts().clear();
915 group.setGroupInstanceArtifacts(new ArrayList<>());
917 if(group.getGroupInstanceArtifactsUuid() != null )
918 group.getGroupInstanceArtifactsUuid().clear();
920 group.setGroupInstanceArtifactsUuid(new ArrayList<>());
923 private void fixArtifactUndergroupInstances(Map<String, ArtifactDefinition> artifactsMap, GroupInstance group,
924 List<String> groupArtifacts, String artifactId) {
925 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
926 log.debug("Migration1707ArtifactUuidFix fix group: group name {} artifactId for fix {} artifactlabel {} ",
927 group.getName(), artifactId, artifactlabel);
928 if (!artifactlabel.isEmpty() && artifactsMap.containsKey(artifactlabel)) {
929 ArtifactDefinition artifact = artifactsMap.get(artifactlabel);
930 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifact.getArtifactType());
931 String correctArtifactId = artifact.getUniqueId();
932 String correctArtifactUUID = artifact.getArtifactUUID();
933 if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
934 boolean isAddToGroup = true;
935 if (groupArtifacts.size() == 1) {
936 if (artifactType == ArtifactTypeEnum.HEAT_ARTIFACT) {
937 isAddToGroup = false;
938 artifact.setArtifactType(ArtifactTypeEnum.OTHER.getType());
943 "Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} ",
944 group.getName(), correctArtifactId, correctArtifactUUID);
945 group.getArtifacts().add(correctArtifactId);
946 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
947 group.getArtifactsUuid().add(correctArtifactUUID);
952 "Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} ",
953 group.getName(), correctArtifactId, correctArtifactUUID);
954 Set<String> tmpSet = new HashSet<>(group.getGroupInstanceArtifacts());
955 tmpSet.add(correctArtifactId);
956 group.setGroupInstanceArtifacts(new ArrayList<>(tmpSet));
957 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
958 Set<String> tmpSetUUID = new HashSet<>(group.getGroupInstanceArtifactsUuid());
959 tmpSetUUID.add(correctArtifactUUID);
960 group.setGroupInstanceArtifactsUuid(new ArrayList<>(tmpSetUUID));
966 private boolean fixVf(List<Resource> vfLst) {
967 for (Resource resource : vfLst) {
968 log.debug("Migration1707ArtifactUuidFix fix resource: id {}, name {} ", resource.getUniqueId(),
970 Map<String, ArtifactDefinition> artifactsMap = resource.getDeploymentArtifacts();
971 List<GroupDefinition> groupsList = resource.getGroups();
972 List<GroupDefinition> groupsToDelete = new ArrayList<>();
973 if (groupsList != null && artifactsMap != null) {
974 for (GroupDefinition group : groupsList) {
975 if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE) && group.getArtifacts() != null) {
976 fixVfGroup(resource, artifactsMap, group);
978 if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)
979 && (group.getArtifacts() == null || group.getArtifacts().isEmpty())) {
981 "Migration1707ArtifactUuidFix add group to delete list fix resource: id {}, name {} ",
982 resource.getUniqueId(), resource.getName(), group.getName());
983 groupsToDelete.add(group);
987 if (!groupsToDelete.isEmpty()) {
988 groupsList.removeAll(groupsToDelete);
998 private void fixVfGroup(Resource resource, Map<String, ArtifactDefinition> artifactsMap, GroupDefinition group) {
999 log.debug("Migration1707ArtifactUuidFix fix group: resource id {}, group name {} ", resource.getUniqueId(),
1001 Set<String> groupArtifactsSet = new HashSet<>(group.getArtifacts());
1002 List<String> groupArtifacts = new ArrayList<>(groupArtifactsSet);
1003 group.getArtifacts().clear();
1004 group.getArtifactsUuid().clear();
1006 for (String artifactId : groupArtifacts) {
1007 fixArtifactUnderGroup(artifactsMap, group, groupArtifacts, artifactId);
1011 private void fixArtifactUnderGroup(Map<String, ArtifactDefinition> artifactsMap, GroupDefinition group,
1012 List<String> groupArtifacts, String artifactId) {
1014 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
1015 log.debug("Migration1707ArtifactUuidFix fix group: group name {} artifactId for fix {} artifactlabel {} ",
1016 group.getName(), artifactId, artifactlabel);
1017 if (!artifactlabel.isEmpty() && artifactsMap.containsKey(artifactlabel)) {
1018 ArtifactDefinition artifact = artifactsMap.get(artifactlabel);
1019 String correctArtifactId = artifact.getUniqueId();
1020 String correctArtifactUUID = artifact.getArtifactUUID();
1021 boolean isAddToGroup = true;
1022 if (groupArtifacts.size() == 1) {
1023 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifact.getArtifactType());
1024 if (artifactType == ArtifactTypeEnum.HEAT_ARTIFACT) {
1025 isAddToGroup = false;
1026 artifact.setArtifactType(ArtifactTypeEnum.OTHER.getType());
1031 "Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} ",
1032 group.getName(), correctArtifactId, correctArtifactUUID);
1033 group.getArtifacts().add(correctArtifactId);
1034 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
1035 group.getArtifactsUuid().add(correctArtifactUUID);
1042 private String findArtifactLabelFromArtifactId(String artifactId) {
1043 String artifactLabel = "";
1045 int index = artifactId.lastIndexOf('.');
1046 if (index > 0 && index + 1 < artifactId.length())
1047 artifactLabel = artifactId.substring(index + 1);
1048 return artifactLabel;
1051 private void writeModuleResultToFile(Writer writer, org.openecomp.sdc.be.model.Component component,
1054 // "service name, service id, state, version
1055 StringBuilder sb = new StringBuilder(component.getName());
1056 sb.append(",").append(component.getUniqueId()).append(",").append(component.getLifecycleState()).append(",")
1057 .append(component.getVersion());
1058 if (service != null) {
1059 sb.append(",").append(service.getName());
1062 writer.write(sb.toString());
1063 } catch (IOException e) {
1064 log.error(e.getMessage());
1068 private void writeModuleResultToFile(Writer writer, List<Component> components) {
1070 // "service name, service id, state, version
1071 for (Component component : components) {
1072 StringBuilder sb = new StringBuilder(component.getName());
1073 sb.append(",").append(component.getUniqueId()).append(",").append(component.getInvariantUUID())
1074 .append(",").append(component.getLifecycleState()).append(",").append(component.getVersion());
1077 writer.write(sb.toString());
1079 } catch (IOException e) {
1081 log.error(e.getMessage());
1085 public boolean doFixTosca(Map<String, List<Component>> nodeToFix, Map<String, List<Component>> vfToFix,
1086 Map<String, List<Component>> serviceToFix) {
1088 Map<GraphPropertyEnum, Object> hasProps = new HashMap<>();
1089 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
1090 hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
1092 Map<String, List<Component>> vertices = getVerticesToValidate(VertexTypeEnum.NODE_TYPE, hasProps);
1093 boolean result = validateTosca(vertices, nodeToFix, "RESOURCE_TOSCA_ARTIFACTS");//
1096 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
1097 hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF);
1098 hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
1100 vertices = getVerticesToValidate(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps);
1101 result = validateTosca(vertices, vfToFix, "VF_TOSCA_ARTIFACTS");
1104 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
1105 hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
1107 vertices = getVerticesToValidate(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps);
1108 result = validateTosca(vertices, serviceToFix, "SERVICE_TOSCA_ARTIFACTS");
1113 public Map<String, List<Component>> getVerticesToValidate(VertexTypeEnum type,
1114 Map<GraphPropertyEnum, Object> hasProps) {
1116 Map<String, List<Component>> result = new HashMap<>();
1119 Either<List<GraphVertex>, TitanOperationStatus> resultsEither = titanDao.getByCriteria(type, hasProps);
1120 if (resultsEither.isRight()) {
1121 log.error("getVerticesToValidate failed {} ",resultsEither.right().value());
1124 log.info("getVerticesToValidate: {} vertices to scan", resultsEither.left().value().size());
1125 List<GraphVertex> componentsList = resultsEither.left().value();
1126 componentsList.forEach(vertex -> {
1127 String ivariantUuid = (String) vertex.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID);
1128 if (!result.containsKey(ivariantUuid)) {
1129 List<Component> compList = new ArrayList<Component>();
1130 result.put(ivariantUuid, compList);
1132 List<Component> compList = result.get(ivariantUuid);
1134 ComponentParametersView filter = new ComponentParametersView(true);
1135 filter.setIgnoreArtifacts(false);
1137 Either<Component, StorageOperationStatus> toscaElement = toscaOperationFacade
1138 .getToscaElement(vertex.getUniqueId(), filter);
1139 if (toscaElement.isRight()) {
1140 log.error("getVerticesToValidate: failed to find element {} staus is {}", vertex.getUniqueId()
1141 ,toscaElement.right().value());
1143 compList.add(toscaElement.left().value());
1149 } catch (Exception e) {
1150 log.info("Failed to fetch vf resources ", e);
1159 public boolean validateTosca(Map<String, List<Component>> vertices, Map<String, List<Component>> compToFix,
1161 boolean result = true;
1162 long time = System.currentTimeMillis();
1163 String fileName = name + "_" + time + ".csv";
1164 Writer writer = null;
1166 writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"));
1167 writer.write("name, UUID, invariantUUID, state, version\n");
1168 for (Map.Entry<String, List<Component>> entry : vertices.entrySet()) {
1169 List<Component> compList = entry.getValue();
1170 Set<String> artifactEsId = new HashSet<>();
1171 for (Component component : compList) {
1172 Map<String, ArtifactDefinition> toscaArtifacts = component.getToscaArtifacts();
1173 Optional<ArtifactDefinition> op = toscaArtifacts.values().stream()
1174 .filter(a -> artifactEsId.contains(a.getEsId()) && a.getEsId() != null).findAny();
1175 if (op.isPresent()) {
1177 writeModuleResultToFile(writer, compList);
1181 artifactEsId.addAll(toscaArtifacts.values().stream().map(ArtifactDefinition::getEsId)
1182 .collect(Collectors.toList()));
1186 List<Component> compListfull = new ArrayList<>();
1187 for (Component c : compList) {
1188 ComponentParametersView filter = new ComponentParametersView(true);
1189 filter.setIgnoreComponentInstances(false);
1190 filter.setIgnoreArtifacts(false);
1191 filter.setIgnoreGroups(false);
1193 Either<Component, StorageOperationStatus> toscaElement = toscaOperationFacade
1194 .getToscaElement(c.getUniqueId(), filter);
1195 if (toscaElement.isRight()) {
1196 System.out.println("getVerticesToValidate: failed to find element" + c.getUniqueId()
1197 + " staus is" + toscaElement.right().value());
1199 compListfull.add(toscaElement.left().value());
1201 this.titanDao.commit();
1204 compToFix.put(entry.getKey(), compListfull);
1210 } catch (Exception e) {
1211 log.info("Failed to fetch vf resources ", e);
1218 } catch (Exception ex) {
1224 private Either<Component, ToscaError> generateToscaArtifact(Component parent) {
1225 log.debug("tosca artifact generation");
1227 Map<String, ArtifactDefinition> toscaArtifacts = parent.getToscaArtifacts();
1229 ArtifactDefinition toscaArtifact = null;
1230 Optional<ArtifactDefinition> op = toscaArtifacts.values().stream()
1231 .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.TOSCA_TEMPLATE.getType())).findAny();
1233 if (op.isPresent()) {
1234 toscaArtifact = op.get();
1236 if (toscaArtifact != null) {
1237 log.debug("Migration1707ArtifactUuidFix generateToscaPerComponent artifact name {} id {} esId {}",
1238 toscaArtifact.getArtifactName(), toscaArtifact.getUniqueId(), toscaArtifact.getEsId());
1240 Either<ToscaRepresentation, ToscaError> exportComponent = toscaExportUtils.exportComponent(parent);
1241 if (exportComponent.isRight()) {
1242 log.debug("Failed export tosca yaml for component {} error {}", parent.getUniqueId(),
1243 exportComponent.right().value());
1245 return Either.right(exportComponent.right().value());
1247 log.debug("Tosca yaml exported for component {} ", parent.getUniqueId());
1248 String payload = exportComponent.left().value().getMainYaml();
1250 toscaArtifact.setPayloadData(payload);
1251 byte[] decodedPayload = toscaArtifact.getPayloadData();
1253 String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(parent.getUniqueId(),
1254 toscaArtifact.getArtifactLabel());
1255 toscaArtifact.setUniqueId(uniqueId);
1256 toscaArtifact.setEsId(toscaArtifact.getUniqueId());
1258 toscaArtifact.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(decodedPayload));
1259 ESArtifactData artifactData = new ESArtifactData(toscaArtifact.getEsId(), decodedPayload);
1260 artifactCassandraDao.saveArtifact(artifactData);
1262 log.debug("Tosca yaml artifact esId {} ", toscaArtifact.getEsId());
1264 ArtifactDefinition csarArtifact = null;
1265 op = toscaArtifacts.values().stream()
1266 .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.TOSCA_CSAR.getType())).findAny();
1268 if (op.isPresent()) {
1269 csarArtifact = op.get();
1272 if (csarArtifact != null) {
1273 Either<byte[], ResponseFormat> generated = csarUtils.createCsar(parent, true, true);
1275 if (generated.isRight()) {
1276 log.debug("Failed to export tosca csar for component {} error {}", parent.getUniqueId(),
1277 generated.right().value());
1279 return Either.right(ToscaError.GENERAL_ERROR);
1281 byte[] value = generated.left().value();
1282 csarArtifact.setPayload(value);
1283 byte[] decodedPayload = csarArtifact.getPayloadData();
1285 String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(parent.getUniqueId(),
1286 csarArtifact.getArtifactLabel());
1287 csarArtifact.setUniqueId(uniqueId);
1288 csarArtifact.setEsId(csarArtifact.getUniqueId());
1290 csarArtifact.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(decodedPayload));
1291 ESArtifactData artifactData = new ESArtifactData(csarArtifact.getEsId(), decodedPayload);
1292 artifactCassandraDao.saveArtifact(artifactData);
1293 log.debug("Tosca csar artifact esId {} ", csarArtifact.getEsId());
1297 } catch (Exception ex) {
1298 log.error("Failed to generate tosca atifact component id {} component name {} error {}",
1299 parent.getUniqueId(), parent.getName(), ex.getMessage());
1301 return Either.right(ToscaError.GENERAL_ERROR);
1304 return Either.left(parent);
1307 private ArtifactDefinition createVfModuleArtifact(ComponentInstance currVF, Service service) {
1309 ArtifactDefinition vfModuleArtifactDefinition = new ArtifactDefinition();
1310 String newCheckSum = null;
1312 vfModuleArtifactDefinition.setDescription("Auto-generated VF Modules information artifact");
1313 vfModuleArtifactDefinition.setArtifactDisplayName("Vf Modules Metadata");
1314 vfModuleArtifactDefinition.setArtifactType(ArtifactTypeEnum.VF_MODULES_METADATA.getType());
1315 vfModuleArtifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
1316 vfModuleArtifactDefinition.setArtifactLabel("vfModulesMetadata");
1317 vfModuleArtifactDefinition.setTimeout(0);
1318 vfModuleArtifactDefinition.setArtifactName(currVF.getNormalizedName() + "_modules.json");
1320 return vfModuleArtifactDefinition;
1324 private void fillVfModuleInstHeatEnvPayload(Component parent, ComponentInstance instance, List<GroupInstance> groupsForCurrVF,
1325 ArtifactDefinition vfModuleArtifact) {
1326 log.debug("generate new vf module for component. name {}, id {}, Version {}", instance.getName(), instance.getUniqueId());
1328 String uniqueId = UniqueIdBuilder.buildInstanceArtifactUniqueId(parent.getUniqueId(), instance.getUniqueId(), vfModuleArtifact.getArtifactLabel());
1330 vfModuleArtifact.setUniqueId(uniqueId);
1331 vfModuleArtifact.setEsId(vfModuleArtifact.getUniqueId());
1333 List<VfModuleArtifactPayload> vfModulePayloadForCurrVF = new ArrayList<>();
1334 if (groupsForCurrVF != null) {
1335 for (GroupInstance groupInstance : groupsForCurrVF) {
1336 VfModuleArtifactPayload modulePayload = new VfModuleArtifactPayload(groupInstance);
1337 vfModulePayloadForCurrVF.add(modulePayload);
1339 Collections.sort(vfModulePayloadForCurrVF,
1340 (art1, art2) -> VfModuleArtifactPayload.compareByGroupName(art1, art2));
1342 final Gson gson = new GsonBuilder().setPrettyPrinting().create();
1344 String vfModulePayloadString = gson.toJson(vfModulePayloadForCurrVF);
1345 log.debug("vfModulePayloadString {}", vfModulePayloadString);
1346 if (vfModulePayloadString != null) {
1347 String newCheckSum = GeneralUtility
1348 .calculateMD5Base64EncodedByByteArray(vfModulePayloadString.getBytes());
1349 vfModuleArtifact.setArtifactChecksum(newCheckSum);
1351 ESArtifactData artifactData = new ESArtifactData(vfModuleArtifact.getEsId(),
1352 vfModulePayloadString.getBytes());
1353 artifactCassandraDao.saveArtifact(artifactData);
1361 private Either<List<VfModuleArtifactPayloadEx>, StorageOperationStatus> parseVFModuleJson(ArtifactDefinition vfModuleArtifact) {
1362 log.info("Try to get vfModule json from cassandra {}", vfModuleArtifact.getEsId());
1363 Either<ESArtifactData, CassandraOperationStatus> vfModuleData = artifactCassandraDao.getArtifact(vfModuleArtifact.getEsId());
1365 if (vfModuleData.isRight()) {
1366 CassandraOperationStatus resourceUploadStatus = vfModuleData.right().value();
1367 StorageOperationStatus storageResponse = DaoStatusConverter.convertCassandraStatusToStorageStatus(resourceUploadStatus);
1368 log.error("failed to fetch vfModule json {} from cassandra. Status is {}", vfModuleArtifact.getEsId(), storageResponse);
1369 return Either.right(storageResponse);
1373 ESArtifactData esArtifactData = vfModuleData.left().value();
1374 String gsonData = new String( esArtifactData.getDataAsArray());
1375 final Gson gson = new GsonBuilder().setPrettyPrinting().create();
1376 JsonArray jsonElement = new JsonArray();
1377 jsonElement = gson.fromJson(gsonData, jsonElement.getClass());
1378 List<VfModuleArtifactPayloadEx> vfModules = new ArrayList<>();
1379 jsonElement.forEach(je ->{
1380 VfModuleArtifactPayloadEx vfModule = ComponentsUtils.parseJsonToObject(je.toString(), VfModuleArtifactPayloadEx.class);
1381 vfModules.add(vfModule);
1384 log.debug ("parse vf module finish {}", gsonData);
1385 return Either.left(vfModules);
1389 /*public boolean manualFix() {
1391 Set<String> fixedIds = new HashSet<>();
1392 Component component;
1393 String componentId = "86683566-20e8-4cc5-872d-12abca1d57f0";//"9f6a6976-18e3-488a-98a4-c1aade480739";
1394 Either<Resource, StorageOperationStatus> toscaElement = toscaOperationFacade.getToscaFullElement(componentId);
1395 if (toscaElement.isRight()) {
1396 log.info("Failed to fetch resources {} {}", componentId, toscaElement.right().value());
1399 boolean res = generateToscaPerComponent(fixedIds, toscaElement.left().value());
1400 log.info("Fix component return res {} ", res);
1407 public boolean manualCheck() {
1409 Set<String> fixedIds = new HashSet<>();
1410 Component component;
1411 String componentId = "86d50186-7b00-4bfc-abcb-9e4c6892f338";//"9f6a6976-18e3-488a-98a4-c1aade480739";
1412 Either<Service, StorageOperationStatus> toscaElement = toscaOperationFacade.getToscaFullElement(componentId);
1413 if (toscaElement.isRight()) {
1414 log.info("Failed to fetch resources {} {}", componentId, toscaElement.right().value());
1419 res = isProblematicService(toscaElement.left().value(), toscaElement.left().value().getName());
1421 List<Service> services = new ArrayList<>();
1422 services.add(toscaElement.left().value());
1423 this.fix(new ArrayList<Resource>(), services, null, null, null);
1425 Either<Service, StorageOperationStatus> toscaElementNew = toscaOperationFacade.getToscaFullElement(componentId);
1426 if (toscaElement.isRight()) {
1427 log.info("Failed to fetch resources {} {}", componentId, toscaElementNew.right().value());
1430 res = isProblematicService(toscaElementNew.left().value(), toscaElementNew.left().value().getName());
1434 } catch (IOException e) {
1435 // TODO Auto-generated catch block
1436 e.printStackTrace();
1438 log.info("Fix component return res {} ", res);