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;
161 * @return the Widget Type of this model.
163 public abstract WidgetType getWidgetType();
165 public abstract String getModelTypeName();
168 * Check whether the model's Widget Type matches the supplied type.
171 * the Widget Type to compare
172 * @return true if the Widget Type of this model matches the supplied type
174 public boolean hasWidgetType(String type) {
175 return getWidgetType() == WidgetType.valueOf(type);
178 public boolean addResource(Resource resource) {
179 return resources.add(resource);
185 * @return the delete flag
187 public boolean getDeleteFlag() {
191 public String getModelDescription() {
192 return modelDescription;
195 public String getModelId() {
199 public String getModelName() {
203 public String getModelVersion() {
207 public String getModelNameVersionId() {
208 return modelNameVersionId;
212 * Gets widget version id.
214 * @return the widget version id
215 * @throws XmlArtifactGenerationException
217 public String getWidgetId() throws XmlArtifactGenerationException {
218 return Widget.getWidget(getWidgetType()).getId();
224 * @return the invariant id
225 * @throws XmlArtifactGenerationException
227 public String getWidgetInvariantId() throws XmlArtifactGenerationException {
228 return Widget.getWidget(getWidgetType()).getWidgetId();
232 * Populate model identification information.
234 * @param modelIdentInfo
235 * the model ident info
237 public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
238 if (modelIdentInfo == null) {
241 Iterator<String> iter = modelIdentInfo.keySet().iterator();
243 while (iter.hasNext()) {
244 property = iter.next();
245 Optional<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property);
246 if (modelIdent.isPresent()) {
247 modelIdent.get().populate(this, modelIdentInfo.get(property));
252 public void setModelVersion(String modelVersion) {
253 this.modelVersion = modelVersion;
256 public Set<Resource> getResources() {
260 public Set<Widget> getWidgets() {
265 public String toString() {
266 return "Model [type=" + getModelTypeName() + ", name=" + getModelName() + "]";