Adding UI extensibility
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / config / oxm / OxmModelLoader.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.config.oxm;
24
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Map;
30 import java.util.Set;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33
34 import org.eclipse.persistence.jaxb.JAXBContextProperties;
35 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
36 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
37 import org.onap.aai.cl.api.Logger;
38 import org.onap.aai.cl.eelf.LoggerFactory;
39 import org.onap.aai.sparky.logging.AaiUiMsgs;
40 import org.springframework.core.io.Resource;
41 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
42 import org.springframework.core.io.support.ResourcePatternResolver;
43
44 public class OxmModelLoader {
45
46   private static final Logger LOG = LoggerFactory.getInstance().getLogger(OxmModelLoader.class);
47
48   /*
49    * The intent of this parameter is to be able to programmatically over-ride the latest AAI schema
50    * version discovered from the aai-schema jar file. This property is optional, but if set on the
51    * bean or by another class in the system, then it will override the spec version that is loaded.
52    * 
53    * If the latestVersionOverride is greater than 0 then it will set the latest version to the
54    * specified version, and that stream will be returned if available.
55    */
56
57   protected int oxmApiVersionOverride;
58   protected Set<OxmModelProcessor> processors;
59   private int latestVersionNum = 0;
60
61   private final static Pattern p = Pattern.compile("aai_oxm_(v)(.*).xml");
62
63   public OxmModelLoader() {
64     this(-1, new HashSet<OxmModelProcessor>());
65   }
66
67   public OxmModelLoader(int apiVersionOverride, Set<OxmModelProcessor> oxmModelProcessors) {
68     this.oxmApiVersionOverride = apiVersionOverride;
69     this.processors = oxmModelProcessors;
70   }
71
72   protected synchronized Map<Integer, InputStream> getStreamHandlesForOxmFromResource() {
73     Map<Integer, InputStream> listOfOxmFiles = new HashMap<Integer, InputStream>();
74     ClassLoader oxmClassLoader = OxmModelLoader.class.getClassLoader();
75     ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(oxmClassLoader);
76     Resource[] resources = null;
77     try {
78       resources = resolver.getResources("classpath*:/oxm/aai_oxm*.xml");
79     } catch (IOException ex) {
80       LOG.error(AaiUiMsgs.OXM_LOADING_ERROR, ex.getMessage());
81     }
82
83     if (resources == null) {
84       LOG.error(AaiUiMsgs.OXM_LOADING_ERROR, "No OXM schema files found on classpath");
85     }
86
87     for (Resource resource : resources) {
88       Matcher m = p.matcher(resource.getFilename());
89
90       if (m.matches()) {
91         try {
92           listOfOxmFiles.put(new Integer(m.group(2)), resource.getInputStream());
93         } catch (Exception e) {
94           LOG.error(AaiUiMsgs.OXM_LOADING_ERROR, resource.getFilename(), e.getMessage());
95         }
96       }
97     }
98     return listOfOxmFiles;
99   }
100
101   /**
102    * Load an oxm model.
103    * 
104    * @param inputStream file handle for oxm
105    */
106   protected void loadModel(InputStream inputStream) {
107     Map<String, Object> properties = new HashMap<String, Object>();
108     properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, inputStream);
109     try {
110       final DynamicJAXBContext oxmContext = DynamicJAXBContextFactory
111           .createContextFromOXM(Thread.currentThread().getContextClassLoader(), properties);
112
113       parseOxmContext(oxmContext);
114       // populateSearchableOxmModel();
115       LOG.info(AaiUiMsgs.OXM_LOAD_SUCCESS, String.valueOf(latestVersionNum));
116     } catch (Exception exc) {
117       LOG.info(AaiUiMsgs.OXM_PARSE_ERROR_NONVERBOSE);
118       LOG.error(AaiUiMsgs.OXM_PARSE_ERROR_VERBOSE, "OXM v" + latestVersionNum, exc.getMessage());
119     }
120   }
121
122   /**
123    * Load the latest oxm model.
124    */
125   public synchronized void loadLatestOxmModel() {
126
127     LOG.info(AaiUiMsgs.INITIALIZE_OXM_MODEL_LOADER);
128
129     // find handles for available oxm models
130     final Map<Integer, InputStream> listOfOxmStreams = getStreamHandlesForOxmFromResource();
131     if (listOfOxmStreams.isEmpty()) {
132       LOG.error(AaiUiMsgs.OXM_FILE_NOT_FOUND);
133       return;
134     }
135
136     InputStream stream = null;
137
138     if (oxmApiVersionOverride > 0) {
139       latestVersionNum = oxmApiVersionOverride;
140       LOG.warn(AaiUiMsgs.WARN_GENERIC, "Overriding AAI Schema with version = " + latestVersionNum);
141       stream = listOfOxmStreams.get(latestVersionNum);
142     } else {
143
144       for (Integer key : listOfOxmStreams.keySet()) {
145         if (key.intValue() > latestVersionNum) {
146           latestVersionNum = key.intValue();
147           stream = listOfOxmStreams.get(key);
148         }
149       }
150     }
151
152     // load the latest oxm file
153     loadModel(stream);
154
155   }
156
157   public int getLatestVersionNum() {
158     return latestVersionNum;
159   }
160
161   public void setLatestVersionNum(int latestVersionNum) {
162     this.latestVersionNum = latestVersionNum;
163   }
164
165   /**
166    * Parses the oxm context.
167    *
168    * @param oxmContext the oxm context
169    */
170   private void parseOxmContext(DynamicJAXBContext oxmContext) {
171
172     if (processors != null && processors.size() > 0) {
173
174       for (OxmModelProcessor processor : processors) {
175
176         processor.processOxmModel(oxmContext);
177
178       }
179
180     }
181
182   }
183
184 }