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.Widget;
35 import org.onap.aai.babel.xml.generator.model.WidgetType;
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 Optional<Resource> resource = Optional.ofNullable(typeToResource.get(typePrefix));
70 if (resource.isPresent()) {
71 // Make a copy of the Resource found in the mappings table.
72 return Optional.of(new Resource(resource.get()));
77 public static Widget createWidgetFromType(String widgetType) throws XmlArtifactGenerationException {
78 Optional<Widget> widget = Optional.ofNullable(typeToWidget.get(widgetType));
79 if (widget.isPresent()) {
80 // Make a copy of the Widget found in the mappings table.
81 return new Widget(widget.get());
86 public static void setWidgetTypes(List<WidgetTypeConfig> types) {
87 WidgetType.clearElements();
88 for (WidgetTypeConfig type : types) {
89 if (type.type == null || type.name == null) {
90 throw new IllegalArgumentException("Incomplete widget type specified: " + type);
92 WidgetType widgetType = new WidgetType(type.type);
93 Widget widget = new Widget(widgetType, type.name, type.deleteFlag);
94 typeToWidget.put(type.type, widget);
96 WidgetType.validateElements();
99 public static void setWidgetMappings(List<WidgetMapping> mappings) throws IOException {
100 for (WidgetMapping mapping : mappings) {
101 ModelType modelType = Optional.ofNullable(mapping.type).map(String::toUpperCase)
102 .map(s -> Enums.getIfPresent(ModelType.class, s).orNull()).orElse(null);
103 if (mapping.prefix == null || mapping.widget == null || modelType == null) {
104 throw new IOException("Invalid widget mapping specified: " + mapping);
106 Resource resource = new Resource(WidgetType.valueOf(mapping.widget), mapping.deleteFlag);
107 resource.setModelType(modelType);
108 typeToResource.put(mapping.prefix, resource);