2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-18 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=========================================================
21 package org.onap.aai.edges;
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25 import com.google.common.cache.CacheBuilder;
26 import com.google.common.cache.CacheLoader;
27 import com.google.common.cache.LoadingCache;
28 import com.google.common.collect.ArrayListMultimap;
29 import com.google.common.collect.Multimap;
30 import com.jayway.jsonpath.Criteria;
31 import com.jayway.jsonpath.DocumentContext;
32 import com.jayway.jsonpath.Filter;
33 import org.apache.tinkerpop.gremlin.structure.Direction;
34 import org.onap.aai.edges.enums.DirectionNotation;
35 import org.onap.aai.edges.enums.EdgeField;
36 import org.onap.aai.edges.enums.EdgeType;
37 import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException;
38 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
39 import org.onap.aai.setup.SchemaVersion;
40 import org.onap.aai.setup.SchemaVersions;
41 import org.onap.aai.setup.Translator;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Component;
45 import javax.annotation.PostConstruct;
46 import java.io.IOException;
48 import java.util.Map.Entry;
49 import java.util.concurrent.ExecutionException;
50 import java.util.stream.Collectors;
52 import static com.jayway.jsonpath.Criteria.where;
53 import static com.jayway.jsonpath.Filter.filter;
56 * EdgeIngestor - ingests A&AI edge rule schema files per given config, serves that edge rule
57 * information, including allowing various filters to extract particular rules.
60 public class EdgeIngestor {
61 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(EdgeIngestor.class);
62 private Map<SchemaVersion, List<DocumentContext>> versionJsonFilesMap = new TreeMap<>();
63 private static final String READ_START = "$.rules.[?]";
64 private static final String READ_ALL_START = "$.rules.*";
65 private SchemaVersions schemaVersions;
67 Map<SchemaVersion, List<String>> filesToIngest;
69 private Set<String> multipleLabelKeys;
71 private LoadingCache<SchemaFilter,Multimap<String,EdgeRule>> cacheFilterStore;
73 private LoadingCache<String, String[]> cousinLabelStore;
75 private Set<Translator> translators;
78 public EdgeIngestor(Set<Translator> translatorSet) {
79 this.translators = translatorSet;
83 public void initialize() {
85 for (Translator translator : translators) {
87 LOGGER.debug("Processing the translator");
88 translateAll(translator);
90 } catch (Exception e) {
91 LOGGER.error("Error while Processing the translator" + e.getMessage());
92 throw new ExceptionInInitializerError("EdgeIngestor could not ingest schema");
95 if (versionJsonFilesMap.isEmpty() || schemaVersions==null ) {
96 throw new ExceptionInInitializerError("EdgeIngestor could not ingest edgerules");
100 public void translateAll(Translator translator) throws ExceptionInInitializerError{
102 Use SchemaVersions from the Translator
104 this.schemaVersions = translator.getSchemaVersions();
105 List<SchemaVersion> schemaVersionList = this.schemaVersions.getVersions();
106 List<String> jsonPayloads = null;
107 JsonIngestor ji = new JsonIngestor();
108 Map<SchemaVersion, List<String>> edgeRulesToIngest = new HashMap<>(); // Obtain a map of schema versions to a list of strings. One List per key
110 // Add to the map the JSON file per version.
111 for (SchemaVersion version : schemaVersionList) {
112 LOGGER.debug("Version being processed" + version);
113 // If the flag is set to not use the local files, obtain the Json from the service.
115 jsonPayloads = translator.getJsonPayload(version); // need to change this - need to receive the json files.
116 } catch (IOException e) {
117 LOGGER.error("Error in retrieving the JSON Payload" + e.getMessage());
118 throw new ExceptionInInitializerError("EdgeIngestor could not ingest schema");
120 if (jsonPayloads == null || jsonPayloads.isEmpty()) {
123 LOGGER.debug("Retrieved json from SchemaService");
124 edgeRulesToIngest.put(version, jsonPayloads);
126 versionJsonFilesMap = ji.ingestContent(edgeRulesToIngest);
128 this.cacheFilterStore = CacheBuilder.newBuilder()
131 new CacheLoader<SchemaFilter, Multimap<String, EdgeRule>>() {
133 public Multimap<String, EdgeRule> load(SchemaFilter key) {
134 return extractRules(key);
139 this.cousinLabelStore = CacheBuilder.newBuilder()
142 new CacheLoader<String, String[]>() {
144 public String[] load(String key) throws Exception {
145 return retrieveCousinLabels(key);
151 // //-----methods for getting rule info-----//
154 * Gets list of all edge rules defined in the latest version's schema
156 * @return Multimap<String, EdgeRule> of node names keys to the EdgeRules associated with those types
157 * where the key takes the form of
158 * {alphabetically first nodetype}|{alphabetically second nodetype}. Map will be empty if
159 * no rules are found.
160 * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link"
161 * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link"
163 * This is alphabetical order to normalize the keys, as sometimes there will be multiple
164 * rules for a pair of node types but the from/to value in the json is flipped for some of them.
165 * @throws EdgeRuleNotFoundException if none found
167 public Multimap<String, EdgeRule> getAllCurrentRules() throws EdgeRuleNotFoundException {
168 return getAllRules(schemaVersions.getDefaultVersion());
172 * Retrieves all the nodes that contain multiple edge labels
174 * A lazy instantiation to retrieve all this info on first call
176 * @return a set containing a list of strings where each string is
177 * concatenated by a pipe (|) character such as aNodeType|bNodeType
179 public Set<String> getMultipleLabelKeys(){
181 if(multipleLabelKeys == null){
182 multipleLabelKeys = new HashSet<>();
184 final Multimap<String, EdgeRule> edges = this.getAllCurrentRules();
185 if(edges == null || edges.isEmpty()){
186 LOGGER.warn("Unable to find any edge rules for the latest version");
187 return multipleLabelKeys;
189 edges.keySet().forEach(key -> {
190 Collection<EdgeRule> rules = edges.get(key);
191 if(rules.size() > 1){
192 multipleLabelKeys.add(key);
195 } catch (EdgeRuleNotFoundException e) {
196 LOGGER.info("For the latest schema version, unable to find any edges with multiple keys");
200 return multipleLabelKeys;
203 * Gets list of all edge rules defined in the given version's schema
205 * @return Multimap<String, EdgeRule> of node names keys to the EdgeRules associated with those types
206 * where the key takes the form of
207 * {alphabetically first nodetype}|{alphabetically second nodetype}. Map will be empty if
208 * no rules are found.
209 * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link"
210 * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link"
212 * This is alphabetical order to normalize the keys, as sometimes there will be multiple
213 * rules for a pair of node types but the from/to value in the json is flipped for some of them.
214 * @throws EdgeRuleNotFoundException if none found
216 public Multimap<String, EdgeRule> getAllRules(SchemaVersion v) throws EdgeRuleNotFoundException {
217 Multimap<String, EdgeRule> found = extractRules(null, v);
218 if (found.isEmpty()) {
219 throw new EdgeRuleNotFoundException("No rules found for version " + v.toString() + ".");
226 * Finds the rules (if any) matching the given query criteria. If none, the returned Multimap
229 * @param q - EdgeRuleQuery with filter criteria set
231 * @return Multimap<String, EdgeRule> of node names keys to the EdgeRules where the key takes the form of
232 * {alphabetically first nodetype}|{alphabetically second nodetype}. Map will be empty if
233 * no rules are found.
234 * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link"
235 * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link"
237 * This is alphabetical order to normalize the keys, as sometimes there will be multiple
238 * rules for a pair of node types but the from/to value in the json is flipped for some of them.
239 * @throws EdgeRuleNotFoundException if none found
242 public Multimap<String, EdgeRule> getRules(EdgeRuleQuery q) throws EdgeRuleNotFoundException {
243 Multimap<String, EdgeRule> found = null;
244 if(q.getVersion().isPresent()){
245 found = extractRules(q.getFilter(), q.getVersion().get());
247 found = extractRules(q.getFilter(), schemaVersions.getDefaultVersion());
249 if (found.isEmpty()) {
250 throw new EdgeRuleNotFoundException("No rules found for " + q.toString());
252 Multimap<String, EdgeRule> copy = ArrayListMultimap.create();
253 found.entries().stream().forEach((entry) -> {
254 EdgeRule rule = new EdgeRule(entry.getValue());
255 if (!q.getFromType().equals(rule.getFrom())) {
256 /* To maintain backwards compatibility with old EdgeRules API,
257 * where the direction of the returned EdgeRule would be
258 * flipped (if necessary) to match the directionality of
260 * ie, If the rule is from=A,to=B,direction=OUT,
261 * if the user asked (A,B) the direction would be OUT,
262 * if they asked (B,A), it would be IN to match.
264 rule.flipDirection();
266 copy.put(entry.getKey(), rule);
276 * Gets the rule satisfying the given filter criteria. If there are more than one
277 * that match, return the default rule. If there is no clear default to return, or
278 * no rules match at all, error.
280 * @param q - EdgeRuleQuery with filter criteria set
281 * @return EdgeRule satisfying given criteria
282 * @throws EdgeRuleNotFoundException if none found that match
283 * @throws AmbiguousRuleChoiceException if multiple match but no way to choice one from them
284 * Specifically, if multiple node type pairs come back (ie bar|foo and asdf|foo,
285 * no way to know which is appropriate over the others),
286 * or if there is a mix of Tree and Cousin edges because again there is no way to
287 * know which is "defaulter" than the other.
288 * The default property only clarifies among multiple cousin edges of the same node pair,
289 * ex: which l-interface|logical-link rule to default to.
291 public EdgeRule getRule(EdgeRuleQuery q) throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
292 Multimap<String, EdgeRule> found = null;
293 if(q.getVersion().isPresent()){
294 found = extractRules(q.getFilter(), q.getVersion().get());
296 found = extractRules(q.getFilter(), schemaVersions.getDefaultVersion());
299 if (found.isEmpty()) {
300 throw new EdgeRuleNotFoundException("No rule found for " + q.toString() + ".");
303 EdgeRule rule = null;
304 if (found.keys().size() == 1) { //only one found, cool we're done
305 for (Entry<String, EdgeRule> e : found.entries()) {
309 rule = getDefaultRule(found);
312 if (rule == null) { //should never get here though
313 throw new EdgeRuleNotFoundException("No rule found for " + q.toString() + ".");
315 rule = new EdgeRule(rule);
316 if (!q.getFromType().equals(rule.getFrom())) {
317 /* To maintain backwards compatibility with old EdgeRules API,
318 * where the direction of the returned EdgeRule would be
319 * flipped (if necessary) to match the directionality of
321 * ie, If the rule is from=A,to=B,direction=OUT,
322 * if the user asked (A,B) the direction would be OUT,
323 * if they asked (B,A), it would be IN to match.
325 rule.flipDirection();
331 private EdgeRule getDefaultRule(Multimap<String, EdgeRule> found) throws AmbiguousRuleChoiceException {
332 if (found.keySet().size() > 1) { //ie multiple node pairs (a|c and b|c not just all a|c) case
333 StringBuilder sb = new StringBuilder();
334 for (String k : found.keySet()) {
335 sb.append(k).append(" ");
337 throw new AmbiguousRuleChoiceException("No way to select single rule from these pairs: " + sb.toString() + ".");
340 int defaultCount = 0;
341 EdgeRule defRule = null;
342 for (Entry<String, EdgeRule> e : found.entries()) {
343 EdgeRule rule = e.getValue();
344 if (rule.isDefault()) {
349 if (defaultCount > 1) {
350 throw new AmbiguousRuleChoiceException("Multiple defaults found.");
351 } else if (defaultCount == 0) {
352 throw new AmbiguousRuleChoiceException("No default found.");
358 * Checks if there exists any rule that satisfies the given filter criteria.
360 * @param q - EdgeRuleQuery with filter criteria set
363 public boolean hasRule(EdgeRuleQuery q) {
364 if(q.getVersion().isPresent()){
365 return !extractRules(q.getFilter(), q.getVersion().get()).isEmpty();
367 return !extractRules(q.getFilter(), schemaVersions.getDefaultVersion()).isEmpty();
372 * Gets all cousin rules for the given node type in the latest schema version.
375 * @return Multimap<String, EdgeRule> of node names keys to the EdgeRules where the key takes the form of
376 * {alphabetically first nodetype}|{alphabetically second nodetype}. Map will be empty if
377 * no rules are found.
378 * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link"
379 * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link"
381 * This is alphabetical order to normalize the keys, as sometimes there will be multiple
382 * rules for a pair of node types but the from/to value in the json is flipped for some of them.
384 public Multimap<String, EdgeRule> getCousinRules(String nodeType) {
385 return getCousinRules(nodeType, schemaVersions.getDefaultVersion()); //default to latest
388 public String[] retrieveCousinLabels(String nodeType){
390 Multimap<String, EdgeRule> cousinRules = getCousinRules(nodeType);
391 String[] cousinLabels = new String[cousinRules.size()];
393 return cousinRules.entries()
395 .map(entry -> entry.getValue().getLabel())
396 .collect(Collectors.toList())
397 .toArray(cousinLabels);
400 public String[] retrieveCachedCousinLabels(String nodeType) throws ExecutionException {
401 return cousinLabelStore.get(nodeType);
405 * Gets all cousin rules for the given node type in the given schema version.
408 * @param v - the version of the edge rules to query
409 * @return Multimap<String, EdgeRule> of node names keys to the EdgeRules where the key takes the form of
410 * {alphabetically first nodetype}|{alphabetically second nodetype}. Map will be empty if
411 * no rules are found.
412 * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link"
413 * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link"
415 * This is alphabetical order to normalize the keys, as sometimes there will be multiple
416 * rules for a pair of node types but the from/to value in the json is flipped for some of them.
418 public Multimap<String, EdgeRule> getCousinRules(String nodeType, SchemaVersion v) {
419 return extractRules(new EdgeRuleQuery.Builder(nodeType).edgeType(EdgeType.COUSIN).build().getFilter(), v);
423 * Returns if the given node type has any cousin relationships in the current version.
427 public boolean hasCousinRule(String nodeType) {
428 return hasCousinRule(nodeType, schemaVersions.getDefaultVersion());
432 * Returns if the given node type has any cousin relationships in the given version.
436 public boolean hasCousinRule(String nodeType, SchemaVersion v) {
437 return !getCousinRules(nodeType, v).isEmpty();
441 * Gets all rules where "{given nodeType} contains {otherType}" in the latest schema version.
443 * @param nodeType - node type that is the container in the returned relationships
444 * @return Multimap<String, EdgeRule> of node names keys to the EdgeRules where the key takes the form of
445 * {alphabetically first nodetype}|{alphabetically second nodetype}. Map will be empty if
446 * no rules are found.
447 * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link"
448 * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link"
450 * This is alphabetical order to normalize the keys, as sometimes there will be multiple
451 * rules for a pair of node types but the from/to value in the json is flipped for some of them.
453 public Multimap<String, EdgeRule> getChildRules(String nodeType) {
454 return getChildRules(nodeType, schemaVersions.getDefaultVersion());
458 * Gets all rules where "{given nodeType} contains {otherType}" in the given schema version.
460 * @param nodeType - node type that is the container in the returned relationships
461 * @return Multimap<String, EdgeRule> of node names keys to the EdgeRules where the key takes the form of
462 * {alphabetically first nodetype}|{alphabetically second nodetype}. Map will be empty if
463 * no rules are found.
464 * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link"
465 * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link"
467 * This is alphabetical order to normalize the keys, as sometimes there will be multiple
468 * rules for a pair of node types but the from/to value in the json is flipped for some of them.
470 public Multimap<String, EdgeRule> getChildRules(String nodeType, SchemaVersion v) {
471 Filter from = assembleFilterSegments(where(EdgeField.FROM.toString()).is(nodeType), getSameDirectionContainmentCriteria());
472 Filter to = assembleFilterSegments(where(EdgeField.TO.toString()).is(nodeType), getOppositeDirectionContainmentCriteria());
473 Filter total = from.or(to);
475 return extractRules(total, v);
479 * Returns if the given node type has any child relationships (ie it contains another node type) in the current version.
483 public boolean hasChildRule(String nodeType) {
484 return hasChildRule(nodeType, schemaVersions.getDefaultVersion());
488 * Returns if the given node type has any child relationships (ie it contains another node type) in the given version.
492 public boolean hasChildRule(String nodeType, SchemaVersion v) {
493 return !getChildRules(nodeType, v).isEmpty();
497 * Gets all rules where "{given nodeType} is contained by {otherType}" in the latest schema version.
499 * @param nodeType - node type that is the containee in the returned relationships
500 * @return Multimap<String, EdgeRule> of node names keys to the EdgeRules where the key takes the form of
501 * {alphabetically first nodetype}|{alphabetically second nodetype}. Map will be empty if
502 * no rules are found.
503 * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link"
504 * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link"
506 * This is alphabetical order to normalize the keys, as sometimes there will be multiple
507 * rules for a pair of node types but the from/to value in the json is flipped for some of them.
509 public Multimap<String, EdgeRule> getParentRules(String nodeType) {
510 return getParentRules(nodeType, schemaVersions.getDefaultVersion());
514 * Gets all rules where "{given nodeType} is contained by {otherType}" in the given schema version.
516 * @param nodeType - node type that is the containee in the returned relationships
517 * @return Multimap<String, EdgeRule> of node names keys to the EdgeRules where the key takes the form of
518 * {alphabetically first nodetype}|{alphabetically second nodetype}. Map will be empty if
519 * no rules are found.
520 * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link"
521 * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link"
523 * This is alphabetical order to normalize the keys, as sometimes there will be multiple
524 * rules for a pair of node types but the from/to value in the json is flipped for some of them.
526 public Multimap<String, EdgeRule> getParentRules(String nodeType, SchemaVersion v) {
527 Filter from = assembleFilterSegments(where(EdgeField.FROM.toString()).is(nodeType), getOppositeDirectionContainmentCriteria());
528 Filter to = assembleFilterSegments(where(EdgeField.TO.toString()).is(nodeType), getSameDirectionContainmentCriteria());
529 Filter total = from.or(to);
531 return extractRules(total, v);
535 * Returns if the given node type has any parent relationships (ie it is contained by another node type) in the current version.
539 public boolean hasParentRule(String nodeType) {
540 return hasParentRule(nodeType, schemaVersions.getDefaultVersion());
544 * Returns if the given node type has any parent relationships (ie it is contained by another node type) in the given version.
548 public boolean hasParentRule(String nodeType, SchemaVersion v) {
549 return !getParentRules(nodeType, v).isEmpty();
553 * Applies the given filter to the DocumentContext(s) for the given version to extract
554 * edge rules, and converts this extracted information into the Multimap form
556 * @param filter - JsonPath filter to read the DocumentContexts with. May be null
557 * to denote no filter, ie get all.
558 * @param v - The schema version to extract from
559 * @return Multimap<String, EdgeRule> of node names keys to the EdgeRules where the key takes the form of
560 * {alphabetically first nodetype}|{alphabetically second nodetype}. Map will be empty if
561 * no rules are found.
562 * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link"
563 * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link"
565 * This is alphabetical order to normalize the keys, as sometimes there will be multiple
566 * rules for a pair of node types but the from/to value in the json is flipped for some of them.
568 private Multimap<String, EdgeRule> extractRules(Filter filter, SchemaVersion v) {
569 SchemaFilter schemaFilter = new SchemaFilter(filter, v);
571 return cacheFilterStore.get(schemaFilter);
572 } catch (ExecutionException e) {
573 LOGGER.info("Encountered exception during the retrieval of the rules");
574 return ArrayListMultimap.create();
578 public Multimap<String, EdgeRule> extractRules(SchemaFilter schemaFilter){
579 List<Map<String, String>> foundRules = new ArrayList<>();
580 List<DocumentContext> docs = versionJsonFilesMap.get(schemaFilter.getSchemaVersion());
582 for (DocumentContext doc : docs) {
583 if (schemaFilter.getFilter() == null) {
584 foundRules.addAll(doc.read(READ_ALL_START));
586 foundRules.addAll(doc.read(READ_START, Filter.parse(schemaFilter.getFilter())));
591 return convertToEdgeRules(foundRules);
594 //-----filter building helpers-----//
596 * ANDs together the given start criteria with each criteria in the pieces list, and
597 * then ORs together these segments into one filter.
599 * JsonPath doesn't have an OR method on Criteria, only on Filters, so assembling
600 * a complete filter requires this sort of roundabout construction.
602 * @param start - Criteria of the form where(from/to).is(nodeType)
603 * (ie the start of any A&AI edge rule query)
604 * @param pieces - Other Criteria to be applied
605 * @return Filter constructed from the given Criteria
607 private Filter assembleFilterSegments(Criteria start, List<Criteria> pieces) {
608 List<Filter> segments = new ArrayList<>();
609 for (Criteria c : pieces) {
610 segments.add(filter(start).and(c));
612 Filter assembled = segments.remove(0);
613 for (Filter f : segments) {
614 assembled = assembled.or(f);
620 * Builds the sub-Criteria for a containment edge rule query where the direction
621 * and containment fields must match.
623 * Used for getChildRules() where the container node type is in the "from" position and
624 * for getParentRules() where the containee type is in the "to" position.
626 * @return List<Criteria> covering property permutations defined with either notation or explicit direction
628 private List<Criteria> getSameDirectionContainmentCriteria() {
629 List<Criteria> crits = new ArrayList<>();
631 crits.add(where(EdgeField.CONTAINS.toString()).is(DirectionNotation.DIRECTION.toString()));
633 crits.add(where(EdgeField.DIRECTION.toString()).is(Direction.OUT.toString())
634 .and(EdgeField.CONTAINS.toString()).is(Direction.OUT.toString()));
636 crits.add(where(EdgeField.DIRECTION.toString()).is(Direction.IN.toString())
637 .and(EdgeField.CONTAINS.toString()).is(Direction.IN.toString()));
643 * Builds the sub-Criteria for a containment edge rule query where the direction
644 * and containment fields must not match.
646 * Used for getChildRules() where the container node type is in the "to" position and
647 * for getParentRules() where the containee type is in the "from" position.
649 * @return List<Criteria> covering property permutations defined with either notation or explicit direction
651 private List<Criteria> getOppositeDirectionContainmentCriteria() {
652 List<Criteria> crits = new ArrayList<>();
654 crits.add(where(EdgeField.CONTAINS.toString()).is(DirectionNotation.OPPOSITE.toString()));
656 crits.add(where(EdgeField.DIRECTION.toString()).is(Direction.OUT.toString())
657 .and(EdgeField.CONTAINS.toString()).is(Direction.IN.toString()));
659 crits.add(where(EdgeField.DIRECTION.toString()).is(Direction.IN.toString())
660 .and(EdgeField.CONTAINS.toString()).is(Direction.OUT.toString()));
665 //-----rule packaging helpers-----//
667 * Converts the raw output from reading the json file to the Multimap<String key, EdgeRule> format
669 * @param allFound - raw edge rule output read from json file(s)
670 * (could be empty if none found)
671 * @return Multimap<String, EdgeRule> of node names keys to the EdgeRules where the key takes the form of
672 * {alphabetically first nodetype}|{alphabetically second nodetype}. Will be empty if input
674 * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link"
675 * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link"
677 * This is alphabetical order to normalize the keys, as sometimes there will be multiple
678 * rules for a pair of node types but the from/to value in the json is flipped for some of them.
680 private Multimap<String, EdgeRule> convertToEdgeRules(List<Map<String, String>> allFound) {
681 Multimap<String, EdgeRule> rules = ArrayListMultimap.create();
683 TypeAlphabetizer alpher = new TypeAlphabetizer();
685 for (Map<String, String> raw : allFound) {
686 EdgeRule converted = new EdgeRule(raw);
687 if (converted.getFrom().equals(converted.getTo())) {
688 /* the way the code worked in the past was with outs and
689 * when we switched it to in the same-node-type to
690 * same-node-type parent child edges were failing because all
691 * of the calling code would pass the parent as the left argument,
692 * so it was either in that method swap the parent/child,
693 * flip the edge rule or make all callers swap. the last seemed
694 * like a bad idea. and felt like the edge flip was the better
695 * of the remaining 2 */
696 converted.flipDirection();
698 String alphabetizedKey = alpher.buildAlphabetizedKey(raw.get(EdgeField.FROM.toString()), raw.get(EdgeField.TO.toString()));
699 rules.put(alphabetizedKey, converted);