7b3d4f29f4570ad4c99af443d5ed5b9daa69a1dd
[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
22 package org.onap.aai.babel.xml.generator.data;
23
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Optional;
29 import java.util.Properties;
30 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
31 import org.onap.aai.babel.xml.generator.model.Resource;
32 import org.onap.aai.babel.xml.generator.model.Widget;
33 import org.onap.aai.babel.xml.generator.model.Widget.Type;
34
35 public class WidgetConfigurationUtil {
36
37     private static Properties config;
38     private static List<String> instanceGroups = Collections.emptyList();
39     private static Map<String, Resource> typeToResource = new HashMap<>();
40     private static Map<String, Widget> typeToWidget = new HashMap<>();
41
42     /*
43      * Private constructor to prevent instantiation.
44      */
45     private WidgetConfigurationUtil() {
46         throw new UnsupportedOperationException("This static class should not be instantiated!");
47     }
48
49     public static Properties getConfig() {
50         return config;
51     }
52
53     public static void setConfig(Properties config) {
54         WidgetConfigurationUtil.config = config;
55     }
56
57     public static void setSupportedInstanceGroups(List<String> supportedInstanceGroups) {
58         instanceGroups = supportedInstanceGroups;
59     }
60
61     public static boolean isSupportedInstanceGroup(String groupType) {
62         return instanceGroups.contains(groupType);
63     }
64
65     public static Optional<Resource> createModelFromType(String typePrefix) {
66         return Optional.ofNullable(typeToResource.get(typePrefix));
67     }
68
69     public static Widget createWidgetFromType(Type type) throws XmlArtifactGenerationException {
70         Optional<Widget> widget = Optional.ofNullable(typeToWidget.get(type.toString()));
71         if (widget.isPresent()) {
72             // Make a copy of the Widget found in the mappings table.
73             return new Widget(widget.get());
74         }
75         return null;
76     }
77
78     public static void setWidgetTypes(List<WidgetType> types) {
79         for (WidgetType type : types) {
80             if (type.type == null || type.name == null) {
81                 throw new IllegalArgumentException("Incomplete widget type specified: " + type);
82             }
83             Type widgetType = Widget.Type.valueOf(type.type);
84             Widget widget = new Widget(widgetType, type.name, type.deleteFlag);
85             typeToWidget.put(type.type, widget);
86         }
87     }
88
89     public static void setWidgetMappings(List<WidgetMapping> mappings) {
90         for (WidgetMapping mapping : mappings) {
91             if (mapping.prefix == null || mapping.widget == null) {
92                 throw new IllegalArgumentException("Incomplete widget mapping specified: " + mapping);
93             }
94             Resource resource = new Resource(Widget.Type.valueOf(mapping.widget), mapping.deleteFlag);
95             resource.setIsResource(mapping.type.equalsIgnoreCase("resource"));
96             typeToResource.put(mapping.prefix, resource);
97         }
98     }
99 }