diff options
Diffstat (limited to 'csarvalidation/src/main')
4 files changed, 129 insertions, 3 deletions
diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocument.java b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocument.java index 5ed4690..7f80d1c 100644 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocument.java +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocument.java @@ -23,13 +23,35 @@ public class YamlDocument { private final Map<String, ?> yaml; - public YamlDocument(Map<String, ?> yaml) { + YamlDocument(Map<String, ?> yaml) { this.yaml = yaml; } public Map<String, ?> getYaml() { return yaml; } + + public boolean containsKey(String key) { + return yaml.containsKey(key); + } + + public String getValue(String key) { + return yaml.get(key).toString(); + } + + public YamlParametersList getListOfValues(String key) + throws YamlParameterListFactory.YamlParameterListParsingException { + return new YamlParameterListFactory().createYamlParameterList( + yaml.get(key) + ); + } + + public YamlDocument getSubStructure(String name) + throws YamlDocumentFactory.YamlDocumentParsingException { + return new YamlDocumentFactory().createYamlDocument( + yaml.get(name) + ); + } } diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocumentFactory.java b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocumentFactory.java index 8e571a9..1cda34b 100644 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocumentFactory.java +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocumentFactory.java @@ -17,21 +17,30 @@ package org.onap.validation.yaml.model; +import java.util.HashMap; import java.util.Map; public class YamlDocumentFactory { public YamlDocument createYamlDocument(Object yaml) throws YamlDocumentParsingException { try { - Map<String, ?> parsedYaml = (Map<String, ?>) yaml; + Map<String, Object> parsedYaml = transformMap((Map) yaml); return new YamlDocument(parsedYaml); } catch (ClassCastException e) { throw new YamlDocumentParsingException( - String.format("Fail to parse given objects: %s as yaml",yaml), e + String.format("Fail to parse given objects: %s as yaml document.", yaml), e ); } } + private Map<String, Object> transformMap(Map<?, ?> yaml) { + Map<String, Object> parsedYaml = new HashMap<>(); + for (Map.Entry entry: yaml.entrySet()) { + parsedYaml.put(entry.getKey().toString(), entry.getValue()); + } + return parsedYaml; + } + public static class YamlDocumentParsingException extends Exception { YamlDocumentParsingException(String message, Throwable throwable) { diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParameterListFactory.java b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParameterListFactory.java new file mode 100644 index 0000000..b85ec7c --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParameterListFactory.java @@ -0,0 +1,61 @@ +/* + * Copyright 2020 Nokia + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.onap.validation.yaml.model; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class YamlParameterListFactory { + + public YamlParametersList createEmptyYamlParameterList() { + return new YamlParametersList(Collections.emptyList()); + } + + public YamlParametersList createYamlParameterList(Object yaml) + throws YamlParameterListParsingException { + try { + return parseYamlToListOfPossibleValues(yaml); + } catch (ClassCastException e) { + throw new YamlParameterListParsingException( + String.format("Fail to parse given objects: %s as list.",yaml), e + ); + } + } + + private YamlParametersList parseYamlToListOfPossibleValues(Object yaml) { + List<String> parametersList = new ArrayList<>(); + if( yaml instanceof List) { + for (Object element : (List) yaml) { + parametersList.add(element.toString()); + } + } else { + parametersList.add(yaml.toString()); + } + return new YamlParametersList(parametersList); + } + + + public static class YamlParameterListParsingException extends Exception { + + YamlParameterListParsingException(String message, Throwable throwable) { + super(message, throwable); + } + } + +} diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParametersList.java b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParametersList.java new file mode 100644 index 0000000..2b93c74 --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParametersList.java @@ -0,0 +1,34 @@ +/* + * Copyright 2020 Nokia + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.onap.validation.yaml.model; + +import java.util.List; + +public class YamlParametersList { + + private final List<String> parameters; + + YamlParametersList(List<String> parameters) { + this.parameters = parameters; + } + + public List<String> getParameters() { + return parameters; + } + +} |