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