+ /**
+ * Create a model-element complex type storing the relationship to a Widget model.
+ *
+ * @param widget
+ * the Widget model storing the relationship data
+ * @return a new Java object for the model-element type storing the Widget relationship
+ */
+ private ModelElement createRelationshipModelElement(Widget widget) {
+ return createRelationshipModelElement(widget.getDeleteFlag(), widget.getId(), widget.getWidgetId());
+ }
+
+ /**
+ * Method to create the <model-element></model-element> holding the relationship value for a resource/widget model.
+ *
+ * @param newDataDelFlag
+ * new-data-del-flag (mapped from boolean to the string T or F)
+ * @param modelVersionId
+ * model-version-id
+ * @param modelInvariantId
+ * model-invariant-id
+ * @return a new Java object for the model-element type storing the relationship
+ */
+ private ModelElement createRelationshipModelElement(boolean newDataDelFlag, String modelVersionId,
+ String modelInvariantId) {
+ ModelElement relationshipModelElement = new ModelElement();
+ relationshipModelElement.setNewDataDelFlag(newDataDelFlag ? "T" : "F");
+ relationshipModelElement.setCardinality("unbounded");
+ relationshipModelElement.setModelElements(new ModelElements());
+ relationshipModelElement.setRelationshipList(createModelRelationship(modelVersionId, modelInvariantId));
+ return relationshipModelElement;
+ }
+
+ /**
+ * Create the Model Version relationship data.
+ *
+ * @param modelVersionId
+ * model-version-id
+ * @param modelInvariantId
+ * model-invariant-id
+ * @return a new RelationshipList object containing the model-ver relationships
+ */
+ private RelationshipList createModelRelationship(String modelVersionId, String modelInvariantId) {
+ Relationship relationship = new Relationship();
+ relationship.setRelatedTo("model-ver");
+ List<RelationshipData> relationshipDataList = relationship.getRelationshipData();
+ relationshipDataList.add(createRelationshipData("model-ver.model-version-id", modelVersionId));
+ relationshipDataList.add(createRelationshipData("model.model-invariant-id", modelInvariantId));
+
+ RelationshipList relationShipList = new RelationshipList();
+ relationShipList.getRelationship().add(relationship);
+ return relationShipList;
+ }
+
+ /**
+ * Create a new RelationshipData element for the given key/value pair.
+ *
+ * @param key
+ * relationship key
+ * @param value
+ * relationship value
+ * @return a new Java object representing the relationship-data complex type
+ */
+ private RelationshipData createRelationshipData(String key, String value) {
+ RelationshipData data = new RelationshipData();
+ data.setRelationshipKey(key);
+ data.setRelationshipValue(value);
+ return data;
+ }
+
+ /**
+ * Method to create the child model elements of the widget. Handles the generation of recursive child widget
+ * elements (if any).
+ *
+ * @param parent
+ * Reference to the parent widget model element
+ * @param widgets
+ * Set of child widgets obtained from the tosca/widget definition
+ */
+ private void generateWidgetChildren(ModelElement parent, Collection<Widget> widgets) {
+ for (Widget widget : widgets) {
+ ModelElement childRelation = createRelationshipModelElement(widget);
+ parent.getModelElements().getModelElement().add(childRelation);
+ // Recursive call to create any child widgets.
+ generateWidgetChildren(childRelation, widget.getWidgets());
+ }
+ }
+
+ /**
+ * JAXB marshalling helper method to convert the Java object model to XML String.
+ *
+ * @param model
+ * Java Object model of a service/widget/resource
+ * @return XML representation of the Java model in String format
+ */
+ private String getModelAsString(org.onap.aai.babel.xml.generator.xsd.Model model) {
+ JAXBContext jaxbContext;
+ StringWriter modelStringWriter = new StringWriter();
+ try {
+ jaxbContext = JAXBContext.newInstance(org.onap.aai.babel.xml.generator.xsd.Model.class);
+ Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
+ jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
+ jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "US-ASCII");
+ jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
+ jaxbMarshaller.marshal(model, modelStringWriter);
+ } catch (JAXBException jaxbException) {
+ log.error(ApplicationMsgs.INVALID_CSAR_FILE, jaxbException);
+ throw new DOMException(DOMException.SYNTAX_ERR, jaxbException.getMessage());
+ }
+
+ return modelStringWriter.toString();
+ }