2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.asdctool.impl;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.JsonArray;
26 import fj.data.Either;
27 import org.openecomp.sdc.asdctool.impl.validator.utils.VfModuleArtifactPayloadEx;
28 import org.openecomp.sdc.be.components.distribution.engine.VfModuleArtifactPayload;
29 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
30 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
31 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
32 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
33 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
34 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
35 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
36 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
37 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
38 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
39 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
40 import org.openecomp.sdc.be.datatypes.elements.MapGroupsDataDefinition;
41 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
42 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
43 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
44 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
45 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
46 import org.openecomp.sdc.be.impl.ComponentsUtils;
47 import org.openecomp.sdc.be.model.*;
48 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
49 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
50 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
51 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
52 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
53 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
54 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
55 import org.openecomp.sdc.be.resources.data.ESArtifactData;
56 import org.openecomp.sdc.be.tosca.CsarUtils;
57 import org.openecomp.sdc.be.tosca.ToscaError;
58 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
59 import org.openecomp.sdc.be.tosca.ToscaRepresentation;
60 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
61 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
62 import org.openecomp.sdc.common.api.Constants;
63 import org.openecomp.sdc.common.log.wrappers.Logger;
64 import org.openecomp.sdc.common.util.GeneralUtility;
65 import org.openecomp.sdc.exception.ResponseFormat;
66 import org.springframework.beans.factory.annotation.Autowired;
68 import java.io.BufferedWriter;
69 import java.io.FileOutputStream;
70 import java.io.IOException;
71 import java.io.OutputStreamWriter;
72 import java.io.Writer;
73 import java.util.ArrayList;
74 import java.util.Collections;
75 import java.util.HashMap;
76 import java.util.HashSet;
77 import java.util.List;
79 import java.util.Optional;
81 import java.util.EnumMap;
82 import java.util.stream.Collectors;
84 @org.springframework.stereotype.Component("artifactUuidFix")
85 public class ArtifactUuidFix {
87 private static final String MIGRATION1707_ARTIFACT_UUID_FIX = "Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} ";
89 private static final String FAILED_TO_FETCH_VF_RESOURCES = "Failed to fetch vf resources ";
91 private static final String UTF8 = "utf-8";
94 private JanusGraphDao janusGraphDao;
97 private ToscaOperationFacade toscaOperationFacade;
99 private ToscaExportHandler toscaExportUtils;
101 private ArtifactCassandraDao artifactCassandraDao;
105 private CsarUtils csarUtils;
107 private static Logger log = Logger.getLogger(ArtifactUuidFix.class.getName());
109 public boolean doFix(String fixComponent, String runMode) {
110 List<Resource> vfLst = new ArrayList<>();
111 List<Service> serviceList = new ArrayList<>();
112 Map<String, List<Component>> nodeToFixTosca = new HashMap<>();
113 Map<String, List<Component>> vfToFixTosca = new HashMap<>();
114 Map<String, List<Component>> serviceToFixTosca = new HashMap<>();
116 long time = System.currentTimeMillis();
118 doFixTosca(nodeToFixTosca, vfToFixTosca, serviceToFixTosca);
120 if ("vf_only".equals(fixComponent)) {
121 if (!fetchFaultVf(fixComponent, vfLst, time)) {
125 if (!fetchServices(fixComponent, serviceList, time)) {
129 if ("service_vf".equals(runMode) || "fix".equals(runMode)) {
130 log.info("Mode {}. Find problem VFs", runMode);
131 if (!fetchVf(serviceList, vfLst, time)) {
132 log.info("Mode {}. Find problem VFs finished with failure", runMode);
135 log.info("Mode {}. Find problem VFs finished with success", runMode);
137 if ("fix".equals(runMode) || "fix_only_services".equals(runMode)) {
138 log.info("Mode {}. Start fix", runMode);
139 if (!fix(vfLst, serviceList, nodeToFixTosca, vfToFixTosca, serviceToFixTosca)) {
140 log.info("Mode {}. Fix finished with failure", runMode);
143 log.info("Mode {}. Fix finished with success", runMode);
149 private boolean fetchFaultVf(String fixComponent, List<Resource> vfLst, long time) {
150 log.info("Find fault VF ");
151 String fileName = "fault_" + time + ".csv";
152 try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF8))) {
153 writer.write("vf name, vf id, state, version\n");
155 Map<GraphPropertyEnum, Object> hasProps = new EnumMap<>(GraphPropertyEnum.class);
156 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
157 hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF.name());
159 Map<GraphPropertyEnum, Object> hasNotProps = new EnumMap<>(GraphPropertyEnum.class);
160 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
161 log.info("Try to fetch resources with properties {} and not {}", hasProps, hasNotProps);
163 Either<List<GraphVertex>, JanusGraphOperationStatus> servicesByCriteria = janusGraphDao
164 .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll);
165 if (servicesByCriteria.isRight()) {
166 log.info("Failed to fetch resources {}", servicesByCriteria.right().value());
169 List<GraphVertex> resources = servicesByCriteria.left().value();
170 for (GraphVertex gv : resources) {
171 ComponentParametersView filter = new ComponentParametersView(true);
172 filter.setIgnoreComponentInstances(false);
173 filter.setIgnoreArtifacts(false);
174 filter.setIgnoreGroups(false);
176 Either<Resource, StorageOperationStatus> toscaElement = toscaOperationFacade
177 .getToscaElement(gv.getUniqueId());
178 if (toscaElement.isRight()) {
179 log.info("Failed to fetch resources {} {}", gv.getUniqueId(), toscaElement.right().value());
183 Resource resource = toscaElement.left().value();
184 String resourceName = resource.getName();
185 Map<String, ArtifactDefinition> deploymentArtifacts = resource.getDeploymentArtifacts();
186 List<GroupDefinition> groups = resource.getGroups();
187 if (groups == null || groups.isEmpty()) {
188 log.info("No groups for resource {} id {} ", resourceName, gv.getUniqueId());
191 boolean isProblematic = false;
192 for (GroupDefinition gr : groups) {
193 if (gr.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
194 if (isProblematicGroup(gr, resourceName, deploymentArtifacts)) {
195 isProblematic = true;
202 writeModuleResultToFile(writer, resource, null);
205 janusGraphDao.commit();
208 } catch (Exception e) {
209 log.info(FAILED_TO_FETCH_VF_RESOURCES, e);
212 janusGraphDao.commit();
217 private boolean fetchVf(List<Service> serviceList, List<Resource> vfLst, long time) {
218 log.info("Find problem VF ");
219 if (serviceList.isEmpty()) {
220 log.info("No services as input");
223 String fileName = "problemVf_" + time + ".csv";
224 try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF8))) {
225 writer.write("vf name, vf id, state, version, example service name\n");
226 Set<String> vfIds = new HashSet<>();
227 for (Service service : serviceList) {
228 List<ComponentInstance> componentInstances = service.getComponentInstances().stream()
229 .filter(ci -> ci.getOriginType().equals(OriginTypeEnum.VF)).collect(Collectors.toList());
230 for (ComponentInstance ci : componentInstances) {
231 if (!vfIds.contains(ci.getComponentUid())) {
232 vfIds.add(ci.getComponentUid());
233 ComponentParametersView filter = new ComponentParametersView(true);
234 filter.setIgnoreComponentInstances(false);
235 filter.setIgnoreArtifacts(false);
236 filter.setIgnoreGroups(false);
237 Either<Resource, StorageOperationStatus> toscaElement = toscaOperationFacade
238 .getToscaElement(ci.getComponentUid(), filter);
239 if (toscaElement.isRight()) {
240 log.info("Failed to fetch resource {} {}", ci.getComponentUid(),
241 toscaElement.right().value());
244 Resource resource = toscaElement.left().value();
245 if (resource.getResourceType().equals(ResourceTypeEnum.VF)) {
247 writeModuleResultToFile(writer, resource, service);
251 janusGraphDao.commit();
255 log.info("output file with list of Vf : {}", fileName);
256 } catch (Exception e) {
257 log.info("Failed to fetch services ", e);
260 janusGraphDao.commit();
265 private boolean fetchServices(String fixServices, List<Service> serviceList, long time) {
266 log.info("Find problem Services {}", fixServices);
267 String fileName = "problemService_" + time + ".csv";
268 try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF8))) {
269 writer.write("service name, service id, state, version\n");
271 Map<GraphPropertyEnum, Object> hasProps = new EnumMap<>(GraphPropertyEnum.class);
272 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
273 if ("distributed_only".equals(fixServices)) {
274 hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
275 hasProps.put(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTED.name());
278 Map<GraphPropertyEnum, Object> hasNotProps = new EnumMap<>(GraphPropertyEnum.class);
279 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
280 log.info("Try to fetch services with properties {} and not {}", hasProps, hasNotProps);
282 Either<List<GraphVertex>, JanusGraphOperationStatus> servicesByCriteria = janusGraphDao
283 .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll);
284 if (servicesByCriteria.isRight()) {
285 log.info("Failed to fetch services {}", servicesByCriteria.right().value());
288 List<GraphVertex> services = servicesByCriteria.left().value();
289 for (GraphVertex gv : services) {
290 ComponentParametersView filter = new ComponentParametersView(true);
291 filter.setIgnoreComponentInstances(false);
292 filter.setIgnoreArtifacts(false);
293 filter.setIgnoreGroups(false);
295 Either<Service, StorageOperationStatus> toscaElement = toscaOperationFacade
296 .getToscaElement(gv.getUniqueId(), filter);
297 if (toscaElement.isRight()) {
298 log.info("Failed to fetch service {} {}", gv.getUniqueId(), toscaElement.right().value());
301 Service service = toscaElement.left().value();
303 String serviceName = (String) gv.getMetadataProperty(GraphPropertyEnum.NAME);
305 boolean isProblematic = isProblematicService(service, serviceName);
307 serviceList.add(service);
308 writeModuleResultToFile(writer, service, null);
313 janusGraphDao.commit();
315 log.info("output file with list of services : {}", fileName);
316 } catch (Exception e) {
317 log.info("Failed to fetch services ", e);
320 janusGraphDao.commit();
325 private boolean isProblematicService( Service service, String serviceName) {
327 List<ComponentInstance> componentInstances = service.getComponentInstances();
329 if (componentInstances == null) {
330 log.info("No instances for service {} ", service.getUniqueId());
333 boolean isCheckVFModules = true;
334 if(service.getLifecycleState() == LifecycleStateEnum.NOT_CERTIFIED_CHECKIN ||
335 service.getLifecycleState() == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT){
336 isCheckVFModules = false;
338 for (ComponentInstance ci : componentInstances) {
339 Map<String, ArtifactDefinition> deploymentArtifacts = ci.getDeploymentArtifacts();
340 List<GroupInstance> groupInstances = ci.getGroupInstances();
341 if (groupInstances == null || groupInstances.isEmpty()) {
342 log.info("No instance groups for instance {} in service {} id {} ", ci.getName(), serviceName,
343 service.getUniqueId());
346 List<VfModuleArtifactPayloadEx> vfModules = null;
347 if(isCheckVFModules){
348 Optional<ArtifactDefinition> optionalVfModuleArtifact = deploymentArtifacts.values().stream()
349 .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.VF_MODULES_METADATA.name())).findAny();
351 if(!optionalVfModuleArtifact.isPresent())
354 ArtifactDefinition vfModuleArtifact = optionalVfModuleArtifact.get();
355 Either<List<VfModuleArtifactPayloadEx>, StorageOperationStatus> vfModulesEither = parseVFModuleJson(vfModuleArtifact);
356 if(vfModulesEither.isRight()){
357 log.error("Failed to parse vfModule for service {} status is {}", service.getUniqueId(), vfModulesEither.right().value());
360 vfModules = vfModulesEither.left().value();
361 if(vfModules == null || vfModules.isEmpty() ){
362 log.info("vfModules empty for service {}", service.getUniqueId());
367 for (GroupInstance gi : groupInstances) {
368 if (gi.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
369 VfModuleArtifactPayloadEx vfModule= null;
370 if(isCheckVFModules && vfModules != null){
371 Optional<VfModuleArtifactPayloadEx> op = vfModules.stream().filter(vf -> vf.getVfModuleModelName().equals(gi.getGroupName())).findAny();
373 log.error("Failed to find vfModule for group {}", gi.getGroupName());
378 if (isProblematicGroupInstance(gi, ci.getName(), serviceName, deploymentArtifacts, vfModule)) {
388 private boolean isProblematicGroup(GroupDefinition gr, String resourceName,
389 Map<String, ArtifactDefinition> deploymentArtifacts) {
390 List<String> artifacts = gr.getArtifacts();
391 List<String> artifactsUuid = gr.getArtifactsUuid();
392 Set<String> artifactsSet = new HashSet<>();
394 if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) {
395 log.info("No groups in resource {} ", resourceName);
398 artifactsSet.addAll(artifacts);
399 if (artifactsSet.size() < artifacts.size()) {
400 log.info(" artifactsSet.size() < artifacts.size() group {} in resource {} ", gr.getName(), resourceName);
404 if ((artifactsUuid != null) && (artifacts.size() < artifactsUuid.size())) {
405 log.info(" artifacts.size() < artifactsUuid.size() group {} in resource {} ", gr.getName(), resourceName);
408 if (!artifacts.isEmpty() && (artifactsUuid == null || artifactsUuid.isEmpty())) {
410 " artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() group {} in resource {} ",
411 gr.getName(), resourceName);
414 if (artifactsUuid != null && artifactsUuid.contains(null)) {
415 log.info(" artifactsUuid.contains(null) group {} in resource {} ", gr.getName(), resourceName);
419 for (String artifactId : artifacts) {
420 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
421 ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactlabel);
422 if (artifactDefinition == null) {
423 log.info(" artifactDefinition == null label {} group {} in resource {} ", artifactlabel, gr.getName(),
427 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactDefinition.getArtifactType());
428 if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
429 if (!artifactId.equals(artifactDefinition.getUniqueId())) {
431 " !artifactId.equals(artifactDefinition.getUniqueId() artifact {} artId {} group {} in resource {} ",
432 artifactlabel, artifactId, gr.getName(), resourceName);
435 if (artifactsUuid != null && !artifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
437 " artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} group {} in resource {} ",
438 artifactlabel, gr.getName(), resourceName);
443 for (String artifactUUID : artifactsUuid) {
444 String label = findArtifactLabelFromArtifactId(artifactUUID);
445 if (label != null && !label.isEmpty() && !label.equals("")) {
453 private boolean isProblematicGroupInstance(GroupInstance gi, String instName, String servicename,
454 Map<String, ArtifactDefinition> deploymentArtifacts, VfModuleArtifactPayloadEx vfModule) {
455 List<String> artifacts = gi.getArtifacts();
456 List<String> artifactsUuid = gi.getArtifactsUuid();
457 List<String> instArtifactsUuid = gi.getGroupInstanceArtifactsUuid();
458 List<String> instArtifactsId = gi.getGroupInstanceArtifacts();
459 Set<String> instArtifatIdSet = new HashSet<>();
460 Set<String> artifactsSet = new HashSet<>();
462 log.info("check group {} for instance {} ", gi.getGroupName(), instName);
463 if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) {
464 log.info("No instance groups for instance {} in service {} ", instName, servicename);
467 artifactsSet.addAll(artifacts);
468 if (artifactsSet.size() < artifacts.size()) {
469 log.info(" artifactsSet.size() < artifacts.size() group {} in resource {} ", instName, servicename);
473 if (instArtifactsId != null && !instArtifactsId.isEmpty()) {
474 instArtifatIdSet.addAll(instArtifactsId);
477 if ((artifactsUuid != null) && (artifacts.size() < artifactsUuid.size())) {
478 log.info(" artifacts.size() < artifactsUuid.size() inst {} in service {} ", instName, servicename);
481 if (!artifacts.isEmpty() && (artifactsUuid == null || artifactsUuid.isEmpty())) {
483 " artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() inst {} in service {} ",
484 instName, servicename);
487 if (artifactsUuid != null && artifactsUuid.contains(null)) {
488 log.info(" artifactsUuid.contains(null) inst {} in service {} ", instName, servicename);
491 if (instArtifactsId != null && instArtifatIdSet.size() < instArtifactsId.size()) {
492 log.info(" instArtifatIdSet.size() < instArtifactsId.size() inst {} in service {} ", instName, servicename);
496 if ((instArtifactsId != null && instArtifactsUuid != null)
497 && instArtifactsId.size() != instArtifactsUuid.size()) {
498 log.info(" instArtifactsId.size() != instArtifactsUuid.size() inst {} in service {} ", instName,
503 for (String artifactId : artifacts) {
504 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
505 ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactlabel);
506 if (artifactDefinition == null) {
507 log.info(" artifactDefinition == null label {} inst {} in service {} ", artifactlabel, instName,
511 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactDefinition.getArtifactType());
512 if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
513 if (!artifactId.equals(artifactDefinition.getUniqueId())) {
515 " !artifactId.equals(artifactDefinition.getUniqueId() artifact {} artId {} inst {} in service {} ",
516 artifactlabel, artifactId, instName, servicename);
519 if (artifactsUuid != null && !artifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
521 " artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ",
522 artifactlabel, instName, servicename);
526 if (instArtifactsUuid == null || instArtifactsUuid.isEmpty()) {
527 log.info(" instArtifactsUuid empty. label {} inst {} in service {} ", artifactlabel, instName,
531 if (!instArtifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
533 " instArtifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ",
534 artifactlabel, instName, servicename);
539 for (String artifactUUID : artifactsUuid) {
540 String label = findArtifactLabelFromArtifactId(artifactUUID);
541 if (label != null && !label.isEmpty() && !"".equals(label)) {
545 if(vfModule != null && artifactsUuid != null){
546 return isProblematicVFModule(vfModule, artifactsUuid, instArtifactsUuid);
552 private boolean isProblematicVFModule(VfModuleArtifactPayloadEx vfModule, List<String> artifactsUuid,
553 List<String> instArtifactsUuid) {
554 log.info(" isProblematicVFModule {} ", vfModule.getVfModuleModelName());
555 List<String> vfModuleArtifacts = vfModule.getArtifacts();
556 List<String> allArtifacts = new ArrayList<>();
557 allArtifacts.addAll(artifactsUuid);
558 if(instArtifactsUuid != null)
559 allArtifacts.addAll(instArtifactsUuid);
560 if((vfModuleArtifacts == null || vfModuleArtifacts.isEmpty()) && !artifactsUuid.isEmpty()){
561 log.error(" vfModuleArtifacts == null || vfModuleArtifacts.isEmpty()) && !artifactsUuid.isEmpty()");
564 if(vfModuleArtifacts!= null){
565 if( vfModuleArtifacts.size() != allArtifacts.size()){
566 log.error(" vfModuleArtifacts.size() != allArtifacts.size()");
569 for(String vfModuleArtifact: vfModuleArtifacts){
570 Optional<String> op = allArtifacts.stream().filter(a -> a.equals(vfModuleArtifact)).findAny();
572 log.error("failed to find artifact {} in group artifacts {}", vfModuleArtifact, allArtifacts);
582 private boolean fix(List<Resource> vfLst, List<Service> serviceList, Map<String, List<Component>> nodesToFixTosca,
583 Map<String, List<Component>> vfToFixTosca, Map<String, List<Component>> servicesToFixTosca) {
585 log.info(" Fix started ***** ");
586 if (vfLst != null && !vfLst.isEmpty()) {
591 if (res && serviceList != null && !serviceList.isEmpty()) {
592 res = fixServices(serviceList);
596 Set<String> fixedIds = new HashSet<>();
598 long time = System.currentTimeMillis();
599 String fileName = "FailedGenerateTosca" + "_" + time + ".csv";
600 try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF8))) {
601 writer.write("componentType, name, version, UID, UUID, invariantUUID, state\n");
602 List<Component> failedList = new ArrayList<>();
604 if (res && nodesToFixTosca != null && !nodesToFixTosca.isEmpty()) {
606 generateAndSaveToscaArtifacts(nodesToFixTosca, fixedIds, null, failedList);
609 if (vfToFixTosca != null && !vfToFixTosca.isEmpty()) {
611 generateAndSaveToscaArtifacts(vfToFixTosca, fixedIds, vfLst, failedList);
615 for (Component component : vfLst) {
616 res = generateToscaPerComponent(fixedIds, component);
618 TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(component);
619 Map<String, GroupDataDefinition> groups = topologyTemplate.getGroups();
620 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.GROUPS, EdgeLabelEnum.GROUPS, groups);
622 Map<String, ArtifactDataDefinition> arifacts = topologyTemplate.getDeploymentArtifacts();
623 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.DEPLOYMENT_ARTIFACTS,
624 EdgeLabelEnum.DEPLOYMENT_ARTIFACTS, arifacts);
627 Map<String, ArtifactDataDefinition> arifacts = topologyTemplate.getToscaArtifacts();
628 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS,
629 EdgeLabelEnum.TOSCA_ARTIFACTS, arifacts);
631 janusGraphDao.commit();
633 failedList.add(component);
637 if (servicesToFixTosca != null && !servicesToFixTosca.isEmpty()) {
638 generateAndSaveToscaArtifacts(servicesToFixTosca, fixedIds, serviceList, failedList);
643 for (Component component : serviceList) {
644 res = generateToscaPerComponent(fixedIds, component);
646 TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(component);
647 Map<String, MapGroupsDataDefinition> groups = topologyTemplate.getInstGroups();
648 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS,
652 Map<String, MapArtifactDataDefinition> artifacts = topologyTemplate
653 .getInstDeploymentArtifacts();
654 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS,
655 EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, artifacts);
658 Map<String, ArtifactDataDefinition> arifacts = topologyTemplate.getToscaArtifacts();
659 res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS,
660 EdgeLabelEnum.TOSCA_ARTIFACTS, arifacts);
662 janusGraphDao.commit();
664 failedList.add(component);
668 if (!failedList.isEmpty()) {
669 for (Component component : failedList) {
670 StringBuilder sb = new StringBuilder(component.getComponentType().getValue());
671 sb.append(",").append(component.getName()).append(",").append(component.getVersion()).append(",")
672 .append(component.getUniqueId()).append(",").append(component.getUUID()).append(",")
673 .append(component.getInvariantUUID()).append(",").append(component.getLifecycleState());
676 writer.write(sb.toString());
680 } catch (IOException e) {
681 log.error(e.getMessage());
683 janusGraphDao.commit();
685 log.info(" Fix finished with res {} ***** ", res);
689 private boolean generateAndSaveToscaArtifacts(Map<String, List<Component>> nodesToFixTosca, Set<String> fixedIds,
690 List<? extends Component> componentsWithFailedGroups, List<Component> failedList) {
692 log.debug("Migration1707ArtifactUuidFix generateAndSaveToscaArtifacts started ");
693 for (Map.Entry<String, List<Component>> entry : nodesToFixTosca.entrySet()) {
695 List<Component> component = entry.getValue();
696 for (Component c : component) {
697 log.debug("Migration1707ArtifactUuidFix fix tosca on component : id {}, name {} ", c.getUniqueId(),
699 if (componentsWithFailedGroups != null) {
700 Optional<Component> op = (Optional<Component>) componentsWithFailedGroups.stream()
701 .filter(cg -> cg.getUniqueId().equals(c.getUniqueId())).findAny();
703 res = generateToscaPerComponent(fixedIds, c);
705 res = generateToscaPerComponent(fixedIds, c);
707 ToscaElement topologyTemplate = ModelConverter.convertToToscaElement(c);
708 Map<String, ArtifactDataDefinition> arifacts = topologyTemplate.getToscaArtifacts();
709 res = fixDataOnGraph(c.getUniqueId(), VertexTypeEnum.TOSCA_ARTIFACTS, EdgeLabelEnum.TOSCA_ARTIFACTS,
711 janusGraphDao.commit();
718 log.debug("Migration1707ArtifactUuidFix generateAndSaveToscaArtifacts finished with res {} ", res);
722 private boolean generateToscaPerComponent(Set<String> fixedIds, Component c) {
724 log.debug("Migration1707ArtifactUuidFix generateToscaPerComponent started component name {} id {}",
725 c.getName(), c.getUniqueId());
727 Either<Component, StorageOperationStatus> toscaElement = toscaOperationFacade
728 .getToscaFullElement(c.getUniqueId());
729 if (toscaElement.isRight()) {
730 log.info("Failed to fetch resources {} {}", c.getUniqueId(), toscaElement.right().value());
733 Component toscaElementFull = toscaElement.left().value();
734 toscaElementFull.setGroups(c.getGroups());
735 List<ComponentInstance> ciListFull = toscaElementFull.getComponentInstances();
736 List<ComponentInstance> ciList = c.getComponentInstances();
737 if (ciListFull != null && !ciListFull.isEmpty()) {
738 ciListFull.forEach(ciFull -> {
739 ComponentInstance compInst = ciList.stream()
740 .filter(ci -> ci.getUniqueId().equals(ciFull.getUniqueId())).findAny().get();
741 ciFull.setGroupInstances(compInst.getGroupInstances());
745 Either<Component, ToscaError> either = generateToscaArtifact(toscaElementFull);
747 if (either.isRight()) {
748 log.error("Couldn't generate and save tosca template component unique id {}, name {} error: {}",
749 toscaElementFull.getUniqueId(), toscaElementFull.getName(), either.right().value());
755 c.setToscaArtifacts(either.left().value().getToscaArtifacts());
756 fixedIds.add(toscaElementFull.getUniqueId());
760 janusGraphDao.commit();
762 janusGraphDao.rollback();
764 log.debug("Migration1707ArtifactUuidFix generateToscaPerComponent finished component name {} id {} res {}",
765 c.getName(), c.getUniqueId(), res);
769 private <T extends ToscaDataDefinition> boolean fixDataOnGraph(String componentId, VertexTypeEnum vertexTypeEnum,
770 EdgeLabelEnum edgeLabelEnum, Map<String, T> groups) {
771 log.debug("amount groups to update: VertexTypeEnum {} EdgeLabelEnum {} data size {}", vertexTypeEnum.getName(),
772 edgeLabelEnum, groups.size());
774 Either<GraphVertex, JanusGraphOperationStatus> getResponse = janusGraphDao.getVertexById(componentId,
775 JsonParseFlagEnum.NoParse);
776 if (getResponse.isRight()) {
777 log.debug("Couldn't fetch component unique id {}, error: {}", componentId, getResponse.right().value());
782 GraphVertex componentVertex = getResponse.left().value();
784 GraphVertex toscaDataVertex = null;
785 Either<GraphVertex, JanusGraphOperationStatus> groupVertexEither = janusGraphDao.getChildVertex(componentVertex,
786 edgeLabelEnum, JsonParseFlagEnum.ParseJson);
787 if (groupVertexEither.isRight() && groupVertexEither.right().value() == JanusGraphOperationStatus.NOT_FOUND) {
788 log.debug("no child {} vertex for component unique id {}, error: {}", edgeLabelEnum, componentId,
789 groupVertexEither.right().value());
792 if (groupVertexEither.isRight()) {
794 log.debug("failed to get child {} vertex for component unique id {}, error: {}", edgeLabelEnum,
795 componentId, groupVertexEither.right().value());
798 toscaDataVertex = groupVertexEither.left().value();
799 toscaDataVertex.setJson(groups);
800 Either<GraphVertex, JanusGraphOperationStatus> updatevertexEither = janusGraphDao.updateVertex(toscaDataVertex);
801 if (updatevertexEither.isRight()) {
802 log.debug("failed to update vertex for component unique id {}, error: {}", componentId,
803 updatevertexEither.right().value());
804 janusGraphDao.rollback();
809 log.debug("Fix data on graph finished: VertexTypeEnum {} EdgeLabelEnum {} res {}", vertexTypeEnum.getName(),
814 private boolean fixServices(List<Service> serviceList) {
815 for (Service service : serviceList) {
816 log.debug("Migration1707ArtifactUuidFix fix service: id {}, name {} ", service.getUniqueId(),
818 List<ComponentInstance> instances = service.getComponentInstances();
819 for (ComponentInstance instance : instances) {
820 fixComponentInstances(service, instance);
828 private void fixComponentInstances(Service service, ComponentInstance instance) {
829 Map<String, ArtifactDefinition> artifactsMap = instance.getDeploymentArtifacts();
830 List<GroupInstance> groupsList = instance.getGroupInstances();
831 if (groupsList != null && artifactsMap != null) {
832 List<GroupInstance> groupsToDelete = new ArrayList<>();
833 for (GroupInstance group : groupsList) {
834 fixGroupInstances(service, artifactsMap, groupsToDelete, group);
838 if (!groupsToDelete.isEmpty()) {
839 log.debug("Migration1707ArtifactUuidFix delete group: resource id {}, group instance to delete {} ",
840 service.getUniqueId(), groupsToDelete);
841 groupsList.removeAll(groupsToDelete);
845 Optional<ArtifactDefinition> optionalVfModuleArtifact = artifactsMap.values().stream()
846 .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.VF_MODULES_METADATA.name())).findAny();
847 ArtifactDefinition vfModuleArtifact;
848 if(!optionalVfModuleArtifact.isPresent()){
849 vfModuleArtifact = createVfModuleArtifact(instance, service);
850 artifactsMap.put(vfModuleArtifact.getArtifactLabel(), vfModuleArtifact);
853 vfModuleArtifact = optionalVfModuleArtifact.get();
855 fillVfModuleInstHeatEnvPayload(service, instance, groupsList, vfModuleArtifact);
859 private void fixGroupInstances(Service service, Map<String, ArtifactDefinition> artifactsMap,
860 List<GroupInstance> groupsToDelete, GroupInstance group) {
861 if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
862 log.debug("Migration1707ArtifactUuidFix fix group: resource id {}, group name {} ", service.getUniqueId(),
864 if(group.getArtifacts() != null){
865 Set<String> groupArtifactsSet = new HashSet<>(group.getArtifacts());
866 if(group.getGroupInstanceArtifacts() != null){
867 List<String> groupInsArtifacts = new ArrayList<>(group.getGroupInstanceArtifacts());
868 groupArtifactsSet.addAll(groupInsArtifacts);
870 List<String> groupArtifacts = new ArrayList<>(groupArtifactsSet);
872 clearGroupInstanceArtifacts(group);
874 for (String artifactId : groupArtifacts) {
875 fixArtifactUndergroupInstances(artifactsMap, group, groupArtifacts, artifactId);
878 if (group.getArtifacts() == null || group.getArtifacts().isEmpty()) {
880 "Migration1707ArtifactUuidFix fix groupInstance add to delete list: resource id {} name {} , group name {} ",
881 service.getUniqueId(), service.getName(), group.getName());
882 groupsToDelete.add(group);
887 private void clearGroupInstanceArtifacts(GroupInstance group) {
888 if(group.getArtifacts() != null)
889 group.getArtifacts().clear();
891 group.setArtifacts(new ArrayList<>());
892 if(group.getArtifactsUuid() != null)
893 group.getArtifactsUuid().clear();
895 group.setArtifactsUuid(new ArrayList<>());
897 if(group.getGroupInstanceArtifacts() != null)
898 group.getGroupInstanceArtifacts().clear();
900 group.setGroupInstanceArtifacts(new ArrayList<>());
902 if(group.getGroupInstanceArtifactsUuid() != null )
903 group.getGroupInstanceArtifactsUuid().clear();
905 group.setGroupInstanceArtifactsUuid(new ArrayList<>());
908 private void fixArtifactUndergroupInstances(Map<String, ArtifactDefinition> artifactsMap, GroupInstance group,
909 List<String> groupArtifacts, String artifactId) {
910 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
911 log.debug("Migration1707ArtifactUuidFix fix group: group name {} artifactId for fix {} artifactlabel {} ",
912 group.getName(), artifactId, artifactlabel);
913 if (!artifactlabel.isEmpty() && artifactsMap.containsKey(artifactlabel)) {
914 ArtifactDefinition artifact = artifactsMap.get(artifactlabel);
915 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifact.getArtifactType());
916 String correctArtifactId = artifact.getUniqueId();
917 String correctArtifactUUID = artifact.getArtifactUUID();
918 if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
919 boolean isAddToGroup = true;
920 if (groupArtifacts.size() == 1) {
921 if (artifactType == ArtifactTypeEnum.HEAT_ARTIFACT) {
922 isAddToGroup = false;
923 artifact.setArtifactType(ArtifactTypeEnum.OTHER.getType());
928 MIGRATION1707_ARTIFACT_UUID_FIX,
929 group.getName(), correctArtifactId, correctArtifactUUID);
930 group.getArtifacts().add(correctArtifactId);
931 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
932 group.getArtifactsUuid().add(correctArtifactUUID);
937 MIGRATION1707_ARTIFACT_UUID_FIX,
938 group.getName(), correctArtifactId, correctArtifactUUID);
939 Set<String> tmpSet = new HashSet<>(group.getGroupInstanceArtifacts());
940 tmpSet.add(correctArtifactId);
941 group.setGroupInstanceArtifacts(new ArrayList<>(tmpSet));
942 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
943 Set<String> tmpSetUUID = new HashSet<>(group.getGroupInstanceArtifactsUuid());
944 tmpSetUUID.add(correctArtifactUUID);
945 group.setGroupInstanceArtifactsUuid(new ArrayList<>(tmpSetUUID));
951 private boolean fixVf(List<Resource> vfLst) {
952 for (Resource resource : vfLst) {
953 log.debug("Migration1707ArtifactUuidFix fix resource: id {}, name {} ", resource.getUniqueId(),
955 Map<String, ArtifactDefinition> artifactsMap = resource.getDeploymentArtifacts();
956 List<GroupDefinition> groupsList = resource.getGroups();
957 List<GroupDefinition> groupsToDelete = new ArrayList<>();
958 if (groupsList != null && artifactsMap != null) {
959 for (GroupDefinition group : groupsList) {
960 if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE) && group.getArtifacts() != null) {
961 fixVfGroup(resource, artifactsMap, group);
963 if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)
964 && (group.getArtifacts() == null || group.getArtifacts().isEmpty())) {
966 "Migration1707ArtifactUuidFix add group to delete list fix resource: id {}, name {} ",
967 resource.getUniqueId(), resource.getName(), group.getName());
968 groupsToDelete.add(group);
972 if (!groupsToDelete.isEmpty()) {
973 groupsList.removeAll(groupsToDelete);
983 private void fixVfGroup(Resource resource, Map<String, ArtifactDefinition> artifactsMap, GroupDefinition group) {
984 log.debug("Migration1707ArtifactUuidFix fix group: resource id {}, group name {} ", resource.getUniqueId(),
986 Set<String> groupArtifactsSet = new HashSet<>(group.getArtifacts());
987 List<String> groupArtifacts = new ArrayList<>(groupArtifactsSet);
988 group.getArtifacts().clear();
989 group.getArtifactsUuid().clear();
991 for (String artifactId : groupArtifacts) {
992 fixArtifactUnderGroup(artifactsMap, group, groupArtifacts, artifactId);
996 private void fixArtifactUnderGroup(Map<String, ArtifactDefinition> artifactsMap, GroupDefinition group,
997 List<String> groupArtifacts, String artifactId) {
999 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
1000 log.debug("Migration1707ArtifactUuidFix fix group: group name {} artifactId for fix {} artifactlabel {} ",
1001 group.getName(), artifactId, artifactlabel);
1002 if (!artifactlabel.isEmpty() && artifactsMap.containsKey(artifactlabel)) {
1003 ArtifactDefinition artifact = artifactsMap.get(artifactlabel);
1004 String correctArtifactId = artifact.getUniqueId();
1005 String correctArtifactUUID = artifact.getArtifactUUID();
1006 boolean isAddToGroup = true;
1007 if (groupArtifacts.size() == 1) {
1008 ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifact.getArtifactType());
1009 if (artifactType == ArtifactTypeEnum.HEAT_ARTIFACT) {
1010 isAddToGroup = false;
1011 artifact.setArtifactType(ArtifactTypeEnum.OTHER.getType());
1016 MIGRATION1707_ARTIFACT_UUID_FIX,
1017 group.getName(), correctArtifactId, correctArtifactUUID);
1018 group.getArtifacts().add(correctArtifactId);
1019 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
1020 group.getArtifactsUuid().add(correctArtifactUUID);
1027 private String findArtifactLabelFromArtifactId(String artifactId) {
1028 String artifactLabel = "";
1030 int index = artifactId.lastIndexOf('.');
1031 if (index > 0 && index + 1 < artifactId.length())
1032 artifactLabel = artifactId.substring(index + 1);
1033 return artifactLabel;
1036 private void writeModuleResultToFile(Writer writer, org.openecomp.sdc.be.model.Component component,
1039 // "service name, service id, state, version
1040 StringBuilder sb = new StringBuilder(component.getName());
1041 sb.append(",").append(component.getUniqueId()).append(",").append(component.getLifecycleState()).append(",")
1042 .append(component.getVersion());
1043 if (service != null) {
1044 sb.append(",").append(service.getName());
1047 writer.write(sb.toString());
1048 } catch (IOException e) {
1049 log.error(e.getMessage());
1053 private void writeModuleResultToFile(Writer writer, List<Component> components) {
1055 // "service name, service id, state, version
1056 for (Component component : components) {
1057 StringBuilder sb = new StringBuilder(component.getName());
1058 sb.append(",").append(component.getUniqueId()).append(",").append(component.getInvariantUUID())
1059 .append(",").append(component.getLifecycleState()).append(",").append(component.getVersion());
1062 writer.write(sb.toString());
1064 } catch (IOException e) {
1066 log.error(e.getMessage());
1070 public boolean doFixTosca(Map<String, List<Component>> nodeToFix, Map<String, List<Component>> vfToFix,
1071 Map<String, List<Component>> serviceToFix) {
1073 Map<GraphPropertyEnum, Object> hasProps = new EnumMap<>(GraphPropertyEnum.class);
1074 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
1075 hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
1077 Map<String, List<Component>> vertices = getVerticesToValidate(VertexTypeEnum.NODE_TYPE, hasProps);
1078 boolean result = validateTosca(vertices, nodeToFix, "RESOURCE_TOSCA_ARTIFACTS");//
1081 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
1082 hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF);
1083 hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
1085 vertices = getVerticesToValidate(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps);
1086 result = validateTosca(vertices, vfToFix, "VF_TOSCA_ARTIFACTS");
1089 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
1090 hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
1092 vertices = getVerticesToValidate(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps);
1093 result = validateTosca(vertices, serviceToFix, "SERVICE_TOSCA_ARTIFACTS");
1098 public Map<String, List<Component>> getVerticesToValidate(VertexTypeEnum type,
1099 Map<GraphPropertyEnum, Object> hasProps) {
1101 Map<String, List<Component>> result = new HashMap<>();
1104 Either<List<GraphVertex>, JanusGraphOperationStatus> resultsEither = janusGraphDao
1105 .getByCriteria(type, hasProps);
1106 if (resultsEither.isRight()) {
1107 log.error("getVerticesToValidate failed {} ",resultsEither.right().value());
1110 log.info("getVerticesToValidate: {} vertices to scan", resultsEither.left().value().size());
1111 List<GraphVertex> componentsList = resultsEither.left().value();
1112 componentsList.forEach(vertex -> {
1113 String ivariantUuid = (String) vertex.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID);
1114 if (!result.containsKey(ivariantUuid)) {
1115 List<Component> compList = new ArrayList<>();
1116 result.put(ivariantUuid, compList);
1118 List<Component> compList = result.get(ivariantUuid);
1120 ComponentParametersView filter = new ComponentParametersView(true);
1121 filter.setIgnoreArtifacts(false);
1123 Either<Component, StorageOperationStatus> toscaElement = toscaOperationFacade
1124 .getToscaElement(vertex.getUniqueId(), filter);
1125 if (toscaElement.isRight()) {
1126 log.error("getVerticesToValidate: failed to find element {} staus is {}", vertex.getUniqueId()
1127 ,toscaElement.right().value());
1129 compList.add(toscaElement.left().value());
1131 janusGraphDao.commit();
1135 } catch (Exception e) {
1136 log.info(FAILED_TO_FETCH_VF_RESOURCES, e);
1139 janusGraphDao.commit();
1145 public boolean validateTosca(Map<String, List<Component>> vertices, Map<String, List<Component>> compToFix,
1147 boolean result = true;
1148 long time = System.currentTimeMillis();
1149 String fileName = name + "_" + time + ".csv";
1150 try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF8))) {
1151 writer.write("name, UUID, invariantUUID, state, version\n");
1152 for (Map.Entry<String, List<Component>> entry : vertices.entrySet()) {
1153 List<Component> compList = entry.getValue();
1154 Set<String> artifactEsId = new HashSet<>();
1155 for (Component component : compList) {
1156 Map<String, ArtifactDefinition> toscaArtifacts = component.getToscaArtifacts();
1157 Optional<ArtifactDefinition> op = toscaArtifacts.values().stream()
1158 .filter(a -> artifactEsId.contains(a.getEsId()) && a.getEsId() != null).findAny();
1159 if (op.isPresent()) {
1161 writeModuleResultToFile(writer, compList);
1165 artifactEsId.addAll(toscaArtifacts.values().stream().map(ArtifactDefinition::getEsId)
1166 .collect(Collectors.toList()));
1170 List<Component> compListfull = new ArrayList<>();
1171 for (Component c : compList) {
1172 ComponentParametersView filter = new ComponentParametersView(true);
1173 filter.setIgnoreComponentInstances(false);
1174 filter.setIgnoreArtifacts(false);
1175 filter.setIgnoreGroups(false);
1177 Either<Component, StorageOperationStatus> toscaElement = toscaOperationFacade
1178 .getToscaElement(c.getUniqueId(), filter);
1179 if (toscaElement.isRight()) {
1180 log.debug("getVerticesToValidate: failed to find element" + c.getUniqueId()
1181 + " status is" + toscaElement.right().value());
1183 compListfull.add(toscaElement.left().value());
1185 this.janusGraphDao.commit();
1188 compToFix.put(entry.getKey(), compListfull);
1194 } catch (Exception e) {
1195 log.info(FAILED_TO_FETCH_VF_RESOURCES, e);
1198 janusGraphDao.commit();
1203 private Either<Component, ToscaError> generateToscaArtifact(Component parent) {
1204 log.debug("tosca artifact generation");
1206 Map<String, ArtifactDefinition> toscaArtifacts = parent.getToscaArtifacts();
1208 ArtifactDefinition toscaArtifact = null;
1209 Optional<ArtifactDefinition> op = toscaArtifacts.values().stream()
1210 .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.TOSCA_TEMPLATE.getType())).findAny();
1212 if (op.isPresent()) {
1213 toscaArtifact = op.get();
1215 if (toscaArtifact != null) {
1216 log.debug("Migration1707ArtifactUuidFix generateToscaPerComponent artifact name {} id {} esId {}",
1217 toscaArtifact.getArtifactName(), toscaArtifact.getUniqueId(), toscaArtifact.getEsId());
1219 Either<ToscaRepresentation, ToscaError> exportComponent = toscaExportUtils.exportComponent(parent);
1220 if (exportComponent.isRight()) {
1221 log.debug("Failed export tosca yaml for component {} error {}", parent.getUniqueId(),
1222 exportComponent.right().value());
1224 return Either.right(exportComponent.right().value());
1226 log.debug("Tosca yaml exported for component {} ", parent.getUniqueId());
1227 String payload = exportComponent.left().value().getMainYaml();
1229 toscaArtifact.setPayloadData(payload);
1230 byte[] decodedPayload = toscaArtifact.getPayloadData();
1232 String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(parent.getUniqueId(),
1233 toscaArtifact.getArtifactLabel());
1234 toscaArtifact.setUniqueId(uniqueId);
1235 toscaArtifact.setEsId(toscaArtifact.getUniqueId());
1237 toscaArtifact.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(decodedPayload));
1238 ESArtifactData artifactData = new ESArtifactData(toscaArtifact.getEsId(), decodedPayload);
1239 artifactCassandraDao.saveArtifact(artifactData);
1241 log.debug("Tosca yaml artifact esId {} ", toscaArtifact.getEsId());
1243 ArtifactDefinition csarArtifact = null;
1244 op = toscaArtifacts.values().stream()
1245 .filter(p -> p.getArtifactType().equals(ArtifactTypeEnum.TOSCA_CSAR.getType())).findAny();
1247 if (op.isPresent()) {
1248 csarArtifact = op.get();
1251 if (csarArtifact != null) {
1252 Either<byte[], ResponseFormat> generated = csarUtils.createCsar(parent, true, true);
1254 if (generated.isRight()) {
1255 log.debug("Failed to export tosca csar for component {} error {}", parent.getUniqueId(),
1256 generated.right().value());
1258 return Either.right(ToscaError.GENERAL_ERROR);
1260 byte[] value = generated.left().value();
1261 csarArtifact.setPayload(value);
1262 byte[] decodedPayload = csarArtifact.getPayloadData();
1264 String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(parent.getUniqueId(),
1265 csarArtifact.getArtifactLabel());
1266 csarArtifact.setUniqueId(uniqueId);
1267 csarArtifact.setEsId(csarArtifact.getUniqueId());
1269 csarArtifact.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(decodedPayload));
1270 ESArtifactData artifactData = new ESArtifactData(csarArtifact.getEsId(), decodedPayload);
1271 artifactCassandraDao.saveArtifact(artifactData);
1272 log.debug("Tosca csar artifact esId {} ", csarArtifact.getEsId());
1276 } catch (Exception ex) {
1277 log.error("Failed to generate tosca atifact component id {} component name {} error {}",
1278 parent.getUniqueId(), parent.getName(), ex.getMessage());
1280 return Either.right(ToscaError.GENERAL_ERROR);
1283 return Either.left(parent);
1286 private ArtifactDefinition createVfModuleArtifact(ComponentInstance currVF, Service service) {
1288 ArtifactDefinition vfModuleArtifactDefinition = new ArtifactDefinition();
1290 vfModuleArtifactDefinition.setDescription("Auto-generated VF Modules information artifact");
1291 vfModuleArtifactDefinition.setArtifactDisplayName("Vf Modules Metadata");
1292 vfModuleArtifactDefinition.setArtifactType(ArtifactTypeEnum.VF_MODULES_METADATA.getType());
1293 vfModuleArtifactDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
1294 vfModuleArtifactDefinition.setArtifactLabel("vfModulesMetadata");
1295 vfModuleArtifactDefinition.setTimeout(0);
1296 vfModuleArtifactDefinition.setArtifactName(currVF.getNormalizedName() + "_modules.json");
1298 return vfModuleArtifactDefinition;
1302 private void fillVfModuleInstHeatEnvPayload(Component parent, ComponentInstance instance, List<GroupInstance> groupsForCurrVF,
1303 ArtifactDefinition vfModuleArtifact) {
1304 log.debug("generate new vf module for component. name {}, id {}, Version {}", instance.getName(), instance.getUniqueId());
1306 String uniqueId = UniqueIdBuilder.buildInstanceArtifactUniqueId(parent.getUniqueId(), instance.getUniqueId(), vfModuleArtifact.getArtifactLabel());
1308 vfModuleArtifact.setUniqueId(uniqueId);
1309 vfModuleArtifact.setEsId(vfModuleArtifact.getUniqueId());
1311 List<VfModuleArtifactPayload> vfModulePayloadForCurrVF = new ArrayList<>();
1312 if (groupsForCurrVF != null) {
1313 for (GroupInstance groupInstance : groupsForCurrVF) {
1314 VfModuleArtifactPayload modulePayload = new VfModuleArtifactPayload(groupInstance);
1315 vfModulePayloadForCurrVF.add(modulePayload);
1317 Collections.sort(vfModulePayloadForCurrVF,
1318 (art1, art2) -> VfModuleArtifactPayload.compareByGroupName(art1, art2));
1320 final Gson gson = new GsonBuilder().setPrettyPrinting().create();
1322 String vfModulePayloadString = gson.toJson(vfModulePayloadForCurrVF);
1323 log.debug("vfModulePayloadString {}", vfModulePayloadString);
1324 if (vfModulePayloadString != null) {
1325 String newCheckSum = GeneralUtility
1326 .calculateMD5Base64EncodedByByteArray(vfModulePayloadString.getBytes());
1327 vfModuleArtifact.setArtifactChecksum(newCheckSum);
1329 ESArtifactData artifactData = new ESArtifactData(vfModuleArtifact.getEsId(),
1330 vfModulePayloadString.getBytes());
1331 artifactCassandraDao.saveArtifact(artifactData);
1339 private Either<List<VfModuleArtifactPayloadEx>, StorageOperationStatus> parseVFModuleJson(ArtifactDefinition vfModuleArtifact) {
1340 log.info("Try to get vfModule json from cassandra {}", vfModuleArtifact.getEsId());
1341 Either<ESArtifactData, CassandraOperationStatus> vfModuleData = artifactCassandraDao.getArtifact(vfModuleArtifact.getEsId());
1343 if (vfModuleData.isRight()) {
1344 CassandraOperationStatus resourceUploadStatus = vfModuleData.right().value();
1345 StorageOperationStatus storageResponse = DaoStatusConverter.convertCassandraStatusToStorageStatus(resourceUploadStatus);
1346 log.error("failed to fetch vfModule json {} from cassandra. Status is {}", vfModuleArtifact.getEsId(), storageResponse);
1347 return Either.right(storageResponse);
1351 ESArtifactData esArtifactData = vfModuleData.left().value();
1352 String gsonData = new String( esArtifactData.getDataAsArray());
1353 final Gson gson = new GsonBuilder().setPrettyPrinting().create();
1354 JsonArray jsonElement = new JsonArray();
1355 jsonElement = gson.fromJson(gsonData, jsonElement.getClass());
1356 List<VfModuleArtifactPayloadEx> vfModules = new ArrayList<>();
1357 jsonElement.forEach(je ->{
1358 VfModuleArtifactPayloadEx vfModule = ComponentsUtils.parseJsonToObject(je.toString(), VfModuleArtifactPayloadEx.class);
1359 vfModules.add(vfModule);
1362 log.debug ("parse vf module finish {}", gsonData);
1363 return Either.left(vfModules);