diff options
Diffstat (limited to 'aai-schema-ingest/src/main/java')
15 files changed, 1635 insertions, 0 deletions
diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/edges/EdgeIngestor.java b/aai-schema-ingest/src/main/java/org/onap/aai/edges/EdgeIngestor.java new file mode 100644 index 00000000..aa90a300 --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/edges/EdgeIngestor.java @@ -0,0 +1,581 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * 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.aai.edges; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.tinkerpop.gremlin.structure.Direction; +import org.onap.aai.edges.enums.AAIDirection; +import org.onap.aai.edges.enums.DirectionNotation; +import org.onap.aai.edges.enums.EdgeField; +import org.onap.aai.edges.enums.EdgeType; +import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException; +import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; +import org.onap.aai.setup.ConfigTranslator; +import org.onap.aai.setup.Version; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; +import com.jayway.jsonpath.Criteria; +import com.jayway.jsonpath.DocumentContext; +import com.jayway.jsonpath.Filter; +import static com.jayway.jsonpath.Filter.filter; +import com.jayway.jsonpath.JsonPath; +import static com.jayway.jsonpath.Criteria.where; + +@Component +/** + * EdgeIngestor - ingests A&AI edge rule schema files per given config, serves that edge rule + * information, including allowing various filters to extract particular rules. + */ +public class EdgeIngestor { + private Map<Version, List<DocumentContext>> versionJsonFilesMap = new EnumMap<>(Version.class); + private static final String READ_START = "$.rules.[?]"; + private static final String READ_ALL_START = "$.rules.*"; + + //-----ingest-----// + @Autowired + /** + * Instantiates the EdgeIngestor bean. + * + * @param translator - ConfigTranslator autowired in by Spring framework which + * contains the configuration information needed to ingest the desired files. + */ + public EdgeIngestor(ConfigTranslator translator) { + Map<Version, List<String>> filesToIngest = translator.getEdgeFiles(); + + for (Entry<Version, List<String>> verFile : filesToIngest.entrySet()) { + Version v = verFile.getKey(); + List<String> files = verFile.getValue(); + + List<DocumentContext> docs = new ArrayList<>(); + + for (String rulesFilename : files) { + String fileContents = readInJsonFile(rulesFilename); + docs.add(JsonPath.parse(fileContents)); + } + versionJsonFilesMap.put(v, docs); + } + } + + /** + * Reads the json file at the given filename into an in-memory String. + * + * @param rulesFilename - json file to be read (must include path to the file) + * @return String json contents of the given file + */ + private String readInJsonFile(String rulesFilename) { + StringBuilder sb = new StringBuilder(); + try(BufferedReader br = new BufferedReader(new FileReader(rulesFilename))) { + String line; + while ((line = br.readLine()) != null) { + sb.append(line); + } + } catch (IOException e) { + throw new ExceptionInInitializerError(e); + } + return sb.toString(); + } + + //-----methods for getting rule info-----// + + /** + * Gets list of all edge rules defined in the latest version's schema + * + * @return Multimap<String, EdgeRule> of node names keys to the EdgeRules associated with those types + * where the key takes the form of + * {alphabetically first nodetype}|{alphabetically second nodetype}. Map will be empty if + * no rules are found. + * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link" + * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link" + * + * This is alphabetical order to normalize the keys, as sometimes there will be multiple + * rules for a pair of node types but the from/to value in the json is flipped for some of them. + * @throws EdgeRuleNotFoundException if none found + */ + public Multimap<String, EdgeRule> getAllCurrentRules() throws EdgeRuleNotFoundException { + return getAllRules(Version.getLatest()); + } + + /** + * Gets list of all edge rules defined in the given version's schema + * + * @return Multimap<String, EdgeRule> of node names keys to the EdgeRules associated with those types + * where the key takes the form of + * {alphabetically first nodetype}|{alphabetically second nodetype}. Map will be empty if + * no rules are found. + * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link" + * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link" + * + * This is alphabetical order to normalize the keys, as sometimes there will be multiple + * rules for a pair of node types but the from/to value in the json is flipped for some of them. + * @throws EdgeRuleNotFoundException if none found + */ + public Multimap<String, EdgeRule> getAllRules(Version v) throws EdgeRuleNotFoundException { + Multimap<String, EdgeRule> found = extractRules(null, v); + if (found.isEmpty()) { + throw new EdgeRuleNotFoundException("No rules found for version " + v.toString() + "."); + } else { + return found; + } + } + + /** + * Finds the rules (if any) matching the given query criteria. If none, the returned Multimap + * will be empty. + * + * @param q - EdgeRuleQuery with filter criteria set + * + * @return Multimap<String, EdgeRule> of node names keys to the EdgeRules where the key takes the form of + * {alphabetically first nodetype}|{alphabetically second nodetype}. Map will be empty if + * no rules are found. + * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link" + * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link" + * + * This is alphabetical order to normalize the keys, as sometimes there will be multiple + * rules for a pair of node types but the from/to value in the json is flipped for some of them. + * @throws EdgeRuleNotFoundException if none found + */ + public Multimap<String, EdgeRule> getRules(EdgeRuleQuery q) throws EdgeRuleNotFoundException { + Multimap<String, EdgeRule> found = extractRules(q.getFilter(), q.getVersion()); + if (found.isEmpty()) { + throw new EdgeRuleNotFoundException("No rules found for " + q.toString()); + } else { + return found; + } + } + + /** + * Gets the rule satisfying the given filter criteria. If there are more than one + * that match, return the default rule. If there is no clear default to return, or + * no rules match at all, error. + * + * @param q - EdgeRuleQuery with filter criteria set + * @return EdgeRule satisfying given criteria + * @throws EdgeRuleNotFoundException if none found that match + * @throws AmbiguousRuleChoiceException if multiple match but no way to choice one from them + * Specifically, if multiple node type pairs come back (ie bar|foo and asdf|foo, + * no way to know which is appropriate over the others), + * or if there is a mix of Tree and Cousin edges because again there is no way to + * know which is "defaulter" than the other. + * The default property only clarifies among multiple cousin edges of the same node pair, + * ex: which l-interface|logical-link rule to default to. + */ + public EdgeRule getRule(EdgeRuleQuery q) throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException { + Multimap<String, EdgeRule> found = extractRules(q.getFilter(), q.getVersion()); + + if (found.isEmpty()) { + throw new EdgeRuleNotFoundException("No rule found for " + q.toString() + "."); + } + + EdgeRule rule = null; + if (found.keys().size() == 1) { //only one found, cool we're done + for (Entry<String, EdgeRule> e : found.entries()) { + rule = e.getValue(); + } + } else { + rule = getDefaultRule(found); + } + + if (rule == null) { //should never get here though + throw new EdgeRuleNotFoundException("No rule found for " + q.toString() + "."); + } else { + return rule; + } + } + + private EdgeRule getDefaultRule(Multimap<String, EdgeRule> found) throws AmbiguousRuleChoiceException { + if (found.keySet().size() > 1) { //ie multiple node pairs (a|c and b|c not just all a|c) case + StringBuilder sb = new StringBuilder(); + for (String k : found.keySet()) { + sb.append(k).append(" "); + } + throw new AmbiguousRuleChoiceException("No way to select single rule from these pairs: " + sb.toString() + "."); + } + + int defaultCount = 0; + EdgeRule defRule = null; + for (Entry<String, EdgeRule> e : found.entries()) { + EdgeRule rule = e.getValue(); + if (rule.isDefault()) { + defaultCount++; + defRule = rule; + } + } + if (defaultCount > 1) { + throw new AmbiguousRuleChoiceException("Multiple defaults found."); + } else if (defaultCount == 0) { + throw new AmbiguousRuleChoiceException("No default found."); + } + + return defRule; + } + + /** + * Checks if there exists any rule that satisfies the given filter criteria. + * + * @param q - EdgeRuleQuery with filter criteria set + * @return boolean + */ + public boolean hasRule(EdgeRuleQuery q) { + return !extractRules(q.getFilter(), q.getVersion()).isEmpty(); + } + + /** + * Gets all cousin rules for the given node type in the latest schema version. + * + * @param nodeType + * @return Multimap<String, EdgeRule> of node names keys to the EdgeRules where the key takes the form of + * {alphabetically first nodetype}|{alphabetically second nodetype}. Map will be empty if + * no rules are found. + * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link" + * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link" + * + * This is alphabetical order to normalize the keys, as sometimes there will be multiple + * rules for a pair of node types but the from/to value in the json is flipped for some of them. + */ + public Multimap<String, EdgeRule> getCousinRules(String nodeType) { + return getCousinRules(nodeType, Version.getLatest()); //default to latest + } + + /** + * Gets all cousin rules for the given node type in the given schema version. + * + * @param nodeType + * @param v - the version of the edge rules to query + * @return Multimap<String, EdgeRule> of node names keys to the EdgeRules where the key takes the form of + * {alphabetically first nodetype}|{alphabetically second nodetype}. Map will be empty if + * no rules are found. + * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link" + * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link" + * + * This is alphabetical order to normalize the keys, as sometimes there will be multiple + * rules for a pair of node types but the from/to value in the json is flipped for some of them. + */ + public Multimap<String, EdgeRule> getCousinRules(String nodeType, Version v) { + return extractRules(new EdgeRuleQuery.Builder(nodeType).edgeType(EdgeType.COUSIN).build().getFilter(), v); + } + + /** + * Returns if the given node type has any cousin relationships in the current version. + * @param nodeType + * @return boolean + */ + public boolean hasCousinRule(String nodeType) { + return hasCousinRule(nodeType, Version.getLatest()); + } + + /** + * Returns if the given node type has any cousin relationships in the given version. + * @param nodeType + * @return boolean + */ + public boolean hasCousinRule(String nodeType, Version v) { + return !getCousinRules(nodeType, v).isEmpty(); + } + + /** + * Gets all rules where "{given nodeType} contains {otherType}" in the latest schema version. + * + * @param nodeType - node type that is the container in the returned relationships + * @return Multimap<String, EdgeRule> of node names keys to the EdgeRules where the key takes the form of + * {alphabetically first nodetype}|{alphabetically second nodetype}. Map will be empty if + * no rules are found. + * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link" + * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link" + * + * This is alphabetical order to normalize the keys, as sometimes there will be multiple + * rules for a pair of node types but the from/to value in the json is flipped for some of them. + */ + public Multimap<String, EdgeRule> getChildRules(String nodeType) { + return getChildRules(nodeType, Version.getLatest()); + } + + /** + * Gets all rules where "{given nodeType} contains {otherType}" in the given schema version. + * + * @param nodeType - node type that is the container in the returned relationships + * @return Multimap<String, EdgeRule> of node names keys to the EdgeRules where the key takes the form of + * {alphabetically first nodetype}|{alphabetically second nodetype}. Map will be empty if + * no rules are found. + * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link" + * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link" + * + * This is alphabetical order to normalize the keys, as sometimes there will be multiple + * rules for a pair of node types but the from/to value in the json is flipped for some of them. + */ + public Multimap<String, EdgeRule> getChildRules(String nodeType, Version v) { + Filter from = assembleFilterSegments(where(EdgeField.FROM.toString()).is(nodeType), getSameDirectionContainmentCriteria()); + Filter to = assembleFilterSegments(where(EdgeField.TO.toString()).is(nodeType), getOppositeDirectionContainmentCriteria()); + Filter total = from.or(to); + + return extractRules(total, v); + } + + /** + * Returns if the given node type has any child relationships (ie it contains another node type) in the current version. + * @param nodeType + * @return boolean + */ + public boolean hasChildRule(String nodeType) { + return hasChildRule(nodeType, Version.getLatest()); + } + + /** + * Returns if the given node type has any child relationships (ie it contains another node type) in the given version. + * @param nodeType + * @return boolean + */ + public boolean hasChildRule(String nodeType, Version v) { + return !getChildRules(nodeType, v).isEmpty(); + } + + /** + * Gets all rules where "{given nodeType} is contained by {otherType}" in the latest schema version. + * + * @param nodeType - node type that is the containee in the returned relationships + * @return Multimap<String, EdgeRule> of node names keys to the EdgeRules where the key takes the form of + * {alphabetically first nodetype}|{alphabetically second nodetype}. Map will be empty if + * no rules are found. + * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link" + * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link" + * + * This is alphabetical order to normalize the keys, as sometimes there will be multiple + * rules for a pair of node types but the from/to value in the json is flipped for some of them. + */ + public Multimap<String, EdgeRule> getParentRules(String nodeType) { + return getParentRules(nodeType, Version.getLatest()); + } + + /** + * Gets all rules where "{given nodeType} is contained by {otherType}" in the given schema version. + * + * @param nodeType - node type that is the containee in the returned relationships + * @return Multimap<String, EdgeRule> of node names keys to the EdgeRules where the key takes the form of + * {alphabetically first nodetype}|{alphabetically second nodetype}. Map will be empty if + * no rules are found. + * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link" + * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link" + * + * This is alphabetical order to normalize the keys, as sometimes there will be multiple + * rules for a pair of node types but the from/to value in the json is flipped for some of them. + */ + public Multimap<String, EdgeRule> getParentRules(String nodeType, Version v) { + Filter from = assembleFilterSegments(where(EdgeField.FROM.toString()).is(nodeType), getOppositeDirectionContainmentCriteria()); + Filter to = assembleFilterSegments(where(EdgeField.TO.toString()).is(nodeType), getSameDirectionContainmentCriteria()); + Filter total = from.or(to); + + return extractRules(total, v); + } + + /** + * Returns if the given node type has any parent relationships (ie it is contained by another node type) in the current version. + * @param nodeType + * @return boolean + */ + public boolean hasParentRule(String nodeType) { + return hasParentRule(nodeType, Version.getLatest()); + } + + /** + * Returns if the given node type has any parent relationships (ie it is contained by another node type) in the given version. + * @param nodeType + * @return boolean + */ + public boolean hasParentRule(String nodeType, Version v) { + return !getParentRules(nodeType, v).isEmpty(); + } + + /** + * Applies the given filter to the DocumentContext(s) for the given version to extract + * edge rules, and converts this extracted information into the Multimap form + * + * @param filter - JsonPath filter to read the DocumentContexts with. May be null + * to denote no filter, ie get all. + * @param v - The schema version to extract from + * @return Multimap<String, EdgeRule> of node names keys to the EdgeRules where the key takes the form of + * {alphabetically first nodetype}|{alphabetically second nodetype}. Map will be empty if + * no rules are found. + * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link" + * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link" + * + * This is alphabetical order to normalize the keys, as sometimes there will be multiple + * rules for a pair of node types but the from/to value in the json is flipped for some of them. + */ + private Multimap<String, EdgeRule> extractRules(Filter filter, Version v) { + List<Map<String, String>> foundRules = new ArrayList<>(); + List<DocumentContext> docs = versionJsonFilesMap.get(v); + if (docs != null) { + for (DocumentContext doc : docs) { + if (filter == null) { + foundRules.addAll(doc.read(READ_ALL_START)); + } else { + foundRules.addAll(doc.read(READ_START, filter)); + } + } + } + + return convertToEdgeRules(foundRules); + } + + //-----filter building helpers-----// + /** + * ANDs together the given start criteria with each criteria in the pieces list, and + * then ORs together these segments into one filter. + * + * JsonPath doesn't have an OR method on Criteria, only on Filters, so assembling + * a complete filter requires this sort of roundabout construction. + * + * @param start - Criteria of the form where(from/to).is(nodeType) + * (ie the start of any A&AI edge rule query) + * @param pieces - Other Criteria to be applied + * @return Filter constructed from the given Criteria + */ + private Filter assembleFilterSegments(Criteria start, List<Criteria> pieces) { + List<Filter> segments = new ArrayList<>(); + for (Criteria c : pieces) { + segments.add(filter(start).and(c)); + } + Filter assembled = segments.remove(0); + for (Filter f : segments) { + assembled = assembled.or(f); + } + return assembled; + } + + /** + * Builds the sub-Criteria for a containment edge rule query where the direction + * and containment fields must match. + * + * Used for getChildRules() where the container node type is in the "from" position and + * for getParentRules() where the containee type is in the "to" position. + * + * @return List<Criteria> covering property permutations defined with either notation or explicit direction + */ + private List<Criteria> getSameDirectionContainmentCriteria() { + List<Criteria> crits = new ArrayList<>(); + + crits.add(where(EdgeField.CONTAINS.toString()).is(DirectionNotation.DIRECTION.toString())); + + crits.add(where(EdgeField.DIRECTION.toString()).is(Direction.OUT.toString()) + .and(EdgeField.CONTAINS.toString()).is(Direction.OUT.toString())); + + crits.add(where(EdgeField.DIRECTION.toString()).is(Direction.IN.toString()) + .and(EdgeField.CONTAINS.toString()).is(Direction.IN.toString())); + + return crits; + } + + /** + * Builds the sub-Criteria for a containment edge rule query where the direction + * and containment fields must not match. + * + * Used for getChildRules() where the container node type is in the "to" position and + * for getParentRules() where the containee type is in the "from" position. + * + * @return List<Criteria> covering property permutations defined with either notation or explicit direction + */ + private List<Criteria> getOppositeDirectionContainmentCriteria() { + List<Criteria> crits = new ArrayList<>(); + + crits.add(where(EdgeField.CONTAINS.toString()).is(DirectionNotation.OPPOSITE.toString())); + + crits.add(where(EdgeField.DIRECTION.toString()).is(Direction.OUT.toString()) + .and(EdgeField.CONTAINS.toString()).is(Direction.IN.toString())); + + crits.add(where(EdgeField.DIRECTION.toString()).is(Direction.IN.toString()) + .and(EdgeField.CONTAINS.toString()).is(Direction.OUT.toString())); + + return crits; + } + + //-----rule packaging helpers-----// + /** + * Converts the raw output from reading the json file to the Multimap<String key, EdgeRule> format + * + * @param List<Map<String, String>> allFound - raw edge rule output read from json file(s) + * (could be empty if none found) + * @return Multimap<String, EdgeRule> of node names keys to the EdgeRules where the key takes the form of + * {alphabetically first nodetype}|{alphabetically second nodetype}. Will be empty if input + * was empty. + * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link" + * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link" + * + * This is alphabetical order to normalize the keys, as sometimes there will be multiple + * rules for a pair of node types but the from/to value in the json is flipped for some of them. + */ + private Multimap<String, EdgeRule> convertToEdgeRules(List<Map<String, String>> allFound) { + Multimap<String, EdgeRule> rules = ArrayListMultimap.create(); + + if (!allFound.isEmpty()) { + for (Map<String, String> raw : allFound) { + EdgeRule converted = new EdgeRule(raw); + String alphabetizedKey = buildAlphabetizedKey(raw.get(EdgeField.FROM.toString()), raw.get(EdgeField.TO.toString())); + rules.put(alphabetizedKey, converted); + } + } + + return rules; + } + + /** + * Builds the multimap key for the rules, where nodetypes are alphabetically sorted + * (ignoring dashes). + * + * @param nodeA - first nodetype + * @param nodeB - second nodetype + * @return {alphabetically first nodetype}|{alphabetically second nodetype} + * ex: buildAlphabetizedKey("l-interface", "logical-link") -> "l-interface|logical-link" + * buildAlphabetizedKey("logical-link", "l-interface") -> "l-interface|logical-link" + * + * This is alphabetical order to normalize the keys, as sometimes there will be multiple + * rules for a pair of node types but the from/to value in the json is flipped for some of them. + */ + private String buildAlphabetizedKey(String nodeA, String nodeB) { + //normalize + String normalizedNodeA = nodeA.replace("-", ""); + String normalizedNodeB = nodeB.replace("-", ""); + int cmp = normalizedNodeA.compareTo(normalizedNodeB); + + StringBuilder sb = new StringBuilder(); + if (cmp <= 0) { + sb.append(nodeA); + sb.append("|"); + sb.append(nodeB); + } else { + sb.append(nodeB); + sb.append("|"); + sb.append(nodeA); + } + return sb.toString(); + } +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/edges/EdgeRule.java b/aai-schema-ingest/src/main/java/org/onap/aai/edges/EdgeRule.java new file mode 100644 index 00000000..f859aae6 --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/edges/EdgeRule.java @@ -0,0 +1,191 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * 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.aai.edges; + +import org.apache.tinkerpop.gremlin.structure.Direction; +import org.onap.aai.edges.enums.AAIDirection; +import org.onap.aai.edges.enums.DirectionNotation; +import org.onap.aai.edges.enums.EdgeField; +import org.onap.aai.edges.enums.EdgeProperty; +import org.onap.aai.edges.enums.MultiplicityRule; + +import java.util.EnumMap; +import java.util.Map; + +/** + * Container for A&AI edge rule information + */ +public class EdgeRule { + private String from; + private String to; + private String label; + private Direction direction; + private MultiplicityRule multiplicityRule; + private Map<EdgeProperty, AAIDirection> edgeFields; + private boolean isDefaultEdge; + private String description; + + /** + * Instantiates a new edge rule. + * + * @param fieldVals - Map<String, String> where first string is + * an EdgeField value and second string is the + * value of that field + */ + public EdgeRule(Map<String, String> fieldVals) { + edgeFields = new EnumMap<>(EdgeProperty.class); + + from = fieldVals.get(EdgeField.FROM.toString()); + to = fieldVals.get(EdgeField.TO.toString()); + label = fieldVals.get(EdgeField.LABEL.toString()); + direction = Direction.valueOf(fieldVals.get(EdgeField.DIRECTION.toString())); + multiplicityRule = MultiplicityRule.getValue(fieldVals.get(EdgeField.MULTIPLICITY.toString())); + + for (EdgeProperty prop : EdgeProperty.values()) { + String rawVal = fieldVals.get(prop.toString()); + edgeFields.put(prop, convertNotation(direction, rawVal)); + } + + isDefaultEdge = Boolean.valueOf(fieldVals.get(EdgeField.DEFAULT.toString())); + + description = fieldVals.get(EdgeField.DESCRIPTION.toString()); + if (description == null) { //bc description is optional and not in v12 and earlier + description = ""; + } + } + + /** + * Converts whatever string was in the json for an edge property value into + * the appropriate AAIDirection + * + * @param Direction dir - the edge direction + * @param String rawVal - property value from the json, may be + * IN, OUT, BOTH, NONE, ${direction}, or !${direction} + * @return AAIDirection - IN/OUT/BOTH/NONE if that's the rawVal, or + * translates the direction notation into the correct IN/OUT + */ + private AAIDirection convertNotation(Direction dir, String rawVal) { + if (AAIDirection.NONE.toString().equals(rawVal)) { + return AAIDirection.NONE; + } else if (AAIDirection.BOTH.toString().equals(rawVal)) { + return AAIDirection.BOTH; + } else if (AAIDirection.OUT.toString().equals(rawVal)) { + return AAIDirection.OUT; + } else if (AAIDirection.IN.toString().equals(rawVal)) { + return AAIDirection.IN; + } + + DirectionNotation rawDN = DirectionNotation.getValue(rawVal); + if (DirectionNotation.DIRECTION.equals(rawDN)) { + return AAIDirection.getValue(dir); + } else { + return AAIDirection.getValue(dir.opposite()); + } + } + + /** + * Gets the name of the node type in the "from" field + * @return String nodetype + */ + public String getFrom() { + return from; + } + + /** + * Gets the name of the node type in the "to" field + * @return String nodetype + */ + public String getTo() { + return to; + } + + /** + * Gets the edge label + * + * @return String label + */ + public String getLabel() { + return label; + } + + /** + * Gets the multiplicity rule. + * + * @return MultiplicityRule + */ + public MultiplicityRule getMultiplicityRule() { + return multiplicityRule; + } + + /** + * Gets the edge direction + * + * @return Direction + */ + public Direction getDirection() { + return direction; + } + + /** + * Gets the value of contains-other-v + * + * @return the value of contains-other-v + */ + public String getContains() { + return edgeFields.get(EdgeProperty.CONTAINS).toString(); + } + + /** + * Gets the value of delete-other-v + * + * @return the value of delete-other-v + */ + public String getDeleteOtherV() { + return edgeFields.get(EdgeProperty.DELETE_OTHER_V).toString(); + } + + /** + * Gets the value of the prevent-delete property + * + * @return String prevent-delete property value + */ + public String getPreventDelete() { + return edgeFields.get(EdgeProperty.PREVENT_DELETE).toString(); + } + + /** + * Returns if this rule is a default or not + * + * @return boolean + */ + public boolean isDefault() { + return isDefaultEdge; + } + + /** + * Gets the description on the edge rule (if there is one) + * @return String description + */ + public String getDescription() { + return this.description; + } +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/edges/EdgeRuleQuery.java b/aai-schema-ingest/src/main/java/org/onap/aai/edges/EdgeRuleQuery.java new file mode 100644 index 00000000..3029685f --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/edges/EdgeRuleQuery.java @@ -0,0 +1,214 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * 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.aai.edges; + +import org.onap.aai.edges.enums.AAIDirection; +import org.onap.aai.edges.enums.EdgeField; +import org.onap.aai.edges.enums.EdgeProperty; +import org.onap.aai.edges.enums.EdgeType; +import org.onap.aai.setup.Version; + +import com.jayway.jsonpath.Filter; +import com.jayway.jsonpath.Predicate; + +import static com.jayway.jsonpath.Filter.filter; + +import java.util.ArrayList; +import java.util.List; + +import static com.jayway.jsonpath.Criteria.where; + +/** + * For querying the edge rules schema (not the database) + * + */ +public class EdgeRuleQuery { + private Filter filter; + private Version v; + private String nodeA; + private String nodeB; + private String label; + private EdgeType type; + + public static class Builder { + //required + private String nodeA; + + //optional - null will translate to any value of the param + private String nodeB = null; + private String label = null; + private EdgeType type = null; + private Version version = Version.getLatest(); //default + + public Builder(String nodeA) { + this.nodeA = nodeA; + } + + public Builder(String nodeA, String nodeB) { + this.nodeA = nodeA; + this.nodeB = nodeB; + } + + private String getFirstNodeType() { + return nodeA; + } + + private String getSecondNodeType() { + return nodeB; + } + + public Builder label(String label) { + this.label = label; + return this; + } + + private String getLabel() { + return label; + } + + public Builder edgeType(EdgeType type) { + this.type = type; + return this; + } + + private EdgeType getEdgeType() { + return type; + } + + public Builder version(Version version) { + this.version = version; + return this; + } + private Version getVersion() { + return version; + } + + public EdgeRuleQuery build() { + return new EdgeRuleQuery(this); + } + } + + private EdgeRuleQuery(Builder builder) { + this.v = builder.getVersion(); + this.nodeA = builder.getFirstNodeType(); + this.nodeB = builder.getSecondNodeType(); + this.label = builder.getLabel(); + this.type = builder.getEdgeType(); + + //will cover from A to B case + List<Predicate> criteriaFromTo = new ArrayList<>(); + criteriaFromTo.add(buildToFromPart(builder.getFirstNodeType(), builder.getSecondNodeType())); + //will cover from B to A case - must be separate bc jsonpath won't let me OR predicates >:C + List<Predicate> criteriaToFrom = new ArrayList<>(); + criteriaToFrom.add(buildToFromPart(builder.getSecondNodeType(), builder.getFirstNodeType())); + + + + if (builder.getLabel() != null) { + Predicate labelPred = addLabel(builder.getLabel()); + criteriaFromTo.add(labelPred); + criteriaToFrom.add(labelPred); + } + + if (builder.getEdgeType() != null) { + Predicate typePred = addType(builder.getEdgeType()); + criteriaFromTo.add(typePred); + criteriaToFrom.add(typePred); + } + + + + this.filter = filter(criteriaFromTo).or(filter(criteriaToFrom)); + } + + private Predicate buildToFromPart(String from, String to) { + if (from == null && to == null) { //shouldn't ever happen though + throw new IllegalStateException("must have at least one node defined"); + } + + Predicate p; + + if (to == null) { + p = where(EdgeField.FROM.toString()).is(from); + } else if (from == null) { + p = where(EdgeField.TO.toString()).is(to); + } else { + p = where(EdgeField.FROM.toString()).is(from).and(EdgeField.TO.toString()).is(to); + } + + return p; + } + + private Predicate addLabel(String label) { + return where(EdgeField.LABEL.toString()).is(label); + } + + private Predicate addType(EdgeType type) { + if (type == EdgeType.COUSIN) { + return where(EdgeProperty.CONTAINS.toString()).is(AAIDirection.NONE.toString()); + } else { //equals TREE + return where(EdgeProperty.CONTAINS.toString()).ne(AAIDirection.NONE.toString()); + } + } + + /** + * Provides the JsonPath filter for actually querying the edge rule schema files + * @return Filter + */ + public Filter getFilter() { + return this.filter; + } + + /** + * So the Ingestor knows which version of the rules to search + * @return the Version + */ + public Version getVersion() { + return this.v; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + + sb.append("EdgeRuleQuery with filter params node type: ").append(nodeA); + + if (nodeB != null) { + sb.append(", node type: ").append(nodeB); + } + + if (label != null) { + sb.append(", label: ").append(label); + } + + sb.append(", type: "); + if (type != null) { + sb.append(type.toString()); + } else { + sb.append("any"); + } + + sb.append(", for version: ").append(v.toString()).append("."); + return sb.toString(); + } +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/edges/enums/AAIDirection.java b/aai-schema-ingest/src/main/java/org/onap/aai/edges/enums/AAIDirection.java new file mode 100644 index 00000000..3f748a50 --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/edges/enums/AAIDirection.java @@ -0,0 +1,62 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * 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.aai.edges.enums; + +import org.apache.tinkerpop.gremlin.structure.Direction; + +public enum AAIDirection { + IN, OUT, BOTH, NONE; + + public AAIDirection opposite() { + if (this.equals(OUT)) { + return IN; + } else if (this.equals(IN)) { + return OUT; + } else { + return BOTH; + } + } + + public static AAIDirection getValue(String aaidir) { + if (OUT.toString().equals(aaidir)) { + return OUT; + } else if (IN.toString().equals(aaidir)) { + return IN; + } else if (NONE.toString().equals(aaidir)) { + return NONE; + } else { //should be BOTH + return BOTH; + } + } + + public static AAIDirection getValue(Direction dir) { + if (dir == Direction.OUT) { + return OUT; + } else if (dir == Direction.IN) { + return IN; + } else if (dir == Direction.BOTH) { + return BOTH; + } else { + return NONE; + } + } +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/edges/enums/DirectionNotation.java b/aai-schema-ingest/src/main/java/org/onap/aai/edges/enums/DirectionNotation.java new file mode 100644 index 00000000..203249a4 --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/edges/enums/DirectionNotation.java @@ -0,0 +1,49 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * 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.aai.edges.enums; + +public enum DirectionNotation { + DIRECTION("${direction}"), + OPPOSITE("!${direction}"); + + private final String val; + + DirectionNotation(String dir) { + this.val = dir; + } + + public static DirectionNotation getValue(String val) { + if (DIRECTION.toString().equals(val)) { + return DIRECTION; + } else if (OPPOSITE.toString().equals(val)) { + return OPPOSITE; + } else { + throw new IllegalArgumentException(); + } + } + + @Override + public String toString() { + return this.val; + } +}
\ No newline at end of file diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/edges/enums/EdgeField.java b/aai-schema-ingest/src/main/java/org/onap/aai/edges/enums/EdgeField.java new file mode 100644 index 00000000..3e896f61 --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/edges/enums/EdgeField.java @@ -0,0 +1,50 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * 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.aai.edges.enums; + +/** + * Enumerates all edge rule fields + * + */ +public enum EdgeField { + FROM("from"), + TO("to"), + LABEL("label"), + DIRECTION("direction"), + MULTIPLICITY("multiplicity"), + CONTAINS("contains-other-v"), + DELETE_OTHER_V("delete-other-v"), + PREVENT_DELETE("prevent-delete"), + DEFAULT("default"), + DESCRIPTION("description"); + private final String name; + + private EdgeField(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/edges/enums/EdgeProperty.java b/aai-schema-ingest/src/main/java/org/onap/aai/edges/enums/EdgeProperty.java new file mode 100644 index 00000000..2f6afa47 --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/edges/enums/EdgeProperty.java @@ -0,0 +1,43 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * 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.aai.edges.enums; + +/** + * Enumerates edge rule fields which are edge properties + * (ie which have a directional value) + */ +public enum EdgeProperty { + CONTAINS("contains-other-v"), + DELETE_OTHER_V("delete-other-v"), + PREVENT_DELETE("prevent-delete"); + private final String name; + + private EdgeProperty(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/edges/enums/EdgeType.java b/aai-schema-ingest/src/main/java/org/onap/aai/edges/enums/EdgeType.java new file mode 100644 index 00000000..00dce0fd --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/edges/enums/EdgeType.java @@ -0,0 +1,27 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * 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.aai.edges.enums; + +public enum EdgeType { + COUSIN, + TREE; +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/edges/enums/MultiplicityRule.java b/aai-schema-ingest/src/main/java/org/onap/aai/edges/enums/MultiplicityRule.java new file mode 100644 index 00000000..4fc8938d --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/edges/enums/MultiplicityRule.java @@ -0,0 +1,41 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * 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.aai.edges.enums; + +public enum MultiplicityRule { + MANY2ONE, + ONE2MANY, + ONE2ONE, + MANY2MANY; + + public static MultiplicityRule getValue(String multiplicity) { + if ("Many2Many".equalsIgnoreCase(multiplicity)) { + return MANY2MANY; + } else if ("One2Many".equalsIgnoreCase(multiplicity)) { + return ONE2MANY; + } else if ("One2One".equalsIgnoreCase(multiplicity)) { + return ONE2ONE; + } else { //should be "Many2One" + return MANY2ONE; + } + } +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/edges/exceptions/AmbiguousRuleChoiceException.java b/aai-schema-ingest/src/main/java/org/onap/aai/edges/exceptions/AmbiguousRuleChoiceException.java new file mode 100644 index 00000000..ebb9739b --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/edges/exceptions/AmbiguousRuleChoiceException.java @@ -0,0 +1,29 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * 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.aai.edges.exceptions; + +public class AmbiguousRuleChoiceException extends Exception { + public AmbiguousRuleChoiceException(String msg) { + super(msg); + } +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/edges/exceptions/EdgeRuleNotFoundException.java b/aai-schema-ingest/src/main/java/org/onap/aai/edges/exceptions/EdgeRuleNotFoundException.java new file mode 100644 index 00000000..e1a8fe6b --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/edges/exceptions/EdgeRuleNotFoundException.java @@ -0,0 +1,29 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * 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.aai.edges.exceptions; + +public class EdgeRuleNotFoundException extends Exception { + public EdgeRuleNotFoundException(String msg) { + super(msg); + } +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/nodes/NodeIngestor.java b/aai-schema-ingest/src/main/java/org/onap/aai/nodes/NodeIngestor.java new file mode 100644 index 00000000..5a29806d --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/nodes/NodeIngestor.java @@ -0,0 +1,107 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * 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.aai.nodes; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import javax.xml.bind.JAXBException; + +import org.eclipse.persistence.jaxb.JAXBContextProperties; +import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext; +import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory; +import org.onap.aai.setup.ConfigTranslator; +import org.onap.aai.setup.Version; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +/** + * NodeIngestor - ingests A&AI OXM files per given config, serves DynamicJAXBContext per version + */ +public class NodeIngestor { + + private Map<Version, DynamicJAXBContext> versionContextMap = new EnumMap<>(Version.class); + + @Autowired + /** + * Instantiates the NodeIngestor bean. + * + * @param translator - ConfigTranslator autowired in by Spring framework which + * contains the configuration information needed to ingest the desired files. + */ + public NodeIngestor(ConfigTranslator translator) { + Map<Version, List<String>> filesToIngest = translator.getNodeFiles(); + + try { + for (Entry<Version, List<String>> verFiles : filesToIngest.entrySet()) { + Version v = verFiles.getKey(); + List<String> files = verFiles.getValue(); + final DynamicJAXBContext ctx = ingest(files); + versionContextMap.put(v, ctx); + } + } catch (FileNotFoundException | JAXBException e) { + throw new ExceptionInInitializerError(e); + } + } + + /** + * Ingests the given OXM files into DynamicJAXBContext + * + * @param files - List<String> of full filenames (ie including the path) to be ingested + * + * @return DynamicJAXBContext including schema information from all given files + * + * @throws FileNotFoundException if an OXM file can't be found + * @throws JAXBException if there's an error creating the DynamicJAXBContext + */ + private DynamicJAXBContext ingest(List<String> files) throws FileNotFoundException, JAXBException { + List<InputStream> streams = new ArrayList<>(); + + for (String name : files) { + streams.add(new FileInputStream(new File(name))); + } + + Map<String, Object> properties = new HashMap<>(); + properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, streams); + return DynamicJAXBContextFactory.createContextFromOXM(this.getClass().getClassLoader(), properties); + } + + /** + * Gets the DynamicJAXBContext for the given version + * + * @param Version v + * @return DynamicJAXBContext + */ + public DynamicJAXBContext getContextForVersion(Version v) { + return versionContextMap.get(v); + } +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/setup/ConfigTranslator.java b/aai-schema-ingest/src/main/java/org/onap/aai/setup/ConfigTranslator.java new file mode 100644 index 00000000..b34622de --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/setup/ConfigTranslator.java @@ -0,0 +1,61 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * 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.aai.setup; + +import java.util.List; +import java.util.Map; + +import org.springframework.beans.factory.annotation.Autowired; + +/** + * Converts the contents of the schema config file + * (which lists which schema files to be loaded) to + * the format the Ingestors can work with. + * + */ +public abstract class ConfigTranslator { + protected SchemaLocationsBean bean; + + @Autowired + public ConfigTranslator(SchemaLocationsBean bean) { + this.bean = bean; + } + + /** + * Translates the contents of the schema config file + * into the input for the NodeIngestor + * + * @return Map of Version to the list of (string) filenames to be + * ingested for that version + */ + public abstract Map<Version, List<String>> getNodeFiles(); + + /** + * Translates the contents of the schema config file + * into the input for the EdgeIngestor + * + * @return Map of Version to the List of (String) filenames to be + * ingested for that version + */ + public abstract Map<Version, List<String>> getEdgeFiles(); +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaLocationsBean.java b/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaLocationsBean.java new file mode 100644 index 00000000..96c63447 --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaLocationsBean.java @@ -0,0 +1,111 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * 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.aai.setup; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; + +@Configuration +@PropertySource("classpath:schemaIngest.properties") +@PropertySource(value = "file:${schemaIngestPropLoc}", ignoreResourceNotFound=true) +public class SchemaLocationsBean { + /* + * Per Spring documentation, the last PropertySource that works will + * be applied. Here, schemaIngestPropLoc will be an environment variable + * set on install that tells Spring where to look for the schema + * ingest properties file (and the actual filename), but the former + * PropertySource gives the default of looking on the classpath for + * schemaIngest.properties in case that second one doesn't work. + * + * The schemaIngest.properties file (or its equivalent if you choose + * to name it otherwise) must contain the entries the below @Value + * annotations are looking for. + */ + + @Value("${schemaConfig}") + private String schemaConfigLoc; + + @Value("${nodeDir}") + private String nodeDirectory; + + @Value("${edgeDir}") + private String edgeDirectory; + + /** + * @return the file name/location with the list of schema files to be ingested + */ + public String getSchemaConfigLocation() { + return schemaConfigLoc; + } + + /** + * Sets the name/location of the file with the list of schema files to ingest + * + * @param String schemaConfigLoc - the file name/location + */ + public void setSchemaConfigLocation(String schemaConfigLoc) { + this.schemaConfigLoc = schemaConfigLoc; + } + + /** + * @return the location of the OXM files + */ + public String getNodeDirectory() { + return nodeDirectory; + } + + /** + * Sets the location of the OXM files + * + * @param String nodeDirectory - the location of the OXM files + */ + public void setNodeDirectory(String nodeDirectory) { + this.nodeDirectory = nodeDirectory; + } + + /** + * @return the location of the edge rule json files + */ + public String getEdgeDirectory() { + return edgeDirectory; + } + + /** + * Sets the location of the edge rule json files + * + * @param String edgeDirectory - the location of the edge rule files + */ + public void setEdgeDirectory(String edgeDirectory) { + this.edgeDirectory = edgeDirectory; + } + + //this allows the code to actually read the value from the config file + //without this those strings get set to literally "${edgeDir}" etc + @Bean + public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { + return new PropertySourcesPlaceholderConfigurer(); + } +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/setup/Version.java b/aai-schema-ingest/src/main/java/org/onap/aai/setup/Version.java new file mode 100644 index 00000000..8cc39426 --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/setup/Version.java @@ -0,0 +1,40 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * 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.aai.setup; + +public enum Version { + V8, + V9, + V10, + V11, + V12, + V13; + + public static boolean isLatest(Version v) { + return getLatest().equals(v); + } + + public static Version getLatest(){ + Version[] vals = values(); //guaranteed to be in declaration order + return vals[vals.length-1]; //requires we always have the latest version listed last + } +} |