2919ba23e2db5c88da73249d7a62feea33edeb09
[aai/data-router.git] / src / main / java / org / openecomp / datarouter / util / OxmModelLoader.java
1 /**
2  * ============LICENSE_START=======================================================
3  * DataRouter
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.openecomp.datarouter.util;
26
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.FileNotFoundException;
30 import java.io.InputStream;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.Date;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Timer;
38 import java.util.TimerTask;
39 import java.util.concurrent.ConcurrentHashMap;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
42
43 import javax.ws.rs.core.Response.Status;
44 import javax.xml.bind.JAXBException;
45
46 import org.eclipse.persistence.jaxb.JAXBContextProperties;
47 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
48 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
49 import org.openecomp.cl.eelf.LoggerFactory;
50 import org.openecomp.datarouter.logging.DataRouterMsgs;
51
52 import org.openecomp.datarouter.util.ExternalOxmModelProcessor;
53
54 public class OxmModelLoader {
55
56         private static Map<String, DynamicJAXBContext> versionContextMap = new ConcurrentHashMap<String, DynamicJAXBContext>();
57         private static Map<String, Timer> timers = new ConcurrentHashMap<String, Timer>();
58         private static List<ExternalOxmModelProcessor> oxmModelProcessorRegistry = new ArrayList<ExternalOxmModelProcessor>();
59         final static Pattern p = Pattern.compile("aai_oxm_(.*).xml");
60         
61         
62
63         private static org.openecomp.cl.api.Logger logger = LoggerFactory.getInstance()
64                         .getLogger(OxmModelLoader.class.getName());
65
66         public synchronized static void loadModels() {
67                 
68                 File[] listOfFiles = new File(DataRouterConstants.DR_HOME_MODEL).listFiles();
69
70                 if (listOfFiles != null) {
71                         for (File file : listOfFiles) {
72                                 if (file.isFile()) {
73                                         Matcher m = p.matcher(file.getName());
74                                         if (m.matches()) {
75                                                 try {
76                                                         OxmModelLoader.loadModel(m.group(1), file);
77                                                 } catch (Exception e) {
78                                                         logger.error(DataRouterMsgs.INVALID_OXM_FILE, file.getName(), e.getMessage());
79                                                 }
80                                         }
81
82                                 }
83                         }
84                 } else {
85                         logger.error(DataRouterMsgs.INVALID_OXM_DIR, DataRouterConstants.DR_HOME_MODEL);
86                 }
87
88
89         }
90         
91         private static void addtimer(String version,File file){
92                 TimerTask task = null;
93                 task = new FileWatcher(
94                                 file) {
95                         protected void onChange(File file) {
96                                 // here we implement the onChange
97                                 logger.info(DataRouterMsgs.FILE_CHANGED, file.getName());
98
99                                 try {
100                                         OxmModelLoader.loadModel(version,file);
101                                 } catch (Exception e) {
102                                         e.printStackTrace();
103                                 }
104
105                         }
106                 };
107
108                 if (!timers.containsKey(version)) {
109                         Timer timer = new Timer("oxm-"+version);
110                         timer.schedule(task, new Date(), 10000);
111                         timers.put(version, timer);
112
113                 }
114         }
115
116         private synchronized static void loadModel(String version,File file) throws JAXBException, FileNotFoundException {
117
118                 
119                 InputStream iStream = new FileInputStream(file);
120                 Map<String, Object> properties = new HashMap<String, Object>();
121                 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream);
122                 final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory
123                                 .createContextFromOXM(Thread.currentThread().getContextClassLoader(), properties);
124                 versionContextMap.put(version, jaxbContext);
125                 if ( oxmModelProcessorRegistry != null) {
126           for ( ExternalOxmModelProcessor processor : oxmModelProcessorRegistry ) {
127              processor.onOxmVersionChange(Version.valueOf(version),  jaxbContext );
128           }
129          }
130                 addtimer(version,file);
131
132         }
133
134         public static DynamicJAXBContext getContextForVersion(String version) throws Exception {
135                 if (versionContextMap == null || versionContextMap.isEmpty()) {
136                         loadModels();
137                 } else if (!versionContextMap.containsKey(version)) {
138                         try {
139                                 loadModel(version,new File (DataRouterConstants.DR_HOME_MODEL + "aai_oxm_" + version + ".xml"));
140                         } catch (Exception e) {
141                                 throw new Exception(Status.NOT_FOUND.toString());
142                         }
143                 }
144
145                 return versionContextMap.get(version);
146         }
147
148         public static Map<String, DynamicJAXBContext> getVersionContextMap() {
149                 return versionContextMap;
150         }
151
152         public static void setVersionContextMap(Map<String, DynamicJAXBContext> versionContextMap) {
153                 OxmModelLoader.versionContextMap = versionContextMap;
154         }
155         
156         public synchronized static void registerExternalOxmModelProcessors(Collection<ExternalOxmModelProcessor> processors) {
157       if(processors != null) {
158          for(ExternalOxmModelProcessor processor : processors) {
159             if(!oxmModelProcessorRegistry.contains(processor)) {
160                oxmModelProcessorRegistry.add(processor);
161             }
162          }
163       }
164    }
165
166 }