Re-implement model type value for Resource Mapping
[aai/babel.git] / src / main / java / org / onap / aai / babel / xml / generator / api / AaiArtifactGenerator.java
index fc5acda..8742de3 100644 (file)
@@ -33,6 +33,7 @@ import org.apache.commons.lang3.StringUtils;
 import org.onap.aai.babel.logging.ApplicationMsgs;
 import org.onap.aai.babel.logging.LogHelper;
 import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser;
+import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
 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;
@@ -43,10 +44,12 @@ import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
 import org.onap.aai.babel.xml.generator.model.Model;
 import org.onap.aai.babel.xml.generator.model.Resource;
 import org.onap.aai.babel.xml.generator.model.Service;
-import org.onap.aai.babel.xml.generator.model.TunnelXconnectWidget;
+import org.onap.aai.babel.xml.generator.model.Widget;
 import org.onap.aai.babel.xml.generator.model.Widget.Type;
+import org.onap.aai.babel.xml.generator.types.ModelType;
 import org.onap.aai.cl.api.Logger;
 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
+import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
 import org.onap.sdc.toscaparser.api.Group;
 import org.onap.sdc.toscaparser.api.NodeTemplate;
@@ -71,6 +74,21 @@ public class AaiArtifactGenerator implements ArtifactGenerator {
     @Override
     public GenerationData generateArtifact(byte[] csarArchive, List<Artifact> input,
             Map<String, String> additionalParams) {
+        String configLocation = System.getProperty(ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE);
+        if (configLocation == null) {
+            throw new IllegalArgumentException(
+                    String.format(ArtifactGeneratorToscaParser.GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND,
+                            ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE));
+        }
+
+        try {
+            ArtifactGeneratorToscaParser.initWidgetConfiguration();
+            ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(configLocation);
+        } catch (IOException e) {
+            log.error(ApplicationMsgs.LOAD_PROPERTIES, e, configLocation);
+            return createErrorData(e);
+        }
+
         Path csarPath;
 
         try {
@@ -81,12 +99,10 @@ public class AaiArtifactGenerator implements ArtifactGenerator {
         }
 
         try {
-            ArtifactGeneratorToscaParser.initWidgetConfiguration();
-            ArtifactGeneratorToscaParser.initGroupFilterConfiguration();
             ISdcCsarHelper csarHelper =
                     SdcToscaParserFactory.getInstance().getSdcCsarHelper(csarPath.toAbsolutePath().toString());
             return generateAllArtifacts(validateServiceVersion(additionalParams), csarHelper);
-        } catch (Exception e) {
+        } catch (SdcToscaParserException | XmlArtifactGenerationException e) {
             log.error(ApplicationMsgs.INVALID_CSAR_FILE, e);
             return createErrorData(e);
         } finally {
@@ -107,8 +123,10 @@ public class AaiArtifactGenerator implements ArtifactGenerator {
      * @param csarHelper
      *            interface to the TOSCA parser
      * @return the generated Artifacts (containing XML models)
+     * @throws XmlArtifactGenerationException
      */
-    private GenerationData generateAllArtifacts(final String serviceVersion, ISdcCsarHelper csarHelper) {
+    private GenerationData generateAllArtifacts(final String serviceVersion, ISdcCsarHelper csarHelper)
+            throws XmlArtifactGenerationException {
         List<NodeTemplate> serviceNodeTemplates = csarHelper.getServiceNodeTemplates();
         if (serviceNodeTemplates == null) {
             throw new IllegalArgumentException(GENERATOR_AAI_ERROR_MISSING_SERVICE_TOSCA);
@@ -163,9 +181,10 @@ public class AaiArtifactGenerator implements ArtifactGenerator {
      * @param serviceNodeTemplates
      * @param serviceModel
      * @return the generated Models
+     * @throws XmlArtifactGenerationException
      */
     private List<Resource> generateResourceModels(ISdcCsarHelper csarHelper, List<NodeTemplate> serviceNodeTemplates,
-            Service serviceModel) {
+            Service serviceModel) throws XmlArtifactGenerationException {
         final List<Group> serviceGroups = csarHelper.getGroupsOfTopologyTemplate();
         final ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(csarHelper);
 
@@ -184,7 +203,7 @@ public class AaiArtifactGenerator implements ArtifactGenerator {
 
     private void generateModelFromNodeTemplate(ISdcCsarHelper csarHelper, Service serviceModel,
             List<Resource> resources, final List<Group> serviceGroups, ArtifactGeneratorToscaParser parser,
-            NodeTemplate nodeTemplate) {
+            NodeTemplate nodeTemplate) throws XmlArtifactGenerationException {
         Resource model = getModelFor(parser, nodeTemplate);
 
         if (model != null) {
@@ -193,15 +212,16 @@ public class AaiArtifactGenerator implements ArtifactGenerator {
             }
 
             parser.addRelatedModel(serviceModel, model);
-            if (model.isResource()) {
+            if (model.getModelType() == ModelType.RESOURCE) {
                 generateResourceModel(csarHelper, resources, parser, nodeTemplate);
             }
         } else {
             for (Group group : serviceGroups) {
-                if (group.getMembers().contains(nodeTemplate.getName())
+                ArrayList<String> members = group.getMembers();
+                if (members != null && members.contains(nodeTemplate.getName())
                         && WidgetConfigurationUtil.isSupportedInstanceGroup(group.getType())) {
                     log.debug(String.format("Adding group %s (type %s) with members %s", group.getName(),
-                            group.getType(), group.getMembers()));
+                            group.getType(), members));
 
                     Resource groupModel = parser.createInstanceGroupModel(
                             parser.mergeProperties(group.getMetadata().getAllProperties(), group.getProperties()));
@@ -219,9 +239,10 @@ public class AaiArtifactGenerator implements ArtifactGenerator {
 
         Resource model = Model.getModelFor(nodeTypeName, nodeTemplate.getMetaData().getValue("type"));
 
-        Metadata metadata = nodeTemplate.getMetaData();
-        if (metadata != null && parser.hasAllottedResource(metadata.getAllProperties())) {
-            if (model.getWidgetType() == Type.VF) {
+        if (model != null) {
+            Metadata metadata = nodeTemplate.getMetaData();
+            if (metadata != null && parser.hasAllottedResource(metadata.getAllProperties())
+                    && model.getWidgetType() == Type.VF) {
                 model = new Resource(Type.ALLOTTED_RESOURCE, true);
             }
         }
@@ -230,8 +251,13 @@ public class AaiArtifactGenerator implements ArtifactGenerator {
     }
 
     private void generateResourceModel(ISdcCsarHelper csarHelper, List<Resource> resources,
-            ArtifactGeneratorToscaParser parser, NodeTemplate nodeTemplate) {
+            ArtifactGeneratorToscaParser parser, NodeTemplate nodeTemplate) throws XmlArtifactGenerationException {
         Resource resourceModel = getModelFor(parser, nodeTemplate);
+        if (resourceModel == null) {
+            log.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Could not generate resource model");
+            return;
+        }
+
         Map<String, String> serviceMetadata = nodeTemplate.getMetaData().getAllProperties();
         resourceModel.populateModelIdentificationInformation(serviceMetadata);
 
@@ -242,7 +268,7 @@ public class AaiArtifactGenerator implements ArtifactGenerator {
         }
 
         if (parser.hasSubCategoryTunnelXConnect(serviceMetadata) && parser.hasAllottedResource(serviceMetadata)) {
-            resourceModel.addWidget(new TunnelXconnectWidget());
+            resourceModel.addWidget(Widget.getWidget(Type.TUNNEL_XCONNECT));
         }
 
         resources.addAll(parser.processInstanceGroups(resourceModel, nodeTemplate));
@@ -252,8 +278,10 @@ public class AaiArtifactGenerator implements ArtifactGenerator {
     /**
      * @param generationData
      * @param resource
+     * @throws XmlArtifactGenerationException
      */
-    private void generateResourceArtifact(GenerationData generationData, Resource resource) {
+    private void generateResourceArtifact(GenerationData generationData, Resource resource)
+            throws XmlArtifactGenerationException {
         if (!isContained(generationData, getArtifactName(resource))) {
             log.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Generating resource model");
             generationData.add(getResourceArtifact(resource, modelGenerator.generateModelFor(resource)));
@@ -276,7 +304,7 @@ public class AaiArtifactGenerator implements ArtifactGenerator {
     private String getArtifactLabel(Model model) {
         StringBuilder artifactName = new StringBuilder(ArtifactType.AAI.name());
         artifactName.append("-");
-        artifactName.append(model.getModelType().name().toLowerCase());
+        artifactName.append(model.getModelTypeName());
         artifactName.append("-");
         artifactName.append(hashCodeUuId(model.getModelNameVersionId()));
         return (artifactName.toString()).replaceAll("[^a-zA-Z0-9 +]+", "-");
@@ -297,7 +325,7 @@ public class AaiArtifactGenerator implements ArtifactGenerator {
         artifactName.append(truncatedArtifactName);
 
         artifactName.append("-");
-        artifactName.append(model.getModelType().name().toLowerCase());
+        artifactName.append(model.getModelTypeName());
         artifactName.append("-");
         artifactName.append(model.getModelVersion());
 
@@ -315,7 +343,7 @@ public class AaiArtifactGenerator implements ArtifactGenerator {
      *            AAI model as string
      * @return Generated {@link Artifact} model for the resource
      */
-    private Artifact getResourceArtifact(Model resourceModel, String aaiResourceModel) {
+    private Artifact getResourceArtifact(Resource resourceModel, String aaiResourceModel) {
         final String resourceArtifactLabel = getArtifactLabel(resourceModel);
         MDC.put(MDC_PARAM_MODEL_INFO, resourceModel.getModelName() + "," + resourceArtifactLabel);
         final byte[] bytes = aaiResourceModel.getBytes();
@@ -324,7 +352,7 @@ public class AaiArtifactGenerator implements ArtifactGenerator {
                 GeneratorUtil.checkSum(bytes), GeneratorUtil.encode(bytes));
         artifact.setName(getArtifactName(resourceModel));
         artifact.setLabel(resourceArtifactLabel);
-        artifact.setDescription(ArtifactGeneratorToscaParser.getArtifactDescription(resourceModel));
+        artifact.setDescription("AAI Resource Model");
         return artifact;
     }
 
@@ -354,8 +382,7 @@ public class AaiArtifactGenerator implements ArtifactGenerator {
         String serviceArtifactLabel = getArtifactLabel(serviceModel);
         artifact.setName(serviceArtifactName);
         artifact.setLabel(serviceArtifactLabel);
-        String description = ArtifactGeneratorToscaParser.getArtifactDescription(serviceModel);
-        artifact.setDescription(description);
+        artifact.setDescription("AAI Service Model");
         return artifact;
     }