5de9c748f5d36320c20019678e1344f47600ac59
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / introspection / PojoInjestor.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.jaxb.JAXBContextFactory;
25 import org.onap.aai.db.props.AAIProperties;
26
27 import javax.xml.bind.JAXBContext;
28 import javax.xml.bind.JAXBException;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32 public class PojoInjestor {
33         
34         private String POJO_CLASSPATH = "org.onap.aai.domain.yang";
35         private final Pattern classNamePattern = Pattern.compile("\\.(v\\d+)\\.");
36
37         public PojoInjestor() {
38         }
39         
40         public JAXBContext getContextForVersion(Version v) {
41                 JAXBContext context = null;
42                 try {
43                         if (!v.equals(AAIProperties.LATEST)) {
44                                 POJO_CLASSPATH += "." + v; 
45                         }
46                         context = JAXBContextFactory.createContext(POJO_CLASSPATH, this.getClass().getClassLoader());
47                 } catch (JAXBException e) {
48                         // TODO Auto-generated catch block
49                         e.printStackTrace();
50                 }
51                 
52                 return context;
53         }
54         public Version getVersion (String classname) {
55                 Matcher m = classNamePattern.matcher(classname);
56                 String version;
57                 if (m.find()) {
58                         version = m.group(1);
59                 } else {
60                         //only POJOs of old versions have the version number in their classnames
61                         //so if we can't find a version, default to the latest
62                         version = AAIProperties.LATEST.toString();
63                 }
64                 
65                 return Version.valueOf(version);
66         }
67         
68 }