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