Add multi-oxm using schemaIngest library
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / 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.datarouter.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.datarouter.logging.DataRouterMsgs;
33 import org.onap.aai.datarouter.util.ExternalOxmModelProcessor;
34 import org.onap.aai.nodes.NodeIngestor;
35 import org.onap.aai.setup.ConfigTranslator;
36 import org.onap.aai.setup.SchemaLocationsBean;
37 import org.onap.aai.setup.Version;
38
39 public class OxmModelLoader {
40
41     private static Map<String, DynamicJAXBContext> versionContextMap = new ConcurrentHashMap<>();
42     private static List<ExternalOxmModelProcessor> oxmModelProcessorRegistry = new ArrayList<>();
43
44     private static final org.onap.aai.cl.api.Logger logger =
45             LoggerFactory.getInstance().getLogger(OxmModelLoader.class.getName());
46
47     private OxmModelLoader() {
48         throw new IllegalStateException("Utility class");
49     }
50
51     public static synchronized void loadModels() {
52         SchemaIngestPropertiesReader schemaIngestPropReader = new SchemaIngestPropertiesReader();
53         SchemaLocationsBean schemaLocationsBean = new SchemaLocationsBean();
54         schemaLocationsBean.setNodeDirectory(schemaIngestPropReader.getNodeDir());
55         ConfigTranslator configTranslator = new OxmConfigTranslator(schemaLocationsBean);
56         NodeIngestor nodeIngestor = new NodeIngestor(configTranslator);
57
58         for (Version oxmVersion : Version.values()) {
59             DynamicJAXBContext jaxbContext = nodeIngestor.getContextForVersion(oxmVersion);
60             if (jaxbContext != null) {
61                 loadModel(oxmVersion.toString(), jaxbContext);
62             }
63         }
64     }
65
66     public static DynamicJAXBContext getContextForVersion(String version) {
67         if (versionContextMap == null || versionContextMap.isEmpty()) {
68             loadModels();
69         } else if (!versionContextMap.containsKey(version)) {
70             throw new NoSuchElementException(Status.NOT_FOUND.toString());
71         }
72         return versionContextMap.get(version);
73     }
74
75     public static synchronized void registerExternalOxmModelProcessors(
76             Collection<ExternalOxmModelProcessor> processors) {
77         if (processors != null) {
78             for (ExternalOxmModelProcessor processor : processors) {
79                 if (!oxmModelProcessorRegistry.contains(processor)) {
80                     oxmModelProcessorRegistry.add(processor);
81                 }
82             }
83         }
84     }
85
86     public static Map<String, DynamicJAXBContext> getVersionContextMap() {
87         return versionContextMap;
88     }
89
90     private static synchronized void loadModel(String oxmVersion, DynamicJAXBContext jaxbContext) {
91         versionContextMap.put(oxmVersion, jaxbContext);
92         if (oxmModelProcessorRegistry != null) {
93             for (ExternalOxmModelProcessor processor : oxmModelProcessorRegistry) {
94                 processor.onOxmVersionChange(org.onap.aai.datarouter.util.Version.valueOf(oxmVersion.toLowerCase()),
95                         jaxbContext);
96             }
97         }
98         logger.info(DataRouterMsgs.LOADED_OXM_FILE, oxmVersion);
99     }
100
101 }