Add 'category' of service model to Babel artifact
[aai/babel.git] / src / main / java / org / onap / aai / babel / xml / generator / api / AaiModelGenerator.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
12  *
13  *       http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.aai.babel.xml.generator.api;
24
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;
47
48 /**
49  * Generates the A&AI XML models from the Service/Resource/Widget Java models.
50  */
51 public class AaiModelGenerator {
52
53     private static Logger log = LogHelper.INSTANCE;
54
55     /**
56      * Method to generate the AAI model for a Service or Resource.
57      *
58      * @param model
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
62      */
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);
68     }
69
70     /**
71      * Create a JAXB Model from the supplied Service or Resource.
72      *
73      * @param model
74      *            the Service or Resource containing the model details
75      * @return a new Model object based on the A&AI schema
76      */
77     private org.onap.aai.babel.xml.generator.xsd.Model createJaxbModel(Model model) {
78         log.debug(model.toString());
79
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());
85         }
86         aaiModel.setModelVers(new ModelVers());
87         aaiModel.getModelVers().getModelVer().add(createModelVersion(model));
88
89         return aaiModel;
90     }
91
92     /**
93      * Create a new JAXB object representing the model-ver complex type, and populate this with the Model Version
94      * information.
95      * 
96      * @param model
97      *            the Service or Resource containing the version details
98      * @return a new ModelVer object
99      */
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());
107         return modelVer;
108     }
109
110     /**
111      * Add base widget model element for the Service or Resource.
112      * 
113      * @param model
114      *            the Service or Resource containing the Model and child resources
115      * @param aaiModel
116      *            the JAXB Model to populate
117      * @return a new ModelElement for the relationship to the base Widget
118      * @throws XmlArtifactGenerationException
119      */
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);
125
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));
130         }
131
132         return widgetElement;
133     }
134
135     /**
136      * Create a model-element complex type storing the relationship to a Service or Resource model's base Widget.
137      * 
138      * @param model
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
142      */
143     private ModelElement createWidgetRelationshipModelElement(Model model) throws XmlArtifactGenerationException {
144         return createRelationshipModelElement(model.getDeleteFlag(), model.getWidgetId(), model.getWidgetInvariantId());
145     }
146
147     /**
148      * Create a model-element complex type storing the relationship to a Resource model.
149      * 
150      * @param resource
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
154      */
155     private ModelElement createRelationshipModelElement(Resource resource) {
156         return createRelationshipModelElement(resource.getDeleteFlag(), resource.getModelNameVersionId(),
157                 resource.getModelId());
158     }
159
160     /**
161      * Create a model-element complex type storing the relationship to a Widget model.
162      * 
163      * @param widget
164      *            the Widget model storing the relationship data
165      * @return a new Java object for the model-element type storing the Widget relationship
166      */
167     private ModelElement createRelationshipModelElement(Widget widget) {
168         return createRelationshipModelElement(widget.getDeleteFlag(), widget.getId(), widget.getWidgetId());
169     }
170
171     /**
172      * Method to create the <model-element></model-element> holding the relationship value for a resource/widget model.
173      *
174      * @param newDataDelFlag
175      *            new-data-del-flag (mapped from boolean to the string T or F)
176      * @param modelVersionId
177      *            model-version-id
178      * @param modelInvariantId
179      *            model-invariant-id
180      * @return a new Java object for the model-element type storing the relationship
181      */
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;
190     }
191
192     /**
193      * Create the Model Version relationship data.
194      * 
195      * @param modelVersionId
196      *            model-version-id
197      * @param modelInvariantId
198      *            model-invariant-id
199      * @return a new RelationshipList object containing the model-ver relationships
200      */
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));
207
208         RelationshipList relationShipList = new RelationshipList();
209         relationShipList.getRelationship().add(relationship);
210         return relationShipList;
211     }
212
213     /**
214      * Create a new RelationshipData element for the given key/value pair.
215      * 
216      * @param key
217      *            relationship key
218      * @param value
219      *            relationship value
220      * @return a new Java object representing the relationship-data complex type
221      */
222     private RelationshipData createRelationshipData(String key, String value) {
223         RelationshipData data = new RelationshipData();
224         data.setRelationshipKey(key);
225         data.setRelationshipValue(value);
226         return data;
227     }
228
229     /**
230      * Method to create the child model elements of the widget. Handles the generation of recursive child widget
231      * elements (if any).
232      * 
233      * @param parent
234      *            Reference to the parent widget model element
235      * @param widgets
236      *            Set of child widgets obtained from the tosca/widget definition
237      */
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());
244         }
245     }
246
247     /**
248      * JAXB marshalling helper method to convert the Java object model to XML String.
249      *
250      * @param model
251      *            Java Object model of a service/widget/resource
252      * @return XML representation of the Java model in String format
253      */
254     private String getModelAsString(org.onap.aai.babel.xml.generator.xsd.Model model) {
255         JAXBContext jaxbContext;
256         StringWriter modelStringWriter = new StringWriter();
257         try {
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());
267         }
268
269         return modelStringWriter.toString();
270     }
271 }