Introduced yaml parser as common lib
[sdc.git] / common / onap-generic-artifact-browser / onap-generic-artifact-browser-service / src / main / java / org / onap / sdc / gab / yaml / GABYamlParser.java
1 /*
2  * ============LICENSE_START=======================================================
3  * GAB
4  * ================================================================================
5  * Copyright (C) 2019 Nokia 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.onap.sdc.gab.yaml;
22
23 import java.io.IOException;
24 import java.util.AbstractMap.SimpleEntry;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Objects;
29 import java.util.Set;
30 import java.util.stream.Collectors;
31 import org.onap.sdc.gab.model.GABResult;
32 import org.onap.sdc.gab.model.GABResultEntry;
33 import org.onap.sdc.gab.model.GABResults;
34
35 /**
36  * Yaml parser and searcher for GAB. Requires 3 steps:
37  *
38  * <br>1. Load content of Yaml file using {@link #parseContent(String)} or {@link #parseFile(String)}
39  * <br>2. Provide keywords to search using {@link #filter(String)} or {@link #filter(Set)}
40  * <br>3. Collect the results using {@link #collect()}
41  */
42 public class GABYamlParser implements AutoCloseable {
43
44     private YamlParser yamlParser;
45
46     public GABYamlParser(YamlParser yamlParser) {
47         this.yamlParser = yamlParser;
48     }
49
50     /**
51      * Provides yaml path for processing.
52      *
53      * @param path Yaml file path.
54      * @return Same parser with loaded source.
55      */
56     public GABYamlParser parseFile(String path) {
57         yamlParser.parseFile(path);
58         return this;
59     }
60
61     /**
62      * Provides yaml content for processing.
63      *
64      * @param content Yaml file content.
65      * @return Same parser with loaded source.
66      */
67     public GABYamlParser parseContent(String content) {
68         yamlParser.parseContent(content);
69         return this;
70     }
71
72     /**
73      * Adds set of filters for processing.
74      *
75      * @param filters correct json paths for searching resources.
76      * @return Same parser with loaded filters.
77      */
78     public GABYamlParser filter(Set<String> filters) {
79         yamlParser.filter(filters);
80         return this;
81     }
82
83     /**
84      * Adds single filter for processing.
85      *
86      * @param filter correct json path for searching resource.
87      * @return Same parser with loaded filter.
88      */
89     public GABYamlParser filter(String filter) {
90         yamlParser.filter(filter);
91         return this;
92     }
93
94     /**
95      * Collects the results from parsed yaml file and applied filters.
96      *
97      * @exception IOException Means that yaml file has invalid content.
98      * @return {@link GABResults}
99      */
100     public GABResults collect() throws IOException {
101         return new GABResults(yamlParser.collect().stream()
102             .map(results -> new GABResult(createGabResultEntryList(results)))
103             .collect(Collectors.toList()));
104     }
105
106     private List<GABResultEntry> createGabResultEntryList(List<SimpleEntry<String, ? extends Collection<Object>>> parsedContent) {
107         return Objects.isNull(parsedContent) ? Collections.emptyList() : parsedContent.stream()
108                 .map(result -> result.getValue().stream()
109                     .map(entry -> new GABResultEntry(result.getKey(), entry))
110                     .collect(Collectors.toList())).flatMap(Collection::stream)
111                 .collect(Collectors.toList());
112     }
113
114     @Override
115     public void close() throws IOException {
116         yamlParser.close();
117     }
118 }