2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.nodes;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.InputStream;
29 import java.util.ArrayList;
30 import java.util.EnumMap;
31 import java.util.HashMap;
32 import java.util.List;
34 import java.util.Map.Entry;
36 import javax.xml.bind.JAXBException;
38 import org.eclipse.persistence.jaxb.JAXBContextProperties;
39 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
40 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
41 import org.onap.aai.setup.ConfigTranslator;
42 import org.onap.aai.setup.Version;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.stereotype.Component;
48 * NodeIngestor - ingests A&AI OXM files per given config, serves DynamicJAXBContext per version
50 public class NodeIngestor {
52 private Map<Version, DynamicJAXBContext> versionContextMap = new EnumMap<>(Version.class);
56 * Instantiates the NodeIngestor bean.
58 * @param translator - ConfigTranslator autowired in by Spring framework which
59 * contains the configuration information needed to ingest the desired files.
61 public NodeIngestor(ConfigTranslator translator) {
62 Map<Version, List<String>> filesToIngest = translator.getNodeFiles();
65 for (Entry<Version, List<String>> verFiles : filesToIngest.entrySet()) {
66 Version v = verFiles.getKey();
67 List<String> files = verFiles.getValue();
68 final DynamicJAXBContext ctx = ingest(files);
69 versionContextMap.put(v, ctx);
71 } catch (FileNotFoundException | JAXBException e) {
72 throw new ExceptionInInitializerError(e);
77 * Ingests the given OXM files into DynamicJAXBContext
79 * @param files - List<String> of full filenames (ie including the path) to be ingested
81 * @return DynamicJAXBContext including schema information from all given files
83 * @throws FileNotFoundException if an OXM file can't be found
84 * @throws JAXBException if there's an error creating the DynamicJAXBContext
86 private DynamicJAXBContext ingest(List<String> files) throws FileNotFoundException, JAXBException {
87 List<InputStream> streams = new ArrayList<>();
89 for (String name : files) {
90 streams.add(new FileInputStream(new File(name)));
93 Map<String, Object> properties = new HashMap<>();
94 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, streams);
95 return DynamicJAXBContextFactory.createContextFromOXM(this.getClass().getClassLoader(), properties);
99 * Gets the DynamicJAXBContext for the given version
102 * @return DynamicJAXBContext
104 public DynamicJAXBContext getContextForVersion(Version v) {
105 return versionContextMap.get(v);