2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.config.oxm;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.HashMap;
28 import java.util.HashSet;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
34 import org.eclipse.persistence.jaxb.JAXBContextProperties;
35 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
36 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
37 import org.onap.aai.cl.api.Logger;
38 import org.onap.aai.cl.eelf.LoggerFactory;
39 import org.onap.aai.sparky.logging.AaiUiMsgs;
40 import org.springframework.core.io.Resource;
41 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
42 import org.springframework.core.io.support.ResourcePatternResolver;
44 public class OxmModelLoader {
46 private static final Logger LOG = LoggerFactory.getInstance().getLogger(OxmModelLoader.class);
49 * The intent of this parameter is to be able to programmatically over-ride the latest AAI schema
50 * version discovered from the aai-schema jar file. This property is optional, but if set on the
51 * bean or by another class in the system, then it will override the spec version that is loaded.
53 * If the latestVersionOverride is greater than 0 then it will set the latest version to the
54 * specified version, and that stream will be returned if available.
57 protected int oxmApiVersionOverride;
58 protected Set<OxmModelProcessor> processors;
59 private int latestVersionNum = 0;
61 private final static Pattern p = Pattern.compile("aai_oxm_(v)(.*).xml");
63 public OxmModelLoader() {
64 this(-1, new HashSet<OxmModelProcessor>());
67 public OxmModelLoader(int apiVersionOverride, Set<OxmModelProcessor> oxmModelProcessors) {
68 this.oxmApiVersionOverride = apiVersionOverride;
69 this.processors = oxmModelProcessors;
72 protected synchronized Map<Integer, InputStream> getStreamHandlesForOxmFromResource() {
73 Map<Integer, InputStream> listOfOxmFiles = new HashMap<Integer, InputStream>();
74 ClassLoader oxmClassLoader = OxmModelLoader.class.getClassLoader();
75 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(oxmClassLoader);
76 Resource[] resources = null;
78 resources = resolver.getResources("classpath*:/oxm/aai_oxm*.xml");
79 } catch (IOException ex) {
80 LOG.error(AaiUiMsgs.OXM_LOADING_ERROR, ex.getMessage());
83 if (resources == null) {
84 LOG.error(AaiUiMsgs.OXM_LOADING_ERROR, "No OXM schema files found on classpath");
87 for (Resource resource : resources) {
88 Matcher m = p.matcher(resource.getFilename());
92 listOfOxmFiles.put(new Integer(m.group(2)), resource.getInputStream());
93 } catch (Exception e) {
94 LOG.error(AaiUiMsgs.OXM_LOADING_ERROR, resource.getFilename(), e.getMessage());
98 return listOfOxmFiles;
104 * @param inputStream file handle for oxm
106 protected void loadModel(InputStream inputStream) {
107 Map<String, Object> properties = new HashMap<String, Object>();
108 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, inputStream);
110 final DynamicJAXBContext oxmContext = DynamicJAXBContextFactory
111 .createContextFromOXM(Thread.currentThread().getContextClassLoader(), properties);
113 parseOxmContext(oxmContext);
114 // populateSearchableOxmModel();
115 LOG.info(AaiUiMsgs.OXM_LOAD_SUCCESS, String.valueOf(latestVersionNum));
116 } catch (Exception exc) {
117 LOG.info(AaiUiMsgs.OXM_PARSE_ERROR_NONVERBOSE);
118 LOG.error(AaiUiMsgs.OXM_PARSE_ERROR_VERBOSE, "OXM v" + latestVersionNum, exc.getMessage());
123 * Load the latest oxm model.
125 public synchronized void loadLatestOxmModel() {
127 LOG.info(AaiUiMsgs.INITIALIZE_OXM_MODEL_LOADER);
129 // find handles for available oxm models
130 final Map<Integer, InputStream> listOfOxmStreams = getStreamHandlesForOxmFromResource();
131 if (listOfOxmStreams.isEmpty()) {
132 LOG.error(AaiUiMsgs.OXM_FILE_NOT_FOUND);
136 InputStream stream = null;
138 if (oxmApiVersionOverride > 0) {
139 latestVersionNum = oxmApiVersionOverride;
140 LOG.warn(AaiUiMsgs.WARN_GENERIC, "Overriding AAI Schema with version = " + latestVersionNum);
141 stream = listOfOxmStreams.get(latestVersionNum);
144 for (Integer key : listOfOxmStreams.keySet()) {
145 if (key.intValue() > latestVersionNum) {
146 latestVersionNum = key.intValue();
147 stream = listOfOxmStreams.get(key);
152 // load the latest oxm file
157 public int getLatestVersionNum() {
158 return latestVersionNum;
161 public void setLatestVersionNum(int latestVersionNum) {
162 this.latestVersionNum = latestVersionNum;
166 * Parses the oxm context.
168 * @param oxmContext the oxm context
170 private void parseOxmContext(DynamicJAXBContext oxmContext) {
172 if (processors != null && processors.size() > 0) {
174 for (OxmModelProcessor processor : processors) {
176 processor.processOxmModel(oxmContext);