Add missing tests
[aai/aai-common.git] / aai-utils / src / main / java / org / onap / aaiutils / oxm / OxmModelLoader.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property.
6  * Copyright © 2017-2018 Amdocs
7  * All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.aaiutils.oxm;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.concurrent.ConcurrentHashMap;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
35 import javax.xml.bind.JAXBException;
36 import org.eclipse.persistence.jaxb.JAXBContextProperties;
37 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
38 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
39 import org.onap.aai.cl.api.Logger;
40 import org.onap.aai.cl.eelf.LoggerFactory;
41 import org.springframework.core.io.Resource;
42 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
43 import org.springframework.core.io.support.ResourcePatternResolver;
44
45
46 public class OxmModelLoader {
47
48     private static final Pattern AAI_OXM_FILE_PATTERN = Pattern.compile("aai_oxm_(.*).xml");
49     private static Map<String, DynamicJAXBContext> versionContextMap = new ConcurrentHashMap<>();
50     private static final Logger LOGGER = LoggerFactory.getInstance()
51         .getLogger(OxmModelLoader.class.getName());
52
53     public synchronized static void loadModels() throws Exception {
54         OxmModelLoader.loadModels("classpath*:/oxm/aai_oxm*.xml", AAI_OXM_FILE_PATTERN);
55     }
56
57     synchronized static void loadModels(String oxmResourcesPattern, Pattern aai_oxm_file_pattern) throws Exception {
58         Resource[] resources = getResources(oxmResourcesPattern);
59
60         for (Resource resource : resources) {
61             Matcher matcher = aai_oxm_file_pattern.matcher(resource.getFilename());
62
63             if (matcher.matches()) {
64                 try {
65                     OxmModelLoader.loadModel(matcher.group(1), resource);
66                 } catch (Exception e) {
67                     LOGGER.error(OxmModelLoaderMsgs.OXM_LOAD_ERROR, "Failed to load " + resource.getFilename()
68                         + ": " + e.getMessage());
69                     throw new Exception("Failed to load schema");
70                 }
71             }
72         }
73     }
74
75     private static Resource[] getResources(String oxmResourcesPattern) throws Exception {
76         ClassLoader cl = OxmModelLoader.class.getClassLoader();
77         ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
78
79         Resource[] resources = resolver.getResources(oxmResourcesPattern);
80         if (resources.length == 0) {
81             LOGGER.error(OxmModelLoaderMsgs.OXM_LOAD_ERROR, "No OXM schema files found on classpath");
82             throw new Exception("Failed to load schema");
83         }
84         return resources;
85
86
87     }
88
89     public static DynamicJAXBContext getContextForVersion(String version) throws Exception {
90         if (versionContextMap == null || versionContextMap.isEmpty()) {
91             loadModels();
92         } else if (!versionContextMap.containsKey(version)) {
93             String filename = OxmModelLoaderConstants.AaiUtils_HOME_MODEL + "aai_oxm_" + version + ".xml";
94             try {
95                 loadModel(version, new File(filename));
96             } catch (Exception e) {
97                 throw new FileNotFoundException(filename);
98             }
99         }
100
101         return versionContextMap.get(version);
102     }
103
104     public static Map<String, DynamicJAXBContext> getVersionContextMap() {
105         return Collections.unmodifiableMap(versionContextMap);
106     }
107
108     public static void setVersionContextMap(Map<String, DynamicJAXBContext> versionContextMap) {
109         OxmModelLoader.versionContextMap = versionContextMap;
110     }
111
112     private synchronized static void loadModel(String version, File file) throws JAXBException, IOException {
113         InputStream inputStream = new FileInputStream(file);
114         loadModel(version, file.getName(), inputStream);
115     }
116
117     private synchronized static void loadModel(String version, Resource resource)
118         throws JAXBException, IOException {
119         InputStream inputStream = resource.getInputStream();
120         loadModel(version, resource.getFilename(), inputStream);
121     }
122
123     private synchronized static void loadModel(String version, String resourceName, InputStream inputStream)
124         throws JAXBException, IOException {
125
126         Map<String, Object> properties = new HashMap<>();
127         properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, inputStream);
128
129         final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory
130             .createContextFromOXM(Thread.currentThread().getContextClassLoader(), properties);
131
132         versionContextMap.put(version, jaxbContext);
133
134         LOGGER.info(OxmModelLoaderMsgs.LOADED_OXM_FILE, resourceName);
135     }
136
137 }