Add multi-oxm support to sparky-be
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / config / oxm / 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.aai.sparky.config.oxm;
22
23 import java.util.Set;
24
25 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
26 import org.onap.aai.cl.api.Logger;
27 import org.onap.aai.cl.eelf.LoggerFactory;
28 import org.onap.aai.nodes.NodeIngestor;
29 import org.onap.aai.setup.Version;
30 import org.onap.aai.sparky.logging.AaiUiMsgs;
31
32 public class OxmModelLoader {
33
34   private static final Logger LOG = LoggerFactory.getInstance().getLogger(OxmModelLoader.class);
35
36   /*
37    * The intent of this parameter is to be able to programmatically over-ride the latest AAI schema
38    * version discovered from the aai-schema jar file. This property is optional, but if set on the
39    * bean or by another class in the system, then it will override the spec version that is loaded.
40    * 
41    * If the latestVersionOverride is greater than 0 then it will set the latest version to the
42    * specified version, and that stream will be returned if available.
43    */
44
45   protected Version oxmApiVersion;
46   protected Set<OxmModelProcessor> processors;
47
48   private NodeIngestor nodeIngestor;
49
50   public OxmModelLoader(Version apiVersionOverride, Set<OxmModelProcessor> oxmModelProcessors,
51       NodeIngestor nodeIngestor) {
52     this.oxmApiVersion = apiVersionOverride;
53     this.processors = oxmModelProcessors;
54     this.nodeIngestor = nodeIngestor;
55   }
56
57   public OxmModelLoader(Set<OxmModelProcessor> oxmModelProcessors, NodeIngestor nodeIngestor) {
58     this.oxmApiVersion = Version.getLatest();
59     this.processors = oxmModelProcessors;
60     this.nodeIngestor = nodeIngestor;
61   }
62
63   public Version getLatestVersionNum() {
64     return oxmApiVersion;
65   }
66
67
68
69   /**
70    * Load an oxm model.
71    * 
72    * @param inputStream file handle for oxm
73    */
74   public void loadModel() {
75     try {
76       final DynamicJAXBContext oxmContext = nodeIngestor.getContextForVersion(oxmApiVersion);
77       parseOxmContext(oxmContext);
78       // populateSearchableOxmModel();
79       LOG.info(AaiUiMsgs.OXM_LOAD_SUCCESS, String.valueOf(oxmApiVersion));
80     } catch (Exception exc) {
81       LOG.info(AaiUiMsgs.OXM_PARSE_ERROR_NONVERBOSE);
82       LOG.error(AaiUiMsgs.OXM_PARSE_ERROR_VERBOSE, "OXM v" + oxmApiVersion, exc.getMessage());
83     }
84   }
85
86   /**
87    * Parses the oxm context.
88    *
89    * @param oxmContext the oxm context
90    */
91   private void parseOxmContext(DynamicJAXBContext oxmContext) {
92
93     if (processors != null && processors.size() > 0) {
94
95       for (OxmModelProcessor processor : processors) {
96
97         try {
98
99           processor.processOxmModel(oxmContext);
100
101         } catch (Exception exc) {
102
103           LOG.warn(AaiUiMsgs.WARN_GENERIC,
104               "OxmModelProcessor experienced an error. Error: " + exc.getMessage());
105
106         }
107
108       }
109
110     }
111
112   }
113
114 }