Added oparent to sdc main
[sdc.git] / common / onap-tosca-datatype / src / main / java / org / onap / sdc / tosca / services / DataModelConvertUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.sdc.tosca.services;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Objects;
28 import java.util.Optional;
29 import java.util.Set;
30
31 import org.apache.commons.collections4.CollectionUtils;
32 import org.onap.sdc.tosca.datatypes.model.Import;
33 import org.onap.sdc.tosca.error.ToscaRuntimeException;
34
35 public class DataModelConvertUtil {
36
37     private static final String INVALID_TOSCA_IMPORT_SECTION = "Invalid TOSCA import section";
38
39     private DataModelConvertUtil() {
40         //Hiding implicit default constructor
41     }
42
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)) {
47             return null;
48         }
49         for (Object importEntry : importObj) {
50             convertToscaImportEntry(convertedImport, importEntry, toscaExtensionYamlUtil);
51         }
52         return convertedImport;
53     }
54
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
59             /*
60             imports:
61               - <file_URI_1>
62               - <file_URI_2>
63              */
64             convertImportShortNotation(convertedImport, importEntry.toString());
65         } else if (importEntry instanceof Map) {
66             handleImportMultiLineGrammar(convertedImport, importEntry, toscaExtensionYamlUtil);
67         }
68     }
69
70     private static void handleImportMultiLineGrammar(List<Map<String, Import>> convertedImport, Object importEntry,
71                                               ToscaExtensionYamlUtil toscaExtensionYamlUtil) {
72         try {
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);
78             } else {
79                 convertImportMultiLineGrammar(convertedImport, (Map) importEntry, toscaExtensionYamlUtil);
80             }
81         } catch (Exception ex) {
82             throw new ToscaRuntimeException(INVALID_TOSCA_IMPORT_SECTION, ex);
83         }
84     }
85
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:
95                           file: path1/file.yaml
96                           repository: service_repo
97                           namespace_uri: http://test.xyz/uri
98                           namespace_prefix: pref
99                  */
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);
105             } else {
106                 //Support for import entry of the format - some_definition_file: path1/path2/fileName.yaml
107                 convertedImport.add((Map<String, Import>) importEntry);
108             }
109         }
110     }
111
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);
119         }
120     }
121
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);
130         }
131     }
132
133     private static Optional<String> getFileNameWithoutExtension(String fileName) {
134         if (Objects.isNull(fileName)) {
135             return Optional.empty();
136         }
137         return !fileName.contains(".") ? Optional.of(fileName)
138                 : Optional.of(fileName.substring(0, fileName.lastIndexOf('.')));
139     }
140
141
142     private static String getFileName(String relativeFileName) {
143         if (relativeFileName.contains("../")) {
144             return relativeFileName.replace("../", "");
145         } else {
146             return relativeFileName;
147         }
148     }
149 }