2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.openecomp.schema;
26 import org.apache.commons.io.IOUtils;
27 import org.openecomp.cl.eelf.LoggerFactory;
28 import org.openecomp.crud.exception.CrudException;
29 import org.openecomp.crud.logging.CrudServiceMsgs;
30 import org.openecomp.crud.util.CrudServiceConstants;
31 import org.openecomp.crud.util.FileWatcher;
34 import java.io.FileInputStream;
35 import java.io.FileNotFoundException;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.util.Date;
39 import java.util.HashMap;
41 import java.util.SortedSet;
42 import java.util.Timer;
43 import java.util.TimerTask;
44 import java.util.TreeSet;
45 import java.util.concurrent.ConcurrentHashMap;
46 import java.util.regex.Matcher;
47 import java.util.regex.Pattern;
48 import javax.ws.rs.core.Response.Status;
49 import javax.xml.bind.JAXBException;
52 public class RelationshipSchemaLoader {
54 private static Map<String, RelationshipSchema> versionContextMap
55 = new ConcurrentHashMap<String, RelationshipSchema>();
56 private static SortedSet<Integer> versions = new TreeSet<Integer>();
57 private static Map<String, Timer> timers = new ConcurrentHashMap<String, Timer>();
59 final static Pattern filePattern = Pattern.compile("aai_relationship_(.*).json");
62 private static org.openecomp.cl.api.Logger logger = LoggerFactory.getInstance()
63 .getLogger(RelationshipSchemaLoader.class.getName());
65 public synchronized static void loadModels() {
67 File[] listOfFiles = new File(CrudServiceConstants.CRD_HOME_MODEL).listFiles();
69 if (listOfFiles != null) {
70 for (File file : listOfFiles) {
72 Matcher matcher = filePattern.matcher(file.getName());
73 if (matcher.matches()) {
75 RelationshipSchemaLoader.loadModel(matcher.group(1), file);
76 } catch (Exception e) {
77 logger.error(CrudServiceMsgs.INVALID_OXM_FILE, file.getName(), e.getMessage());
84 logger.error(CrudServiceMsgs.INVALID_OXM_DIR, CrudServiceConstants.CRD_HOME_MODEL);
90 private static void addtimer(String version, File file) {
91 TimerTask task = null;
92 task = new FileWatcher(
94 protected void onChange(File file) {
95 // here we implement the onChange
96 logger.info(CrudServiceMsgs.OXM_FILE_CHANGED, file.getName());
99 RelationshipSchemaLoader.loadModel(version, file);
100 } catch (Exception e) {
107 if (!timers.containsKey(version)) {
108 Timer timer = new Timer("aai_relationship_" + version);
109 timer.schedule(task, new Date(), 10000);
110 timers.put(version, timer);
115 private synchronized static void loadModel(String version, File file)
116 throws JAXBException, IOException, CrudException {
118 InputStream inputStream = new FileInputStream(file);
119 String content = IOUtils.toString(inputStream, "UTF-8");
120 versionContextMap.put(version, new RelationshipSchema(content));
121 addtimer(version, file);
122 versions.add(Integer.parseInt(version.substring(1)));
125 public static RelationshipSchema getSchemaForVersion(String version) throws CrudException {
126 if (versionContextMap == null || versionContextMap.isEmpty()) {
128 } else if (!versionContextMap.containsKey(version)) {
130 loadModel(version, new File(CrudServiceConstants.CRD_HOME_MODEL + "aai_relationship_"
131 + version + ".json"));
132 } catch (Exception e) {
133 throw new CrudException("", Status.NOT_FOUND);
137 return versionContextMap.get(version);
140 public static String getLatestSchemaVersion() throws CrudException {
141 return "v" + versions.last();
144 public static Map<String, RelationshipSchema> getVersionContextMap() {
145 return versionContextMap;
148 public static void setVersionContextMap(HashMap<String, RelationshipSchema> versionContextMap) {
149 RelationshipSchemaLoader.versionContextMap = versionContextMap;
152 public static void main(String[] args) throws FileNotFoundException, Exception {
153 File initialFile = new File("C:\\Software\\gizmo\\src\\main\\java\\org\\openecomp\\schema\\vio.json");
155 loadModel("v8", initialFile);