2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.aai.serialization.db;
22 import static com.jayway.jsonpath.Criteria.where;
23 import static com.jayway.jsonpath.Filter.filter;
25 import java.io.InputStream;
27 import java.util.Map.Entry;
28 import java.util.concurrent.ConcurrentHashMap;
30 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
31 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
32 import org.apache.tinkerpop.gremlin.structure.Direction;
33 import org.apache.tinkerpop.gremlin.structure.Edge;
34 import org.apache.tinkerpop.gremlin.structure.Vertex;
35 import org.onap.aai.db.props.AAIProperties;
36 import org.onap.aai.exceptions.AAIException;
37 import org.onap.aai.introspection.Version;
38 import org.onap.aai.serialization.db.exceptions.EdgeMultiplicityException;
39 import org.onap.aai.serialization.db.exceptions.MultipleEdgeRuleFoundException;
40 import org.onap.aai.serialization.db.exceptions.NoEdgeRuleFoundException;
42 import com.att.eelf.configuration.EELFLogger;
43 import com.att.eelf.configuration.EELFManager;
44 import com.google.common.collect.ArrayListMultimap;
45 import com.google.common.collect.Multimap;
46 import com.jayway.jsonpath.DocumentContext;
47 import com.jayway.jsonpath.Filter;
48 import com.jayway.jsonpath.JsonPath;
50 public class EdgeRules {
52 private static final String LABEL = "label";
54 private static final String NOT_DIRECTION_NOTATION = "!${direction}";
56 private static final String DIRECTION_NOTATION = "${direction}";
58 private EELFLogger logger = EELFManager.getInstance().getLogger(EdgeRules.class);
60 private DocumentContext rulesDoc;
63 * Loads the most recent DbEdgeRules json file for later parsing.
64 * Only need most recent version for actual A&AI operations that call this class;
65 * the old ones are only used in tests.
69 String json = this.getEdgeRuleJson(Version.getLatest());
70 rulesDoc = JsonPath.parse(json);
74 private EdgeRules(String rulesFilename) {
75 String json = this.getEdgeRuleJson(rulesFilename);
76 rulesDoc = JsonPath.parse(json);
80 * Loads the versioned DbEdgeRules json file for later parsing.
82 private EdgeRules(Version version) {
83 String json = this.getEdgeRuleJson(version);
84 rulesDoc = JsonPath.parse(json);
88 * Gets the single instance of EdgeRules.
90 * @return single instance of EdgeRules
92 public static EdgeRules getInstance() {
93 return Helper.INSTANCE;
98 * Gets the versioned instance of EdgeRules.
100 * @return versioned instance of EdgeRules
102 public static EdgeRules getInstance(Version v) {
103 return Helper.getVersionedEdgeRules(v);
108 * Loads edge rules from the given file.
110 * @param rulesFilename - name of the file to load rules from
111 * @return the EdgeRules instance
113 public static EdgeRules getInstance(String rulesFilename) {
114 return Helper.getEdgeRulesByFilename(rulesFilename);
117 private String getEdgeRuleJson(String rulesFilename) {
118 InputStream is = getClass().getResourceAsStream(rulesFilename);
120 Scanner scanner = new Scanner(is);
121 String json = scanner.useDelimiter("\\Z").next();
127 private String getEdgeRuleJson(Version version) {
128 return this.getEdgeRuleJson("/dbedgerules/DbEdgeRules_" + version.toString() + ".json");
132 * Adds the tree edge.
134 * @param aVertex the out vertex
135 * @param bVertex the in vertex
137 * @throws AAIException the AAI exception
139 public Edge addTreeEdge(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex) throws AAIException {
140 return this.addEdge(EdgeType.TREE, traversalSource, aVertex, bVertex, false, null);
146 * @param aVertex the out vertex
147 * @param bVertex the in vertex
149 * @throws AAIException the AAI exception
151 public Edge addEdge(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex) throws AAIException {
152 return this.addEdge(traversalSource, aVertex, bVertex, null);
155 public Edge addEdge(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex, String label) throws AAIException {
156 return this.addEdge(EdgeType.COUSIN, traversalSource, aVertex, bVertex, false, label);
160 * Adds the tree edge.
162 * @param aVertex the out vertex
163 * @param bVertex the in vertex
165 * @throws AAIException the AAI exception
167 public Edge addTreeEdgeIfPossible(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex) throws AAIException {
168 return this.addEdge(EdgeType.TREE, traversalSource, aVertex, bVertex, true, null);
174 * @param aVertex the out vertex
175 * @param bVertex the in vertex
177 * @throws AAIException the AAI exception
179 public Edge addEdgeIfPossible(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex) throws AAIException {
180 return this.addEdgeIfPossible(traversalSource, aVertex, bVertex, null);
183 public Edge addEdgeIfPossible(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex, String label) throws AAIException {
184 return this.addEdge(EdgeType.COUSIN, traversalSource, aVertex, bVertex, true, label);
190 * @param type the type
191 * @param aVertex the out vertex
192 * @param bVertex the in vertex
194 * @throws AAIException the AAI exception
196 private Edge addEdge(EdgeType type, GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex, boolean isBestEffort, String label) throws AAIException {
198 EdgeRule rule = this.getEdgeRule(type, aVertex, bVertex, label);
202 Optional<String> message = this.validateMultiplicity(rule, traversalSource, aVertex, bVertex);
204 if (message.isPresent() && !isBestEffort) {
205 throw new EdgeMultiplicityException(message.get());
207 if (!message.isPresent()) {
208 if (rule.getDirection().equals(Direction.OUT)) {
209 e = aVertex.addEdge(rule.getLabel(), bVertex);
210 } else if (rule.getDirection().equals(Direction.IN)) {
211 e = bVertex.addEdge(rule.getLabel(), aVertex);
214 this.addProperties(e, rule);
220 * Adds the properties.
222 * @param edge the edge
223 * @param rule the rule
225 public void addProperties(Edge edge, EdgeRule rule) {
227 // In DbEdgeRules.EdgeRules -- What we have as "edgeRule" is a comma-delimited set of strings.
228 // The first item is the edgeLabel.
229 // The second in the list is always "direction" which is always OUT for the way we've implemented it.
230 // Items starting at "firstTagIndex" and up are all assumed to be booleans that map according to
231 // tags as defined in EdgeInfoMap.
232 // Note - if they are tagged as 'reverse', that means they get the tag name with "-REV" on it
233 Map<EdgeProperty, String> propMap = rule.getEdgeProperties();
235 for (Entry<EdgeProperty, String> entry : propMap.entrySet()) {
236 edge.property(entry.getKey().toString(), entry.getValue());
239 edge.property(AAIProperties.AAI_UUID, UUID.randomUUID().toString());
243 * Checks if any edge rules exist between the two given node types, in either A|B or B|A order.
245 * @param nodeA - node at one end of the edge
246 * @param nodeB - node at the other end
247 * @return true, if any such rules exist
249 public boolean hasEdgeRule(String nodeA, String nodeB) {
250 return this.hasEdgeRule(nodeA, nodeB, null);
254 * Checks if any edge rules exist between the two given node types with contains-other-v !NONE, in either A|B or B|A order.
256 * @param nodeA - node at one end of the edge
257 * @param nodeB - node at the other end
258 * @return true, if any such rules exist
260 public boolean hasTreeEdgeRule(String nodeA, String nodeB) {
261 return this.hasEdgeRule(EdgeType.TREE, nodeA, nodeB, null);
265 * Checks if any edge rules exist between the two given node types with contains-other-v NONE, in either A|B or B|A order.
267 * @param nodeA - node at one end of the edge
268 * @param nodeB - node at the other end
269 * @param label - edge label
270 * @return true, if any such rules exist
272 public boolean hasCousinEdgeRule(String nodeA, String nodeB, String label) {
273 return this.hasEdgeRule(EdgeType.COUSIN, nodeA, nodeB, label);
277 * Checks if any edge rules exist between the two given nodes with contains-other-v !NONE, in either A|B or B|A order.
279 * @param aVertex - node at one end of the edge
280 * @param bVertex - node at the other end
281 * @return true, if any such rules exist
283 public boolean hasTreeEdgeRule(Vertex aVertex, Vertex bVertex) {
284 String outType = aVertex.<String>property(AAIProperties.NODE_TYPE).orElse(null);
285 String inType = bVertex.<String>property(AAIProperties.NODE_TYPE).orElse(null);
286 return this.hasTreeEdgeRule(outType, inType);
290 * Checks if any edge rules exist between the two given nodes with contains-other-v NONE with edge label, in either A|B or B|A order.
292 * @param aVertex - node at one end of the edge
293 * @param bVertex - node at the other end
294 * @param label - edge label
295 * @return true, if any such rules exist
297 public boolean hasCousinEdgeRule(Vertex aVertex, Vertex bVertex, String label) {
298 String outType = aVertex.<String>property(AAIProperties.NODE_TYPE).orElse(null);
299 String inType = bVertex.<String>property(AAIProperties.NODE_TYPE).orElse(null);
300 return this.hasCousinEdgeRule(outType, inType, label);
304 * Checks if any edge rules exist between the two given nodes w/ edge label, in either A|B or B|A order.
306 * @param nodeA - node at one end of the edge
307 * @param nodeB - node at the other end
308 * @param label - edge label
309 * @return true, if any such rules exist
311 public boolean hasEdgeRule(String nodeA, String nodeB, String label) {
312 return this.hasEdgeRule(null, nodeA, nodeB, label);
316 * Checks if any edge rules exist between the two given nodes, in either A|B or B|A order.
318 * @param aVertex - node at one end of the edge
319 * @param bVertex - node at the other end
320 * @return true, if any such rules exist
322 public boolean hasEdgeRule(Vertex aVertex, Vertex bVertex) {
323 return this.hasEdgeRule(aVertex, bVertex, null);
328 * Checks if any edge rules exist between the two given nodes with label, in either A|B or B|A order with edge label.
330 * @param aVertex - node at one end of the edge
331 * @param bVertex - node at the other end
332 * @param label - edge label
333 * @return true, if any such rules exist
335 public boolean hasEdgeRule(Vertex aVertex, Vertex bVertex, String label) {
336 String outType = aVertex.<String>property(AAIProperties.NODE_TYPE).orElse(null);
337 String inType = bVertex.<String>property(AAIProperties.NODE_TYPE).orElse(null);
340 return this.hasEdgeRule(outType, inType);
342 return this.hasEdgeRule(outType, inType, label);
347 * Checks if any edge rules exist between the two given node types, in either A|B or B|A order with edge label and edge type.
349 * @param type - type of edge EdgeType.COUSIN | EdgeType.TREE
350 * @param nodeA - node at one end of the edge
351 * @param nodeB - node at the other end
352 * @param label - edge label
353 * @return true, if any such rules exist
355 public boolean hasEdgeRule(EdgeType type, String nodeA, String nodeB, String label) {
356 Filter aToB = filter(
357 where("from").is(nodeA)
360 Filter bToA = filter(
361 where("from").is(nodeB)
365 if (EdgeType.TREE.equals(type)) {
366 aToB = aToB.and(where(EdgeProperty.CONTAINS.toString()).ne(AAIDirection.NONE.toString()));
367 bToA = bToA.and(where(EdgeProperty.CONTAINS.toString()).ne(AAIDirection.NONE.toString()));
368 } else if (EdgeType.COUSIN.equals(type)) {
369 aToB = aToB.and(where(EdgeProperty.CONTAINS.toString()).is(AAIDirection.NONE.toString()));
370 bToA = bToA.and(where(EdgeProperty.CONTAINS.toString()).is(AAIDirection.NONE.toString()));
374 aToB = aToB.and(where(LABEL).is(label));
375 bToA = bToA.and(where(LABEL).is(label));
378 List<Object> results = rulesDoc.read("$.rules.[?]", aToB);
379 results.addAll(rulesDoc.read("$.rules.[?]", bToA));
381 return !results.isEmpty();
385 * Gets all the edge rules that exist between the given node types.
386 * The rules will be phrased in terms of out|in, though this will
387 * also find rules defined as in|out (it will flip the direction in
388 * the EdgeRule object returned accordingly to match out|in).
392 * @return Map<String edgeLabel, EdgeRule rule> where edgeLabel is the label name
393 * @throws AAIException
395 public Map<String, EdgeRule> getEdgeRules(String outType, String inType) {
396 return this.getEdgeRules(outType, inType, null);
400 * Gets all the edge rules that exist between the given node types with given label.
401 * The rules will be phrased in terms of out|in, though this will
402 * also find rules defined as in|out (it will flip the direction in
403 * the EdgeRule object returned accordingly to match out|in).
408 * @return Map<String edgeLabel, EdgeRule rule> where edgeLabel is the label name
409 * @throws AAIException
411 public Map<String, EdgeRule> getEdgeRules(String outType, String inType, String label) {
412 final Map<String, EdgeRule> result = new HashMap<>();
414 for (EdgeType type : EdgeType.values()) {
415 result.putAll(this.getEdgeRules(type, outType, inType, label));
422 * Looks up edge rules for the given node types and the labels specified
428 * @throws NoEdgeRuleFoundException
429 * @throws MultipleEdgeRuleFoundException
431 public Map<String, EdgeRule> getEdgeRulesWithLabels(EdgeType type, String outType, String inType, List<String> labels) throws NoEdgeRuleFoundException, MultipleEdgeRuleFoundException {
432 final Map<String, EdgeRule> result = new HashMap<>();
434 if (labels == null || labels.isEmpty()) {
435 throw new NoEdgeRuleFoundException("No labels specified");
437 for (String label : labels) {
438 EdgeRule er = this.getEdgeRule(type, outType, inType, label);
439 result.put(er.getLabel(), er);
446 * Gets all the edge rules of that edge type that exist between the given node types with given label.
447 * The rules will be phrased in terms of out|in, though this will
448 * also find rules defined as in|out (it will flip the direction in
449 * the EdgeRule object returned accordingly to match out|in).
456 * @throws AAIException
458 public Map<String, EdgeRule> getEdgeRules(EdgeType type, String outType, String inType, String label) {
459 final Map<String, EdgeRule> result = new HashMap<>();
461 this.getEdgeRulesFromJson(type, outType, inType, label).forEach(edgeRuleJson -> {
462 EdgeRule edgeRule = this.buildRule(edgeRuleJson);
463 result.put(edgeRule.getLabel(), edgeRule);
465 this.getEdgeRulesFromJson(type, inType, outType, label).forEach(erj -> {
466 EdgeRule edgeRule = this.flipDirection(this.buildRule(erj));
467 if (!result.containsKey(edgeRule.getLabel())) {
468 result.put(edgeRule.getLabel(), edgeRule);
477 * Gets all the edge rules of that edge type that exist between the given node types.
478 * The rules will be phrased in terms of out|in, though this will
479 * also find rules defined as in|out (it will flip the direction in
480 * the EdgeRule object returned accordingly to match out|in).
486 * @throws AAIException
488 public Map<String, EdgeRule> getEdgeRules(EdgeType type, String outType, String inType) {
489 return this.getEdgeRules(type, outType, inType, null);
493 * Gets the edge rule of the given type that exists between A and B.
494 * Will check B|A as well, and flips the direction accordingly if that succeeds
495 * to match the expected A|B return.
497 * @param type - the type of edge you're looking for
498 * @param nodeA - first node type
499 * @param nodeB - second node type
500 * @return EdgeRule describing the rule in terms of A|B, if there is any such rule
501 * @throws AAIException if no such edge exists
503 public EdgeRule getEdgeRule(EdgeType type, String nodeA, String nodeB) throws AAIException {
504 return this.getEdgeRule(type, nodeA, nodeB, null);
508 * Gets the edge rule of the given type that exists between A and B with edge label.
509 * Will check B|A as well, and flips the direction accordingly if that succeeds
510 * to match the expected A|B return.
512 * @param type - the type of edge you're looking for
513 * @param nodeA - first node type
514 * @param nodeB - second node type
515 * @param label - edge label
516 * @return EdgeRule describing the rule in terms of A|B, if there is any such rule
517 * @throws MultipleEdgeRuleFoundException
518 * @throws AAIException if no such edge exists
520 public EdgeRule getEdgeRule(EdgeType type, String nodeA, String nodeB, String label) throws NoEdgeRuleFoundException, MultipleEdgeRuleFoundException {
522 final StringBuilder errorMsg = new StringBuilder();
523 errorMsg.append(type.toString())
524 .append(" edge rule between ")
525 .append(nodeA).append(" and ").append(nodeB);
527 errorMsg.append(" with label ").append(label);
531 Map<String, EdgeRule> edgeRules = this.getEdgeRules(type, nodeA, nodeB, label);
534 if (edgeRules.isEmpty()) {
536 throw new NoEdgeRuleFoundException("no " + errorMsg);
538 } else if (edgeRules.size() == 1) {
540 edgeRule = edgeRules.values().iterator().next();
544 Optional<EdgeRule> optionalEdgeRule = Optional.empty();
547 optionalEdgeRule = this.getDefaultEdgeRule(edgeRules);
548 } catch (MultipleEdgeRuleFoundException e) {
549 throw new MultipleEdgeRuleFoundException("multiple default edge rule exists " + errorMsg);
552 edgeRule = optionalEdgeRule.orElseThrow(() -> new MultipleEdgeRuleFoundException("multiple edge rule exists with no default " + errorMsg));
559 private Optional<EdgeRule> getDefaultEdgeRule(Map<String, EdgeRule> edgeRules) throws MultipleEdgeRuleFoundException {
561 EdgeRule edgeRule = null;
564 for (Map.Entry<String, EdgeRule> entry : edgeRules.entrySet()) {
565 if (entry.getValue().isDefault()) {
566 edgeRule = entry.getValue();
571 if (numDefaults > 1) {
572 throw new MultipleEdgeRuleFoundException("");
575 if (edgeRule == null) {
576 return Optional.empty();
578 return Optional.of(edgeRule);
583 * Gets the rules from the edge rules Json
586 * @param nodeA - start node
587 * @param nodeB - end node
588 * @param label - edge label to filter on
591 private List<Map<String, String>> getEdgeRulesFromJson(EdgeType type, String nodeA, String nodeB, String label) {
593 return rulesDoc.read("$.rules.[?]", buildFilter(type, nodeA, nodeB));
595 return rulesDoc.read("$.rules.[?]", buildFilter(type, nodeA, nodeB, label));
600 * Builds a JsonPath filter to search for an edge from nodeA to nodeB with the given edge type (cousin or parent/child)
603 * @param nodeA - start node
604 * @param nodeB - end node
607 private Filter buildFilter(EdgeType type, String nodeA, String nodeB) {
608 return this.buildFilter(type, nodeA, nodeB, null);
611 private Filter buildFilter(EdgeType type, String nodeA, String nodeB, String label) {
612 if (EdgeType.COUSIN.equals(type)) {
614 where("from").is(nodeA)
616 .and(EdgeProperty.CONTAINS.toString()).is(AAIDirection.NONE.toString())
619 f = f.and(where(LABEL).is(label));
625 where("from").is(nodeA).and("to").is(nodeB).and(EdgeProperty.CONTAINS.toString()).is(DIRECTION_NOTATION)).or(
626 where("from").is(nodeA).and("to").is(nodeB).and(EdgeProperty.CONTAINS.toString()).is(NOT_DIRECTION_NOTATION)
632 * Puts the give edge rule information into an EdgeRule object.
634 * @param map edge rule property map
635 * @return EdgeRule containing that information
637 private EdgeRule buildRule(Map<String, String> map) {
638 Map<String, String> edge = new EdgePropertyMap<>();
641 EdgeRule rule = new EdgeRule();
642 rule.setLabel(edge.get(LABEL));
643 rule.setDirection(edge.get("direction"));
644 rule.setMultiplicityRule(edge.get("multiplicity"));
645 rule.setContains(edge.get(EdgeProperty.CONTAINS.toString()));
646 rule.setDeleteOtherV(edge.get(EdgeProperty.DELETE_OTHER_V.toString()));
647 rule.setServiceInfrastructure(edge.get(EdgeProperty.SVC_INFRA.toString()));
648 rule.setPreventDelete(edge.get(EdgeProperty.PREVENT_DELETE.toString()));
649 rule.setTo(edge.get("to"));
650 rule.setFrom(edge.get("from"));
651 if (edge.containsKey("default")) {
652 rule.setIsDefault(edge.get("default"));
655 if(rule.getFrom().equals(rule.getTo())){
656 return this.flipDirection(rule);
663 * If getEdgeRule gets a request for A|B, and it finds something as B|A, the caller still expects
664 * the returned EdgeRule to reflect A|B directionality. This helper method flips B|A direction to
665 * match this expectation.
667 * @param rule whose direction needs flipped
668 * @return the updated rule
670 private EdgeRule flipDirection(EdgeRule rule) {
671 if (Direction.IN.equals(rule.getDirection())) {
672 rule.setDirection(Direction.OUT);
674 } else if (Direction.OUT.equals(rule.getDirection())) {
675 rule.setDirection(Direction.IN);
677 } else { //direction is BOTH, flipping both is still both
683 * Gets the edge rule of the given type that exists between A and B.
684 * Will check B|A as well, and flips the direction accordingly if that succeeds
685 * to match the expected A|B return.
687 * @param type - the type of edge you're looking for
688 * @param aVertex - first node type
689 * @param bVertex - second node type
690 * @return EdgeRule describing the rule in terms of A|B, if there is any such rule
691 * @throws AAIException if no such edge exists
693 public EdgeRule getEdgeRule(EdgeType type, Vertex aVertex, Vertex bVertex) throws AAIException {
694 return this.getEdgeRule(type, aVertex, bVertex, null);
698 * Gets the edge rule of the given type that exists between A and B with label.
699 * Will check B|A as well, and flips the direction accordingly if that succeeds
700 * to match the expected A|B return.
702 * @param type - the type of edge you're looking for
703 * @param aVertex - first node type
704 * @param bVertex - second node type
705 * @param label - edge label
706 * @return EdgeRule describing the rule in terms of A|B, if there is any such rule
707 * @throws AAIException if no such edge exists
709 public EdgeRule getEdgeRule(EdgeType type, Vertex aVertex, Vertex bVertex, String label) throws AAIException {
710 String outType = aVertex.<String>property(AAIProperties.NODE_TYPE).orElse(null);
711 String inType = bVertex.<String>property(AAIProperties.NODE_TYPE).orElse(null);
713 return this.getEdgeRule(type, outType, inType, label);
719 * Validate multiplicity.
721 * @param rule the rule
722 * @param aVertex the out vertex
723 * @param bVertex the in vertex
724 * @return true, if successful
725 * @throws AAIException the AAI exception
727 private Optional<String> validateMultiplicity(EdgeRule rule, GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex) {
732 if (rule.getDirection().equals(Direction.OUT)) {
735 } else if (rule.getDirection().equals(Direction.IN)) {
740 String aVertexType = a.<String>property(AAIProperties.NODE_TYPE).orElse(null);
741 String bVertexType = b.<String>property(AAIProperties.NODE_TYPE).orElse(null);
742 String label = rule.getLabel();
743 MultiplicityRule multiplicityRule = rule.getMultiplicityRule();
744 List<Edge> outEdges = traversalSource.V(a).outE(label).where(__.inV().has(AAIProperties.NODE_TYPE, bVertexType)).toList();
745 List<Edge> inEdges = traversalSource.V(b).inE(label).where(__.outV().has(AAIProperties.NODE_TYPE, aVertexType)).toList();
747 final String msg = "multiplicity rule violated: only one edge can exist with label: ";
748 if (multiplicityRule.equals(MultiplicityRule.ONE2ONE)) {
749 if (!inEdges.isEmpty() || !outEdges.isEmpty() ) {
750 detail = msg + label + " between " + aVertexType + " and " + bVertexType;
752 } else if (multiplicityRule.equals(MultiplicityRule.ONE2MANY)) {
753 if (!inEdges.isEmpty()) {
754 detail = msg + label + " between " + aVertexType + " and " + bVertexType;
756 } else if (multiplicityRule.equals(MultiplicityRule.MANY2ONE)) {
757 if (!outEdges.isEmpty()) {
758 detail = msg + label + " between " + aVertexType + " and " + bVertexType;
762 if (!"".equals(detail)) {
763 return Optional.of(detail);
765 return Optional.empty();
772 * Verifies that all required properties are defined in the given edge rule.
773 * If they are not, throws a RuntimeException.
775 * @param rule - Map<String edge property, String edge property value> representing
778 private void verifyRule(Map<String, String> rule) {
779 for (EdgeProperty prop : EdgeProperty.values()) {
781 // Description is not required as it is only set for v12 versions
782 if("description".equals(prop.toString())){
785 if (!rule.containsKey(prop.toString())) {
786 /* Throws RuntimeException as rule definition errors
787 * cannot be recovered from, and should never happen anyway
788 * because these are configuration files, so requiring all
789 * downstream code to check for this exception seems inappropriate.
790 * It's instantiated with an AAIException to make sure all
791 * relevant information is present in the error message.
793 throw new RuntimeException(new AAIException("AAI_4005",
794 "Rule between " + rule.get("from") + " and " + rule.get("to") +
795 " is missing property " + prop + "."));
801 * Reads all the edge rules from the loaded json file.
803 * @return List<Map<String edge property, String edge property value>>
804 * Each map represents a rule read from the json.
806 private List<Map<String, String>> readRules() {
807 return readRules(null);
811 * Reads the edge rules from the loaded json file, using the given filter
812 * to get specific rules. If filter is null, will get all rules.
814 * @param filter - may be null to indicate get all
815 * @return List<Map<String edge property, String edge property value>>
816 * Each map represents a rule read from the json.
818 private List<Map<String, String>> readRules(Filter filter) {
819 List<Map<String, String>> results;
820 if (filter == null) { //no filter means get all
821 results = rulesDoc.read("$.rules.*");
823 results = rulesDoc.read("$.rules.[?]", filter);
825 for (Map<String, String> result : results) {
832 * Gets all the edge rules we define.
834 * @return Multimap<String "from|to", EdgeRule rule>
836 public Multimap<String, EdgeRule> getAllRules() {
837 Multimap<String, EdgeRule> result = ArrayListMultimap.create();
839 List<Map<String, String>> rules = readRules();
840 for (Map<String, String> rule : rules) {
841 EdgeRule er = buildRule(rule);
842 String name = rule.get("from") + "|" + rule.get("to");
843 result.put(name, er);
850 * Gets all edge rules that define a child relationship from
851 * the given node type.
856 public Set<EdgeRule> getChildren(String nodeType) {
858 final Filter filter = filter(
859 where("from").is(nodeType).and(EdgeProperty.CONTAINS.toString()).is(DIRECTION_NOTATION)
860 ).or(where("to").is(nodeType).and(EdgeProperty.CONTAINS.toString()).is(NOT_DIRECTION_NOTATION));
862 final List<Map<String, String>> rules = readRules(filter);
863 final Set<EdgeRule> result = new HashSet<>();
864 rules.forEach(item -> {
866 result.add(buildRule(item));
873 private static class Helper {
874 private static final EdgeRules INSTANCE = new EdgeRules();
875 private static final Map<Version, EdgeRules> INSTANCEMAP = new ConcurrentHashMap<>();
879 private static EdgeRules getEdgeRulesByFilename(String rulesFilename) {
880 return new EdgeRules(rulesFilename);
883 private static EdgeRules getVersionedEdgeRules(Version v) {
884 if (Version.isLatest(v)) {
887 if (!INSTANCEMAP.containsKey(v)) {
888 INSTANCEMAP.put(v, new EdgeRules(v));
890 return INSTANCEMAP.get(v);