Re-implement model type value for Resource Mapping
[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.Widget.Type;
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         return Optional.ofNullable(typeToResource.get(typePrefix));
70     }
71
72     public static Widget createWidgetFromType(Type type) throws XmlArtifactGenerationException {
73         Optional<Widget> widget = Optional.ofNullable(typeToWidget.get(type.toString()));
74         if (widget.isPresent()) {
75             // Make a copy of the Widget found in the mappings table.
76             return new Widget(widget.get());
77         }
78         return null;
79     }
80
81     public static void setWidgetTypes(List<WidgetType> types) {
82         for (WidgetType type : types) {
83             if (type.type == null || type.name == null) {
84                 throw new IllegalArgumentException("Incomplete widget type specified: " + type);
85             }
86             Type widgetType = Widget.Type.valueOf(type.type);
87             Widget widget = new Widget(widgetType, type.name, type.deleteFlag);
88             typeToWidget.put(type.type, widget);
89         }
90     }
91
92     public static void setWidgetMappings(List<WidgetMapping> mappings) throws IOException {
93         for (WidgetMapping mapping : mappings) {
94             ModelType modelType = Optional.ofNullable(mapping.type).map(String::toUpperCase)
95                     .map(s -> Enums.getIfPresent(ModelType.class, s).orNull()).orElse(null);
96             if (mapping.prefix == null || mapping.widget == null || modelType == null) {
97                 throw new IOException("Invalid widget mapping specified: " + mapping);
98             }
99             Resource resource = new Resource(Widget.Type.valueOf(mapping.widget), mapping.deleteFlag);
100             resource.setModelType(modelType);
101             typeToResource.put(mapping.prefix, resource);
102         }
103     }
104 }