Update the license for 2017-2018 license
[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-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.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         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(PojoInjestor.class);
34         
35         private String POJO_CLASSPATH = "org.onap.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                         LOGGER.error(e.getMessage(),e);
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 }