d8b2f62705a6602ba9035cffb4ed4b391ba74c42
[sdc.git] /
1 package org.openecomp.sdc.asdctool.impl.migration.v1707;
2
3 import java.io.BufferedWriter;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.OutputStreamWriter;
7 import java.io.Writer;
8 import java.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.HashSet;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Set;
14
15 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
16 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
17 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
18 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
19 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
20 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
21 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
22 import org.openecomp.sdc.be.datatypes.elements.MapGroupsDataDefinition;
23 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
24 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
25 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
26 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
27 import org.openecomp.sdc.be.model.ArtifactDefinition;
28 import org.openecomp.sdc.be.model.Component;
29 import org.openecomp.sdc.be.model.ComponentInstance;
30 import org.openecomp.sdc.be.model.ComponentParametersView;
31 import org.openecomp.sdc.be.model.DistributionStatusEnum;
32 import org.openecomp.sdc.be.model.GroupDefinition;
33 import org.openecomp.sdc.be.model.GroupInstance;
34 import org.openecomp.sdc.be.model.LifecycleStateEnum;
35 import org.openecomp.sdc.be.model.Resource;
36 import org.openecomp.sdc.be.model.Service;
37 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
38 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
39 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
40 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
41 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
42 import org.openecomp.sdc.common.api.Constants;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46
47 import fj.data.Either;
48
49 @org.springframework.stereotype.Component("migration1707UuidFix")
50 public class Migration1707ArtifactUuidFix {
51
52         @Autowired
53         private TitanDao titanDao;
54
55         @Autowired
56         private ToscaOperationFacade toscaOperationFacade;
57
58         private static Logger log = LoggerFactory.getLogger(Migration1707ArtifactUuidFix.class.getName());
59
60         public boolean migrate(String fixComponent, String runMode) {
61                 List<Resource> vfLst = new ArrayList<>();
62                 List<Service> serviceList = new ArrayList<>();
63
64                 long time = System.currentTimeMillis();
65
66                 if (fixComponent.equals("vf_only")) {
67                         if (fetchFaultVf(fixComponent, vfLst, time) == false) {
68                                 return false;
69                         }
70                 } else {
71                         if (fetchServices(fixComponent, serviceList, time) == false) {
72                                 return false;
73                         }
74                 }
75                 if (runMode.equals("service_vf") || runMode.equals("fix")) {
76                         log.info("Mode {}. Find problem VFs", runMode);
77                         if (fetchVf(serviceList, vfLst, time) == false) {
78                                 log.info("Mode {}. Find problem VFs finished with failure", runMode);
79                                 return false;
80                         }
81                         log.info("Mode {}. Find problem VFs finished with success", runMode);
82                 }
83                 if (runMode.equals("fix") || runMode.equals("fix_only_services")) {
84                         log.info("Mode {}. Start fix", runMode);
85                         if (fix(vfLst, serviceList) == false) {
86                                 log.info("Mode {}. Fix finished withh failure", runMode);
87                                 return false;
88                         }
89                         log.info("Mode {}. Fix finished withh success", runMode);
90                 }
91
92                 return true;
93         }
94
95         private boolean fetchFaultVf(String fixComponent, List<Resource> vfLst, long time) {
96                 log.info("Find fault VF ");
97                 Writer writer = null;
98                 try {
99                         String fileName = "fault_" + time + ".csv";
100                         writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"));
101                         writer.write("vf name, vf id, state, version\n");
102
103                         Map<GraphPropertyEnum, Object> hasProps = new HashMap<>();
104                         hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
105                         hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF.name());
106
107                         Map<GraphPropertyEnum, Object> hasNotProps = new HashMap<>();
108                         hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
109                         log.info("Try to fetch resources with properties {} and not {}", hasProps, hasNotProps);
110
111                         Either<List<GraphVertex>, TitanOperationStatus> servicesByCriteria = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll);
112                         if (servicesByCriteria.isRight()) {
113                                 log.info("Failed to fetch resources {}", servicesByCriteria.right().value());
114                                 return false;
115                         }
116                         List<GraphVertex> resources = servicesByCriteria.left().value();
117                         for (GraphVertex gv : resources) {
118                                 ComponentParametersView filter = new ComponentParametersView(true);
119                                 filter.setIgnoreComponentInstances(false);
120                                 filter.setIgnoreArtifacts(false);
121                                 filter.setIgnoreGroups(false);
122
123                                 Either<Resource, StorageOperationStatus> toscaElement = toscaOperationFacade.getToscaElement(gv.getUniqueId());
124                                 if (toscaElement.isRight()) {
125                                         log.info("Failed to fetch resources {} {}", gv.getUniqueId(), toscaElement.right().value());
126                                         return false;
127                                 }
128
129                                 Resource resource = toscaElement.left().value();
130                                 String resourceName = resource.getName();
131                                 Map<String, ArtifactDefinition> deploymentArtifacts = resource.getDeploymentArtifacts();
132                                 List<GroupDefinition> groups = resource.getGroups();
133                                 if (groups == null || groups.isEmpty()) {
134                                         log.info("No groups for resource {} id {} ", resourceName, gv.getUniqueId());
135                                         continue;
136                                 }
137                                 boolean isProblematic = false;
138                                 for (GroupDefinition gr : groups) {
139                                         if (gr.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
140                                                 if (isProblematicGroup(gr, resourceName, deploymentArtifacts)) {
141                                                         isProblematic = true;
142                                                         break;
143                                                 }
144                                         }
145                                 }
146                                 if (isProblematic) {
147                                         vfLst.add(resource);
148                                         writeModuleResultToFile(writer, resource, null);
149                                         writer.flush();
150                                 }
151                                 titanDao.commit();
152                         }
153
154                 } catch (Exception e) {
155                         log.info("Failed to fetch vf resources ", e);
156                         return false;
157                 } finally {
158                         titanDao.commit();
159                         try {
160                                 writer.flush();
161                                 writer.close();
162                         } catch (Exception ex) {
163                                 /* ignore */}
164                 }
165                 return true;
166         }
167
168         private boolean fetchVf(List<Service> serviceList, List<Resource> vfLst, long time) {
169                 log.info("Find problem VF ");
170                 if (serviceList.isEmpty()) {
171                         log.info("No services as input");
172                         return true;
173                 }
174                 Writer writer = null;
175                 try {
176                         String fileName = "problemVf_" + time + ".csv";
177                         writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"));
178                         writer.write("vf name, vf id, state, version, example service name\n");
179                         Set<String> vfIds = new HashSet<>();
180                         for (Service service : serviceList) {
181                                 List<ComponentInstance> componentInstances = service.getComponentInstances();
182                                 for (ComponentInstance ci : componentInstances) {
183                                         if (!vfIds.contains(ci.getComponentUid())) {
184                                                 vfIds.add(ci.getComponentUid());
185                                                 Either<Resource, StorageOperationStatus> toscaElement = toscaOperationFacade.getToscaElement(ci.getComponentUid());
186                                                 if (toscaElement.isRight()) {
187                                                         log.info("Failed to fetch resource {} {}", ci.getComponentUid(), toscaElement.right().value());
188                                                         return false;
189                                                 }
190                                                 Resource resource = toscaElement.left().value();
191                                                 vfLst.add(resource);
192                                                 writeModuleResultToFile(writer, resource, service);
193                                                 writer.flush();
194                                                 titanDao.commit();
195                                         }
196                                 }
197                         }
198                         log.info("output file with list of Vf : {}", fileName);
199                 } catch (Exception e) {
200                         log.info("Failed to fetch services ", e);
201                         return false;
202                 } finally {
203                         titanDao.commit();
204                         try {
205                                 writer.flush();
206                                 writer.close();
207                         } catch (Exception ex) {
208                                 /* ignore */}
209                 }
210                 return true;
211         }
212
213         private boolean fetchServices(String fixServices, List<Service> serviceList, long time) {
214                 log.info("Find problem Services {}", fixServices);
215                 Writer writer = null;
216
217                 try {
218                         String fileName = "problemService_" + time + ".csv";
219                         writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"));
220                         writer.write("service name, service id, state, version\n");
221
222                         Map<GraphPropertyEnum, Object> hasProps = new HashMap<>();
223                         hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
224                         if (fixServices.equals("distributed_only")) {
225                                 hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
226                                 hasProps.put(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTED.name());
227                         }
228
229                         Map<GraphPropertyEnum, Object> hasNotProps = new HashMap<>();
230                         hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
231                         log.info("Try to fetch services with properties {} and not {}", hasProps, hasNotProps);
232
233                         Either<List<GraphVertex>, TitanOperationStatus> servicesByCriteria = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll);
234                         if (servicesByCriteria.isRight()) {
235                                 log.info("Failed to fetch services {}", servicesByCriteria.right().value());
236                                 return false;
237                         }
238                         List<GraphVertex> services = servicesByCriteria.left().value();
239                         for (GraphVertex gv : services) {
240                                 ComponentParametersView filter = new ComponentParametersView(true);
241                                 filter.setIgnoreComponentInstances(false);
242                                 filter.setIgnoreArtifacts(false);
243                                 filter.setIgnoreGroups(false);
244
245                                 Either<Service, StorageOperationStatus> toscaElement = toscaOperationFacade.getToscaElement(gv.getUniqueId());
246                                 if (toscaElement.isRight()) {
247                                         log.info("Failed to fetch service {} {}", gv.getUniqueId(), toscaElement.right().value());
248                                         return false;
249                                 }
250                                 Service service = toscaElement.left().value();
251                                 List<ComponentInstance> componentInstances = service.getComponentInstances();
252                                 boolean isProblematic = false;
253                                 if (componentInstances == null) {
254                                         log.info("No instances for service {} ", gv.getUniqueId());
255                                         continue;
256                                 }
257                                 String serviceName = (String) gv.getMetadataProperty(GraphPropertyEnum.NAME);
258
259                                 for (ComponentInstance ci : componentInstances) {
260                                         Map<String, ArtifactDefinition> deploymentArtifacts = ci.getDeploymentArtifacts();
261                                         List<GroupInstance> groupInstances = ci.getGroupInstances();
262                                         if (groupInstances == null || groupInstances.isEmpty()) {
263                                                 log.info("No instance groups for instance {} in service {} id {} ", ci.getName(), serviceName, gv.getUniqueId());
264                                                 continue;
265                                         }
266
267                                         for (GroupInstance gi : groupInstances) {
268                                                 if (gi.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
269                                                         if (isProblematicGroupInstance(gi, ci.getName(), serviceName, deploymentArtifacts)) {
270                                                                 isProblematic = true;
271                                                                 break;
272                                                         }
273                                                 }
274                                         }
275                                         if (isProblematic) {
276                                                 serviceList.add(service);
277                                                 writeModuleResultToFile(writer, service, null);
278                                                 writer.flush();
279                                                 break;
280                                         }
281                                 }
282                                 titanDao.commit();
283                         }
284                         log.info("output file with list of services : {}", fileName);
285                 } catch (Exception e) {
286                         log.info("Failed to fetch services ", e);
287                         return false;
288                 } finally {
289                         titanDao.commit();
290                         try {
291                                 writer.flush();
292                                 writer.close();
293                         } catch (Exception ex) {
294                                 /* ignore */}
295                 }
296                 return true;
297         }
298
299         private boolean isProblematicGroup(GroupDefinition gr, String resourceName, Map<String, ArtifactDefinition> deploymentArtifacts) {
300                 List<String> artifacts = gr.getArtifacts();
301                 List<String> artifactsUuid = gr.getArtifactsUuid();
302
303                 if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) {
304                         log.info("No groups in resource {} ", resourceName);
305                         return false;
306                 }
307                 if (artifacts.size() < artifactsUuid.size()) {
308                         log.info(" artifacts.size() < artifactsUuid.size() group {} in resource {} ", gr.getName(), resourceName);
309                         return true;
310                 }
311                 if (artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty())) {
312                         log.info(" artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() group {} in resource {} ", gr.getName(), resourceName);
313                         return true;
314                 }
315                 if (artifactsUuid.contains(null)) {
316                         log.info(" artifactsUuid.contains(null) group {} in resource {} ", gr.getName(), resourceName);
317                         return true;
318                 }
319
320                 for (String artifactId : artifacts) {
321                         String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
322                         ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactlabel);
323                         if (artifactDefinition == null) {
324                                 log.info(" artifactDefinition == null label {} group {} in resource {} ", artifactlabel, gr.getName(), resourceName);
325                                 return true;
326                         }
327                         ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactDefinition.getArtifactType());
328                         if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
329                                 if (!artifactId.equals(artifactDefinition.getUniqueId())) {
330                                         log.info(" !artifactId.equals(artifactDefinition.getUniqueId() artifact {}  artId {} group {} in resource {} ", artifactlabel, artifactId, gr.getName(), resourceName);
331                                         return true;
332                                 }
333                                 if (!artifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
334                                         log.info(" artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} group {} in resource {} ", artifactlabel, gr.getName(), resourceName);
335                                         return true;
336                                 }
337                         }
338                 }
339                 return false;
340         }
341
342         private boolean isProblematicGroupInstance(GroupInstance gi, String instName, String servicename, Map<String, ArtifactDefinition> deploymentArtifacts) {
343                 List<String> artifacts = gi.getArtifacts();
344                 List<String> artifactsUuid = gi.getArtifactsUuid();
345                 List<String> instArtifactsUuid = gi.getGroupInstanceArtifactsUuid();
346
347                 if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) {
348                         log.info("No instance groups for instance {} in service {} ", instName, servicename);
349                         return false;
350                 }
351                 if (artifacts.size() < artifactsUuid.size()) {
352                         log.info(" artifacts.size() < artifactsUuid.size() inst {} in service {} ", instName, servicename);
353                         return true;
354                 }
355                 if (artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty())) {
356                         log.info(" artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() inst {} in service {} ", instName, servicename);
357                         return true;
358                 }
359                 if (artifactsUuid.contains(null)) {
360                         log.info(" artifactsUuid.contains(null) inst {} in service {} ", instName, servicename);
361                         return true;
362                 }
363
364                 for (String artifactId : artifacts) {
365                         String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
366                         ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactlabel);
367                         if (artifactDefinition == null) {
368                                 log.info(" artifactDefinition == null label {} inst {} in service {} ", artifactlabel, instName, servicename);
369                                 return true;
370                         }
371                         ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactDefinition.getArtifactType());
372                         if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
373                                 if (!artifactId.equals(artifactDefinition.getUniqueId())) {
374                                         log.info(" !artifactId.equals(artifactDefinition.getUniqueId() artifact {}  artId {} inst {} in service {} ", artifactlabel, artifactId, instName, servicename);
375                                         return true;
376                                 }
377                                 if (!artifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
378                                         log.info(" artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ", artifactlabel, instName, servicename);
379                                         return true;
380                                 }
381                         } else {
382                                 if (!instArtifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
383                                         log.info(" instArtifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ", artifactlabel, instName, servicename);
384                                         return true;
385                                 }
386                         }
387                 }
388                 return false;
389         }
390
391         private boolean fix(List<Resource> vfLst, List<Service> serviceList) {
392                 boolean res = true;
393
394                 if (vfLst != null && !vfLst.isEmpty()) {
395                         res = fixVf(vfLst);
396                         if (res) {
397                                 for (Component component : vfLst) {
398                                         TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(component);
399                                         Map<String, GroupDataDefinition> groups = topologyTemplate.getGroups();
400                                         res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.GROUPS, EdgeLabelEnum.GROUPS, groups);
401                                 }
402                         }
403                 }
404
405                 if (res == true && serviceList != null && !serviceList.isEmpty()) {
406                         res = fixServices(serviceList);
407                         if (res) {
408                                 for (Component component : serviceList) {
409                                         TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(component);
410                                         Map<String, MapGroupsDataDefinition> groups = topologyTemplate.getInstGroups();
411                                         res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS, groups);
412                                 }
413                         }
414                 }
415
416                 return res;
417         }
418
419         private <T extends ToscaDataDefinition> boolean fixDataOnGraph(String componentId, VertexTypeEnum vertexTypeEnum, EdgeLabelEnum edgeLabelEnum, Map<String, T> groups) {
420                 boolean res = true;
421                 Either<GraphVertex, TitanOperationStatus> getResponse = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
422                 if (getResponse.isRight()) {
423                         log.debug("Couldn't fetch component  unique id {}, error: {}", componentId, getResponse.right().value());
424                         res = false;
425
426                 }
427                 if (res) {
428                         GraphVertex componentVertex = getResponse.left().value();
429
430                         GraphVertex toscaDataVertex = null;
431                         Either<GraphVertex, TitanOperationStatus> groupVertexEither = titanDao.getChildVertex(componentVertex, edgeLabelEnum, JsonParseFlagEnum.ParseJson);
432                         if (groupVertexEither.isRight()) {
433                                 res = false;
434                                 log.debug("failed to get child {}  vertex for component  unique id {}, error: {}", edgeLabelEnum, componentId, groupVertexEither.right().value());
435                         }
436                         if (res) {
437                                 toscaDataVertex = groupVertexEither.left().value();
438                                 toscaDataVertex.setJson(groups);
439                                 Either<GraphVertex, TitanOperationStatus> updatevertexEither = titanDao.updateVertex(toscaDataVertex);
440                                 if (updatevertexEither.isRight()) {
441                                         log.debug("failed to update vertex for component  unique id {}, error: {}", componentId, updatevertexEither.right().value());
442                                         titanDao.rollback();
443                                         return false;
444                                 }
445                         }
446                 }
447
448                 titanDao.commit();
449
450                 return res;
451         }
452
453         private boolean fixServices(List<Service> serviceList) {
454                 for (Service service : serviceList) {
455                         log.debug("Migration1707ArtifactUuidFix  fix service: id {},  name {} ", service.getUniqueId(), service.getName());
456                         List<ComponentInstance> instances = service.getComponentInstances();
457                         for (ComponentInstance instance : instances) {
458                                 Map<String, ArtifactDefinition> artifactsMap = instance.getDeploymentArtifacts();
459                                 List<GroupInstance> groupsList = instance.getGroupInstances();
460                                 if (groupsList != null && artifactsMap != null) {
461                                         for (GroupInstance group : groupsList) {
462                                                 if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
463                                                         log.debug("Migration1707ArtifactUuidFix  fix group:  resource id {}, group name {} ", service.getUniqueId(), group.getName());
464                                                         List<String> groupArtifacts = new ArrayList<String>(group.getArtifacts());
465
466                                                         group.getArtifacts().clear();
467                                                         group.getArtifactsUuid().clear();
468                                                         group.getGroupInstanceArtifacts().clear();
469                                                         group.getGroupInstanceArtifactsUuid().clear();
470
471                                                         for (String artifactId : groupArtifacts) {
472                                                                 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
473                                                                 log.debug("Migration1707ArtifactUuidFix  fix group:  group name {} artifactId for fix {} artifactlabel {} ", group.getName(), artifactId, artifactlabel);
474                                                                 if (!artifactlabel.isEmpty() && artifactsMap.containsKey(artifactlabel)) {
475                                                                         ArtifactDefinition artifact = artifactsMap.get(artifactlabel);
476                                                                         ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifact.getArtifactType());
477                                                                         String correctArtifactId = artifact.getUniqueId();
478                                                                         String correctArtifactUUID = artifact.getArtifactUUID();
479                                                                         if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
480
481                                                                                 log.debug("Migration1707ArtifactUuidFix  fix group:  group name {} correct artifactId {} artifactUUID {} ", group.getName(), correctArtifactId, correctArtifactUUID);
482                                                                                 group.getArtifacts().add(correctArtifactId);
483                                                                                 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
484                                                                                         group.getArtifactsUuid().add(correctArtifactUUID);
485                                                                                 }
486                                                                         } else {
487                                                                                 log.debug("Migration1707ArtifactUuidFix  fix group:  group name {} correct artifactId {} artifactUUID {} ", group.getName(), correctArtifactId, correctArtifactUUID);
488                                                                                 group.getGroupInstanceArtifacts().add(correctArtifactId);
489                                                                                 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
490                                                                                         group.getGroupInstanceArtifactsUuid().add(correctArtifactUUID);
491                                                                                 }
492                                                                         }
493                                                                 }
494                                                         }
495                                                 }
496                                         }
497                                 }
498                         }
499
500                 }
501                 return true;
502
503         }
504
505         private boolean fixVf(List<Resource> vfLst) {
506                 for (Resource resource : vfLst) {
507                         log.debug("Migration1707ArtifactUuidFix  fix resource: id {},  name {} ", resource.getUniqueId(), resource.getName());
508                         Map<String, ArtifactDefinition> artifactsMap = resource.getDeploymentArtifacts();
509                         List<GroupDefinition> groupsList = resource.getGroups();
510                         if (groupsList != null && artifactsMap != null) {
511                                 for (GroupDefinition group : groupsList) {
512                                         if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE) && group.getArtifacts() != null) {
513                                                 log.debug("Migration1707ArtifactUuidFix  fix group:  resource id {}, group name {} ", resource.getUniqueId(), group.getName());
514                                                 List<String> groupArtifacts = new ArrayList<String>(group.getArtifacts());
515                                                 group.getArtifacts().clear();
516                                                 group.getArtifactsUuid().clear();
517                                                 for (String artifactId : groupArtifacts) {
518                                                         String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
519                                                         log.debug("Migration1707ArtifactUuidFix  fix group:  group name {} artifactId for fix {} artifactlabel {} ", group.getName(), artifactId, artifactlabel);
520                                                         if (!artifactlabel.isEmpty() && artifactsMap.containsKey(artifactlabel)) {
521                                                                 ArtifactDefinition artifact = artifactsMap.get(artifactlabel);
522                                                                 String correctArtifactId = artifact.getUniqueId();
523                                                                 String correctArtifactUUID = artifact.getArtifactUUID();
524                                                                 log.debug("Migration1707ArtifactUuidFix  fix group:  group name {} correct artifactId {} artifactUUID {} ", group.getName(), correctArtifactId, correctArtifactUUID);
525                                                                 group.getArtifacts().add(correctArtifactId);
526                                                                 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
527                                                                         group.getArtifactsUuid().add(correctArtifactUUID);
528                                                                 }
529
530                                                         }
531                                                 }
532                                         }
533                                 }
534                         }
535
536                 }
537
538                 return true;
539         }
540
541         private String findArtifactLabelFromArtifactId(String artifactId) {
542                 String artifactLabel = "";
543
544                 int index = artifactId.lastIndexOf(".");
545                 if (index > 0 && index + 1 < artifactId.length())
546                         artifactLabel = artifactId.substring(index + 1);
547                 return artifactLabel;
548         }
549
550         private void writeModuleResultToFile(Writer writer, org.openecomp.sdc.be.model.Component component, Service service) {
551                 try {
552                         // "service name, service id, state, version
553                         StringBuffer sb = new StringBuffer(component.getName());
554                         sb.append(",").append(component.getUniqueId()).append(",").append(component.getLifecycleState()).append(",").append(component.getVersion());
555                         if (service != null) {
556                                 sb.append(",").append(service.getName());
557                         }
558                         sb.append("\n");
559                         writer.write(sb.toString());
560                 } catch (IOException e) {
561                         // TODO Auto-generated catch block
562                         e.printStackTrace();
563                 }
564         }
565 }