summaryrefslogtreecommitdiffstats
path: root/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/handling/ApexModelReader.java
blob: 7e136f6b3d2166173f751aa3a17f5655ea9415b6 (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
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2019-2021 Nordix Foundation.
 *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.model.basicmodel.handling;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.regex.Pattern;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.SchemaFactory;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import org.eclipse.persistence.jaxb.MarshallerProperties;
import org.eclipse.persistence.oxm.MediaType;
import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.common.utils.resources.TextFileUtils;
import org.onap.policy.common.utils.validation.Assertions;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;

/**
 * This class reads an Apex concept from an XML file into a Java Apex Concept {@link AxConcept}.
 *
 * @author Liam Fallon (liam.fallon@ericsson.com)
 * @param <C> the type of Apex concept to read, must be a sub class of {@link AxConcept}
 */
public class ApexModelReader<C extends AxConcept> {
    // Get a reference to the logger
    private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelReader.class);

    // Regular expressions for checking input types
    // (starts with <?xml...>
    private static final String XML_INPUT_TYPE_REGEXP = "^\\s*<\\?xml.*>\\s*";
    // starts with some kind of bracket [ or (
    private static final String JSON_INPUT_TYPE_REGEXP = "^\\s*[\\(\\{\\[][\\s\\S]*[\\)\\}\\]]";
    // or {, then has something, then has
    // and has a close bracket

    //  The root class of the concept we are reading
    private final Class<C> rootConceptClass;

    // The unmarshaller for the Apex concepts
    private Unmarshaller unmarshaller = null;

    // All read concepts are validated after reading if this flag is set
    private boolean validateFlag = true;

    /**
     * Constructor, initiates the reader with validation on.
     *
     * @param rootConceptClass the root concept class for concept reading
     * @throws ApexModelException the apex concept reader exception
     */
    public ApexModelReader(final Class<C> rootConceptClass) throws ApexModelException {
        // Save the root concept class
        this.rootConceptClass = rootConceptClass;

        try {
            final var jaxbContext = JAXBContextFactory.createContext(new Class[] {rootConceptClass}, null);

            // Set up the unmarshaller to carry out validation
            unmarshaller = jaxbContext.createUnmarshaller();
            unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
        } catch (final JAXBException e) {
            LOGGER.error("Unable to set JAXB context", e);
            throw new ApexModelException("Unable to set JAXB context", e);
        }
    }

    /**
     * Constructor, initiates the reader.
     *
     * @param rootConceptClass the root concept class for concept reading
     * @param validate whether to perform validation by default
     * @throws ApexModelException the apex concept reader exception
     */
    public ApexModelReader(final Class<C> rootConceptClass, final boolean validate) throws ApexModelException {
        this(rootConceptClass);
        this.validateFlag = validate;
    }

    /**
     * Set the schema to use for reading XML files.
     *
     * @param schemaFileName the schema file to use
     * @throws ApexModelException if the schema cannot be set
     */
    public void setSchema(final String schemaFileName) throws ApexModelException {
        // Has a schema been set
        if (schemaFileName != null) {
            try {
                // Set the concept schema
                final var schemaUrl = ResourceUtils.getUrlResource(schemaFileName);
                final var apexConceptSchema =
                        SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaUrl);
                unmarshaller.setSchema(apexConceptSchema);
            } catch (final Exception e) {
                LOGGER.error("Unable to load schema ", e);
                throw new ApexModelException("Unable to load schema", e);
            }
        } else {
            // Clear the schema
            unmarshaller.setSchema(null);
        }
    }

    /**
     * This method checks the specified Apex concept XML file and reads it into an Apex concept.
     *
     * @param apexConceptStream the apex concept stream
     * @return the Apex concept
     * @throws ApexModelException on reading exceptions
     */
    public C read(final InputStream apexConceptStream) throws ApexModelException {
        Assertions.argumentNotNull(apexConceptStream, "concept stream may not be null");

        return read(new BufferedReader(new InputStreamReader(apexConceptStream)));
    }

    /**
     * This method reads the specified Apex reader into an Apex concept.
     *
     * @param apexConceptReader the apex concept reader
     * @return the Apex concept
     * @throws ApexModelException on reading exceptions
     */
    public C read(final BufferedReader apexConceptReader) throws ApexModelException {
        Assertions.argumentNotNull(apexConceptReader, "concept reader may not be null");

        LOGGER.entry("reading Apex concept into a String . . .");

        // Get the Apex concept as a string
        String apexConceptString = null;
        try {
            apexConceptString = TextFileUtils.getReaderAsString(apexConceptReader).trim();
        } catch (final IOException e) {
            throw new ApexModelException("Unable to read Apex concept ", e);
        }

        return read(apexConceptString);
    }

    /**
     * This method reads the specified Apex string into an Apex concept.
     *
     * @param apexConceptString the apex concept as a string
     * @return the Apex concept
     * @throws ApexModelException on reading exceptions
     */
    public C read(final String apexConceptString) throws ApexModelException {
        Assertions.argumentNotNull(apexConceptString, "concept string may not be null");

        LOGGER.entry("reading Apex concept from string . . .");

        final var apexString = apexConceptString.trim();

        // Set the type of input for this stream
        setInputType(apexString);

        // The Apex Concept
        C apexConcept = null;

        // Use JAXB to read and verify the Apex concept XML file
        try {
            // Load the configuration file
            final var source = new StreamSource(new StringReader(apexString));
            final JAXBElement<C> rootElement = unmarshaller.unmarshal(source, rootConceptClass);
            apexConcept = rootElement.getValue();
        } catch (final JAXBException e) {
            throw new ApexModelException("Unable to unmarshal Apex concept ", e);
        }

        LOGGER.debug("reading of Apex concept {} completed");

        // Check if the concept should be validated
        if (validateFlag) {
            // Validate the configuration file
            final AxValidationResult validationResult = apexConcept.validate(new AxValidationResult());
            if (validationResult.isValid()) {
                return apexConcept;
            } else {
                String message = "Apex concept validation failed" + validationResult.toString();
                LOGGER.error(message);
                throw new ApexModelException(message);
            }
        } else {
            // No validation check
            return apexConcept;
        }
    }

    /**
     * Gets the value of the validation flag.
     *
     * @return the validation flag value
     */
    public boolean getValidateFlag() {
        return validateFlag;
    }

    /**
     * Sets the validation flag.
     *
     * @param validateFlag the validation flag value
     */
    public void setValidateFlag(final boolean validateFlag) {
        this.validateFlag = validateFlag;
    }

    /**
     * Set the type of input for the concept reader.
     *
     * @param apexConceptString The stream with
     * @throws ApexModelException on errors setting input type
     */
    private void setInputType(final String apexConceptString) throws ApexModelException {
        // Check the input type
        if (Pattern.compile(JSON_INPUT_TYPE_REGEXP).matcher(apexConceptString).find()) {
            // is json
            try {
                unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
                unmarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
            } catch (final Exception e) {
                LOGGER.warn("JAXB error setting marshaller for JSON Input", e);
                throw new ApexModelException("JAXB error setting unmarshaller for JSON input", e);
            }
        } else if (Pattern.compile(XML_INPUT_TYPE_REGEXP).matcher(apexConceptString).find()) {
            // is xml
            try {
                unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_XML);
            } catch (final Exception e) {
                LOGGER.warn("JAXB error setting marshaller for XML Input", e);
                throw new ApexModelException("JAXB error setting unmarshaller for XML input", e);
            }
        } else {
            LOGGER.warn("format of input for Apex concept is neither JSON nor XML");
            throw new ApexModelException("format of input for Apex concept is neither JSON nor XML");
        }
    }

}