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.logging.ApplicationMsgs;
31 import org.onap.aai.babel.logging.LogHelper;
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.types.Cardinality;
35 import org.onap.aai.babel.xml.generator.types.ModelType;
36 import org.onap.aai.cl.api.Logger;
38 public abstract class Model {
40 public static final String GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION = "Operation Not Supported for Widgets";
42 private static Logger log = LogHelper.INSTANCE;
44 private enum ModelIdentification {
45 ID("vfModuleModelInvariantUUID", "serviceInvariantUUID", "resourceInvariantUUID", "invariantUUID",
46 "providing_service_invariant_uuid") {
48 public void populate(Model model, String value) {
49 model.modelId = value;
52 NAME_VERSION_ID("vfModuleModelUUID", "resourceUUID", "serviceUUID", "UUID", "providing_service_uuid") {
54 public void populate(Model model, String value) {
55 model.modelNameVersionId = value;
58 VERSION("vfModuleModelVersion", "serviceVersion", "resourceversion", "version") {
60 public void populate(Model model, String value) {
61 model.modelVersion = value;
64 NAME("vfModuleModelName", "serviceName", "resourceName", "name") {
66 public void populate(Model model, String value) {
67 model.modelName = value;
70 DESCRIPTION("serviceDescription", "resourceDescription", "vf_module_description", "description") {
72 public void populate(Model model, String value) {
73 model.modelDescription = value;
76 NAME_AND_DESCRIPTION("providing_service_name") {
78 public void populate(Model model, String value) {
79 model.modelName = model.modelDescription = value;
83 private static final Map<String, ModelIdentification> propertyToModelIdent;
84 private String[] keys;
86 ModelIdentification(String... keys) {
91 Map<String, ModelIdentification> mappings = new HashMap<>();
92 for (ModelIdentification ident : ModelIdentification.values()) {
93 for (String key : ident.keys) {
94 mappings.put(key, ident);
97 propertyToModelIdent = Collections.unmodifiableMap(mappings);
100 private static Optional<ModelIdentification> getModelIdentFromProperty(String property) {
101 return Optional.ofNullable(propertyToModelIdent.get(property));
104 public abstract void populate(Model model, String value);
107 private String modelId;
108 private String modelName;
109 private String modelNameVersionId;
110 private String modelVersion;
111 private String modelDescription;
113 protected Set<Resource> resources = new HashSet<>();
114 protected Set<Widget> widgets = new HashSet<>();
117 * Gets the object (model) corresponding to the supplied TOSCA type.
121 * @return the model for the type, or null
123 public static Model getModelFor(String toscaType) {
125 if (toscaType != null && !toscaType.isEmpty()) {
126 model = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType));
131 private static Model getModelFromPrefix(String toscaType) {
133 int lastSeparator = toscaType.lastIndexOf('.');
134 if (lastSeparator != -1) {
135 model = getModelFor(toscaType.substring(0, lastSeparator));
140 private static Optional<Model> getModelFromType(String typePrefix) {
141 Optional<Model> modelToBeReturned = Optional.empty();
142 Class<? extends Model> clazz = WidgetConfigurationUtil.getModelFromType(typePrefix);
145 modelToBeReturned = Optional.ofNullable(clazz.getConstructor().newInstance());
146 } catch (Exception e) {
147 log.error(ApplicationMsgs.INVALID_CSAR_FILE, e);
150 return modelToBeReturned;
154 * Gets the object (model) corresponding to the supplied TOSCA type information, prioritising the metadata
159 * @param metaDataType
160 * the type from the TOSCA metadata
161 * @return the model for the type, or null
163 public static Model getModelFor(String toscaType, String metaDataType) {
164 if ("Configuration".equals(metaDataType)) {
165 return new Configuration();
166 } else if ("CR".equals(metaDataType)) {
169 return getModelFor(toscaType);
173 public abstract boolean addResource(Resource resource);
175 public abstract boolean addWidget(Widget resource);
177 public abstract Widget.Type getWidgetType();
182 * @return the cardinality
184 public Cardinality getCardinality() {
185 org.onap.aai.babel.xml.generator.types.Model model =
186 this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
187 return model.cardinality();
193 * @return the delete flag
195 public boolean getDeleteFlag() {
196 org.onap.aai.babel.xml.generator.types.Model model =
197 this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
198 return model.dataDeleteFlag();
201 public String getModelDescription() {
202 return modelDescription;
205 public String getModelId() {
210 public String getModelName() {
214 public String getModelVersion() {
218 public String getModelNameVersionId() {
220 return modelNameVersionId;
226 * @return the model type
228 public ModelType getModelType() {
229 if (this instanceof Service) {
230 return ModelType.SERVICE;
231 } else if (this instanceof Resource) {
232 return ModelType.RESOURCE;
233 } else if (this instanceof Widget) {
234 return ModelType.WIDGET;
241 * Gets widget version id.
243 * @return the widget version id
245 public String getWidgetId() {
246 org.onap.aai.babel.xml.generator.types.Model model =
247 this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
248 return Widget.getWidget(model.widget()).getId();
254 * @return the invariant id
256 public String getWidgetInvariantId() {
257 org.onap.aai.babel.xml.generator.types.Model model =
258 this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
259 return Widget.getWidget(model.widget()).getWidgetId();
263 * Populate model identification information.
265 * @param modelIdentInfo
266 * the model ident info
268 public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
269 Iterator<String> iter = modelIdentInfo.keySet().iterator();
271 while (iter.hasNext()) {
272 property = iter.next();
273 Optional<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property);
274 if (modelIdent.isPresent()) {
275 modelIdent.get().populate(this, modelIdentInfo.get(property));
280 public void setModelVersion(String modelVersion) {
281 this.modelVersion = modelVersion;
284 public Set<Resource> getResources() {
288 public Set<Widget> getWidgets() {
292 private void checkSupported() {
293 if (this instanceof Widget) {
294 throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);