2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.sdc.gab.yaml;
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;
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;
36 * Yaml parser and searcher for GAB. Requires 3 steps:
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()}
42 public class GABYamlParser implements AutoCloseable {
44 private YamlParser yamlParser;
46 public GABYamlParser(YamlParser yamlParser) {
47 this.yamlParser = yamlParser;
51 * Provides yaml path for processing.
53 * @param path Yaml file path.
54 * @return Same parser with loaded source.
56 public GABYamlParser parseFile(String path) {
57 yamlParser.parseFile(path);
62 * Provides yaml content for processing.
64 * @param content Yaml file content.
65 * @return Same parser with loaded source.
67 public GABYamlParser parseContent(String content) {
68 yamlParser.parseContent(content);
73 * Adds set of filters for processing.
75 * @param filters correct json paths for searching resources.
76 * @return Same parser with loaded filters.
78 public GABYamlParser filter(Set<String> filters) {
79 yamlParser.filter(filters);
84 * Adds single filter for processing.
86 * @param filter correct json path for searching resource.
87 * @return Same parser with loaded filter.
89 public GABYamlParser filter(String filter) {
90 yamlParser.filter(filter);
95 * Collects the results from parsed yaml file and applied filters.
97 * @exception IOException Means that yaml file has invalid content.
98 * @return {@link GABResults}
100 public GABResults collect() throws IOException {
101 return new GABResults(yamlParser.collect().stream()
102 .map(results -> new GABResult(createGabResultEntryList(results)))
103 .collect(Collectors.toList()));
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());
115 public void close() throws IOException {