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 org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
32 import org.onap.aai.babel.xml.generator.model.Resource;
33 import org.onap.aai.babel.xml.generator.model.Widget;
34 import org.onap.aai.babel.xml.generator.model.WidgetType;
35 import org.onap.aai.babel.xml.generator.types.ModelType;
37 public class WidgetConfigurationUtil {
39 public static final String GENERATOR_AAI_CONFIGLPROP_NOT_FOUND =
40 "Cannot generate artifacts. Widget configuration not found for %s";
42 private static List<String> instanceGroups = Collections.emptyList();
43 private static Map<String, Resource> typeToResource = new HashMap<>();
44 private static Map<String, Widget> typeToWidget = new HashMap<>();
47 * Private constructor to prevent instantiation.
49 private WidgetConfigurationUtil() {
50 throw new UnsupportedOperationException("This static class should not be instantiated!");
53 public static void setSupportedInstanceGroups(List<String> supportedInstanceGroups) {
54 instanceGroups = supportedInstanceGroups;
57 public static boolean isSupportedInstanceGroup(String groupType) {
58 return instanceGroups.contains(groupType);
61 public static Optional<Resource> createModelFromType(String typePrefix) {
62 Optional<Resource> resource = Optional.ofNullable(typeToResource.get(typePrefix));
63 if (resource.isPresent()) {
64 // Make a copy of the Resource found in the mappings table.
65 return Optional.of(new Resource(resource.get()));
71 * Create a new Widget object according to the supplied Widget Type.
74 * a String identifying the type of Widget to create
75 * @return a new Widget object from the defined widget type, or else null
76 * @throws XmlArtifactGenerationException
77 * if there is an internal error creating the Widget because of the defined widget mappings
79 public static Widget createWidgetFromType(String widgetType) throws XmlArtifactGenerationException {
80 Optional<Widget> widget = Optional.ofNullable(typeToWidget.get(widgetType));
81 if (widget.isPresent()) {
82 // Make a copy of the Widget found in the mappings table.
83 return new Widget(widget.get());
88 public static void setWidgetTypes(List<WidgetTypeConfig> types) {
89 WidgetType.clearElements();
90 for (WidgetTypeConfig type : types) {
91 if (type.type == null || type.name == null || type.modelInvariantId == null
92 || type.modelVersionId == null) {
93 throw new IllegalArgumentException("Incomplete widget type specified: " + type);
95 Widget widget = new Widget(new WidgetType(type.type), type.name, type.deleteFlag, //
96 type.modelInvariantId, type.modelVersionId);
97 typeToWidget.put(type.type, widget);
99 WidgetType.validateElements();
102 public static void setWidgetMappings(List<WidgetMapping> mappings) throws IOException {
103 for (WidgetMapping mapping : mappings) {
104 ModelType modelType = Optional.ofNullable(mapping.type).map(String::toUpperCase)
105 .map(s -> Enums.getIfPresent(ModelType.class, s).orNull()).orElse(null);
106 if (mapping.prefix == null || mapping.widget == null || modelType == null) {
107 throw new IOException("Invalid widget mapping specified: " + mapping);
109 Resource resource = new Resource(WidgetType.valueOf(mapping.widget), mapping.deleteFlag);
110 resource.setModelType(modelType);
111 typeToResource.put(mapping.prefix, resource);