Sonar fixes in ArtifactUuidFix
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / impl / ArtifactUuidFix.java
1 package org.openecomp.sdc.asdctool.impl;
2
3 import com.google.gson.Gson;
4 import com.google.gson.GsonBuilder;
5 import fj.data.Either;
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;
15 import java.util.Map;
16 import java.util.Optional;
17 import java.util.Set;
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;
63
64 @org.springframework.stereotype.Component("artifactUuidFix")
65 public class ArtifactUuidFix {
66
67     @Autowired
68     private TitanDao titanDao;
69
70     @Autowired
71     private ToscaOperationFacade toscaOperationFacade;
72     @Autowired
73     private ToscaExportHandler toscaExportUtils;
74     @Autowired
75     private ArtifactCassandraDao artifactCassandraDao;
76
77     @Autowired
78     private CsarUtils csarUtils;
79
80     private static Logger log = LoggerFactory.getLogger(ArtifactUuidFix.class.getName());
81
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<>();
88
89         long time = System.currentTimeMillis();
90
91         doFixTosca(nodeToFixTosca, vfToFixTosca, serviceToFixTosca);
92
93         if (fixComponent.equals("vf_only")) {
94             if (!fetchFaultVf(vfLst, time)) {
95                 return false;
96             }
97         } else {
98             if (!fetchServices(fixComponent, serviceList, time)) {
99                 return false;
100             }
101         }
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);
106                 return false;
107             }
108             log.info("Mode {}. Find problem VFs finished with success", runMode);
109         }
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);
114                 return false;
115             }
116             log.info("Mode {}. Fix finished with success", runMode);
117         }
118         return true;
119     }
120
121     private boolean fetchFaultVf(List<Resource> vfLst, long time) {
122         log.info("Find fault VF ");
123         String fileName = "fault_" + time + ".csv";
124
125         try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"))) {
126             writer.write("vf name, vf id, state, version\n");
127
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());
131
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);
135
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());
140                 return false;
141             }
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);
148
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());
153                     return false;
154                 }
155
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());
162                     continue;
163                 }
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;
169                         break;
170                     }
171                 }
172                 if (isProblematic) {
173                     vfLst.add(resource);
174                     writeModuleResultToFile(writer, resource, null);
175                     writer.flush();
176                 }
177                 titanDao.commit();
178             }
179
180         } catch (Exception e) {
181             log.info("Failed to fetch vf resources ", e);
182             return false;
183         } finally {
184             titanDao.commit();
185         }
186         return true;
187     }
188
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");
193             return true;
194         }
195         String fileName = "problemVf_" + time + ".csv";
196
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());
211                             return false;
212                         }
213                         Resource resource = toscaElement.left().value();
214                         if (resource.getResourceType().equals(ResourceTypeEnum.VF)) {
215                             vfLst.add(resource);
216                             writeModuleResultToFile(writer, resource, service);
217                             writer.flush();
218                             titanDao.commit();
219                         }
220                     }
221                 }
222             }
223             log.info("output file with list of Vf : {}", fileName);
224         } catch (Exception e) {
225             log.info("Failed to fetch services ", e);
226             return false;
227         } finally {
228             titanDao.commit();
229         }
230         return true;
231     }
232
233     private boolean fetchServices(String fixServices, List<Service> serviceList, long time) {
234         log.info("Find problem Services {}", fixServices);
235         String fileName = "problemService_" + time + ".csv";
236
237         try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"))) {
238             writer.write("service name, service id, state, version\n");
239
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());
245             }
246
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);
250
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());
255                 return false;
256             }
257
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);
264
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());
269                     continue;
270                 }
271
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());
277                     continue;
278                 }
279                 String serviceName = (String) gv.getMetadataProperty(GraphPropertyEnum.NAME);
280
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,
286                             gv.getUniqueId());
287                         continue;
288                     }
289
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;
294                             break;
295                         }
296                     }
297                     if (isProblematic) {
298                         serviceList.add(service);
299                         writeModuleResultToFile(writer, service, null);
300                         writer.flush();
301                         break;
302                     }
303                 }
304                 titanDao.commit();
305             }
306             log.info("output file with list of services : {}", fileName);
307         } catch (Exception e) {
308             log.info("Failed to fetch services ", e);
309             return false;
310         } finally {
311             titanDao.commit();
312         }
313         return true;
314     }
315
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();
320
321         if (artifactsUuid == null || artifactsUuid.isEmpty() || artifacts == null || artifacts.isEmpty()) {
322             log.info("No groups in resource {} ", resourceName);
323             return true;
324         }
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);
328             return true;
329         }
330         if (artifacts.size() < artifactsUuid.size()) {
331             log.info(" artifacts.size() < artifactsUuid.size() group {} in resource {} ", gr.getName(), resourceName);
332             return true;
333         }
334         if (artifactsUuid.contains(null)) {
335             log.info(" artifactsUuid.contains(null) group {} in resource {} ", gr.getName(), resourceName);
336             return true;
337         }
338
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(),
344                     resourceName);
345                 return true;
346             }
347             ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactDefinition.getArtifactType());
348             if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
349                 if (!artifactId.equals(artifactDefinition.getUniqueId())) {
350                     log.info(
351                         " !artifactId.equals(artifactDefinition.getUniqueId() artifact {}  artId {} group {} in resource {} ",
352                         artifactlabel, artifactId, gr.getName(), resourceName);
353                     return true;
354                 }
355                 if (!artifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
356                     log.info(
357                         " artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} group {} in resource {} ",
358                         artifactlabel, gr.getName(), resourceName);
359                     return true;
360                 }
361             }
362         }
363
364         for (String artifactUuid : artifactsUuid) {
365             String label = findArtifactLabelFromArtifactId(artifactUuid);
366             if (label != null && !label.isEmpty()) {
367                 return true;
368             }
369         }
370         return false;
371     }
372
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<>();
380
381         if (artifactsUuid == null || artifactsUuid.isEmpty() || artifacts == null || artifacts.isEmpty()) {
382             log.info("No instance groups for instance {} in service {} ", instName, servicename);
383             return true;
384         }
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);
388             return true;
389         }
390         if (instArtifactsId != null && !instArtifactsId.isEmpty()) {
391             instArtifatIdSet.addAll(instArtifactsId);
392         }
393         if (artifacts.size() < artifactsUuid.size()) {
394             log.info(" artifacts.size() < artifactsUuid.size() inst {} in service {} ", instName, servicename);
395             return true;
396         }
397         if (artifactsUuid.contains(null)) {
398             log.info(" artifactsUuid.contains(null) inst {} in service {} ", instName, servicename);
399             return true;
400         }
401         if (instArtifactsId != null && instArtifatIdSet.size() < instArtifactsId.size()) {
402             log.info(" instArtifatIdSet.size() < instArtifactsId.size() inst {} in service {} ", instName, servicename);
403             return true;
404         }
405         if ((instArtifactsId != null && instArtifactsUuid != null)
406             && instArtifactsId.size() != instArtifactsUuid.size()) {
407             log.info(" instArtifactsId.size() != instArtifactsUuid.size() inst {} in service {} ", instName,
408                 servicename);
409             return true;
410         }
411
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,
417                     servicename);
418                 return true;
419             }
420             ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactDefinition.getArtifactType());
421             if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
422                 if (!artifactId.equals(artifactDefinition.getUniqueId())) {
423                     log.info(
424                         " !artifactId.equals(artifactDefinition.getUniqueId() artifact {}  artId {} inst {} in service {} ",
425                         artifactlabel, artifactId, instName, servicename);
426                     return true;
427                 }
428                 if (!artifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
429                     log.info(
430                         " artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ",
431                         artifactlabel, instName, servicename);
432                     return true;
433                 }
434             } else {
435                 if (instArtifactsUuid == null || instArtifactsUuid.isEmpty()) {
436                     log.info(" instArtifactsUuid empty. label {} inst {} in service {} ", artifactlabel, instName,
437                         servicename);
438                     return true;
439                 }
440                 if (!instArtifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
441                     log.info(
442                         " instArtifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ",
443                         artifactlabel, instName, servicename);
444                     return true;
445                 }
446             }
447         }
448         for (String artifactUuid : artifactsUuid) {
449             String label = findArtifactLabelFromArtifactId(artifactUuid);
450             if (label != null && !label.isEmpty()) {
451                 return true;
452             }
453         }
454         return false;
455     }
456
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) {
459         boolean res = true;
460         log.info(" Fix started ***** ");
461         if (vfLst != null && !vfLst.isEmpty()) {
462             res = fixVf(vfLst);
463         }
464         if (res && serviceList != null && !serviceList.isEmpty()) {
465             res = fixServices(serviceList);
466         }
467
468         Set<String> fixedIds = new HashSet<>();
469         if (res && nodesToFixTosca != null && !nodesToFixTosca.isEmpty()) {
470             generateAndSaveToscaArtifacts(nodesToFixTosca, fixedIds, null);
471
472             for (Map.Entry<String, List<Component>> entry : nodesToFixTosca.entrySet()) {
473                 List<Component> components = entry.getValue();
474                 for (Component c : components) {
475
476                     ToscaElement topologyTemplate = ModelConverter.convertToToscaElement(c);
477                     Map<String, ArtifactDataDefinition> arifacts = topologyTemplate.getToscaArtifacts();
478                     res = fixDataOnGraph(c.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS, EdgeLabelEnum.TOSCA_ARTIFACTS,
479                         arifacts);
480                     titanDao.commit();
481                 }
482             }
483         }
484         if (res && vfToFixTosca != null && !vfToFixTosca.isEmpty()) {
485             generateAndSaveToscaArtifacts(vfToFixTosca, fixedIds, vfLst);
486
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,
493                         arifacts);
494                     titanDao.commit();
495                 }
496             }
497         }
498
499         if (res && servicesToFixTosca != null && !servicesToFixTosca.isEmpty()) {
500             generateAndSaveToscaArtifacts(servicesToFixTosca, fixedIds, serviceList);
501
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,
508                         arifacts);
509                     titanDao.commit();
510                 }
511             }
512         }
513
514         if (res) {
515             for (Component component : vfLst) {
516                 generateToscaPerComponent(fixedIds, component);
517
518                 TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(component);
519                 Map<String, GroupDataDefinition> groups = topologyTemplate.getGroups();
520                 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.GROUPS, EdgeLabelEnum.GROUPS, groups);
521                 if (res) {
522                     Map<String, ArtifactDataDefinition> arifacts = topologyTemplate.getDeploymentArtifacts();
523                     res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.DEPLOYMENT_ARTIFACTS,
524                         EdgeLabelEnum.DEPLOYMENT_ARTIFACTS, arifacts);
525                 }
526                 if (res) {
527                     Map<String, ArtifactDataDefinition> arifacts = topologyTemplate.getToscaArtifacts();
528                     res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS,
529                         EdgeLabelEnum.TOSCA_ARTIFACTS, arifacts);
530                 }
531                 titanDao.commit();
532             }
533         }
534
535         if (res) {
536             for (Component component : serviceList) {
537                 generateToscaPerComponent(fixedIds, component);
538
539                 TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(component);
540                 Map<String, MapGroupsDataDefinition> groups = topologyTemplate.getInstGroups();
541                 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS,
542                     groups);
543
544                 if (res) {
545                     Map<String, MapArtifactDataDefinition> artifacts = topologyTemplate.getInstDeploymentArtifacts();
546                     res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS,
547                         EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, artifacts);
548                 }
549                 if (res) {
550                     Map<String, ArtifactDataDefinition> arifacts = topologyTemplate.getToscaArtifacts();
551                     res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS,
552                         EdgeLabelEnum.TOSCA_ARTIFACTS, arifacts);
553                 }
554                 titanDao.commit();
555             }
556         }
557         log.info(" Fix finished with res {} ***** ", res);
558         return res;
559     }
560
561     private boolean generateAndSaveToscaArtifacts(Map<String, List<Component>> nodesToFixTosca, Set<String> fixedIds,
562         List<? extends Component> componentsWithFailedGroups) {
563         boolean res = true;
564         log.debug("Migration1707ArtifactUuidFix  generateAndSaveToscaArtifacts started ");
565         for (Map.Entry<String, List<Component>> entry : nodesToFixTosca.entrySet()) {
566
567             List<Component> component = entry.getValue();
568             for (Component c : component) {
569                 log.debug("Migration1707ArtifactUuidFix  fix tosca on component : id {},  name {} ", c.getUniqueId(),
570                     c.getName());
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);
576                     }
577                 } else {
578                     res = generateToscaPerComponent(fixedIds, c);
579                 }
580             }
581         }
582         log.debug("Migration1707ArtifactUuidFix generateAndSaveToscaArtifacts finished with res {} ", res);
583         return res;
584     }
585
586     private boolean generateToscaPerComponent(Set<String> fixedIds, Component c) {
587         boolean res = true;
588         log.debug("Migration1707ArtifactUuidFix  generateToscaPerComponent started component name {} id {}",
589             c.getName(), c.getUniqueId());
590         try {
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());
595                 return false;
596             }
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()))
604                     .findAny()
605                     .ifPresent(compInst -> ciFull.setGroupInstances(compInst.getGroupInstances())));
606             }
607
608             Map<String, ArtifactDefinition> toscaArtifacts = c.getToscaArtifacts();
609             log.debug("Migration1707ArtifactUuidFix  generateToscaPerComponent tocsa artifacts size {}",
610                 toscaArtifacts.size());
611
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();
616
617             if (op.isPresent()) {
618                 toscaArtifact = op.get();
619             }
620
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());
628                     res = false;
629
630                 }
631             }
632             if (res) {
633                 ArtifactDefinition csarArtifact = null;
634                 op = toscaArtifacts.values().stream()
635                     .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.TOSCA_CSAR.getType())).findAny();
636
637                 if (op.isPresent()) {
638                     csarArtifact = op.get();
639                 }
640
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());
648                         res = false;
649
650                     }
651                 }
652             }
653             c.setToscaArtifacts(toscaArtifacts);
654
655             if (res) {
656                 fixedIds.add(toscaElementFull.getUniqueId());
657             }
658         } finally {
659             titanDao.commit();
660         }
661         log.debug("Migration1707ArtifactUuidFix  generateToscaPerComponent finished  component name {} id {} res {}",
662             c.getName(), c.getUniqueId(), res);
663         return res;
664     }
665
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());
670         boolean res = true;
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());
675             res = false;
676         }
677         if (res) {
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());
685                 return true;
686             }
687             if (groupVertexEither.isRight()) {
688                 res = false;
689                 log.debug("failed to get child {}  vertex for component  unique id {}, error: {}", edgeLabelEnum,
690                     componentId, groupVertexEither.right().value());
691             }
692             if (res) {
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());
699                     titanDao.rollback();
700                     return false;
701                 }
702             }
703         }
704         log.debug("Fix data on graph finished: VertexTypeEnum {} EdgeLabelEnum {} res {}", vertexTypeEnum.getName(),
705             res);
706         return res;
707     }
708
709     private boolean fixServices(List<Service> serviceList) {
710         for (Service service : serviceList) {
711             log.debug("Migration1707ArtifactUuidFix  fix service: id {},  name {} ", service.getUniqueId(),
712                 service.getName());
713             List<ComponentInstance> instances = service.getComponentInstances();
714             for (ComponentInstance instance : instances) {
715                 fixComponentInstances(service, instance);
716             }
717         }
718         return true;
719     }
720
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);
728             }
729
730             if (!groupsToDelete.isEmpty()) {
731                 log.debug("Migration1707ArtifactUuidFix  delete group:  resource id {}, group instance to delete {} ",
732                     service.getUniqueId(), groupsToDelete);
733                 groupsList.removeAll(groupsToDelete);
734             }
735
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);
741             }
742         }
743     }
744
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(),
749                 group.getName());
750             List<String> groupArtifacts = new ArrayList<>(group.getArtifacts());
751
752             group.getArtifacts().clear();
753             group.getArtifactsUuid().clear();
754             group.getGroupInstanceArtifacts().clear();
755             group.getGroupInstanceArtifactsUuid().clear();
756
757             for (String artifactId : groupArtifacts) {
758                 fixArtifactUndergroupInstances(artifactsMap, group, groupArtifacts, artifactId);
759             }
760             if (group.getArtifacts() == null || group.getArtifacts().isEmpty()) {
761                 log.debug(
762                     "Migration1707ArtifactUuidFix  fix groupInstance add to delete list:  resource id {} name {} , group name {} ",
763                     service.getUniqueId(), service.getName(), group.getName());
764                 groupsToDelete.add(group);
765             }
766         }
767     }
768
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());
784                 }
785                 if (isAddToGroup) {
786                     log.debug(
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);
792                     }
793                 }
794             } else {
795                 log.debug(
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);
801                 }
802             }
803         }
804     }
805
806     private boolean fixVf(List<Resource> vfLst) {
807         for (Resource resource : vfLst) {
808             log.debug("Migration1707ArtifactUuidFix  fix resource: id {},  name {} ", resource.getUniqueId(),
809                 resource.getName());
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);
817                     }
818                     if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)
819                         && (group.getArtifacts() == null || group.getArtifacts().isEmpty())) {
820                         log.debug(
821                             "Migration1707ArtifactUuidFix  add group to delete list fix resource: id {},  name {} ",
822                             resource.getUniqueId(), resource.getName(), group.getName());
823                         groupsToDelete.add(group);
824                     }
825                 }
826                 if (!groupsToDelete.isEmpty()) {
827                     groupsList.removeAll(groupsToDelete);
828                 }
829             }
830         }
831         return true;
832     }
833
834     private void fixVfGroup(Resource resource, Map<String, ArtifactDefinition> artifactsMap, GroupDefinition group) {
835         log.debug("Migration1707ArtifactUuidFix  fix group:  resource id {}, group name {} ", resource.getUniqueId(),
836             group.getName());
837         List<String> groupArtifacts = new ArrayList<>(group.getArtifacts());
838
839         for (String artifactId : groupArtifacts) {
840             fixArtifactUnderGroup(artifactsMap, group, groupArtifacts, artifactId);
841         }
842     }
843
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());
861                 }
862             }
863             if (isAddToGroup) {
864                 log.debug(
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);
870                 }
871             }
872         }
873     }
874
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);
880         }
881         return artifactLabel;
882     }
883
884     private void writeModuleResultToFile(Writer writer, org.openecomp.sdc.be.model.Component component,
885         Service service) {
886         try {
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());
893             }
894             sb.append("\n");
895             writer.write(sb.toString());
896         } catch (IOException e) {
897             log.error(e.getMessage(), e);
898         }
899     }
900
901     private void writeModuleResultToFile(Writer writer, List<Component> components) {
902         try {
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())
910                     .append("\n");
911                 writer.write(sb.toString());
912             }
913         } catch (IOException e) {
914             log.error(e.getMessage(), e);
915         }
916     }
917
918     public boolean doFixTosca(Map<String, List<Component>> nodeToFix, Map<String, List<Component>> vfToFix,
919         Map<String, List<Component>> serviceToFix) {
920
921         Map<GraphPropertyEnum, Object> hasProps = new HashMap<>();
922         hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
923         hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
924
925         Map<String, List<Component>> vertices = getVerticesToValidate(VertexTypeEnum.NODE_TYPE, hasProps);
926         boolean result = validateTosca(vertices, nodeToFix, "RESOURCE_TOSCA_ARTIFACTS");
927
928         hasProps.clear();
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());
932
933         vertices = getVerticesToValidate(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps);
934         result = validateTosca(vertices, vfToFix, "VF_TOSCA_ARTIFACTS");
935
936         hasProps.clear();
937         hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
938         hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
939
940         vertices = getVerticesToValidate(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps);
941         result = validateTosca(vertices, serviceToFix, "SERVICE_TOSCA_ARTIFACTS");
942
943         return result;
944     }
945
946     public Map<String, List<Component>> getVerticesToValidate(VertexTypeEnum type,
947         Map<GraphPropertyEnum, Object> hasProps) {
948
949         Map<String, List<Component>> result = new HashMap<>();
950         try {
951             Either<List<GraphVertex>, TitanOperationStatus> resultsEither = titanDao.getByCriteria(type, hasProps);
952             if (resultsEither.isRight()) {
953                 log.info("getVerticesToValidate failed " + resultsEither.right().value());
954                 return result;
955             }
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<>());
962                 }
963                 List<Component> compList = result.get(ivariantUuid);
964
965                 ComponentParametersView filter = new ComponentParametersView(true);
966                 filter.setIgnoreArtifacts(false);
967
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());
973                 } else {
974                     compList.add(toscaElement.left().value());
975                 }
976                 titanDao.commit();
977             });
978         } catch (Exception e) {
979             log.info("Failed to fetch vf resources ", e);
980         } finally {
981             titanDao.commit();
982         }
983         return result;
984     }
985
986     public boolean validateTosca(Map<String, List<Component>> vertices, Map<String, List<Component>> compToFix,
987         String name) {
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()) {
1001                         result = false;
1002                         writeModuleResultToFile(writer, compList);
1003                         writer.flush();
1004                         break;
1005                     } else {
1006                         artifactEsId.addAll(toscaArtifacts.values().stream().map(ArtifactDefinition::getEsId)
1007                             .collect(Collectors.toList()));
1008                     }
1009                 }
1010                 if (!result) {
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);
1017
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());
1023                         } else {
1024                             compListfull.add(toscaElement.left().value());
1025                         }
1026                         this.titanDao.commit();
1027                     }
1028                     compToFix.put(entry.getKey(), compListfull);
1029                     result = true;
1030                 }
1031             }
1032         } catch (Exception e) {
1033             log.info("Failed to fetch vf resources ", e);
1034             return false;
1035         } finally {
1036             titanDao.commit();
1037         }
1038         return result;
1039     }
1040
1041     private Either<ArtifactDefinition, ToscaError> generateToscaArtifact(Component parent,
1042         ArtifactDefinition artifactInfo) {
1043         log.debug("tosca artifact generation");
1044         try {
1045             if (artifactInfo.getArtifactType().equals(ArtifactTypeEnum.TOSCA_CSAR.getType())) {
1046                 Either<byte[], ResponseFormat> generated = csarUtils.createCsar(parent, true, true);
1047
1048                 if (generated.isRight()) {
1049                     log.debug("Failed to export tosca csar for component {} error {}", parent.getUniqueId(),
1050                         generated.right().value());
1051
1052                     return Either.right(ToscaError.GENERAL_ERROR);
1053                 }
1054                 byte[] value = generated.left().value();
1055                 artifactInfo.setPayload(value);
1056             } else {
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());
1061
1062                     return Either.right(exportComponent.right().value());
1063                 }
1064                 log.debug("Tosca yaml exported for component {} ", parent.getUniqueId());
1065                 String payload = exportComponent.left().value().getMainYaml();
1066
1067                 artifactInfo.setPayloadData(payload);
1068             }
1069
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);
1080         }
1081         return Either.left(artifactInfo);
1082     }
1083
1084     private void fillVfModuleInstHeatEnvPayload(List<GroupInstance> groupsForCurrVF,
1085         ArtifactDefinition vfModuleArtifact) {
1086
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);
1092             }
1093             vfModulePayloadForCurrVF.sort(VfModuleArtifactPayload::compareByGroupName);
1094
1095             final Gson gson = new GsonBuilder().setPrettyPrinting().create();
1096
1097             String vfModulePayloadString = gson.toJson(vfModulePayloadForCurrVF);
1098             if (vfModulePayloadString != null) {
1099                 String newCheckSum = GeneralUtility
1100                     .calculateMD5Base64EncodedByByteArray(vfModulePayloadString.getBytes());
1101                 vfModuleArtifact.setArtifactChecksum(newCheckSum);
1102
1103                 ESArtifactData artifactData = new ESArtifactData(vfModuleArtifact.getEsId(),
1104                     vfModulePayloadString.getBytes());
1105                 artifactCassandraDao.saveArtifact(artifactData);
1106             }
1107         }
1108     }
1109
1110 }