[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-be / lib / openecomp-tosca-lib / src / test / java / org / openecomp / sdc / tosca / TestUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.sdc.tosca;
22
23 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
24 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
25 import org.openecomp.sdc.tosca.services.ToscaExtensionYamlUtil;
26
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.FileNotFoundException;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.net.URL;
33 import java.nio.file.NotDirectoryException;
34 import java.util.HashMap;
35 import java.util.Map;
36
37 public class TestUtil {
38
39   public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
40                                                         String globalServiceTemplatesPath,
41                                                         String entryDefinitionServiceTemplate)
42       throws IOException {
43     ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
44     Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
45     if (entryDefinitionServiceTemplate == null) {
46       entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
47     }
48
49     loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
50     if (globalServiceTemplatesPath != null) {
51       loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
52     }
53
54     return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
55   }
56
57   private static void loadServiceTemplates(String serviceTemplatesPath,
58                                            ToscaExtensionYamlUtil toscaExtensionYamlUtil,
59                                            Map<String, ServiceTemplate> serviceTemplates)
60       throws IOException {
61     URL urlFile = TestUtil.class.getResource(serviceTemplatesPath);
62     if (urlFile != null) {
63       File pathFile = new File(urlFile.getFile());
64       File[] files = pathFile.listFiles();
65       if (files != null) {
66         addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
67       } else {
68         throw new NotDirectoryException(serviceTemplatesPath);
69       }
70     } else {
71       throw new NotDirectoryException(serviceTemplatesPath);
72     }
73   }
74
75   private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
76                                               File[] files,
77                                               ToscaExtensionYamlUtil toscaExtensionYamlUtil)
78       throws IOException {
79     for (File file : files) {
80       try (InputStream yamlFile = new FileInputStream(file)) {
81         ServiceTemplate serviceTemplateFromYaml =
82             toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
83         serviceTemplates.put(file.getName(), serviceTemplateFromYaml);
84         try {
85           yamlFile.close();
86         } catch (IOException ignore) {
87         }
88       } catch (FileNotFoundException e) {
89         throw e;
90       } catch (IOException e) {
91         throw e;
92       }
93     }
94   }
95 }