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;
41 public class ModelInjestor {
43 private Map<Version, DynamicJAXBContext> versionContextMap = new HashMap<>();
44 private static final Pattern classNamePattern = Pattern.compile("\\.(v\\d+)\\.");
45 private static final Pattern uriPattern = Pattern.compile("(v\\d+)\\/");
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);
94 InputStream iStream = new FileInputStream(new File(fileName));
95 Map<String, Object> properties = new HashMap<String, Object>();
96 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream);
97 final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromOXM(this.getClass().getClassLoader(), properties);
98 versionContextMap.put(version, jaxbContext);
102 * Gets the version from class name.
104 * @param classname the classname
105 * @return the version from class name
107 public Version getVersionFromClassName (String classname) {
108 Matcher m = classNamePattern.matcher(classname);
109 String version = "v2"; //for the OXM, only the v2 ones don't include a model name, hence this default
111 version = m.group(1);
114 return Version.valueOf(version);
118 * Gets the context for URI.
121 * @return the context for URI
123 public DynamicJAXBContext getContextForURI(String uri) {
124 DynamicJAXBContext result = null;
125 Matcher m = uriPattern.matcher(uri);
126 Version version = null;
128 version = Version.valueOf(m.group(1));
129 result = versionContextMap.get(version);
136 * Gets the context for version.
138 * @param version the version
139 * @return the context for version
141 public DynamicJAXBContext getContextForVersion(Version version) {
142 DynamicJAXBContext result = null;
144 result = versionContextMap.get(version);
151 * Gets the dynamic type for class name.
153 * @param classname the classname
154 * @return the dynamic type for class name
156 public DynamicType getDynamicTypeForClassName(String classname) {
157 DynamicType result = null;
158 DynamicJAXBContext context = null;
160 Version version = this.getVersionFromClassName(classname);
162 context = versionContextMap.get(version);
164 if (context != null) {
165 result = context.getDynamicType(classname);
171 public String getOXMFileName(Version v) {
172 return AAIConstants.AAI_HOME_ETC_OXM + "aai_oxm_" + v.toString() + ".xml";