a8574d8940f44c1e46be2a810bb51ae65bc9e0b9
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / introspection / ModelInjestor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.aai.introspection;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.InputStream;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32 import javax.xml.bind.JAXBException;
33
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;
38
39 import org.openecomp.aai.util.AAIConstants;
40
41 public class ModelInjestor {
42         
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+)\\/");
46
47         
48         /**
49          * Instantiates a new model injestor.
50          */
51         private ModelInjestor() {
52                 try {
53                         injestModels();
54                 } catch (FileNotFoundException | JAXBException e) {
55                         throw new RuntimeException(e);
56                 }
57         }
58         
59         private static class Helper {
60                 private static final ModelInjestor INSTANCE = new ModelInjestor();
61         }
62         
63         /**
64          * Gets the single instance of ModelInjestor.
65          *
66          * @return single instance of ModelInjestor
67          */
68         public synchronized static ModelInjestor getInstance() {
69                 return Helper.INSTANCE;
70         }
71         
72         /**
73          * Injest models.
74          *
75          * @throws FileNotFoundException the file not found exception
76          * @throws JAXBException the JAXB exception
77          */
78         private void injestModels() throws FileNotFoundException, JAXBException {
79                 
80                 for (Version version : Version.values()) {
81                         this.injestModel(version);
82                 }
83         }
84         
85         /**
86          * Injest model.
87          *
88          * @param version the version
89          * @throws JAXBException the JAXB exception
90          * @throws FileNotFoundException the file not found exception
91          */
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);
99         }
100         
101         /**
102          * Gets the version from class name.
103          *
104          * @param classname the classname
105          * @return the version from class name
106          */
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
110                 if (m.find()) {
111                         version = m.group(1);
112                 }
113                 
114                 return Version.valueOf(version);
115         }
116         
117         /**
118          * Gets the context for URI.
119          *
120          * @param uri the uri
121          * @return the context for URI
122          */
123         public DynamicJAXBContext getContextForURI(String uri) {
124                 DynamicJAXBContext result = null;
125                 Matcher m = uriPattern.matcher(uri);
126                 Version version = null;
127                 if (m.find()) {
128                         version = Version.valueOf(m.group(1));
129                         result = versionContextMap.get(version);
130                 }
131                 
132                 return result;
133         }
134         
135         /**
136          * Gets the context for version.
137          *
138          * @param version the version
139          * @return the context for version
140          */
141         public DynamicJAXBContext getContextForVersion(Version version) {
142                 DynamicJAXBContext result = null;
143                 
144                 result = versionContextMap.get(version);
145                 
146                 
147                 return result;
148         }
149         
150         /**
151          * Gets the dynamic type for class name.
152          *
153          * @param classname the classname
154          * @return the dynamic type for class name
155          */
156         public DynamicType getDynamicTypeForClassName(String classname) {
157                 DynamicType result = null;
158                 DynamicJAXBContext context = null;
159
160                 Version version = this.getVersionFromClassName(classname);
161
162                 context = versionContextMap.get(version);
163                 
164                 if (context != null) {
165                         result = context.getDynamicType(classname);
166                 }
167                 
168                 return result;
169         }
170         
171         public String getOXMFileName(Version v) {
172                 return  AAIConstants.AAI_HOME_ETC_OXM + "aai_oxm_" + v.toString() + ".xml";
173         }
174         
175 }