aboutsummaryrefslogtreecommitdiffstats
path: root/aai-schema-ingest/src/main/java/org/onap/aai/nodes/NodeIngestor.java
blob: 014a444279157c88534d7095d65c9335612d1411 (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Modifications Copyright © 2018 IBM.
 * ================================================================================
 * 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.nodes;

import com.google.common.base.CaseFormat;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.PostConstruct;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
import org.onap.aai.setup.ConfigTranslator;
import org.onap.aai.setup.SchemaVersion;
import org.onap.aai.setup.SchemaVersions;
import org.onap.aai.setup.Translator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

@Component
/*
 * NodeIngestor - ingests A&AI OXM files per given config, serves DynamicJAXBContext per version
 */
@PropertySource(value = "classpath:schema-ingest.properties", ignoreResourceNotFound = true)
@PropertySource(value = "file:${schema.ingest.file}", ignoreResourceNotFound = true)
public class NodeIngestor {

    private static final Logger LOGGER = LoggerFactory.getLogger(NodeIngestor.class);
    private static final Pattern classNamePattern = Pattern.compile("\\.(v\\d+)\\.");
    private Map<SchemaVersion, DynamicJAXBContext> versionContextMap = new HashMap<>();
    private Map<SchemaVersion, Set<String>> typesPerVersion = new HashMap<>();
    private Map<SchemaVersion, Document> schemaPerVersion = new HashMap<>();
    private String localSchema;
    private SchemaVersions schemaVersions;
    private Set<Translator> translators;

    private CaseFormatStore caseFormatStore;
    // TODO : See if you can get rid of InputStream resets

    /**
     * Instantiates the NodeIngestor bean.
     *
     * @param translatorSet
     */

    @Autowired
    public NodeIngestor(Set<Translator> translatorSet) {
        this.translators = translatorSet;
        this.caseFormatStore = new CaseFormatStore();
    }

    @PostConstruct
    public void initialize() {

        for (Translator translator : translators) {
            try {
                LOGGER.debug("Processing the translator");
                translateAll(translator);

            } catch (Exception e) {
                LOGGER.error("Error while Processing the translator" + e.getMessage());
                throw new ExceptionInInitializerError("NodeIngestor could not ingest schema");
            }
        }
        if (versionContextMap.isEmpty() || schemaPerVersion.isEmpty() || typesPerVersion.isEmpty()) {
            throw new ExceptionInInitializerError("NodeIngestor could not ingest schema");
        }
    }

    private void translateAll(Translator translator) throws ExceptionInInitializerError {
        if (translator instanceof ConfigTranslator) {
            this.localSchema = "true";
        }

        Boolean retrieveLocalSchema = Boolean.parseBoolean(this.localSchema);
        /*
         * Set this to default schemaVersion
         */
        this.schemaVersions = translator.getSchemaVersions();
        List<SchemaVersion> schemaVersionList = translator.getSchemaVersions().getVersions();

        try {
            for (SchemaVersion version : schemaVersionList) {
                LOGGER.debug("Version being processed" + version);
                List<InputStream> inputStreams = retrieveOXM(version, translator);
                LOGGER.debug("Retrieved OXMs from SchemaService");
                /*
                 * IOUtils.copy and copy the inputstream
                 */
                if (inputStreams.isEmpty()) {
                    continue;
                }

                final DynamicJAXBContext ctx = ingest(inputStreams);
                versionContextMap.put(version, ctx);
                setAllTypesAndProperties(version, inputStreams);
                schemaPerVersion.put(version, createCombinedSchema(inputStreams, version, retrieveLocalSchema));
            }
        } catch (JAXBException | ParserConfigurationException | SAXException | IOException e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    /**
     * Ingests the given OXM files into DynamicJAXBContext
     *
     * @param inputStreams - inputStrean of oxms from SchemaService to be ingested
     *
     * @return DynamicJAXBContext including schema information from all given files
     *
     * @throws JAXBException if there's an error creating the DynamicJAXBContext
     */
    private DynamicJAXBContext ingest(List<InputStream> inputStreams) throws JAXBException {
        Map<String, Object> properties = new HashMap<>();
        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, inputStreams);
        LOGGER.debug("Ingested the InputStream");
        return DynamicJAXBContextFactory.createContextFromOXM(this.getClass().getClassLoader(), properties);
    }

    private void setAllTypesAndProperties(SchemaVersion version, List<InputStream> inputStreams)
            throws ParserConfigurationException, IOException, SAXException {
        Set<String> types = new HashSet<>();
        final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        docFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
        docFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
        docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        for (InputStream inputStream : inputStreams) {
            // TODO Change this
            inputStream.reset();
            final Document doc = docBuilder.parse(inputStream);
            final NodeList list = doc.getElementsByTagName("java-type");
            getAllNodeTypes(list, types);
            caseFormatStore.parse(doc);
        }

        LOGGER.debug("Types size {}", types.size());
        typesPerVersion.put(version, types);
    }

    private void getAllNodeTypes(NodeList list, Set<String> types) {

        for (int i = 0; i < list.getLength(); i++) {
            String type = list.item(i).getAttributes().getNamedItem("name").getNodeValue();
            types.add(CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, type));
        }
    }

    private Document createCombinedSchema(List<InputStream> inputStreams, SchemaVersion version, boolean localSchema)
            throws ParserConfigurationException, SAXException, IOException {
        if (localSchema) {
            return createCombinedSchema(inputStreams, version);
        }

        InputStream inputStream = inputStreams.get(0);
        inputStream.reset();
        final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        docFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
        docFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
        docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        DocumentBuilder masterDocBuilder = docFactory.newDocumentBuilder();
        return masterDocBuilder.parse(inputStream);
    }

    private Document createCombinedSchema(List<InputStream> inputStreams, SchemaVersion version)
            throws ParserConfigurationException, SAXException, IOException {
        final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        docFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
        docFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
        docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        DocumentBuilder masterDocBuilder = docFactory.newDocumentBuilder();
        Document combinedDoc = masterDocBuilder.parse(getShell(version));
        NodeList masterList = combinedDoc.getElementsByTagName("java-types");
        Node javaTypesContainer = masterList.getLength() == 0 ? combinedDoc.getDocumentElement() : masterList.item(0);

        for (InputStream inputStream : inputStreams) {
            inputStream.reset();
            final Document doc = docBuilder.parse(inputStream);
            final NodeList list = doc.getElementsByTagName("java-type");
            for (int i = 0; i < list.getLength(); i++) {
                Node copy = combinedDoc.importNode(list.item(i), true);
                javaTypesContainer.appendChild(copy);
            }
        }
        return combinedDoc;
    }

    /**
     * Gets the DynamicJAXBContext for the given version
     *
     * @param v - schema version to retrieve the context
     * @return DynamicJAXBContext
     */
    public DynamicJAXBContext getContextForVersion(SchemaVersion v) {
        return versionContextMap.get(v);
    }

    /**
     * Determines if the given version contains the given node type
     *
     * @param nodeType - node type to check, must be in lower hyphen form (ie "type-name")
     * @param v - schema version to check against
     * @return boolean
     */
    public boolean hasNodeType(String nodeType, SchemaVersion v) {
        return typesPerVersion.get(v).contains(nodeType);
    }

    public Set<String> getObjectsInVersion(SchemaVersion v) {
        return typesPerVersion.get(v);
    }

    /**
     * Determines if the given version contains the given node type
     *
     * @param v - Schemaversion to retrieve the schema
     * @return Document
     */
    public Document getSchema(SchemaVersion v) {
        return schemaPerVersion.get(v);
    }

    public SchemaVersion getVersionFromClassName(String classname) {
        Matcher m = classNamePattern.matcher(classname);
        if (m.find()) {
            String version = m.group(1);
            return new SchemaVersion(version);
        } else {
            return this.schemaVersions.getDefaultVersion();
        }
    }

    private List<InputStream> retrieveOXM(SchemaVersion version, Translator translator) throws IOException {
        /*
         * Call Schema MS to get versions using RestTemplate or Local
         */
        return translator.getVersionNodeStream(version);

    }

    private InputStream getShell(SchemaVersion v) {
        String source = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                + "<xml-bindings xmlns=\"http://www.eclipse.org/eclipselink/xsds/persistence/oxm\" package-name=\"inventory.aai.onap.org."
                + v.toString().toLowerCase() + "\" xml-mapping-metadata-complete=\"true\">\n"
                + "	<xml-schema element-form-default=\"QUALIFIED\">\n"
                + "		<xml-ns namespace-uri=\"http://org.onap.aai.inventory/" + v.toString().toLowerCase() + "\" />\n"
                + "	</xml-schema>\n" + "	<java-types>\n" + "	</java-types>\n" + "</xml-bindings>";
        return new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8));
    }

    public CaseFormatStore getCaseFormatStore() {
        return caseFormatStore;
    }
}