Process multi-OXM files
[aai/gizmo.git] / src / main / java / org / onap / schema / OxmModelLoader.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.schema;
22
23 import java.io.IOException;
24 import java.util.Map;
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;
38
39 public class OxmModelLoader {
40
41     private static Map<String, DynamicJAXBContext> versionContextMap =
42             new ConcurrentHashMap<>();
43
44     static final Pattern p = Pattern.compile("aai_oxm_(.*).xml");
45     static final Pattern versionPattern = Pattern.compile("V(\\d*)");
46
47     private static org.onap.aai.cl.api.Logger logger =
48             LoggerFactory.getInstance().getLogger(OxmModelLoader.class.getName());
49
50     private OxmModelLoader() {
51     }
52
53     /**
54      * Finds all OXM model files
55      *
56      * @throws SpikeException
57      * @throws IOException
58      *
59      */
60     public static synchronized void loadModels() throws CrudException {
61         SchemaIngestPropertyReader schemaIngestPropertyReader = new SchemaIngestPropertyReader();
62
63         SchemaLocationsBean schemaLocationsBean = new SchemaLocationsBean();
64         schemaLocationsBean.setNodeDirectory(schemaIngestPropertyReader.getNodeDir());
65         ConfigTranslator configTranslator = new OxmModelConfigTranslator(schemaLocationsBean);
66         NodeIngestor nodeIngestor = new NodeIngestor(configTranslator);
67
68         if (logger.isDebugEnabled()) {
69             logger.debug("Loading OXM Models");
70         }
71
72         for (Version oxmVersion : Version.values()) {
73             DynamicJAXBContext jaxbContext = nodeIngestor.getContextForVersion(oxmVersion);
74             if (jaxbContext != null) {
75                 loadModel(oxmVersion.toString().toLowerCase(), jaxbContext);
76             }
77         }
78     }
79
80
81     private static synchronized void loadModel(String oxmVersion, DynamicJAXBContext jaxbContext) {
82         versionContextMap.put(oxmVersion, jaxbContext);
83         logger.info(CrudServiceMsgs.LOADED_OXM_FILE, oxmVersion);
84     }
85
86     /**
87      * Retrieves the JAXB context for the specified OXM model version.
88      *
89      * @param version - The OXM version that we want the JAXB context for.
90      *
91      * @return - A JAXB context derived from the OXM model schema.
92      *
93      * @throws SpikeException
94      */
95     public static DynamicJAXBContext getContextForVersion(String version) throws CrudException {
96
97         // If we haven't already loaded in the available OXM models, then do so now.
98         if (versionContextMap == null || versionContextMap.isEmpty()) {
99             loadModels();
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);
103         }
104
105         return versionContextMap.get(version);
106     }
107
108     public static String getLatestVersion() throws CrudException {
109
110         // If we haven't already loaded in the available OXM models, then do so now.
111         if (versionContextMap == null || versionContextMap.isEmpty()) {
112             loadModels();
113         }
114
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);
120         }
121
122         // Iterate over the available model versions to determine which is the most
123         // recent.
124         Integer latestVersion = null;
125         String latestVersionStr = null;
126         for (String versionKey : versionContextMap.keySet()) {
127
128             Matcher matcher = versionPattern.matcher(versionKey.toUpperCase());
129             if (matcher.find()) {
130
131                 int currentVersion = Integer.parseInt(matcher.group(1));
132
133                 if ((latestVersion == null) || (currentVersion > latestVersion)) {
134                     latestVersion = currentVersion;
135                     latestVersionStr = versionKey;
136                 }
137             }
138         }
139
140         return latestVersionStr;
141     }
142
143     /**
144      * Retrieves the map of all JAXB context objects that have been created by importing the
145      * available OXM model schemas.
146      *
147      * @return - Map of JAXB context objects.
148      */
149     public static Map<String, DynamicJAXBContext> getVersionContextMap() {
150         return versionContextMap;
151     }
152
153     /**
154      * Assigns the map of all JAXB context objects.
155      *
156      * @param versionContextMap
157      */
158     public static void setVersionContextMap(Map<String, DynamicJAXBContext> versionContextMap) {
159         OxmModelLoader.versionContextMap = versionContextMap;
160     }
161 }