aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/babel/xml/generator/api/AaiModelGenerator.java
blob: 91eb6a4d23e897175270a5595802446c061f1d91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
 * Copyright (c) 2017-2019 European Software Marketing Ltd.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.aai.babel.xml.generator.api;

import java.io.StringWriter;
import java.util.Collection;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import org.onap.aai.babel.logging.ApplicationMsgs;
import org.onap.aai.babel.logging.LogHelper;
import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
import org.onap.aai.babel.xml.generator.model.Model;
import org.onap.aai.babel.xml.generator.model.Resource;
import org.onap.aai.babel.xml.generator.model.Service;
import org.onap.aai.babel.xml.generator.model.Widget;
import org.onap.aai.babel.xml.generator.xsd.ModelElement;
import org.onap.aai.babel.xml.generator.xsd.ModelElements;
import org.onap.aai.babel.xml.generator.xsd.ModelVer;
import org.onap.aai.babel.xml.generator.xsd.ModelVers;
import org.onap.aai.babel.xml.generator.xsd.Relationship;
import org.onap.aai.babel.xml.generator.xsd.RelationshipData;
import org.onap.aai.babel.xml.generator.xsd.RelationshipList;
import org.onap.aai.cl.api.Logger;
import org.w3c.dom.DOMException;

/**
 * Generates the A&AI XML models from the Service/Resource/Widget Java models.
 */
public class AaiModelGenerator {

    private static Logger log = LogHelper.INSTANCE;

    /**
     * Method to generate the AAI model for a Service or Resource.
     *
     * @param model
     *            Java object model representing an AAI {@link Service} or {@link Resource} model
     * @return XML representation of the model in String format
     * @throws XmlArtifactGenerationException
     */
    public String generateModelFor(Model model) throws XmlArtifactGenerationException {
        org.onap.aai.babel.xml.generator.xsd.Model aaiModel = createJaxbModel(model);
        ModelElement baseWidget = addBaseWidgetRelation(model, aaiModel);
        generateWidgetChildren(baseWidget, model.getWidgets());
        return getModelAsString(aaiModel);
    }

    /**
     * Create a JAXB Model from the supplied Service or Resource.
     *
     * @param model
     *            the Service or Resource containing the model details
     * @return a new Model object based on the A&AI schema
     */
    private org.onap.aai.babel.xml.generator.xsd.Model createJaxbModel(Model model) {
        log.debug(model.toString());

        org.onap.aai.babel.xml.generator.xsd.Model aaiModel = new org.onap.aai.babel.xml.generator.xsd.Model();
        aaiModel.setModelInvariantId(model.getModelId());
        aaiModel.setModelType(model.getModelTypeName());

        aaiModel.setModelVers(new ModelVers());
        aaiModel.getModelVers().getModelVer().add(createModelVersion(model));

        return aaiModel;
    }

    /**
     * Create a new JAXB object representing the model-ver complex type, and populate this with the Model Version
     * information.
     * 
     * @param model
     *            the Service or Resource containing the version details
     * @return a new ModelVer object
     */
    private ModelVer createModelVersion(Model model) {
        ModelVer modelVer = new ModelVer();
        modelVer.setModelDescription(model.getModelDescription());
        modelVer.setModelName(model.getModelName());
        modelVer.setModelVersion(model.getModelVersion());
        modelVer.setModelVersionId(model.getModelNameVersionId());
        modelVer.setModelElements(new ModelElements());
        return modelVer;
    }

    /**
     * Add base widget model element for the Service or Resource.
     * 
     * @param model
     *            the Service or Resource containing the Model and child resources
     * @param aaiModel
     *            the JAXB Model to populate
     * @return a new ModelElement for the relationship to the base Widget
     * @throws XmlArtifactGenerationException
     */
    private ModelElement addBaseWidgetRelation(Model model, org.onap.aai.babel.xml.generator.xsd.Model aaiModel)
            throws XmlArtifactGenerationException {
        ModelElement widgetElement = createWidgetRelationshipModelElement(model);
        ModelVer modelVer = aaiModel.getModelVers().getModelVer().get(0);
        modelVer.getModelElements().getModelElement().add(widgetElement);

        // Add the child resources to the base widget model element list
        List<ModelElement> modelElements = widgetElement.getModelElements().getModelElement();
        for (Resource resource : model.getResources()) {
            modelElements.add(createRelationshipModelElement(resource));
        }

        return widgetElement;
    }

    /**
     * Create a model-element complex type storing the relationship to a Service or Resource model's base Widget.
     * 
     * @param model
     *            the Service or Resource model storing the widget's relationship data
     * @return a new Java object for the model-element type storing the Widget relationship
     * @throws XmlArtifactGenerationException
     */
    private ModelElement createWidgetRelationshipModelElement(Model model) throws XmlArtifactGenerationException {
        return createRelationshipModelElement(model.getDeleteFlag(), model.getWidgetId(),
                model.getWidgetInvariantId());
    }

    /**
     * Create a model-element complex type storing the relationship to a Resource model.
     * 
     * @param resource
     *            the Resource model storing the relationship data
     * @return a new Java object for the model-element type storing the relationship
     * @throws XmlArtifactGenerationException
     */
    private ModelElement createRelationshipModelElement(Resource resource) {
        return createRelationshipModelElement(resource.getDeleteFlag(), resource.getModelNameVersionId(),
                resource.getModelId());
    }

    /**
     * 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();
    }
}