2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.aai.nodes;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.InputStream;
26 import java.util.ArrayList;
27 import java.util.EnumMap;
28 import java.util.HashMap;
29 import java.util.List;
31 import java.util.Map.Entry;
33 import javax.xml.bind.JAXBException;
35 import org.eclipse.persistence.jaxb.JAXBContextProperties;
36 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
37 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
38 import org.onap.aai.setup.ConfigTranslator;
39 import org.onap.aai.setup.Version;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Component;
45 * NodeIngestor - ingests A&AI OXM files per given config, serves DynamicJAXBContext per version
47 public class NodeIngestor {
49 private Map<Version, DynamicJAXBContext> versionContextMap = new EnumMap<>(Version.class);
53 * Instantiates the NodeIngestor bean.
55 * @param translator - ConfigTranslator autowired in by Spring framework which
56 * contains the configuration information needed to ingest the desired files.
58 public NodeIngestor(ConfigTranslator translator) {
59 Map<Version, List<String>> filesToIngest = translator.getNodeFiles();
62 for (Entry<Version, List<String>> verFiles : filesToIngest.entrySet()) {
63 Version v = verFiles.getKey();
64 List<String> files = verFiles.getValue();
65 final DynamicJAXBContext ctx = ingest(files);
66 versionContextMap.put(v, ctx);
68 } catch (FileNotFoundException | JAXBException e) {
69 throw new ExceptionInInitializerError(e);
74 * Ingests the given OXM files into DynamicJAXBContext
76 * @param files - List<String> of full filenames (ie including the path) to be ingested
78 * @return DynamicJAXBContext including schema information from all given files
80 * @throws FileNotFoundException if an OXM file can't be found
81 * @throws JAXBException if there's an error creating the DynamicJAXBContext
83 private DynamicJAXBContext ingest(List<String> files) throws FileNotFoundException, JAXBException {
84 List<InputStream> streams = new ArrayList<>();
86 for (String name : files) {
87 streams.add(new FileInputStream(new File(name)));
90 Map<String, Object> properties = new HashMap<>();
91 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, streams);
92 return DynamicJAXBContextFactory.createContextFromOXM(this.getClass().getClassLoader(), properties);
96 * Gets the DynamicJAXBContext for the given version
99 * @return DynamicJAXBContext
101 public DynamicJAXBContext getContextForVersion(Version v) {
102 return versionContextMap.get(v);