Removed below sonar issues:
[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 org.eclipse.persistence.dynamic.DynamicType;
24 import org.eclipse.persistence.jaxb.JAXBContextProperties;
25 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
26 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
27 import org.openecomp.aai.util.AAIConstants;
28
29 import javax.xml.bind.JAXBException;
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.FileNotFoundException;
33 import java.io.InputStream;
34 import java.util.HashMap;
35 import java.util.Map;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38
39 public class ModelInjestor {
40         
41         private Map<Version, DynamicJAXBContext> versionContextMap = new HashMap<>();
42         private static final Pattern classNamePattern = Pattern.compile("\\.(v\\d+)\\.");
43         private static final Pattern uriPattern =       Pattern.compile("(v\\d+)\\/");
44
45         
46         /**
47          * Instantiates a new model injestor.
48          */
49         private ModelInjestor() {
50                 try {
51                         injestModels();
52                 } catch (FileNotFoundException | JAXBException e) {
53                         throw new RuntimeException(e);
54                 }
55         }
56         
57         private static class Helper {
58                 private static final ModelInjestor INSTANCE = new ModelInjestor();
59         }
60         
61         /**
62          * Gets the single instance of ModelInjestor.
63          *
64          * @return single instance of ModelInjestor
65          */
66         public synchronized static ModelInjestor getInstance() {
67                 return Helper.INSTANCE;
68         }
69         
70         /**
71          * Injest models.
72          *
73          * @throws FileNotFoundException the file not found exception
74          * @throws JAXBException the JAXB exception
75          */
76         private void injestModels() throws FileNotFoundException, JAXBException {
77                 
78                 for (Version version : Version.values()) {
79                         this.injestModel(version);
80                 }
81         }
82         
83         /**
84          * Injest model.
85          *
86          * @param version the version
87          * @throws JAXBException the JAXB exception
88          * @throws FileNotFoundException the file not found exception
89          */
90         private void injestModel (Version version) throws JAXBException, FileNotFoundException {
91                 String fileName = this.getOXMFileName(version);
92                 InputStream iStream = new FileInputStream(new File(fileName));
93                 Map<String, Object> properties = new HashMap<String, Object>(); 
94                 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream);
95                 final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromOXM(this.getClass().getClassLoader(), properties);
96                 versionContextMap.put(version, jaxbContext);
97         }
98         
99         /**
100          * Gets the version from class name.
101          *
102          * @param classname the classname
103          * @return the version from class name
104          */
105         public Version getVersionFromClassName (String classname) {
106                 Matcher m = classNamePattern.matcher(classname);
107                 String version = "v2"; //for the OXM, only the v2 ones don't include a model name, hence this default
108                 if (m.find()) {
109                         version = m.group(1);
110                 }
111                 
112                 return Version.valueOf(version);
113         }
114         
115         /**
116          * Gets the context for URI.
117          *
118          * @param uri the uri
119          * @return the context for URI
120          */
121         public DynamicJAXBContext getContextForURI(String uri) {
122                 DynamicJAXBContext result = null;
123                 Matcher m = uriPattern.matcher(uri);
124                 Version version = null;
125                 if (m.find()) {
126                         version = Version.valueOf(m.group(1));
127                         result = versionContextMap.get(version);
128                 }
129                 
130                 return result;
131         }
132         
133         /**
134          * Gets the context for version.
135          *
136          * @param version the version
137          * @return the context for version
138          */
139         public DynamicJAXBContext getContextForVersion(Version version) {
140                 DynamicJAXBContext result = null;
141                 
142                 result = versionContextMap.get(version);
143                 
144                 
145                 return result;
146         }
147         
148         /**
149          * Gets the dynamic type for class name.
150          *
151          * @param classname the classname
152          * @return the dynamic type for class name
153          */
154         public DynamicType getDynamicTypeForClassName(String classname) {
155                 DynamicType result = null;
156                 DynamicJAXBContext context = null;
157
158                 Version version = this.getVersionFromClassName(classname);
159
160                 context = versionContextMap.get(version);
161                 
162                 if (context != null) {
163                         result = context.getDynamicType(classname);
164                 }
165                 
166                 return result;
167         }
168         
169         public String getOXMFileName(Version v) {
170                 return  AAIConstants.AAI_HOME_ETC_OXM + "aai_oxm_" + v.toString() + ".xml";
171         }
172         
173 }