2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Copyright (c) 2017-2019 European Software Marketing Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.aai.babel.xml.generator.api;
24 import java.io.StringWriter;
25 import java.util.Collection;
26 import java.util.List;
27 import javax.xml.bind.JAXBContext;
28 import javax.xml.bind.JAXBException;
29 import javax.xml.bind.Marshaller;
30 import org.onap.aai.babel.logging.ApplicationMsgs;
31 import org.onap.aai.babel.logging.LogHelper;
32 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
33 import org.onap.aai.babel.xml.generator.model.Model;
34 import org.onap.aai.babel.xml.generator.model.Resource;
35 import org.onap.aai.babel.xml.generator.model.Service;
36 import org.onap.aai.babel.xml.generator.model.Widget;
37 import org.onap.aai.babel.xml.generator.xsd.ModelElement;
38 import org.onap.aai.babel.xml.generator.xsd.ModelElements;
39 import org.onap.aai.babel.xml.generator.xsd.ModelVer;
40 import org.onap.aai.babel.xml.generator.xsd.ModelVers;
41 import org.onap.aai.babel.xml.generator.xsd.Relationship;
42 import org.onap.aai.babel.xml.generator.xsd.RelationshipData;
43 import org.onap.aai.babel.xml.generator.xsd.RelationshipList;
44 import org.onap.aai.cl.api.Logger;
45 import org.w3c.dom.DOMException;
48 * Generates the A&AI XML models from the Service/Resource/Widget Java models.
50 public class AaiModelGenerator {
52 private static Logger log = LogHelper.INSTANCE;
55 * Method to generate the AAI model for a Service or Resource.
58 * Java object model representing an AAI {@link Service} or {@link Resource} model
59 * @return XML representation of the model in String format
60 * @throws XmlArtifactGenerationException
62 public String generateModelFor(Model model) throws XmlArtifactGenerationException {
63 org.onap.aai.babel.xml.generator.xsd.Model aaiModel = createJaxbModel(model);
64 ModelElement baseWidget = addBaseWidgetRelation(model, aaiModel);
65 generateWidgetChildren(baseWidget, model.getWidgets());
66 return getModelAsString(aaiModel);
70 * Create a JAXB Model from the supplied Service or Resource.
73 * the Service or Resource containing the model details
74 * @return a new Model object based on the A&AI schema
76 private org.onap.aai.babel.xml.generator.xsd.Model createJaxbModel(Model model) {
77 log.debug(model.toString());
79 org.onap.aai.babel.xml.generator.xsd.Model aaiModel = new org.onap.aai.babel.xml.generator.xsd.Model();
80 aaiModel.setModelInvariantId(model.getModelId());
81 aaiModel.setModelType(model.getModelTypeName());
83 aaiModel.setModelVers(new ModelVers());
84 aaiModel.getModelVers().getModelVer().add(createModelVersion(model));
90 * Create a new JAXB object representing the model-ver complex type, and populate this with the Model Version
94 * the Service or Resource containing the version details
95 * @return a new ModelVer object
97 private ModelVer createModelVersion(Model model) {
98 ModelVer modelVer = new ModelVer();
99 modelVer.setModelDescription(model.getModelDescription());
100 modelVer.setModelName(model.getModelName());
101 modelVer.setModelVersion(model.getModelVersion());
102 modelVer.setModelVersionId(model.getModelNameVersionId());
103 modelVer.setModelElements(new ModelElements());
108 * Add base widget model element for the Service or Resource.
111 * the Service or Resource containing the Model and child resources
113 * the JAXB Model to populate
114 * @return a new ModelElement for the relationship to the base Widget
115 * @throws XmlArtifactGenerationException
117 private ModelElement addBaseWidgetRelation(Model model, org.onap.aai.babel.xml.generator.xsd.Model aaiModel)
118 throws XmlArtifactGenerationException {
119 ModelElement widgetElement = createWidgetRelationshipModelElement(model);
120 ModelVer modelVer = aaiModel.getModelVers().getModelVer().get(0);
121 modelVer.getModelElements().getModelElement().add(widgetElement);
123 // Add the child resources to the base widget model element list
124 List<ModelElement> modelElements = widgetElement.getModelElements().getModelElement();
125 for (Resource resource : model.getResources()) {
126 modelElements.add(createRelationshipModelElement(resource));
129 return widgetElement;
133 * Create a model-element complex type storing the relationship to a Service or Resource model's base Widget.
136 * the Service or Resource model storing the widget's relationship data
137 * @return a new Java object for the model-element type storing the Widget relationship
138 * @throws XmlArtifactGenerationException
140 private ModelElement createWidgetRelationshipModelElement(Model model) throws XmlArtifactGenerationException {
141 return createRelationshipModelElement(model.getDeleteFlag(), model.getWidgetId(),
142 model.getWidgetInvariantId());
146 * Create a model-element complex type storing the relationship to a Resource model.
149 * the Resource model storing the relationship data
150 * @return a new Java object for the model-element type storing the relationship
151 * @throws XmlArtifactGenerationException
153 private ModelElement createRelationshipModelElement(Resource resource) {
154 return createRelationshipModelElement(resource.getDeleteFlag(), resource.getModelNameVersionId(),
155 resource.getModelId());
159 * Create a model-element complex type storing the relationship to a Widget model.
162 * the Widget model storing the relationship data
163 * @return a new Java object for the model-element type storing the Widget relationship
165 private ModelElement createRelationshipModelElement(Widget widget) {
166 return createRelationshipModelElement(widget.getDeleteFlag(), widget.getId(), widget.getWidgetId());
170 * Method to create the <model-element></model-element> holding the relationship value for a resource/widget model.
172 * @param newDataDelFlag
173 * new-data-del-flag (mapped from boolean to the string T or F)
174 * @param modelVersionId
176 * @param modelInvariantId
178 * @return a new Java object for the model-element type storing the relationship
180 private ModelElement createRelationshipModelElement(boolean newDataDelFlag, String modelVersionId,
181 String modelInvariantId) {
182 ModelElement relationshipModelElement = new ModelElement();
183 relationshipModelElement.setNewDataDelFlag(newDataDelFlag ? "T" : "F");
184 relationshipModelElement.setCardinality("unbounded");
185 relationshipModelElement.setModelElements(new ModelElements());
186 relationshipModelElement.setRelationshipList(createModelRelationship(modelVersionId, modelInvariantId));
187 return relationshipModelElement;
191 * Create the Model Version relationship data.
193 * @param modelVersionId
195 * @param modelInvariantId
197 * @return a new RelationshipList object containing the model-ver relationships
199 private RelationshipList createModelRelationship(String modelVersionId, String modelInvariantId) {
200 Relationship relationship = new Relationship();
201 relationship.setRelatedTo("model-ver");
202 List<RelationshipData> relationshipDataList = relationship.getRelationshipData();
203 relationshipDataList.add(createRelationshipData("model-ver.model-version-id", modelVersionId));
204 relationshipDataList.add(createRelationshipData("model.model-invariant-id", modelInvariantId));
206 RelationshipList relationShipList = new RelationshipList();
207 relationShipList.getRelationship().add(relationship);
208 return relationShipList;
212 * Create a new RelationshipData element for the given key/value pair.
218 * @return a new Java object representing the relationship-data complex type
220 private RelationshipData createRelationshipData(String key, String value) {
221 RelationshipData data = new RelationshipData();
222 data.setRelationshipKey(key);
223 data.setRelationshipValue(value);
228 * Method to create the child model elements of the widget. Handles the generation of recursive child widget
232 * Reference to the parent widget model element
234 * Set of child widgets obtained from the tosca/widget definition
236 private void generateWidgetChildren(ModelElement parent, Collection<Widget> widgets) {
237 for (Widget widget : widgets) {
238 ModelElement childRelation = createRelationshipModelElement(widget);
239 parent.getModelElements().getModelElement().add(childRelation);
240 // Recursive call to create any child widgets.
241 generateWidgetChildren(childRelation, widget.getWidgets());
246 * JAXB marshalling helper method to convert the Java object model to XML String.
249 * Java Object model of a service/widget/resource
250 * @return XML representation of the Java model in String format
252 private String getModelAsString(org.onap.aai.babel.xml.generator.xsd.Model model) {
253 JAXBContext jaxbContext;
254 StringWriter modelStringWriter = new StringWriter();
256 jaxbContext = JAXBContext.newInstance(org.onap.aai.babel.xml.generator.xsd.Model.class);
257 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
258 jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
259 jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "US-ASCII");
260 jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
261 jaxbMarshaller.marshal(model, modelStringWriter);
262 } catch (JAXBException jaxbException) {
263 log.error(ApplicationMsgs.INVALID_CSAR_FILE, jaxbException);
264 throw new DOMException(DOMException.SYNTAX_ERR, jaxbException.getMessage());
267 return modelStringWriter.toString();