1 package org.openecomp.sdc.asdctool.impl;
3 import com.google.gson.Gson;
4 import com.google.gson.GsonBuilder;
5 import com.google.gson.JsonArray;
7 import org.openecomp.sdc.asdctool.impl.validator.utils.VfModuleArtifactPayloadEx;
8 import org.openecomp.sdc.be.components.distribution.engine.VfModuleArtifactPayload;
9 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
10 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
11 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
12 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
13 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
14 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
15 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
16 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
17 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
18 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
19 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
20 import org.openecomp.sdc.be.datatypes.elements.MapGroupsDataDefinition;
21 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
22 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
23 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
24 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
25 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
26 import org.openecomp.sdc.be.impl.ComponentsUtils;
27 import org.openecomp.sdc.be.model.*;
28 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
29 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
30 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
31 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
32 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
33 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
34 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
35 import org.openecomp.sdc.be.resources.data.ESArtifactData;
36 import org.openecomp.sdc.be.tosca.CsarUtils;
37 import org.openecomp.sdc.be.tosca.ToscaError;
38 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
39 import org.openecomp.sdc.be.tosca.ToscaRepresentation;
40 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
41 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
42 import org.openecomp.sdc.common.api.Constants;
43 import org.openecomp.sdc.common.log.wrappers.Logger;
44 import org.openecomp.sdc.common.util.GeneralUtility;
45 import org.openecomp.sdc.exception.ResponseFormat;
46 import org.springframework.beans.factory.annotation.Autowired;
48 import java.io.BufferedWriter;
49 import java.io.FileOutputStream;
50 import java.io.IOException;
51 import java.io.OutputStreamWriter;
52 import java.io.Writer;
53 import java.util.ArrayList;
54 import java.util.Collections;
55 import java.util.HashMap;
56 import java.util.HashSet;
57 import java.util.List;
59 import java.util.Optional;
61 import java.util.EnumMap;
62 import java.util.stream.Collectors;
64 @org.springframework.stereotype.Component("artifactUuidFix")
65 public class ArtifactUuidFix {
67 private static final String MIGRATION1707_ARTIFACT_UUID_FIX = "Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} ";
69 private static final String FAILED_TO_FETCH_VF_RESOURCES = "Failed to fetch vf resources ";
71 private static final String UTF8 = "utf-8";
74 private JanusGraphDao janusGraphDao;
77 private ToscaOperationFacade toscaOperationFacade;
79 private ToscaExportHandler toscaExportUtils;
81 private ArtifactCassandraDao artifactCassandraDao;
85 private CsarUtils csarUtils;
87 private static Logger log = Logger.getLogger(ArtifactUuidFix.class.getName());
89 public boolean doFix(String fixComponent, String runMode) {
90 List<Resource> vfLst = new ArrayList<>();
91 List<Service> serviceList = new ArrayList<>();
92 Map<String, List<Component>> nodeToFixTosca = new HashMap<>();
93 Map<String, List<Component>> vfToFixTosca = new HashMap<>();
94 Map<String, List<Component>> serviceToFixTosca = new HashMap<>();
96 long time = System.currentTimeMillis();
98 doFixTosca(nodeToFixTosca, vfToFixTosca, serviceToFixTosca);
100 if ("vf_only".equals(fixComponent)) {
101 if (!fetchFaultVf(fixComponent, vfLst, time)) {
105 if (!fetchServices(fixComponent, serviceList, time)) {
109 if ("service_vf".equals(runMode) || "fix".equals(runMode)) {
110 log.info("Mode {}. Find problem VFs", runMode);
111 if (!fetchVf(serviceList, vfLst, time)) {
112 log.info("Mode {}. Find problem VFs finished with failure", runMode);
115 log.info("Mode {}. Find problem VFs finished with success", runMode);
117 if ("fix".equals(runMode) || "fix_only_services".equals(runMode)) {
118 log.info("Mode {}. Start fix", runMode);
119 if (!fix(vfLst, serviceList, nodeToFixTosca, vfToFixTosca, serviceToFixTosca)) {
120 log.info("Mode {}. Fix finished with failure", runMode);
123 log.info("Mode {}. Fix finished with success", runMode);
129 private boolean fetchFaultVf(String fixComponent, List<Resource> vfLst, long time) {
130 log.info("Find fault VF ");
131 String fileName = "fault_" + time + ".csv";
132 try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF8))) {
133 writer.write("vf name, vf id, state, version\n");
135 Map<GraphPropertyEnum, Object> hasProps = new EnumMap<>(GraphPropertyEnum.class);
136 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
137 hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF.name());
139 Map<GraphPropertyEnum, Object> hasNotProps = new EnumMap<>(GraphPropertyEnum.class);
140 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
141 log.info("Try to fetch resources with properties {} and not {}", hasProps, hasNotProps);
143 Either<List<GraphVertex>, JanusGraphOperationStatus> servicesByCriteria = janusGraphDao
144 .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll);
145 if (servicesByCriteria.isRight()) {
146 log.info("Failed to fetch resources {}", servicesByCriteria.right().value());
149 List<GraphVertex> resources = servicesByCriteria.left().value();
150 for (GraphVertex gv : resources) {
151 ComponentParametersView filter = new ComponentParametersView(true);
152 filter.setIgnoreComponentInstances(false);
153 filter.setIgnoreArtifacts(false);
154 filter.setIgnoreGroups(false);
156 Either<Resource, StorageOperationStatus> toscaElement = toscaOperationFacade
157 .getToscaElement(gv.getUniqueId());
158 if (toscaElement.isRight()) {
159 log.info("Failed to fetch resources {} {}", gv.getUniqueId(), toscaElement.right().value());
163 Resource resource = toscaElement.left().value();
164 String resourceName = resource.getName();
165 Map<String, ArtifactDefinition> deploymentArtifacts = resource.getDeploymentArtifacts();
166 List<GroupDefinition> groups = resource.getGroups();
167 if (groups == null || groups.isEmpty()) {
168 log.info("No groups for resource {} id {} ", resourceName, gv.getUniqueId());
171 boolean isProblematic = false;
172 for (GroupDefinition gr : groups) {
173 if (gr.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
174 if (isProblematicGroup(gr, resourceName, deploymentArtifacts)) {
175 isProblematic = true;
182 writeModuleResultToFile(writer, resource, null);
185 janusGraphDao.commit();
188 } catch (Exception e) {
189 log.info(FAILED_TO_FETCH_VF_RESOURCES, e);
192 janusGraphDao.commit();
197 private boolean fetchVf(List<Service> serviceList, List<Resource> vfLst, long time) {
198 log.info("Find problem VF ");
199 if (serviceList.isEmpty()) {
200 log.info("No services as input");
203 String fileName = "problemVf_" + time + ".csv";
204 try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF8))) {
205 writer.write("vf name, vf id, state, version, example service name\n");
206 Set<String> vfIds = new HashSet<>();
207 for (Service service : serviceList) {
208 List<ComponentInstance> componentInstances = service.getComponentInstances().stream()
209 .filter(ci -> ci.getOriginType().equals(OriginTypeEnum.VF)).collect(Collectors.toList());
210 for (ComponentInstance ci : componentInstances) {
211 if (!vfIds.contains(ci.getComponentUid())) {
212 vfIds.add(ci.getComponentUid());
213 ComponentParametersView filter = new ComponentParametersView(true);
214 filter.setIgnoreComponentInstances(false);
215 filter.setIgnoreArtifacts(false);
216 filter.setIgnoreGroups(false);
217 Either<Resource, StorageOperationStatus> toscaElement = toscaOperationFacade
218 .getToscaElement(ci.getComponentUid(), filter);
219 if (toscaElement.isRight()) {
220 log.info("Failed to fetch resource {} {}", ci.getComponentUid(),
221 toscaElement.right().value());
224 Resource resource = toscaElement.left().value();
225 if (resource.getResourceType().equals(ResourceTypeEnum.VF)) {
227 writeModuleResultToFile(writer, resource, service);
231 janusGraphDao.commit();
235 log.info("output file with list of Vf : {}", fileName);
236 } catch (Exception e) {
237 log.info("Failed to fetch services ", e);
240 janusGraphDao.commit();
245 private boolean fetchServices(String fixServices, List<Service> serviceList, long time) {
246 log.info("Find problem Services {}", fixServices);
247 String fileName = "problemService_" + time + ".csv";
248 try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF8))) {
249 writer.write("service name, service id, state, version\n");
251 Map<GraphPropertyEnum, Object> hasProps = new EnumMap<>(GraphPropertyEnum.class);
252 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
253 if ("distributed_only".equals(fixServices)) {
254 hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
255 hasProps.put(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTED.name());
258 Map<GraphPropertyEnum, Object> hasNotProps = new EnumMap<>(GraphPropertyEnum.class);
259 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
260 log.info("Try to fetch services with properties {} and not {}", hasProps, hasNotProps);
262 Either<List<GraphVertex>, JanusGraphOperationStatus> servicesByCriteria = janusGraphDao
263 .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll);
264 if (servicesByCriteria.isRight()) {
265 log.info("Failed to fetch services {}", servicesByCriteria.right().value());
268 List<GraphVertex> services = servicesByCriteria.left().value();
269 for (GraphVertex gv : services) {
270 ComponentParametersView filter = new ComponentParametersView(true);
271 filter.setIgnoreComponentInstances(false);
272 filter.setIgnoreArtifacts(false);
273 filter.setIgnoreGroups(false);
275 Either<Service, StorageOperationStatus> toscaElement = toscaOperationFacade
276 .getToscaElement(gv.getUniqueId(), filter);
277 if (toscaElement.isRight()) {
278 log.info("Failed to fetch service {} {}", gv.getUniqueId(), toscaElement.right().value());
281 Service service = toscaElement.left().value();
283 String serviceName = (String) gv.getMetadataProperty(GraphPropertyEnum.NAME);
285 boolean isProblematic = isProblematicService(service, serviceName);
287 serviceList.add(service);
288 writeModuleResultToFile(writer, service, null);
293 janusGraphDao.commit();
295 log.info("output file with list of services : {}", fileName);
296 } catch (Exception e) {
297 log.info("Failed to fetch services ", e);
300 janusGraphDao.commit();
305 private boolean isProblematicService( Service service, String serviceName) {
307 List<ComponentInstance> componentInstances = service.getComponentInstances();
309 if (componentInstances == null) {
310 log.info("No instances for service {} ", service.getUniqueId());
313 boolean isCheckVFModules = true;
314 if(service.getLifecycleState() == LifecycleStateEnum.NOT_CERTIFIED_CHECKIN ||
315 service.getLifecycleState() == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT){
316 isCheckVFModules = false;
318 for (ComponentInstance ci : componentInstances) {
319 Map<String, ArtifactDefinition> deploymentArtifacts = ci.getDeploymentArtifacts();
320 List<GroupInstance> groupInstances = ci.getGroupInstances();
321 if (groupInstances == null || groupInstances.isEmpty()) {
322 log.info("No instance groups for instance {} in service {} id {} ", ci.getName(), serviceName,
323 service.getUniqueId());
326 List<VfModuleArtifactPayloadEx> vfModules = null;
327 if(isCheckVFModules){
328 Optional<ArtifactDefinition> optionalVfModuleArtifact = deploymentArtifacts.values().stream()
329 .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.VF_MODULES_METADATA.name())).findAny();
331 if(!optionalVfModuleArtifact.isPresent())
334 ArtifactDefinition vfModuleArtifact = optionalVfModuleArtifact.get();
335 Either<List<VfModuleArtifactPayloadEx>, StorageOperationStatus> vfModulesEither = parseVFModuleJson(vfModuleArtifact);
336 if(vfModulesEither.isRight()){
337 log.error("Failed to parse vfModule for service {} status is {}", service.getUniqueId(), vfModulesEither.right().value());
340 vfModules = vfModulesEither.left().value();
341 if(vfModules == null || vfModules.isEmpty() ){
342 log.info("vfModules empty for service {}", service.getUniqueId());
347 for (GroupInstance gi : groupInstances) {
348 if (gi.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
349 VfModuleArtifactPayloadEx vfModule= null;
350 if(isCheckVFModules && vfModules != null){
351 Optional<VfModuleArtifactPayloadEx> op = vfModules.stream().filter(vf -> vf.getVfModuleModelName().equals(gi.getGroupName())).findAny();
353 log.error("Failed to find vfModule for group {}", gi.getGroupName());
358 if (isProblematicGroupInstance(gi, ci.getName(), serviceName, deploymentArtifacts, vfModule)) {
368 private boolean isProblematicGroup(GroupDefinition gr, String resourceName,
369 Map<String, ArtifactDefinition> deploymentArtifacts) {
370 List<String> artifacts = gr.getArtifacts();
371 List<String> artifactsUuid = gr.getArtifactsUuid();
372 Set<String> artifactsSet = new HashSet<>();
374 if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) {
375 log.info("No groups in resource {} ", resourceName);
378 artifactsSet.addAll(artifacts);
379 if (artifactsSet.size() < artifacts.size()) {
380 log.info(" artifactsSet.size() < artifacts.size() group {} in resource {} ", gr.getName(), resourceName);
384 if ((artifactsUuid != null) && (artifacts.size() < artifactsUuid.size())) {
385 log.info(" artifacts.size() < artifactsUuid.size() group {} in resource {} ", gr.getName(), resourceName);
388 if (!artifacts.isEmpty() && (artifactsUuid == null || artifactsUuid.isEmpty())) {
390 " artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() group {} in resource {} ",
391 gr.getName(), resourceName);
394 if (artifactsUuid != null && artifactsUuid.contains(null)) {
395 log.info(" artifactsUuid.contains(null) group {} in resource {} ", gr.getName(), resourceName);
399 for (String artifactId : artifacts) {
400 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
401 ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactlabel);
402 if (artifactDefinition == null) {
403 log.info(" artifactDefinition == null label {} group {} in resource {} ", artifactlabel, gr.getName(),
407 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactDefinition.getArtifactType());
408 if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
409 if (!artifactId.equals(artifactDefinition.getUniqueId())) {
411 " !artifactId.equals(artifactDefinition.getUniqueId() artifact {} artId {} group {} in resource {} ",
412 artifactlabel, artifactId, gr.getName(), resourceName);
415 if (artifactsUuid != null && !artifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
417 " artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} group {} in resource {} ",
418 artifactlabel, gr.getName(), resourceName);
423 for (String artifactUUID : artifactsUuid) {
424 String label = findArtifactLabelFromArtifactId(artifactUUID);
425 if (label != null && !label.isEmpty() && !label.equals("")) {
433 private boolean isProblematicGroupInstance(GroupInstance gi, String instName, String servicename,
434 Map<String, ArtifactDefinition> deploymentArtifacts, VfModuleArtifactPayloadEx vfModule) {
435 List<String> artifacts = gi.getArtifacts();
436 List<String> artifactsUuid = gi.getArtifactsUuid();
437 List<String> instArtifactsUuid = gi.getGroupInstanceArtifactsUuid();
438 List<String> instArtifactsId = gi.getGroupInstanceArtifacts();
439 Set<String> instArtifatIdSet = new HashSet<>();
440 Set<String> artifactsSet = new HashSet<>();
442 log.info("check group {} for instance {} ", gi.getGroupName(), instName);
443 if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) {
444 log.info("No instance groups for instance {} in service {} ", instName, servicename);
447 artifactsSet.addAll(artifacts);
448 if (artifactsSet.size() < artifacts.size()) {
449 log.info(" artifactsSet.size() < artifacts.size() group {} in resource {} ", instName, servicename);
453 if (instArtifactsId != null && !instArtifactsId.isEmpty()) {
454 instArtifatIdSet.addAll(instArtifactsId);
457 if ((artifactsUuid != null) && (artifacts.size() < artifactsUuid.size())) {
458 log.info(" artifacts.size() < artifactsUuid.size() inst {} in service {} ", instName, servicename);
461 if (!artifacts.isEmpty() && (artifactsUuid == null || artifactsUuid.isEmpty())) {
463 " artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() inst {} in service {} ",
464 instName, servicename);
467 if (artifactsUuid != null && artifactsUuid.contains(null)) {
468 log.info(" artifactsUuid.contains(null) inst {} in service {} ", instName, servicename);
471 if (instArtifactsId != null && instArtifatIdSet.size() < instArtifactsId.size()) {
472 log.info(" instArtifatIdSet.size() < instArtifactsId.size() inst {} in service {} ", instName, servicename);
476 if ((instArtifactsId != null && instArtifactsUuid != null)
477 && instArtifactsId.size() != instArtifactsUuid.size()) {
478 log.info(" instArtifactsId.size() != instArtifactsUuid.size() inst {} in service {} ", instName,
483 for (String artifactId : artifacts) {
484 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
485 ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactlabel);
486 if (artifactDefinition == null) {
487 log.info(" artifactDefinition == null label {} inst {} in service {} ", artifactlabel, instName,
491 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactDefinition.getArtifactType());
492 if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
493 if (!artifactId.equals(artifactDefinition.getUniqueId())) {
495 " !artifactId.equals(artifactDefinition.getUniqueId() artifact {} artId {} inst {} in service {} ",
496 artifactlabel, artifactId, instName, servicename);
499 if (artifactsUuid != null && !artifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
501 " artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ",
502 artifactlabel, instName, servicename);
506 if (instArtifactsUuid == null || instArtifactsUuid.isEmpty()) {
507 log.info(" instArtifactsUuid empty. label {} inst {} in service {} ", artifactlabel, instName,
511 if (!instArtifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
513 " instArtifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ",
514 artifactlabel, instName, servicename);
519 for (String artifactUUID : artifactsUuid) {
520 String label = findArtifactLabelFromArtifactId(artifactUUID);
521 if (label != null && !label.isEmpty() && !"".equals(label)) {
525 if(vfModule != null && artifactsUuid != null){
526 return isProblematicVFModule(vfModule, artifactsUuid, instArtifactsUuid);
532 private boolean isProblematicVFModule(VfModuleArtifactPayloadEx vfModule, List<String> artifactsUuid,
533 List<String> instArtifactsUuid) {
534 log.info(" isProblematicVFModule {} ", vfModule.getVfModuleModelName());
535 List<String> vfModuleArtifacts = vfModule.getArtifacts();
536 List<String> allArtifacts = new ArrayList<>();
537 allArtifacts.addAll(artifactsUuid);
538 if(instArtifactsUuid != null)
539 allArtifacts.addAll(instArtifactsUuid);
540 if((vfModuleArtifacts == null || vfModuleArtifacts.isEmpty()) && !artifactsUuid.isEmpty()){
541 log.error(" vfModuleArtifacts == null || vfModuleArtifacts.isEmpty()) && !artifactsUuid.isEmpty()");
544 if(vfModuleArtifacts!= null){
545 if( vfModuleArtifacts.size() != allArtifacts.size()){
546 log.error(" vfModuleArtifacts.size() != allArtifacts.size()");
549 for(String vfModuleArtifact: vfModuleArtifacts){
550 Optional<String> op = allArtifacts.stream().filter(a -> a.equals(vfModuleArtifact)).findAny();
552 log.error("failed to find artifact {} in group artifacts {}", vfModuleArtifact, allArtifacts);
562 private boolean fix(List<Resource> vfLst, List<Service> serviceList, Map<String, List<Component>> nodesToFixTosca,
563 Map<String, List<Component>> vfToFixTosca, Map<String, List<Component>> servicesToFixTosca) {
565 log.info(" Fix started ***** ");
566 if (vfLst != null && !vfLst.isEmpty()) {
571 if (res && serviceList != null && !serviceList.isEmpty()) {
572 res = fixServices(serviceList);
576 Set<String> fixedIds = new HashSet<>();
578 long time = System.currentTimeMillis();
579 String fileName = "FailedGenerateTosca" + "_" + time + ".csv";
580 try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF8))) {
581 writer.write("componentType, name, version, UID, UUID, invariantUUID, state\n");
582 List<Component> failedList = new ArrayList<>();
584 if (res && nodesToFixTosca != null && !nodesToFixTosca.isEmpty()) {
586 generateAndSaveToscaArtifacts(nodesToFixTosca, fixedIds, null, failedList);
589 if (vfToFixTosca != null && !vfToFixTosca.isEmpty()) {
591 generateAndSaveToscaArtifacts(vfToFixTosca, fixedIds, vfLst, failedList);
595 for (Component component : vfLst) {
596 res = generateToscaPerComponent(fixedIds, component);
598 TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(component);
599 Map<String, GroupDataDefinition> groups = topologyTemplate.getGroups();
600 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.GROUPS, EdgeLabelEnum.GROUPS, groups);
602 Map<String, ArtifactDataDefinition> arifacts = topologyTemplate.getDeploymentArtifacts();
603 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.DEPLOYMENT_ARTIFACTS,
604 EdgeLabelEnum.DEPLOYMENT_ARTIFACTS, arifacts);
607 Map<String, ArtifactDataDefinition> arifacts = topologyTemplate.getToscaArtifacts();
608 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS,
609 EdgeLabelEnum.TOSCA_ARTIFACTS, arifacts);
611 janusGraphDao.commit();
613 failedList.add(component);
617 if (servicesToFixTosca != null && !servicesToFixTosca.isEmpty()) {
618 generateAndSaveToscaArtifacts(servicesToFixTosca, fixedIds, serviceList, failedList);
623 for (Component component : serviceList) {
624 res = generateToscaPerComponent(fixedIds, component);
626 TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(component);
627 Map<String, MapGroupsDataDefinition> groups = topologyTemplate.getInstGroups();
628 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS,
632 Map<String, MapArtifactDataDefinition> artifacts = topologyTemplate
633 .getInstDeploymentArtifacts();
634 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS,
635 EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, artifacts);
638 Map<String, ArtifactDataDefinition> arifacts = topologyTemplate.getToscaArtifacts();
639 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS,
640 EdgeLabelEnum.TOSCA_ARTIFACTS, arifacts);
642 janusGraphDao.commit();
644 failedList.add(component);
648 if (!failedList.isEmpty()) {
649 for (Component component : failedList) {
650 StringBuilder sb = new StringBuilder(component.getComponentType().getValue());
651 sb.append(",").append(component.getName()).append(",").append(component.getVersion()).append(",")
652 .append(component.getUniqueId()).append(",").append(component.getUUID()).append(",")
653 .append(component.getInvariantUUID()).append(",").append(component.getLifecycleState());
656 writer.write(sb.toString());
660 } catch (IOException e) {
661 log.error(e.getMessage());
663 janusGraphDao.commit();
665 log.info(" Fix finished with res {} ***** ", res);
669 private boolean generateAndSaveToscaArtifacts(Map<String, List<Component>> nodesToFixTosca, Set<String> fixedIds,
670 List<? extends Component> componentsWithFailedGroups, List<Component> failedList) {
672 log.debug("Migration1707ArtifactUuidFix generateAndSaveToscaArtifacts started ");
673 for (Map.Entry<String, List<Component>> entry : nodesToFixTosca.entrySet()) {
675 List<Component> component = entry.getValue();
676 for (Component c : component) {
677 log.debug("Migration1707ArtifactUuidFix fix tosca on component : id {}, name {} ", c.getUniqueId(),
679 if (componentsWithFailedGroups != null) {
680 Optional<Component> op = (Optional<Component>) componentsWithFailedGroups.stream()
681 .filter(cg -> cg.getUniqueId().equals(c.getUniqueId())).findAny();
683 res = generateToscaPerComponent(fixedIds, c);
685 res = generateToscaPerComponent(fixedIds, c);
687 ToscaElement topologyTemplate = ModelConverter.convertToToscaElement(c);
688 Map<String, ArtifactDataDefinition> arifacts = topologyTemplate.getToscaArtifacts();
689 res = fixDataOnGraph(c.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS, EdgeLabelEnum.TOSCA_ARTIFACTS,
691 janusGraphDao.commit();
698 log.debug("Migration1707ArtifactUuidFix generateAndSaveToscaArtifacts finished with res {} ", res);
702 private boolean generateToscaPerComponent(Set<String> fixedIds, Component c) {
704 log.debug("Migration1707ArtifactUuidFix generateToscaPerComponent started component name {} id {}",
705 c.getName(), c.getUniqueId());
707 Either<Component, StorageOperationStatus> toscaElement = toscaOperationFacade
708 .getToscaFullElement(c.getUniqueId());
709 if (toscaElement.isRight()) {
710 log.info("Failed to fetch resources {} {}", c.getUniqueId(), toscaElement.right().value());
713 Component toscaElementFull = toscaElement.left().value();
714 toscaElementFull.setGroups(c.getGroups());
715 List<ComponentInstance> ciListFull = toscaElementFull.getComponentInstances();
716 List<ComponentInstance> ciList = c.getComponentInstances();
717 if (ciListFull != null && !ciListFull.isEmpty()) {
718 ciListFull.forEach(ciFull -> {
719 ComponentInstance compInst = ciList.stream()
720 .filter(ci -> ci.getUniqueId().equals(ciFull.getUniqueId())).findAny().get();
721 ciFull.setGroupInstances(compInst.getGroupInstances());
725 Either<Component, ToscaError> either = generateToscaArtifact(toscaElementFull);
727 if (either.isRight()) {
728 log.error("Couldn't generate and save tosca template component unique id {}, name {} error: {}",
729 toscaElementFull.getUniqueId(), toscaElementFull.getName(), either.right().value());
735 c.setToscaArtifacts(either.left().value().getToscaArtifacts());
736 fixedIds.add(toscaElementFull.getUniqueId());
740 janusGraphDao.commit();
742 janusGraphDao.rollback();
744 log.debug("Migration1707ArtifactUuidFix generateToscaPerComponent finished component name {} id {} res {}",
745 c.getName(), c.getUniqueId(), res);
749 private <T extends ToscaDataDefinition> boolean fixDataOnGraph(String componentId, VertexTypeEnum vertexTypeEnum,
750 EdgeLabelEnum edgeLabelEnum, Map<String, T> groups) {
751 log.debug("amount groups to update: VertexTypeEnum {} EdgeLabelEnum {} data size {}", vertexTypeEnum.getName(),
752 edgeLabelEnum, groups.size());
754 Either<GraphVertex, JanusGraphOperationStatus> getResponse = janusGraphDao.getVertexById(componentId,
755 JsonParseFlagEnum.NoParse);
756 if (getResponse.isRight()) {
757 log.debug("Couldn't fetch component unique id {}, error: {}", componentId, getResponse.right().value());
762 GraphVertex componentVertex = getResponse.left().value();
764 GraphVertex toscaDataVertex = null;
765 Either<GraphVertex, JanusGraphOperationStatus> groupVertexEither = janusGraphDao.getChildVertex(componentVertex,
766 edgeLabelEnum, JsonParseFlagEnum.ParseJson);
767 if (groupVertexEither.isRight() && groupVertexEither.right().value() == JanusGraphOperationStatus.NOT_FOUND) {
768 log.debug("no child {} vertex for component unique id {}, error: {}", edgeLabelEnum, componentId,
769 groupVertexEither.right().value());
772 if (groupVertexEither.isRight()) {
774 log.debug("failed to get child {} vertex for component unique id {}, error: {}", edgeLabelEnum,
775 componentId, groupVertexEither.right().value());
778 toscaDataVertex = groupVertexEither.left().value();
779 toscaDataVertex.setJson(groups);
780 Either<GraphVertex, JanusGraphOperationStatus> updatevertexEither = janusGraphDao.updateVertex(toscaDataVertex);
781 if (updatevertexEither.isRight()) {
782 log.debug("failed to update vertex for component unique id {}, error: {}", componentId,
783 updatevertexEither.right().value());
784 janusGraphDao.rollback();
789 log.debug("Fix data on graph finished: VertexTypeEnum {} EdgeLabelEnum {} res {}", vertexTypeEnum.getName(),
794 private boolean fixServices(List<Service> serviceList) {
795 for (Service service : serviceList) {
796 log.debug("Migration1707ArtifactUuidFix fix service: id {}, name {} ", service.getUniqueId(),
798 List<ComponentInstance> instances = service.getComponentInstances();
799 for (ComponentInstance instance : instances) {
800 fixComponentInstances(service, instance);
808 private void fixComponentInstances(Service service, ComponentInstance instance) {
809 Map<String, ArtifactDefinition> artifactsMap = instance.getDeploymentArtifacts();
810 List<GroupInstance> groupsList = instance.getGroupInstances();
811 if (groupsList != null && artifactsMap != null) {
812 List<GroupInstance> groupsToDelete = new ArrayList<>();
813 for (GroupInstance group : groupsList) {
814 fixGroupInstances(service, artifactsMap, groupsToDelete, group);
818 if (!groupsToDelete.isEmpty()) {
819 log.debug("Migration1707ArtifactUuidFix delete group: resource id {}, group instance to delete {} ",
820 service.getUniqueId(), groupsToDelete);
821 groupsList.removeAll(groupsToDelete);
825 Optional<ArtifactDefinition> optionalVfModuleArtifact = artifactsMap.values().stream()
826 .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.VF_MODULES_METADATA.name())).findAny();
827 ArtifactDefinition vfModuleArtifact;
828 if(!optionalVfModuleArtifact.isPresent()){
829 vfModuleArtifact = createVfModuleArtifact(instance, service);
830 artifactsMap.put(vfModuleArtifact.getArtifactLabel(), vfModuleArtifact);
833 vfModuleArtifact = optionalVfModuleArtifact.get();
835 fillVfModuleInstHeatEnvPayload(service, instance, groupsList, vfModuleArtifact);
839 private void fixGroupInstances(Service service, Map<String, ArtifactDefinition> artifactsMap,
840 List<GroupInstance> groupsToDelete, GroupInstance group) {
841 if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
842 log.debug("Migration1707ArtifactUuidFix fix group: resource id {}, group name {} ", service.getUniqueId(),
844 if(group.getArtifacts() != null){
845 Set<String> groupArtifactsSet = new HashSet<>(group.getArtifacts());
846 if(group.getGroupInstanceArtifacts() != null){
847 List<String> groupInsArtifacts = new ArrayList<>(group.getGroupInstanceArtifacts());
848 groupArtifactsSet.addAll(groupInsArtifacts);
850 List<String> groupArtifacts = new ArrayList<>(groupArtifactsSet);
852 clearGroupInstanceArtifacts(group);
854 for (String artifactId : groupArtifacts) {
855 fixArtifactUndergroupInstances(artifactsMap, group, groupArtifacts, artifactId);
858 if (group.getArtifacts() == null || group.getArtifacts().isEmpty()) {
860 "Migration1707ArtifactUuidFix fix groupInstance add to delete list: resource id {} name {} , group name {} ",
861 service.getUniqueId(), service.getName(), group.getName());
862 groupsToDelete.add(group);
867 private void clearGroupInstanceArtifacts(GroupInstance group) {
868 if(group.getArtifacts() != null)
869 group.getArtifacts().clear();
871 group.setArtifacts(new ArrayList<>());
872 if(group.getArtifactsUuid() != null)
873 group.getArtifactsUuid().clear();
875 group.setArtifactsUuid(new ArrayList<>());
877 if(group.getGroupInstanceArtifacts() != null)
878 group.getGroupInstanceArtifacts().clear();
880 group.setGroupInstanceArtifacts(new ArrayList<>());
882 if(group.getGroupInstanceArtifactsUuid() != null )
883 group.getGroupInstanceArtifactsUuid().clear();
885 group.setGroupInstanceArtifactsUuid(new ArrayList<>());
888 private void fixArtifactUndergroupInstances(Map<String, ArtifactDefinition> artifactsMap, GroupInstance group,
889 List<String> groupArtifacts, String artifactId) {
890 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
891 log.debug("Migration1707ArtifactUuidFix fix group: group name {} artifactId for fix {} artifactlabel {} ",
892 group.getName(), artifactId, artifactlabel);
893 if (!artifactlabel.isEmpty() && artifactsMap.containsKey(artifactlabel)) {
894 ArtifactDefinition artifact = artifactsMap.get(artifactlabel);
895 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifact.getArtifactType());
896 String correctArtifactId = artifact.getUniqueId();
897 String correctArtifactUUID = artifact.getArtifactUUID();
898 if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
899 boolean isAddToGroup = true;
900 if (groupArtifacts.size() == 1) {
901 if (artifactType == ArtifactTypeEnum.HEAT_ARTIFACT) {
902 isAddToGroup = false;
903 artifact.setArtifactType(ArtifactTypeEnum.OTHER.getType());
908 MIGRATION1707_ARTIFACT_UUID_FIX,
909 group.getName(), correctArtifactId, correctArtifactUUID);
910 group.getArtifacts().add(correctArtifactId);
911 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
912 group.getArtifactsUuid().add(correctArtifactUUID);
917 MIGRATION1707_ARTIFACT_UUID_FIX,
918 group.getName(), correctArtifactId, correctArtifactUUID);
919 Set<String> tmpSet = new HashSet<>(group.getGroupInstanceArtifacts());
920 tmpSet.add(correctArtifactId);
921 group.setGroupInstanceArtifacts(new ArrayList<>(tmpSet));
922 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
923 Set<String> tmpSetUUID = new HashSet<>(group.getGroupInstanceArtifactsUuid());
924 tmpSetUUID.add(correctArtifactUUID);
925 group.setGroupInstanceArtifactsUuid(new ArrayList<>(tmpSetUUID));
931 private boolean fixVf(List<Resource> vfLst) {
932 for (Resource resource : vfLst) {
933 log.debug("Migration1707ArtifactUuidFix fix resource: id {}, name {} ", resource.getUniqueId(),
935 Map<String, ArtifactDefinition> artifactsMap = resource.getDeploymentArtifacts();
936 List<GroupDefinition> groupsList = resource.getGroups();
937 List<GroupDefinition> groupsToDelete = new ArrayList<>();
938 if (groupsList != null && artifactsMap != null) {
939 for (GroupDefinition group : groupsList) {
940 if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE) && group.getArtifacts() != null) {
941 fixVfGroup(resource, artifactsMap, group);
943 if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)
944 && (group.getArtifacts() == null || group.getArtifacts().isEmpty())) {
946 "Migration1707ArtifactUuidFix add group to delete list fix resource: id {}, name {} ",
947 resource.getUniqueId(), resource.getName(), group.getName());
948 groupsToDelete.add(group);
952 if (!groupsToDelete.isEmpty()) {
953 groupsList.removeAll(groupsToDelete);
963 private void fixVfGroup(Resource resource, Map<String, ArtifactDefinition> artifactsMap, GroupDefinition group) {
964 log.debug("Migration1707ArtifactUuidFix fix group: resource id {}, group name {} ", resource.getUniqueId(),
966 Set<String> groupArtifactsSet = new HashSet<>(group.getArtifacts());
967 List<String> groupArtifacts = new ArrayList<>(groupArtifactsSet);
968 group.getArtifacts().clear();
969 group.getArtifactsUuid().clear();
971 for (String artifactId : groupArtifacts) {
972 fixArtifactUnderGroup(artifactsMap, group, groupArtifacts, artifactId);
976 private void fixArtifactUnderGroup(Map<String, ArtifactDefinition> artifactsMap, GroupDefinition group,
977 List<String> groupArtifacts, String artifactId) {
979 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
980 log.debug("Migration1707ArtifactUuidFix fix group: group name {} artifactId for fix {} artifactlabel {} ",
981 group.getName(), artifactId, artifactlabel);
982 if (!artifactlabel.isEmpty() && artifactsMap.containsKey(artifactlabel)) {
983 ArtifactDefinition artifact = artifactsMap.get(artifactlabel);
984 String correctArtifactId = artifact.getUniqueId();
985 String correctArtifactUUID = artifact.getArtifactUUID();
986 boolean isAddToGroup = true;
987 if (groupArtifacts.size() == 1) {
988 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifact.getArtifactType());
989 if (artifactType == ArtifactTypeEnum.HEAT_ARTIFACT) {
990 isAddToGroup = false;
991 artifact.setArtifactType(ArtifactTypeEnum.OTHER.getType());
996 MIGRATION1707_ARTIFACT_UUID_FIX,
997 group.getName(), correctArtifactId, correctArtifactUUID);
998 group.getArtifacts().add(correctArtifactId);
999 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
1000 group.getArtifactsUuid().add(correctArtifactUUID);
1007 private String findArtifactLabelFromArtifactId(String artifactId) {
1008 String artifactLabel = "";
1010 int index = artifactId.lastIndexOf('.');
1011 if (index > 0 && index + 1 < artifactId.length())
1012 artifactLabel = artifactId.substring(index + 1);
1013 return artifactLabel;
1016 private void writeModuleResultToFile(Writer writer, org.openecomp.sdc.be.model.Component component,
1019 // "service name, service id, state, version
1020 StringBuilder sb = new StringBuilder(component.getName());
1021 sb.append(",").append(component.getUniqueId()).append(",").append(component.getLifecycleState()).append(",")
1022 .append(component.getVersion());
1023 if (service != null) {
1024 sb.append(",").append(service.getName());
1027 writer.write(sb.toString());
1028 } catch (IOException e) {
1029 log.error(e.getMessage());
1033 private void writeModuleResultToFile(Writer writer, List<Component> components) {
1035 // "service name, service id, state, version
1036 for (Component component : components) {
1037 StringBuilder sb = new StringBuilder(component.getName());
1038 sb.append(",").append(component.getUniqueId()).append(",").append(component.getInvariantUUID())
1039 .append(",").append(component.getLifecycleState()).append(",").append(component.getVersion());
1042 writer.write(sb.toString());
1044 } catch (IOException e) {
1046 log.error(e.getMessage());
1050 public boolean doFixTosca(Map<String, List<Component>> nodeToFix, Map<String, List<Component>> vfToFix,
1051 Map<String, List<Component>> serviceToFix) {
1053 Map<GraphPropertyEnum, Object> hasProps = new EnumMap<>(GraphPropertyEnum.class);
1054 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
1055 hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
1057 Map<String, List<Component>> vertices = getVerticesToValidate(VertexTypeEnum.NODE_TYPE, hasProps);
1058 boolean result = validateTosca(vertices, nodeToFix, "RESOURCE_TOSCA_ARTIFACTS");//
1061 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
1062 hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF);
1063 hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
1065 vertices = getVerticesToValidate(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps);
1066 result = validateTosca(vertices, vfToFix, "VF_TOSCA_ARTIFACTS");
1069 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
1070 hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
1072 vertices = getVerticesToValidate(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps);
1073 result = validateTosca(vertices, serviceToFix, "SERVICE_TOSCA_ARTIFACTS");
1078 public Map<String, List<Component>> getVerticesToValidate(VertexTypeEnum type,
1079 Map<GraphPropertyEnum, Object> hasProps) {
1081 Map<String, List<Component>> result = new HashMap<>();
1084 Either<List<GraphVertex>, JanusGraphOperationStatus> resultsEither = janusGraphDao
1085 .getByCriteria(type, hasProps);
1086 if (resultsEither.isRight()) {
1087 log.error("getVerticesToValidate failed {} ",resultsEither.right().value());
1090 log.info("getVerticesToValidate: {} vertices to scan", resultsEither.left().value().size());
1091 List<GraphVertex> componentsList = resultsEither.left().value();
1092 componentsList.forEach(vertex -> {
1093 String ivariantUuid = (String) vertex.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID);
1094 if (!result.containsKey(ivariantUuid)) {
1095 List<Component> compList = new ArrayList<>();
1096 result.put(ivariantUuid, compList);
1098 List<Component> compList = result.get(ivariantUuid);
1100 ComponentParametersView filter = new ComponentParametersView(true);
1101 filter.setIgnoreArtifacts(false);
1103 Either<Component, StorageOperationStatus> toscaElement = toscaOperationFacade
1104 .getToscaElement(vertex.getUniqueId(), filter);
1105 if (toscaElement.isRight()) {
1106 log.error("getVerticesToValidate: failed to find element {} staus is {}", vertex.getUniqueId()
1107 ,toscaElement.right().value());
1109 compList.add(toscaElement.left().value());
1111 janusGraphDao.commit();
1115 } catch (Exception e) {
1116 log.info(FAILED_TO_FETCH_VF_RESOURCES, e);
1119 janusGraphDao.commit();
1125 public boolean validateTosca(Map<String, List<Component>> vertices, Map<String, List<Component>> compToFix,
1127 boolean result = true;
1128 long time = System.currentTimeMillis();
1129 String fileName = name + "_" + time + ".csv";
1130 try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF8))) {
1131 writer.write("name, UUID, invariantUUID, state, version\n");
1132 for (Map.Entry<String, List<Component>> entry : vertices.entrySet()) {
1133 List<Component> compList = entry.getValue();
1134 Set<String> artifactEsId = new HashSet<>();
1135 for (Component component : compList) {
1136 Map<String, ArtifactDefinition> toscaArtifacts = component.getToscaArtifacts();
1137 Optional<ArtifactDefinition> op = toscaArtifacts.values().stream()
1138 .filter(a -> artifactEsId.contains(a.getEsId()) && a.getEsId() != null).findAny();
1139 if (op.isPresent()) {
1141 writeModuleResultToFile(writer, compList);
1145 artifactEsId.addAll(toscaArtifacts.values().stream().map(ArtifactDefinition::getEsId)
1146 .collect(Collectors.toList()));
1150 List<Component> compListfull = new ArrayList<>();
1151 for (Component c : compList) {
1152 ComponentParametersView filter = new ComponentParametersView(true);
1153 filter.setIgnoreComponentInstances(false);
1154 filter.setIgnoreArtifacts(false);
1155 filter.setIgnoreGroups(false);
1157 Either<Component, StorageOperationStatus> toscaElement = toscaOperationFacade
1158 .getToscaElement(c.getUniqueId(), filter);
1159 if (toscaElement.isRight()) {
1160 log.debug("getVerticesToValidate: failed to find element" + c.getUniqueId()
1161 + " status is" + toscaElement.right().value());
1163 compListfull.add(toscaElement.left().value());
1165 this.janusGraphDao.commit();
1168 compToFix.put(entry.getKey(), compListfull);
1174 } catch (Exception e) {
1175 log.info(FAILED_TO_FETCH_VF_RESOURCES, e);
1178 janusGraphDao.commit();
1183 private Either<Component, ToscaError> generateToscaArtifact(Component parent) {
1184 log.debug("tosca artifact generation");
1186 Map<String, ArtifactDefinition> toscaArtifacts = parent.getToscaArtifacts();
1188 ArtifactDefinition toscaArtifact = null;
1189 Optional<ArtifactDefinition> op = toscaArtifacts.values().stream()
1190 .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.TOSCA_TEMPLATE.getType())).findAny();
1192 if (op.isPresent()) {
1193 toscaArtifact = op.get();
1195 if (toscaArtifact != null) {
1196 log.debug("Migration1707ArtifactUuidFix generateToscaPerComponent artifact name {} id {} esId {}",
1197 toscaArtifact.getArtifactName(), toscaArtifact.getUniqueId(), toscaArtifact.getEsId());
1199 Either<ToscaRepresentation, ToscaError> exportComponent = toscaExportUtils.exportComponent(parent);
1200 if (exportComponent.isRight()) {
1201 log.debug("Failed export tosca yaml for component {} error {}", parent.getUniqueId(),
1202 exportComponent.right().value());
1204 return Either.right(exportComponent.right().value());
1206 log.debug("Tosca yaml exported for component {} ", parent.getUniqueId());
1207 String payload = exportComponent.left().value().getMainYaml();
1209 toscaArtifact.setPayloadData(payload);
1210 byte[] decodedPayload = toscaArtifact.getPayloadData();
1212 String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(parent.getUniqueId(),
1213 toscaArtifact.getArtifactLabel());
1214 toscaArtifact.setUniqueId(uniqueId);
1215 toscaArtifact.setEsId(toscaArtifact.getUniqueId());
1217 toscaArtifact.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(decodedPayload));
1218 ESArtifactData artifactData = new ESArtifactData(toscaArtifact.getEsId(), decodedPayload);
1219 artifactCassandraDao.saveArtifact(artifactData);
1221 log.debug("Tosca yaml artifact esId {} ", toscaArtifact.getEsId());
1223 ArtifactDefinition csarArtifact = null;
1224 op = toscaArtifacts.values().stream()
1225 .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.TOSCA_CSAR.getType())).findAny();
1227 if (op.isPresent()) {
1228 csarArtifact = op.get();
1231 if (csarArtifact != null) {
1232 Either<byte[], ResponseFormat> generated = csarUtils.createCsar(parent, true, true);
1234 if (generated.isRight()) {
1235 log.debug("Failed to export tosca csar for component {} error {}", parent.getUniqueId(),
1236 generated.right().value());
1238 return Either.right(ToscaError.GENERAL_ERROR);
1240 byte[] value = generated.left().value();
1241 csarArtifact.setPayload(value);
1242 byte[] decodedPayload = csarArtifact.getPayloadData();
1244 String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(parent.getUniqueId(),
1245 csarArtifact.getArtifactLabel());
1246 csarArtifact.setUniqueId(uniqueId);
1247 csarArtifact.setEsId(csarArtifact.getUniqueId());
1249 csarArtifact.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(decodedPayload));
1250 ESArtifactData artifactData = new ESArtifactData(csarArtifact.getEsId(), decodedPayload);
1251 artifactCassandraDao.saveArtifact(artifactData);
1252 log.debug("Tosca csar artifact esId {} ", csarArtifact.getEsId());
1256 } catch (Exception ex) {
1257 log.error("Failed to generate tosca atifact component id {} component name {} error {}",
1258 parent.getUniqueId(), parent.getName(), ex.getMessage());
1260 return Either.right(ToscaError.GENERAL_ERROR);
1263 return Either.left(parent);
1266 private ArtifactDefinition createVfModuleArtifact(ComponentInstance currVF, Service service) {
1268 ArtifactDefinition vfModuleArtifactDefinition = new ArtifactDefinition();
1270 vfModuleArtifactDefinition.setDescription("Auto-generated VF Modules information artifact");
1271 vfModuleArtifactDefinition.setArtifactDisplayName("Vf Modules Metadata");
1272 vfModuleArtifactDefinition.setArtifactType(ArtifactTypeEnum.VF_MODULES_METADATA.getType());
1273 vfModuleArtifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
1274 vfModuleArtifactDefinition.setArtifactLabel("vfModulesMetadata");
1275 vfModuleArtifactDefinition.setTimeout(0);
1276 vfModuleArtifactDefinition.setArtifactName(currVF.getNormalizedName() + "_modules.json");
1278 return vfModuleArtifactDefinition;
1282 private void fillVfModuleInstHeatEnvPayload(Component parent, ComponentInstance instance, List<GroupInstance> groupsForCurrVF,
1283 ArtifactDefinition vfModuleArtifact) {
1284 log.debug("generate new vf module for component. name {}, id {}, Version {}", instance.getName(), instance.getUniqueId());
1286 String uniqueId = UniqueIdBuilder.buildInstanceArtifactUniqueId(parent.getUniqueId(), instance.getUniqueId(), vfModuleArtifact.getArtifactLabel());
1288 vfModuleArtifact.setUniqueId(uniqueId);
1289 vfModuleArtifact.setEsId(vfModuleArtifact.getUniqueId());
1291 List<VfModuleArtifactPayload> vfModulePayloadForCurrVF = new ArrayList<>();
1292 if (groupsForCurrVF != null) {
1293 for (GroupInstance groupInstance : groupsForCurrVF) {
1294 VfModuleArtifactPayload modulePayload = new VfModuleArtifactPayload(groupInstance);
1295 vfModulePayloadForCurrVF.add(modulePayload);
1297 Collections.sort(vfModulePayloadForCurrVF,
1298 (art1, art2) -> VfModuleArtifactPayload.compareByGroupName(art1, art2));
1300 final Gson gson = new GsonBuilder().setPrettyPrinting().create();
1302 String vfModulePayloadString = gson.toJson(vfModulePayloadForCurrVF);
1303 log.debug("vfModulePayloadString {}", vfModulePayloadString);
1304 if (vfModulePayloadString != null) {
1305 String newCheckSum = GeneralUtility
1306 .calculateMD5Base64EncodedByByteArray(vfModulePayloadString.getBytes());
1307 vfModuleArtifact.setArtifactChecksum(newCheckSum);
1309 ESArtifactData artifactData = new ESArtifactData(vfModuleArtifact.getEsId(),
1310 vfModulePayloadString.getBytes());
1311 artifactCassandraDao.saveArtifact(artifactData);
1319 private Either<List<VfModuleArtifactPayloadEx>, StorageOperationStatus> parseVFModuleJson(ArtifactDefinition vfModuleArtifact) {
1320 log.info("Try to get vfModule json from cassandra {}", vfModuleArtifact.getEsId());
1321 Either<ESArtifactData, CassandraOperationStatus> vfModuleData = artifactCassandraDao.getArtifact(vfModuleArtifact.getEsId());
1323 if (vfModuleData.isRight()) {
1324 CassandraOperationStatus resourceUploadStatus = vfModuleData.right().value();
1325 StorageOperationStatus storageResponse = DaoStatusConverter.convertCassandraStatusToStorageStatus(resourceUploadStatus);
1326 log.error("failed to fetch vfModule json {} from cassandra. Status is {}", vfModuleArtifact.getEsId(), storageResponse);
1327 return Either.right(storageResponse);
1331 ESArtifactData esArtifactData = vfModuleData.left().value();
1332 String gsonData = new String( esArtifactData.getDataAsArray());
1333 final Gson gson = new GsonBuilder().setPrettyPrinting().create();
1334 JsonArray jsonElement = new JsonArray();
1335 jsonElement = gson.fromJson(gsonData, jsonElement.getClass());
1336 List<VfModuleArtifactPayloadEx> vfModules = new ArrayList<>();
1337 jsonElement.forEach(je ->{
1338 VfModuleArtifactPayloadEx vfModule = ComponentsUtils.parseJsonToObject(je.toString(), VfModuleArtifactPayloadEx.class);
1339 vfModules.add(vfModule);
1342 log.debug ("parse vf module finish {}", gsonData);
1343 return Either.left(vfModules);