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 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+)\\/");
48 * Instantiates a new model injestor.
50 private ModelInjestor() {
53 } catch (FileNotFoundException | JAXBException e) {
54 throw new RuntimeException(e);
58 private static class Helper {
59 private static final ModelInjestor INSTANCE = new ModelInjestor();
63 * Gets the single instance of ModelInjestor.
65 * @return single instance of ModelInjestor
67 public synchronized static ModelInjestor getInstance() {
68 return Helper.INSTANCE;
74 * @throws FileNotFoundException the file not found exception
75 * @throws JAXBException the JAXB exception
77 private void injestModels() throws FileNotFoundException, JAXBException {
79 for (Version version : Version.values()) {
80 this.injestModel(version);
87 * @param version the version
88 * @throws JAXBException the JAXB exception
89 * @throws FileNotFoundException the file not found exception
91 private void injestModel (Version version) throws JAXBException, FileNotFoundException {
92 String fileName = this.getOXMFileName(version);
93 InputStream iStream = new FileInputStream(new File(fileName));
94 Map<String, Object> properties = new HashMap<String, Object>();
95 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream);
96 final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromOXM(this.getClass().getClassLoader(), properties);
97 versionContextMap.put(version, jaxbContext);
101 * Gets the version from class name.
103 * @param classname the classname
104 * @return the version from class name
106 public Version getVersionFromClassName (String classname) {
107 Matcher m = classNamePattern.matcher(classname);
108 String version = "v2"; //for the OXM, only the v2 ones don't include a model name, hence this default
110 version = m.group(1);
113 return Version.valueOf(version);
117 * Gets the context for URI.
120 * @return the context for URI
122 public DynamicJAXBContext getContextForURI(String uri) {
123 DynamicJAXBContext result = null;
124 Matcher m = uriPattern.matcher(uri);
125 Version version = null;
127 version = Version.valueOf(m.group(1));
128 result = versionContextMap.get(version);
135 * Gets the context for version.
137 * @param version the version
138 * @return the context for version
140 public DynamicJAXBContext getContextForVersion(Version version) {
141 DynamicJAXBContext result = null;
143 result = versionContextMap.get(version);
150 * Gets the dynamic type for class name.
152 * @param classname the classname
153 * @return the dynamic type for class name
155 public DynamicType getDynamicTypeForClassName(String classname) {
156 DynamicType result = null;
157 DynamicJAXBContext context = null;
159 Version version = this.getVersionFromClassName(classname);
161 context = versionContextMap.get(version);
163 if (context != null) {
164 result = context.getDynamicType(classname);
170 public String getOXMFileName(Version v) {
171 return AAIConstants.AAI_HOME_ETC_OXM + "aai_oxm_" + v.toString() + ".xml";