2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Copyright (c) 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.aai.babel.xml.generator.data;
24 import com.google.common.base.Enums;
25 import java.io.IOException;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.Optional;
31 import java.util.Properties;
32 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
33 import org.onap.aai.babel.xml.generator.model.Resource;
34 import org.onap.aai.babel.xml.generator.model.WidgetType;
35 import org.onap.aai.babel.xml.generator.model.Widget;
36 import org.onap.aai.babel.xml.generator.types.ModelType;
38 public class WidgetConfigurationUtil {
40 private static Properties config;
41 private static List<String> instanceGroups = Collections.emptyList();
42 private static Map<String, Resource> typeToResource = new HashMap<>();
43 private static Map<String, Widget> typeToWidget = new HashMap<>();
46 * Private constructor to prevent instantiation.
48 private WidgetConfigurationUtil() {
49 throw new UnsupportedOperationException("This static class should not be instantiated!");
52 public static Properties getConfig() {
56 public static void setConfig(Properties config) {
57 WidgetConfigurationUtil.config = config;
60 public static void setSupportedInstanceGroups(List<String> supportedInstanceGroups) {
61 instanceGroups = supportedInstanceGroups;
64 public static boolean isSupportedInstanceGroup(String groupType) {
65 return instanceGroups.contains(groupType);
68 public static Optional<Resource> createModelFromType(String typePrefix) {
69 return Optional.ofNullable(typeToResource.get(typePrefix));
72 public static Widget createWidgetFromType(String widgetType) throws XmlArtifactGenerationException {
73 Optional<Widget> widget = Optional.ofNullable(typeToWidget.get(widgetType));
74 if (widget.isPresent()) {
75 // Make a copy of the Widget found in the mappings table.
76 return new Widget(widget.get());
81 public static void setWidgetTypes(List<WidgetTypeConfig> types) {
82 for (WidgetTypeConfig type : types) {
83 if (type.type == null || type.name == null) {
84 throw new IllegalArgumentException("Incomplete widget type specified: " + type);
86 WidgetType widgetType = new WidgetType(type.type);
87 Widget widget = new Widget(widgetType, type.name, type.deleteFlag);
88 typeToWidget.put(type.type, widget);
90 WidgetType.validateElements();
93 public static void setWidgetMappings(List<WidgetMapping> mappings) throws IOException {
94 for (WidgetMapping mapping : mappings) {
95 ModelType modelType = Optional.ofNullable(mapping.type).map(String::toUpperCase)
96 .map(s -> Enums.getIfPresent(ModelType.class, s).orNull()).orElse(null);
97 if (mapping.prefix == null || mapping.widget == null || modelType == null) {
98 throw new IOException("Invalid widget mapping specified: " + mapping);
100 Resource resource = new Resource(WidgetType.valueOf(mapping.widget), mapping.deleteFlag);
101 resource.setModelType(modelType);
102 typeToResource.put(mapping.prefix, resource);