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