From 550216df935437b1380b6d4425fd422ea7c013f5 Mon Sep 17 00:00:00 2001 From: "mark.j.leonard" Date: Mon, 25 Mar 2019 23:38:53 +0000 Subject: Refactor Widget creation methods Rename getWidget() to createWidget() to indicate that a new Object is created. Add a method to create a Widget from the String representation of its type. Change-Id: I430acf02ea67c7b730e28e79887f8fbdb18feb42 Issue-ID: AAI-2281 Signed-off-by: mark.j.leonard --- .../babel/parser/ArtifactGeneratorToscaParser.java | 25 ++++++++++++-- .../xml/generator/api/AaiArtifactGenerator.java | 22 ++++++++++++- .../generator/data/WidgetConfigurationUtil.java | 11 ++++++- .../onap/aai/babel/xml/generator/model/Model.java | 4 +-- .../aai/babel/xml/generator/model/Resource.java | 4 +-- .../onap/aai/babel/xml/generator/model/Widget.java | 38 +++++++++++++++------- 6 files changed, 83 insertions(+), 21 deletions(-) (limited to 'src/main') 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 684324a..9b66e58 100644 --- a/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java +++ b/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java @@ -209,7 +209,7 @@ public class ArtifactGeneratorToscaParser { if (relation.getModelType() == ModelType.RESOURCE) { model.addResource(relation); } else { - model.addWidget(Widget.getWidget(relation.getWidgetType())); + model.addWidget(Widget.createWidget(relation.getWidgetType())); } } @@ -228,10 +228,11 @@ public class ArtifactGeneratorToscaParser { * @param model * @param serviceNode * @throws XmlArtifactGenerationException + * if the configured widget mappings do not support the widget type of a VF Module */ public void processVfModules(List resources, Model resourceModel, NodeTemplate serviceNode) throws XmlArtifactGenerationException { - // Get the customisation UUID for each VF node and use it to get its Groups + // Get the customization UUID for each VF node and use it to get its Groups String uuid = csarHelper.getNodeTemplateCustomizationUuid(serviceNode); List serviceGroups = csarHelper.getVfModulesByVf(uuid); @@ -341,6 +342,15 @@ public class ArtifactGeneratorToscaParser { return resources; } + /** + * @param resources + * @param vfModel + * @param groupDefinition + * @param serviceNode + * @param groupModel + * @throws XmlArtifactGenerationException + * if the configured widget mappings do not support the widget type of a VF Module + */ private void processVfModule(List resources, Model vfModel, Group groupDefinition, NodeTemplate serviceNode, Resource groupModel) throws XmlArtifactGenerationException { groupModel.populateModelIdentificationInformation( @@ -355,6 +365,12 @@ public class ArtifactGeneratorToscaParser { } } + /** + * @param groupModel + * @param members + * @throws XmlArtifactGenerationException + * if the configured widget mappings do not support the widget type of a member + */ private void processVfModuleGroup(Resource groupModel, List members) throws XmlArtifactGenerationException { if (members != null && !members.isEmpty()) { @@ -371,8 +387,11 @@ public class ArtifactGeneratorToscaParser { * Process the Widget members of a VF Module Group * * @param group + * the group resource model * @param member + * the group member to process * @throws XmlArtifactGenerationException + * if the configured widget mappings do not support the widget type of the member */ private void processGroupMembers(Resource group, NodeTemplate member) throws XmlArtifactGenerationException { Resource resource = Model.getModelFor(member.getType()); @@ -385,7 +404,7 @@ public class ArtifactGeneratorToscaParser { } if (resource.getModelType() == ModelType.WIDGET) { - Widget widget = Widget.getWidget(resource.getWidgetType()); + Widget widget = Widget.createWidget(resource.getWidgetType()); widget.addKey(member.getName()); // Add the widget element encountered to the Group model group.addWidget(widget); 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 7415702..2b8b128 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 @@ -124,6 +124,7 @@ public class AaiArtifactGenerator implements ArtifactGenerator { * interface to the TOSCA parser * @return the generated Artifacts (containing XML models) * @throws XmlArtifactGenerationException + * if the configured widget mappings do not support processed widget type(s) */ public GenerationData generateAllArtifacts(final String serviceVersion, ISdcCsarHelper csarHelper) throws XmlArtifactGenerationException { @@ -182,6 +183,7 @@ public class AaiArtifactGenerator implements ArtifactGenerator { * @param serviceModel * @return the generated Models * @throws XmlArtifactGenerationException + * if the configured widget mappings do not support processed widget type(s) */ private List generateResourceModels(ISdcCsarHelper csarHelper, List serviceNodeTemplates, Service serviceModel) throws XmlArtifactGenerationException { @@ -201,6 +203,16 @@ public class AaiArtifactGenerator implements ArtifactGenerator { return resources; } + /** + * @param csarHelper + * @param serviceModel + * @param resources + * @param serviceGroups + * @param parser + * @param nodeTemplate + * @throws XmlArtifactGenerationException + * if the configured widget mappings do not support processed widget type(s) + */ private void generateModelFromNodeTemplate(ISdcCsarHelper csarHelper, Service serviceModel, List resources, final List serviceGroups, ArtifactGeneratorToscaParser parser, NodeTemplate nodeTemplate) throws XmlArtifactGenerationException { @@ -250,6 +262,14 @@ public class AaiArtifactGenerator implements ArtifactGenerator { return model; } + /** + * @param csarHelper + * @param resources + * @param parser + * @param nodeTemplate + * @throws XmlArtifactGenerationException + * if the configured widget mappings do not support processed widget type(s) + */ private void generateResourceModel(ISdcCsarHelper csarHelper, List resources, ArtifactGeneratorToscaParser parser, NodeTemplate nodeTemplate) throws XmlArtifactGenerationException { Resource resourceModel = getModelFor(parser, nodeTemplate); @@ -268,7 +288,7 @@ public class AaiArtifactGenerator implements ArtifactGenerator { } if (parser.hasSubCategoryTunnelXConnect(serviceMetadata) && parser.hasAllottedResource(serviceMetadata)) { - resourceModel.addWidget(Widget.getWidget(WidgetType.valueOf("TUNNEL_XCONNECT"))); + resourceModel.addWidget(Widget.createWidget("TUNNEL_XCONNECT")); } resources.addAll(parser.processInstanceGroups(resourceModel, nodeTemplate)); diff --git a/src/main/java/org/onap/aai/babel/xml/generator/data/WidgetConfigurationUtil.java b/src/main/java/org/onap/aai/babel/xml/generator/data/WidgetConfigurationUtil.java index 1e41f60..baecada 100644 --- a/src/main/java/org/onap/aai/babel/xml/generator/data/WidgetConfigurationUtil.java +++ b/src/main/java/org/onap/aai/babel/xml/generator/data/WidgetConfigurationUtil.java @@ -74,6 +74,15 @@ public class WidgetConfigurationUtil { return resource; } + /** + * Create a new Widget object according to the supplied Widget Type. + * + * @param widgetType + * a String identifying the type of Widget to create + * @return a new Widget object from the defined widget type, or else null + * @throws XmlArtifactGenerationException + * if there is an internal error creating the Widget because of the defined widget mappings + */ public static Widget createWidgetFromType(String widgetType) throws XmlArtifactGenerationException { Optional widget = Optional.ofNullable(typeToWidget.get(widgetType)); if (widget.isPresent()) { @@ -96,7 +105,7 @@ public class WidgetConfigurationUtil { WidgetType.validateElements(); } - public static void setWidgetMappings(List mappings) throws IOException { + public static void setWidgetMappings(List mappings) throws IOException { for (WidgetMapping mapping : mappings) { ModelType modelType = Optional.ofNullable(mapping.type).map(String::toUpperCase) .map(s -> Enums.getIfPresent(ModelType.class, s).orNull()).orElse(null); 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 b2020cd..84b5489 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 @@ -215,7 +215,7 @@ public abstract class Model { * @throws XmlArtifactGenerationException */ public String getWidgetId() throws XmlArtifactGenerationException { - return Widget.getWidget(getWidgetType()).getId(); + return Widget.createWidget(getWidgetType()).getId(); } /** @@ -225,7 +225,7 @@ public abstract class Model { * @throws XmlArtifactGenerationException */ public String getWidgetInvariantId() throws XmlArtifactGenerationException { - return Widget.getWidget(getWidgetType()).getWidgetId(); + return Widget.createWidget(getWidgetType()).getWidgetId(); } /** diff --git a/src/main/java/org/onap/aai/babel/xml/generator/model/Resource.java b/src/main/java/org/onap/aai/babel/xml/generator/model/Resource.java index 77679ac..04c69bb 100644 --- a/src/main/java/org/onap/aai/babel/xml/generator/model/Resource.java +++ b/src/main/java/org/onap/aai/babel/xml/generator/model/Resource.java @@ -165,10 +165,10 @@ public class Resource extends Model { private void addVserverWidget(Widget widget) throws XmlArtifactGenerationException { vserver = widget; if (addlintf) { - vserver.addWidget(Widget.getWidget(WidgetType.valueOf("LINT"))); + vserver.addWidget(Widget.createWidget("LINT")); } if (addvolume) { - vserver.addWidget(Widget.getWidget(WidgetType.valueOf("VOLUME"))); + vserver.addWidget(Widget.createWidget("VOLUME")); } } 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 732ec4d..78a1e8a 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 @@ -51,37 +51,51 @@ public class Widget extends Model { /** * Copy Constructor. - * + * * @param baseWidget * @throws XmlArtifactGenerationException + * if there is no widget mapping defined for any of the VSERVER child types */ public Widget(Widget baseWidget) throws XmlArtifactGenerationException { this(baseWidget.getWidgetType(), baseWidget.getName(), baseWidget.getDeleteFlag()); - if (type == WidgetType.valueOf("VSERVER")) { - widgets.add(getWidget(WidgetType.valueOf("FLAVOR"))); - widgets.add(getWidget(WidgetType.valueOf("IMAGE"))); - widgets.add(getWidget(WidgetType.valueOf("TENANT"))); - widgets.add(getWidget(WidgetType.valueOf("VFC"))); + if (this.hasWidgetType("VSERVER")) { + widgets.add(createWidget("FLAVOR")); + widgets.add(createWidget("IMAGE")); + widgets.add(createWidget("TENANT")); + widgets.add(createWidget("VFC")); } } /** - * Gets widget. + * Creates a new widget of the specified type. * - * @param typeString - * + * @param type + * String value of the Widget Type * @return a new widget of the specified type * @throws XmlArtifactGenerationException - * if there is no configuration defined for the specified type + * if the configured widget mappings do not support the specified type */ - public static Widget getWidget(WidgetType type) throws XmlArtifactGenerationException { - Widget widget = WidgetConfigurationUtil.createWidgetFromType(type.toString()); + public static Widget createWidget(String type) throws XmlArtifactGenerationException { + Widget widget = WidgetConfigurationUtil.createWidgetFromType(type); if (widget == null) { throw new XmlArtifactGenerationException("No widget type is defined for " + type); } return widget; } + /** + * Creates a new widget of the specified type. + * + * @param type + * the Widget Type + * @return a new widget of the specified type + * @throws XmlArtifactGenerationException + * if there is no configuration defined for the specified type + */ + public static Widget createWidget(WidgetType type) throws XmlArtifactGenerationException { + return createWidget(type.toString()); + } + public String getId() { String id = WidgetConfigurationUtil.getConfig() .getProperty(ArtifactType.AAI.name() + ".model-version-id." + getName()); -- cgit 1.2.3-korg