9007d8ce05ea200c5680fb16f40f77706e9af8e6
[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.BufferedWriter;
38 import java.io.FileOutputStream;
39 import java.io.IOException;
40 import java.io.OutputStreamWriter;
41 import java.io.Writer;
42 import java.util.ArrayList;
43 import java.util.Collection;
44 import java.util.HashMap;
45 import java.util.HashSet;
46 import java.util.List;
47 import java.util.Map;
48 import java.util.Optional;
49 import java.util.Set;
50 import java.util.stream.Collectors;
51
52 public class ArtifactValidatorExecuter {
53
54     protected JanusGraphDao janusGraphDao;
55     protected ToscaOperationFacade toscaOperationFacade;
56
57     public ArtifactValidatorExecuter(JanusGraphDao janusGraphDao,
58         ToscaOperationFacade toscaOperationFacade) {
59         this.janusGraphDao = janusGraphDao;
60         this.toscaOperationFacade = toscaOperationFacade;
61     }
62
63     private static Logger log = Logger.getLogger(ArtifactValidatorExecuter.class.getName());
64
65     protected String name;
66
67     public void setName(String name) {
68         this.name = name;
69     }
70
71     public String getName() {
72         return name;
73     }
74
75
76     public Map<String, List<Component>> getVerticesToValidate(VertexTypeEnum type,
77         Map<GraphPropertyEnum, Object> hasProps) {
78         Map<String, List<Component>> result = new HashMap<>();
79         Either<List<GraphVertex>, JanusGraphOperationStatus> resultsEither = janusGraphDao
80             .getByCriteria(type, hasProps);
81         if (resultsEither.isRight()) {
82             log.error("getVerticesToValidate failed " + resultsEither.right().value());
83             return result;
84         }
85         System.out.println("getVerticesToValidate: " + resultsEither.left().value().size() + " vertices to scan");
86         List<GraphVertex> componentsList = resultsEither.left().value();
87         componentsList.forEach(vertex -> {
88             String ivariantUuid = (String) vertex.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID);
89             if (!result.containsKey(ivariantUuid)) {
90                 List<Component> compList = new ArrayList<Component>();
91                 result.put(ivariantUuid, compList);
92             }
93             List<Component> compList = result.get(ivariantUuid);
94
95             ComponentParametersView filter = new ComponentParametersView(true);
96             filter.setIgnoreArtifacts(false);
97
98             Either<Component, StorageOperationStatus> toscaElement = toscaOperationFacade
99                 .getToscaElement(vertex.getUniqueId(), filter);
100             if (toscaElement.isRight()) {
101                 log.error(
102                     "getVerticesToValidate: failed to find element" + vertex.getUniqueId() + " staus is" + toscaElement
103                         .right().value());
104             } else {
105                 compList.add(toscaElement.left().value());
106             }
107
108         });
109
110         return result;
111     }
112
113     public boolean validate(Map<String, List<Component>> vertices, String outputFilePath) {
114         boolean result = true;
115         long time = System.currentTimeMillis();
116         String fileName = outputFilePath + this.getName() + "_" + time + ".csv";
117         try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"))) {
118             writer.write("name, UUID, invariantUUID, state, version\n");
119             Collection<List<Component>> collection = vertices.values();
120             for (List<Component> compList : collection) {
121                 Set<String> artifactEsId = new HashSet<>();
122                 for (Component component : compList) {
123                     Map<String, ArtifactDefinition> toscaArtifacts = component.getToscaArtifacts();
124                     Optional<ArtifactDefinition> op = toscaArtifacts.values().
125                         stream().filter(a -> artifactEsId.contains(a.getEsId())).findAny();
126                     if (op.isPresent()) {
127                         result = false;
128                         writeModuleResultToFile(writer, compList);
129                         writer.flush();
130                         break;
131                     } else {
132                         artifactEsId.addAll(toscaArtifacts.values().stream().map(ArtifactDefinition::getEsId)
133                             .collect(Collectors.toList()));
134                     }
135                 }
136
137             }
138
139         } catch (Exception e) {
140             log.error("Failed to fetch vf resources ", e);
141             return false;
142         } finally {
143             janusGraphDao.commit();
144         }
145         return result;
146     }
147
148     private void writeModuleResultToFile(Writer writer, List<Component> components) {
149         try {
150             // "service name, service id, state, version
151             for (Component component : components) {
152                 StringBuffer sb = new StringBuffer(component.getName());
153                 sb.append(",").append(component.getUniqueId()).append(",").append(component.getInvariantUUID())
154                     .append(",").append(component.getLifecycleState()).append(",").append(component.getVersion());
155
156                 sb.append("\n");
157                 writer.write(sb.toString());
158             }
159         } catch (IOException e) {
160             log.error("Failed to write module result to file ", e);
161         }
162     }
163
164 }