Update the license for 2017-2018 license
[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-2018 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 package org.onap.aai.introspection;
21
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
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         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(ModelInjestor.class);
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
95                 File oxmFile = new File(AAIConstants.AAI_HOME_ETC + fileName);
96
97                 // Check if the file exists on the path and if it doesn't exist then
98                 // Using classloader makes it easy to have a different oxm file
99                 // for unit testing the oxm files otherwise, you will be
100                 // stuck with using the main oxm for even the testing
101
102                 InputStream iStream;
103                 if(oxmFile.exists()){
104                         LOGGER.info("Oxm file exists on the system {}", oxmFile);
105                     iStream = new FileInputStream(oxmFile);
106                 } else {
107                         LOGGER.warn("Unable to find oxm file {} on the system so using classloader", oxmFile);
108                         iStream = getClass().getClassLoader().getResourceAsStream(fileName);
109                 }
110
111                 Map<String, Object> properties = new HashMap<String, Object>();
112                 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream);
113                 final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromOXM(this.getClass().getClassLoader(), properties);
114                 versionContextMap.put(version, jaxbContext);
115         }
116         
117         /**
118          * Gets the version from class name.
119          *
120          * @param classname the classname
121          * @return the version from class name
122          */
123         public Version getVersionFromClassName (String classname) {
124                 Matcher m = classNamePattern.matcher(classname);
125                 String version = "v12";
126                 if (m.find()) {
127                         version = m.group(1);
128                 }
129                 
130                 return Version.valueOf(version);
131         }
132         
133         /**
134          * Gets the context for URI.
135          *
136          * @param uri the uri
137          * @return the context for URI
138          */
139         public DynamicJAXBContext getContextForURI(String uri) {
140                 DynamicJAXBContext result = null;
141                 Matcher m = uriPattern.matcher(uri);
142                 Version version = null;
143                 if (m.find()) {
144                         version = Version.valueOf(m.group(1));
145                         result = versionContextMap.get(version);
146                 }
147                 
148                 return result;
149         }
150         
151         /**
152          * Gets the context for version.
153          *
154          * @param version the version
155          * @return the context for version
156          */
157         public DynamicJAXBContext getContextForVersion(Version version) {
158                 DynamicJAXBContext result = null;
159                 
160                 result = versionContextMap.get(version);
161                 
162                 
163                 return result;
164         }
165         
166         /**
167          * Gets the dynamic type for class name.
168          *
169          * @param classname the classname
170          * @return the dynamic type for class name
171          */
172         public DynamicType getDynamicTypeForClassName(String classname) {
173                 DynamicType result = null;
174                 DynamicJAXBContext context = null;
175
176                 Version version = this.getVersionFromClassName(classname);
177
178                 context = versionContextMap.get(version);
179                 
180                 if (context != null) {
181                         result = context.getDynamicType(classname);
182                 }
183                 
184                 return result;
185         }
186         
187         public String getOXMFileName(Version v) {
188                 // Changed the /oxm/aai_oxm_*.xml to oxm/aai_oxm_*.xml
189                 // Modified to load from input stream using getClass().getClassLoader()
190                 // As this will be able to relatively get the oxm if the oxm file is there
191                 // So if there is a src/main/resources/oxm/ and src/test/resources/oxm, the
192                 // test oxm will end up being used for tests and src oxm for source files
193                 // Don't change this unless you understand the details specified
194                 return  "oxm/aai_oxm_" + v.toString() + ".xml";
195         }
196         
197 }