Initial commit with all the necessary files
[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
42 public class ModelInjestor {
43         
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
48         
49         /**
50          * Instantiates a new model injestor.
51          */
52         private ModelInjestor() {
53                 try {
54                         injestModels();
55                 } catch (FileNotFoundException | JAXBException e) {
56                         throw new RuntimeException(e);
57                 }
58         }
59         
60         private static class Helper {
61                 private static final ModelInjestor INSTANCE = new ModelInjestor();
62         }
63         
64         /**
65          * Gets the single instance of ModelInjestor.
66          *
67          * @return single instance of ModelInjestor
68          */
69         public synchronized static ModelInjestor getInstance() {
70                 return Helper.INSTANCE;
71         }
72         
73         /**
74          * Injest models.
75          *
76          * @throws FileNotFoundException the file not found exception
77          * @throws JAXBException the JAXB exception
78          */
79         private void injestModels() throws FileNotFoundException, JAXBException {
80                 
81                 for (Version version : Version.values()) {
82                         this.injestModel(version);
83                 }
84         }
85         
86         /**
87          * Injest model.
88          *
89          * @param version the version
90          * @throws JAXBException the JAXB exception
91          * @throws FileNotFoundException the file not found exception
92          */
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);
100         }
101         
102         /**
103          * Gets the version from class name.
104          *
105          * @param classname the classname
106          * @return the version from class name
107          */
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
111                 if (m.find()) {
112                         version = m.group(1);
113                 }
114                 
115                 return Version.valueOf(version);
116         }
117         
118         /**
119          * Gets the context for URI.
120          *
121          * @param uri the uri
122          * @return the context for URI
123          */
124         public DynamicJAXBContext getContextForURI(String uri) {
125                 DynamicJAXBContext result = null;
126                 Matcher m = uriPattern.matcher(uri);
127                 Version version = null;
128                 if (m.find()) {
129                         version = Version.valueOf(m.group(1));
130                         result = versionContextMap.get(version);
131                 }
132                 
133                 return result;
134         }
135         
136         /**
137          * Gets the context for version.
138          *
139          * @param version the version
140          * @return the context for version
141          */
142         public DynamicJAXBContext getContextForVersion(Version version) {
143                 DynamicJAXBContext result = null;
144                 
145                 result = versionContextMap.get(version);
146                 
147                 
148                 return result;
149         }
150         
151         /**
152          * Gets the dynamic type for class name.
153          *
154          * @param classname the classname
155          * @return the dynamic type for class name
156          */
157         public DynamicType getDynamicTypeForClassName(String classname) {
158                 DynamicType result = null;
159                 DynamicJAXBContext context = null;
160
161                 Version version = this.getVersionFromClassName(classname);
162
163                 context = versionContextMap.get(version);
164                 
165                 if (context != null) {
166                         result = context.getDynamicType(classname);
167                 }
168                 
169                 return result;
170         }
171         
172         public String getOXMFileName(Version v) {
173                 return  AAIConstants.AAI_HOME_ETC_OXM + "aai_oxm_" + v.toString() + ".xml";
174         }
175         
176 }