added schema validation tools
[aai/aai-common.git] / aai-schema-ingest / src / main / java / org / onap / aai / nodes / NodeIngestor.java
1 /** 
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  *
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.onap.aai.nodes;
24
25 import java.io.File;
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;
35 import java.util.Map;
36 import java.util.Map.Entry;
37 import java.util.Set;
38
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;
44
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;
55
56 import com.google.common.base.CaseFormat;
57
58 @Component
59 /**
60  * NodeIngestor - ingests A&AI OXM files per given config, serves DynamicJAXBContext per version
61  */
62 public class NodeIngestor {
63         
64         private Map<Version, DynamicJAXBContext> versionContextMap = new EnumMap<>(Version.class);
65         private Map<Version, Set<String>> typesPerVersion = new EnumMap<>(Version.class);
66         
67         @Autowired
68         /**
69          * Instantiates the NodeIngestor bean.
70          * 
71          * @param translator - ConfigTranslator autowired in by Spring framework which
72          * contains the configuration information needed to ingest the desired files.
73          */
74         public NodeIngestor(ConfigTranslator translator) {
75                 Map<Version, List<String>> filesToIngest = translator.getNodeFiles();
76                 
77                 try {
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));
84                         }
85                 } catch (JAXBException | ParserConfigurationException | SAXException | IOException e) {
86                         throw new ExceptionInInitializerError(e);
87                 }
88         }
89         
90         /**
91          * Ingests the given OXM files into DynamicJAXBContext
92          * 
93          * @param files - List<String> of full filenames (ie including the path) to be ingested
94          * 
95          * @return DynamicJAXBContext including schema information from all given files
96          * 
97          * @throws FileNotFoundException if an OXM file can't be found
98          * @throws JAXBException if there's an error creating the DynamicJAXBContext
99          */
100         private DynamicJAXBContext ingest(List<String> files) throws FileNotFoundException, JAXBException {
101                 List<InputStream> streams = new ArrayList<>();
102                 
103                 for (String name : files) {
104                         streams.add(new FileInputStream(new File(name)));
105                 }
106                 
107                 Map<String, Object> properties = new HashMap<>(); 
108                 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, streams);
109                 return DynamicJAXBContextFactory.createContextFromOXM(this.getClass().getClassLoader(), properties);
110         }
111         
112         private Set<String> getAllNodeTypes(List<String> files) throws ParserConfigurationException, SAXException, IOException {
113                 Set<String> types = new HashSet<>();
114                 
115                 final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
116                 docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
117                 final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
118                 
119                 for (String file : files) {
120                         InputStream inputStream = new FileInputStream(file);
121                         
122                         final Document doc = docBuilder.parse(inputStream);
123                         final NodeList list = doc.getElementsByTagName("java-type");
124                         
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));
128                         }
129                 }
130                 
131                 return types;
132         }
133
134         /**
135          * Gets the DynamicJAXBContext for the given version
136          * 
137          * @param Version v
138          * @return DynamicJAXBContext
139          */
140         public DynamicJAXBContext getContextForVersion(Version v) {
141                 return versionContextMap.get(v);
142         }
143         
144         /**
145          * Determines if the given version contains the given node type
146          * 
147          * @param String nodeType - node type to check, must be in lower hyphen form (ie "type-name")
148          * @param v
149          * @return
150          */
151         public boolean hasNodeType(String nodeType, Version v) {
152                 return typesPerVersion.get(v).contains(nodeType);
153         }
154 }