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