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=========================================================
21 package org.onap.aai.babel.xml.generator.model;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Iterator;
28 import java.util.Optional;
30 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
31 import org.onap.aai.babel.xml.generator.error.IllegalAccessException;
32 import org.onap.aai.babel.xml.generator.model.Widget.Type;
33 import org.onap.aai.babel.xml.generator.types.ModelType;
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 DESCRIPTION("serviceDescription", "resourceDescription", "vf_module_description", "description") {
67 public void populate(Model model, String value) {
68 model.modelDescription = value;
71 NAME_AND_DESCRIPTION("providing_service_name") {
73 public void populate(Model model, String value) {
74 model.modelName = model.modelDescription = value;
78 private static final Map<String, ModelIdentification> propertyToModelIdent;
79 private String[] keys;
81 ModelIdentification(String... keys) {
86 Map<String, ModelIdentification> mappings = new HashMap<>();
87 for (ModelIdentification ident : ModelIdentification.values()) {
88 for (String key : ident.keys) {
89 mappings.put(key, ident);
92 propertyToModelIdent = Collections.unmodifiableMap(mappings);
95 private static Optional<ModelIdentification> getModelIdentFromProperty(String property) {
96 return Optional.ofNullable(propertyToModelIdent.get(property));
99 public abstract void populate(Model model, String value);
102 private String modelId;
103 private String modelName;
104 private String modelNameVersionId;
105 private String modelVersion;
106 private String modelDescription;
108 protected Set<Resource> resources = new HashSet<>();
109 protected Set<Widget> widgets = new HashSet<>();
112 * Gets the object (model) corresponding to the supplied TOSCA type.
116 * @return the model for the type, or null
118 public static Resource getModelFor(String toscaType) {
119 Resource resource = null;
120 if (toscaType != null && !toscaType.isEmpty()) {
121 resource = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType));
126 private static Resource getModelFromPrefix(String toscaType) {
127 Resource resource = null;
128 int lastSeparator = toscaType.lastIndexOf('.');
129 if (lastSeparator != -1) {
130 resource = getModelFor(toscaType.substring(0, lastSeparator));
135 private static Optional<Resource> getModelFromType(String typePrefix) {
136 return WidgetConfigurationUtil.createModelFromType(typePrefix);
140 * Gets the object (model) corresponding to the supplied TOSCA type information, prioritising the metadata
145 * @param metaDataType
146 * the type from the TOSCA metadata
147 * @return the model for the type, or null
149 public static Resource getModelFor(String toscaType, String metaDataType) {
150 if ("Configuration".equals(metaDataType)) {
151 return new Resource(Type.CONFIGURATION, true);
152 } else if ("CR".equals(metaDataType)) {
153 return new Resource(Type.CR, true);
155 return getModelFor(toscaType);
159 public abstract boolean addResource(Resource resource);
161 public abstract boolean addWidget(Widget resource);
163 public abstract Widget.Type getWidgetType();
165 public abstract Map<String, Object> getProperties();
167 public abstract boolean isResource();
172 * @return the delete flag
174 public boolean getDeleteFlag() {
175 org.onap.aai.babel.xml.generator.types.Model model =
176 this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
177 return model.dataDeleteFlag();
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
224 public String getWidgetId() {
225 org.onap.aai.babel.xml.generator.types.Model model =
226 this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
227 return Widget.getWidget(model.widget()).getId();
233 * @return the invariant id
235 public String getWidgetInvariantId() {
236 org.onap.aai.babel.xml.generator.types.Model model =
237 this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
238 return Widget.getWidget(model.widget()).getWidgetId();
242 * Populate model identification information.
244 * @param modelIdentInfo
245 * the model ident info
247 public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
248 Iterator<String> iter = modelIdentInfo.keySet().iterator();
250 while (iter.hasNext()) {
251 property = iter.next();
252 Optional<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property);
253 if (modelIdent.isPresent()) {
254 modelIdent.get().populate(this, modelIdentInfo.get(property));
259 public void setModelVersion(String modelVersion) {
260 this.modelVersion = modelVersion;
263 public Set<Resource> getResources() {
267 public Set<Widget> getWidgets() {
271 private void checkSupported() {
272 if (this instanceof Widget) {
273 throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);