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