Rename packages from openecomp to onap.
[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.logging.api.Logger;
24 import org.openecomp.sdc.logging.api.LoggerFactory;
25 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
26 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
27 import org.onap.sdc.tosca.datatypes.model.RequirementAssignment;
28 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
29 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
30
31 import java.io.File;
32 import java.io.FileInputStream;
33 import java.io.FileNotFoundException;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.net.URL;
37 import java.nio.file.NotDirectoryException;
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.ListIterator;
42 import java.util.Map;
43
44 public class TestUtil {
45
46   private static final Logger logger = LoggerFactory.getLogger(TestUtil.class);
47
48   public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
49                                                         String globalServiceTemplatesPath,
50                                                         String entryDefinitionServiceTemplate)
51       throws IOException {
52     ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
53     Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
54     if (entryDefinitionServiceTemplate == null) {
55       entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
56     }
57
58     loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
59     if (globalServiceTemplatesPath != null) {
60       loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
61     }
62
63     return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
64   }
65
66   private static void loadServiceTemplates(String serviceTemplatesPath,
67                                            ToscaExtensionYamlUtil toscaExtensionYamlUtil,
68                                            Map<String, ServiceTemplate> serviceTemplates)
69       throws IOException {
70     URL urlFile = TestUtil.class.getResource(serviceTemplatesPath);
71     if (urlFile != null) {
72       File pathFile = new File(urlFile.getFile());
73       File[] files = pathFile.listFiles();
74       if (files != null) {
75         addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
76       } else {
77         throw new NotDirectoryException(serviceTemplatesPath);
78       }
79     } else {
80       throw new NotDirectoryException(serviceTemplatesPath);
81     }
82   }
83
84   private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
85                                               File[] files,
86                                               ToscaExtensionYamlUtil toscaExtensionYamlUtil)
87       throws IOException {
88     for (File file : files) {
89       try (InputStream yamlFile = new FileInputStream(file)) {
90         ServiceTemplate serviceTemplateFromYaml =
91             toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
92         serviceTemplates.put(file.getName(), serviceTemplateFromYaml);
93         try {
94           yamlFile.close();
95         } catch (IOException ignore) {
96             logger.debug(ignore.getMessage(), ignore);
97         }
98       } catch (FileNotFoundException e) {
99         throw e;
100       } catch (IOException e) {
101         throw e;
102       }
103     }
104   }
105
106   public static void createConcreteRequirementObjectsInServiceTemplate(
107       ServiceTemplate serviceTemplateFromYaml,
108       ToscaExtensionYamlUtil toscaExtensionYamlUtil){
109
110     if (serviceTemplateFromYaml == null
111         || serviceTemplateFromYaml.getTopology_template() == null
112         || serviceTemplateFromYaml.getTopology_template().getNode_templates() == null) {
113       return;
114     }
115
116     //Creating concrete objects
117     Map<String, NodeTemplate> nodeTemplates =
118         serviceTemplateFromYaml.getTopology_template().getNode_templates();
119     for (Map.Entry<String, NodeTemplate> entry : nodeTemplates.entrySet()) {
120       NodeTemplate nodeTemplate = entry.getValue();
121       List<Map<String, RequirementAssignment>> requirements = nodeTemplate.getRequirements();
122       List<Map<String, RequirementAssignment>> concreteRequirementList = new ArrayList<>();
123       if (requirements != null) {
124         ListIterator<Map<String, RequirementAssignment>> reqListIterator = requirements
125             .listIterator();
126         while (reqListIterator.hasNext()){
127           Map<String, RequirementAssignment> requirement = reqListIterator.next();
128           Map<String, RequirementAssignment> concreteRequirement = new HashMap<>();
129           for (Map.Entry<String, RequirementAssignment> reqEntry : requirement.entrySet()) {
130             RequirementAssignment requirementAssignment = (toscaExtensionYamlUtil
131                 .yamlToObject(toscaExtensionYamlUtil.objectToYaml(reqEntry.getValue()),
132                     RequirementAssignment.class));
133             concreteRequirement.put(reqEntry.getKey(), requirementAssignment);
134             concreteRequirementList.add(concreteRequirement);
135             reqListIterator.remove();
136           }
137         }
138         requirements.clear();
139         requirements.addAll(concreteRequirementList);
140         nodeTemplate.setRequirements(requirements);
141       }
142     }
143   }
144 }