From 3452a390f7f04f4873a47718e2d8e558e92ce03a Mon Sep 17 00:00:00 2001 From: "mark.j.leonard" Date: Fri, 9 Nov 2018 18:03:13 +0000 Subject: Add support for ResourceInstanceGroup Refactor existing code and add a Junit test for the relevant method. Change-Id: I66703f1d2e93173df4676070faf66e289360609a Issue-ID: AAI-1884 Signed-off-by: mark.j.leonard --- .../babel/parser/ArtifactGeneratorToscaParser.java | 76 ++++++++++++++-------- 1 file changed, 50 insertions(+), 26 deletions(-) (limited to 'src/main/java') 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 e5141a5..80b75b0 100644 --- a/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java +++ b/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java @@ -31,7 +31,6 @@ import java.util.Map; import java.util.Optional; import java.util.Properties; import java.util.stream.Collectors; - import org.onap.aai.babel.logging.ApplicationMsgs; import org.onap.aai.babel.logging.LogHelper; import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil; @@ -234,13 +233,14 @@ public class ArtifactGeneratorToscaParser { * @param serviceNode * @return resources for which XML Models should be generated */ - private List processInstanceGroups(Model resourceModel, NodeTemplate serviceNode) { + List processInstanceGroups(Model resourceModel, NodeTemplate serviceNode) { List resources = new ArrayList<>(); if (csarHelper.getNodeTemplateByName(serviceNode.getName()).getSubMappingToscaTemplate() != null) { List serviceGroups = csarHelper.getGroupsOfOriginOfNodeTemplate(serviceNode); for (Group group : serviceGroups) { if (WidgetConfigurationUtil.isSupportedInstanceGroup(group.getType())) { - resources.addAll(processInstanceGroup(resourceModel, group)); + resources.addAll(processInstanceGroup(resourceModel, group.getMemberNodes(), + group.getMetadata().getAllProperties(), group.getProperties())); } } } @@ -248,42 +248,56 @@ public class ArtifactGeneratorToscaParser { } /** - * Create an Instance Group Model for the supplied Service Group and relate this to the supplied resource Model. + * Create an Instance Group Model and populate it with the supplied data. * * @param resourceModel the Resource node template Model - * @param group the Service Group + * @param memberNodes the Resources and Widgets belonging to the Group + * @param metaProperties the metadata of the Group + * @param properties the properties of the Group * @return the Instance Group and Member resource models */ - private List processInstanceGroup(Model resourceModel, Group group) { + private List processInstanceGroup(Model resourceModel, ArrayList memberNodes, + Map metaProperties, Map properties) { List resources = new ArrayList<>(); Resource groupModel = new InstanceGroup(); - groupModel.populateModelIdentificationInformation(group.getMetadata().getAllProperties()); - groupModel.populateModelIdentificationInformation(populateStringProperties(group.getProperties())); + groupModel.populateModelIdentificationInformation(metaProperties); + groupModel.populateModelIdentificationInformation(populateStringProperties(properties)); resourceModel.addResource(groupModel); resources.add(groupModel); - List members = group.getMemberNodes(); - if (members != null && !members.isEmpty()) { - for (NodeTemplate nodeTemplate : members) { - String nodeTypeName = normaliseNodeTypeName(nodeTemplate); - Model memberModel = Model.getModelFor(nodeTypeName, nodeTemplate.getMetaData().getValue("type")); - memberModel.populateModelIdentificationInformation(nodeTemplate.getMetaData().getAllProperties()); - if (memberModel instanceof Resource) { - log.debug("Generating grouped Resource " + nodeTypeName); - groupModel.addResource((Resource) memberModel); - resources.add((Resource) memberModel); - } else { - log.debug("Generating grouped Widget " + nodeTypeName); - groupModel.addWidget((Widget) memberModel); - } - } + if (memberNodes != null && !memberNodes.isEmpty()) { + resources.addAll(generateResourcesAndWidgets(memberNodes, groupModel)); } return resources; } + /** + * @param memberNodes + * @param groupModel + * @return + */ + private List generateResourcesAndWidgets(final ArrayList memberNodes, + final Resource groupModel) { + List resources = new ArrayList<>(); + for (NodeTemplate nodeTemplate : memberNodes) { + String nodeTypeName = normaliseNodeTypeName(nodeTemplate); + Model memberModel = Model.getModelFor(nodeTypeName, nodeTemplate.getMetaData().getValue("type")); + memberModel.populateModelIdentificationInformation(nodeTemplate.getMetaData().getAllProperties()); + + log.debug(String.format("Generating grouped %s (%s) from TOSCA type %s", + memberModel.getClass().getSuperclass().getSimpleName(), memberModel.getClass(), nodeTypeName)); + + addRelatedModel(groupModel, memberModel); + if (memberModel instanceof Resource) { + resources.add((Resource) memberModel); + } + } + return resources; + } + /** * Add the supplied Node Template to the Service, provided that it is a valid Resource or Widget. If the Node * Template is a Resource type, this is also recorded in the supplied nodesById Map. @@ -300,15 +314,25 @@ public class ArtifactGeneratorToscaParser { model.populateModelIdentificationInformation(nodeTemplate.getMetaData().getAllProperties()); } + addRelatedModel(service, model); if (model instanceof Resource) { nodesById.put(model.getModelNameVersionId(), nodeTypeName); - service.addResource((Resource) model); - } else { - service.addWidget((Widget) model); } } } + /** + * @param model + * @param relation + */ + private void addRelatedModel(final Model model, final Model relation) { + if (relation instanceof Resource) { + model.addResource((Resource) relation); + } else { + model.addWidget((Widget) relation); + } + } + /** * Process TOSCA Group information for VF Modules. * -- cgit 1.2.3-korg