Update router core new schema ingest lib
[aai/router-core.git] / src / main / java / org / onap / aai / 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.aai.schema;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.NoSuchElementException;
28 import java.util.concurrent.ConcurrentHashMap;
29 import javax.ws.rs.core.Response.Status;
30 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
31 import org.onap.aai.cl.eelf.LoggerFactory;
32 import org.onap.aai.logging.RouterCoreMsgs;
33 import org.onap.aai.util.ExternalOxmModelProcessor;
34 import org.onap.aai.nodes.NodeIngestor;
35 import org.onap.aai.setup.SchemaLocationsBean;
36 import org.onap.aai.setup.SchemaVersion;
37 import org.onap.aai.setup.SchemaVersions;
38
39
40 public class OxmModelLoader {
41
42     private static Map<String, DynamicJAXBContext> versionContextMap = new ConcurrentHashMap<>();
43     private static List<ExternalOxmModelProcessor> oxmModelProcessorRegistry = new ArrayList<>();
44
45     private static final org.onap.aai.cl.api.Logger logger =
46             LoggerFactory.getInstance().getLogger(OxmModelLoader.class.getName());
47
48     private static NodeIngestor nodeIngestor;
49
50     private OxmModelLoader(NodeIngestor setNodeIngestor) {
51         nodeIngestor = setNodeIngestor;
52     }
53
54     public static synchronized void loadModels(SchemaVersions schemaVersions, SchemaLocationsBean schemaLocationsBean) {
55
56         for (SchemaVersion oxmVersion : schemaVersions.getVersions()) {
57           DynamicJAXBContext jaxbContext = nodeIngestor.getContextForVersion(oxmVersion);
58           if (jaxbContext != null) {
59             loadModel(oxmVersion.toString(), jaxbContext);
60           }
61         }
62     }
63
64     public static DynamicJAXBContext getContextForVersion(String version, SchemaVersions schemaVersions, SchemaLocationsBean schemaLocationsBean) {
65         if (versionContextMap == null || versionContextMap.isEmpty()) {
66             loadModels(schemaVersions, schemaLocationsBean);
67         } else if (!versionContextMap.containsKey(version)) {
68             throw new NoSuchElementException(Status.NOT_FOUND.toString());
69         }
70         return versionContextMap.get(version);
71     }
72
73     public static synchronized void registerExternalOxmModelProcessors(
74             Collection<ExternalOxmModelProcessor> processors) {
75         if (processors != null) {
76             for (ExternalOxmModelProcessor processor : processors) {
77                 if (!oxmModelProcessorRegistry.contains(processor)) {
78                     oxmModelProcessorRegistry.add(processor);
79                 }
80             }
81         }
82     }
83
84     public static Map<String, DynamicJAXBContext> getVersionContextMap() {
85         return versionContextMap;
86     }
87
88     public static NodeIngestor getNodeIngestor() {
89         return nodeIngestor;
90     }
91
92     public static void setNodeIngestor(NodeIngestor nodeIngestor) {
93         OxmModelLoader.nodeIngestor = nodeIngestor;
94     }
95
96     private static synchronized void loadModel(String oxmVersion, DynamicJAXBContext jaxbContext) {
97         versionContextMap.put(oxmVersion, jaxbContext);
98         if (oxmModelProcessorRegistry != null) {
99             for (ExternalOxmModelProcessor processor : oxmModelProcessorRegistry) {
100                 processor.onOxmVersionChange(org.onap.aai.util.Version.valueOf(oxmVersion.toLowerCase()),
101                         jaxbContext);
102             }
103         }
104         logger.info(RouterCoreMsgs.LOADED_OXM_FILE, oxmVersion);
105     }
106
107 }