2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.core.converter.impl.pnfd.parser;
22 import static org.openecomp.core.converter.pnfd.model.PnfTransformationToken.CONVERSIONS;
23 import static org.openecomp.core.converter.pnfd.model.PnfTransformationToken.DESCRIPTION;
24 import static org.openecomp.core.converter.pnfd.model.PnfTransformationToken.NAME;
25 import static org.openecomp.core.converter.pnfd.model.PnfTransformationToken.QUERY;
26 import static org.openecomp.core.converter.pnfd.model.PnfTransformationToken.TRANSFORMATION_FOR;
28 import java.util.Collections;
29 import java.util.List;
31 import java.util.stream.Collectors;
32 import org.apache.commons.collections.CollectionUtils;
33 import org.openecomp.core.converter.pnfd.model.ConversionDefinition;
34 import org.openecomp.core.converter.pnfd.model.Transformation;
35 import org.openecomp.core.converter.pnfd.model.TransformationBlock;
38 * Handles YAML from/to {@link Transformation} conversions
40 public class TransformationYamlParser {
42 private TransformationYamlParser() {
47 * Parses the given YAML object to a {@link Transformation} instance.
48 * @param transformationYaml the YAML object representing a transformation
50 * A new instance of {@link Transformation}.
52 public static Transformation parse(final Map<String, Object> transformationYaml) {
53 final Transformation transformation = new Transformation();
54 transformation.setName((String) transformationYaml.get(NAME.getName()));
55 transformation.setDescription((String) transformationYaml.get(DESCRIPTION.getName()));
57 final String block = (String) transformationYaml.get(TRANSFORMATION_FOR.getName());
58 transformation.setBlock(TransformationBlock.parse(block).orElse(null));
60 transformation.setConversionQuery(
61 ConversionQueryYamlParser.parse(transformationYaml.get(QUERY.getName()))
63 transformation.setConversionDefinitionList(parseConversions(transformationYaml));
65 return transformation;
68 private static List<ConversionDefinition> parseConversions(final Map<String, Object> conversionYaml) {
69 final List<Object> conversionList = (List<Object>) conversionYaml.get(CONVERSIONS.getName());
71 if (CollectionUtils.isEmpty(conversionList)) {
72 return Collections.emptyList();
75 return conversionList.stream()
76 .map(conversion -> ConversionDefinitionYamlParser.parse((Map<String, Object>) conversion))
77 .collect(Collectors.toList());