[AAI-26] Adding gizmo data to the repository.
[aai/gizmo.git] / src / main / java / org / openecomp / schema / RelationshipSchemaLoader.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Gizmo
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 is a trademark and service mark of AT&T Intellectual Property.
23  */
24 package org.openecomp.schema;
25
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;
32
33 import java.io.File;
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;
40 import java.util.Map;
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;
50
51
52 public class RelationshipSchemaLoader {
53
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>();
58
59   final static Pattern filePattern = Pattern.compile("aai_relationship_(.*).json");
60
61
62   private static org.openecomp.cl.api.Logger logger = LoggerFactory.getInstance()
63       .getLogger(RelationshipSchemaLoader.class.getName());
64
65   public synchronized static void loadModels() {
66
67     File[] listOfFiles = new File(CrudServiceConstants.CRD_HOME_MODEL).listFiles();
68
69     if (listOfFiles != null) {
70       for (File file : listOfFiles) {
71         if (file.isFile()) {
72           Matcher matcher = filePattern.matcher(file.getName());
73           if (matcher.matches()) {
74             try {
75               RelationshipSchemaLoader.loadModel(matcher.group(1), file);
76             } catch (Exception e) {
77               logger.error(CrudServiceMsgs.INVALID_OXM_FILE, file.getName(), e.getMessage());
78             }
79           }
80
81         }
82       }
83     } else {
84       logger.error(CrudServiceMsgs.INVALID_OXM_DIR, CrudServiceConstants.CRD_HOME_MODEL);
85     }
86
87
88   }
89
90   private static void addtimer(String version, File file) {
91     TimerTask task = null;
92     task = new FileWatcher(
93         file) {
94       protected void onChange(File file) {
95         // here we implement the onChange
96         logger.info(CrudServiceMsgs.OXM_FILE_CHANGED, file.getName());
97
98         try {
99           RelationshipSchemaLoader.loadModel(version, file);
100         } catch (Exception e) {
101           e.printStackTrace();
102         }
103
104       }
105     };
106
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);
111
112     }
113   }
114
115   private synchronized static void loadModel(String version, File file)
116       throws JAXBException, IOException, CrudException {
117
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)));
123   }
124
125   public static RelationshipSchema getSchemaForVersion(String version) throws CrudException {
126     if (versionContextMap == null || versionContextMap.isEmpty()) {
127       loadModels();
128     } else if (!versionContextMap.containsKey(version)) {
129       try {
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);
134       }
135     }
136
137     return versionContextMap.get(version);
138   }
139
140   public static String getLatestSchemaVersion() throws CrudException {
141     return "v" + versions.last();
142   }
143
144   public static Map<String, RelationshipSchema> getVersionContextMap() {
145     return versionContextMap;
146   }
147
148   public static void setVersionContextMap(HashMap<String, RelationshipSchema> versionContextMap) {
149     RelationshipSchemaLoader.versionContextMap = versionContextMap;
150   }
151
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");
154
155     loadModel("v8", initialFile);
156   }
157
158 }