2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.aai.datarouter.util;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.Collection;
31 import java.util.NoSuchElementException;
32 import java.util.concurrent.ConcurrentHashMap;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
36 import javax.ws.rs.core.Response.Status;
37 import javax.xml.bind.JAXBException;
39 import org.eclipse.persistence.jaxb.JAXBContextProperties;
40 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
41 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
42 import org.onap.aai.cl.eelf.LoggerFactory;
43 import org.onap.aai.datarouter.logging.DataRouterMsgs;
44 import org.springframework.core.io.Resource;
45 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
46 import org.springframework.core.io.support.ResourcePatternResolver;
48 public class OxmModelLoader {
50 private static Map<String, DynamicJAXBContext> versionContextMap = new ConcurrentHashMap<>();
51 private static List<ExternalOxmModelProcessor> oxmModelProcessorRegistry = new ArrayList<>();
52 static final Pattern p = Pattern.compile("aai_oxm_(.*).xml");
54 private static org.onap.aai.cl.api.Logger logger = LoggerFactory.getInstance()
55 .getLogger(OxmModelLoader.class.getName());
57 public static synchronized void loadModels() throws FileNotFoundException {
59 ClassLoader cl = OxmModelLoader.class.getClassLoader();
60 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
63 resources = resolver.getResources("classpath*:/oxm/aai_oxm*.xml");
64 } catch (IOException ex) {
65 logger.error(DataRouterMsgs.LOAD_OXM_ERROR, ex.getMessage());
66 throw new FileNotFoundException("Unable to load OXM models from schema path : /oxm/aai_oxm*.xml");
69 if (resources.length == 0) {
70 logger.error(DataRouterMsgs.LOAD_OXM_ERROR, "No OXM schema files found on classpath");
71 throw new FileNotFoundException("Unable to load OXM models from schema path : /oxm/aai_oxm*.xml");
74 for (Resource resource : resources) {
75 Matcher matcher = p.matcher(resource.getFilename());
77 if (matcher.matches()) {
79 OxmModelLoader.loadModel(matcher.group(1), resource.getFilename(),resource.getInputStream());
80 } catch (Exception e) {
81 logger.error(DataRouterMsgs.LOAD_OXM_ERROR, "Failed to load " + resource.getFilename()
82 + ": " + e.getMessage());
91 private static synchronized void loadModel(String version,String resourceName,InputStream inputStream) throws JAXBException {
92 Map<String, Object> properties = new HashMap<>();
93 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, inputStream);
94 final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory
95 .createContextFromOXM(Thread.currentThread().getContextClassLoader(), properties);
96 versionContextMap.put(version, jaxbContext);
97 if ( oxmModelProcessorRegistry != null) {
98 for ( ExternalOxmModelProcessor processor : oxmModelProcessorRegistry ) {
99 processor.onOxmVersionChange(Version.valueOf(version), jaxbContext );
102 logger.info(DataRouterMsgs.LOADED_OXM_FILE, resourceName);
105 public static DynamicJAXBContext getContextForVersion(String version) throws NoSuchElementException, FileNotFoundException {
106 if (versionContextMap == null || versionContextMap.isEmpty()) {
108 } else if (!versionContextMap.containsKey(version)) {
109 throw new NoSuchElementException(Status.NOT_FOUND.toString());
113 return versionContextMap.get(version);
116 public static Map<String, DynamicJAXBContext> getVersionContextMap() {
117 return versionContextMap;
120 public static void setVersionContextMap(Map<String, DynamicJAXBContext> versionContextMap) {
121 OxmModelLoader.versionContextMap = versionContextMap;
124 public static synchronized void registerExternalOxmModelProcessors(Collection<ExternalOxmModelProcessor> processors) {
125 if(processors != null) {
126 for(ExternalOxmModelProcessor processor : processors) {
127 if(!oxmModelProcessorRegistry.contains(processor)) {
128 oxmModelProcessorRegistry.add(processor);