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