Allow UUID definitions in the mappings JSON
[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     public static final String GENERATOR_AAI_CONFIGLPROP_NOT_FOUND =
41             "Cannot generate artifacts. Widget configuration not found for %s";
42
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<>();
47
48     /*
49      * Private constructor to prevent instantiation.
50      */
51     private WidgetConfigurationUtil() {
52         throw new UnsupportedOperationException("This static class should not be instantiated!");
53     }
54
55     public static void setConfig(Properties config) {
56         WidgetConfigurationUtil.config = config;
57     }
58
59     public static void setSupportedInstanceGroups(List<String> supportedInstanceGroups) {
60         instanceGroups = supportedInstanceGroups;
61     }
62
63     public static boolean isSupportedInstanceGroup(String groupType) {
64         return instanceGroups.contains(groupType);
65     }
66
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()));
72         }
73         return resource;
74     }
75
76     /**
77      * Create a new Widget object according to the supplied Widget Type.
78      *
79      * @param widgetType
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
84      */
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());
90         }
91         return null;
92     }
93
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);
99             }
100             if (type.modelInvariantId == null) {
101                 type.modelInvariantId = WidgetConfigurationUtil.getModelInvariantId(type.name);
102             }
103             if (type.modelVersionId == null) {
104                 type.modelVersionId = WidgetConfigurationUtil.getModelVersionId(type.name);
105             }
106             Widget widget = new Widget(new WidgetType(type.type), type.name, type.deleteFlag, //
107                     type.modelInvariantId, type.modelVersionId);
108             typeToWidget.put(type.type, widget);
109         }
110         WidgetType.validateElements();
111     }
112
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);
119             }
120             Resource resource = new Resource(WidgetType.valueOf(mapping.widget), mapping.deleteFlag);
121             resource.setModelType(modelType);
122             typeToResource.put(mapping.prefix, resource);
123         }
124     }
125
126     public static String getModelInvariantId(String name) {
127         String id = config.getProperty(ArtifactType.AAI.name() + ".model-invariant-id." + name);
128         if (id == null) {
129             throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
130                     ArtifactType.AAI.name() + ".model-invariant-id." + name));
131         }
132         return id;
133     }
134
135     public static String getModelVersionId(String name) {
136         String id = config.getProperty(ArtifactType.AAI.name() + ".model-version-id." + name);
137         if (id == null) {
138             throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
139                     ArtifactType.AAI.name() + ".model-version-id." + name));
140         }
141         return id;
142     }
143 }