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