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