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.IOException;
29 import java.io.InputStream;
30 import java.util.ArrayList;
31 import java.util.EnumMap;
32 import java.util.HashMap;
33 import java.util.HashSet;
34 import java.util.List;
36 import java.util.Map.Entry;
39 import javax.xml.XMLConstants;
40 import javax.xml.bind.JAXBException;
41 import javax.xml.parsers.DocumentBuilder;
42 import javax.xml.parsers.DocumentBuilderFactory;
43 import javax.xml.parsers.ParserConfigurationException;
45 import org.eclipse.persistence.jaxb.JAXBContextProperties;
46 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
47 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
48 import org.onap.aai.setup.ConfigTranslator;
49 import org.onap.aai.setup.Version;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.stereotype.Component;
52 import org.w3c.dom.Document;
53 import org.w3c.dom.NodeList;
54 import org.xml.sax.SAXException;
56 import com.google.common.base.CaseFormat;
60 * NodeIngestor - ingests A&AI OXM files per given config, serves DynamicJAXBContext per version
62 public class NodeIngestor {
64 private Map<Version, DynamicJAXBContext> versionContextMap = new EnumMap<>(Version.class);
65 private Map<Version, Set<String>> typesPerVersion = new EnumMap<>(Version.class);
69 * Instantiates the NodeIngestor bean.
71 * @param translator - ConfigTranslator autowired in by Spring framework which
72 * contains the configuration information needed to ingest the desired files.
74 public NodeIngestor(ConfigTranslator translator) {
75 Map<Version, List<String>> filesToIngest = translator.getNodeFiles();
78 for (Entry<Version, List<String>> verFiles : filesToIngest.entrySet()) {
79 Version v = verFiles.getKey();
80 List<String> files = verFiles.getValue();
81 final DynamicJAXBContext ctx = ingest(files);
82 versionContextMap.put(v, ctx);
83 typesPerVersion.put(v, getAllNodeTypes(files));
85 } catch (JAXBException | ParserConfigurationException | SAXException | IOException e) {
86 throw new ExceptionInInitializerError(e);
91 * Ingests the given OXM files into DynamicJAXBContext
93 * @param files - List<String> of full filenames (ie including the path) to be ingested
95 * @return DynamicJAXBContext including schema information from all given files
97 * @throws FileNotFoundException if an OXM file can't be found
98 * @throws JAXBException if there's an error creating the DynamicJAXBContext
100 private DynamicJAXBContext ingest(List<String> files) throws FileNotFoundException, JAXBException {
101 List<InputStream> streams = new ArrayList<>();
103 for (String name : files) {
104 streams.add(new FileInputStream(new File(name)));
107 Map<String, Object> properties = new HashMap<>();
108 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, streams);
109 return DynamicJAXBContextFactory.createContextFromOXM(this.getClass().getClassLoader(), properties);
112 private Set<String> getAllNodeTypes(List<String> files) throws ParserConfigurationException, SAXException, IOException {
113 Set<String> types = new HashSet<>();
115 final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
116 docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
117 final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
119 for (String file : files) {
120 InputStream inputStream = new FileInputStream(file);
122 final Document doc = docBuilder.parse(inputStream);
123 final NodeList list = doc.getElementsByTagName("java-type");
125 for (int i = 0; i < list.getLength(); i++) {
126 String type = list.item(i).getAttributes().getNamedItem("name").getNodeValue();
127 types.add(CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, type));
135 * Gets the DynamicJAXBContext for the given version
138 * @return DynamicJAXBContext
140 public DynamicJAXBContext getContextForVersion(Version v) {
141 return versionContextMap.get(v);
145 * Determines if the given version contains the given node type
147 * @param String nodeType - node type to check, must be in lower hyphen form (ie "type-name")
151 public boolean hasNodeType(String nodeType, Version v) {
152 return typesPerVersion.get(v).contains(nodeType);