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;
24 import java.util.ArrayList;
25 import java.util.List;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30 import javax.ws.rs.core.Response.Status;
31 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
32 import org.onap.aai.cl.eelf.LoggerFactory;
33 import org.onap.aai.nodes.NodeIngestor;
34 import org.onap.aai.setup.ConfigTranslator;
35 import org.onap.aai.setup.SchemaLocationsBean;
36 import org.onap.aai.setup.Version;
37 import org.onap.crud.exception.CrudException;
38 import org.onap.crud.logging.CrudServiceMsgs;
39 import org.onap.schema.util.SchemaIngestPropertyReader;
41 public class OxmModelLoader {
43 private static Map<String, DynamicJAXBContext> versionContextMap =
44 new ConcurrentHashMap<>();
46 static final Pattern p = Pattern.compile("aai_oxm_(.*).xml");
47 static final Pattern versionPattern = Pattern.compile("V(\\d*)");
49 private static org.onap.aai.cl.api.Logger logger =
50 LoggerFactory.getInstance().getLogger(OxmModelLoader.class.getName());
52 private OxmModelLoader() {
56 * Finds all OXM model files
58 * @throws SpikeException
62 public static synchronized void loadModels() throws CrudException {
63 SchemaIngestPropertyReader schemaIngestPropertyReader = new SchemaIngestPropertyReader();
65 SchemaLocationsBean schemaLocationsBean = new SchemaLocationsBean();
66 schemaLocationsBean.setNodeDirectory(schemaIngestPropertyReader.getNodeDir());
67 ConfigTranslator configTranslator = new OxmModelConfigTranslator(schemaLocationsBean);
68 NodeIngestor nodeIngestor = new NodeIngestor(configTranslator);
70 if (logger.isDebugEnabled()) {
71 logger.debug("Loading OXM Models");
74 for (Version oxmVersion : Version.values()) {
75 DynamicJAXBContext jaxbContext = nodeIngestor.getContextForVersion(oxmVersion);
76 if (jaxbContext != null) {
77 loadModel(oxmVersion.toString().toLowerCase(), jaxbContext);
83 private static synchronized void loadModel(String oxmVersion, DynamicJAXBContext jaxbContext) {
84 versionContextMap.put(oxmVersion, jaxbContext);
85 logger.info(CrudServiceMsgs.LOADED_OXM_FILE, oxmVersion);
89 * Retrieves the JAXB context for the specified OXM model version.
91 * @param version - The OXM version that we want the JAXB context for.
93 * @return - A JAXB context derived from the OXM model schema.
95 * @throws SpikeException
97 public static DynamicJAXBContext getContextForVersion(String version) throws CrudException {
99 // If we haven't already loaded in the available OXM models, then do so now.
100 if (versionContextMap == null || versionContextMap.isEmpty()) {
102 } else if (!versionContextMap.containsKey(version)) {
103 logger.error(CrudServiceMsgs.OXM_LOAD_ERROR, "Error loading oxm model: " + version);
104 throw new CrudException("Error loading oxm model: " + version, Status.INTERNAL_SERVER_ERROR);
107 return versionContextMap.get(version);
110 public static String getLatestVersion() throws CrudException {
112 // If we haven't already loaded in the available OXM models, then do so now.
113 if (versionContextMap == null || versionContextMap.isEmpty()) {
117 // If there are still no models available, then there's not much we can do...
118 if (versionContextMap.isEmpty()) {
119 logger.error(CrudServiceMsgs.OXM_LOAD_ERROR, "No available OXM schemas to get latest version for.");
120 throw new CrudException("No available OXM schemas to get latest version for.",
121 Status.INTERNAL_SERVER_ERROR);
124 // Iterate over the available model versions to determine which is the most
126 Integer latestVersion = null;
127 String latestVersionStr = null;
128 for (String versionKey : versionContextMap.keySet()) {
130 Matcher matcher = versionPattern.matcher(versionKey.toUpperCase());
131 if (matcher.find()) {
133 int currentVersion = Integer.parseInt(matcher.group(1));
135 if ((latestVersion == null) || (currentVersion > latestVersion)) {
136 latestVersion = currentVersion;
137 latestVersionStr = versionKey;
142 return latestVersionStr;
146 * Retrieves the list of all Loaded OXM versions.
148 * @return - A List of Strings of all loaded OXM versions.
150 * @throws CrudException
152 public static List<String> getLoadedOXMVersions() throws CrudException {
154 // If we haven't already loaded in the available OXM models, then do so now.
155 if (versionContextMap == null || versionContextMap.isEmpty()) {
159 // If there are still no models available, then there's not much we can do...
160 if (versionContextMap.isEmpty()) {
161 logger.error(CrudServiceMsgs.OXM_LOAD_ERROR, "No available OXM schemas to get versions for.");
162 throw new CrudException("No available OXM schemas to get versions for.",
163 Status.INTERNAL_SERVER_ERROR);
166 List<String> versions = new ArrayList<String> ();
167 for (String versionKey : versionContextMap.keySet()) {
169 Matcher matcher = versionPattern.matcher(versionKey.toUpperCase());
170 if (matcher.find()) {
171 versions.add ( "V" + matcher.group ( 1 ) );
178 * Retrieves the map of all JAXB context objects that have been created by importing the
179 * available OXM model schemas.
181 * @return - Map of JAXB context objects.
183 public static Map<String, DynamicJAXBContext> getVersionContextMap() {
184 return versionContextMap;
188 * Assigns the map of all JAXB context objects.
190 * @param versionContextMap
192 public static void setVersionContextMap(Map<String, DynamicJAXBContext> versionContextMap) {
193 OxmModelLoader.versionContextMap = versionContextMap;