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 * Copyright (C) 2019-2020 Wipro Limited.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.aai.babel.xml.generator.api;
25 import java.io.StringWriter;
26 import java.util.Collection;
27 import java.util.List;
28 import javax.xml.bind.JAXBContext;
29 import javax.xml.bind.JAXBException;
30 import javax.xml.bind.Marshaller;
31 import org.onap.aai.babel.logging.ApplicationMsgs;
32 import org.onap.aai.babel.logging.LogHelper;
33 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
34 import org.onap.aai.babel.xml.generator.model.Model;
35 import org.onap.aai.babel.xml.generator.model.Resource;
36 import org.onap.aai.babel.xml.generator.model.Service;
37 import org.onap.aai.babel.xml.generator.model.Widget;
38 import org.onap.aai.babel.xml.generator.xsd.ModelElement;
39 import org.onap.aai.babel.xml.generator.xsd.ModelElements;
40 import org.onap.aai.babel.xml.generator.xsd.ModelVer;
41 import org.onap.aai.babel.xml.generator.xsd.ModelVers;
42 import org.onap.aai.babel.xml.generator.xsd.Relationship;
43 import org.onap.aai.babel.xml.generator.xsd.RelationshipData;
44 import org.onap.aai.babel.xml.generator.xsd.RelationshipList;
45 import org.onap.aai.cl.api.Logger;
46 import org.w3c.dom.DOMException;
49 * Generates the A&AI XML models from the Service/Resource/Widget Java models.
51 public class AaiModelGenerator {
53 private static Logger log = LogHelper.INSTANCE;
56 * Method to generate the AAI model for a Service or Resource.
59 * Java object model representing an AAI {@link Service} or {@link Resource} model
60 * @return XML representation of the model in String format
61 * @throws XmlArtifactGenerationException
63 public String generateModelFor(Model model) throws XmlArtifactGenerationException {
64 org.onap.aai.babel.xml.generator.xsd.Model aaiModel = createJaxbModel(model);
65 ModelElement baseWidget = addBaseWidgetRelation(model, aaiModel);
66 generateWidgetChildren(baseWidget, model.getWidgets());
67 return getModelAsString(aaiModel);
71 * Create a JAXB Model from the supplied Service or Resource.
74 * the Service or Resource containing the model details
75 * @return a new Model object based on the A&AI schema
77 private org.onap.aai.babel.xml.generator.xsd.Model createJaxbModel(Model model) {
78 log.debug(model.toString());
80 org.onap.aai.babel.xml.generator.xsd.Model aaiModel = new org.onap.aai.babel.xml.generator.xsd.Model();
81 aaiModel.setModelInvariantId(model.getModelId());
82 aaiModel.setModelType(model.getModelTypeName());
83 if (model.getModelTypeName() == "service"){
84 aaiModel.setModelRole(model.getCategory());
86 aaiModel.setModelVers(new ModelVers());
87 aaiModel.getModelVers().getModelVer().add(createModelVersion(model));
93 * Create a new JAXB object representing the model-ver complex type, and populate this with the Model Version
97 * the Service or Resource containing the version details
98 * @return a new ModelVer object
100 private ModelVer createModelVersion(Model model) {
101 ModelVer modelVer = new ModelVer();
102 modelVer.setModelDescription(model.getModelDescription());
103 modelVer.setModelName(model.getModelName());
104 modelVer.setModelVersion(model.getModelVersion());
105 modelVer.setModelVersionId(model.getModelNameVersionId());
106 modelVer.setModelElements(new ModelElements());
111 * Add base widget model element for the Service or Resource.
114 * the Service or Resource containing the Model and child resources
116 * the JAXB Model to populate
117 * @return a new ModelElement for the relationship to the base Widget
118 * @throws XmlArtifactGenerationException
120 private ModelElement addBaseWidgetRelation(Model model, org.onap.aai.babel.xml.generator.xsd.Model aaiModel)
121 throws XmlArtifactGenerationException {
122 ModelElement widgetElement = createWidgetRelationshipModelElement(model);
123 ModelVer modelVer = aaiModel.getModelVers().getModelVer().get(0);
124 modelVer.getModelElements().getModelElement().add(widgetElement);
126 // Add the child resources to the base widget model element list
127 List<ModelElement> modelElements = widgetElement.getModelElements().getModelElement();
128 for (Resource resource : model.getResources()) {
129 modelElements.add(createRelationshipModelElement(resource));
132 return widgetElement;
136 * Create a model-element complex type storing the relationship to a Service or Resource model's base Widget.
139 * the Service or Resource model storing the widget's relationship data
140 * @return a new Java object for the model-element type storing the Widget relationship
141 * @throws XmlArtifactGenerationException
143 private ModelElement createWidgetRelationshipModelElement(Model model) throws XmlArtifactGenerationException {
144 return createRelationshipModelElement(model.getDeleteFlag(), model.getWidgetId(), model.getWidgetInvariantId());
148 * Create a model-element complex type storing the relationship to a Resource model.
151 * the Resource model storing the relationship data
152 * @return a new Java object for the model-element type storing the relationship
153 * @throws XmlArtifactGenerationException
155 private ModelElement createRelationshipModelElement(Resource resource) {
156 return createRelationshipModelElement(resource.getDeleteFlag(), resource.getModelNameVersionId(),
157 resource.getModelId());
161 * Create a model-element complex type storing the relationship to a Widget model.
164 * the Widget model storing the relationship data
165 * @return a new Java object for the model-element type storing the Widget relationship
167 private ModelElement createRelationshipModelElement(Widget widget) {
168 return createRelationshipModelElement(widget.getDeleteFlag(), widget.getId(), widget.getWidgetId());
172 * Method to create the <model-element></model-element> holding the relationship value for a resource/widget model.
174 * @param newDataDelFlag
175 * new-data-del-flag (mapped from boolean to the string T or F)
176 * @param modelVersionId
178 * @param modelInvariantId
180 * @return a new Java object for the model-element type storing the relationship
182 private ModelElement createRelationshipModelElement(boolean newDataDelFlag, String modelVersionId,
183 String modelInvariantId) {
184 ModelElement relationshipModelElement = new ModelElement();
185 relationshipModelElement.setNewDataDelFlag(newDataDelFlag ? "T" : "F");
186 relationshipModelElement.setCardinality("unbounded");
187 relationshipModelElement.setModelElements(new ModelElements());
188 relationshipModelElement.setRelationshipList(createModelRelationship(modelVersionId, modelInvariantId));
189 return relationshipModelElement;
193 * Create the Model Version relationship data.
195 * @param modelVersionId
197 * @param modelInvariantId
199 * @return a new RelationshipList object containing the model-ver relationships
201 private RelationshipList createModelRelationship(String modelVersionId, String modelInvariantId) {
202 Relationship relationship = new Relationship();
203 relationship.setRelatedTo("model-ver");
204 List<RelationshipData> relationshipDataList = relationship.getRelationshipData();
205 relationshipDataList.add(createRelationshipData("model-ver.model-version-id", modelVersionId));
206 relationshipDataList.add(createRelationshipData("model.model-invariant-id", modelInvariantId));
208 RelationshipList relationShipList = new RelationshipList();
209 relationShipList.getRelationship().add(relationship);
210 return relationShipList;
214 * Create a new RelationshipData element for the given key/value pair.
220 * @return a new Java object representing the relationship-data complex type
222 private RelationshipData createRelationshipData(String key, String value) {
223 RelationshipData data = new RelationshipData();
224 data.setRelationshipKey(key);
225 data.setRelationshipValue(value);
230 * Method to create the child model elements of the widget. Handles the generation of recursive child widget
234 * Reference to the parent widget model element
236 * Set of child widgets obtained from the tosca/widget definition
238 private void generateWidgetChildren(ModelElement parent, Collection<Widget> widgets) {
239 for (Widget widget : widgets) {
240 ModelElement childRelation = createRelationshipModelElement(widget);
241 parent.getModelElements().getModelElement().add(childRelation);
242 // Recursive call to create any child widgets.
243 generateWidgetChildren(childRelation, widget.getWidgets());
248 * JAXB marshalling helper method to convert the Java object model to XML String.
251 * Java Object model of a service/widget/resource
252 * @return XML representation of the Java model in String format
254 private String getModelAsString(org.onap.aai.babel.xml.generator.xsd.Model model) {
255 JAXBContext jaxbContext;
256 StringWriter modelStringWriter = new StringWriter();
258 jaxbContext = JAXBContext.newInstance(org.onap.aai.babel.xml.generator.xsd.Model.class);
259 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
260 jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
261 jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "US-ASCII");
262 jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
263 jaxbMarshaller.marshal(model, modelStringWriter);
264 } catch (JAXBException jaxbException) {
265 log.error(ApplicationMsgs.INVALID_CSAR_FILE, jaxbException);
266 throw new DOMException(DOMException.SYNTAX_ERR, jaxbException.getMessage());
269 return modelStringWriter.toString();