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=b2020cdea69a781ddb6d54fe7c54d0f16277d01f;hb=6c585913c11b2e1973bfcd0d7671d4114e1c3e66;hp=956db40f913fb35e5c50067d1735d74ff8ce837a;hpb=79e8066769162f4b2b14c32f500a89ccb5869fa3;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 956db40..b2020cd 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 @@ -2,8 +2,8 @@ * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2018 European Software Marketing Ltd. + * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (c) 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,9 +18,9 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.aai.babel.xml.generator.model; -import java.lang.reflect.InvocationTargetException; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -28,34 +28,13 @@ import java.util.Iterator; import java.util.Map; import java.util.Optional; import java.util.Set; -import org.onap.aai.babel.logging.ApplicationMsgs; -import org.onap.aai.babel.logging.LogHelper; -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.cl.api.Logger; +import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException; +import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil; public abstract class Model { public static final String GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION = "Operation Not Supported for Widgets"; - private static Logger log = LogHelper.INSTANCE; - - private static Map> typeToModel = new HashMap<>(); - static { - typeToModel.put("org.openecomp.resource.vf.allottedResource", AllotedResource.class); - typeToModel.put("org.openecomp.resource.vfc.AllottedResource", ProvidingService.class); - typeToModel.put("org.openecomp.resource.vfc", VServerWidget.class); - typeToModel.put("org.openecomp.resource.cp", LIntfWidget.class); - typeToModel.put("org.openecomp.cp", LIntfWidget.class); - typeToModel.put("org.openecomp.resource.vl", L3Network.class); - typeToModel.put("org.openecomp.resource.vf", VirtualFunction.class); - typeToModel.put("org.openecomp.groups.vfmodule", VfModule.class); - typeToModel.put("org.openecomp.groups.VfModule", VfModule.class); - typeToModel.put("org.openecomp.resource.vfc.nodes.heat.cinder", VolumeWidget.class); - typeToModel.put("org.openecomp.nodes.PortMirroringConfiguration", Configuration.class); - } - private enum ModelIdentification { ID("vfModuleModelInvariantUUID", "serviceInvariantUUID", "resourceInvariantUUID", "invariantUUID", "providing_service_invariant_uuid") { @@ -119,9 +98,9 @@ public abstract class Model { public abstract void populate(Model model, String value); } - private String modelId; + private String modelId; // model-invariant-id private String modelName; - private String modelNameVersionId; + private String modelNameVersionId; // model-version-id private String modelVersion; private String modelDescription; @@ -129,58 +108,75 @@ public abstract class Model { 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 * @return the model for the type, or null */ - public static Model getModelFor(String toscaType) { - Model model = null; + public static Resource getModelFor(String toscaType) { + Resource resource = null; if (toscaType != null && !toscaType.isEmpty()) { - model = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType)); + resource = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType)); } - return model; + return resource; } - private static Model getModelFromPrefix(String toscaType) { - Model model = null; + private static Resource getModelFromPrefix(String toscaType) { + Resource resource = null; int lastSeparator = toscaType.lastIndexOf('.'); if (lastSeparator != -1) { - model = getModelFor(toscaType.substring(0, lastSeparator)); + resource = getModelFor(toscaType.substring(0, lastSeparator)); } - return model; + return resource; } - private static Optional getModelFromType(String typePrefix) { - Optional modelToBeReturned = Optional.empty(); - Class clazz = typeToModel.get(typePrefix); - if (clazz != null) { - try { - modelToBeReturned = Optional.ofNullable(clazz.getConstructor().newInstance()); - } catch (InstantiationException | java.lang.IllegalAccessException | IllegalArgumentException - | InvocationTargetException | NoSuchMethodException | SecurityException e) { - log.error(ApplicationMsgs.INVALID_CSAR_FILE, e); - } + private static Optional getModelFromType(String typePrefix) { + return WidgetConfigurationUtil.createModelFromType(typePrefix); + } + + /** + * Gets the object (model) corresponding to the supplied TOSCA type information, prioritising the metadata + * information. + * + * @param toscaType + * the TOSCA type + * @param metaDataType + * the type from the TOSCA metadata + * @return the model for the type, or null + */ + public static Resource getModelFor(String toscaType, String metaDataType) { + if ("Configuration".equals(metaDataType)) { + return new Resource(WidgetType.valueOf("CONFIGURATION"), true); + } else if ("CR".equals(metaDataType)) { + return new Resource(WidgetType.valueOf("CR"), true); + } else { + return getModelFor(toscaType); } - return modelToBeReturned; } - public abstract boolean addResource(Resource resource); + public abstract boolean addWidget(Widget resource) throws XmlArtifactGenerationException; - public abstract boolean addWidget(Widget resource); + /** + * @return the Widget Type of this model. + */ + public abstract WidgetType getWidgetType(); - public abstract Widget.Type getWidgetType(); + public abstract String getModelTypeName(); /** - * Gets cardinality. + * Check whether the model's Widget Type matches the supplied type. * - * @return the cardinality + * @param type + * the Widget Type to compare + * @return true if the Widget Type of this model matches the supplied type */ - 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 boolean hasWidgetType(String type) { + return getWidgetType() == WidgetType.valueOf(type); + } + + public boolean addResource(Resource resource) { + return resources.add(resource); } /** @@ -189,9 +185,7 @@ 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; } public String getModelDescription() { @@ -199,7 +193,6 @@ public abstract class Model { } public String getModelId() { - checkSupported(); return modelId; } @@ -212,47 +205,27 @@ public abstract class Model { } public String getModelNameVersionId() { - checkSupported(); return modelNameVersionId; } - /** - * 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; - } - } - /** * Gets widget version id. * * @return the widget version id + * @throws XmlArtifactGenerationException */ - 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 String getWidgetId() throws XmlArtifactGenerationException { + return Widget.getWidget(getWidgetType()).getId(); } /** * Gets invariant id. * * @return the invariant id + * @throws XmlArtifactGenerationException */ - 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 String getWidgetInvariantId() throws XmlArtifactGenerationException { + return Widget.getWidget(getWidgetType()).getWidgetId(); } /** @@ -262,6 +235,9 @@ public abstract class Model { * the model ident info */ public void populateModelIdentificationInformation(Map modelIdentInfo) { + if (modelIdentInfo == null) { + return; + } Iterator iter = modelIdentInfo.keySet().iterator(); String property; while (iter.hasNext()) { @@ -285,9 +261,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() + "]"; } + }