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.
22 package org.onap.aai.introspection;
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import org.eclipse.persistence.dynamic.DynamicType;
27 import org.eclipse.persistence.jaxb.JAXBContextProperties;
28 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
29 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
30 import org.onap.aai.util.AAIConstants;
32 import javax.xml.bind.JAXBException;
34 import java.io.FileInputStream;
35 import java.io.FileNotFoundException;
36 import java.io.InputStream;
37 import java.util.HashMap;
39 import java.util.regex.Matcher;
40 import java.util.regex.Pattern;
42 public class ModelInjestor {
44 private Map<Version, DynamicJAXBContext> versionContextMap = new HashMap<>();
45 private static final Pattern classNamePattern = Pattern.compile("\\.(v\\d+)\\.");
46 private static final Pattern uriPattern = Pattern.compile("(v\\d+)\\/");
47 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(ModelInjestor.class);
51 * Instantiates a new model injestor.
53 private ModelInjestor() {
56 } catch (FileNotFoundException | JAXBException e) {
57 throw new RuntimeException(e);
61 private static class Helper {
62 private static final ModelInjestor INSTANCE = new ModelInjestor();
66 * Gets the single instance of ModelInjestor.
68 * @return single instance of ModelInjestor
70 public synchronized static ModelInjestor getInstance() {
71 return Helper.INSTANCE;
77 * @throws FileNotFoundException the file not found exception
78 * @throws JAXBException the JAXB exception
80 private void injestModels() throws FileNotFoundException, JAXBException {
82 for (Version version : Version.values()) {
83 this.injestModel(version);
90 * @param version the version
91 * @throws JAXBException the JAXB exception
92 * @throws FileNotFoundException the file not found exception
94 private void injestModel (Version version) throws JAXBException, FileNotFoundException {
95 String fileName = this.getOXMFileName(version);
97 File oxmFile = new File(AAIConstants.AAI_HOME_ETC + fileName);
99 // Check if the file exists on the path and if it doesn't exist then
100 // Using classloader makes it easy to have a different oxm file
101 // for unit testing the oxm files otherwise, you will be
102 // stuck with using the main oxm for even the testing
105 if(oxmFile.exists()){
106 LOGGER.info("Oxm file exists on the system {}", oxmFile);
107 iStream = new FileInputStream(oxmFile);
109 LOGGER.warn("Unable to find oxm file {} on the system so using classloader", oxmFile);
110 iStream = getClass().getClassLoader().getResourceAsStream(fileName);
113 Map<String, Object> properties = new HashMap<String, Object>();
114 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream);
115 final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromOXM(this.getClass().getClassLoader(), properties);
116 versionContextMap.put(version, jaxbContext);
120 * Gets the version from class name.
122 * @param classname the classname
123 * @return the version from class name
125 public Version getVersionFromClassName (String classname) {
126 Matcher m = classNamePattern.matcher(classname);
127 String version = "v12";
129 version = m.group(1);
132 return Version.valueOf(version);
136 * Gets the context for URI.
139 * @return the context for URI
141 public DynamicJAXBContext getContextForURI(String uri) {
142 DynamicJAXBContext result = null;
143 Matcher m = uriPattern.matcher(uri);
144 Version version = null;
146 version = Version.valueOf(m.group(1));
147 result = versionContextMap.get(version);
154 * Gets the context for version.
156 * @param version the version
157 * @return the context for version
159 public DynamicJAXBContext getContextForVersion(Version version) {
160 DynamicJAXBContext result = null;
162 result = versionContextMap.get(version);
169 * Gets the dynamic type for class name.
171 * @param classname the classname
172 * @return the dynamic type for class name
174 public DynamicType getDynamicTypeForClassName(String classname) {
175 DynamicType result = null;
176 DynamicJAXBContext context = null;
178 Version version = this.getVersionFromClassName(classname);
180 context = versionContextMap.get(version);
182 if (context != null) {
183 result = context.getDynamicType(classname);
189 public String getOXMFileName(Version v) {
190 // Changed the /oxm/aai_oxm_*.xml to oxm/aai_oxm_*.xml
191 // Modified to load from input stream using getClass().getClassLoader()
192 // As this will be able to relatively get the oxm if the oxm file is there
193 // So if there is a src/main/resources/oxm/ and src/test/resources/oxm, the
194 // test oxm will end up being used for tests and src oxm for source files
195 // Don't change this unless you understand the details specified
196 return "oxm/aai_oxm_" + v.toString() + ".xml";