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 * Copyright (C) 2019-2020 Wipro Limited.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.aai.babel.xml.generator.model;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.Iterator;
30 import java.util.Optional;
32 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
33 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
35 public abstract class Model {
37 public static final String GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION = "Operation Not Supported for Widgets";
39 private enum ModelIdentification {
40 ID("vfModuleModelInvariantUUID", "serviceInvariantUUID", "resourceInvariantUUID", "invariantUUID",
41 "providing_service_invariant_uuid") {
43 public void populate(Model model, String value) {
44 model.modelId = value;
47 NAME_VERSION_ID("vfModuleModelUUID", "resourceUUID", "serviceUUID", "UUID", "providing_service_uuid") {
49 public void populate(Model model, String value) {
50 model.modelNameVersionId = value;
53 VERSION("vfModuleModelVersion", "serviceVersion", "resourceversion", "version") {
55 public void populate(Model model, String value) {
56 model.modelVersion = value;
59 NAME("vfModuleModelName", "serviceName", "resourceName", "name") {
61 public void populate(Model model, String value) {
62 model.modelName = value;
65 CATEGORY("category") {
67 public void populate(Model model, String value) {
68 model.category = value;
71 DESCRIPTION("serviceDescription", "resourceDescription", "vf_module_description", "description") {
73 public void populate(Model model, String value) {
74 model.modelDescription = value;
77 ORCHESTRATION_TYPE("instantiationType"){
79 public void populate(Model model, String value) {
80 model.instantiationType = value;
83 NAME_AND_DESCRIPTION("providing_service_name") {
85 public void populate(Model model, String value) {
86 model.modelName = model.modelDescription = value;
90 private static final Map<String, ModelIdentification> propertyToModelIdent;
91 private String[] keys;
93 ModelIdentification(String... keys) {
98 Map<String, ModelIdentification> mappings = new HashMap<>();
99 for (ModelIdentification ident : ModelIdentification.values()) {
100 for (String key : ident.keys) {
101 mappings.put(key, ident);
104 propertyToModelIdent = Collections.unmodifiableMap(mappings);
107 private static Optional<ModelIdentification> getModelIdentFromProperty(String property) {
108 return Optional.ofNullable(propertyToModelIdent.get(property));
111 public abstract void populate(Model model, String value);
114 private String modelId; // model-invariant-id
115 private String modelName;
116 private String modelNameVersionId; // model-version-id
117 private String modelVersion;
118 private String modelDescription;
119 private String instantiationType;
120 private String category;
121 protected Set<Resource> resources = new HashSet<>();
122 protected Set<Widget> widgets = new HashSet<>();
125 * Gets the Resource Model corresponding to the supplied TOSCA type.
129 * @return the model for the type, or null
131 public static Resource getModelFor(String toscaType) {
132 Resource resource = null;
133 if (toscaType != null && !toscaType.isEmpty()) {
134 resource = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType));
139 private static Resource getModelFromPrefix(String toscaType) {
140 Resource resource = null;
141 int lastSeparator = toscaType.lastIndexOf('.');
142 if (lastSeparator != -1) {
143 resource = getModelFor(toscaType.substring(0, lastSeparator));
148 private static Optional<Resource> getModelFromType(String typePrefix) {
149 return WidgetConfigurationUtil.createModelFromType(typePrefix);
153 * Gets the object (model) corresponding to the supplied TOSCA type information, prioritising the metadata
158 * @param metaDataType
159 * the type from the TOSCA metadata
160 * @return the model for the type, or null
162 public static Resource getModelFor(String toscaType, String metaDataType) {
163 if ("Configuration".equals(metaDataType)) {
164 return new Resource(WidgetType.valueOf("CONFIGURATION"), true);
165 } else if ("CR".equals(metaDataType)) {
166 return new Resource(WidgetType.valueOf("CR"), true);
168 return getModelFor(toscaType);
172 public abstract boolean addWidget(Widget resource) throws XmlArtifactGenerationException;
175 * @return the Widget Type of this model.
177 public abstract WidgetType getWidgetType();
179 public abstract String getModelTypeName();
182 * Check whether the model's Widget Type matches the supplied type.
185 * the Widget Type to compare
186 * @return true if the Widget Type of this model matches the supplied type
188 public boolean hasWidgetType(String type) {
189 return getWidgetType() == WidgetType.valueOf(type);
192 public boolean addResource(Resource resource) {
193 return resources.add(resource);
199 * @return the delete flag
201 public boolean getDeleteFlag() {
205 public String getModelDescription() {
206 return modelDescription;
209 public String getModelId() {
213 public String getModelName() {
217 public String getModelVersion() {
221 public String getModelNameVersionId() {
222 return modelNameVersionId;
225 public String getInstantiationType() {
226 return instantiationType;
229 public String getCategory() {
233 * Gets widget version id.
235 * @return the widget version id
236 * @throws XmlArtifactGenerationException
238 public String getWidgetId() throws XmlArtifactGenerationException {
239 return Widget.createWidget(getWidgetType()).getId();
245 * @return the invariant id
246 * @throws XmlArtifactGenerationException
248 public String getWidgetInvariantId() throws XmlArtifactGenerationException {
249 return Widget.createWidget(getWidgetType()).getWidgetId();
253 * Populate model identification information.
255 * @param modelIdentInfo
256 * the model ident info
258 public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
259 if (modelIdentInfo == null) {
262 Iterator<String> iter = modelIdentInfo.keySet().iterator();
264 while (iter.hasNext()) {
265 property = iter.next();
266 Optional<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property);
267 if (modelIdent.isPresent()) {
268 modelIdent.get().populate(this, modelIdentInfo.get(property));
273 public void setModelVersion(String modelVersion) {
274 this.modelVersion = modelVersion;
277 public Set<Resource> getResources() {
281 public Set<Widget> getWidgets() {
286 public String toString() {
287 return "Model [type=" + getModelTypeName() + ", name=" + getModelName() + "]";