X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Faai%2Fbabel%2Fxml%2Fgenerator%2Fmodel%2FModel.java;h=c67d67f1b0980b82f9729ef7dcdea826cf76a56b;hb=5a6fd49a6aca3a567464527335b107746e28f3bd;hp=0ee08380586ea42d8f66529f918d9152dfd9ace4;hpb=dc94e09008ba0eb9dce7541d2c898ddc5c500814;p=aai%2Fbabel.git diff --git a/src/main/java/org/onap/aai/babel/xml/generator/model/Model.java b/src/main/java/org/onap/aai/babel/xml/generator/model/Model.java index 0ee0838..c67d67f 100644 --- a/src/main/java/org/onap/aai/babel/xml/generator/model/Model.java +++ b/src/main/java/org/onap/aai/babel/xml/generator/model/Model.java @@ -1,9 +1,9 @@ /** - * ============LICENSE_START======================================================= + * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2018 European Software Marketing Ltd. + * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2019 European Software Marketing Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,98 +18,152 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.aai.babel.xml.generator.model; +import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; +import java.util.Optional; import java.util.Set; -import org.onap.aai.babel.xml.generator.error.IllegalAccessException; -import org.onap.aai.babel.xml.generator.types.Cardinality; -import org.onap.aai.babel.xml.generator.types.ModelType; +import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException; +import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil; +import org.onap.aai.babel.xml.generator.model.Widget.Type; public abstract class Model { + public static final String GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION = "Operation Not Supported for Widgets"; - protected Set resources = new HashSet<>(); - protected Set widgets = new HashSet<>(); + private enum ModelIdentification { + ID("vfModuleModelInvariantUUID", "serviceInvariantUUID", "resourceInvariantUUID", "invariantUUID", + "providing_service_invariant_uuid") { + @Override + public void populate(Model model, String value) { + model.modelId = value; + } + }, + NAME_VERSION_ID("vfModuleModelUUID", "resourceUUID", "serviceUUID", "UUID", "providing_service_uuid") { + @Override + public void populate(Model model, String value) { + model.modelNameVersionId = value; + } + }, + VERSION("vfModuleModelVersion", "serviceVersion", "resourceversion", "version") { + @Override + public void populate(Model model, String value) { + model.modelVersion = value; + } + }, + NAME("vfModuleModelName", "serviceName", "resourceName", "name") { + @Override + public void populate(Model model, String value) { + model.modelName = value; + } + }, + DESCRIPTION("serviceDescription", "resourceDescription", "vf_module_description", "description") { + @Override + public void populate(Model model, String value) { + model.modelDescription = value; + } + }, + NAME_AND_DESCRIPTION("providing_service_name") { + @Override + public void populate(Model model, String value) { + model.modelName = model.modelDescription = value; + } + }; + + private static final Map propertyToModelIdent; + private String[] keys; + + ModelIdentification(String... keys) { + this.keys = keys; + } + + static { + Map mappings = new HashMap<>(); + for (ModelIdentification ident : ModelIdentification.values()) { + for (String key : ident.keys) { + mappings.put(key, ident); + } + } + propertyToModelIdent = Collections.unmodifiableMap(mappings); + } + + private static Optional getModelIdentFromProperty(String property) { + return Optional.ofNullable(propertyToModelIdent.get(property)); + } + + public abstract void populate(Model model, String value); + } + private String modelId; private String modelName; - private String modelVersion; private String modelNameVersionId; + private String modelVersion; private String modelDescription; + protected Set resources = new HashSet<>(); + protected Set widgets = new HashSet<>(); + /** - * Gets the object (model) corresponding to the supplied TOSCA type. + * Gets the Resource Model corresponding to the supplied TOSCA type. * - * @param toscaType the tosca type + * @param toscaType + * the tosca type * @return the model for the type, or null */ - public static Model getModelFor(String toscaType) { - Model modelToBeReturned = null; - String typePrefix = toscaType; - while (modelToBeReturned == null && typePrefix != null && typePrefix.lastIndexOf('.') != -1) { - switch (typePrefix) { - case "org.openecomp.resource.vf.allottedResource": - modelToBeReturned = new AllotedResource(); - break; - case "org.openecomp.resource.vfc.AllottedResource": - modelToBeReturned = new ProvidingService(); - break; - case "org.openecomp.resource.vfc": - modelToBeReturned = new VServerWidget(); - break; - case "org.openecomp.resource.cp": - case "org.openecomp.cp": - modelToBeReturned = new LIntfWidget(); - break; - case "org.openecomp.resource.vl": - modelToBeReturned = new L3Network(); - break; - case "org.openecomp.resource.vf": - modelToBeReturned = new VirtualFunction(); - break; - case "org.openecomp.groups.vfmodule": - case "org.openecomp.groups.VfModule": - modelToBeReturned = new VfModule(); - break; - case "org.openecomp.resource.vfc.nodes.heat.cinder": - modelToBeReturned = new VolumeWidget(); - break; - default: - modelToBeReturned = null; - break; - } - typePrefix = typePrefix.substring(0, typePrefix.lastIndexOf('.')); + public static Resource getModelFor(String toscaType) { + Resource resource = null; + if (toscaType != null && !toscaType.isEmpty()) { + resource = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType)); } - - return modelToBeReturned; + return resource; } - public abstract boolean addResource(Resource resource); + private static Resource getModelFromPrefix(String toscaType) { + Resource resource = null; + int lastSeparator = toscaType.lastIndexOf('.'); + if (lastSeparator != -1) { + resource = getModelFor(toscaType.substring(0, lastSeparator)); + } + return resource; + } - public abstract boolean addWidget(Widget resource); + private static Optional getModelFromType(String typePrefix) { + return WidgetConfigurationUtil.createModelFromType(typePrefix); + } /** - * Gets widget version id. + * Gets the object (model) corresponding to the supplied TOSCA type information, prioritising the metadata + * information. * - * @return the widget version id + * @param toscaType + * the TOSCA type + * @param metaDataType + * the type from the TOSCA metadata + * @return the model for the type, or null */ - public String getWidgetId() { - org.onap.aai.babel.xml.generator.types.Model model = - this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class); - return Widget.getWidget(model.widget()).getId(); + public static Resource getModelFor(String toscaType, String metaDataType) { + if ("Configuration".equals(metaDataType)) { + return new Resource(Type.CONFIGURATION, true); + } else if ("CR".equals(metaDataType)) { + return new Resource(Type.CR, true); + } else { + return getModelFor(toscaType); + } } - /** - * Gets invariant id. - * - * @return the invariant id - */ - public String getWidgetInvariantId() { - org.onap.aai.babel.xml.generator.types.Model model = - this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class); - return Widget.getWidget(model.widget()).getWidgetId(); + public abstract boolean addWidget(Widget resource) throws XmlArtifactGenerationException; + + public abstract Widget.Type getWidgetType(); + + public abstract String getModelTypeName(); + + public boolean addResource(Resource resource) { + return resources.add(resource); } /** @@ -118,46 +172,17 @@ public abstract class Model { * @return the delete flag */ public boolean getDeleteFlag() { - org.onap.aai.babel.xml.generator.types.Model model = - this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class); - return model.dataDeleteFlag(); + return true; } - /** - * Gets cardinality. - * - * @return the cardinality - */ - public Cardinality getCardinality() { - org.onap.aai.babel.xml.generator.types.Model model = - this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class); - return model.cardinality(); + public String getModelDescription() { + return modelDescription; } - public abstract Widget.Type getWidgetType(); - public String getModelId() { - checkSupported(); return modelId; } - /** - * Gets model type. - * - * @return the model type - */ - public ModelType getModelType() { - if (this instanceof Service) { - return ModelType.SERVICE; - } else if (this instanceof Resource) { - return ModelType.RESOURCE; - } else if (this instanceof Widget) { - return ModelType.WIDGET; - } else { - return null; - } - } - public String getModelName() { return modelName; } @@ -167,63 +192,43 @@ public abstract class Model { } public String getModelNameVersionId() { - checkSupported(); return modelNameVersionId; } - public String getModelDescription() { - return modelDescription; + /** + * Gets widget version id. + * + * @return the widget version id + * @throws XmlArtifactGenerationException + */ + public String getWidgetId() throws XmlArtifactGenerationException { + return Widget.getWidget(getWidgetType()).getId(); + } + + /** + * Gets invariant id. + * + * @return the invariant id + * @throws XmlArtifactGenerationException + */ + public String getWidgetInvariantId() throws XmlArtifactGenerationException { + return Widget.getWidget(getWidgetType()).getWidgetId(); } /** * Populate model identification information. * - * @param modelIdentInfo the model ident info + * @param modelIdentInfo + * the model ident info */ public void populateModelIdentificationInformation(Map modelIdentInfo) { Iterator iter = modelIdentInfo.keySet().iterator(); String property; while (iter.hasNext()) { property = iter.next(); - switch (property) { - case "vfModuleModelInvariantUUID": - case "serviceInvariantUUID": - case "resourceInvariantUUID": - case "invariantUUID": - case "providing_service_invariant_uuid": - modelId = modelIdentInfo.get(property); - break; - case "vfModuleModelUUID": - case "resourceUUID": - case "serviceUUID": - case "UUID": - case "providing_service_uuid": - modelNameVersionId = modelIdentInfo.get(property); - break; - case "vfModuleModelVersion": - case "serviceVersion": - case "resourceversion": - case "version": - modelVersion = modelIdentInfo.get(property); - break; - case "vfModuleModelName": - case "serviceName": - case "resourceName": - case "name": - modelName = modelIdentInfo.get(property); - break; - case "serviceDescription": - case "resourceDescription": - case "vf_module_description": - case "description": - modelDescription = modelIdentInfo.get(property); - break; - case "providing_service_name": - modelName = modelIdentInfo.get(property); - modelDescription = modelIdentInfo.get(property); - break; - default: - break; + Optional modelIdent = ModelIdentification.getModelIdentFromProperty(property); + if (modelIdent.isPresent()) { + modelIdent.get().populate(this, modelIdentInfo.get(property)); } } } @@ -240,9 +245,9 @@ public abstract class Model { return widgets; } - private void checkSupported() { - if (this instanceof Widget) { - throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION); - } + @Override + public String toString() { + return "Model [type=" + getModelTypeName() + ", name=" + getModelName() + "]"; } + }