2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 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;
33 import org.onap.aai.babel.xml.generator.error.IllegalAccessException;
34 import org.onap.aai.babel.xml.generator.model.Widget.Type;
35 import org.onap.aai.babel.xml.generator.types.ModelType;
37 public abstract class Model {
39 public static final String GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION = "Operation Not Supported for Widgets";
41 private enum ModelIdentification {
42 ID("vfModuleModelInvariantUUID", "serviceInvariantUUID", "resourceInvariantUUID", "invariantUUID",
43 "providing_service_invariant_uuid") {
45 public void populate(Model model, String value) {
46 model.modelId = value;
49 NAME_VERSION_ID("vfModuleModelUUID", "resourceUUID", "serviceUUID", "UUID", "providing_service_uuid") {
51 public void populate(Model model, String value) {
52 model.modelNameVersionId = value;
55 VERSION("vfModuleModelVersion", "serviceVersion", "resourceversion", "version") {
57 public void populate(Model model, String value) {
58 model.modelVersion = value;
61 NAME("vfModuleModelName", "serviceName", "resourceName", "name") {
63 public void populate(Model model, String value) {
64 model.modelName = value;
67 DESCRIPTION("serviceDescription", "resourceDescription", "vf_module_description", "description") {
69 public void populate(Model model, String value) {
70 model.modelDescription = value;
73 NAME_AND_DESCRIPTION("providing_service_name") {
75 public void populate(Model model, String value) {
76 model.modelName = model.modelDescription = value;
80 private static final Map<String, ModelIdentification> propertyToModelIdent;
81 private String[] keys;
83 ModelIdentification(String... keys) {
88 Map<String, ModelIdentification> mappings = new HashMap<>();
89 for (ModelIdentification ident : ModelIdentification.values()) {
90 for (String key : ident.keys) {
91 mappings.put(key, ident);
94 propertyToModelIdent = Collections.unmodifiableMap(mappings);
97 private static Optional<ModelIdentification> getModelIdentFromProperty(String property) {
98 return Optional.ofNullable(propertyToModelIdent.get(property));
101 public abstract void populate(Model model, String value);
104 private String modelId;
105 private String modelName;
106 private String modelNameVersionId;
107 private String modelVersion;
108 private String modelDescription;
110 protected Set<Resource> resources = new HashSet<>();
111 protected Set<Widget> widgets = new HashSet<>();
114 * Gets the Resource Model corresponding to the supplied TOSCA type.
118 * @return the model for the type, or null
120 public static Resource getModelFor(String toscaType) {
121 Resource resource = null;
122 if (toscaType != null && !toscaType.isEmpty()) {
123 resource = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType));
128 private static Resource getModelFromPrefix(String toscaType) {
129 Resource resource = null;
130 int lastSeparator = toscaType.lastIndexOf('.');
131 if (lastSeparator != -1) {
132 resource = getModelFor(toscaType.substring(0, lastSeparator));
137 private static Optional<Resource> getModelFromType(String typePrefix) {
138 return WidgetConfigurationUtil.createModelFromType(typePrefix);
142 * Gets the object (model) corresponding to the supplied TOSCA type information, prioritising the metadata
147 * @param metaDataType
148 * the type from the TOSCA metadata
149 * @return the model for the type, or null
151 public static Resource getModelFor(String toscaType, String metaDataType) {
152 if ("Configuration".equals(metaDataType)) {
153 return new Resource(Type.CONFIGURATION, true);
154 } else if ("CR".equals(metaDataType)) {
155 return new Resource(Type.CR, true);
157 return getModelFor(toscaType);
161 public abstract boolean addResource(Resource resource);
163 public abstract boolean addWidget(Widget resource) throws XmlArtifactGenerationException;
165 public abstract Widget.Type getWidgetType();
167 public abstract Map<String, Object> getProperties();
169 public abstract boolean isResource();
174 * @return the delete flag
176 public boolean getDeleteFlag() {
180 public String getModelDescription() {
181 return modelDescription;
184 public String getModelId() {
189 public String getModelName() {
193 public String getModelVersion() {
197 public String getModelNameVersionId() {
199 return modelNameVersionId;
205 * @return the model type
207 public ModelType getModelType() {
208 if (this instanceof Service) {
209 return ModelType.SERVICE;
210 } else if (this instanceof Resource) {
211 return ModelType.RESOURCE;
212 } else if (this instanceof Widget) {
213 return ModelType.WIDGET;
220 * Gets widget version id.
222 * @return the widget version id
223 * @throws XmlArtifactGenerationException
225 public String getWidgetId() throws XmlArtifactGenerationException {
226 return Widget.getWidget(getWidgetType()).getId();
232 * @return the invariant id
233 * @throws XmlArtifactGenerationException
235 public String getWidgetInvariantId() throws XmlArtifactGenerationException {
236 return Widget.getWidget(getWidgetType()).getWidgetId();
240 * Populate model identification information.
242 * @param modelIdentInfo
243 * the model ident info
245 public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
246 Iterator<String> iter = modelIdentInfo.keySet().iterator();
248 while (iter.hasNext()) {
249 property = iter.next();
250 Optional<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property);
251 if (modelIdent.isPresent()) {
252 modelIdent.get().populate(this, modelIdentInfo.get(property));
257 public void setModelVersion(String modelVersion) {
258 this.modelVersion = modelVersion;
261 public Set<Resource> getResources() {
265 public Set<Widget> getWidgets() {
269 private void checkSupported() {
270 if (this instanceof Widget) {
271 throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);