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 * ================================================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.aai.babel.xml.generator.model;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Iterator;
29 import java.util.Optional;
31 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
32 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
34 public abstract class Model {
36 public static final String GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION = "Operation Not Supported for Widgets";
38 private enum ModelIdentification {
39 ID("vfModuleModelInvariantUUID", "serviceInvariantUUID", "resourceInvariantUUID", "invariantUUID",
40 "providing_service_invariant_uuid") {
42 public void populate(Model model, String value) {
43 model.modelId = value;
46 NAME_VERSION_ID("vfModuleModelUUID", "resourceUUID", "serviceUUID", "UUID", "providing_service_uuid") {
48 public void populate(Model model, String value) {
49 model.modelNameVersionId = value;
52 VERSION("vfModuleModelVersion", "serviceVersion", "resourceversion", "version") {
54 public void populate(Model model, String value) {
55 model.modelVersion = value;
58 NAME("vfModuleModelName", "serviceName", "resourceName", "name") {
60 public void populate(Model model, String value) {
61 model.modelName = value;
64 DESCRIPTION("serviceDescription", "resourceDescription", "vf_module_description", "description") {
66 public void populate(Model model, String value) {
67 model.modelDescription = value;
70 NAME_AND_DESCRIPTION("providing_service_name") {
72 public void populate(Model model, String value) {
73 model.modelName = model.modelDescription = value;
77 private static final Map<String, ModelIdentification> propertyToModelIdent;
78 private String[] keys;
80 ModelIdentification(String... keys) {
85 Map<String, ModelIdentification> mappings = new HashMap<>();
86 for (ModelIdentification ident : ModelIdentification.values()) {
87 for (String key : ident.keys) {
88 mappings.put(key, ident);
91 propertyToModelIdent = Collections.unmodifiableMap(mappings);
94 private static Optional<ModelIdentification> getModelIdentFromProperty(String property) {
95 return Optional.ofNullable(propertyToModelIdent.get(property));
98 public abstract void populate(Model model, String value);
101 private String modelId; // model-invariant-id
102 private String modelName;
103 private String modelNameVersionId; // model-version-id
104 private String modelVersion;
105 private String modelDescription;
107 protected Set<Resource> resources = new HashSet<>();
108 protected Set<Widget> widgets = new HashSet<>();
111 * Gets the Resource Model corresponding to the supplied TOSCA type.
115 * @return the model for the type, or null
117 public static Resource getModelFor(String toscaType) {
118 Resource resource = null;
119 if (toscaType != null && !toscaType.isEmpty()) {
120 resource = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType));
125 private static Resource getModelFromPrefix(String toscaType) {
126 Resource resource = null;
127 int lastSeparator = toscaType.lastIndexOf('.');
128 if (lastSeparator != -1) {
129 resource = getModelFor(toscaType.substring(0, lastSeparator));
134 private static Optional<Resource> getModelFromType(String typePrefix) {
135 return WidgetConfigurationUtil.createModelFromType(typePrefix);
139 * Gets the object (model) corresponding to the supplied TOSCA type information, prioritising the metadata
144 * @param metaDataType
145 * the type from the TOSCA metadata
146 * @return the model for the type, or null
148 public static Resource getModelFor(String toscaType, String metaDataType) {
149 if ("Configuration".equals(metaDataType)) {
150 return new Resource(WidgetType.valueOf("CONFIGURATION"), true);
151 } else if ("CR".equals(metaDataType)) {
152 return new Resource(WidgetType.valueOf("CR"), true);
154 return getModelFor(toscaType);
158 public abstract boolean addWidget(Widget resource) throws XmlArtifactGenerationException;
160 public abstract WidgetType getWidgetType();
162 public abstract String getModelTypeName();
164 public boolean addResource(Resource resource) {
165 return resources.add(resource);
171 * @return the delete flag
173 public boolean getDeleteFlag() {
177 public String getModelDescription() {
178 return modelDescription;
181 public String getModelId() {
185 public String getModelName() {
189 public String getModelVersion() {
193 public String getModelNameVersionId() {
194 return modelNameVersionId;
198 * Gets widget version id.
200 * @return the widget version id
201 * @throws XmlArtifactGenerationException
203 public String getWidgetId() throws XmlArtifactGenerationException {
204 return Widget.getWidget(getWidgetType()).getId();
210 * @return the invariant id
211 * @throws XmlArtifactGenerationException
213 public String getWidgetInvariantId() throws XmlArtifactGenerationException {
214 return Widget.getWidget(getWidgetType()).getWidgetId();
218 * Populate model identification information.
220 * @param modelIdentInfo
221 * the model ident info
223 public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
224 Iterator<String> iter = modelIdentInfo.keySet().iterator();
226 while (iter.hasNext()) {
227 property = iter.next();
228 Optional<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property);
229 if (modelIdent.isPresent()) {
230 modelIdent.get().populate(this, modelIdentInfo.get(property));
235 public void setModelVersion(String modelVersion) {
236 this.modelVersion = modelVersion;
239 public Set<Resource> getResources() {
243 public Set<Widget> getWidgets() {
248 public String toString() {
249 return "Model [type=" + getModelTypeName() + ", name=" + getModelName() + "]";