2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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=========================================================
21 package org.openecomp.aai.introspection;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.InputStream;
27 import java.util.HashMap;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
32 import javax.xml.bind.JAXBException;
34 import org.eclipse.persistence.dynamic.DynamicType;
35 import org.eclipse.persistence.jaxb.JAXBContextProperties;
36 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
37 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
39 import org.openecomp.aai.util.AAIConstants;
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+)\\/");
50 * Instantiates a new model injestor.
52 private ModelInjestor() {
55 } catch (FileNotFoundException | JAXBException e) {
56 throw new RuntimeException(e);
60 private static class Helper {
61 private static final ModelInjestor INSTANCE = new ModelInjestor();
65 * Gets the single instance of ModelInjestor.
67 * @return single instance of ModelInjestor
69 public synchronized static ModelInjestor getInstance() {
70 return Helper.INSTANCE;
76 * @throws FileNotFoundException the file not found exception
77 * @throws JAXBException the JAXB exception
79 private void injestModels() throws FileNotFoundException, JAXBException {
81 for (Version version : Version.values()) {
82 this.injestModel(version);
89 * @param version the version
90 * @throws JAXBException the JAXB exception
91 * @throws FileNotFoundException the file not found exception
93 private void injestModel (Version version) throws JAXBException, FileNotFoundException {
94 String fileName = this.getOXMFileName(version);
95 InputStream iStream = new FileInputStream(new File(fileName));
96 Map<String, Object> properties = new HashMap<String, Object>();
97 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream);
98 final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromOXM(this.getClass().getClassLoader(), properties);
99 versionContextMap.put(version, jaxbContext);
103 * Gets the version from class name.
105 * @param classname the classname
106 * @return the version from class name
108 public Version getVersionFromClassName (String classname) {
109 Matcher m = classNamePattern.matcher(classname);
110 String version = "v2"; //for the OXM, only the v2 ones don't include a model name, hence this default
112 version = m.group(1);
115 return Version.valueOf(version);
119 * Gets the context for URI.
122 * @return the context for URI
124 public DynamicJAXBContext getContextForURI(String uri) {
125 DynamicJAXBContext result = null;
126 Matcher m = uriPattern.matcher(uri);
127 Version version = null;
129 version = Version.valueOf(m.group(1));
130 result = versionContextMap.get(version);
137 * Gets the context for version.
139 * @param version the version
140 * @return the context for version
142 public DynamicJAXBContext getContextForVersion(Version version) {
143 DynamicJAXBContext result = null;
145 result = versionContextMap.get(version);
152 * Gets the dynamic type for class name.
154 * @param classname the classname
155 * @return the dynamic type for class name
157 public DynamicType getDynamicTypeForClassName(String classname) {
158 DynamicType result = null;
159 DynamicJAXBContext context = null;
161 Version version = this.getVersionFromClassName(classname);
163 context = versionContextMap.get(version);
165 if (context != null) {
166 result = context.getDynamicType(classname);
172 public String getOXMFileName(Version v) {
173 return AAIConstants.AAI_HOME_ETC_OXM + "aai_oxm_" + v.toString() + ".xml";