2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2019 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2019 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=========================================================
22 package org.onap.aai.schemaif.oxm;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.concurrent.ConcurrentHashMap;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
33 import org.eclipse.persistence.dynamic.DynamicType;
34 import org.eclipse.persistence.internal.oxm.mappings.Descriptor;
35 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
36 import org.onap.aai.cl.eelf.LoggerFactory;
37 import org.onap.aai.nodes.NodeIngestor;
38 import org.onap.aai.schemaif.SchemaProviderException;
39 import org.onap.aai.schemaif.SchemaProviderMsgs;
40 import org.onap.aai.schemaif.definitions.VertexSchema;
41 import org.onap.aai.setup.SchemaVersion;
42 import org.onap.aai.setup.Translator;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.stereotype.Component;
47 * This class contains all of the logic for importing OXM model schemas from the available OXM
51 public class OxmSchemaLoader {
53 private static Translator translator;
54 private static NodeIngestor nodeIngestor;
56 private static Map<String, DynamicJAXBContext> versionContextMap = new ConcurrentHashMap<>();
57 private static Map<String, HashMap<String, DynamicType>> xmlElementLookup = new ConcurrentHashMap<>();
58 private static Map<String, HashMap<String, VertexSchema>> vertexLookup = new ConcurrentHashMap<>();
60 final static Pattern versionPattern = Pattern.compile("(?i)v(\\d*)");
62 private static org.onap.aai.cl.api.Logger logger =
63 LoggerFactory.getInstance().getLogger(OxmSchemaLoader.class.getName());
65 private OxmSchemaLoader() {
69 * This constructor presents an awkward marrying of Spring bean creation and static method use. This
70 * is technical debt that will need fixing.
72 * @param translator contains schema versions configuration
73 * @param nodeIngestor provides DynamicJAXBContext for the OXM version
76 public OxmSchemaLoader(Translator translator, NodeIngestor nodeIngestor) {
77 OxmSchemaLoader.translator = translator;
78 OxmSchemaLoader.nodeIngestor = nodeIngestor;
82 * Finds all OXM model files
84 * @throws SchemaProviderException
88 public synchronized static void loadModels() throws SchemaProviderException {
89 if (logger.isDebugEnabled()) {
90 logger.debug("Loading OXM Models");
93 for (SchemaVersion oxmVersion : translator.getSchemaVersions().getVersions()) {
94 DynamicJAXBContext jaxbContext = nodeIngestor.getContextForVersion(oxmVersion);
95 if (jaxbContext != null) {
96 loadModel(oxmVersion.toString(), jaxbContext);
101 private synchronized static void loadModel(String oxmVersion, DynamicJAXBContext jaxbContext) {
102 versionContextMap.put(oxmVersion, jaxbContext);
103 loadXmlLookupMap(oxmVersion, jaxbContext);
104 loadVertexLookupMap(oxmVersion, jaxbContext);
105 logger.info(SchemaProviderMsgs.LOADED_SCHEMA_FILE, oxmVersion);
109 * Retrieves the JAXB context for the specified OXM model version.
111 * @param version - The OXM version that we want the JAXB context for.
113 * @return - A JAXB context derived from the OXM model schema.
115 * @throws SchemaProviderException
117 public static DynamicJAXBContext getContextForVersion(String version) throws SchemaProviderException {
119 // If we haven't already loaded in the available OXM models, then do so now.
120 if (versionContextMap == null || versionContextMap.isEmpty()) {
122 } else if (!versionContextMap.containsKey(version)) {
123 throw new SchemaProviderException("Error loading oxm model: " + version);
126 return versionContextMap.get(version);
129 public static String getLatestVersion() throws SchemaProviderException {
131 // If we haven't already loaded in the available OXM models, then do so now.
132 if (versionContextMap == null || versionContextMap.isEmpty()) {
136 // If there are still no models available, then there's not much we can do...
137 if (versionContextMap.isEmpty()) {
138 throw new SchemaProviderException("No available OXM schemas to get latest version for.");
141 // Iterate over the available model versions to determine which is the most
143 Integer latestVersion = null;
144 String latestVersionStr = null;
145 for (String versionKey : versionContextMap.keySet()) {
147 Matcher matcher = versionPattern.matcher(versionKey);
148 if (matcher.find()) {
150 int currentVersion = Integer.valueOf(matcher.group(1));
152 if ((latestVersion == null) || (currentVersion > latestVersion)) {
153 latestVersion = currentVersion;
154 latestVersionStr = versionKey;
159 return latestVersionStr;
162 private static void loadXmlLookupMap(String version, DynamicJAXBContext jaxbContext) {
164 @SuppressWarnings("rawtypes")
165 List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
166 HashMap<String, DynamicType> types = new HashMap<String, DynamicType>();
168 for (@SuppressWarnings("rawtypes")
169 Descriptor desc : descriptorsList) {
171 DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
172 String entityName = desc.getDefaultRootElement();
173 types.put(entityName, entity);
175 xmlElementLookup.put(version, types);
178 private static void loadVertexLookupMap(String version, DynamicJAXBContext jaxbContext) {
180 @SuppressWarnings("rawtypes")
181 List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
182 HashMap<String, VertexSchema> vertexMap = new HashMap<String, VertexSchema>();
184 for (@SuppressWarnings("rawtypes")
185 Descriptor desc : descriptorsList) {
187 FromOxmVertexSchema vs = new FromOxmVertexSchema();
188 vs.fromOxm(desc.getDefaultRootElement(), jaxbContext, getXmlLookupMap(version));
189 vertexMap.put(vs.getName(), vs);
190 } catch (SchemaProviderException e) {
194 vertexLookup.put(version, vertexMap);
198 * Retrieves the list of all Loaded OXM versions.
200 * @return - A List of Strings of all loaded OXM versions.
202 * @throws SpikeException
204 public static List<String> getLoadedOXMVersions() throws SchemaProviderException {
205 // If we haven't already loaded in the available OXM models, then do so now.
206 if (versionContextMap == null || versionContextMap.isEmpty()) {
209 // If there are still no models available, then there's not much we can do...
210 if (versionContextMap.isEmpty()) {
211 logger.error(SchemaProviderMsgs.SCHEMA_LOAD_ERROR, "No available OXM schemas to get versions for.");
212 throw new SchemaProviderException("No available OXM schemas to get latest version for.");
214 List<String> versions = new ArrayList<String>();
215 for (String versionKey : versionContextMap.keySet()) {
216 Matcher matcher = versionPattern.matcher(versionKey);
217 if (matcher.find()) {
218 versions.add("V" + matcher.group(1));
224 public static HashMap<String, DynamicType> getXmlLookupMap(String version) {
225 return xmlElementLookup.get(version);
228 public static HashMap<String, VertexSchema> getVertexLookupForVersion(String version)
229 throws SchemaProviderException {
230 // If we haven't already loaded in the available OXM models, then do so now.
231 if (vertexLookup == null || vertexLookup.isEmpty()) {
233 } else if (!vertexLookup.containsKey(version)) {
234 throw new SchemaProviderException("Error loading oxm model: " + version);
236 return vertexLookup.get(version);