8f5560261c9027c37ce67ddd9537784085ef8a24
[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.ArrayList;
25 import java.util.List;
26 import java.util.Map;
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;
40
41 public class OxmModelLoader {
42
43     private static Map<String, DynamicJAXBContext> versionContextMap =
44             new ConcurrentHashMap<>();
45
46     static final Pattern p = Pattern.compile("aai_oxm_(.*).xml");
47     static final Pattern versionPattern = Pattern.compile("V(\\d*)");
48
49     private static org.onap.aai.cl.api.Logger logger =
50             LoggerFactory.getInstance().getLogger(OxmModelLoader.class.getName());
51
52     private OxmModelLoader() {
53     }
54
55     /**
56      * Finds all OXM model files
57      *
58      * @throws SpikeException
59      * @throws IOException
60      *
61      */
62     public static synchronized void loadModels() throws CrudException {
63         SchemaIngestPropertyReader schemaIngestPropertyReader = new SchemaIngestPropertyReader();
64
65         SchemaLocationsBean schemaLocationsBean = new SchemaLocationsBean();
66         schemaLocationsBean.setNodeDirectory(schemaIngestPropertyReader.getNodeDir());
67         ConfigTranslator configTranslator = new OxmModelConfigTranslator(schemaLocationsBean);
68         NodeIngestor nodeIngestor = new NodeIngestor(configTranslator);
69
70         if (logger.isDebugEnabled()) {
71             logger.debug("Loading OXM Models");
72         }
73
74         for (Version oxmVersion : Version.values()) {
75             DynamicJAXBContext jaxbContext = nodeIngestor.getContextForVersion(oxmVersion);
76             if (jaxbContext != null) {
77                 loadModel(oxmVersion.toString().toLowerCase(), jaxbContext);
78             }
79         }
80     }
81
82
83     private static synchronized void loadModel(String oxmVersion, DynamicJAXBContext jaxbContext) {
84         versionContextMap.put(oxmVersion, jaxbContext);
85         logger.info(CrudServiceMsgs.LOADED_OXM_FILE, oxmVersion);
86     }
87
88     /**
89      * Retrieves the JAXB context for the specified OXM model version.
90      *
91      * @param version - The OXM version that we want the JAXB context for.
92      *
93      * @return - A JAXB context derived from the OXM model schema.
94      *
95      * @throws SpikeException
96      */
97     public static DynamicJAXBContext getContextForVersion(String version) throws CrudException {
98
99         // If we haven't already loaded in the available OXM models, then do so now.
100         if (versionContextMap == null || versionContextMap.isEmpty()) {
101             loadModels();
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);
105         }
106
107         return versionContextMap.get(version);
108     }
109
110     public static String getLatestVersion() throws CrudException {
111
112         // If we haven't already loaded in the available OXM models, then do so now.
113         if (versionContextMap == null || versionContextMap.isEmpty()) {
114             loadModels();
115         }
116
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);
122         }
123
124         // Iterate over the available model versions to determine which is the most
125         // recent.
126         Integer latestVersion = null;
127         String latestVersionStr = null;
128         for (String versionKey : versionContextMap.keySet()) {
129
130             Matcher matcher = versionPattern.matcher(versionKey.toUpperCase());
131             if (matcher.find()) {
132
133                 int currentVersion = Integer.parseInt(matcher.group(1));
134
135                 if ((latestVersion == null) || (currentVersion > latestVersion)) {
136                     latestVersion = currentVersion;
137                     latestVersionStr = versionKey;
138                 }
139             }
140         }
141
142         return latestVersionStr;
143     }
144
145     /**
146      * Retrieves the list of all Loaded OXM versions.
147      *
148      * @return - A List of Strings of all loaded OXM versions.
149      *
150      * @throws CrudException
151      */
152     public static List<String> getLoadedOXMVersions() throws CrudException {
153
154         // If we haven't already loaded in the available OXM models, then do so now.
155         if (versionContextMap == null || versionContextMap.isEmpty()) {
156             loadModels();
157         }
158
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);
164         }
165
166         List<String> versions = new ArrayList<String> ();
167         for (String versionKey : versionContextMap.keySet()) {
168
169             Matcher matcher = versionPattern.matcher(versionKey.toUpperCase());
170             if (matcher.find()) {
171                 versions.add ( "V" + matcher.group ( 1 ) );
172             }
173         }
174         return versions;
175     }
176
177     /**
178      * Retrieves the map of all JAXB context objects that have been created by importing the
179      * available OXM model schemas.
180      *
181      * @return - Map of JAXB context objects.
182      */
183     public static Map<String, DynamicJAXBContext> getVersionContextMap() {
184         return versionContextMap;
185     }
186
187     /**
188      * Assigns the map of all JAXB context objects.
189      *
190      * @param versionContextMap
191      */
192     public static void setVersionContextMap(Map<String, DynamicJAXBContext> versionContextMap) {
193         OxmModelLoader.versionContextMap = versionContextMap;
194     }
195 }