diff options
Diffstat (limited to 'src/main')
9 files changed, 299 insertions, 320 deletions
diff --git a/src/main/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractor.java b/src/main/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractor.java index 9836e2a..b7d79a0 100644 --- a/src/main/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractor.java +++ b/src/main/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractor.java @@ -30,7 +30,6 @@ import java.util.Map.Entry; import java.util.Objects; import java.util.function.Consumer; import java.util.stream.Collectors; - import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.time.StopWatch; import org.apache.commons.lang3.tuple.ImmutablePair; diff --git a/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java b/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java index 70e78ec..c769e9a 100644 --- a/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java +++ b/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java @@ -18,12 +18,8 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package org.onap.aai.babel.parser; -import static org.onap.aai.babel.xml.generator.data.GeneratorConstants.GENERATOR_AAI_CONFIGFILE_NOT_FOUND; -import static org.onap.aai.babel.xml.generator.data.GeneratorConstants.GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND; -import static org.onap.aai.babel.xml.generator.data.GeneratorConstants.GENERATOR_AAI_PROVIDING_SERVICE_METADATA_MISSING; -import static org.onap.aai.babel.xml.generator.data.GeneratorConstants.GENERATOR_AAI_PROVIDING_SERVICE_MISSING; +package org.onap.aai.babel.parser; import java.io.File; import java.io.FileInputStream; @@ -33,9 +29,7 @@ import java.util.List; import java.util.Map; import java.util.Properties; import java.util.stream.Collectors; - import org.onap.aai.babel.logging.LogHelper; -import org.onap.aai.babel.xml.generator.data.GeneratorConstants; import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil; import org.onap.aai.babel.xml.generator.model.AllotedResource; import org.onap.aai.babel.xml.generator.model.L3NetworkWidget; @@ -55,11 +49,26 @@ import org.onap.sdc.toscaparser.api.Property; public class ArtifactGeneratorToscaParser { - public static final String GENERATOR_AAI_ERROR_INVALID_ID = - "Invalid value for mandatory attribute <%s> in Artifact: <%s>"; + private static Logger log = LogHelper.INSTANCE; + + public static final String PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE = "artifactgenerator.config"; + + private static final String GENERATOR_AAI_CONFIGFILE_NOT_FOUND = + "Cannot generate artifacts. Artifact Generator Configuration file not found at %s"; + private static final String GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND = + "Cannot generate artifacts. artifactgenerator.config system property not configured"; + private static final String GENERATOR_AAI_PROVIDING_SERVICE_METADATA_MISSING = + "Cannot generate artifacts. Providing Service Metadata is missing for allotted resource %s"; + private static final String GENERATOR_AAI_PROVIDING_SERVICE_MISSING = + "Cannot generate artifacts. Providing Service is missing for allotted resource %s"; + + // Metadata properties + private static final String CATEGORY = "category"; private static final String ALLOTTED_RESOURCE = "Allotted Resource"; + private static final String SUBCATEGORY = "subcategory"; private static final String TUNNEL_XCONNECT = "Tunnel XConnect"; - private static Logger log = LogHelper.INSTANCE; + + private static final String VERSION = "version"; private ISdcCsarHelper csarHelper; @@ -95,7 +104,7 @@ public class ArtifactGeneratorToscaParser { */ public static void initWidgetConfiguration() throws IOException { log.debug("Getting Widget Configuration"); - String configLocation = System.getProperty(GeneratorConstants.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE); + String configLocation = System.getProperty(PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE); if (configLocation != null) { File file = new File(configLocation); if (file.exists()) { @@ -111,7 +120,7 @@ public class ArtifactGeneratorToscaParser { } /** - * Generates a Resource List using input Service Node Templates + * Generates a Resource List using input Service Node Templates. * * @param serviceNodes input Service Node Templates * @param idTypeStore ID->Type mapping @@ -254,11 +263,11 @@ public class ArtifactGeneratorToscaParser { } private boolean hasAllottedResource(Map<String, String> metadata) { - return ALLOTTED_RESOURCE.equals(metadata.get(GeneratorConstants.CATEGORY)); + return ALLOTTED_RESOURCE.equals(metadata.get(CATEGORY)); } private boolean hasSubCategoryTunnelXConnect(Map<String, String> metadata) { - return TUNNEL_XCONNECT.equals(metadata.get(GeneratorConstants.SUBCATEGORY)); + return TUNNEL_XCONNECT.equals(metadata.get(SUBCATEGORY)); } /** @@ -286,7 +295,7 @@ public class ArtifactGeneratorToscaParser { String.format(GENERATOR_AAI_PROVIDING_SERVICE_METADATA_MISSING, model.getModelId())); } Map<String, String> properties = populateStringProperties(nodeProperties); - properties.put(GeneratorConstants.VERSION, "1.0"); + properties.put(VERSION, "1.0"); resourceNode.populateModelIdentificationInformation(properties); model.addResource((Resource) resourceNode); } else if (resourceNode instanceof Resource && !(resourceNode.getWidgetType().equals(Widget.Type.L3_NET))) { diff --git a/src/main/java/org/onap/aai/babel/xml/generator/api/AaiArtifactGenerator.java b/src/main/java/org/onap/aai/babel/xml/generator/api/AaiArtifactGenerator.java index c6ca460..79c7492 100644 --- a/src/main/java/org/onap/aai/babel/xml/generator/api/AaiArtifactGenerator.java +++ b/src/main/java/org/onap/aai/babel/xml/generator/api/AaiArtifactGenerator.java @@ -1,5 +1,5 @@ /** - * ============LICENSE_START======================================================= + * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. @@ -26,7 +26,6 @@ import java.nio.file.Path; import java.util.HashMap; import java.util.List; import java.util.Map; - import org.apache.commons.io.FileUtils; import org.onap.aai.babel.logging.ApplicationMsgs; import org.onap.aai.babel.logging.LogHelper; @@ -35,7 +34,6 @@ import org.onap.aai.babel.xml.generator.data.AdditionalParams; import org.onap.aai.babel.xml.generator.data.Artifact; import org.onap.aai.babel.xml.generator.data.ArtifactType; import org.onap.aai.babel.xml.generator.data.GenerationData; -import org.onap.aai.babel.xml.generator.data.GeneratorConstants; import org.onap.aai.babel.xml.generator.data.GeneratorUtil; import org.onap.aai.babel.xml.generator.data.GroupType; import org.onap.aai.babel.xml.generator.model.Model; @@ -49,10 +47,16 @@ import org.slf4j.MDC; public class AaiArtifactGenerator implements ArtifactGenerator { - private static final String ARTIFACT_MODEL_INFO = "ARTIFACT_MODEL_INFO"; - private static Logger log = LogHelper.INSTANCE; + private static final String MDC_PARAM_MODEL_INFO = "ARTIFACT_MODEL_INFO"; + private static final String GENERATOR_AAI_GENERATED_ARTIFACT_EXTENSION = "xml"; + private static final String GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA = "Service tosca missing from list of input artifacts"; + private static final String GENERATOR_AAI_ERROR_MISSING_SERVICE_VERSION = "Cannot generate artifacts. Service version is not specified"; + private static final String GENERATOR_AAI_INVALID_SERVICE_VERSION = "Cannot generate artifacts. Service version is incorrect"; + + private AaiModelGenerator modelGenerator = new AaiModelGeneratorImpl(); + @Override public GenerationData generateArtifact(byte[] csarArchive, List<Artifact> input, Map<String, String> additionalParams) { @@ -65,15 +69,14 @@ public class AaiArtifactGenerator implements ArtifactGenerator { path = createTempFile(csarArchive); if (path != null) { - ISdcCsarHelper csarHelper = - SdcToscaParserFactory.getInstance().getSdcCsarHelper(path.toAbsolutePath().toString()); + ISdcCsarHelper csarHelper = SdcToscaParserFactory.getInstance() + .getSdcCsarHelper(path.toAbsolutePath().toString()); - List<NodeTemplate> serviceNodes = - csarHelper.getServiceNodeTemplates(); + List<NodeTemplate> serviceNodes = csarHelper.getServiceNodeTemplates(); Map<String, String> serviceMetaData = csarHelper.getServiceMetadataAllProperties(); if (serviceNodes == null) { - throw new IllegalArgumentException(GeneratorConstants.GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA); + throw new IllegalArgumentException(GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA); } // Populate basic service model metadata @@ -91,15 +94,13 @@ public class AaiArtifactGenerator implements ArtifactGenerator { // Process the resource TOSCA files List<Resource> resources = parser.processResourceToscas(serviceNodes, idTypeStore); - // Generate AAI XML service model - AaiModelGenerator modelGenerator = AaiModelGenerator.getInstance(); - MDC.put(ARTIFACT_MODEL_INFO, serviceModel.getModelName() + "," + getArtifactLabel(serviceModel)); + MDC.put(MDC_PARAM_MODEL_INFO, serviceModel.getModelName() + "," + getArtifactLabel(serviceModel)); String aaiServiceModel = modelGenerator.generateModelFor(serviceModel); generationData.add(getServiceArtifact(serviceModel, aaiServiceModel)); // Generate AAI XML resource model for (Resource res : resources) { - MDC.put(ARTIFACT_MODEL_INFO, res.getModelName() + "," + getArtifactLabel(res)); + MDC.put(MDC_PARAM_MODEL_INFO, res.getModelName() + "," + getArtifactLabel(res)); String aaiResourceModel = modelGenerator.generateModelFor(res); generationData.add(getResourceArtifact(res, aaiResourceModel)); @@ -149,7 +150,8 @@ public class AaiArtifactGenerator implements ArtifactGenerator { /** * Method to generate the artifact name for an AAI model. * - * @param model AAI artifact model + * @param model + * AAI artifact model * @return Model artifact name */ private String getArtifactName(Model model) { @@ -165,15 +167,17 @@ public class AaiArtifactGenerator implements ArtifactGenerator { artifactName.append(model.getModelVersion()); artifactName.append("."); - artifactName.append(GeneratorConstants.GENERATOR_AAI_GENERATED_ARTIFACT_EXTENSION); + artifactName.append(GENERATOR_AAI_GENERATED_ARTIFACT_EXTENSION); return artifactName.toString(); } /** * Create Resource artifact model from the AAI xml model string. * - * @param resourceModel Model of the resource artifact - * @param aaiResourceModel AAI model as string + * @param resourceModel + * Model of the resource artifact + * @param aaiResourceModel + * AAI model as string * @return Generated {@link Artifact} model for the resource */ private Artifact getResourceArtifact(Model resourceModel, String aaiResourceModel) { @@ -191,8 +195,10 @@ public class AaiArtifactGenerator implements ArtifactGenerator { /** * Create Service artifact model from the AAI xml model string. * - * @param serviceModel Model of the service artifact - * @param aaiServiceModel AAI model as string + * @param serviceModel + * Model of the service artifact + * @param aaiServiceModel + * AAI model as string * @return Generated {@link Artifact} model for the service */ private Artifact getServiceArtifact(Service serviceModel, String aaiServiceModel) { @@ -227,12 +233,12 @@ public class AaiArtifactGenerator implements ArtifactGenerator { String serviceVersion; serviceVersion = additionalParams.get(AdditionalParams.SERVICE_VERSION.getName()); if (serviceVersion == null) { - throw new IllegalArgumentException(GeneratorConstants.GENERATOR_AAI_ERROR_MISSING_SERVICE_VERSION); + throw new IllegalArgumentException(GENERATOR_AAI_ERROR_MISSING_SERVICE_VERSION); } else { String versionRegex = "^[1-9]\\d*(\\.0)$"; if (!(serviceVersion.matches(versionRegex))) { throw new IllegalArgumentException( - String.format(GeneratorConstants.GENERATOR_AAI_INVALID_SERVICE_VERSION)); + String.format(GENERATOR_AAI_INVALID_SERVICE_VERSION)); } } return serviceVersion; diff --git a/src/main/java/org/onap/aai/babel/xml/generator/api/AaiModelGenerator.java b/src/main/java/org/onap/aai/babel/xml/generator/api/AaiModelGenerator.java index 409dbc2..daf9d36 100644 --- a/src/main/java/org/onap/aai/babel/xml/generator/api/AaiModelGenerator.java +++ b/src/main/java/org/onap/aai/babel/xml/generator/api/AaiModelGenerator.java @@ -1,5 +1,5 @@ /** - * ============LICENSE_START======================================================= + * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. @@ -20,44 +20,11 @@ */ package org.onap.aai.babel.xml.generator.api; -import static org.onap.aai.babel.xml.generator.data.GeneratorConstants.ERROR_CATEGORY; -import static org.onap.aai.babel.xml.generator.data.GeneratorConstants.ERROR_CODE; -import static org.onap.aai.babel.xml.generator.data.GeneratorConstants.ERROR_DESCRIPTION; -import static org.onap.aai.babel.xml.generator.data.GeneratorConstants.GENERATOR_ERROR_CODE; -import static org.onap.aai.babel.xml.generator.data.GeneratorConstants.GENERATOR_ERROR_SERVICE_INSTANTIATION_FAILED; -import static org.onap.aai.babel.xml.generator.data.GeneratorConstants.GENERATOR_PARTNER_NAME; -import static org.onap.aai.babel.xml.generator.data.GeneratorConstants.PARTNER_NAME; - -import org.onap.aai.babel.logging.ApplicationMsgs; -import org.onap.aai.babel.logging.LogHelper; -import org.onap.aai.babel.xml.generator.logging.CategoryLogLevel; import org.onap.aai.babel.xml.generator.model.Resource; import org.onap.aai.babel.xml.generator.model.Service; -import org.onap.aai.cl.api.Logger; -import org.slf4j.MDC; public interface AaiModelGenerator { - /** - * Gets instance. - * - * @return the instance - */ - public static AaiModelGenerator getInstance() { - Logger log = LogHelper.INSTANCE; - try { - return AaiModelGenerator.class - .cast(Class.forName("org.onap.aai.babel.xml.generator.api.AaiModelGeneratorImpl").newInstance()); - } catch (Exception exception) { - MDC.put(PARTNER_NAME, GENERATOR_PARTNER_NAME); - MDC.put(ERROR_CATEGORY, CategoryLogLevel.ERROR.name()); - MDC.put(ERROR_CODE, GENERATOR_ERROR_CODE); - MDC.put(ERROR_DESCRIPTION, GENERATOR_ERROR_SERVICE_INSTANTIATION_FAILED); - log.error(ApplicationMsgs.PROCESS_REQUEST_ERROR, exception); - } - return null; - } - public String generateModelFor(Service service); public String generateModelFor(Resource resource); diff --git a/src/main/java/org/onap/aai/babel/xml/generator/data/GeneratorConstants.java b/src/main/java/org/onap/aai/babel/xml/generator/data/GeneratorConstants.java deleted file mode 100644 index b9d8cb2..0000000 --- a/src/main/java/org/onap/aai/babel/xml/generator/data/GeneratorConstants.java +++ /dev/null @@ -1,92 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2018 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ -package org.onap.aai.babel.xml.generator.data; - -public class GeneratorConstants { - - /* - * Private constructor to prevent instantiation - */ - private GeneratorConstants() { - throw new UnsupportedOperationException("This static class should not be instantiated!"); - } - - public static final String PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE = "artifactgenerator.config"; - - public static final String VERSION = "version"; - public static final String CATEGORY = "category"; - public static final String SUBCATEGORY = "subcategory"; - public static final int ID_LENGTH = 36; - - public static final String GENERATOR_AAI_GENERATED_ARTIFACT_EXTENSION = "xml"; - - // Error codes - public static final String GENERATOR_INVOCATION_ERROR_CODE = "ARTIFACT_GENERATOR_INVOCATION_ERROR"; - - // Error Constants - public static final String GENERATOR_ERROR_INVALID_CLIENT_CONFIGURATION = "Invalid Client Configuration"; - public static final String GENERATOR_ERROR_ARTIFACT_GENERATION_FAILED = - "Unable to generate artifacts for the provided input"; - public static final String GENERATOR_ERROR_SERVICE_INSTANTIATION_FAILED = - "Artifact Generation Service Instantiation failed"; - - // AAI Generator Error Messages - public static final String GENERATOR_AAI_ERROR_CHECKSUM_MISMATCH = "Checksum Mismatch for file : %s"; - public static final String GENERATOR_AAI_ERROR_INVALID_TOSCA = "Invalid format for Tosca YML : %s"; - public static final String GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION = "Operation Not Supported for Widgets"; - public static final String GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA = - "Service tosca missing from list of input artifacts"; - public static final String GENERATOR_AAI_ERROR_NULL_RESOURCE_VERSION_IN_SERVICE_TOSCA = - "Invalid Service definition mandatory attribute version missing for resource with UUID: <%s>"; - - public static final String GENERATOR_AAI_ERROR_INVALID_RESOURCE_VERSION_IN_SERVICE_TOSCA = - "Cannot generate artifacts. Invalid Resource version in Service tosca for resource with " + "UUID: " - + "<%s>"; - public static final String GENERATOR_AAI_ERROR_MISSING_RESOURCE_TOSCA = - "Cannot generate artifacts. Resource Tosca missing for resource with UUID: <%s>"; - - public static final String GENERATOR_AAI_ERROR_MISSING_SERVICE_VERSION = - "Cannot generate artifacts. Service version is not specified"; - - public static final String GENERATOR_AAI_INVALID_SERVICE_VERSION = - "Cannot generate artifacts. Service version is incorrect"; - - // Logging constants - public static final String PARTNER_NAME = "userId"; - public static final String ERROR_CATEGORY = "ErrorCategory"; - public static final String ERROR_CODE = "ErrorCode"; - public static final String ERROR_DESCRIPTION = "ErrorDescription"; - - public static final String GENERATOR_ERROR_CODE = "300F"; - public static final String GENERATOR_PARTNER_NAME = "SDC Catalog"; - - // AAI Generator Error Messages for Logging - public static final String GENERATOR_AAI_CONFIGFILE_NOT_FOUND = - "Cannot generate artifacts. Artifact Generator Configuration file not found at %s"; - public static final String GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND = - "Cannot generate artifacts. artifactgenerator.config system property not configured"; - public static final String GENERATOR_AAI_CONFIGLPROP_NOT_FOUND = - "Cannot generate artifacts. Widget configuration not found for %s"; - public static final String GENERATOR_AAI_PROVIDING_SERVICE_MISSING = - "Cannot generate artifacts. Providing Service is missing for allotted resource %s"; - public static final String GENERATOR_AAI_PROVIDING_SERVICE_METADATA_MISSING = - "Cannot generate artifacts. Providing Service Metadata is missing for allotted resource %s"; -} diff --git a/src/main/java/org/onap/aai/babel/xml/generator/model/Configuration.java b/src/main/java/org/onap/aai/babel/xml/generator/model/Configuration.java new file mode 100644 index 0000000..20dbea9 --- /dev/null +++ b/src/main/java/org/onap/aai/babel/xml/generator/model/Configuration.java @@ -0,0 +1,28 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.babel.xml.generator.model; + +import org.onap.aai.babel.xml.generator.types.Cardinality; +import org.onap.aai.babel.xml.generator.types.Model; + +@Model(widget = Widget.Type.CONFIGURATION, cardinality = Cardinality.UNBOUNDED, dataDeleteFlag = true) +public class Configuration extends Resource { +} diff --git a/src/main/java/org/onap/aai/babel/xml/generator/model/ConfigurationWidget.java b/src/main/java/org/onap/aai/babel/xml/generator/model/ConfigurationWidget.java new file mode 100644 index 0000000..7822926 --- /dev/null +++ b/src/main/java/org/onap/aai/babel/xml/generator/model/ConfigurationWidget.java @@ -0,0 +1,31 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.babel.xml.generator.model; + +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.types.ModelWidget; + +@org.onap.aai.babel.xml.generator.types.Model(widget = Widget.Type.CONFIGURATION, cardinality = Cardinality.UNBOUNDED, + dataDeleteFlag = true) +@ModelWidget(type = ModelType.WIDGET, name = "configuration") +public class ConfigurationWidget extends ResourceWidget { +} 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 0a8f0b1..956db40 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,5 +1,5 @@ /** - * ============LICENSE_START======================================================= + * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. @@ -20,69 +20,149 @@ */ 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; import java.util.Iterator; import java.util.Map; +import java.util.Optional; import java.util.Set; -import org.onap.aai.babel.xml.generator.data.GeneratorConstants; +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; public abstract class Model { - protected Set<Resource> resources = new HashSet<>(); - protected Set<Widget> widgets = new HashSet<>(); + public static final String GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION = "Operation Not Supported for Widgets"; + + private static Logger log = LogHelper.INSTANCE; + + private static Map<String, Class<? extends Model>> 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") { + @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<String, ModelIdentification> propertyToModelIdent; + private String[] keys; + + ModelIdentification(String... keys) { + this.keys = keys; + } + + static { + Map<String, ModelIdentification> mappings = new HashMap<>(); + for (ModelIdentification ident : ModelIdentification.values()) { + for (String key : ident.keys) { + mappings.put(key, ident); + } + } + propertyToModelIdent = Collections.unmodifiableMap(mappings); + } + + private static Optional<ModelIdentification> 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<Resource> resources = new HashSet<>(); + protected Set<Widget> widgets = new HashSet<>(); + /** * Gets the object (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('.')); + Model model = null; + if (toscaType != null && !toscaType.isEmpty()) { + model = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType)); } + return model; + } + + private static Model getModelFromPrefix(String toscaType) { + Model model = null; + int lastSeparator = toscaType.lastIndexOf('.'); + if (lastSeparator != -1) { + model = getModelFor(toscaType.substring(0, lastSeparator)); + } + return model; + } + private static Optional<Model> getModelFromType(String typePrefix) { + Optional<Model> modelToBeReturned = Optional.empty(); + Class<? extends Model> 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); + } + } return modelToBeReturned; } @@ -90,26 +170,17 @@ public abstract class Model { public abstract boolean addWidget(Widget resource); - /** - * Gets widget version id. - * - * @return the widget version id - */ - 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 abstract Widget.Type getWidgetType(); /** - * Gets invariant id. + * Gets cardinality. * - * @return the invariant id + * @return the cardinality */ - 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 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(); } /** @@ -118,29 +189,33 @@ 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); + org.onap.aai.babel.xml.generator.types.Model model = this.getClass() + .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class); return model.dataDeleteFlag(); } - /** - * 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; } + public String getModelName() { + return modelName; + } + + public String getModelVersion() { + return modelVersion; + } + + public String getModelNameVersionId() { + checkSupported(); + return modelNameVersionId; + } + /** * Gets model type. * @@ -158,72 +233,42 @@ public abstract class Model { } } - public String getModelName() { - return modelName; - } - - public String getModelVersion() { - return modelVersion; - } - - public String getModelNameVersionId() { - checkSupported(); - return modelNameVersionId; + /** + * Gets widget version id. + * + * @return the widget version id + */ + 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 getModelDescription() { - return modelDescription; + /** + * 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(); } /** * Populate model identification information. * - * @param modelIdentInfo the model ident info + * @param modelIdentInfo + * the model ident info */ public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) { Iterator<String> 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<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property); + if (modelIdent.isPresent()) { + modelIdent.get().populate(this, modelIdentInfo.get(property)); } } } @@ -242,7 +287,7 @@ public abstract class Model { private void checkSupported() { if (this instanceof Widget) { - throw new IllegalAccessException(GeneratorConstants.GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION); + throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION); } } } diff --git a/src/main/java/org/onap/aai/babel/xml/generator/model/Widget.java b/src/main/java/org/onap/aai/babel/xml/generator/model/Widget.java index 2a85e27..7f39438 100644 --- a/src/main/java/org/onap/aai/babel/xml/generator/model/Widget.java +++ b/src/main/java/org/onap/aai/babel/xml/generator/model/Widget.java @@ -1,5 +1,5 @@ /** - * ============LICENSE_START======================================================= + * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. @@ -20,15 +20,12 @@ */ package org.onap.aai.babel.xml.generator.model; -import static org.onap.aai.babel.xml.generator.data.GeneratorConstants.GENERATOR_AAI_CONFIGLPROP_NOT_FOUND; - import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Properties; import java.util.Set; import org.onap.aai.babel.xml.generator.data.ArtifactType; -import org.onap.aai.babel.xml.generator.data.GeneratorConstants; import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil; import org.onap.aai.babel.xml.generator.error.IllegalAccessException; import org.onap.aai.babel.xml.generator.types.ModelType; @@ -36,16 +33,22 @@ import org.onap.aai.babel.xml.generator.types.ModelWidget; public abstract class Widget extends Model { + public static final String GENERATOR_AAI_CONFIGLPROP_NOT_FOUND = "Cannot generate artifacts. Widget configuration not found for %s"; + + public enum Type { + SERVICE, VF, VFC, VSERVER, VOLUME, FLAVOR, TENANT, VOLUME_GROUP, LINT, L3_NET, VFMODULE, IMAGE, OAM_NETWORK, ALLOTTED_RESOURCE, TUNNEL_XCONNECT, CONFIGURATION; + } + private Set<String> keys = new HashSet<>(); /** * Gets widget. * - * @param type the type + * @param type + * the type * @return the widget */ public static Widget getWidget(Type type) { - switch (type) { case SERVICE: return new ServiceWidget(); @@ -77,16 +80,13 @@ public abstract class Widget extends Model { return new AllotedResourceWidget(); case TUNNEL_XCONNECT: return new TunnelXconnectWidget(); + case CONFIGURATION: + return new ConfigurationWidget(); default: return null; } } - /** - * Gets id. - * - * @return the id - */ public String getId() { Properties properties = WidgetConfigurationUtil.getConfig(); String id = properties.getProperty(ArtifactType.AAI.name() + ".model-version-id." + getName()); @@ -136,7 +136,8 @@ public abstract class Widget extends Model { /** * Equals. * - * @param obj Object + * @param obj + * Object * @return the boolean */ @Override @@ -159,7 +160,8 @@ public abstract class Widget extends Model { /** * Member of boolean. * - * @param keys the keys + * @param keys + * the keys * @return the boolean */ public boolean memberOf(List<String> keys) { @@ -172,7 +174,8 @@ public abstract class Widget extends Model { /** * All instances used boolean. * - * @param collection the collection + * @param collection + * the collection * @return the boolean */ public boolean allInstancesUsed(Set<String> collection) { @@ -183,7 +186,7 @@ public abstract class Widget extends Model { @Override public boolean addResource(Resource resource) { - throw new IllegalAccessException(GeneratorConstants.GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION); + throw new IllegalAccessException(Model.GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION); } @Override @@ -191,21 +194,4 @@ public abstract class Widget extends Model { return true; } - public enum Type { - SERVICE, - VF, - VFC, - VSERVER, - VOLUME, - FLAVOR, - TENANT, - VOLUME_GROUP, - LINT, - L3_NET, - VFMODULE, - IMAGE, - OAM_NETWORK, - ALLOTTED_RESOURCE, - TUNNEL_XCONNECT - } } |