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