Fix Spike Event Processing with Common Format
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / util / 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.util;
22
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Collection;
31 import java.util.NoSuchElementException;
32 import java.util.concurrent.ConcurrentHashMap;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
35
36 import javax.ws.rs.core.Response.Status;
37 import javax.xml.bind.JAXBException;
38
39 import org.eclipse.persistence.jaxb.JAXBContextProperties;
40 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
41 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
42 import org.onap.aai.cl.eelf.LoggerFactory;
43 import org.onap.aai.datarouter.logging.DataRouterMsgs;
44 import org.springframework.core.io.Resource;
45 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
46 import org.springframework.core.io.support.ResourcePatternResolver;
47
48 public class OxmModelLoader {
49
50         private static Map<String, DynamicJAXBContext> versionContextMap = new ConcurrentHashMap<>();
51         private static List<ExternalOxmModelProcessor> oxmModelProcessorRegistry = new ArrayList<>();
52         static final Pattern p = Pattern.compile("aai_oxm_(.*).xml");
53
54         private static org.onap.aai.cl.api.Logger logger = LoggerFactory.getInstance()
55                         .getLogger(OxmModelLoader.class.getName());
56
57         public static synchronized void loadModels() throws FileNotFoundException {
58           
59     ClassLoader cl = OxmModelLoader.class.getClassLoader();
60     ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
61     Resource[] resources;
62     try {
63       resources = resolver.getResources("classpath*:/oxm/aai_oxm*.xml");
64     } catch (IOException ex) {
65       logger.error(DataRouterMsgs.LOAD_OXM_ERROR, ex.getMessage());  
66       throw new FileNotFoundException("Unable to load OXM models from schema path : /oxm/aai_oxm*.xml");
67     }
68
69     if (resources.length == 0) {
70       logger.error(DataRouterMsgs.LOAD_OXM_ERROR, "No OXM schema files found on classpath"); 
71       throw new FileNotFoundException("Unable to load OXM models from schema path : /oxm/aai_oxm*.xml");
72     }
73
74     for (Resource resource : resources) {
75       Matcher matcher = p.matcher(resource.getFilename());
76
77       if (matcher.matches()) {
78         try {
79           OxmModelLoader.loadModel(matcher.group(1), resource.getFilename(),resource.getInputStream());
80         } catch (Exception e) {
81           logger.error(DataRouterMsgs.LOAD_OXM_ERROR, "Failed to load " + resource.getFilename()
82               + ": " + e.getMessage());          
83         }
84       }
85     }
86                 
87                 
88         }
89         
90
91         private static synchronized void loadModel(String version,String resourceName,InputStream inputStream) throws JAXBException {
92                 Map<String, Object> properties = new HashMap<>();
93                 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, inputStream);
94                 final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory
95                                 .createContextFromOXM(Thread.currentThread().getContextClassLoader(), properties);
96                 versionContextMap.put(version, jaxbContext);
97                 if ( oxmModelProcessorRegistry != null) {
98           for ( ExternalOxmModelProcessor processor : oxmModelProcessorRegistry ) {
99              processor.onOxmVersionChange(Version.valueOf(version),  jaxbContext );
100           }
101          }              
102                 logger.info(DataRouterMsgs.LOADED_OXM_FILE, resourceName);
103         }
104
105         public static DynamicJAXBContext getContextForVersion(String version) throws NoSuchElementException, FileNotFoundException {
106                 if (versionContextMap == null || versionContextMap.isEmpty()) {
107                         loadModels();
108                 } else if (!versionContextMap.containsKey(version)) {                   
109                                 throw new NoSuchElementException(Status.NOT_FOUND.toString());
110                         
111                 }
112
113                 return versionContextMap.get(version);
114         }
115
116         public static Map<String, DynamicJAXBContext> getVersionContextMap() {
117                 return versionContextMap;
118         }
119
120         public static void setVersionContextMap(Map<String, DynamicJAXBContext> versionContextMap) {
121                 OxmModelLoader.versionContextMap = versionContextMap;
122         }
123         
124         public static synchronized void registerExternalOxmModelProcessors(Collection<ExternalOxmModelProcessor> processors) {
125       if(processors != null) {
126          for(ExternalOxmModelProcessor processor : processors) {
127             if(!oxmModelProcessorRegistry.contains(processor)) {
128                oxmModelProcessorRegistry.add(processor);
129             }
130          }
131       }
132    }
133
134 }