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.schema;
23 import java.io.IOException;
25 import java.util.concurrent.ConcurrentHashMap;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28 import javax.ws.rs.core.Response.Status;
29 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
30 import org.onap.aai.cl.eelf.LoggerFactory;
31 import org.onap.aai.nodes.NodeIngestor;
32 import org.onap.aai.setup.ConfigTranslator;
33 import org.onap.aai.setup.SchemaLocationsBean;
34 import org.onap.aai.setup.Version;
35 import org.onap.crud.exception.CrudException;
36 import org.onap.crud.logging.CrudServiceMsgs;
37 import org.onap.schema.util.SchemaIngestPropertyReader;
39 public class OxmModelLoader {
41 private static Map<String, DynamicJAXBContext> versionContextMap =
42 new ConcurrentHashMap<>();
44 static final Pattern p = Pattern.compile("aai_oxm_(.*).xml");
45 static final Pattern versionPattern = Pattern.compile("V(\\d*)");
47 private static org.onap.aai.cl.api.Logger logger =
48 LoggerFactory.getInstance().getLogger(OxmModelLoader.class.getName());
50 private OxmModelLoader() {
54 * Finds all OXM model files
56 * @throws SpikeException
60 public static synchronized void loadModels() throws CrudException {
61 SchemaIngestPropertyReader schemaIngestPropertyReader = new SchemaIngestPropertyReader();
63 SchemaLocationsBean schemaLocationsBean = new SchemaLocationsBean();
64 schemaLocationsBean.setNodeDirectory(schemaIngestPropertyReader.getNodeDir());
65 ConfigTranslator configTranslator = new OxmModelConfigTranslator(schemaLocationsBean);
66 NodeIngestor nodeIngestor = new NodeIngestor(configTranslator);
68 if (logger.isDebugEnabled()) {
69 logger.debug("Loading OXM Models");
72 for (Version oxmVersion : Version.values()) {
73 DynamicJAXBContext jaxbContext = nodeIngestor.getContextForVersion(oxmVersion);
74 if (jaxbContext != null) {
75 loadModel(oxmVersion.toString().toLowerCase(), jaxbContext);
81 private static synchronized void loadModel(String oxmVersion, DynamicJAXBContext jaxbContext) {
82 versionContextMap.put(oxmVersion, jaxbContext);
83 logger.info(CrudServiceMsgs.LOADED_OXM_FILE, oxmVersion);
87 * Retrieves the JAXB context for the specified OXM model version.
89 * @param version - The OXM version that we want the JAXB context for.
91 * @return - A JAXB context derived from the OXM model schema.
93 * @throws SpikeException
95 public static DynamicJAXBContext getContextForVersion(String version) throws CrudException {
97 // If we haven't already loaded in the available OXM models, then do so now.
98 if (versionContextMap == null || versionContextMap.isEmpty()) {
100 } else if (!versionContextMap.containsKey(version)) {
101 logger.error(CrudServiceMsgs.OXM_LOAD_ERROR, "Error loading oxm model: " + version);
102 throw new CrudException("Error loading oxm model: " + version, Status.INTERNAL_SERVER_ERROR);
105 return versionContextMap.get(version);
108 public static String getLatestVersion() throws CrudException {
110 // If we haven't already loaded in the available OXM models, then do so now.
111 if (versionContextMap == null || versionContextMap.isEmpty()) {
115 // If there are still no models available, then there's not much we can do...
116 if (versionContextMap.isEmpty()) {
117 logger.error(CrudServiceMsgs.OXM_LOAD_ERROR, "No available OXM schemas to get latest version for.");
118 throw new CrudException("No available OXM schemas to get latest version for.",
119 Status.INTERNAL_SERVER_ERROR);
122 // Iterate over the available model versions to determine which is the most
124 Integer latestVersion = null;
125 String latestVersionStr = null;
126 for (String versionKey : versionContextMap.keySet()) {
128 Matcher matcher = versionPattern.matcher(versionKey.toUpperCase());
129 if (matcher.find()) {
131 int currentVersion = Integer.parseInt(matcher.group(1));
133 if ((latestVersion == null) || (currentVersion > latestVersion)) {
134 latestVersion = currentVersion;
135 latestVersionStr = versionKey;
140 return latestVersionStr;
144 * Retrieves the map of all JAXB context objects that have been created by importing the
145 * available OXM model schemas.
147 * @return - Map of JAXB context objects.
149 public static Map<String, DynamicJAXBContext> getVersionContextMap() {
150 return versionContextMap;
154 * Assigns the map of all JAXB context objects.
156 * @param versionContextMap
158 public static void setVersionContextMap(Map<String, DynamicJAXBContext> versionContextMap) {
159 OxmModelLoader.versionContextMap = versionContextMap;