1 package org.openecomp.sdc.asdctool.impl;
3 import com.google.gson.Gson;
4 import com.google.gson.GsonBuilder;
6 import java.io.BufferedWriter;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.io.OutputStreamWriter;
10 import java.io.Writer;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
16 import java.util.Optional;
18 import java.util.stream.Collectors;
19 import org.openecomp.sdc.be.components.distribution.engine.VfModuleArtifactPayload;
20 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
21 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
22 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
23 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
24 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
25 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
26 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
27 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
28 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
29 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
30 import org.openecomp.sdc.be.datatypes.elements.MapGroupsDataDefinition;
31 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
32 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
33 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
34 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
35 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
36 import org.openecomp.sdc.be.model.ArtifactDefinition;
37 import org.openecomp.sdc.be.model.Component;
38 import org.openecomp.sdc.be.model.ComponentInstance;
39 import org.openecomp.sdc.be.model.ComponentParametersView;
40 import org.openecomp.sdc.be.model.DistributionStatusEnum;
41 import org.openecomp.sdc.be.model.GroupDefinition;
42 import org.openecomp.sdc.be.model.GroupInstance;
43 import org.openecomp.sdc.be.model.LifecycleStateEnum;
44 import org.openecomp.sdc.be.model.Resource;
45 import org.openecomp.sdc.be.model.Service;
46 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
47 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
48 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
49 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
50 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
51 import org.openecomp.sdc.be.resources.data.ESArtifactData;
52 import org.openecomp.sdc.be.tosca.CsarUtils;
53 import org.openecomp.sdc.be.tosca.ToscaError;
54 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
55 import org.openecomp.sdc.be.tosca.ToscaRepresentation;
56 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
57 import org.openecomp.sdc.common.api.Constants;
58 import org.openecomp.sdc.common.util.GeneralUtility;
59 import org.openecomp.sdc.exception.ResponseFormat;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62 import org.springframework.beans.factory.annotation.Autowired;
64 @org.springframework.stereotype.Component("artifactUuidFix")
65 public class ArtifactUuidFix {
68 private TitanDao titanDao;
71 private ToscaOperationFacade toscaOperationFacade;
73 private ToscaExportHandler toscaExportUtils;
75 private ArtifactCassandraDao artifactCassandraDao;
78 private CsarUtils csarUtils;
80 private static Logger log = LoggerFactory.getLogger(ArtifactUuidFix.class.getName());
82 public boolean doFix(String fixComponent, String runMode) {
83 List<Resource> vfLst = new ArrayList<>();
84 List<Service> serviceList = new ArrayList<>();
85 Map<String, List<Component>> nodeToFixTosca = new HashMap<>();
86 Map<String, List<Component>> vfToFixTosca = new HashMap<>();
87 Map<String, List<Component>> serviceToFixTosca = new HashMap<>();
89 long time = System.currentTimeMillis();
91 doFixTosca(nodeToFixTosca, vfToFixTosca, serviceToFixTosca);
93 if (fixComponent.equals("vf_only")) {
94 if (!fetchFaultVf(vfLst, time)) {
98 if (!fetchServices(fixComponent, serviceList, time)) {
102 if (runMode.equals("service_vf") || runMode.equals("fix")) {
103 log.info("Mode {}. Find problem VFs", runMode);
104 if (!fetchVf(serviceList, vfLst, time)) {
105 log.info("Mode {}. Find problem VFs finished with failure", runMode);
108 log.info("Mode {}. Find problem VFs finished with success", runMode);
110 if (runMode.equals("fix") || runMode.equals("fix_only_services")) {
111 log.info("Mode {}. Start fix", runMode);
112 if (!fix(vfLst, serviceList, nodeToFixTosca, vfToFixTosca, serviceToFixTosca)) {
113 log.info("Mode {}. Fix finished with failure", runMode);
116 log.info("Mode {}. Fix finished with success", runMode);
121 private boolean fetchFaultVf(List<Resource> vfLst, long time) {
122 log.info("Find fault VF ");
123 String fileName = "fault_" + time + ".csv";
125 try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"))) {
126 writer.write("vf name, vf id, state, version\n");
128 Map<GraphPropertyEnum, Object> hasProps = new HashMap<>();
129 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
130 hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF.name());
132 Map<GraphPropertyEnum, Object> hasNotProps = new HashMap<>();
133 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
134 log.info("Try to fetch resources with properties {} and not {}", hasProps, hasNotProps);
136 Either<List<GraphVertex>, TitanOperationStatus> servicesByCriteria = titanDao
137 .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll);
138 if (servicesByCriteria.isRight()) {
139 log.info("Failed to fetch resources {}", servicesByCriteria.right().value());
142 List<GraphVertex> resources = servicesByCriteria.left().value();
143 for (GraphVertex gv : resources) {
144 ComponentParametersView filter = new ComponentParametersView(true);
145 filter.setIgnoreComponentInstances(false);
146 filter.setIgnoreArtifacts(false);
147 filter.setIgnoreGroups(false);
149 Either<Resource, StorageOperationStatus> toscaElement = toscaOperationFacade
150 .getToscaElement(gv.getUniqueId());
151 if (toscaElement.isRight()) {
152 log.info("Failed to fetch resources {} {}", gv.getUniqueId(), toscaElement.right().value());
156 Resource resource = toscaElement.left().value();
157 String resourceName = resource.getName();
158 Map<String, ArtifactDefinition> deploymentArtifacts = resource.getDeploymentArtifacts();
159 List<GroupDefinition> groups = resource.getGroups();
160 if (groups == null || groups.isEmpty()) {
161 log.info("No groups for resource {} id {} ", resourceName, gv.getUniqueId());
164 boolean isProblematic = false;
165 for (GroupDefinition gr : groups) {
166 if ((gr.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE))
167 && (isProblematicGroup(gr, resourceName, deploymentArtifacts))) {
168 isProblematic = true;
174 writeModuleResultToFile(writer, resource, null);
180 } catch (Exception e) {
181 log.info("Failed to fetch vf resources ", e);
189 private boolean fetchVf(List<Service> serviceList, List<Resource> vfLst, long time) {
190 log.info("Find problem VF ");
191 if (serviceList.isEmpty()) {
192 log.info("No services as input");
195 String fileName = "problemVf_" + time + ".csv";
197 try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"))) {
198 writer.write("vf name, vf id, state, version, example service name\n");
199 Set<String> vfIds = new HashSet<>();
200 for (Service service : serviceList) {
201 List<ComponentInstance> componentInstances = service.getComponentInstances().stream()
202 .filter(ci -> ci.getOriginType().equals(OriginTypeEnum.VF)).collect(Collectors.toList());
203 for (ComponentInstance ci : componentInstances) {
204 if (!vfIds.contains(ci.getComponentUid())) {
205 vfIds.add(ci.getComponentUid());
206 Either<Resource, StorageOperationStatus> toscaElement = toscaOperationFacade
207 .getToscaElement(ci.getComponentUid());
208 if (toscaElement.isRight()) {
209 log.info("Failed to fetch resource {} {}", ci.getComponentUid(),
210 toscaElement.right().value());
213 Resource resource = toscaElement.left().value();
214 if (resource.getResourceType().equals(ResourceTypeEnum.VF)) {
216 writeModuleResultToFile(writer, resource, service);
223 log.info("output file with list of Vf : {}", fileName);
224 } catch (Exception e) {
225 log.info("Failed to fetch services ", e);
233 private boolean fetchServices(String fixServices, List<Service> serviceList, long time) {
234 log.info("Find problem Services {}", fixServices);
235 String fileName = "problemService_" + time + ".csv";
237 try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"))) {
238 writer.write("service name, service id, state, version\n");
240 Map<GraphPropertyEnum, Object> hasProps = new HashMap<>();
241 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
242 if (fixServices.equals("distributed_only")) {
243 hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
244 hasProps.put(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTED.name());
247 Map<GraphPropertyEnum, Object> hasNotProps = new HashMap<>();
248 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
249 log.info("Try to fetch services with properties {} and not {}", hasProps, hasNotProps);
251 Either<List<GraphVertex>, TitanOperationStatus> servicesByCriteria = titanDao
252 .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll);
253 if (servicesByCriteria.isRight()) {
254 log.info("Failed to fetch services {}", servicesByCriteria.right().value());
258 List<GraphVertex> services = servicesByCriteria.left().value();
259 for (GraphVertex gv : services) {
260 ComponentParametersView filter = new ComponentParametersView(true);
261 filter.setIgnoreComponentInstances(false);
262 filter.setIgnoreArtifacts(false);
263 filter.setIgnoreGroups(false);
265 Either<Service, StorageOperationStatus> toscaElement = toscaOperationFacade
266 .getToscaElement(gv.getUniqueId());
267 if (toscaElement.isRight()) {
268 log.info("Failed to fetch service {} {}", gv.getUniqueId(), toscaElement.right().value());
272 Service service = toscaElement.left().value();
273 List<ComponentInstance> componentInstances = service.getComponentInstances();
274 boolean isProblematic = false;
275 if (componentInstances == null) {
276 log.info("No instances for service {} ", gv.getUniqueId());
279 String serviceName = (String) gv.getMetadataProperty(GraphPropertyEnum.NAME);
281 for (ComponentInstance ci : componentInstances) {
282 Map<String, ArtifactDefinition> deploymentArtifacts = ci.getDeploymentArtifacts();
283 List<GroupInstance> groupInstances = ci.getGroupInstances();
284 if (groupInstances == null || groupInstances.isEmpty()) {
285 log.info("No instance groups for instance {} in service {} id {} ", ci.getName(), serviceName,
290 for (GroupInstance gi : groupInstances) {
291 if (gi.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)
292 && isProblematicGroupInstance(gi, ci.getName(), serviceName, deploymentArtifacts)) {
293 isProblematic = true;
298 serviceList.add(service);
299 writeModuleResultToFile(writer, service, null);
306 log.info("output file with list of services : {}", fileName);
307 } catch (Exception e) {
308 log.info("Failed to fetch services ", e);
316 private boolean isProblematicGroup(GroupDefinition gr, String resourceName,
317 Map<String, ArtifactDefinition> deploymentArtifacts) {
318 List<String> artifacts = gr.getArtifacts();
319 List<String> artifactsUuid = gr.getArtifactsUuid();
321 if (artifactsUuid == null || artifactsUuid.isEmpty() || artifacts == null || artifacts.isEmpty()) {
322 log.info("No groups in resource {} ", resourceName);
325 Set<String> artifactsSet = new HashSet<>(artifacts);
326 if (artifactsSet.size() < artifacts.size()) {
327 log.info(" artifactsSet.size() < artifacts.size() group {} in resource {} ", gr.getName(), resourceName);
330 if (artifacts.size() < artifactsUuid.size()) {
331 log.info(" artifacts.size() < artifactsUuid.size() group {} in resource {} ", gr.getName(), resourceName);
334 if (artifactsUuid.contains(null)) {
335 log.info(" artifactsUuid.contains(null) group {} in resource {} ", gr.getName(), resourceName);
339 for (String artifactId : artifacts) {
340 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
341 ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactlabel);
342 if (artifactDefinition == null) {
343 log.info(" artifactDefinition == null label {} group {} in resource {} ", artifactlabel, gr.getName(),
347 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactDefinition.getArtifactType());
348 if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
349 if (!artifactId.equals(artifactDefinition.getUniqueId())) {
351 " !artifactId.equals(artifactDefinition.getUniqueId() artifact {} artId {} group {} in resource {} ",
352 artifactlabel, artifactId, gr.getName(), resourceName);
355 if (!artifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
357 " artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} group {} in resource {} ",
358 artifactlabel, gr.getName(), resourceName);
364 for (String artifactUuid : artifactsUuid) {
365 String label = findArtifactLabelFromArtifactId(artifactUuid);
366 if (label != null && !label.isEmpty()) {
373 private boolean isProblematicGroupInstance(GroupInstance gi, String instName, String servicename,
374 Map<String, ArtifactDefinition> deploymentArtifacts) {
375 List<String> artifacts = gi.getArtifacts();
376 List<String> artifactsUuid = gi.getArtifactsUuid();
377 List<String> instArtifactsUuid = gi.getGroupInstanceArtifactsUuid();
378 List<String> instArtifactsId = gi.getGroupInstanceArtifacts();
379 Set<String> instArtifatIdSet = new HashSet<>();
381 if (artifactsUuid == null || artifactsUuid.isEmpty() || artifacts == null || artifacts.isEmpty()) {
382 log.info("No instance groups for instance {} in service {} ", instName, servicename);
385 Set<String> artifactsSet = new HashSet<>(artifacts);
386 if (artifactsSet.size() < artifacts.size()) {
387 log.info(" artifactsSet.size() < artifacts.size() group {} in resource {} ", instName, servicename);
390 if (instArtifactsId != null && !instArtifactsId.isEmpty()) {
391 instArtifatIdSet.addAll(instArtifactsId);
393 if (artifacts.size() < artifactsUuid.size()) {
394 log.info(" artifacts.size() < artifactsUuid.size() inst {} in service {} ", instName, servicename);
397 if (artifactsUuid.contains(null)) {
398 log.info(" artifactsUuid.contains(null) inst {} in service {} ", instName, servicename);
401 if (instArtifactsId != null && instArtifatIdSet.size() < instArtifactsId.size()) {
402 log.info(" instArtifatIdSet.size() < instArtifactsId.size() inst {} in service {} ", instName, servicename);
405 if ((instArtifactsId != null && instArtifactsUuid != null)
406 && instArtifactsId.size() != instArtifactsUuid.size()) {
407 log.info(" instArtifactsId.size() != instArtifactsUuid.size() inst {} in service {} ", instName,
412 for (String artifactId : artifacts) {
413 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
414 ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactlabel);
415 if (artifactDefinition == null) {
416 log.info(" artifactDefinition == null label {} inst {} in service {} ", artifactlabel, instName,
420 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactDefinition.getArtifactType());
421 if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
422 if (!artifactId.equals(artifactDefinition.getUniqueId())) {
424 " !artifactId.equals(artifactDefinition.getUniqueId() artifact {} artId {} inst {} in service {} ",
425 artifactlabel, artifactId, instName, servicename);
428 if (!artifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
430 " artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ",
431 artifactlabel, instName, servicename);
435 if (instArtifactsUuid == null || instArtifactsUuid.isEmpty()) {
436 log.info(" instArtifactsUuid empty. label {} inst {} in service {} ", artifactlabel, instName,
440 if (!instArtifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
442 " instArtifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ",
443 artifactlabel, instName, servicename);
448 for (String artifactUuid : artifactsUuid) {
449 String label = findArtifactLabelFromArtifactId(artifactUuid);
450 if (label != null && !label.isEmpty()) {
457 private boolean fix(List<Resource> vfLst, List<Service> serviceList, Map<String, List<Component>> nodesToFixTosca,
458 Map<String, List<Component>> vfToFixTosca, Map<String, List<Component>> servicesToFixTosca) {
460 log.info(" Fix started ***** ");
461 if (vfLst != null && !vfLst.isEmpty()) {
464 if (res && serviceList != null && !serviceList.isEmpty()) {
465 res = fixServices(serviceList);
468 Set<String> fixedIds = new HashSet<>();
469 if (res && nodesToFixTosca != null && !nodesToFixTosca.isEmpty()) {
470 generateAndSaveToscaArtifacts(nodesToFixTosca, fixedIds, null);
472 for (Map.Entry<String, List<Component>> entry : nodesToFixTosca.entrySet()) {
473 List<Component> components = entry.getValue();
474 for (Component c : components) {
476 ToscaElement topologyTemplate = ModelConverter.convertToToscaElement(c);
477 Map<String, ArtifactDataDefinition> arifacts = topologyTemplate.getToscaArtifacts();
478 res = fixDataOnGraph(c.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS, EdgeLabelEnum.TOSCA_ARTIFACTS,
484 if (res && vfToFixTosca != null && !vfToFixTosca.isEmpty()) {
485 generateAndSaveToscaArtifacts(vfToFixTosca, fixedIds, vfLst);
487 for (Map.Entry<String, List<Component>> entry : vfToFixTosca.entrySet()) {
488 List<Component> components = entry.getValue();
489 for (Component c : components) {
490 TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(c);
491 Map<String, ArtifactDataDefinition> arifacts = topologyTemplate.getToscaArtifacts();
492 res = fixDataOnGraph(c.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS, EdgeLabelEnum.TOSCA_ARTIFACTS,
499 if (res && servicesToFixTosca != null && !servicesToFixTosca.isEmpty()) {
500 generateAndSaveToscaArtifacts(servicesToFixTosca, fixedIds, serviceList);
502 for (Map.Entry<String, List<Component>> entry : servicesToFixTosca.entrySet()) {
503 List<Component> components = entry.getValue();
504 for (Component c : components) {
505 TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(c);
506 Map<String, ArtifactDataDefinition> arifacts = topologyTemplate.getToscaArtifacts();
507 res = fixDataOnGraph(c.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS, EdgeLabelEnum.TOSCA_ARTIFACTS,
515 for (Component component : vfLst) {
516 generateToscaPerComponent(fixedIds, component);
518 TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(component);
519 Map<String, GroupDataDefinition> groups = topologyTemplate.getGroups();
520 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.GROUPS, EdgeLabelEnum.GROUPS, groups);
522 Map<String, ArtifactDataDefinition> arifacts = topologyTemplate.getDeploymentArtifacts();
523 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.DEPLOYMENT_ARTIFACTS,
524 EdgeLabelEnum.DEPLOYMENT_ARTIFACTS, arifacts);
527 Map<String, ArtifactDataDefinition> arifacts = topologyTemplate.getToscaArtifacts();
528 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS,
529 EdgeLabelEnum.TOSCA_ARTIFACTS, arifacts);
536 for (Component component : serviceList) {
537 generateToscaPerComponent(fixedIds, component);
539 TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(component);
540 Map<String, MapGroupsDataDefinition> groups = topologyTemplate.getInstGroups();
541 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS,
545 Map<String, MapArtifactDataDefinition> artifacts = topologyTemplate.getInstDeploymentArtifacts();
546 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS,
547 EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, artifacts);
550 Map<String, ArtifactDataDefinition> arifacts = topologyTemplate.getToscaArtifacts();
551 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS,
552 EdgeLabelEnum.TOSCA_ARTIFACTS, arifacts);
557 log.info(" Fix finished with res {} ***** ", res);
561 private boolean generateAndSaveToscaArtifacts(Map<String, List<Component>> nodesToFixTosca, Set<String> fixedIds,
562 List<? extends Component> componentsWithFailedGroups) {
564 log.debug("Migration1707ArtifactUuidFix generateAndSaveToscaArtifacts started ");
565 for (Map.Entry<String, List<Component>> entry : nodesToFixTosca.entrySet()) {
567 List<Component> component = entry.getValue();
568 for (Component c : component) {
569 log.debug("Migration1707ArtifactUuidFix fix tosca on component : id {}, name {} ", c.getUniqueId(),
571 if (componentsWithFailedGroups != null) {
572 Optional<Component> op = (Optional<Component>) componentsWithFailedGroups.stream()
573 .filter(cg -> cg.getUniqueId().equals(c.getUniqueId())).findAny();
574 if (!op.isPresent()) {
575 res = generateToscaPerComponent(fixedIds, c);
578 res = generateToscaPerComponent(fixedIds, c);
582 log.debug("Migration1707ArtifactUuidFix generateAndSaveToscaArtifacts finished with res {} ", res);
586 private boolean generateToscaPerComponent(Set<String> fixedIds, Component c) {
588 log.debug("Migration1707ArtifactUuidFix generateToscaPerComponent started component name {} id {}",
589 c.getName(), c.getUniqueId());
591 Either<Component, StorageOperationStatus> toscaElement = toscaOperationFacade
592 .getToscaFullElement(c.getUniqueId());
593 if (toscaElement.isRight()) {
594 log.info("Failed to fetch resources {} {}", c.getUniqueId(), toscaElement.right().value());
597 Component toscaElementFull = toscaElement.left().value();
598 toscaElementFull.setGroups(c.getGroups());
599 List<ComponentInstance> ciListFull = toscaElementFull.getComponentInstances();
600 List<ComponentInstance> ciList = c.getComponentInstances();
601 if (ciListFull != null && !ciListFull.isEmpty()) {
602 ciListFull.forEach(ciFull -> ciList.stream()
603 .filter(ci -> ci.getUniqueId().equals(ciFull.getUniqueId()))
605 .ifPresent(compInst -> ciFull.setGroupInstances(compInst.getGroupInstances())));
608 Map<String, ArtifactDefinition> toscaArtifacts = c.getToscaArtifacts();
609 log.debug("Migration1707ArtifactUuidFix generateToscaPerComponent tocsa artifacts size {}",
610 toscaArtifacts.size());
612 Either<ArtifactDefinition, ToscaError> either = Either.right(ToscaError.GENERAL_ERROR);
613 ArtifactDefinition toscaArtifact = null;
614 Optional<ArtifactDefinition> op = toscaArtifacts.values().stream()
615 .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.TOSCA_TEMPLATE.getType())).findAny();
617 if (op.isPresent()) {
618 toscaArtifact = op.get();
621 if (toscaArtifact != null) {
622 log.debug("Migration1707ArtifactUuidFix generateToscaPerComponent artifact name {} id {} esId {}",
623 toscaArtifact.getArtifactName(), toscaArtifact.getUniqueId(), toscaArtifact.getEsId());
624 either = generateToscaArtifact(toscaElementFull, toscaArtifact);
625 if (either.isRight()) {
626 log.error("Couldn't generate and save tosca template component unique id {}, name {} error: {}",
627 toscaElementFull.getUniqueId(), toscaElementFull.getName(), either.right().value());
633 ArtifactDefinition csarArtifact = null;
634 op = toscaArtifacts.values().stream()
635 .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.TOSCA_CSAR.getType())).findAny();
637 if (op.isPresent()) {
638 csarArtifact = op.get();
641 if (csarArtifact != null) {
642 log.debug("Migration1707ArtifactUuidFix generateToscaPerComponent artifact name {} id {} esId {}",
643 csarArtifact.getArtifactName(), csarArtifact.getUniqueId(), csarArtifact.getEsId());
644 either = generateToscaArtifact(toscaElementFull, csarArtifact);
645 if (either.isRight()) {
646 log.error("Couldn't generate and save tosca csar for component uuid {}, id {}, name {}. error: {}",
647 toscaElementFull.getUUID(), toscaElementFull.getUniqueId(), toscaElementFull.getName(), either.right().value());
653 c.setToscaArtifacts(toscaArtifacts);
656 fixedIds.add(toscaElementFull.getUniqueId());
661 log.debug("Migration1707ArtifactUuidFix generateToscaPerComponent finished component name {} id {} res {}",
662 c.getName(), c.getUniqueId(), res);
666 private <T extends ToscaDataDefinition> boolean fixDataOnGraph(String componentId, VertexTypeEnum vertexTypeEnum,
667 EdgeLabelEnum edgeLabelEnum, Map<String, T> groups) {
668 log.debug("amount groups to update: VertexTypeEnum {} EdgeLabelEnum {} data size {}", vertexTypeEnum.getName(),
669 edgeLabelEnum, groups.size());
671 Either<GraphVertex, TitanOperationStatus> getResponse = titanDao.getVertexById(componentId,
672 JsonParseFlagEnum.NoParse);
673 if (getResponse.isRight()) {
674 log.debug("Couldn't fetch component unique id {}, error: {}", componentId, getResponse.right().value());
678 GraphVertex componentVertex = getResponse.left().value();
679 GraphVertex toscaDataVertex;
680 Either<GraphVertex, TitanOperationStatus> groupVertexEither = titanDao.getChildVertex(componentVertex,
681 edgeLabelEnum, JsonParseFlagEnum.ParseJson);
682 if (groupVertexEither.isRight() && groupVertexEither.right().value() == TitanOperationStatus.NOT_FOUND) {
683 log.debug("no child {} vertex for component unique id {}, error: {}", edgeLabelEnum, componentId,
684 groupVertexEither.right().value());
687 if (groupVertexEither.isRight()) {
689 log.debug("failed to get child {} vertex for component unique id {}, error: {}", edgeLabelEnum,
690 componentId, groupVertexEither.right().value());
693 toscaDataVertex = groupVertexEither.left().value();
694 toscaDataVertex.setJson(groups);
695 Either<GraphVertex, TitanOperationStatus> updatevertexEither = titanDao.updateVertex(toscaDataVertex);
696 if (updatevertexEither.isRight()) {
697 log.debug("failed to update vertex for component unique id {}, error: {}", componentId,
698 updatevertexEither.right().value());
704 log.debug("Fix data on graph finished: VertexTypeEnum {} EdgeLabelEnum {} res {}", vertexTypeEnum.getName(),
709 private boolean fixServices(List<Service> serviceList) {
710 for (Service service : serviceList) {
711 log.debug("Migration1707ArtifactUuidFix fix service: id {}, name {} ", service.getUniqueId(),
713 List<ComponentInstance> instances = service.getComponentInstances();
714 for (ComponentInstance instance : instances) {
715 fixComponentInstances(service, instance);
721 private void fixComponentInstances(Service service, ComponentInstance instance) {
722 Map<String, ArtifactDefinition> artifactsMap = instance.getDeploymentArtifacts();
723 List<GroupInstance> groupsList = instance.getGroupInstances();
724 if (groupsList != null && artifactsMap != null) {
725 List<GroupInstance> groupsToDelete = new ArrayList<>();
726 for (GroupInstance group : groupsList) {
727 fixGroupInstances(service, artifactsMap, groupsToDelete, group);
730 if (!groupsToDelete.isEmpty()) {
731 log.debug("Migration1707ArtifactUuidFix delete group: resource id {}, group instance to delete {} ",
732 service.getUniqueId(), groupsToDelete);
733 groupsList.removeAll(groupsToDelete);
736 Optional<ArtifactDefinition> optionalVfModuleArtifact = artifactsMap.values().stream()
737 .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.VF_MODULES_METADATA.name())).findAny();
738 if (optionalVfModuleArtifact.isPresent()) {
739 ArtifactDefinition vfModuleAertifact = optionalVfModuleArtifact.get();
740 fillVfModuleInstHeatEnvPayload(groupsList, vfModuleAertifact);
745 private void fixGroupInstances(Service service, Map<String, ArtifactDefinition> artifactsMap,
746 List<GroupInstance> groupsToDelete, GroupInstance group) {
747 if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
748 log.debug("Migration1707ArtifactUuidFix fix group: resource id {}, group name {} ", service.getUniqueId(),
750 List<String> groupArtifacts = new ArrayList<>(group.getArtifacts());
752 group.getArtifacts().clear();
753 group.getArtifactsUuid().clear();
754 group.getGroupInstanceArtifacts().clear();
755 group.getGroupInstanceArtifactsUuid().clear();
757 for (String artifactId : groupArtifacts) {
758 fixArtifactUndergroupInstances(artifactsMap, group, groupArtifacts, artifactId);
760 if (group.getArtifacts() == null || group.getArtifacts().isEmpty()) {
762 "Migration1707ArtifactUuidFix fix groupInstance add to delete list: resource id {} name {} , group name {} ",
763 service.getUniqueId(), service.getName(), group.getName());
764 groupsToDelete.add(group);
769 private void fixArtifactUndergroupInstances(Map<String, ArtifactDefinition> artifactsMap, GroupInstance group,
770 List<String> groupArtifacts, String artifactId) {
771 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
772 log.debug("Migration1707ArtifactUuidFix fix group: group name {} artifactId for fix {} artifactlabel {} ",
773 group.getName(), artifactId, artifactlabel);
774 if (!artifactlabel.isEmpty() && artifactsMap.containsKey(artifactlabel)) {
775 ArtifactDefinition artifact = artifactsMap.get(artifactlabel);
776 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifact.getArtifactType());
777 String correctArtifactId = artifact.getUniqueId();
778 String correctArtifactUUID = artifact.getArtifactUUID();
779 if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
780 boolean isAddToGroup = true;
781 if (groupArtifacts.size() == 1 && artifactType == ArtifactTypeEnum.HEAT_ARTIFACT) {
782 isAddToGroup = false;
783 artifact.setArtifactType(ArtifactTypeEnum.OTHER.getType());
787 "Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} ",
788 group.getName(), correctArtifactId, correctArtifactUUID);
789 group.getArtifacts().add(correctArtifactId);
790 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
791 group.getArtifactsUuid().add(correctArtifactUUID);
796 "Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} ",
797 group.getName(), correctArtifactId, correctArtifactUUID);
798 group.getGroupInstanceArtifacts().add(correctArtifactId);
799 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
800 group.getGroupInstanceArtifactsUuid().add(correctArtifactUUID);
806 private boolean fixVf(List<Resource> vfLst) {
807 for (Resource resource : vfLst) {
808 log.debug("Migration1707ArtifactUuidFix fix resource: id {}, name {} ", resource.getUniqueId(),
810 Map<String, ArtifactDefinition> artifactsMap = resource.getDeploymentArtifacts();
811 List<GroupDefinition> groupsList = resource.getGroups();
812 List<GroupDefinition> groupsToDelete = new ArrayList<>();
813 if (groupsList != null && artifactsMap != null) {
814 for (GroupDefinition group : groupsList) {
815 if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE) && group.getArtifacts() != null) {
816 fixVfGroup(resource, artifactsMap, group);
818 if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)
819 && (group.getArtifacts() == null || group.getArtifacts().isEmpty())) {
821 "Migration1707ArtifactUuidFix add group to delete list fix resource: id {}, name {} ",
822 resource.getUniqueId(), resource.getName(), group.getName());
823 groupsToDelete.add(group);
826 if (!groupsToDelete.isEmpty()) {
827 groupsList.removeAll(groupsToDelete);
834 private void fixVfGroup(Resource resource, Map<String, ArtifactDefinition> artifactsMap, GroupDefinition group) {
835 log.debug("Migration1707ArtifactUuidFix fix group: resource id {}, group name {} ", resource.getUniqueId(),
837 List<String> groupArtifacts = new ArrayList<>(group.getArtifacts());
839 for (String artifactId : groupArtifacts) {
840 fixArtifactUnderGroup(artifactsMap, group, groupArtifacts, artifactId);
844 private void fixArtifactUnderGroup(Map<String, ArtifactDefinition> artifactsMap, GroupDefinition group,
845 List<String> groupArtifacts, String artifactId) {
846 group.getArtifacts().clear();
847 group.getArtifactsUuid().clear();
848 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
849 log.debug("Migration1707ArtifactUuidFix fix group: group name {} artifactId for fix {} artifactlabel {} ",
850 group.getName(), artifactId, artifactlabel);
851 if (!artifactlabel.isEmpty() && artifactsMap.containsKey(artifactlabel)) {
852 ArtifactDefinition artifact = artifactsMap.get(artifactlabel);
853 String correctArtifactId = artifact.getUniqueId();
854 String correctArtifactUUID = artifact.getArtifactUUID();
855 boolean isAddToGroup = true;
856 if (groupArtifacts.size() == 1) {
857 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifact.getArtifactType());
858 if (artifactType == ArtifactTypeEnum.HEAT_ARTIFACT) {
859 isAddToGroup = false;
860 artifact.setArtifactType(ArtifactTypeEnum.OTHER.getType());
865 "Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} ",
866 group.getName(), correctArtifactId, correctArtifactUUID);
867 group.getArtifacts().add(correctArtifactId);
868 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
869 group.getArtifactsUuid().add(correctArtifactUUID);
875 private String findArtifactLabelFromArtifactId(String artifactId) {
876 String artifactLabel = "";
877 int index = artifactId.lastIndexOf('.');
878 if (index > 0 && index + 1 < artifactId.length()) {
879 artifactLabel = artifactId.substring(index + 1);
881 return artifactLabel;
884 private void writeModuleResultToFile(Writer writer, org.openecomp.sdc.be.model.Component component,
887 // "service name, service id, state, version
888 StringBuilder sb = new StringBuilder(component.getName());
889 sb.append(",").append(component.getUniqueId()).append(",").append(component.getLifecycleState()).append(",")
890 .append(component.getVersion());
891 if (service != null) {
892 sb.append(",").append(service.getName());
895 writer.write(sb.toString());
896 } catch (IOException e) {
897 log.error(e.getMessage(), e);
901 private void writeModuleResultToFile(Writer writer, List<Component> components) {
903 // "service name, service id, state, version
904 for (Component component : components) {
905 StringBuilder sb = new StringBuilder(component.getName());
906 sb.append(",").append(component.getUniqueId())
907 .append(",").append(component.getInvariantUUID())
908 .append(",").append(component.getLifecycleState())
909 .append(",").append(component.getVersion())
911 writer.write(sb.toString());
913 } catch (IOException e) {
914 log.error(e.getMessage(), e);
918 public boolean doFixTosca(Map<String, List<Component>> nodeToFix, Map<String, List<Component>> vfToFix,
919 Map<String, List<Component>> serviceToFix) {
921 Map<GraphPropertyEnum, Object> hasProps = new HashMap<>();
922 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
923 hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
925 Map<String, List<Component>> vertices = getVerticesToValidate(VertexTypeEnum.NODE_TYPE, hasProps);
926 boolean result = validateTosca(vertices, nodeToFix, "RESOURCE_TOSCA_ARTIFACTS");
929 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
930 hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF);
931 hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
933 vertices = getVerticesToValidate(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps);
934 result = validateTosca(vertices, vfToFix, "VF_TOSCA_ARTIFACTS");
937 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
938 hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
940 vertices = getVerticesToValidate(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps);
941 result = validateTosca(vertices, serviceToFix, "SERVICE_TOSCA_ARTIFACTS");
946 public Map<String, List<Component>> getVerticesToValidate(VertexTypeEnum type,
947 Map<GraphPropertyEnum, Object> hasProps) {
949 Map<String, List<Component>> result = new HashMap<>();
951 Either<List<GraphVertex>, TitanOperationStatus> resultsEither = titanDao.getByCriteria(type, hasProps);
952 if (resultsEither.isRight()) {
953 log.info("getVerticesToValidate failed " + resultsEither.right().value());
956 log.info("getVerticesToValidate: " + resultsEither.left().value().size() + " vertices to scan");
957 List<GraphVertex> componentsList = resultsEither.left().value();
958 componentsList.forEach(vertex -> {
959 String ivariantUuid = (String) vertex.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID);
960 if (!result.containsKey(ivariantUuid)) {
961 result.put(ivariantUuid, new ArrayList<>());
963 List<Component> compList = result.get(ivariantUuid);
965 ComponentParametersView filter = new ComponentParametersView(true);
966 filter.setIgnoreArtifacts(false);
968 Either<Component, StorageOperationStatus> toscaElement = toscaOperationFacade
969 .getToscaElement(vertex.getUniqueId(), filter);
970 if (toscaElement.isRight()) {
971 log.info("getVerticesToValidate: failed to find element" + vertex.getUniqueId()
972 + " staus is" + toscaElement.right().value());
974 compList.add(toscaElement.left().value());
978 } catch (Exception e) {
979 log.info("Failed to fetch vf resources ", e);
986 public boolean validateTosca(Map<String, List<Component>> vertices, Map<String, List<Component>> compToFix,
988 boolean result = true;
989 long time = System.currentTimeMillis();
990 String fileName = name + "_" + time + ".csv";
991 try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"))) {
992 writer.write("name, UUID, invariantUUID, state, version\n");
993 for (Map.Entry<String, List<Component>> entry : vertices.entrySet()) {
994 List<Component> compList = entry.getValue();
995 Set<String> artifactEsId = new HashSet<>();
996 for (Component component : compList) {
997 Map<String, ArtifactDefinition> toscaArtifacts = component.getToscaArtifacts();
998 Optional<ArtifactDefinition> op = toscaArtifacts.values().stream()
999 .filter(a -> artifactEsId.contains(a.getEsId()) && a.getEsId() != null).findAny();
1000 if (op.isPresent()) {
1002 writeModuleResultToFile(writer, compList);
1006 artifactEsId.addAll(toscaArtifacts.values().stream().map(ArtifactDefinition::getEsId)
1007 .collect(Collectors.toList()));
1011 List<Component> compListfull = new ArrayList<>();
1012 for (Component c : compList) {
1013 ComponentParametersView filter = new ComponentParametersView(true);
1014 filter.setIgnoreComponentInstances(false);
1015 filter.setIgnoreArtifacts(false);
1016 filter.setIgnoreGroups(false);
1018 Either<Component, StorageOperationStatus> toscaElement = toscaOperationFacade
1019 .getToscaElement(c.getUniqueId(), filter);
1020 if (toscaElement.isRight()) {
1021 log.info("getVerticesToValidate: failed to find element" + c.getUniqueId()
1022 + " staus is" + toscaElement.right().value());
1024 compListfull.add(toscaElement.left().value());
1026 this.titanDao.commit();
1028 compToFix.put(entry.getKey(), compListfull);
1032 } catch (Exception e) {
1033 log.info("Failed to fetch vf resources ", e);
1041 private Either<ArtifactDefinition, ToscaError> generateToscaArtifact(Component parent,
1042 ArtifactDefinition artifactInfo) {
1043 log.debug("tosca artifact generation");
1045 if (artifactInfo.getArtifactType().equals(ArtifactTypeEnum.TOSCA_CSAR.getType())) {
1046 Either<byte[], ResponseFormat> generated = csarUtils.createCsar(parent, true, true);
1048 if (generated.isRight()) {
1049 log.debug("Failed to export tosca csar for component {} error {}", parent.getUniqueId(),
1050 generated.right().value());
1052 return Either.right(ToscaError.GENERAL_ERROR);
1054 byte[] value = generated.left().value();
1055 artifactInfo.setPayload(value);
1057 Either<ToscaRepresentation, ToscaError> exportComponent = toscaExportUtils.exportComponent(parent);
1058 if (exportComponent.isRight()) {
1059 log.debug("Failed export tosca yaml for component {} error {}", parent.getUniqueId(),
1060 exportComponent.right().value());
1062 return Either.right(exportComponent.right().value());
1064 log.debug("Tosca yaml exported for component {} ", parent.getUniqueId());
1065 String payload = exportComponent.left().value().getMainYaml();
1067 artifactInfo.setPayloadData(payload);
1070 byte[] decodedPayload = artifactInfo.getPayloadData();
1071 artifactInfo.setEsId(artifactInfo.getUniqueId());
1072 artifactInfo.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(decodedPayload));
1073 ESArtifactData artifactData = new ESArtifactData(artifactInfo.getEsId(), decodedPayload);
1074 artifactCassandraDao.saveArtifact(artifactData);
1075 log.debug("Tosca yaml artifact esId ", artifactInfo.getEsId());
1076 } catch (Exception ex) {
1077 log.error("Failed to generate tosca atifact id {} component id {} component name {} error {}",
1078 artifactInfo.getUniqueId(), parent.getUniqueId(), parent.getName(), ex.getMessage(), ex);
1079 return Either.right(ToscaError.GENERAL_ERROR);
1081 return Either.left(artifactInfo);
1084 private void fillVfModuleInstHeatEnvPayload(List<GroupInstance> groupsForCurrVF,
1085 ArtifactDefinition vfModuleArtifact) {
1087 List<VfModuleArtifactPayload> vfModulePayloadForCurrVF = new ArrayList<>();
1088 if (groupsForCurrVF != null) {
1089 for (GroupInstance groupInstance : groupsForCurrVF) {
1090 VfModuleArtifactPayload modulePayload = new VfModuleArtifactPayload(groupInstance);
1091 vfModulePayloadForCurrVF.add(modulePayload);
1093 vfModulePayloadForCurrVF.sort(VfModuleArtifactPayload::compareByGroupName);
1095 final Gson gson = new GsonBuilder().setPrettyPrinting().create();
1097 String vfModulePayloadString = gson.toJson(vfModulePayloadForCurrVF);
1098 if (vfModulePayloadString != null) {
1099 String newCheckSum = GeneralUtility
1100 .calculateMD5Base64EncodedByByteArray(vfModulePayloadString.getBytes());
1101 vfModuleArtifact.setArtifactChecksum(newCheckSum);
1103 ESArtifactData artifactData = new ESArtifactData(vfModuleArtifact.getEsId(),
1104 vfModulePayloadString.getBytes());
1105 artifactCassandraDao.saveArtifact(artifactData);