aboutsummaryrefslogtreecommitdiffstats
path: root/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ArtifactValidatorExecuter.java
blob: 34696b33eb3bdb865288e07f4c05259bfc41f582 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package org.openecomp.sdc.asdctool.impl.validator.executers;

import fj.data.Either;
import org.openecomp.sdc.asdctool.impl.validator.config.ValidationConfigManager;
import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
import org.openecomp.sdc.be.model.ArtifactDefinition;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.ComponentParametersView;
import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class ArtifactValidatorExecuter{
	
	 @Autowired
	 protected TitanDao titanDao;

	 @Autowired
	 private ToscaOperationFacade toscaOperationFacade;
	 private static Logger log = Logger.getLogger(ArtifactValidatorExecuter.class.getName());
	 
	 protected String name;

	    public void setName(String name) {
	        this.name = name;
	    }

	    public String getName() {
	        return name;
	    }

	 
	
	   public Map<String, List<Component>> getVerticesToValidate(VertexTypeEnum type, Map<GraphPropertyEnum, Object> hasProps){
		   Map<String, List<Component>> result = new HashMap<>();
	        Either<List<GraphVertex>, TitanOperationStatus> resultsEither = titanDao.getByCriteria(type, hasProps);
	        if (resultsEither.isRight()) {
	        	log.error("getVerticesToValidate failed "+ resultsEither.right().value());
	            return result;
	        }
	        System.out.println("getVerticesToValidate: "+resultsEither.left().value().size()+" vertices to scan");
	        List<GraphVertex> componentsList = resultsEither.left().value();
	        componentsList.forEach(vertex -> {
	        	String ivariantUuid = (String)vertex.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID);
	        	if(!result.containsKey(ivariantUuid)){
	        		List<Component> compList = new ArrayList<Component>();
	        		result.put(ivariantUuid, compList);
	        	}
	        	List<Component> compList = result.get(ivariantUuid);
	        	
	        	ComponentParametersView filter = new ComponentParametersView(true);				
				filter.setIgnoreArtifacts(false);
				
				Either<Component, StorageOperationStatus> toscaElement = toscaOperationFacade.getToscaElement(vertex.getUniqueId(), filter);
				if (toscaElement.isRight()) {
					log.error("getVerticesToValidate: failed to find element"+ vertex.getUniqueId()+" staus is" + toscaElement.right().value());
				}else{
					compList.add(toscaElement.left().value());
				}
	        	 
	        });	        
	      
			return result;
	    }
	    
	   public boolean validate( Map<String, List<Component>> vertices) {
		   boolean result = true;
		   long time = System.currentTimeMillis();
		   String fileName = ValidationConfigManager.getOutputFilePath() + this.getName() + "_"+ time + ".csv";
		   try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"))) {
			writer.write("name, UUID, invariantUUID, state, version\n");
			Collection<List<Component>> collection = vertices.values();
			for(List<Component> compList: collection ){
				Set<String> artifactEsId = new HashSet<>();
				for(Component component: compList ){
					Map<String, ArtifactDefinition> toscaArtifacts = component.getToscaArtifacts();
					Optional<ArtifactDefinition> op = toscaArtifacts.values().
							stream().filter(a -> artifactEsId.contains(a.getEsId())).findAny();
					if(op.isPresent()){
						result = false;
						writeModuleResultToFile(writer, compList);
						writer.flush();
						break;
					}else{
						artifactEsId.addAll(toscaArtifacts.values().stream().map(ArtifactDefinition::getEsId).collect(Collectors.toList()))	;
					}
				}
				
			}
			
		   } catch (Exception e) {
				log.error("Failed to fetch vf resources ", e);
				return false;
			} finally {
				titanDao.commit();
			}
			return result;
	    }
	   
	   private void writeModuleResultToFile(Writer writer, List<Component> components) {
			try {
				// "service name, service id, state, version
				for(Component component: components ){
					StringBuffer sb = new StringBuffer(component.getName());
					sb.append(",").append(component.getUniqueId()).append(",").append(component.getInvariantUUID()).append(",").append(component.getLifecycleState()).append(",").append(component.getVersion());
					
					sb.append("\n");
					writer.write(sb.toString());
				}
			} catch (IOException e) {
				log.error("Failed to write module result to file ", e);
			}
		}

}