524dcbf00a4084537c2e3ad894771e0a086bde6f
[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 com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import org.eclipse.persistence.dynamic.DynamicType;
27 import org.eclipse.persistence.jaxb.JAXBContextProperties;
28 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
29 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
30 import org.onap.aai.util.AAIConstants;
31
32 import javax.xml.bind.JAXBException;
33 import java.io.File;
34 import java.io.FileInputStream;
35 import java.io.FileNotFoundException;
36 import java.io.InputStream;
37 import java.util.HashMap;
38 import java.util.Map;
39 import java.util.regex.Matcher;
40 import java.util.regex.Pattern;
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         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(ModelInjestor.class);
48
49         
50         /**
51          * Instantiates a new model injestor.
52          */
53         private ModelInjestor() {
54                 try {
55                         injestModels();
56                 } catch (FileNotFoundException | JAXBException e) {
57                         throw new RuntimeException(e);
58                 }
59         }
60         
61         private static class Helper {
62                 private static final ModelInjestor INSTANCE = new ModelInjestor();
63         }
64         
65         /**
66          * Gets the single instance of ModelInjestor.
67          *
68          * @return single instance of ModelInjestor
69          */
70         public synchronized static ModelInjestor getInstance() {
71                 return Helper.INSTANCE;
72         }
73         
74         /**
75          * Injest models.
76          *
77          * @throws FileNotFoundException the file not found exception
78          * @throws JAXBException the JAXB exception
79          */
80         private void injestModels() throws FileNotFoundException, JAXBException {
81                 
82                 for (Version version : Version.values()) {
83                         this.injestModel(version);
84                 }
85         }
86         
87         /**
88          * Injest model.
89          *
90          * @param version the version
91          * @throws JAXBException the JAXB exception
92          * @throws FileNotFoundException the file not found exception
93          */
94         private void injestModel (Version version) throws JAXBException, FileNotFoundException {
95                 String fileName = this.getOXMFileName(version);
96
97                 File oxmFile = new File(AAIConstants.AAI_HOME_ETC + fileName);
98
99                 // Check if the file exists on the path and if it doesn't exist then
100                 // Using classloader makes it easy to have a different oxm file
101                 // for unit testing the oxm files otherwise, you will be
102                 // stuck with using the main oxm for even the testing
103
104                 InputStream iStream;
105                 if(oxmFile.exists()){
106                         LOGGER.info("Oxm file exists on the system {}", oxmFile);
107                     iStream = new FileInputStream(oxmFile);
108                 } else {
109                         LOGGER.warn("Unable to find oxm file {} on the system so using classloader", oxmFile);
110                         iStream = getClass().getClassLoader().getResourceAsStream(fileName);
111                 }
112
113                 Map<String, Object> properties = new HashMap<String, Object>();
114                 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream);
115                 final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromOXM(this.getClass().getClassLoader(), properties);
116                 versionContextMap.put(version, jaxbContext);
117         }
118         
119         /**
120          * Gets the version from class name.
121          *
122          * @param classname the classname
123          * @return the version from class name
124          */
125         public Version getVersionFromClassName (String classname) {
126                 Matcher m = classNamePattern.matcher(classname);
127                 String version = "v12";
128                 if (m.find()) {
129                         version = m.group(1);
130                 }
131                 
132                 return Version.valueOf(version);
133         }
134         
135         /**
136          * Gets the context for URI.
137          *
138          * @param uri the uri
139          * @return the context for URI
140          */
141         public DynamicJAXBContext getContextForURI(String uri) {
142                 DynamicJAXBContext result = null;
143                 Matcher m = uriPattern.matcher(uri);
144                 Version version = null;
145                 if (m.find()) {
146                         version = Version.valueOf(m.group(1));
147                         result = versionContextMap.get(version);
148                 }
149                 
150                 return result;
151         }
152         
153         /**
154          * Gets the context for version.
155          *
156          * @param version the version
157          * @return the context for version
158          */
159         public DynamicJAXBContext getContextForVersion(Version version) {
160                 DynamicJAXBContext result = null;
161                 
162                 result = versionContextMap.get(version);
163                 
164                 
165                 return result;
166         }
167         
168         /**
169          * Gets the dynamic type for class name.
170          *
171          * @param classname the classname
172          * @return the dynamic type for class name
173          */
174         public DynamicType getDynamicTypeForClassName(String classname) {
175                 DynamicType result = null;
176                 DynamicJAXBContext context = null;
177
178                 Version version = this.getVersionFromClassName(classname);
179
180                 context = versionContextMap.get(version);
181                 
182                 if (context != null) {
183                         result = context.getDynamicType(classname);
184                 }
185                 
186                 return result;
187         }
188         
189         public String getOXMFileName(Version v) {
190                 // Changed the /oxm/aai_oxm_*.xml to oxm/aai_oxm_*.xml
191                 // Modified to load from input stream using getClass().getClassLoader()
192                 // As this will be able to relatively get the oxm if the oxm file is there
193                 // So if there is a src/main/resources/oxm/ and src/test/resources/oxm, the
194                 // test oxm will end up being used for tests and src oxm for source files
195                 // Don't change this unless you understand the details specified
196                 return  "oxm/aai_oxm_" + v.toString() + ".xml";
197         }
198         
199 }