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