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