Process multi-OXM files
[aai/gizmo.git] / src / main / java / org / onap / schema / RelationshipSchemaLoader.java
index 48727a0..9a9a37c 100644 (file)
@@ -1,16 +1,15 @@
 /**
  * ============LICENSE_START=======================================================
- * Gizmo
+ * org.onap.aai
  * ================================================================================
- * Copyright © 2017 AT&T Intellectual Property.
- * Copyright © 2017 Amdocs
- * All rights reserved.
+ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2017-2018 Amdocs
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *    http://www.apache.org/licenses/LICENSE-2.0
+ *       http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -18,8 +17,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * ============LICENSE_END=========================================================
- *
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
  */
 package org.onap.schema;
 
@@ -32,8 +29,10 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Comparator;
 import java.util.Date;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.SortedSet;
 import java.util.Timer;
 import java.util.TimerTask;
@@ -42,15 +41,14 @@ import java.util.concurrent.ConcurrentHashMap;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
-
 import javax.ws.rs.core.Response.Status;
-
 import org.apache.commons.io.IOUtils;
 import org.onap.aai.cl.eelf.LoggerFactory;
 import org.onap.crud.exception.CrudException;
 import org.onap.crud.logging.CrudServiceMsgs;
 import org.onap.crud.util.CrudServiceConstants;
 import org.onap.crud.util.FileWatcher;
+import org.springframework.core.io.Resource;
 import org.springframework.core.io.UrlResource;
 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
 import org.springframework.core.io.support.ResourcePatternResolver;
@@ -74,7 +72,7 @@ public class RelationshipSchemaLoader {
   }
 
   public synchronized static void loadModels(String version) throws CrudException {
-    String pattern = String.format(".*(%s)" + fileExt, version);
+    String pattern = String.format("DbEdgeRules.*(%s)" + fileExt, version);
     load(Pattern.compile(pattern), Pattern.compile(edgePropsFiles + version + fileExt));
   }
 
@@ -114,19 +112,30 @@ public class RelationshipSchemaLoader {
   private static void load(Pattern rulesPattern, Pattern edgePropsPattern) throws CrudException {
     ClassLoader cl = RelationshipSchemaLoader.class.getClassLoader();
     ResourcePatternResolver rulesResolver = new PathMatchingResourcePatternResolver(cl);
-    List<Object> rulesFiles;
+    List<Object> rulesFiles = new ArrayList<Object>();
+    Set<String> existingFiles = new HashSet<String>();
     String rulesDir = CrudServiceConstants.CRD_HOME_MODEL;
     try {
 
-      // getResources method returns objects of type "Resource"
-      // 1. We are getting all the objects from the classpath which has
-      // "DbEdgeRules" in the name.
-      // 2. We run them through a filter and return only the objects which match
-      // the supplied pattern "p"
-      // 3. We then collect the objects in a list. At this point we have a list
-      // of the kind of files we require.
-      rulesFiles = Arrays.stream(rulesResolver.getResources("classpath*:/dbedgerules/DbEdgeRules*" + fileExt))
-          .filter(r -> !myMatcher(rulesPattern, r.getFilename()).isEmpty()).collect(Collectors.toList());
+      // Allow additional DBEdgeRule files to be dropped in manually (in addition to those found on the classpath)
+      File[] edgeRuleFileList = new File(rulesDir).listFiles((d, name) -> rulesPattern.matcher(name).matches());
+      for (File file : edgeRuleFileList) {
+        rulesFiles.add(file);
+        existingFiles.add(filename(file));
+      }
+
+      // Get DBEdgeRules from the jar on the classpath.  Don't include any that conflict with files which
+      // were dropped manually.
+      Resource[] rawResourceList = rulesResolver.getResources("classpath*:/dbedgerules/DbEdgeRules*" + fileExt);
+      List<Resource> prunedResourceList = new ArrayList<Resource>();
+      for (Resource resource : rawResourceList) {
+        if (!existingFiles.contains(filename(resource))) {
+          prunedResourceList.add(resource);
+        }
+      }
+
+      rulesFiles.addAll(Arrays.stream(prunedResourceList.toArray(new Resource[prunedResourceList.size()]))
+          .filter(r -> !myMatcher(rulesPattern, r.getFilename()).isEmpty()).collect(Collectors.toList()));
 
       // This gets all the objects of type "File" from external directory (not
       // on the classpath)
@@ -138,7 +147,7 @@ public class RelationshipSchemaLoader {
       // this list
       // to the previous collection (rulesFiles)
       rulesFiles
-          .addAll(Arrays.stream(new File(rulesDir).listFiles((d, name) -> edgePropsPattern.matcher(name).matches()))
+          .addAll(Arrays.stream(new File(rulesDir).listFiles( (d, name) -> edgePropsPattern.matcher(name).matches()  ))
               .collect(Collectors.toList()));
 
       if (rulesFiles.isEmpty()) {
@@ -260,4 +269,4 @@ public class RelationshipSchemaLoader {
     Matcher m = p.matcher(s);
     return m.matches() ? m.group(1) : "";
   }
-}
+}
\ No newline at end of file