[AAI-84 Amsterdam] Update License and trademark
[aai/data-router.git] / src / main / java / org / openecomp / datarouter / util / OxmModelLoader.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.openecomp.datarouter.util;
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.InputStream;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Date;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Timer;
36 import java.util.TimerTask;
37 import java.util.concurrent.ConcurrentHashMap;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
40
41 import javax.ws.rs.core.Response.Status;
42 import javax.xml.bind.JAXBException;
43
44 import org.eclipse.persistence.jaxb.JAXBContextProperties;
45 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
46 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
47 import org.openecomp.cl.eelf.LoggerFactory;
48 import org.openecomp.datarouter.logging.DataRouterMsgs;
49
50 import org.openecomp.datarouter.util.ExternalOxmModelProcessor;
51
52 public class OxmModelLoader {
53
54         private static Map<String, DynamicJAXBContext> versionContextMap = new ConcurrentHashMap<String, DynamicJAXBContext>();
55         private static Map<String, Timer> timers = new ConcurrentHashMap<String, Timer>();
56         private static List<ExternalOxmModelProcessor> oxmModelProcessorRegistry = new ArrayList<ExternalOxmModelProcessor>();
57         final static Pattern p = Pattern.compile("aai_oxm_(.*).xml");
58         
59         
60
61         private static org.openecomp.cl.api.Logger logger = LoggerFactory.getInstance()
62                         .getLogger(OxmModelLoader.class.getName());
63
64         public synchronized static void loadModels() {
65                 
66                 File[] listOfFiles = new File(DataRouterConstants.DR_HOME_MODEL).listFiles();
67
68                 if (listOfFiles != null) {
69                         for (File file : listOfFiles) {
70                                 if (file.isFile()) {
71                                         Matcher m = p.matcher(file.getName());
72                                         if (m.matches()) {
73                                                 try {
74                                                         OxmModelLoader.loadModel(m.group(1), file);
75                                                 } catch (Exception e) {
76                                                         logger.error(DataRouterMsgs.INVALID_OXM_FILE, file.getName(), e.getMessage());
77                                                 }
78                                         }
79
80                                 }
81                         }
82                 } else {
83                         logger.error(DataRouterMsgs.INVALID_OXM_DIR, DataRouterConstants.DR_HOME_MODEL);
84                 }
85
86
87         }
88         
89         private static void addtimer(String version,File file){
90                 TimerTask task = null;
91                 task = new FileWatcher(
92                                 file) {
93                         protected void onChange(File file) {
94                                 // here we implement the onChange
95                                 logger.info(DataRouterMsgs.FILE_CHANGED, file.getName());
96
97                                 try {
98                                         OxmModelLoader.loadModel(version,file);
99                                 } catch (Exception e) {
100                                         e.printStackTrace();
101                                 }
102
103                         }
104                 };
105
106                 if (!timers.containsKey(version)) {
107                         Timer timer = new Timer("oxm-"+version);
108                         timer.schedule(task, new Date(), 10000);
109                         timers.put(version, timer);
110
111                 }
112         }
113
114         private synchronized static void loadModel(String version,File file) throws JAXBException, FileNotFoundException {
115
116                 
117                 InputStream iStream = new FileInputStream(file);
118                 Map<String, Object> properties = new HashMap<String, Object>();
119                 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream);
120                 final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory
121                                 .createContextFromOXM(Thread.currentThread().getContextClassLoader(), properties);
122                 versionContextMap.put(version, jaxbContext);
123                 if ( oxmModelProcessorRegistry != null) {
124           for ( ExternalOxmModelProcessor processor : oxmModelProcessorRegistry ) {
125              processor.onOxmVersionChange(Version.valueOf(version),  jaxbContext );
126           }
127          }
128                 addtimer(version,file);
129
130         }
131
132         public static DynamicJAXBContext getContextForVersion(String version) throws Exception {
133                 if (versionContextMap == null || versionContextMap.isEmpty()) {
134                         loadModels();
135                 } else if (!versionContextMap.containsKey(version)) {
136                         try {
137                                 loadModel(version,new File (DataRouterConstants.DR_HOME_MODEL + "aai_oxm_" + version + ".xml"));
138                         } catch (Exception e) {
139                                 throw new Exception(Status.NOT_FOUND.toString());
140                         }
141                 }
142
143                 return versionContextMap.get(version);
144         }
145
146         public static Map<String, DynamicJAXBContext> getVersionContextMap() {
147                 return versionContextMap;
148         }
149
150         public static void setVersionContextMap(Map<String, DynamicJAXBContext> versionContextMap) {
151                 OxmModelLoader.versionContextMap = versionContextMap;
152         }
153         
154         public synchronized static void registerExternalOxmModelProcessors(Collection<ExternalOxmModelProcessor> processors) {
155       if(processors != null) {
156          for(ExternalOxmModelProcessor processor : processors) {
157             if(!oxmModelProcessorRegistry.contains(processor)) {
158                oxmModelProcessorRegistry.add(processor);
159             }
160          }
161       }
162    }
163
164 }