30b6c8ee3721c7dc4f8ea69b8cc35ec37200aec5
[aai/babel.git] / src / main / java / org / onap / aai / babel / xml / generator / data / WidgetConfigurationUtil.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2019 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.babel.xml.generator.data;
22
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.Properties;
29 import org.onap.aai.babel.xml.generator.model.Model;
30
31 public class WidgetConfigurationUtil {
32
33     private static Properties config;
34     private static List<String> instanceGroups = Collections.emptyList();
35     private static Map<String, Class<? extends Model>> typeToModel = new HashMap<>();
36
37     /*
38      * Private constructor to prevent instantiation
39      */
40     private WidgetConfigurationUtil() {
41         throw new UnsupportedOperationException("This static class should not be instantiated!");
42     }
43
44     public static Properties getConfig() {
45         return config;
46     }
47
48     public static void setConfig(Properties config) {
49         WidgetConfigurationUtil.config = config;
50     }
51
52     public static void setSupportedInstanceGroups(List<String> supportedInstanceGroups) {
53         instanceGroups = supportedInstanceGroups;
54     }    
55
56     public static boolean isSupportedInstanceGroup(String groupType) {
57         return instanceGroups.contains(groupType);
58     }
59
60     /**
61      * Create the mappings from TOSCA type to Widget type. The Properties store a set of TOSCA type prefix Strings.
62      * These keys take a single class name (String), which is used to map to a Widget Class in the Model.
63      * 
64      * @param map
65      *            the key/value pairs of TOSCA type and Class name
66      */
67     @SuppressWarnings("unchecked")
68     public static void setTypeMappings(Map<String, String> map) {
69         for (Entry<String, String> entry : map.entrySet()) {
70             final String toscaType = entry.getKey();
71             final String javaBean = entry.getValue();
72             final String modelClassName = Model.class.getPackage().getName() + "." + javaBean;
73             try {
74                 typeToModel.put(toscaType, (Class<? extends Model>) Class.forName(modelClassName));
75             } catch (ClassNotFoundException e) {
76                 throw new IllegalArgumentException(
77                         String.format("Unsupported type \"%s\" for TOSCA mapping %s: no class found for %s", //
78                                 javaBean, toscaType, modelClassName));
79             }
80         }
81     }
82
83     public static Class<? extends Model> getModelFromType(String typePrefix) {
84         return typeToModel.get(typePrefix);
85     }
86 }