849ad5773d4202ca97aa51a4915b7ea86e0286cf
[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-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
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 package org.onap.aai.nodes;
21
22 import java.io.File;
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;
30 import java.util.Map;
31 import java.util.Map.Entry;
32
33 import javax.xml.bind.JAXBException;
34
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;
42
43 @Component
44 /**
45  * NodeIngestor - ingests A&AI OXM files per given config, serves DynamicJAXBContext per version
46  */
47 public class NodeIngestor {
48         
49         private Map<Version, DynamicJAXBContext> versionContextMap = new EnumMap<>(Version.class);
50         
51         @Autowired
52         /**
53          * Instantiates the NodeIngestor bean.
54          * 
55          * @param translator - ConfigTranslator autowired in by Spring framework which
56          * contains the configuration information needed to ingest the desired files.
57          */
58         public NodeIngestor(ConfigTranslator translator) {
59                 Map<Version, List<String>> filesToIngest = translator.getNodeFiles();
60                 
61                 try {
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);
67                         }
68                 } catch (FileNotFoundException | JAXBException e) {
69                         throw new ExceptionInInitializerError(e);
70                 }
71         }
72         
73         /**
74          * Ingests the given OXM files into DynamicJAXBContext
75          * 
76          * @param files - List<String> of full filenames (ie including the path) to be ingested
77          * 
78          * @return DynamicJAXBContext including schema information from all given files
79          * 
80          * @throws FileNotFoundException if an OXM file can't be found
81          * @throws JAXBException if there's an error creating the DynamicJAXBContext
82          */
83         private DynamicJAXBContext ingest(List<String> files) throws FileNotFoundException, JAXBException {
84                 List<InputStream> streams = new ArrayList<>();
85                 
86                 for (String name : files) {
87                         streams.add(new FileInputStream(new File(name)));
88                 }
89                 
90                 Map<String, Object> properties = new HashMap<>(); 
91                 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, streams);
92                 return DynamicJAXBContextFactory.createContextFromOXM(this.getClass().getClassLoader(), properties);
93         }
94
95         /**
96          * Gets the DynamicJAXBContext for the given version
97          * 
98          * @param Version v
99          * @return DynamicJAXBContext
100          */
101         public DynamicJAXBContext getContextForVersion(Version v) {
102                 return versionContextMap.get(v);
103         }
104 }