Merge "Fix sync update flow"
[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.SchemaVersion;
30 import org.onap.aai.setup.SchemaVersions;
31 import org.onap.aai.sparky.logging.AaiUiMsgs;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Component;
34
35 @Component("oxmModelLoader")
36 public class OxmModelLoader {
37
38   private static final Logger LOG = LoggerFactory.getInstance().getLogger(OxmModelLoader.class);
39
40   /*
41    * The intent of this parameter is to be able to programmatically over-ride the latest AAI schema
42    * version discovered from the aai-schema jar file. This property is optional, but if set on the
43    * bean or by another class in the system, then it will override the spec version that is loaded.
44    * 
45    * If the latestVersionOverride is greater than 0 then it will set the latest version to the
46    * specified version, and that stream will be returned if available.
47    */
48
49   protected SchemaVersion oxmApiVersion;
50   protected Set<OxmModelProcessor> processors;
51
52   private NodeIngestor nodeIngestor;
53
54   public OxmModelLoader(String apiVersionOverride, Set<OxmModelProcessor> oxmModelProcessors) {
55     this.oxmApiVersion = new SchemaVersion(apiVersionOverride);
56     this.processors = oxmModelProcessors;
57   }
58
59   public OxmModelLoader(Set<OxmModelProcessor> oxmModelProcessors, SchemaVersions schemaVersions) {
60     this.oxmApiVersion = schemaVersions.getDefaultVersion();
61     this.processors = oxmModelProcessors;
62   }
63
64   public SchemaVersion getOxmApiVersion() {
65     return oxmApiVersion;
66   }
67
68
69
70   public NodeIngestor getNodeIngestor() {
71     return nodeIngestor;
72   }
73
74   @Autowired
75   public void setNodeIngestor(NodeIngestor nodeIngestor) {
76     this.nodeIngestor = nodeIngestor;
77   }
78
79   /**
80    * Load an oxm model.
81    * 
82    * @param inputStream file handle for oxm
83    */
84   public void loadModel() {
85     try {
86       final DynamicJAXBContext oxmContext = nodeIngestor.getContextForVersion(oxmApiVersion);
87       parseOxmContext(oxmContext);
88       // populateSearchableOxmModel();
89       LOG.info(AaiUiMsgs.OXM_LOAD_SUCCESS, String.valueOf(oxmApiVersion));
90     } catch (Exception exc) {
91       LOG.info(AaiUiMsgs.OXM_PARSE_ERROR_NONVERBOSE);
92       LOG.error(AaiUiMsgs.OXM_PARSE_ERROR_VERBOSE, "OXM v" + oxmApiVersion, exc.getMessage());
93     }
94   }
95
96   /**
97    * Parses the oxm context.
98    *
99    * @param oxmContext the oxm context
100    */
101   private void parseOxmContext(DynamicJAXBContext oxmContext) {
102
103     if (processors != null && processors.size() > 0) {
104
105       for (OxmModelProcessor processor : processors) {
106         
107         try {
108
109           processor.processOxmModel(oxmContext);
110
111         } catch (Exception exc) {
112
113           LOG.warn(AaiUiMsgs.WARN_GENERIC,
114               "OxmModelProcessor experienced an error. Error: " + exc.getMessage());
115
116         }
117
118       }
119
120     }
121
122   }
123
124 }