diff options
author | Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> | 2020-07-01 14:10:59 +0200 |
---|---|---|
committer | Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> | 2020-07-01 14:10:59 +0200 |
commit | 3d6d5ed1c133e19c4091bd9ef98b5cc8e83eed51 (patch) | |
tree | a4c8ea87ccf32f8d134b1e19b5ac0a358ab271e4 /csarvalidation/src/main/java | |
parent | 8fd3dcfc8252a986300c35d1c58f34ab4d210ea4 (diff) |
Add wrappers for yaml generic objects
Issue-ID: VNFSDK-594
Signed-off-by: Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com>
Change-Id: Ic6e2f291a0f8d5a34b75208f7cff68c4262f6cbf
Diffstat (limited to 'csarvalidation/src/main/java')
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; + } + +} |