2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 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.lang.reflect.InvocationTargetException;
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.logging.ApplicationMsgs;
32 import org.onap.aai.babel.logging.LogHelper;
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 static Map<String, Class<? extends Model>> typeToModel = new HashMap<>();
46 typeToModel.put("org.openecomp.resource.vf.allottedResource", AllotedResource.class);
47 typeToModel.put("org.openecomp.resource.vfc.AllottedResource", ProvidingService.class);
48 typeToModel.put("org.openecomp.resource.vfc", VServerWidget.class);
49 typeToModel.put("org.openecomp.resource.cp", LIntfWidget.class);
50 typeToModel.put("org.openecomp.cp", LIntfWidget.class);
51 typeToModel.put("org.openecomp.resource.vl", L3Network.class);
52 typeToModel.put("org.openecomp.resource.vf", VirtualFunction.class);
53 typeToModel.put("org.openecomp.groups.vfmodule", VfModule.class);
54 typeToModel.put("org.openecomp.groups.VfModule", VfModule.class);
55 typeToModel.put("org.openecomp.resource.vfc.nodes.heat.cinder", VolumeWidget.class);
56 typeToModel.put("org.openecomp.nodes.PortMirroringConfiguration", Configuration.class);
59 private enum ModelIdentification {
60 ID("vfModuleModelInvariantUUID", "serviceInvariantUUID", "resourceInvariantUUID", "invariantUUID",
61 "providing_service_invariant_uuid") {
63 public void populate(Model model, String value) {
64 model.modelId = value;
67 NAME_VERSION_ID("vfModuleModelUUID", "resourceUUID", "serviceUUID", "UUID", "providing_service_uuid") {
69 public void populate(Model model, String value) {
70 model.modelNameVersionId = value;
73 VERSION("vfModuleModelVersion", "serviceVersion", "resourceversion", "version") {
75 public void populate(Model model, String value) {
76 model.modelVersion = value;
79 NAME("vfModuleModelName", "serviceName", "resourceName", "name") {
81 public void populate(Model model, String value) {
82 model.modelName = value;
85 DESCRIPTION("serviceDescription", "resourceDescription", "vf_module_description", "description") {
87 public void populate(Model model, String value) {
88 model.modelDescription = value;
91 NAME_AND_DESCRIPTION("providing_service_name") {
93 public void populate(Model model, String value) {
94 model.modelName = model.modelDescription = value;
98 private static final Map<String, ModelIdentification> propertyToModelIdent;
99 private String[] keys;
101 ModelIdentification(String... keys) {
106 Map<String, ModelIdentification> mappings = new HashMap<>();
107 for (ModelIdentification ident : ModelIdentification.values()) {
108 for (String key : ident.keys) {
109 mappings.put(key, ident);
112 propertyToModelIdent = Collections.unmodifiableMap(mappings);
115 private static Optional<ModelIdentification> getModelIdentFromProperty(String property) {
116 return Optional.ofNullable(propertyToModelIdent.get(property));
119 public abstract void populate(Model model, String value);
122 private String modelId;
123 private String modelName;
124 private String modelNameVersionId;
125 private String modelVersion;
126 private String modelDescription;
128 protected Set<Resource> resources = new HashSet<>();
129 protected Set<Widget> widgets = new HashSet<>();
132 * Gets the object (model) corresponding to the supplied TOSCA type.
136 * @return the model for the type, or null
138 public static Model getModelFor(String toscaType) {
140 if (toscaType != null && !toscaType.isEmpty()) {
141 model = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType));
146 private static Model getModelFromPrefix(String toscaType) {
148 int lastSeparator = toscaType.lastIndexOf('.');
149 if (lastSeparator != -1) {
150 model = getModelFor(toscaType.substring(0, lastSeparator));
155 private static Optional<Model> getModelFromType(String typePrefix) {
156 Optional<Model> modelToBeReturned = Optional.empty();
157 Class<? extends Model> clazz = typeToModel.get(typePrefix);
160 modelToBeReturned = Optional.ofNullable(clazz.getConstructor().newInstance());
161 } catch (InstantiationException | java.lang.IllegalAccessException | IllegalArgumentException
162 | InvocationTargetException | NoSuchMethodException | SecurityException e) {
163 log.error(ApplicationMsgs.INVALID_CSAR_FILE, e);
166 return modelToBeReturned;
169 public abstract boolean addResource(Resource resource);
171 public abstract boolean addWidget(Widget resource);
173 public abstract Widget.Type getWidgetType();
178 * @return the cardinality
180 public Cardinality getCardinality() {
181 org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
182 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
183 return model.cardinality();
189 * @return the delete flag
191 public boolean getDeleteFlag() {
192 org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
193 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
194 return model.dataDeleteFlag();
197 public String getModelDescription() {
198 return modelDescription;
201 public String getModelId() {
206 public String getModelName() {
210 public String getModelVersion() {
214 public String getModelNameVersionId() {
216 return modelNameVersionId;
222 * @return the model type
224 public ModelType getModelType() {
225 if (this instanceof Service) {
226 return ModelType.SERVICE;
227 } else if (this instanceof Resource) {
228 return ModelType.RESOURCE;
229 } else if (this instanceof Widget) {
230 return ModelType.WIDGET;
237 * Gets widget version id.
239 * @return the widget version id
241 public String getWidgetId() {
242 org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
243 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
244 return Widget.getWidget(model.widget()).getId();
250 * @return the invariant id
252 public String getWidgetInvariantId() {
253 org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
254 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
255 return Widget.getWidget(model.widget()).getWidgetId();
259 * Populate model identification information.
261 * @param modelIdentInfo
262 * the model ident info
264 public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
265 Iterator<String> iter = modelIdentInfo.keySet().iterator();
267 while (iter.hasNext()) {
268 property = iter.next();
269 Optional<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property);
270 if (modelIdent.isPresent()) {
271 modelIdent.get().populate(this, modelIdentInfo.get(property));
276 public void setModelVersion(String modelVersion) {
277 this.modelVersion = modelVersion;
280 public Set<Resource> getResources() {
284 public Set<Widget> getWidgets() {
288 private void checkSupported() {
289 if (this instanceof Widget) {
290 throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);