2 * ============LICENSE_START=======================================================
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
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 package org.onap.aai.introspection;
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import org.eclipse.persistence.dynamic.DynamicType;
25 import org.eclipse.persistence.jaxb.JAXBContextProperties;
26 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
27 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
28 import org.onap.aai.util.AAIConstants;
30 import javax.xml.bind.JAXBException;
32 import java.io.FileInputStream;
33 import java.io.FileNotFoundException;
34 import java.io.InputStream;
35 import java.util.HashMap;
37 import java.util.regex.Matcher;
38 import java.util.regex.Pattern;
40 public class ModelInjestor {
42 private Map<Version, DynamicJAXBContext> versionContextMap = new HashMap<>();
43 private static final Pattern classNamePattern = Pattern.compile("\\.(v\\d+)\\.");
44 private static final Pattern uriPattern = Pattern.compile("(v\\d+)\\/");
45 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(ModelInjestor.class);
49 * Instantiates a new model injestor.
51 private ModelInjestor() {
54 } catch (FileNotFoundException | JAXBException e) {
55 throw new RuntimeException(e);
59 private static class Helper {
60 private static final ModelInjestor INSTANCE = new ModelInjestor();
64 * Gets the single instance of ModelInjestor.
66 * @return single instance of ModelInjestor
68 public synchronized static ModelInjestor getInstance() {
69 return Helper.INSTANCE;
75 * @throws FileNotFoundException the file not found exception
76 * @throws JAXBException the JAXB exception
78 private void injestModels() throws FileNotFoundException, JAXBException {
80 for (Version version : Version.values()) {
81 this.injestModel(version);
88 * @param version the version
89 * @throws JAXBException the JAXB exception
90 * @throws FileNotFoundException the file not found exception
92 private void injestModel (Version version) throws JAXBException, FileNotFoundException {
93 String fileName = this.getOXMFileName(version);
95 File oxmFile = new File(AAIConstants.AAI_HOME_ETC + fileName);
97 // Check if the file exists on the path and if it doesn't exist then
98 // Using classloader makes it easy to have a different oxm file
99 // for unit testing the oxm files otherwise, you will be
100 // stuck with using the main oxm for even the testing
103 if(oxmFile.exists()){
104 LOGGER.info("Oxm file exists on the system {}", oxmFile);
105 iStream = new FileInputStream(oxmFile);
107 LOGGER.warn("Unable to find oxm file {} on the system so using classloader", oxmFile);
108 iStream = getClass().getClassLoader().getResourceAsStream(fileName);
111 Map<String, Object> properties = new HashMap<String, Object>();
112 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream);
113 final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromOXM(this.getClass().getClassLoader(), properties);
114 versionContextMap.put(version, jaxbContext);
118 * Gets the version from class name.
120 * @param classname the classname
121 * @return the version from class name
123 public Version getVersionFromClassName (String classname) {
124 Matcher m = classNamePattern.matcher(classname);
125 String version = "v12";
127 version = m.group(1);
130 return Version.valueOf(version);
134 * Gets the context for URI.
137 * @return the context for URI
139 public DynamicJAXBContext getContextForURI(String uri) {
140 DynamicJAXBContext result = null;
141 Matcher m = uriPattern.matcher(uri);
142 Version version = null;
144 version = Version.valueOf(m.group(1));
145 result = versionContextMap.get(version);
152 * Gets the context for version.
154 * @param version the version
155 * @return the context for version
157 public DynamicJAXBContext getContextForVersion(Version version) {
158 DynamicJAXBContext result = null;
160 result = versionContextMap.get(version);
167 * Gets the dynamic type for class name.
169 * @param classname the classname
170 * @return the dynamic type for class name
172 public DynamicType getDynamicTypeForClassName(String classname) {
173 DynamicType result = null;
174 DynamicJAXBContext context = null;
176 Version version = this.getVersionFromClassName(classname);
178 context = versionContextMap.get(version);
180 if (context != null) {
181 result = context.getDynamicType(classname);
187 public String getOXMFileName(Version v) {
188 // Changed the /oxm/aai_oxm_*.xml to oxm/aai_oxm_*.xml
189 // Modified to load from input stream using getClass().getClassLoader()
190 // As this will be able to relatively get the oxm if the oxm file is there
191 // So if there is a src/main/resources/oxm/ and src/test/resources/oxm, the
192 // test oxm will end up being used for tests and src oxm for source files
193 // Don't change this unless you understand the details specified
194 return "oxm/aai_oxm_" + v.toString() + ".xml";