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);
57 typeToModel.put("org.openecomp.resource.cr.Kk1806Cr1", CR.class);
60 private enum ModelIdentification {
61 ID("vfModuleModelInvariantUUID", "serviceInvariantUUID", "resourceInvariantUUID", "invariantUUID",
62 "providing_service_invariant_uuid") {
64 public void populate(Model model, String value) {
65 model.modelId = value;
68 NAME_VERSION_ID("vfModuleModelUUID", "resourceUUID", "serviceUUID", "UUID", "providing_service_uuid") {
70 public void populate(Model model, String value) {
71 model.modelNameVersionId = value;
74 VERSION("vfModuleModelVersion", "serviceVersion", "resourceversion", "version") {
76 public void populate(Model model, String value) {
77 model.modelVersion = value;
80 NAME("vfModuleModelName", "serviceName", "resourceName", "name") {
82 public void populate(Model model, String value) {
83 model.modelName = value;
86 DESCRIPTION("serviceDescription", "resourceDescription", "vf_module_description", "description") {
88 public void populate(Model model, String value) {
89 model.modelDescription = value;
92 NAME_AND_DESCRIPTION("providing_service_name") {
94 public void populate(Model model, String value) {
95 model.modelName = model.modelDescription = value;
99 private static final Map<String, ModelIdentification> propertyToModelIdent;
100 private String[] keys;
102 ModelIdentification(String... keys) {
107 Map<String, ModelIdentification> mappings = new HashMap<>();
108 for (ModelIdentification ident : ModelIdentification.values()) {
109 for (String key : ident.keys) {
110 mappings.put(key, ident);
113 propertyToModelIdent = Collections.unmodifiableMap(mappings);
116 private static Optional<ModelIdentification> getModelIdentFromProperty(String property) {
117 return Optional.ofNullable(propertyToModelIdent.get(property));
120 public abstract void populate(Model model, String value);
123 private String modelId;
124 private String modelName;
125 private String modelNameVersionId;
126 private String modelVersion;
127 private String modelDescription;
129 protected Set<Resource> resources = new HashSet<>();
130 protected Set<Widget> widgets = new HashSet<>();
133 * Gets the object (model) corresponding to the supplied TOSCA type.
137 * @return the model for the type, or null
139 public static Model getModelFor(String toscaType) {
141 if (toscaType != null && !toscaType.isEmpty()) {
142 model = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType));
147 private static Model getModelFromPrefix(String toscaType) {
149 int lastSeparator = toscaType.lastIndexOf('.');
150 if (lastSeparator != -1) {
151 model = getModelFor(toscaType.substring(0, lastSeparator));
156 private static Optional<Model> getModelFromType(String typePrefix) {
157 Optional<Model> modelToBeReturned = Optional.empty();
158 Class<? extends Model> clazz = typeToModel.get(typePrefix);
161 modelToBeReturned = Optional.ofNullable(clazz.getConstructor().newInstance());
162 } catch (InstantiationException | java.lang.IllegalAccessException | IllegalArgumentException
163 | InvocationTargetException | NoSuchMethodException | SecurityException e) {
164 log.error(ApplicationMsgs.INVALID_CSAR_FILE, e);
167 return modelToBeReturned;
170 public abstract boolean addResource(Resource resource);
172 public abstract boolean addWidget(Widget resource);
174 public abstract Widget.Type getWidgetType();
179 * @return the cardinality
181 public Cardinality getCardinality() {
182 org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
183 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
184 return model.cardinality();
190 * @return the delete flag
192 public boolean getDeleteFlag() {
193 org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
194 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
195 return model.dataDeleteFlag();
198 public String getModelDescription() {
199 return modelDescription;
202 public String getModelId() {
207 public String getModelName() {
211 public String getModelVersion() {
215 public String getModelNameVersionId() {
217 return modelNameVersionId;
223 * @return the model type
225 public ModelType getModelType() {
226 if (this instanceof Service) {
227 return ModelType.SERVICE;
228 } else if (this instanceof Resource) {
229 return ModelType.RESOURCE;
230 } else if (this instanceof Widget) {
231 return ModelType.WIDGET;
238 * Gets widget version id.
240 * @return the widget version id
242 public String getWidgetId() {
243 org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
244 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
245 return Widget.getWidget(model.widget()).getId();
251 * @return the invariant id
253 public String getWidgetInvariantId() {
254 org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
255 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
256 return Widget.getWidget(model.widget()).getWidgetId();
260 * Populate model identification information.
262 * @param modelIdentInfo
263 * the model ident info
265 public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
266 Iterator<String> iter = modelIdentInfo.keySet().iterator();
268 while (iter.hasNext()) {
269 property = iter.next();
270 Optional<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property);
271 if (modelIdent.isPresent()) {
272 modelIdent.get().populate(this, modelIdentInfo.get(property));
277 public void setModelVersion(String modelVersion) {
278 this.modelVersion = modelVersion;
281 public Set<Resource> getResources() {
285 public Set<Widget> getWidgets() {
289 private void checkSupported() {
290 if (this instanceof Widget) {
291 throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);