Safeguard against deprecated DbEdgeRules versions.
[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) -> {
149                 if (resourceAndFile.size() == 2 ) {
150                   versionContextMap.put(version, jsonFilesLoader(version, resourceAndFile));
151                 } else {
152                   String filenames = resourceAndFile.stream().map(f-> filename(f)).collect(Collectors.toList()).toString();
153                   String errorMsg = "Expecting a rules and a edge_properties files for " + version + ". Found: " + filenames;
154                   logger.warn(CrudServiceMsgs.INVALID_OXM_FILE, errorMsg);
155                 }});
156
157       logger.info(CrudServiceMsgs.LOADED_OXM_FILE, "Relationship Schema and Properties files: " + rulesFiles.stream().map(f -> filename(f)).collect(Collectors.toList()));
158     } catch (IOException e) {
159       logger.error(CrudServiceMsgs.INVALID_OXM_DIR, rulesDir);
160       throw new CrudException("DbEdgeRules or edge_properties files were not found.", new FileNotFoundException());
161     }
162   }
163
164   private static String filename (Object k) throws ClassCastException {
165     if (k instanceof UrlResource){
166       return ((UrlResource) k).getFilename();
167     } else if (k instanceof File) {
168       return ((File) k).getName();
169     } else {
170       throw new ClassCastException();
171     }
172   }
173
174   private static RelationshipSchema jsonFilesLoader (String version, List<Object> files) {
175     List<String> fileContents = new ArrayList<>();
176     RelationshipSchema rsSchema = null;
177     if (files.size() == 2) {
178       for (Object file : files) {
179         fileContents.add(jsonToRelationSchema(version, file));
180         versions.add(Integer.parseInt(version.substring(1)));
181       }
182
183       try {
184         rsSchema = new RelationshipSchema(fileContents);
185       } catch (CrudException | IOException e) {
186         e.printStackTrace();
187         logger.error(CrudServiceMsgs.INVALID_OXM_FILE,
188                 files.stream().map(f -> filename(f)).collect(Collectors.toList()).toString(), e.getMessage());
189       }
190       return rsSchema;
191     }
192     return rsSchema;
193   }
194
195   private synchronized static void updateVersionContext(String version, RelationshipSchema rs){
196     versionContextMap.put(version, rs);
197   }
198
199   private synchronized static String jsonToRelationSchema (String version, Object file) {
200     InputStream inputStream = null;
201     String content = null;
202
203     try {
204       if (file instanceof  UrlResource) {
205         inputStream = ((UrlResource) file).getInputStream();
206       } else {
207         inputStream = new FileInputStream((File) file);
208         addtimer(version, file);
209       }
210       content =  IOUtils.toString(inputStream, "UTF-8");
211     } catch (IOException e) {
212       e.printStackTrace();
213     }
214     return content;
215   }
216
217   private static void addtimer(String version, Object file) {
218     TimerTask task = null;
219     task = new FileWatcher(
220             (File) file) {
221       protected void onChange(File file) {
222         // here we implement the onChange
223         logger.info(CrudServiceMsgs.OXM_FILE_CHANGED, file.getName());
224
225         try {
226           // Cannot use the file object here because we also have to get the edge properties associated with that version.
227           // The properties are stored in a different file.
228           RelationshipSchemaLoader.loadModels(version);
229         } catch (Exception e) {
230           e.printStackTrace();
231         }
232       }
233     };
234
235     if (!timers.containsKey(version)) {
236       Timer timer = new Timer("db_edge_rules_" + version);
237       timer.schedule(task, new Date(), 10000);
238       timers.put(version, timer);
239
240     }
241   }
242
243   private static String myMatcher (Pattern p, String s) {
244     Matcher m = p.matcher(s);
245     return m.matches() ? m.group(1) : "";
246   }
247 }