Update license date and text
[aai/gizmo.git] / src / main / java / org / onap / schema / RelationshipSchemaLoader.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.schema;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Comparator;
31 import java.util.Date;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.SortedSet;
35 import java.util.Timer;
36 import java.util.TimerTask;
37 import java.util.TreeSet;
38 import java.util.concurrent.ConcurrentHashMap;
39 import java.util.regex.Matcher;
40 import java.util.regex.Pattern;
41 import java.util.stream.Collectors;
42
43 import javax.ws.rs.core.Response.Status;
44
45 import org.apache.commons.io.IOUtils;
46 import org.onap.aai.cl.eelf.LoggerFactory;
47 import org.onap.crud.exception.CrudException;
48 import org.onap.crud.logging.CrudServiceMsgs;
49 import org.onap.crud.util.CrudServiceConstants;
50 import org.onap.crud.util.FileWatcher;
51 import org.springframework.core.io.UrlResource;
52 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
53 import org.springframework.core.io.support.ResourcePatternResolver;
54
55 public class RelationshipSchemaLoader {
56
57   private static Map<String, RelationshipSchema> versionContextMap = new ConcurrentHashMap<>();
58   private static SortedSet<Integer> versions = new TreeSet<Integer>();
59   private static Map<String, Timer> timers = new ConcurrentHashMap<String, Timer>();
60   final static String edgePropsFiles = "edge_properties_";
61   final static String fileExt = ".json";
62   final static Pattern rulesFilePattern = Pattern.compile("DbEdgeRules(.*)" + fileExt);
63   final static Pattern propsFilePattern = Pattern.compile(edgePropsFiles + "(.*)" + fileExt);
64   final static Pattern versionPattern = Pattern.compile(".*(v\\d+)" + fileExt);
65
66   private static org.onap.aai.cl.api.Logger logger = LoggerFactory.getInstance()
67       .getLogger(RelationshipSchemaLoader.class.getName());
68
69   public synchronized static void loadModels() throws CrudException {
70     load(rulesFilePattern, propsFilePattern);
71   }
72
73   public synchronized static void loadModels(String version) throws CrudException {
74     String pattern = String.format(".*(%s)" + fileExt, version);
75     load(Pattern.compile(pattern), Pattern.compile(edgePropsFiles + version + fileExt));
76   }
77
78   public static RelationshipSchema getSchemaForVersion(String version) throws CrudException {
79     if (versionContextMap == null || versionContextMap.isEmpty()) {
80       loadModels();
81     } else if (!versionContextMap.containsKey(version)) {
82       try {
83         loadModels(version);
84       } catch (Exception e) {
85         throw new CrudException("", Status.NOT_FOUND);
86       }
87     }
88     RelationshipSchema schema = versionContextMap.get(version);
89     if (schema == null) {
90       throw new CrudException("", Status.NOT_FOUND);
91     } else
92       return schema;
93   }
94
95   public static String getLatestSchemaVersion() throws CrudException {
96     return "v" + versions.last();
97   }
98
99   public static Map<String, RelationshipSchema> getVersionContextMap() {
100     return versionContextMap;
101   }
102
103   public static void setVersionContextMap(Map<String, RelationshipSchema> versionContextMap) {
104     RelationshipSchemaLoader.versionContextMap = versionContextMap;
105   }
106
107   public static void resetVersionContextMap() {
108     RelationshipSchemaLoader.versionContextMap = new ConcurrentHashMap<>();
109   }
110
111   private static void load(Pattern rulesPattern, Pattern edgePropsPattern) throws CrudException {
112     ClassLoader cl = RelationshipSchemaLoader.class.getClassLoader();
113     ResourcePatternResolver rulesResolver = new PathMatchingResourcePatternResolver(cl);
114     List<Object> rulesFiles;
115     String rulesDir = CrudServiceConstants.CRD_HOME_MODEL;
116     try {
117
118       // getResources method returns objects of type "Resource"
119       // 1. We are getting all the objects from the classpath which has
120       // "DbEdgeRules" in the name.
121       // 2. We run them through a filter and return only the objects which match
122       // the supplied pattern "p"
123       // 3. We then collect the objects in a list. At this point we have a list
124       // of the kind of files we require.
125       rulesFiles = Arrays.stream(rulesResolver.getResources("classpath*:/dbedgerules/DbEdgeRules*" + fileExt))
126           .filter(r -> !myMatcher(rulesPattern, r.getFilename()).isEmpty()).collect(Collectors.toList());
127
128       // This gets all the objects of type "File" from external directory (not
129       // on the classpath)
130       // 1. From an external directory (one not on the classpath) we get all the
131       // objects of type "File"
132       // 2. We only return the files whose names matched the supplied pattern
133       // "p2".
134       // 3. We then collect all the objects in a list and add the contents of
135       // this list
136       // to the previous collection (rulesFiles)
137       rulesFiles
138           .addAll(Arrays.stream(new File(rulesDir).listFiles((d, name) -> edgePropsPattern.matcher(name).matches()))
139               .collect(Collectors.toList()));
140
141       if (rulesFiles.isEmpty()) {
142         logger.error(CrudServiceMsgs.INVALID_OXM_DIR, rulesDir);
143         throw new FileNotFoundException("DbEdgeRules and edge_properties files were not found.");
144       }
145
146       // Sort and then group the files with their versions, convert them to the
147       // schema, and add them to versionContextMap
148       // 1. Sort the files. We need the DbEdgeRules files to be before the
149       // edgeProperties files.
150       // 2. Group the files with their versions. ie. v11 ->
151       // ["DbEdgeRule_v11.json", "edgeProperties_v11.json"].
152       // The "group method" returns a HashMap whose key is the version and the
153       // value is a list of objects.
154       // 3. Go through each version and map the files into one schema using the
155       // "jsonFilesLoader" method.
156       // Also update the "versionContextMap" with the version and it's schema.
157       rulesFiles.stream().sorted(Comparator.comparing(RelationshipSchemaLoader::filename))
158           .collect(Collectors.groupingBy(f -> myMatcher(versionPattern, filename(f))))
159           .forEach((version, resourceAndFile) -> {
160             if (resourceAndFile.size() == 2) {
161               versionContextMap.put(version, jsonFilesLoader(version, resourceAndFile));
162             } else {
163               String filenames = resourceAndFile.stream().map(f -> filename(f)).collect(Collectors.toList()).toString();
164               String errorMsg = "Expecting a rules and a edge_properties files for " + version + ". Found: "
165                   + filenames;
166               logger.warn(CrudServiceMsgs.INVALID_OXM_FILE, errorMsg);
167             }
168           });
169       logger.info(CrudServiceMsgs.LOADED_OXM_FILE, "Relationship Schema and Properties files: "
170           + rulesFiles.stream().map(f -> filename(f)).collect(Collectors.toList()));
171     } catch (IOException e) {
172       logger.error(CrudServiceMsgs.INVALID_OXM_DIR, rulesDir);
173       throw new CrudException("DbEdgeRules or edge_properties files were not found.", new FileNotFoundException());
174     }
175   }
176
177   private static String filename(Object k) throws ClassCastException {
178     if (k instanceof UrlResource) {
179       return ((UrlResource) k).getFilename();
180     } else if (k instanceof File) {
181       return ((File) k).getName();
182     } else {
183       throw new ClassCastException();
184     }
185   }
186
187   private static RelationshipSchema jsonFilesLoader(String version, List<Object> files) {
188     List<String> fileContents = new ArrayList<>();
189     RelationshipSchema rsSchema = null;
190     if (files.size() == 2) {
191       for (Object file : files) {
192         fileContents.add(jsonToRelationSchema(version, file));
193         versions.add(Integer.parseInt(version.substring(1)));
194       }
195
196       try {
197         rsSchema = new RelationshipSchema(fileContents);
198       } catch (CrudException | IOException e) {
199         e.printStackTrace();
200         logger.error(CrudServiceMsgs.INVALID_OXM_FILE,
201             files.stream().map(f -> filename(f)).collect(Collectors.toList()).toString(), e.getMessage());
202       }
203       return rsSchema;
204     }
205     return rsSchema;
206   }
207
208   private synchronized static void updateVersionContext(String version, RelationshipSchema rs) {
209     versionContextMap.put(version, rs);
210   }
211
212   private synchronized static String jsonToRelationSchema(String version, Object file) {
213     InputStream inputStream = null;
214     String content = null;
215
216     try {
217       if (file instanceof UrlResource) {
218         inputStream = ((UrlResource) file).getInputStream();
219       } else {
220         inputStream = new FileInputStream((File) file);
221         addtimer(version, file);
222       }
223       content = IOUtils.toString(inputStream, "UTF-8");
224     } catch (IOException e) {
225       e.printStackTrace();
226     }
227     return content;
228   }
229
230   private static void addtimer(String version, Object file) {
231     TimerTask task = null;
232     task = new FileWatcher((File) file) {
233       protected void onChange(File file) {
234         // here we implement the onChange
235         logger.info(CrudServiceMsgs.OXM_FILE_CHANGED, file.getName());
236
237         try {
238           // Cannot use the file object here because we also have to get the
239           // edge properties associated with that version.
240           // The properties are stored in a different file.
241           RelationshipSchemaLoader.loadModels(version);
242         } catch (Exception e) {
243           e.printStackTrace();
244         }
245       }
246     };
247
248     if (!timers.containsKey(version)) {
249       Timer timer = new Timer("db_edge_rules_" + version);
250       timer.schedule(task, new Date(), 10000);
251       timers.put(version, timer);
252
253     }
254   }
255
256   private static String myMatcher(Pattern p, String s) {
257     Matcher m = p.matcher(s);
258     return m.matches() ? m.group(1) : "";
259   }
260 }