34696b33eb3bdb865288e07f4c05259bfc41f582
[sdc.git] /
1 package org.openecomp.sdc.asdctool.impl.validator.executers;
2
3 import fj.data.Either;
4 import org.openecomp.sdc.asdctool.impl.validator.config.ValidationConfigManager;
5 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
6 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
7 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
8 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
9 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
10 import org.openecomp.sdc.be.model.ArtifactDefinition;
11 import org.openecomp.sdc.be.model.Component;
12 import org.openecomp.sdc.be.model.ComponentParametersView;
13 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
14 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
15 import org.openecomp.sdc.common.log.wrappers.Logger;
16 import org.springframework.beans.factory.annotation.Autowired;
17
18 import java.io.*;
19 import java.util.*;
20 import java.util.stream.Collectors;
21
22 public class ArtifactValidatorExecuter{
23         
24          @Autowired
25          protected TitanDao titanDao;
26
27          @Autowired
28          private ToscaOperationFacade toscaOperationFacade;
29          private static Logger log = Logger.getLogger(ArtifactValidatorExecuter.class.getName());
30          
31          protected String name;
32
33             public void setName(String name) {
34                 this.name = name;
35             }
36
37             public String getName() {
38                 return name;
39             }
40
41          
42         
43            public Map<String, List<Component>> getVerticesToValidate(VertexTypeEnum type, Map<GraphPropertyEnum, Object> hasProps){
44                    Map<String, List<Component>> result = new HashMap<>();
45                 Either<List<GraphVertex>, TitanOperationStatus> resultsEither = titanDao.getByCriteria(type, hasProps);
46                 if (resultsEither.isRight()) {
47                         log.error("getVerticesToValidate failed "+ resultsEither.right().value());
48                     return result;
49                 }
50                 System.out.println("getVerticesToValidate: "+resultsEither.left().value().size()+" vertices to scan");
51                 List<GraphVertex> componentsList = resultsEither.left().value();
52                 componentsList.forEach(vertex -> {
53                         String ivariantUuid = (String)vertex.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID);
54                         if(!result.containsKey(ivariantUuid)){
55                                 List<Component> compList = new ArrayList<Component>();
56                                 result.put(ivariantUuid, compList);
57                         }
58                         List<Component> compList = result.get(ivariantUuid);
59                         
60                         ComponentParametersView filter = new ComponentParametersView(true);                             
61                                 filter.setIgnoreArtifacts(false);
62                                 
63                                 Either<Component, StorageOperationStatus> toscaElement = toscaOperationFacade.getToscaElement(vertex.getUniqueId(), filter);
64                                 if (toscaElement.isRight()) {
65                                         log.error("getVerticesToValidate: failed to find element"+ vertex.getUniqueId()+" staus is" + toscaElement.right().value());
66                                 }else{
67                                         compList.add(toscaElement.left().value());
68                                 }
69                          
70                 });             
71               
72                         return result;
73             }
74             
75            public boolean validate( Map<String, List<Component>> vertices) {
76                    boolean result = true;
77                    long time = System.currentTimeMillis();
78                    String fileName = ValidationConfigManager.getOutputFilePath() + this.getName() + "_"+ time + ".csv";
79                    try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"))) {
80                         writer.write("name, UUID, invariantUUID, state, version\n");
81                         Collection<List<Component>> collection = vertices.values();
82                         for(List<Component> compList: collection ){
83                                 Set<String> artifactEsId = new HashSet<>();
84                                 for(Component component: compList ){
85                                         Map<String, ArtifactDefinition> toscaArtifacts = component.getToscaArtifacts();
86                                         Optional<ArtifactDefinition> op = toscaArtifacts.values().
87                                                         stream().filter(a -> artifactEsId.contains(a.getEsId())).findAny();
88                                         if(op.isPresent()){
89                                                 result = false;
90                                                 writeModuleResultToFile(writer, compList);
91                                                 writer.flush();
92                                                 break;
93                                         }else{
94                                                 artifactEsId.addAll(toscaArtifacts.values().stream().map(ArtifactDefinition::getEsId).collect(Collectors.toList()))     ;
95                                         }
96                                 }
97                                 
98                         }
99                         
100                    } catch (Exception e) {
101                                 log.error("Failed to fetch vf resources ", e);
102                                 return false;
103                         } finally {
104                                 titanDao.commit();
105                         }
106                         return result;
107             }
108            
109            private void writeModuleResultToFile(Writer writer, List<Component> components) {
110                         try {
111                                 // "service name, service id, state, version
112                                 for(Component component: components ){
113                                         StringBuffer sb = new StringBuffer(component.getName());
114                                         sb.append(",").append(component.getUniqueId()).append(",").append(component.getInvariantUUID()).append(",").append(component.getLifecycleState()).append(",").append(component.getVersion());
115                                         
116                                         sb.append("\n");
117                                         writer.write(sb.toString());
118                                 }
119                         } catch (IOException e) {
120                                 log.error("Failed to write module result to file ", e);
121                         }
122                 }
123
124 }