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 ORCHESTRATION_TYPE("instantiationType"){
72 public void populate(Model model, String value) {
73 model.instantiationType = value;
76 NAME_AND_DESCRIPTION("providing_service_name") {
78 public void populate(Model model, String value) {
79 model.modelName = model.modelDescription = value;
83 private static final Map<String, ModelIdentification> propertyToModelIdent;
84 private String[] keys;
86 ModelIdentification(String... keys) {
91 Map<String, ModelIdentification> mappings = new HashMap<>();
92 for (ModelIdentification ident : ModelIdentification.values()) {
93 for (String key : ident.keys) {
94 mappings.put(key, ident);
97 propertyToModelIdent = Collections.unmodifiableMap(mappings);
100 private static Optional<ModelIdentification> getModelIdentFromProperty(String property) {
101 return Optional.ofNullable(propertyToModelIdent.get(property));
104 public abstract void populate(Model model, String value);
107 private String modelId; // model-invariant-id
108 private String modelName;
109 private String modelNameVersionId; // model-version-id
110 private String modelVersion;
111 private String modelDescription;
112 private String instantiationType;
113 protected Set<Resource> resources = new HashSet<>();
114 protected Set<Widget> widgets = new HashSet<>();
117 * Gets the Resource Model corresponding to the supplied TOSCA type.
121 * @return the model for the type, or null
123 public static Resource getModelFor(String toscaType) {
124 Resource resource = null;
125 if (toscaType != null && !toscaType.isEmpty()) {
126 resource = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType));
131 private static Resource getModelFromPrefix(String toscaType) {
132 Resource resource = null;
133 int lastSeparator = toscaType.lastIndexOf('.');
134 if (lastSeparator != -1) {
135 resource = getModelFor(toscaType.substring(0, lastSeparator));
140 private static Optional<Resource> getModelFromType(String typePrefix) {
141 return WidgetConfigurationUtil.createModelFromType(typePrefix);
145 * Gets the object (model) corresponding to the supplied TOSCA type information, prioritising the metadata
150 * @param metaDataType
151 * the type from the TOSCA metadata
152 * @return the model for the type, or null
154 public static Resource getModelFor(String toscaType, String metaDataType) {
155 if ("Configuration".equals(metaDataType)) {
156 return new Resource(WidgetType.valueOf("CONFIGURATION"), true);
157 } else if ("CR".equals(metaDataType)) {
158 return new Resource(WidgetType.valueOf("CR"), true);
160 return getModelFor(toscaType);
164 public abstract boolean addWidget(Widget resource) throws XmlArtifactGenerationException;
167 * @return the Widget Type of this model.
169 public abstract WidgetType getWidgetType();
171 public abstract String getModelTypeName();
174 * Check whether the model's Widget Type matches the supplied type.
177 * the Widget Type to compare
178 * @return true if the Widget Type of this model matches the supplied type
180 public boolean hasWidgetType(String type) {
181 return getWidgetType() == WidgetType.valueOf(type);
184 public boolean addResource(Resource resource) {
185 return resources.add(resource);
191 * @return the delete flag
193 public boolean getDeleteFlag() {
197 public String getModelDescription() {
198 return modelDescription;
201 public String getModelId() {
205 public String getModelName() {
209 public String getModelVersion() {
213 public String getModelNameVersionId() {
214 return modelNameVersionId;
217 public String getInstantiationType() {
218 return instantiationType;
222 * Gets widget version id.
224 * @return the widget version id
225 * @throws XmlArtifactGenerationException
227 public String getWidgetId() throws XmlArtifactGenerationException {
228 return Widget.createWidget(getWidgetType()).getId();
234 * @return the invariant id
235 * @throws XmlArtifactGenerationException
237 public String getWidgetInvariantId() throws XmlArtifactGenerationException {
238 return Widget.createWidget(getWidgetType()).getWidgetId();
242 * Populate model identification information.
244 * @param modelIdentInfo
245 * the model ident info
247 public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
248 if (modelIdentInfo == null) {
251 Iterator<String> iter = modelIdentInfo.keySet().iterator();
253 while (iter.hasNext()) {
254 property = iter.next();
255 Optional<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property);
256 if (modelIdent.isPresent()) {
257 modelIdent.get().populate(this, modelIdentInfo.get(property));
262 public void setModelVersion(String modelVersion) {
263 this.modelVersion = modelVersion;
266 public Set<Resource> getResources() {
270 public Set<Widget> getWidgets() {
275 public String toString() {
276 return "Model [type=" + getModelTypeName() + ", name=" + getModelName() + "]";