2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.schema;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
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.ConfigTranslator;
36 import org.onap.aai.setup.SchemaLocationsBean;
37 import org.onap.aai.setup.SchemaVersion;
38 import org.onap.aai.setup.SchemaVersions;
39 import org.onap.aai.setup.AAIConfigTranslator;
41 public class OxmModelLoader {
43 private static Map<String, DynamicJAXBContext> versionContextMap = new ConcurrentHashMap<>();
44 private static List<ExternalOxmModelProcessor> oxmModelProcessorRegistry = new ArrayList<>();
46 private static final org.onap.aai.cl.api.Logger logger =
47 LoggerFactory.getInstance().getLogger(OxmModelLoader.class.getName());
49 private OxmModelLoader() {
50 throw new IllegalStateException("Utility class");
53 public static synchronized void loadModels(SchemaVersions schemaVersions, SchemaLocationsBean schemaLocationsBean) {
54 ConfigTranslator configTranslator = new AAIConfigTranslator(schemaLocationsBean, schemaVersions);
55 NodeIngestor nodeIngestor = new NodeIngestor(configTranslator);
57 for (SchemaVersion oxmVersion : schemaVersions.getVersions()) {
58 DynamicJAXBContext jaxbContext = nodeIngestor.getContextForVersion(oxmVersion);
59 if (jaxbContext != null) {
60 loadModel(oxmVersion.toString(), jaxbContext);
65 public static DynamicJAXBContext getContextForVersion(String version, SchemaVersions schemaVersions, SchemaLocationsBean schemaLocationsBean) {
66 if (versionContextMap == null || versionContextMap.isEmpty()) {
67 loadModels(schemaVersions, schemaLocationsBean);
68 } else if (!versionContextMap.containsKey(version)) {
69 throw new NoSuchElementException(Status.NOT_FOUND.toString());
71 return versionContextMap.get(version);
74 public static synchronized void registerExternalOxmModelProcessors(
75 Collection<ExternalOxmModelProcessor> processors) {
76 if (processors != null) {
77 for (ExternalOxmModelProcessor processor : processors) {
78 if (!oxmModelProcessorRegistry.contains(processor)) {
79 oxmModelProcessorRegistry.add(processor);
85 public static Map<String, DynamicJAXBContext> getVersionContextMap() {
86 return versionContextMap;
89 private static synchronized void loadModel(String oxmVersion, DynamicJAXBContext jaxbContext) {
90 versionContextMap.put(oxmVersion, jaxbContext);
91 if (oxmModelProcessorRegistry != null) {
92 for (ExternalOxmModelProcessor processor : oxmModelProcessorRegistry) {
93 processor.onOxmVersionChange(org.onap.aai.util.Version.valueOf(oxmVersion.toLowerCase()),
97 logger.info(RouterCoreMsgs.LOADED_OXM_FILE, oxmVersion);