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.XmlArtifactGenerationException;
31 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
32 import org.onap.aai.babel.xml.generator.error.IllegalAccessException;
33 import org.onap.aai.babel.xml.generator.model.Widget.Type;
34 import org.onap.aai.babel.xml.generator.types.ModelType;
36 public abstract class Model {
38 public static final String GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION = "Operation Not Supported for Widgets";
40 private enum ModelIdentification {
41 ID("vfModuleModelInvariantUUID", "serviceInvariantUUID", "resourceInvariantUUID", "invariantUUID",
42 "providing_service_invariant_uuid") {
44 public void populate(Model model, String value) {
45 model.modelId = value;
48 NAME_VERSION_ID("vfModuleModelUUID", "resourceUUID", "serviceUUID", "UUID", "providing_service_uuid") {
50 public void populate(Model model, String value) {
51 model.modelNameVersionId = value;
54 VERSION("vfModuleModelVersion", "serviceVersion", "resourceversion", "version") {
56 public void populate(Model model, String value) {
57 model.modelVersion = value;
60 NAME("vfModuleModelName", "serviceName", "resourceName", "name") {
62 public void populate(Model model, String value) {
63 model.modelName = value;
66 DESCRIPTION("serviceDescription", "resourceDescription", "vf_module_description", "description") {
68 public void populate(Model model, String value) {
69 model.modelDescription = value;
72 NAME_AND_DESCRIPTION("providing_service_name") {
74 public void populate(Model model, String value) {
75 model.modelName = model.modelDescription = value;
79 private static final Map<String, ModelIdentification> propertyToModelIdent;
80 private String[] keys;
82 ModelIdentification(String... keys) {
87 Map<String, ModelIdentification> mappings = new HashMap<>();
88 for (ModelIdentification ident : ModelIdentification.values()) {
89 for (String key : ident.keys) {
90 mappings.put(key, ident);
93 propertyToModelIdent = Collections.unmodifiableMap(mappings);
96 private static Optional<ModelIdentification> getModelIdentFromProperty(String property) {
97 return Optional.ofNullable(propertyToModelIdent.get(property));
100 public abstract void populate(Model model, String value);
103 private String modelId;
104 private String modelName;
105 private String modelNameVersionId;
106 private String modelVersion;
107 private String modelDescription;
109 protected Set<Resource> resources = new HashSet<>();
110 protected Set<Widget> widgets = new HashSet<>();
113 * Gets the object (model) corresponding to the supplied TOSCA type.
117 * @return the model for the type, or null
119 public static Resource getModelFor(String toscaType) {
120 Resource resource = null;
121 if (toscaType != null && !toscaType.isEmpty()) {
122 resource = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType));
127 private static Resource getModelFromPrefix(String toscaType) {
128 Resource resource = null;
129 int lastSeparator = toscaType.lastIndexOf('.');
130 if (lastSeparator != -1) {
131 resource = getModelFor(toscaType.substring(0, lastSeparator));
136 private static Optional<Resource> getModelFromType(String typePrefix) {
137 return WidgetConfigurationUtil.createModelFromType(typePrefix);
141 * Gets the object (model) corresponding to the supplied TOSCA type information, prioritising the metadata
146 * @param metaDataType
147 * the type from the TOSCA metadata
148 * @return the model for the type, or null
150 public static Resource getModelFor(String toscaType, String metaDataType) {
151 if ("Configuration".equals(metaDataType)) {
152 return new Resource(Type.CONFIGURATION, true);
153 } else if ("CR".equals(metaDataType)) {
154 return new Resource(Type.CR, true);
156 return getModelFor(toscaType);
160 public abstract boolean addResource(Resource resource);
162 public abstract boolean addWidget(Widget resource) throws XmlArtifactGenerationException;
164 public abstract Widget.Type getWidgetType();
166 public abstract Map<String, Object> getProperties();
168 public abstract boolean isResource();
173 * @return the delete flag
175 public boolean getDeleteFlag() {
179 public String getModelDescription() {
180 return modelDescription;
183 public String getModelId() {
188 public String getModelName() {
192 public String getModelVersion() {
196 public String getModelNameVersionId() {
198 return modelNameVersionId;
204 * @return the model type
206 public ModelType getModelType() {
207 if (this instanceof Service) {
208 return ModelType.SERVICE;
209 } else if (this instanceof Resource) {
210 return ModelType.RESOURCE;
211 } else if (this instanceof Widget) {
212 return ModelType.WIDGET;
219 * Gets widget version id.
221 * @return the widget version id
222 * @throws XmlArtifactGenerationException
224 public String getWidgetId() throws XmlArtifactGenerationException {
225 return Widget.getWidget(getWidgetType()).getId();
231 * @return the invariant id
232 * @throws XmlArtifactGenerationException
234 public String getWidgetInvariantId() throws XmlArtifactGenerationException {
235 return Widget.getWidget(getWidgetType()).getWidgetId();
239 * Populate model identification information.
241 * @param modelIdentInfo
242 * the model ident info
244 public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
245 Iterator<String> iter = modelIdentInfo.keySet().iterator();
247 while (iter.hasNext()) {
248 property = iter.next();
249 Optional<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property);
250 if (modelIdent.isPresent()) {
251 modelIdent.get().populate(this, modelIdentInfo.get(property));
256 public void setModelVersion(String modelVersion) {
257 this.modelVersion = modelVersion;
260 public Set<Resource> getResources() {
264 public Set<Widget> getWidgets() {
268 private void checkSupported() {
269 if (this instanceof Widget) {
270 throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);