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