[sdc] update to the current code base
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / impl / migration / v1707 / Migration1707ArtifactUuidFix.java
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                                         break;
151                                 }
152                         }
153                         titanDao.commit();
154
155                 } catch (Exception e) {
156                         log.info("Failed to fetch vf resources ", e);
157                         return false;
158                 } finally {
159                         titanDao.commit();
160                         try {
161                                 writer.flush();
162                                 writer.close();
163                         } catch (Exception ex) {
164                                 /* ignore */}
165                 }
166                 return true;
167         }
168
169         private boolean fetchVf(List<Service> serviceList, List<Resource> vfLst, long time) {
170                 log.info("Find problem VF ");
171                 if (serviceList.isEmpty()) {
172                         log.info("No services as input");
173                         return true;
174                 }
175                 Writer writer = null;
176                 try {
177                         String fileName = "problemVf_" + time + ".csv";
178                         writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"));
179                         writer.write("vf name, vf id, state, version, example service name\n");
180                         Set<String> vfIds = new HashSet<>();
181                         for (Service service : serviceList) {
182                                 List<ComponentInstance> componentInstances = service.getComponentInstances();
183                                 for (ComponentInstance ci : componentInstances) {
184                                         if (!vfIds.contains(ci.getComponentUid())) {
185                                                 vfIds.add(ci.getComponentUid());
186                                                 Either<Resource, StorageOperationStatus> toscaElement = toscaOperationFacade.getToscaElement(ci.getComponentUid());
187                                                 if (toscaElement.isRight()) {
188                                                         log.info("Failed to fetch resource {} {}", ci.getComponentUid(), toscaElement.right().value());
189                                                         return false;
190                                                 }
191                                                 Resource resource = toscaElement.left().value();
192                                                 vfLst.add(resource);
193                                                 writeModuleResultToFile(writer, resource, service);
194                                                 writer.flush();
195                                                 titanDao.commit();
196                                         }
197                                 }
198                         }
199                         log.info("output file with list of Vf : {}", fileName);
200                 } catch (Exception e) {
201                         log.info("Failed to fetch services ", e);
202                         return false;
203                 } finally {
204                         titanDao.commit();
205                         try {
206                                 writer.flush();
207                                 writer.close();
208                         } catch (Exception ex) {
209                                 /* ignore */}
210                 }
211                 return true;
212         }
213
214         private boolean fetchServices(String fixServices, List<Service> serviceList, long time) {
215                 log.info("Find problem Services {}", fixServices);
216                 Writer writer = null;
217
218                 try {
219                         String fileName = "problemService_" + time + ".csv";
220                         writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"));
221                         writer.write("service name, service id, state, version\n");
222
223                         Map<GraphPropertyEnum, Object> hasProps = new HashMap<>();
224                         hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
225                         if (fixServices.equals("distributed_only")) {
226                                 hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
227                                 hasProps.put(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTED.name());
228                         }
229
230                         Map<GraphPropertyEnum, Object> hasNotProps = new HashMap<>();
231                         hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
232                         log.info("Try to fetch services with properties {} and not {}", hasProps, hasNotProps);
233
234                         Either<List<GraphVertex>, TitanOperationStatus> servicesByCriteria = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll);
235                         if (servicesByCriteria.isRight()) {
236                                 log.info("Failed to fetch services {}", servicesByCriteria.right().value());
237                                 return false;
238                         }
239                         List<GraphVertex> services = servicesByCriteria.left().value();
240                         for (GraphVertex gv : services) {
241                                 ComponentParametersView filter = new ComponentParametersView(true);
242                                 filter.setIgnoreComponentInstances(false);
243                                 filter.setIgnoreArtifacts(false);
244                                 filter.setIgnoreGroups(false);
245
246                                 Either<Service, StorageOperationStatus> toscaElement = toscaOperationFacade.getToscaElement(gv.getUniqueId());
247                                 if (toscaElement.isRight()) {
248                                         log.info("Failed to fetch service {} {}", gv.getUniqueId(), toscaElement.right().value());
249                                         return false;
250                                 }
251                                 Service service = toscaElement.left().value();
252                                 List<ComponentInstance> componentInstances = service.getComponentInstances();
253                                 boolean isProblematic = false;
254                                 if (componentInstances == null) {
255                                         log.info("No instances for service {} ", gv.getUniqueId());
256                                         continue;
257                                 }
258                                 String serviceName = (String) gv.getMetadataProperty(GraphPropertyEnum.NAME);
259
260                                 for (ComponentInstance ci : componentInstances) {
261                                         Map<String, ArtifactDefinition> deploymentArtifacts = ci.getDeploymentArtifacts();
262                                         List<GroupInstance> groupInstances = ci.getGroupInstances();
263                                         if (groupInstances == null || groupInstances.isEmpty()) {
264                                                 log.info("No instance groups for instance {} in service {} id {} ", ci.getName(), serviceName, gv.getUniqueId());
265                                                 continue;
266                                         }
267
268                                         for (GroupInstance gi : groupInstances) {
269                                                 if (gi.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
270                                                         if (isProblematicGroupInstance(gi, ci.getName(), serviceName, deploymentArtifacts)) {
271                                                                 isProblematic = true;
272                                                                 break;
273                                                         }
274                                                 }
275                                         }
276                                         if (isProblematic) {
277                                                 serviceList.add(service);
278                                                 writeModuleResultToFile(writer, service, null);
279                                                 writer.flush();
280                                                 break;
281                                         }
282                                 }
283                                 titanDao.commit();
284                         }
285                         log.info("output file with list of services : {}", fileName);
286                 } catch (Exception e) {
287                         log.info("Failed to fetch services ", e);
288                         return false;
289                 } finally {
290                         titanDao.commit();
291                         try {
292                                 writer.flush();
293                                 writer.close();
294                         } catch (Exception ex) {
295                                 /* ignore */}
296                 }
297                 return true;
298         }
299
300         private boolean isProblematicGroup(GroupDefinition gr, String resourceName, Map<String, ArtifactDefinition> deploymentArtifacts) {
301                 List<String> artifacts = gr.getArtifacts();
302                 List<String> artifactsUuid = gr.getArtifactsUuid();
303
304                 if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) {
305                         log.info("No groups in resource {} ", resourceName);
306                         return false;
307                 }
308                 if (artifacts.size() < artifactsUuid.size()) {
309                         log.info(" artifacts.size() < artifactsUuid.size() group {} in resource {} ", gr.getName(), resourceName);
310                         return true;
311                 }
312                 if (artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty())) {
313                         log.info(" artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() group {} in resource {} ", gr.getName(), resourceName);
314                         return true;
315                 }
316                 if (artifactsUuid.contains(null)) {
317                         log.info(" artifactsUuid.contains(null) group {} in resource {} ", gr.getName(), resourceName);
318                         return true;
319                 }
320
321                 for (String artifactId : artifacts) {
322                         String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
323                         ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactlabel);
324                         if (artifactDefinition == null) {
325                                 log.info(" artifactDefinition == null label {} group {} in resource {} ", artifactlabel, gr.getName(), resourceName);
326                                 return true;
327                         }
328                         ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactDefinition.getArtifactType());
329                         if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
330                                 if (!artifactId.equals(artifactDefinition.getUniqueId())) {
331                                         log.info(" !artifactId.equals(artifactDefinition.getUniqueId() artifact {}  artId {} group {} in resource {} ", artifactlabel, artifactId, gr.getName(), resourceName);
332                                         return true;
333                                 }
334                                 if (!artifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
335                                         log.info(" artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} group {} in resource {} ", artifactlabel, gr.getName(), resourceName);
336                                         return true;
337                                 }
338                         }
339                 }
340                 return false;
341         }
342
343         private boolean isProblematicGroupInstance(GroupInstance gi, String instName, String servicename, Map<String, ArtifactDefinition> deploymentArtifacts) {
344                 List<String> artifacts = gi.getArtifacts();
345                 List<String> artifactsUuid = gi.getArtifactsUuid();
346                 List<String> instArtifactsUuid = gi.getGroupInstanceArtifactsUuid();
347
348                 if ((artifactsUuid == null || artifactsUuid.isEmpty()) && (artifacts == null || artifacts.isEmpty())) {
349                         log.info("No instance groups for instance {} in service {} ", instName, servicename);
350                         return false;
351                 }
352                 if (artifacts.size() < artifactsUuid.size()) {
353                         log.info(" artifacts.size() < artifactsUuid.size() inst {} in service {} ", instName, servicename);
354                         return true;
355                 }
356                 if (artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty())) {
357                         log.info(" artifacts.size() > 0 && (artifactsUuid == null || artifactsUuid.isEmpty() inst {} in service {} ", instName, servicename);
358                         return true;
359                 }
360                 if (artifactsUuid.contains(null)) {
361                         log.info(" artifactsUuid.contains(null) inst {} in service {} ", instName, servicename);
362                         return true;
363                 }
364
365                 for (String artifactId : artifacts) {
366                         String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
367                         ArtifactDefinition artifactDefinition = deploymentArtifacts.get(artifactlabel);
368                         if (artifactDefinition == null) {
369                                 log.info(" artifactDefinition == null label {} inst {} in service {} ", artifactlabel, instName, servicename);
370                                 return true;
371                         }
372                         ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifactDefinition.getArtifactType());
373                         if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
374                                 if (!artifactId.equals(artifactDefinition.getUniqueId())) {
375                                         log.info(" !artifactId.equals(artifactDefinition.getUniqueId() artifact {}  artId {} inst {} in service {} ", artifactlabel, artifactId, instName, servicename);
376                                         return true;
377                                 }
378                                 if (!artifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
379                                         log.info(" artifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ", artifactlabel, instName, servicename);
380                                         return true;
381                                 }
382                         } else {
383                                 if (!instArtifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
384                                         log.info(" instArtifactsUuid.contains(artifactDefinition.getArtifactUUID() label {} inst {} in service {} ", artifactlabel, instName, servicename);
385                                         return true;
386                                 }
387                         }
388                 }
389                 return false;
390         }
391
392         private boolean fix(List<Resource> vfLst, List<Service> serviceList) {
393                 boolean res = true;
394
395                 if (vfLst != null && !vfLst.isEmpty()) {
396                         res = fixVf(vfLst);
397                         if (res) {
398                                 for (Component component : vfLst) {
399                                         TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(component);
400                                         Map<String, GroupDataDefinition> groups = topologyTemplate.getGroups();
401                                         res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.GROUPS, EdgeLabelEnum.GROUPS, groups);
402                                 }
403                         }
404                 }
405
406                 if (res == true && serviceList != null && !serviceList.isEmpty()) {
407                         res = fixServices(serviceList);
408                         if (res) {
409                                 for (Component component : serviceList) {
410                                         TopologyTemplate topologyTemplate = ModelConverter.convertToToscaElement(component);
411                                         Map<String, MapGroupsDataDefinition> groups = topologyTemplate.getInstGroups();
412                                         res = fixDataOnGraph(component.getUniqueId(), VertexTypeEnum.INST_GROUPS, EdgeLabelEnum.INST_GROUPS, groups);
413                                 }
414                         }
415                 }
416
417                 return res;
418         }
419
420         private <T extends ToscaDataDefinition> boolean fixDataOnGraph(String componentId, VertexTypeEnum vertexTypeEnum, EdgeLabelEnum edgeLabelEnum, Map<String, T> groups) {
421                 boolean res = true;
422                 Either<GraphVertex, TitanOperationStatus> getResponse = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse);
423                 if (getResponse.isRight()) {
424                         log.debug("Couldn't fetch component  unique id {}, error: {}", componentId, getResponse.right().value());
425                         res = false;
426
427                 }
428                 if (res) {
429                         GraphVertex componentVertex = getResponse.left().value();
430
431                         GraphVertex toscaDataVertex = null;
432                         Either<GraphVertex, TitanOperationStatus> groupVertexEither = titanDao.getChildVertex(componentVertex, edgeLabelEnum, JsonParseFlagEnum.ParseJson);
433                         if (groupVertexEither.isRight()) {
434                                 res = false;
435                                 log.debug("failed to get child {}  vertex for component  unique id {}, error: {}", edgeLabelEnum, componentId, groupVertexEither.right().value());
436                         }
437                         if (res) {
438                                 toscaDataVertex = groupVertexEither.left().value();
439                                 toscaDataVertex.setJson(groups);
440                                 Either<GraphVertex, TitanOperationStatus> updatevertexEither = titanDao.updateVertex(toscaDataVertex);
441                                 if (updatevertexEither.isRight()) {
442                                         log.debug("failed to update vertex for component  unique id {}, error: {}", componentId, updatevertexEither.right().value());
443                                         titanDao.rollback();
444                                         return false;
445                                 }
446                         }
447                 }
448
449                 titanDao.commit();
450
451                 return res;
452         }
453
454         private boolean fixServices(List<Service> serviceList) {
455                 for (Service service : serviceList) {
456                         log.debug("Migration1707ArtifactUuidFix  fix service: id {},  name {} ", service.getUniqueId(), service.getName());
457                         List<ComponentInstance> instances = service.getComponentInstances();
458                         for (ComponentInstance instance : instances) {
459                                 Map<String, ArtifactDefinition> artifactsMap = instance.getDeploymentArtifacts();
460                                 List<GroupInstance> groupsList = instance.getGroupInstances();
461                                 if (groupsList != null && artifactsMap != null) {
462                                         for (GroupInstance group : groupsList) {
463                                                 if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
464                                                         log.debug("Migration1707ArtifactUuidFix  fix group:  resource id {}, group name {} ", service.getUniqueId(), group.getName());
465                                                         List<String> groupArtifacts = new ArrayList<String>(group.getArtifacts());
466
467                                                         group.getArtifacts().clear();
468                                                         group.getArtifactsUuid().clear();
469                                                         group.getGroupInstanceArtifacts().clear();
470                                                         group.getGroupInstanceArtifactsUuid().clear();
471
472                                                         for (String artifactId : groupArtifacts) {
473                                                                 String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
474                                                                 log.debug("Migration1707ArtifactUuidFix  fix group:  group name {} artifactId for fix {} artifactlabel {} ", group.getName(), artifactId, artifactlabel);
475                                                                 if (!artifactlabel.isEmpty() && artifactsMap.containsKey(artifactlabel)) {
476                                                                         ArtifactDefinition artifact = artifactsMap.get(artifactlabel);
477                                                                         ArtifactTypeEnum artifactType = ArtifactTypeEnum.findType(artifact.getArtifactType());
478                                                                         String correctArtifactId = artifact.getUniqueId();
479                                                                         String correctArtifactUUID = artifact.getArtifactUUID();
480                                                                         if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
481
482                                                                                 log.debug("Migration1707ArtifactUuidFix  fix group:  group name {} correct artifactId {} artifactUUID {} ", group.getName(), correctArtifactId, correctArtifactUUID);
483                                                                                 group.getArtifacts().add(correctArtifactId);
484                                                                                 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
485                                                                                         group.getArtifactsUuid().add(correctArtifactUUID);
486                                                                                 }
487                                                                         } else {
488                                                                                 log.debug("Migration1707ArtifactUuidFix  fix group:  group name {} correct artifactId {} artifactUUID {} ", group.getName(), correctArtifactId, correctArtifactUUID);
489                                                                                 group.getGroupInstanceArtifacts().add(correctArtifactId);
490                                                                                 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
491                                                                                         group.getGroupInstanceArtifactsUuid().add(correctArtifactUUID);
492                                                                                 }
493                                                                         }
494                                                                 }
495                                                         }
496                                                 }
497                                         }
498                                 }
499                         }
500
501                 }
502                 return true;
503
504         }
505
506         private boolean fixVf(List<Resource> vfLst) {
507                 for (Resource resource : vfLst) {
508                         log.debug("Migration1707ArtifactUuidFix  fix resource: id {},  name {} ", resource.getUniqueId(), resource.getName());
509                         Map<String, ArtifactDefinition> artifactsMap = resource.getDeploymentArtifacts();
510                         List<GroupDefinition> groupsList = resource.getGroups();
511                         if (groupsList != null && artifactsMap != null) {
512                                 for (GroupDefinition group : groupsList) {
513                                         if (group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
514                                                 log.debug("Migration1707ArtifactUuidFix  fix group:  resource id {}, group name {} ", resource.getUniqueId(), group.getName());
515                                                 List<String> groupArtifacts = new ArrayList<String>(group.getArtifacts());
516                                                 group.getArtifacts().clear();
517                                                 group.getArtifactsUuid().clear();
518                                                 for (String artifactId : groupArtifacts) {
519                                                         String artifactlabel = findArtifactLabelFromArtifactId(artifactId);
520                                                         log.debug("Migration1707ArtifactUuidFix  fix group:  group name {} artifactId for fix {} artifactlabel {} ", group.getName(), artifactId, artifactlabel);
521                                                         if (!artifactlabel.isEmpty() && artifactsMap.containsKey(artifactlabel)) {
522                                                                 ArtifactDefinition artifact = artifactsMap.get(artifactlabel);
523                                                                 String correctArtifactId = artifact.getUniqueId();
524                                                                 String correctArtifactUUID = artifact.getArtifactUUID();
525                                                                 log.debug("Migration1707ArtifactUuidFix  fix group:  group name {} correct artifactId {} artifactUUID {} ", group.getName(), correctArtifactId, correctArtifactUUID);
526                                                                 group.getArtifacts().add(correctArtifactId);
527                                                                 if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) {
528                                                                         group.getArtifactsUuid().add(correctArtifactUUID);
529                                                                 }
530
531                                                         }
532                                                 }
533                                         }
534                                 }
535                         }
536
537                 }
538
539                 return true;
540         }
541
542         private String findArtifactLabelFromArtifactId(String artifactId) {
543                 String artifactLabel = "";
544
545                 int index = artifactId.lastIndexOf(".");
546                 if (index > 0 && index + 1 < artifactId.length())
547                         artifactLabel = artifactId.substring(index + 1);
548                 return artifactLabel;
549         }
550
551         private void writeModuleResultToFile(Writer writer, org.openecomp.sdc.be.model.Component component, Service service) {
552                 try {
553                         // "service name, service id, state, version
554                         StringBuffer sb = new StringBuffer(component.getName());
555                         sb.append(",").append(component.getUniqueId()).append(",").append(component.getLifecycleState()).append(",").append(component.getVersion());
556                         if (service != null) {
557                                 sb.append(",").append(service.getName());
558                         }
559                         sb.append("\n");
560                         writer.write(sb.toString());
561                 } catch (IOException e) {
562                         // TODO Auto-generated catch block
563                         e.printStackTrace();
564                 }
565         }
566 }