2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T 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.tosca.services;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.Objects;
28 import java.util.Optional;
31 import org.apache.commons.collections4.CollectionUtils;
32 import org.onap.sdc.tosca.datatypes.model.Import;
33 import org.onap.sdc.tosca.error.ToscaRuntimeException;
35 public class DataModelConvertUtil {
37 private static final String INVALID_TOSCA_IMPORT_SECTION = "Invalid TOSCA import section";
39 private DataModelConvertUtil() {
40 //Hiding implicit default constructor
43 public static List<Map<String, Import>> convertToscaImports(List importObj) {
44 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
45 List<Map<String, Import>> convertedImport = new ArrayList<>();
46 if (CollectionUtils.isEmpty(importObj)) {
49 for (Object importEntry : importObj) {
50 convertToscaImportEntry(convertedImport, importEntry, toscaExtensionYamlUtil);
52 return convertedImport;
55 private static void convertToscaImportEntry(List<Map<String, Import>> convertedImport, Object importEntry,
56 ToscaExtensionYamlUtil toscaExtensionYamlUtil) {
57 if (importEntry instanceof String) {
58 //Support for import short notation
64 convertImportShortNotation(convertedImport, importEntry.toString());
65 } else if (importEntry instanceof Map) {
66 handleImportMultiLineGrammar(convertedImport, importEntry, toscaExtensionYamlUtil);
70 private static void handleImportMultiLineGrammar(List<Map<String, Import>> convertedImport, Object importEntry,
71 ToscaExtensionYamlUtil toscaExtensionYamlUtil) {
73 if (((Map) importEntry).containsKey("file")) {
74 //Support for import entry of the format - file: <file_uri> or - file: <import object>
75 Import importObject = toscaExtensionYamlUtil
76 .yamlToObject(toscaExtensionYamlUtil.objectToYaml(importEntry), Import.class);
77 convertImportExtendedNotation(convertedImport, importObject);
79 convertImportMultiLineGrammar(convertedImport, (Map) importEntry, toscaExtensionYamlUtil);
81 } catch (Exception ex) {
82 throw new ToscaRuntimeException(INVALID_TOSCA_IMPORT_SECTION, ex);
86 private static void convertImportMultiLineGrammar(List<Map<String, Import>> convertedImport, Map importEntry,
87 ToscaExtensionYamlUtil toscaExtensionYamlUtil) {
88 Set<Map.Entry<String, Object>> importEntries = importEntry.entrySet();
89 for (Map.Entry<String, Object> toscaImport : importEntries) {
90 String key = toscaImport.getKey();
91 Object importValue = toscaImport.getValue();
92 if (importValue instanceof Map) {
93 /* Support for import entry of the format multi line extended import notation
94 - another_definition_file:
96 repository: service_repo
97 namespace_uri: http://test.xyz/uri
98 namespace_prefix: pref
100 Import importObject = toscaExtensionYamlUtil
101 .yamlToObject(toscaExtensionYamlUtil.objectToYaml(importValue), Import.class);
102 Map<String, Import> convertedToscaImport = new HashMap<>();
103 convertedToscaImport.put(key, importObject);
104 convertedImport.add(convertedToscaImport);
106 //Support for import entry of the format - some_definition_file: path1/path2/fileName.yaml
107 convertedImport.add((Map<String, Import>) importEntry);
112 private static void convertImportExtendedNotation(List<Map<String, Import>> convertedImport, Import importEntry) {
113 Map<String, Import> importMap = new HashMap<>();
114 Optional<String> fileNameWithoutExtension =
115 getFileNameWithoutExtension(getFileName(importEntry.getFile()).replaceAll("/", "_"));
116 if (fileNameWithoutExtension.isPresent()) {
117 importMap.put(fileNameWithoutExtension.get(), importEntry);
118 convertedImport.add(importMap);
122 private static void convertImportShortNotation(List<Map<String, Import>> convertImport, String fileFullName) {
123 Import importObject = new Import();
124 importObject.setFile(fileFullName);
125 Map<String, Import> importMap = new HashMap<>();
126 Optional<String> fileNameWithoutExtension = getFileNameWithoutExtension(getFileName(fileFullName));
127 if (fileNameWithoutExtension.isPresent()) {
128 importMap.put(fileNameWithoutExtension.get().replaceAll("/", "_"), importObject);
129 convertImport.add(importMap);
133 private static Optional<String> getFileNameWithoutExtension(String fileName) {
134 if (Objects.isNull(fileName)) {
135 return Optional.empty();
137 return !fileName.contains(".") ? Optional.of(fileName)
138 : Optional.of(fileName.substring(0, fileName.lastIndexOf('.')));
142 private static String getFileName(String relativeFileName) {
143 if (relativeFileName.contains("../")) {
144 return relativeFileName.replace("../", "");
146 return relativeFileName;