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