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 public static final String GENERATOR_AAI_CONFIGLPROP_NOT_FOUND =
41 "Cannot generate artifacts. Widget configuration not found for %s";
43 private static Properties config;
44 private static List<String> instanceGroups = Collections.emptyList();
45 private static Map<String, Resource> typeToResource = new HashMap<>();
46 private static Map<String, Widget> typeToWidget = new HashMap<>();
49 * Private constructor to prevent instantiation.
51 private WidgetConfigurationUtil() {
52 throw new UnsupportedOperationException("This static class should not be instantiated!");
55 public static void setConfig(Properties config) {
56 WidgetConfigurationUtil.config = config;
59 public static void setSupportedInstanceGroups(List<String> supportedInstanceGroups) {
60 instanceGroups = supportedInstanceGroups;
63 public static boolean isSupportedInstanceGroup(String groupType) {
64 return instanceGroups.contains(groupType);
67 public static Optional<Resource> createModelFromType(String typePrefix) {
68 Optional<Resource> resource = Optional.ofNullable(typeToResource.get(typePrefix));
69 if (resource.isPresent()) {
70 // Make a copy of the Resource found in the mappings table.
71 return Optional.of(new Resource(resource.get()));
77 * Create a new Widget object according to the supplied Widget Type.
80 * a String identifying the type of Widget to create
81 * @return a new Widget object from the defined widget type, or else null
82 * @throws XmlArtifactGenerationException
83 * if there is an internal error creating the Widget because of the defined widget mappings
85 public static Widget createWidgetFromType(String widgetType) throws XmlArtifactGenerationException {
86 Optional<Widget> widget = Optional.ofNullable(typeToWidget.get(widgetType));
87 if (widget.isPresent()) {
88 // Make a copy of the Widget found in the mappings table.
89 return new Widget(widget.get());
94 public static void setWidgetTypes(List<WidgetTypeConfig> types) {
95 WidgetType.clearElements();
96 for (WidgetTypeConfig type : types) {
97 if (type.type == null || type.name == null) {
98 throw new IllegalArgumentException("Incomplete widget type specified: " + type);
100 if (type.modelInvariantId == null) {
101 type.modelInvariantId = WidgetConfigurationUtil.getModelInvariantId(type.name);
103 if (type.modelVersionId == null) {
104 type.modelVersionId = WidgetConfigurationUtil.getModelVersionId(type.name);
106 Widget widget = new Widget(new WidgetType(type.type), type.name, type.deleteFlag, //
107 type.modelInvariantId, type.modelVersionId);
108 typeToWidget.put(type.type, widget);
110 WidgetType.validateElements();
113 public static void setWidgetMappings(List<WidgetMapping> mappings) throws IOException {
114 for (WidgetMapping mapping : mappings) {
115 ModelType modelType = Optional.ofNullable(mapping.type).map(String::toUpperCase)
116 .map(s -> Enums.getIfPresent(ModelType.class, s).orNull()).orElse(null);
117 if (mapping.prefix == null || mapping.widget == null || modelType == null) {
118 throw new IOException("Invalid widget mapping specified: " + mapping);
120 Resource resource = new Resource(WidgetType.valueOf(mapping.widget), mapping.deleteFlag);
121 resource.setModelType(modelType);
122 typeToResource.put(mapping.prefix, resource);
126 public static String getModelInvariantId(String name) {
127 String id = config.getProperty(ArtifactType.AAI.name() + ".model-invariant-id." + name);
129 throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
130 ArtifactType.AAI.name() + ".model-invariant-id." + name));
135 public static String getModelVersionId(String name) {
136 String id = config.getProperty(ArtifactType.AAI.name() + ".model-version-id." + name);
138 throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
139 ArtifactType.AAI.name() + ".model-version-id." + name));