Create new Resource objects for each new model
[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 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;
37
38 public class WidgetConfigurationUtil {
39
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<>();
44
45     /*
46      * Private constructor to prevent instantiation.
47      */
48     private WidgetConfigurationUtil() {
49         throw new UnsupportedOperationException("This static class should not be instantiated!");
50     }
51
52     public static Properties getConfig() {
53         return config;
54     }
55
56     public static void setConfig(Properties config) {
57         WidgetConfigurationUtil.config = config;
58     }
59
60     public static void setSupportedInstanceGroups(List<String> supportedInstanceGroups) {
61         instanceGroups = supportedInstanceGroups;
62     }
63
64     public static boolean isSupportedInstanceGroup(String groupType) {
65         return instanceGroups.contains(groupType);
66     }
67
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()));
73         }
74         return resource;
75     }
76
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());
82         }
83         return null;
84     }
85
86     public static void setWidgetTypes(List<WidgetTypeConfig> types) {
87         for (WidgetTypeConfig type : types) {
88             if (type.type == null || type.name == null) {
89                 throw new IllegalArgumentException("Incomplete widget type specified: " + type);
90             }
91             WidgetType widgetType = new WidgetType(type.type);
92             Widget widget = new Widget(widgetType, type.name, type.deleteFlag);
93             typeToWidget.put(type.type, widget);
94         }
95         WidgetType.validateElements();
96     }
97
98     public static void setWidgetMappings(List<WidgetMapping> mappings) throws IOException  {
99         for (WidgetMapping mapping : mappings) {
100             ModelType modelType = Optional.ofNullable(mapping.type).map(String::toUpperCase)
101                     .map(s -> Enums.getIfPresent(ModelType.class, s).orNull()).orElse(null);
102             if (mapping.prefix == null || mapping.widget == null || modelType == null) {
103                 throw new IOException("Invalid widget mapping specified: " + mapping);
104             }
105             Resource resource = new Resource(WidgetType.valueOf(mapping.widget), mapping.deleteFlag);
106             resource.setModelType(modelType);
107             typeToResource.put(mapping.prefix, resource);
108         }
109     }
110 }