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