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.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.error.IllegalAccessException;
33 import org.onap.aai.babel.xml.generator.types.Cardinality;
34 import org.onap.aai.babel.xml.generator.types.ModelType;
35 import org.onap.aai.cl.api.Logger;
37 public abstract class Model {
39 public static final String GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION = "Operation Not Supported for Widgets";
41 private static Logger log = LogHelper.INSTANCE;
43 private static Map<String, Class<? extends Model>> typeToModel = new HashMap<>();
45 typeToModel.put("org.openecomp.resource.vf.allottedResource", AllotedResource.class);
46 typeToModel.put("org.openecomp.resource.vfc.AllottedResource", ProvidingService.class);
47 typeToModel.put("org.openecomp.resource.vfc", VServerWidget.class);
48 typeToModel.put("org.openecomp.resource.cp", LIntfWidget.class);
49 typeToModel.put("org.openecomp.cp", LIntfWidget.class);
50 typeToModel.put("org.openecomp.resource.vl", L3Network.class);
51 typeToModel.put("org.openecomp.resource.vf", VirtualFunction.class);
52 typeToModel.put("org.openecomp.groups.vfmodule", VfModule.class);
53 typeToModel.put("org.openecomp.groups.VfModule", VfModule.class);
54 typeToModel.put("org.openecomp.resource.vfc.nodes.heat.cinder", VolumeWidget.class);
55 typeToModel.put("org.openecomp.nodes.PortMirroringConfiguration", Configuration.class);
56 typeToModel.put("org.openecomp.resource.cr.Kk1806Cr1", CR.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 (Exception e) {
162 log.error(ApplicationMsgs.INVALID_CSAR_FILE, e);
165 return modelToBeReturned;
169 * Gets the object (model) corresponding to the supplied TOSCA type information, prioritising the metadata
174 * @param metaDataType
175 * the type from the TOSCA metadata
176 * @return the model for the type, or null
178 public static Model getModelFor(String toscaType, String metaDataType) {
179 if ("Configuration".equals(metaDataType)) {
180 return new Configuration();
181 } else if ("CR".equals(metaDataType)) {
184 return getModelFor(toscaType);
188 public abstract boolean addResource(Resource resource);
190 public abstract boolean addWidget(Widget resource);
192 public abstract Widget.Type getWidgetType();
197 * @return the cardinality
199 public Cardinality getCardinality() {
200 org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
201 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
202 return model.cardinality();
208 * @return the delete flag
210 public boolean getDeleteFlag() {
211 org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
212 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
213 return model.dataDeleteFlag();
216 public String getModelDescription() {
217 return modelDescription;
220 public String getModelId() {
225 public String getModelName() {
229 public String getModelVersion() {
233 public String getModelNameVersionId() {
235 return modelNameVersionId;
241 * @return the model type
243 public ModelType getModelType() {
244 if (this instanceof Service) {
245 return ModelType.SERVICE;
246 } else if (this instanceof Resource) {
247 return ModelType.RESOURCE;
248 } else if (this instanceof Widget) {
249 return ModelType.WIDGET;
256 * Gets widget version id.
258 * @return the widget version id
260 public String getWidgetId() {
261 org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
262 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
263 return Widget.getWidget(model.widget()).getId();
269 * @return the invariant id
271 public String getWidgetInvariantId() {
272 org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
273 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
274 return Widget.getWidget(model.widget()).getWidgetId();
278 * Populate model identification information.
280 * @param modelIdentInfo
281 * the model ident info
283 public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
284 Iterator<String> iter = modelIdentInfo.keySet().iterator();
286 while (iter.hasNext()) {
287 property = iter.next();
288 Optional<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property);
289 if (modelIdent.isPresent()) {
290 modelIdent.get().populate(this, modelIdentInfo.get(property));
295 public void setModelVersion(String modelVersion) {
296 this.modelVersion = modelVersion;
299 public Set<Resource> getResources() {
303 public Set<Widget> getWidgets() {
307 private void checkSupported() {
308 if (this instanceof Widget) {
309 throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);