Remove all references to artifactgenerator config
[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 (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
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 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;
29 import java.util.Map;
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;
36
37 public class WidgetConfigurationUtil {
38
39     public static final String GENERATOR_AAI_CONFIGLPROP_NOT_FOUND =
40             "Cannot generate artifacts. Widget configuration not found for %s";
41
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<>();
45
46     /*
47      * Private constructor to prevent instantiation.
48      */
49     private WidgetConfigurationUtil() {
50         throw new UnsupportedOperationException("This static class should not be instantiated!");
51     }
52
53     public static void setSupportedInstanceGroups(List<String> supportedInstanceGroups) {
54         instanceGroups = supportedInstanceGroups;
55     }
56
57     public static boolean isSupportedInstanceGroup(String groupType) {
58         return instanceGroups.contains(groupType);
59     }
60
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()));
66         }
67         return resource;
68     }
69
70     /**
71      * Create a new Widget object according to the supplied Widget Type.
72      *
73      * @param widgetType
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
78      */
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());
84         }
85         return null;
86     }
87
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);
94             }
95             Widget widget = new Widget(new WidgetType(type.type), type.name, type.deleteFlag, //
96                     type.modelInvariantId, type.modelVersionId);
97             typeToWidget.put(type.type, widget);
98         }
99         WidgetType.validateElements();
100     }
101
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);
108             }
109             Resource resource = new Resource(WidgetType.valueOf(mapping.widget), mapping.deleteFlag);
110             resource.setModelType(modelType);
111             typeToResource.put(mapping.prefix, resource);
112         }
113     }
114
115 }