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;
89 SDNC_MODEL_NAME("sdnc_model_name") {
91 public void populate(Model model, String value) {
92 model.sdncModelName = value;
95 SDNC_MODEL_VERSION("sdnc_model_version") {
97 public void populate(Model model, String value) {
98 model.sdncModelVersion = value;
102 private static final Map<String, ModelIdentification> propertyToModelIdent;
103 private String[] keys;
105 ModelIdentification(String... keys) {
110 Map<String, ModelIdentification> mappings = new HashMap<>();
111 for (ModelIdentification ident : ModelIdentification.values()) {
112 for (String key : ident.keys) {
113 mappings.put(key, ident);
116 propertyToModelIdent = Collections.unmodifiableMap(mappings);
119 private static Optional<ModelIdentification> getModelIdentFromProperty(String property) {
120 return Optional.ofNullable(propertyToModelIdent.get(property));
123 public abstract void populate(Model model, String value);
126 private String modelId; // model-invariant-id
127 private String modelName;
128 private String modelNameVersionId; // model-version-id
129 private String modelVersion;
130 private String modelDescription;
131 private String instantiationType;
132 private String category;
133 private String sdncModelVersion;
134 private String sdncModelName;
135 protected Set<Resource> resources = new HashSet<>();
136 protected Set<Widget> widgets = new HashSet<>();
139 * Gets the Resource Model corresponding to the supplied TOSCA type.
143 * @return the model for the type, or null
145 public static Resource getModelFor(String toscaType) {
146 Resource resource = null;
147 if (toscaType != null && !toscaType.isEmpty()) {
148 resource = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType));
153 private static Resource getModelFromPrefix(String toscaType) {
154 Resource resource = null;
155 int lastSeparator = toscaType.lastIndexOf('.');
156 if (lastSeparator != -1) {
157 resource = getModelFor(toscaType.substring(0, lastSeparator));
162 private static Optional<Resource> getModelFromType(String typePrefix) {
163 return WidgetConfigurationUtil.createModelFromType(typePrefix);
167 * Gets the object (model) corresponding to the supplied TOSCA type information, prioritising the metadata
172 * @param metaDataType
173 * the type from the TOSCA metadata
174 * @return the model for the type, or null
176 public static Resource getModelFor(String toscaType, String metaDataType) {
177 if ("Configuration".equals(metaDataType)) {
178 return new Resource(WidgetType.valueOf("CONFIGURATION"), true);
179 } else if ("CR".equals(metaDataType)) {
180 return new Resource(WidgetType.valueOf("CR"), true);
182 return getModelFor(toscaType);
186 public abstract boolean addWidget(Widget resource) throws XmlArtifactGenerationException;
189 * @return the Widget Type of this model.
191 public abstract WidgetType getWidgetType();
193 public abstract String getModelTypeName();
196 * Check whether the model's Widget Type matches the supplied type.
199 * the Widget Type to compare
200 * @return true if the Widget Type of this model matches the supplied type
202 public boolean hasWidgetType(String type) {
203 return getWidgetType() == WidgetType.valueOf(type);
206 public boolean addResource(Resource resource) {
207 return resources.add(resource);
213 * @return the delete flag
215 public boolean getDeleteFlag() {
219 public String getModelDescription() {
220 return modelDescription;
223 public String getModelId() {
227 public String getModelName() {
231 public String getModelVersion() {
235 public String getModelNameVersionId() {
236 return modelNameVersionId;
239 public String getInstantiationType() {
240 return instantiationType;
243 public String getCategory() {
247 public String getSdncModelName() {
248 return sdncModelName;
251 public String getSdncModelVersion() {
252 return sdncModelVersion;
256 * Gets widget version id.
258 * @return the widget version id
259 * @throws XmlArtifactGenerationException
261 public String getWidgetId() throws XmlArtifactGenerationException {
262 return Widget.createWidget(getWidgetType()).getId();
268 * @return the invariant id
269 * @throws XmlArtifactGenerationException
271 public String getWidgetInvariantId() throws XmlArtifactGenerationException {
272 return Widget.createWidget(getWidgetType()).getWidgetId();
276 * Populate model identification information.
278 * @param modelIdentInfo
279 * the model ident info
281 public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
282 if (modelIdentInfo == null) {
285 Iterator<String> iter = modelIdentInfo.keySet().iterator();
287 while (iter.hasNext()) {
288 property = iter.next();
289 Optional<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property);
290 if (modelIdent.isPresent()) {
291 modelIdent.get().populate(this, modelIdentInfo.get(property));
296 public void setModelVersion(String modelVersion) {
297 this.modelVersion = modelVersion;
300 public Set<Resource> getResources() {
304 public Set<Widget> getWidgets() {
309 public String toString() {
310 return "Model [type=" + getModelTypeName() + ", name=" + getModelName() + "]";