package org.openecomp.aai.serialization.db;
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
-import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.Multimap;
-import com.jayway.jsonpath.DocumentContext;
-import com.jayway.jsonpath.Filter;
-import com.jayway.jsonpath.JsonPath;
+import static com.jayway.jsonpath.Criteria.where;
+import static com.jayway.jsonpath.Filter.filter;
+
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Optional;
+import java.util.Scanner;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.openecomp.aai.db.props.AAIProperties;
-import org.openecomp.aai.dbmodel.DbEdgeRules;
import org.openecomp.aai.exceptions.AAIException;
import org.openecomp.aai.introspection.Version;
import org.openecomp.aai.serialization.db.exceptions.EdgeMultiplicityException;
import org.openecomp.aai.serialization.db.exceptions.NoEdgeRuleFoundException;
-import java.io.InputStream;
-import java.util.*;
-import java.util.Map.Entry;
-import java.util.concurrent.ConcurrentHashMap;
-
-import static com.jayway.jsonpath.Criteria.where;
-import static com.jayway.jsonpath.Filter.filter;
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Multimap;
+import com.jayway.jsonpath.DocumentContext;
+import com.jayway.jsonpath.Filter;
+import com.jayway.jsonpath.JsonPath;
public class EdgeRules {
private EELFLogger logger = EELFManager.getInstance().getLogger(EdgeRules.class);
- private Multimap<String, String> deleteScope = DbEdgeRules.DefaultDeleteScope;
-
private DocumentContext rulesDoc;
/**
}
+ private EdgeRules(String rulesFilename) {
+ String json = this.getEdgeRuleJson(rulesFilename);
+ rulesDoc = JsonPath.parse(json);
+ }
+
+ private String getEdgeRuleJson(String rulesFilename) {
+ InputStream is = getClass().getResourceAsStream(rulesFilename);
+
+ Scanner scanner = new Scanner(is);
+ String json = scanner.useDelimiter("\\Z").next();
+ scanner.close();
+
+ return json;
+ }
+
/**
* Loads the versioned DbEdgeRules json file for later parsing.
*/
@SuppressWarnings("unchecked")
private EdgeRules(Version version) {
-
String json = this.getEdgeRuleJson(version);
rulesDoc = JsonPath.parse(json);
-
- if (!Version.isLatest(version)) {
- try {
- Class<?> dbEdgeRules = Class.forName("org.openecomp.aai.dbmodel." + version.toString() + ".gen.DbEdgeRules");
- this.deleteScope = (Multimap<String, String>)dbEdgeRules.getDeclaredField("DefaultDeleteScope").get(null);
- } catch (Exception e) {
- }
- }
}
private String getEdgeRuleJson(Version version) {
private static class Helper {
private static final EdgeRules INSTANCE = new EdgeRules();
private static final Map<Version, EdgeRules> INSTANCEMAP = new ConcurrentHashMap<>();
+
+ private static EdgeRules getEdgeRulesByFilename(String rulesFilename) {
+ return new EdgeRules(rulesFilename);
+ }
+
private static EdgeRules getVersionedEdgeRules(Version v) {
if (Version.isLatest(v)) {
return INSTANCE;
}
+ /**
+ * Loads edge rules from the given file.
+ *
+ * @param rulesFilename - name of the file to load rules from
+ * @return the EdgeRules instance
+ */
+ public static EdgeRules getInstance(String rulesFilename) {
+ return Helper.getEdgeRulesByFilename(rulesFilename);
+ }
+
/**
* Adds the tree edge.
*
where("from").is(nodeB).and("to").is(nodeA)
);
- List<Object> results = rulesDoc.read("$.rules.[?]", aToB);
- results.addAll(rulesDoc.read("$.rules.[?]", bToA));
+ List<Map<String, String>> results = readRules(aToB);
+ results.addAll(readRules(bToA));
return !results.isEmpty();
return result;
}
+
+
/**
* Gets the edge rule of the given type that exists between A and B.
* Will check B|A as well, and flips the direction accordingly if that succeeds
*/
public EdgeRule getEdgeRule(EdgeType type, String nodeA, String nodeB) throws AAIException {
//try A to B
- List<Map<String, String>> aToBEdges = rulesDoc.read("$.rules.[?]", buildFilter(type, nodeA, nodeB));
+ List<Map<String, String>> aToBEdges = readRules(buildFilter(type, nodeA, nodeB));
if (!aToBEdges.isEmpty()) {
//lazily stop iterating if we find a match
//should there be a mismatch between type and isParent,
//the caller will receive something.
//this operates on the assumption that there are at most two rules
//for a given vertex pair
+ verifyRule(aToBEdges.get(0));
return buildRule(aToBEdges.get(0));
}
//we get here if there was nothing for A to B, so let's try B to A
- List<Map<String, String>> bToAEdges = rulesDoc.read("$.rules.[?]", buildFilter(type, nodeB, nodeA));
+ List<Map<String, String>> bToAEdges = readRules(buildFilter(type, nodeB, nodeA));
if (!bToAEdges.isEmpty()) {
+ verifyRule(bToAEdges.get(0));
return flipDirection(buildRule(bToAEdges.get(0))); //bc we need to return as A|B, so flip the direction to match
}
}
+ /**
+ * Verifies that all required properties are defined in the given edge rule.
+ * If they are not, throws a RuntimeException.
+ *
+ * @param rule - Map<String edge property, String edge property value> representing
+ * an edge rule
+ */
+ private void verifyRule(Map<String, String> rule) {
+ for (EdgeProperty prop : EdgeProperty.values()) {
+ if (!rule.containsKey(prop.toString())) {
+ /* Throws RuntimeException as rule definition errors
+ * cannot be recovered from, and should never happen anyway
+ * because these are configuration files, so requiring all
+ * downstream code to check for this exception seems inappropriate.
+ * It's instantiated with an AAIException to make sure all
+ * relevant information is present in the error message.
+ */
+ throw new RuntimeException(new AAIException("AAI_4005",
+ "Rule between " + rule.get("from") + " and " + rule.get("to") +
+ " is missing property " + prop + "."));
+ }
+ }
+ }
+
+ /**
+ * Reads all the edge rules from the loaded json file.
+ *
+ * @return List<Map<String edge property, String edge property value>>
+ * Each map represents a rule read from the json.
+ */
+ private List<Map<String, String>> readRules() {
+ return readRules(null);
+ }
+
+ /**
+ * Reads the edge rules from the loaded json file, using the given filter
+ * to get specific rules. If filter is null, will get all rules.
+ *
+ * @param filter - may be null to indicate get all
+ * @return List<Map<String edge property, String edge property value>>
+ * Each map represents a rule read from the json.
+ */
+ private List<Map<String, String>> readRules(Filter filter) {
+ List<Map<String, String>> results;
+ if (filter == null) { //no filter means get all
+ results = rulesDoc.read("$.rules.*");
+ } else {
+ results = rulesDoc.read("$.rules.[?]", filter);
+ }
+ for (Map<String, String> result : results) {
+ verifyRule(result);
+ }
+ return results;
+ }
+
/**
* Gets all the edge rules we define.
*
public Multimap<String, EdgeRule> getAllRules() {
Multimap<String, EdgeRule> result = ArrayListMultimap.create();
- List<Map<String, String>> rules = rulesDoc.read("$.rules.*");
+ List<Map<String, String>> rules = readRules();
for (Map<String, String> rule : rules) {
EdgeRule er = buildRule(rule);
String name = rule.get("from") + "|" + rule.get("to");
return result;
}
- public Multimap<String, String> getDeleteSemantics() {
- return this.deleteScope;
- }
-
+ /**
+ * Gets all edge rules that define a child relationship from
+ * the given node type.
+ *
+ * @param nodeType
+ * @return
+ */
public Set<EdgeRule> getChildren(String nodeType) {
final Filter filter = filter(
where("from").is(nodeType).and(EdgeProperty.CONTAINS.toString()).is("${direction}")
).or(where("to").is(nodeType).and(EdgeProperty.CONTAINS.toString()).is("!${direction}"));
- final List<Map<String, String>> rules = rulesDoc.read("$.rules.[?]", filter);
+ final List<Map<String, String>> rules = readRules(filter);
final Set<EdgeRule> result = new HashSet<>();
rules.forEach(item -> {
+ verifyRule(item);
result.add(buildRule(item));
});