diff options
author | James Forsyth <jf2512@att.com> | 2018-12-06 19:13:02 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-12-06 19:13:02 +0000 |
commit | 88bb14a7c2a1638133182b6030a01eead186bb66 (patch) | |
tree | 77ba2150a0af8b2b0ae0b0635ab675cc4139ec44 /aai-schema-ingest/src/main | |
parent | ddf953affc477a77f9b68583f8ba0d6d5705882a (diff) | |
parent | 5948f878fa0ee735e81f1cf648d5d3bdb35048cd (diff) |
Merge "Update schema ingest library call schema service"
Diffstat (limited to 'aai-schema-ingest/src/main')
33 files changed, 1891 insertions, 835 deletions
diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/config/ConfigConfiguration.java b/aai-schema-ingest/src/main/java/org/onap/aai/config/ConfigConfiguration.java new file mode 100644 index 00000000..9f28cf8f --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/config/ConfigConfiguration.java @@ -0,0 +1,46 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Modifications Copyright © 2018 IBM. + * ================================================================================ + * 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========================================================= + */ +package org.onap.aai.config; + +import org.onap.aai.setup.SchemaLocationsBean; +import org.onap.aai.setup.SchemaVersions; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; + +@Configuration +@ConditionalOnProperty(prefix = "schema.translator.list", value = "config", matchIfMissing = true) +@PropertySource(value = "classpath:schema-ingest.properties", ignoreResourceNotFound = true) +@PropertySource(value = "file:${schema.ingest.file}", ignoreResourceNotFound = true) +public class ConfigConfiguration { + + @Bean(name = "schemaVersions") + public SchemaVersions schemaVersions() { + return new SchemaVersions(); + } + + @Bean(name = "schemaLocationsBean") + public SchemaLocationsBean schemaLocationsBean() { + return new SchemaLocationsBean(); + } +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/config/EdgesConfiguration.java b/aai-schema-ingest/src/main/java/org/onap/aai/config/EdgesConfiguration.java new file mode 100644 index 00000000..e492d213 --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/config/EdgesConfiguration.java @@ -0,0 +1,77 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Modifications Copyright © 2018 IBM. + * ================================================================================ + * 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========================================================= + */ +package org.onap.aai.config; + +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.Translator; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.PropertySource; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +@Import({SchemaServiceConfiguration.class, ConfigConfiguration.class, TranslatorConfiguration.class}) +@PropertySource(value = "classpath:schema-ingest.properties", ignoreResourceNotFound = true) +@PropertySource(value = "file:${schema.ingest.file}", ignoreResourceNotFound = true) +@Configuration +public class EdgesConfiguration { + + private static final String CONFIG_TRANSLATOR = "config"; + private static final String SCHEMA_SERVICE_TRANSLATOR = "schema-service"; + + @Autowired(required = false) + SchemaServiceConfiguration schemaConfiguration; + + @Autowired(required = false) + ConfigConfiguration configConfiguration; + + @Autowired(required = false) + TranslatorConfiguration translatorConfiguration; + + @Value("${schema.translator.list}") + private String[] translatorArray; + + public Set<Translator> translators() { + Set<Translator> translators = new HashSet<>(); + + List<String> translatorList = Arrays.asList(translatorArray); + if (translatorList.contains(SCHEMA_SERVICE_TRANSLATOR)) { + translators.add(schemaConfiguration.schemaServiceTranslator()); + } else { + translators.add(translatorConfiguration.configTranslator); + } + return translators; + } + + @Bean(name = "edgeIngestor") + public EdgeIngestor edgeIngestor() { + return new EdgeIngestor(translators()); + } + +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/config/NodesConfiguration.java b/aai-schema-ingest/src/main/java/org/onap/aai/config/NodesConfiguration.java new file mode 100644 index 00000000..94bc6da5 --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/config/NodesConfiguration.java @@ -0,0 +1,82 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Modifications Copyright © 2018 IBM. + * ================================================================================ + * 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========================================================= + */ +package org.onap.aai.config; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.onap.aai.nodes.NodeIngestor; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.Translator; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.PropertySource; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +@Import({SchemaServiceConfiguration.class, ConfigConfiguration.class, TranslatorConfiguration.class}) +@PropertySource(value = "classpath:schema-ingest.properties", ignoreResourceNotFound = true) +@PropertySource(value = "file:${schema.ingest.file}", ignoreResourceNotFound = true) +@Configuration +public class NodesConfiguration { + + private static final String CONFIG_TRANSLATOR = "config"; + private static final String SCHEMA_SERVICE_TRANSLATOR = "schema-service"; + private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(NodesConfiguration.class); + + @Autowired(required = false) + SchemaServiceConfiguration schemaConfiguration; + + @Autowired(required = false) + ConfigConfiguration configConfiguration; + + @Autowired(required = false) + TranslatorConfiguration translatorConfiguration; + + @Value("${schema.translator.list}") + private String[] translatorArray; + + public Set<Translator> translators() { + Set<Translator> translators = new HashSet<>(); + + List<String> translatorList = Arrays.asList(translatorArray); + if (translatorList.contains(SCHEMA_SERVICE_TRANSLATOR)) { + LOGGER.info("Translator is SchemaServiceTranslator"); + translators.add(schemaConfiguration.schemaServiceTranslator()); + } else { + LOGGER.info("Translator is SchemaServiceTranslator"); + translators.add(translatorConfiguration.configTranslator); + } + return translators; + } + + @Bean(name = "nodeIngestor") + public NodeIngestor nodeIngestor() { + return new NodeIngestor(translators()); + } + +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/config/RestConfiguration.java b/aai-schema-ingest/src/main/java/org/onap/aai/config/RestConfiguration.java new file mode 100644 index 00000000..0a3dbf5e --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/config/RestConfiguration.java @@ -0,0 +1,82 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Modifications Copyright © 2018 IBM. + * ================================================================================ + * 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========================================================= + */ +package org.onap.aai.config; + +import org.onap.aai.restclient.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; + +@Configuration +@ConditionalOnExpression("'${schema.translator.list}'.contains('schema-service')") +@PropertySource(value = "classpath:schema-ingest.properties", ignoreResourceNotFound = true) +@PropertySource(value = "file:${schema.ingest.file}", ignoreResourceNotFound = true) +public class RestConfiguration { + + private static final String TWO_WAY_SSL = "two-way-ssl"; + private static final String ONE_WAY_SSL = "one-way-ssl"; + private static final String NO_AUTH = "no-auth"; + + @Value("${schema.service.client:two-way-ssl}") + private String schemaServiceClient; + + @Autowired + private RestClient restClient; + + @Bean + public RestClientFactory restClientFactory() { + + return new RestClientFactory() { + @Override + public RestClient getRestClient(String clientType) { + return restClient; + + } + }; + } + + /* + In the below cases bean name and method names are different because all of them qualify as restClient + */ + @Bean(name="restClient") + @ConditionalOnProperty(name = "schema.service.client", havingValue = "two-way-ssl", matchIfMissing = true) + public RestClient getSchemaServiceTwoWayClient() { + return new SchemaServiceRestClient(); + } + + @Bean(name="restClient") + @ConditionalOnProperty(name = "schema.service.client", havingValue = "no-auth") + public RestClient getSchemaServiceNoAuthClient() { + return new SchemaServiceNoAuthClient(); + } + + @Bean(name="restClient") + @ConditionalOnProperty(name = "schema.service.client", havingValue = "one-way-ssl") + public RestClient getSchemaServiceOneWayClient() { + return new SchemaServiceOneWayClient(); + } + +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/config/SchemaServiceConfiguration.java b/aai-schema-ingest/src/main/java/org/onap/aai/config/SchemaServiceConfiguration.java new file mode 100644 index 00000000..81ef02a8 --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/config/SchemaServiceConfiguration.java @@ -0,0 +1,54 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Modifications Copyright © 2018 IBM. + * ================================================================================ + * 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========================================================= + */ +package org.onap.aai.config; + +import org.onap.aai.setup.SchemaServiceTranslator; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersionsBean; +import org.onap.aai.setup.Translator; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; + +@Configuration +@ConditionalOnExpression("'${schema.translator.list}'.contains('schema-service')") +@PropertySource(value = "classpath:schema-ingest.properties", ignoreResourceNotFound = true) +@PropertySource(value = "file:${schema.ingest.file}", ignoreResourceNotFound = true) +public class SchemaServiceConfiguration { + + @Bean(name = "schemaVersionsBean") + public SchemaVersionsBean schemaVersionsBean() { + return new SchemaVersionsBean(); + } + + @Bean(name = "schemaVersions") + public SchemaVersions schemaVersions() { + return schemaVersionsBean().getSchemaVersions(); + } + + @Bean(name = "schemaServiceTranslator") + public Translator schemaServiceTranslator() { + return new SchemaServiceTranslator(schemaVersions()); + } + +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/config/TranslatorConfiguration.java b/aai-schema-ingest/src/main/java/org/onap/aai/config/TranslatorConfiguration.java new file mode 100644 index 00000000..941e03d9 --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/config/TranslatorConfiguration.java @@ -0,0 +1,36 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Modifications Copyright © 2018 IBM. + * ================================================================================ + * 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========================================================= + */ +package org.onap.aai.config; + +import org.onap.aai.setup.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConditionalOnProperty(prefix = "schema.translator.list", value = "config", matchIfMissing = true) +public class TranslatorConfiguration { + //TODO check if you can put dependsOn to reduce number of config files + @Autowired(required = false) + ConfigTranslator configTranslator; + +} 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 index c2c58d5f..71225340 100644 --- 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 @@ -20,37 +20,37 @@ package org.onap.aai.edges; -import java.util.*; -import java.util.Map.Entry; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; - -import com.google.common.cache.Cache; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; +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 org.apache.tinkerpop.gremlin.structure.Direction; 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.SchemaVersion; import org.onap.aai.setup.SchemaVersions; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.onap.aai.setup.Translator; 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 javax.annotation.PostConstruct; +import java.io.IOException; +import java.util.*; +import java.util.Map.Entry; +import java.util.concurrent.ExecutionException; +import java.util.stream.Collectors; + import static com.jayway.jsonpath.Criteria.where; +import static com.jayway.jsonpath.Filter.filter; /** * EdgeIngestor - ingests A&AI edge rule schema files per given config, serves that edge rule @@ -58,34 +58,73 @@ import static com.jayway.jsonpath.Criteria.where; */ @Component public class EdgeIngestor { + private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(EdgeIngestor.class); + private Map<SchemaVersion, List<DocumentContext>> versionJsonFilesMap; + private static final String READ_START = "$.rules.[?]"; + private static final String READ_ALL_START = "$.rules.*"; + private SchemaVersions schemaVersions; - private static final Logger LOG = LoggerFactory.getLogger(EdgeIngestor.class); + Map<SchemaVersion, List<String>> filesToIngest; - private Map<SchemaVersion, List<DocumentContext>> versionJsonFilesMap; - private static final String READ_START = "$.rules.[?]"; - private static final String READ_ALL_START = "$.rules.*"; + private Set<String> multipleLabelKeys; - private SchemaVersions schemaVersions; + private LoadingCache<SchemaFilter,Multimap<String,EdgeRule>> cacheFilterStore; - private Set<String> multipleLabelKeys; + private LoadingCache<String, String[]> cousinLabelStore; - private final LoadingCache<SchemaFilter,Multimap<String,EdgeRule>> cacheFilterStore; + private Set<Translator> translators; - private final LoadingCache<String, String[]> cousinLabelStore; + @Autowired + public EdgeIngestor(Set<Translator> translatorSet) { + LOGGER.debug("Local Schema files will be fetched"); + this.translators = translatorSet; + } + + @PostConstruct + public void initialize() { + + for (Translator translator : translators) { + try { + LOGGER.debug("Processing the translator"); + translateAll(translator); + + } catch (Exception e) { + LOGGER.debug("Error while Processing the translator" + e.getMessage()); + continue; + } + } + if (versionJsonFilesMap.isEmpty() || schemaVersions==null ) { + throw new ExceptionInInitializerError(); + } + } + + public void translateAll(Translator translator) { + /* + Use SchemaVersions from the Translator + */ + this.schemaVersions = translator.getSchemaVersions(); + List<SchemaVersion> schemaVersionList = this.schemaVersions.getVersions(); + List<String> jsonPayloads = null; + JsonIngestor ji = new JsonIngestor(); + Map<SchemaVersion, List<String>> edgeRulesToIngest = new HashMap<>(); // Obtain a map of schema versions to a list of strings. One List per key + + // Add to the map the JSON file per version. + for (SchemaVersion version : schemaVersionList) { + LOGGER.debug("Version being processed" + version); + // If the flag is set to not use the local files, obtain the Json from the service. + try { + jsonPayloads = translator.getJsonPayload(version); // need to change this - need to receive the json files. + } catch (IOException e) { + LOGGER.info("Exception in retrieving the JSON Payload"+e.getMessage()); + } + if (jsonPayloads == null || jsonPayloads.isEmpty()) { + continue; + } + LOGGER.debug("Retrieved json from SchemaService"); + edgeRulesToIngest.put(version, jsonPayloads); + } + versionJsonFilesMap = ji.ingestContent(edgeRulesToIngest); - //-----ingest-----// - /** - * Instantiates the EdgeIngestor bean. - * - * @param translator - ConfigTranslator autowired in by Spring framework which - * contains the configuration information needed to ingest the desired files. - */ - @Autowired - public EdgeIngestor(ConfigTranslator translator, SchemaVersions schemaVersions) { - Map<SchemaVersion, List<String>> filesToIngest = translator.getEdgeFiles(); - JsonIngestor ji = new JsonIngestor(); - this.schemaVersions = schemaVersions; - versionJsonFilesMap = ji.ingest(filesToIngest); this.cacheFilterStore = CacheBuilder.newBuilder() .maximumSize(2000) .build( @@ -107,27 +146,27 @@ public class EdgeIngestor { } } ); - } - - //-----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(schemaVersions.getDefaultVersion()); - } +// //-----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(schemaVersions.getDefaultVersion()); + } /** * Retrieves all the nodes that contain multiple edge labels @@ -137,81 +176,83 @@ public class EdgeIngestor { * @return a set containing a list of strings where each string is * concatenated by a pipe (|) character such as aNodeType|bNodeType */ - public Set<String> getMultipleLabelKeys(){ + public Set<String> getMultipleLabelKeys(){ - if(multipleLabelKeys == null){ + if(multipleLabelKeys == null){ multipleLabelKeys = new HashSet<>(); try { final Multimap<String, EdgeRule> edges = this.getAllCurrentRules(); if(edges == null || edges.isEmpty()){ - LOG.warn("Unable to find any edge rules for the latest version"); + LOGGER.warn("Unable to find any edge rules for the latest version"); + return multipleLabelKeys; } - edges.keySet().forEach((key) -> { + edges.keySet().forEach(key -> { Collection<EdgeRule> rules = edges.get(key); if(rules.size() > 1){ multipleLabelKeys.add(key); } }); } catch (EdgeRuleNotFoundException e) { - LOG.info("For the latest schema version, unable to find any edges with multiple keys"); + LOGGER.info("For the latest schema version, unable to find any edges with multiple keys"); } } return multipleLabelKeys; } - /** - * 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(SchemaVersion 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; - } - } + /** + * 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(SchemaVersion 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 = null; - if(q.getVersion().isPresent()){ - found = extractRules(q.getFilter(), q.getVersion().get()); - } else { - found = extractRules(q.getFilter(), schemaVersions.getDefaultVersion()); - } - if (found.isEmpty()) { - throw new EdgeRuleNotFoundException("No rules found for " + q.toString()); - } else { - Multimap<String, EdgeRule> copy = ArrayListMultimap.create(); - found.entries().stream().forEach((entry) -> { - EdgeRule rule = new EdgeRule(entry.getValue()); - if(!q.getFromType().equals(rule.getFrom())){ + /** + * 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 = null; + if(q.getVersion().isPresent()){ + found = extractRules(q.getFilter(), q.getVersion().get()); + } else { + found = extractRules(q.getFilter(), schemaVersions.getDefaultVersion()); + } + if (found.isEmpty()) { + throw new EdgeRuleNotFoundException("No rules found for " + q.toString()); + } else { + Multimap<String, EdgeRule> copy = ArrayListMultimap.create(); + found.entries().stream().forEach((entry) -> { + EdgeRule rule = new EdgeRule(entry.getValue()); + if (!q.getFromType().equals(rule.getFrom())) { /* To maintain backwards compatibility with old EdgeRules API, * where the direction of the returned EdgeRule would be * flipped (if necessary) to match the directionality of @@ -220,296 +261,293 @@ public class EdgeIngestor { * if the user asked (A,B) the direction would be OUT, * if they asked (B,A), it would be IN to match. */ - rule.flipDirection(); + rule.flipDirection(); } - copy.put(entry.getKey(), rule); + copy.put(entry.getKey(), rule); }); - return copy; - } - } - - /** - * 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 = null; - if(q.getVersion().isPresent()){ - found = extractRules(q.getFilter(), q.getVersion().get()); - } else { - found = extractRules(q.getFilter(), schemaVersions.getDefaultVersion()); - } - - 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); - } + return copy; + } + } - if (rule == null) { //should never get here though - throw new EdgeRuleNotFoundException("No rule found for " + q.toString() + "."); - } else { - rule = new EdgeRule(rule); - if (!q.getFromType().equals(rule.getFrom())) { - /* To maintain backwards compatibility with old EdgeRules API, - * where the direction of the returned EdgeRule would be - * flipped (if necessary) to match the directionality of - * the input params. - * ie, If the rule is from=A,to=B,direction=OUT, - * if the user asked (A,B) the direction would be OUT, - * if they asked (B,A), it would be IN to match. - */ - rule.flipDirection(); - } - return rule; - } - } + /** + * 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 = null; + if(q.getVersion().isPresent()){ + found = extractRules(q.getFilter(), q.getVersion().get()); + } else { + found = extractRules(q.getFilter(), schemaVersions.getDefaultVersion()); + } + if (found.isEmpty()) { + throw new EdgeRuleNotFoundException("No rule found for " + q.toString() + "."); + } - 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() + "."); - } + 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); + } - 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."); - } + if (rule == null) { //should never get here though + throw new EdgeRuleNotFoundException("No rule found for " + q.toString() + "."); + } else { + rule = new EdgeRule(rule); + if (!q.getFromType().equals(rule.getFrom())) { + /* To maintain backwards compatibility with old EdgeRules API, + * where the direction of the returned EdgeRule would be + * flipped (if necessary) to match the directionality of + * the input params. + * ie, If the rule is from=A,to=B,direction=OUT, + * if the user asked (A,B) the direction would be OUT, + * if they asked (B,A), it would be IN to match. + */ + rule.flipDirection(); + } + return rule; + } + } - return defRule; - } + 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() + "."); + } - /** - * 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) { - if(q.getVersion().isPresent()){ - return !extractRules(q.getFilter(), q.getVersion().get()).isEmpty(); - } else { - return !extractRules(q.getFilter(), schemaVersions.getDefaultVersion()).isEmpty(); - } - } + 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."); + } - /** - * 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, schemaVersions.getDefaultVersion()); //default to latest - } + 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) { + if(q.getVersion().isPresent()){ + return !extractRules(q.getFilter(), q.getVersion().get()).isEmpty(); + } else { + return !extractRules(q.getFilter(), schemaVersions.getDefaultVersion()).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, schemaVersions.getDefaultVersion()); //default to latest + } - public String[] retrieveCousinLabels(String nodeType){ + public String[] retrieveCousinLabels(String nodeType){ - Multimap<String, EdgeRule> cousinRules = getCousinRules(nodeType); - String[] cousinLabels = new String[cousinRules.size()]; + Multimap<String, EdgeRule> cousinRules = getCousinRules(nodeType); + String[] cousinLabels = new String[cousinRules.size()]; - return cousinRules.entries() - .stream() - .map((entry) -> entry.getValue().getLabel()) - .collect(Collectors.toList()) - .toArray(cousinLabels); + return cousinRules.entries() + .stream() + .map(entry -> entry.getValue().getLabel()) + .collect(Collectors.toList()) + .toArray(cousinLabels); } public String[] retrieveCachedCousinLabels(String nodeType) throws ExecutionException { - return cousinLabelStore.get(nodeType); + return cousinLabelStore.get(nodeType); } - /** - * 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, SchemaVersion v) { - return extractRules(new EdgeRuleQuery.Builder(nodeType).edgeType(EdgeType.COUSIN).build().getFilter(), v); - } + /** + * 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, SchemaVersion 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, schemaVersions.getDefaultVersion()); - } + /** + * 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, schemaVersions.getDefaultVersion()); + } - /** - * Returns if the given node type has any cousin relationships in the given version. - * @param nodeType - * @return boolean - */ - public boolean hasCousinRule(String nodeType, SchemaVersion v) { - return !getCousinRules(nodeType, v).isEmpty(); - } + /** + * Returns if the given node type has any cousin relationships in the given version. + * @param nodeType + * @return boolean + */ + public boolean hasCousinRule(String nodeType, SchemaVersion 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, schemaVersions.getDefaultVersion()); - } + /** + * 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, schemaVersions.getDefaultVersion()); + } - /** - * 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, SchemaVersion 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); + /** + * 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, SchemaVersion 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); - } + 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, schemaVersions.getDefaultVersion()); - } + /** + * 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, schemaVersions.getDefaultVersion()); + } - /** - * 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, SchemaVersion v) { - return !getChildRules(nodeType, v).isEmpty(); - } + /** + * 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, SchemaVersion 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, schemaVersions.getDefaultVersion()); - } + /** + * 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, schemaVersions.getDefaultVersion()); + } - /** - * 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, SchemaVersion 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); + /** + * 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, SchemaVersion 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); - } + 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, schemaVersions.getDefaultVersion()); - } + /** + * 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, schemaVersions.getDefaultVersion()); + } - /** - * 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, SchemaVersion v) { - return !getParentRules(nodeType, v).isEmpty(); - } + /** + * 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, SchemaVersion v) { + return !getParentRules(nodeType, v).isEmpty(); + } /** * Applies the given filter to the DocumentContext(s) for the given version to extract @@ -527,17 +565,17 @@ public class EdgeIngestor { * 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, SchemaVersion v) { - SchemaFilter schemaFilter = new SchemaFilter(filter, v); + private Multimap<String, EdgeRule> extractRules(Filter filter, SchemaVersion v) { + SchemaFilter schemaFilter = new SchemaFilter(filter, v); try { return cacheFilterStore.get(schemaFilter); } catch (ExecutionException e) { - LOG.info("Encountered exception during the retrieval of the rules"); + LOGGER.info("Encountered exception during the retrieval of the rules"); return ArrayListMultimap.create(); } } - public Multimap<String, EdgeRule> extractRules(SchemaFilter schemaFilter){ + public Multimap<String, EdgeRule> extractRules(SchemaFilter schemaFilter){ List<Map<String, String>> foundRules = new ArrayList<>(); List<DocumentContext> docs = versionJsonFilesMap.get(schemaFilter.getSchemaVersion()); if (docs != null) { 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 index f914f6cb..f8f8caca 100644 --- 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 @@ -20,13 +20,8 @@ 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 org.onap.aai.edges.enums.*; -import java.util.Collections; import java.util.EnumMap; import java.util.HashMap; import java.util.Map; @@ -45,52 +40,52 @@ public class EdgeRule { private String description; private boolean isPrivateEdge = false; - /** + /** * Instantiates a new edge rule. - * + * * @param fieldVals - Map<String, String> where first string is - * an EdgeField value and second string is the + * 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())); isPrivateEdge = Boolean.valueOf(fieldVals.getOrDefault(EdgeField.PRIVATE.toString(), "false")); - for (EdgeProperty prop : EdgeProperty.values()) { + + 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()); + description = fieldVals.get(EdgeField.DESCRIPTION.toString()); if (description == null) { //bc description is optional and not in v12 and earlier description = ""; } } - // Copy Constructor - public EdgeRule(EdgeRule edgeRule){ - this.from = edgeRule.from; - this.to = edgeRule.to; - this.label = edgeRule.label; - this.direction = Direction.valueOf(edgeRule.direction.toString()); - this.multiplicityRule = MultiplicityRule.valueOf(edgeRule.multiplicityRule.toString()); + // Copy Constructor + public EdgeRule(EdgeRule edgeRule){ + this.from = edgeRule.from; + this.to = edgeRule.to; + this.label = edgeRule.label; + this.direction = Direction.valueOf(edgeRule.direction.toString()); + this.multiplicityRule = MultiplicityRule.valueOf(edgeRule.multiplicityRule.toString()); this.edgeFields = new HashMap<>(edgeRule.edgeFields); - this.isDefaultEdge = edgeRule.isDefaultEdge; - this.description = edgeRule.description; - this.isPrivateEdge = edgeRule.isPrivateEdge; + this.isDefaultEdge = edgeRule.isDefaultEdge; + this.description = edgeRule.description; + this.isPrivateEdge = edgeRule.isPrivateEdge; } /** * 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} @@ -107,7 +102,7 @@ public class EdgeRule { } else if (AAIDirection.IN.toString().equalsIgnoreCase(rawVal)) { return AAIDirection.IN; } - + DirectionNotation rawDN = DirectionNotation.getValue(rawVal); if (DirectionNotation.DIRECTION.equals(rawDN)) { return AAIDirection.getValue(dir); @@ -115,7 +110,7 @@ public class EdgeRule { return AAIDirection.getValue(dir.opposite()); } } - + /** * Gets the name of the node type in the "from" field * @return String nodetype @@ -140,7 +135,7 @@ public class EdgeRule { public String getLabel() { return label; } - + /** * Gets the multiplicity rule. * @@ -149,7 +144,7 @@ public class EdgeRule { public MultiplicityRule getMultiplicityRule() { return multiplicityRule; } - + /** * Gets the edge direction * @@ -158,7 +153,7 @@ public class EdgeRule { public Direction getDirection() { return direction; } - + /** * Gets the value of contains-other-v * @@ -167,7 +162,7 @@ public class EdgeRule { public String getContains() { return edgeFields.get(EdgeProperty.CONTAINS).toString(); } - + /** * Gets the value of delete-other-v * @@ -176,10 +171,10 @@ public class EdgeRule { 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() { @@ -188,13 +183,13 @@ public class EdgeRule { /** * 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 @@ -202,7 +197,7 @@ public class EdgeRule { public String getDescription() { return this.description; } - + /** * Flips the direction value * IN -> OUT 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 index 58c8c546..d6fec83d 100644 --- 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 @@ -170,7 +170,7 @@ public class EdgeRuleQuery { List<Predicate> criteriaFromTo = new ArrayList<>(); //Special logic to allow for A to B case only if(("FromOnly").equals(builder.getSecondNodeType())) { - criteriaFromTo.add(buildToFromPart(builder.getFirstNodeType(), null)); + criteriaFromTo.add(buildToFromPart(builder.getFirstNodeType(), null)); } else { criteriaFromTo.add(buildToFromPart(builder.getFirstNodeType(), builder.getSecondNodeType())); } @@ -245,16 +245,16 @@ public class EdgeRuleQuery { } private Predicate addDirection(AAIDirection direction) { - if (direction == AAIDirection.OUT) { - return where(EdgeField.DIRECTION.toString()).in(AAIDirection.OUT.toString(), AAIDirection.BOTH.toString()); - } else if (direction == AAIDirection.IN) { - return where(EdgeField.DIRECTION.toString()).in(AAIDirection.IN.toString(), AAIDirection.BOTH.toString()); - } else if (direction == AAIDirection.BOTH) { - return where(EdgeField.DIRECTION.toString()).is(AAIDirection.BOTH.toString()); - } else if (direction == AAIDirection.NONE) { - return where(EdgeField.DIRECTION.toString()).is(AAIDirection.NONE.toString()); - } + if (direction == AAIDirection.OUT) { + return where(EdgeField.DIRECTION.toString()).in(AAIDirection.OUT.toString(), AAIDirection.BOTH.toString()); + } else if (direction == AAIDirection.IN) { + return where(EdgeField.DIRECTION.toString()).in(AAIDirection.IN.toString(), AAIDirection.BOTH.toString()); + } else if (direction == AAIDirection.BOTH) { + return where(EdgeField.DIRECTION.toString()).is(AAIDirection.BOTH.toString()); + } else if (direction == AAIDirection.NONE) { return where(EdgeField.DIRECTION.toString()).is(AAIDirection.NONE.toString()); + } + return where(EdgeField.DIRECTION.toString()).is(AAIDirection.NONE.toString()); } /** @@ -345,4 +345,3 @@ public class EdgeRuleQuery { } - diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/edges/JsonIngestor.java b/aai-schema-ingest/src/main/java/org/onap/aai/edges/JsonIngestor.java index 16562756..713103a0 100644 --- a/aai-schema-ingest/src/main/java/org/onap/aai/edges/JsonIngestor.java +++ b/aai-schema-ingest/src/main/java/org/onap/aai/edges/JsonIngestor.java @@ -1,4 +1,4 @@ -/** +/** * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ @@ -20,64 +20,85 @@ package org.onap.aai.edges; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.jayway.jsonpath.DocumentContext; +import com.jayway.jsonpath.JsonPath; +import org.onap.aai.setup.SchemaVersion; + import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.Map.Entry; -import org.onap.aai.setup.SchemaVersion; - -import com.jayway.jsonpath.DocumentContext; -import com.jayway.jsonpath.JsonPath; - /** * JsonIngestor produces DocumentContexts from json files */ public class JsonIngestor { - - /** - * Reads in given json files to queryable DocumentContexts. - * - * @param filesToIngest - map of filenames to ingest - * per Version - * @return Map<SchemaVersion, List<DocumentContext>> - map of DocumentContexts per Version - */ - public Map<SchemaVersion, List<DocumentContext>> ingest(Map<SchemaVersion, List<String>> filesToIngest) { - Map<SchemaVersion, List<DocumentContext>> result = new HashMap<>(); - - for (Entry<SchemaVersion, List<String>> verFiles : filesToIngest.entrySet()) { - SchemaVersion v = verFiles.getKey(); - List<String> files = verFiles.getValue(); - - List<DocumentContext> docs = new ArrayList<>(); - - for (String rulesFilename : files) { - String fileContents = readInJsonFile(rulesFilename); - docs.add(JsonPath.parse(fileContents)); - } - result.put(v, docs); - } - - return result; - } - - /** - * 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(); - } + private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(JsonIngestor.class); + + /** + * Reads in given json files to queryable DocumentContexts. + * + * @param filesToIngest - map of filenames to ingest + * per Version + * @return Map<SchemaVersion, List<DocumentContext>> - map of DocumentContexts per Version + */ + public Map<SchemaVersion, List<DocumentContext>> ingest(Map<SchemaVersion, List<String>> filesToIngest) { + Map<SchemaVersion, List<DocumentContext>> result = new HashMap<>(); + + for (Entry<SchemaVersion, List<String>> verFiles : filesToIngest.entrySet()) { + SchemaVersion v = verFiles.getKey(); + List<String> files = verFiles.getValue(); + + List<DocumentContext> docs = new ArrayList<>(); + for (String rulesFilename : files) { + String fileContents = readInJsonFile(rulesFilename); + docs.add(JsonPath.parse(fileContents)); + } + result.put(v, docs); + } + + return result; + } + + public Map<SchemaVersion, List<DocumentContext>> ingestContent(Map<SchemaVersion, List<String>> filesToIngest) { + Map<SchemaVersion, List<DocumentContext>> result = new HashMap<>(); + + for (Entry<SchemaVersion, List<String>> verFiles : filesToIngest.entrySet()) { + SchemaVersion v = verFiles.getKey(); + List<String> files = verFiles.getValue(); + + List<DocumentContext> docs = new ArrayList<>(); + for (String jsonPayload : files) { + docs.add(JsonPath.parse(jsonPayload)); + } + result.put(v, docs); + } + return result; + } + + /** + * 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 + */ + public 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) { + LOGGER.warn("Exception in file"+e.getMessage()); + throw new ExceptionInInitializerError(e); + } + return sb.toString(); + } } 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 index 3ddced9a..da79d9c7 100644 --- 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 @@ -1,4 +1,4 @@ -/** +/** * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ @@ -25,7 +25,7 @@ public class AmbiguousRuleChoiceException extends Exception { super(msg); } - public AmbiguousRuleChoiceException(Throwable throwable){ - super(throwable); + public AmbiguousRuleChoiceException(Throwable throwable){ + super(throwable); } } 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 index de022bc5..f9b9b643 100644 --- 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 @@ -1,4 +1,4 @@ -/** +/** * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ @@ -25,7 +25,7 @@ public class EdgeRuleNotFoundException extends Exception { super(msg); } - public EdgeRuleNotFoundException(Throwable throwable){ - super(throwable); + public EdgeRuleNotFoundException(Throwable throwable){ + super(throwable); } } 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 index 95232386..1945a1ef 100644 --- 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 @@ -2,13 +2,15 @@ * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ - * Copyright © 2017-18 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Modifications Copyright © 2018 IBM. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -20,210 +22,256 @@ package org.onap.aai.nodes; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.util.*; -import java.io.ByteArrayInputStream; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import javax.xml.XMLConstants; -import javax.xml.bind.JAXBException; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; - +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.google.common.base.CaseFormat; 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.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.Translator; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; -import com.google.common.base.CaseFormat; +import javax.annotation.PostConstruct; +import javax.xml.XMLConstants; +import javax.xml.bind.JAXBException; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.io.*; +import java.nio.charset.StandardCharsets; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; @Component -/** - * NodeIngestor - ingests A&AI OXM files per given config, serves DynamicJAXBContext per version +/* + NodeIngestor - ingests A&AI OXM files per given config, serves DynamicJAXBContext per version */ +@PropertySource(value = "classpath:schema-ingest.properties", ignoreResourceNotFound=true) +@PropertySource(value = "file:${schema.ingest.file}", ignoreResourceNotFound=true) public class NodeIngestor { + private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(NodeIngestor.class); + private static final Pattern classNamePattern = Pattern.compile("\\.(v\\d+)\\."); + Map<SchemaVersion, List<String>> filesToIngest; + private Map<SchemaVersion, DynamicJAXBContext> versionContextMap = new TreeMap<>(); + private Map<SchemaVersion, Set<String>> typesPerVersion = new TreeMap<>(); + private Map<SchemaVersion, Document> schemaPerVersion = new TreeMap<>(); + private String localSchema; + private SchemaVersions schemaVersions; + private Set<Translator> translators; + + //TODO : See if you can get rid of InputStream resets + /** + * Instantiates the NodeIngestor bean. + * + * @param - ConfigTranslator autowired in by Spring framework which + * contains the configuration information needed to ingest the desired files. + */ - private Map<SchemaVersion, DynamicJAXBContext> versionContextMap = new TreeMap<>(); - private Map<SchemaVersion, Set<String>> typesPerVersion = new TreeMap<>(); - private Map<SchemaVersion, Document> schemaPerVersion = new TreeMap<>(); - private static final Pattern classNamePattern = Pattern.compile("\\.(v\\d+)\\."); - - private ConfigTranslator translator; - - - @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) { - this.translator = translator; - Map<SchemaVersion, List<String>> filesToIngest = translator.getNodeFiles(); - - try { - for (Entry<SchemaVersion, List<String>> verFiles : filesToIngest.entrySet()) { - SchemaVersion v = verFiles.getKey(); - List<String> files = verFiles.getValue(); - final DynamicJAXBContext ctx = ingest(files); - versionContextMap.put(v, ctx); - typesPerVersion.put(v, getAllNodeTypes(files)); - schemaPerVersion.put(v, createCombinedSchema(files, v)); - } - } catch (JAXBException | ParserConfigurationException | SAXException | IOException 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); - } - - - - private Set<String> getAllNodeTypes(List<String> files) throws ParserConfigurationException, SAXException, IOException { - Set<String> types = new HashSet<>(); - final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); - docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); - final DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); - - ArrayList<Node> javaTypes = new ArrayList<Node>(); - for (String file : files) { - InputStream inputStream = new FileInputStream(file); - - final Document doc = docBuilder.parse(inputStream); - final NodeList list = doc.getElementsByTagName("java-type"); - - for (int i = 0; i < list.getLength(); i++) { - String type = list.item(i).getAttributes().getNamedItem("name").getNodeValue(); - javaTypes.add(list.item(i)); - types.add(CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, type)); - } - } - - return types; - } - - private Document createCombinedSchema(List<String> files,SchemaVersion v) throws ParserConfigurationException, SAXException, IOException { - final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); - docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); - final DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); - DocumentBuilder masterDocBuilder = docFactory.newDocumentBuilder(); - Document combinedDoc = masterDocBuilder.parse(getShell(v)); - NodeList masterList = combinedDoc.getElementsByTagName("java-types"); - Node javaTypesContainer = masterList.getLength() == 0 ? combinedDoc.getDocumentElement() : masterList.item(0); - for (String file : files) { - InputStream inputStream = new FileInputStream(file); - - final Document doc = docBuilder.parse(inputStream); - final NodeList list = doc.getElementsByTagName("java-type"); - for (int i = 0; i < list.getLength(); i++) { - Node copy = combinedDoc.importNode(list.item(i),true); - javaTypesContainer.appendChild(copy); - } - } - return combinedDoc; - } - - /** - * Gets the DynamicJAXBContext for the given version - * - * @param v - * @return DynamicJAXBContext - */ - public DynamicJAXBContext getContextForVersion(SchemaVersion v) { - return versionContextMap.get(v); - } - - /** - * Determines if the given version contains the given node type - * - * @param nodeType - node type to check, must be in lower hyphen form (ie "type-name") - * @param v - schema version to check against - * @return - */ - public boolean hasNodeType(String nodeType, SchemaVersion v) { - return typesPerVersion.get(v).contains(nodeType); - } - - public Set<String> getObjectsInVersion(SchemaVersion v){ - return typesPerVersion.get(v); - } - /** - * Determines if the given version contains the given node type - * - * @param String nodeType - node type to check, must be in lower hyphen form (ie "type-name") - * @param v - * @return - */ - public Document getSchema(SchemaVersion v) { - return schemaPerVersion.get(v); - } - - private InputStream getShell(SchemaVersion v) { - String source = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + - "<xml-bindings xmlns=\"http://www.eclipse.org/eclipselink/xsds/persistence/oxm\" package-name=\"inventory.aai.onap.org."+v.toString().toLowerCase()+"\" xml-mapping-metadata-complete=\"true\">\n" + - " <xml-schema element-form-default=\"QUALIFIED\">\n" + - " <xml-ns namespace-uri=\"http://org.onap.aai.inventory/"+v.toString().toLowerCase()+"\" />\n" + - " </xml-schema>\n" + - " <java-types>\n" + - " </java-types>\\n" + - "</xml-bindings>"; -// source.rep.replace("v11", v.toString()); - return new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8)); - } - - - public SchemaVersion getVersionFromClassName (String classname) { - Matcher m = classNamePattern.matcher(classname); - String version = null; - if (m.find()) { - version = m.group(1); - return new SchemaVersion(version); - } else { - return translator.getSchemaVersions().getDefaultVersion(); - } - } -} + @Autowired + public NodeIngestor(Set<Translator> translatorSet) { + LOGGER.debug("Local Schema files will be fetched"); + this.translators = translatorSet; + } + @PostConstruct + public void initialize() { + + for (Translator translator : translators) { + try { + LOGGER.debug("Processing the translator"); + translateAll(translator); + + } catch (Exception e) { + LOGGER.info("Error while Processing the translator" + e.getMessage()); + continue; + } + } + if (versionContextMap.isEmpty() || schemaPerVersion.isEmpty() || typesPerVersion.isEmpty()) { + throw new ExceptionInInitializerError(); + } + } + + private void translateAll(Translator translator) throws ExceptionInInitializerError { + if (translator instanceof ConfigTranslator) { + this.localSchema = "true"; + } + + Boolean retrieveLocalSchema = Boolean.parseBoolean(this.localSchema); + /* + * Set this to default schemaVersion + */ + this.schemaVersions = translator.getSchemaVersions(); + List<SchemaVersion> schemaVersionList = translator.getSchemaVersions().getVersions(); + + try { + for (SchemaVersion version : schemaVersionList) { + LOGGER.debug("Version being processed" + version); + List<InputStream> inputStreams = retrieveOXM(version, translator); + LOGGER.debug("Retrieved OXMs from SchemaService"); + /* + IOUtils.copy and copy the inputstream + */ + if (inputStreams.isEmpty()) { + continue; + } + + final DynamicJAXBContext ctx = ingest(inputStreams); + versionContextMap.put(version, ctx); + typesPerVersion.put(version, getAllNodeTypes(inputStreams)); + schemaPerVersion.put(version, createCombinedSchema(inputStreams, version, retrieveLocalSchema)); + } + } catch (JAXBException | ParserConfigurationException | SAXException | IOException e) { + throw new ExceptionInInitializerError(e); + } + } + + /** + * Ingests the given OXM files into DynamicJAXBContext + * + * @param inputStreams - inputStrean of oxms from SchemaService to be ingested + * + * @return DynamicJAXBContext including schema information from all given files + * + * @throws JAXBException if there's an error creating the DynamicJAXBContext + */ + private DynamicJAXBContext ingest(List<InputStream> inputStreams) throws JAXBException { + Map<String, Object> properties = new HashMap<>(); + properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, inputStreams); + LOGGER.debug("Ingested the InputStream"); + return DynamicJAXBContextFactory.createContextFromOXM(this.getClass().getClassLoader(), properties); + } + + private Set<String> getAllNodeTypes(List<InputStream> inputStreams) throws ParserConfigurationException, SAXException, IOException { + //Reset the InputStream to reset the offset to inital position + Set<String> types = new HashSet<>(); + final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); + docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); + final DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); + + for (InputStream inputStream : inputStreams) { + //TODO Change this + inputStream.reset(); + final Document doc = docBuilder.parse(inputStream); + final NodeList list = doc.getElementsByTagName("java-type"); + + for (int i = 0; i < list.getLength(); i++) { + String type = list.item(i).getAttributes().getNamedItem("name").getNodeValue(); + types.add(CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, type)); + } + } + + LOGGER.debug("Types size" + types.size()); + return types; + } + + private Document createCombinedSchema(List<InputStream> inputStreams, SchemaVersion version, boolean localSchema) throws ParserConfigurationException, SAXException, IOException { + if (localSchema) { + return createCombinedSchema(inputStreams, version); + } + + InputStream inputStream = inputStreams.get(0); + inputStream.reset(); + final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); + docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); + DocumentBuilder masterDocBuilder = docFactory.newDocumentBuilder(); + return masterDocBuilder.parse(inputStream); + } + + private Document createCombinedSchema(List<InputStream> inputStreams, SchemaVersion version) throws ParserConfigurationException, SAXException, IOException { + final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); + docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); + final DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); + DocumentBuilder masterDocBuilder = docFactory.newDocumentBuilder(); + Document combinedDoc = masterDocBuilder.parse(getShell(version)); + NodeList masterList = combinedDoc.getElementsByTagName("java-types"); + Node javaTypesContainer = masterList.getLength() == 0 ? combinedDoc.getDocumentElement() : masterList.item(0); + + for (InputStream inputStream : inputStreams) { + inputStream.reset(); + final Document doc = docBuilder.parse(inputStream); + final NodeList list = doc.getElementsByTagName("java-type"); + for (int i = 0; i < list.getLength(); i++) { + Node copy = combinedDoc.importNode(list.item(i), true); + javaTypesContainer.appendChild(copy); + } + } + return combinedDoc; + } + + /** + * Gets the DynamicJAXBContext for the given version + * + * @param v - schema version to retrieve the context + * @return DynamicJAXBContext + */ + public DynamicJAXBContext getContextForVersion(SchemaVersion v) { + return versionContextMap.get(v); + } + + /** + * Determines if the given version contains the given node type + * + * @param nodeType - node type to check, must be in lower hyphen form (ie "type-name") + * @param v - schema version to check against + * @return boolean + */ + public boolean hasNodeType(String nodeType, SchemaVersion v) { + return typesPerVersion.get(v).contains(nodeType); + } + + public Set<String> getObjectsInVersion(SchemaVersion v) { + return typesPerVersion.get(v); + } + + /** + * Determines if the given version contains the given node type + * + * @param v - Schemaversion to retrieve the schema + * @return Document + */ + public Document getSchema(SchemaVersion v) { + return schemaPerVersion.get(v); + } + + + public SchemaVersion getVersionFromClassName(String classname) { + Matcher m = classNamePattern.matcher(classname); + if (m.find()) { + String version = m.group(1); + return new SchemaVersion(version); + } else { + return this.schemaVersions.getDefaultVersion(); + } + } + + private List<InputStream> retrieveOXM(SchemaVersion version, Translator translator) throws IOException { + /* + Call Schema MS to get versions using RestTemplate or Local + */ + return translator.getVersionNodeStream(version); + + } + + private InputStream getShell(SchemaVersion v) { + String source = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + + "<xml-bindings xmlns=\"http://www.eclipse.org/eclipselink/xsds/persistence/oxm\" package-name=\"inventory.aai.onap.org." + v.toString().toLowerCase() + "\" xml-mapping-metadata-complete=\"true\">\n" + + " <xml-schema element-form-default=\"QUALIFIED\">\n" + + " <xml-ns namespace-uri=\"http://org.onap.aai.inventory/" + v.toString().toLowerCase() + "\" />\n" + + " </xml-schema>\n" + + " <java-types>\n" + + " </java-types>\n" + + "</xml-bindings>"; + return new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8)); + } +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/restclient/SchemaServiceNoAuthClient.java b/aai-schema-ingest/src/main/java/org/onap/aai/restclient/SchemaServiceNoAuthClient.java new file mode 100644 index 00000000..07f522b4 --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/restclient/SchemaServiceNoAuthClient.java @@ -0,0 +1,75 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2018-19 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========================================================= + */ +package org.onap.aai.restclient; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Component; +import org.springframework.util.MultiValueMap; + +import javax.annotation.PostConstruct; +import java.util.Collections; +import java.util.Map; +import java.util.UUID; + +@Component(value="no-auth-service-rest-client") +public class SchemaServiceNoAuthClient extends NoAuthRestClient{ + + private static EELFLogger logger = EELFManager.getInstance().getLogger(SchemaServiceNoAuthClient.class); + + @Value("${schema.service.base.url}") + private String baseUrl; + + @PostConstruct + public void init () throws Exception { + super.init(); + } + + @Override + public String getBaseUrl() { + return baseUrl; + } + + @Override + public MultiValueMap<String, String> getHeaders(Map<String, String> headers) { + HttpHeaders httpHeaders = new HttpHeaders(); + + String defaultAccept = headers.getOrDefault(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON.toString()); + String defaultContentType = headers.getOrDefault(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON.toString()); + + if(headers.isEmpty()){ + httpHeaders.setAccept(Collections.singletonList(MediaType.parseMediaType(defaultAccept))); + httpHeaders.setContentType(MediaType.parseMediaType(defaultContentType)); + } + + httpHeaders.add("X-FromAppId", appName); + httpHeaders.add("X-TransactionId", UUID.randomUUID().toString()); + headers.forEach(httpHeaders::add); + return httpHeaders; + } + + @Override + public EELFLogger getLogger() { + return logger; + } +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/restclient/SchemaServiceOneWayClient.java b/aai-schema-ingest/src/main/java/org/onap/aai/restclient/SchemaServiceOneWayClient.java new file mode 100644 index 00000000..3aa3b1da --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/restclient/SchemaServiceOneWayClient.java @@ -0,0 +1,87 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2018-19 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========================================================= + */ +package org.onap.aai.restclient; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Component; +import org.springframework.util.MultiValueMap; + +import javax.annotation.PostConstruct; +import java.util.Collections; +import java.util.Map; +import java.util.UUID; + +@Component(value="schema-service-one-way-rest-client") +public class SchemaServiceOneWayClient extends OneWaySSLRestClient{ + + private static EELFLogger logger = EELFManager.getInstance().getLogger(SchemaServiceOneWayClient.class); + + @Value("${schema.service.base.url}") + private String baseUrl; + + @Value("${schema.service.ssl.trust-store}") + private String truststorePath; + + @Value("${schema.service.ssl.trust-store-password}") + private String truststorePassword; + + @Override + public String getBaseUrl() { + return baseUrl; + } + + @Override + protected String getTruststorePath() { + return truststorePath; + } + + @Override + protected char[] getTruststorePassword() { + return truststorePassword.toCharArray(); + } + + @Override + public MultiValueMap<String, String> getHeaders(Map<String, String> headers) { + HttpHeaders httpHeaders = new HttpHeaders(); + + String defaultAccept = headers.getOrDefault(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON.toString()); + String defaultContentType = headers.getOrDefault(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON.toString()); + + if(headers.isEmpty()){ + httpHeaders.setAccept(Collections.singletonList(MediaType.parseMediaType(defaultAccept))); + httpHeaders.setContentType(MediaType.parseMediaType(defaultContentType)); + } + + httpHeaders.add("X-FromAppId", appName); + httpHeaders.add("X-TransactionId", UUID.randomUUID().toString()); + httpHeaders.add("X-TransactionId", appName); + headers.forEach(httpHeaders::add); + return httpHeaders; + } + + @Override + public EELFLogger getLogger() { + return logger; + } +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/restclient/SchemaServiceRestClient.java b/aai-schema-ingest/src/main/java/org/onap/aai/restclient/SchemaServiceRestClient.java new file mode 100644 index 00000000..db1a50b5 --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/restclient/SchemaServiceRestClient.java @@ -0,0 +1,102 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Modifications Copyright © 2018 IBM. + * ================================================================================ + * 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========================================================= + */ +package org.onap.aai.restclient; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Component; +import org.springframework.util.MultiValueMap; + +import java.util.Collections; +import java.util.Map; +import java.util.UUID; + +@Component(value = "schema-service-rest-client") +public class SchemaServiceRestClient extends TwoWaySSLRestClient { + private static EELFLogger logger = EELFManager.getInstance().getLogger(SchemaServiceRestClient.class); + + @Value("${schema.service.base.url}") + private String baseUrl; + + @Value("${schema.service.ssl.key-store}") + private String keystorePath; + + @Value("${schema.service.ssl.trust-store}") + private String truststorePath; + + @Value("${schema.service.ssl.key-store-password}") + private String keystorePassword; + + @Value("${schema.service.ssl.trust-store-password}") + private String truststorePassword; + + @Override + public String getBaseUrl() { + return baseUrl; + } + + @Override + protected String getKeystorePath() { + return keystorePath; + } + + @Override + protected String getTruststorePath() { + return truststorePath; + } + + @Override + protected char[] getKeystorePassword() { + return keystorePassword.toCharArray(); + } + + @Override + protected char[] getTruststorePassword() { + return truststorePassword.toCharArray(); + } + + @Override + public MultiValueMap<String, String> getHeaders(Map<String, String> headers) { + HttpHeaders httpHeaders = new HttpHeaders(); + + String defaultAccept = headers.getOrDefault(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON.toString()); + String defaultContentType = headers.getOrDefault(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON.toString()); + + if (headers.isEmpty()) { + httpHeaders.setAccept(Collections.singletonList(MediaType.parseMediaType(defaultAccept))); + httpHeaders.setContentType(MediaType.parseMediaType(defaultContentType)); + } + + httpHeaders.add("X-FromAppId", appName); + httpHeaders.add("X-TransactionId", UUID.randomUUID().toString()); + headers.forEach(httpHeaders::add); + return httpHeaders; + } + + @Override + public EELFLogger getLogger() { + return logger; + } +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/setup/AAIConfigTranslator.java b/aai-schema-ingest/src/main/java/org/onap/aai/setup/AAIConfigTranslator.java index 2cb0c99b..899a520d 100644 --- a/aai-schema-ingest/src/main/java/org/onap/aai/setup/AAIConfigTranslator.java +++ b/aai-schema-ingest/src/main/java/org/onap/aai/setup/AAIConfigTranslator.java @@ -20,7 +20,10 @@ package org.onap.aai.setup; import java.io.File; -import java.util.*; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -43,7 +46,7 @@ public class AAIConfigTranslator extends ConfigTranslator { */ @Override public Map<SchemaVersion, List<String>> getNodeFiles() { - + Map<SchemaVersion, List<String>> files = new TreeMap<>(); for (SchemaVersion v : schemaVersions.getVersions()) { List<String> container = getVersionNodeFiles(v); @@ -55,7 +58,6 @@ public class AAIConfigTranslator extends ConfigTranslator { private List<String> getVersionNodeFiles(SchemaVersion v) { - return getVersionFiles( bean.getNodeDirectory(), v, @@ -99,7 +101,6 @@ public class AAIConfigTranslator extends ConfigTranslator { List<String> container; final String directoryName = startDirectory + FILESEP + schemaVersion.toString() + FILESEP; - container = Arrays.stream(new File(directoryName).listFiles()) .map(File::getName) .filter(name -> inclusionPattern.get().anyMatch(name::matches)) 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 index ccbe7065..4e6a6bdf 100644 --- 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 @@ -20,25 +20,35 @@ package org.onap.aai.setup; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.apache.commons.io.IOUtils; +import org.onap.aai.edges.JsonIngestor; +import org.springframework.beans.factory.annotation.Autowired; + +import java.io.*; +import java.nio.charset.Charset; +import java.util.ArrayList; 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 { +public abstract class ConfigTranslator extends Translator{ + private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(ConfigTranslator.class); + protected SchemaLocationsBean bean; - protected SchemaVersions schemaVersions; + @Autowired public ConfigTranslator(SchemaLocationsBean schemaLocationbean, SchemaVersions schemaVersions) { + super(schemaVersions); this.bean = schemaLocationbean; - this.schemaVersions = schemaVersions; + } /** @@ -49,6 +59,49 @@ public abstract class ConfigTranslator { * ingested for that version */ public abstract Map<SchemaVersion, List<String>> getNodeFiles(); + + public List<InputStream> getVersionNodeStream(SchemaVersion version) { + + Map<SchemaVersion, List<String>> filesToIngest = getNodeFiles(); + List<InputStream> streams = new ArrayList<>(); + + if(!filesToIngest.containsKey(version)) { + return streams; + } + List<String> versionFiles = filesToIngest.get(version); + + for (String name : versionFiles) { + try { + InputStream stream = new FileInputStream(new File(name)); + String value = IOUtils.toString(stream, Charset.defaultCharset()); + InputStream bis =(IOUtils.toInputStream(value, Charset.defaultCharset())); + streams.add(bis); + } catch (FileNotFoundException e) { + //TODO This may have to be cascaded + LOGGER.warn("File Not Found"+e.getMessage()); + } catch (IOException e) { + LOGGER.warn("IOException while reading files"+e.getMessage()); + } + } + return streams; + } + + @Override + public List<String> getJsonPayload(SchemaVersion version) { + Map<SchemaVersion, List<String>> filesToIngest = getEdgeFiles(); + List<String> jsonPayloads = new ArrayList<>(); + if(!filesToIngest.containsKey(version)) { + return jsonPayloads; + } + List<String> versionFiles = filesToIngest.get(version); + JsonIngestor ji = new JsonIngestor(); + for (String rulesFilename : versionFiles) { + jsonPayloads.add(ji.readInJsonFile(rulesFilename)); + + } + + return jsonPayloads; + } /** * Translates the contents of the schema config file @@ -59,7 +112,4 @@ public abstract class ConfigTranslator { */ public abstract Map<SchemaVersion, List<String>> getEdgeFiles(); - public SchemaVersions getSchemaVersions(){ - return schemaVersions; - } } 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 index e3cd7236..9e740a8c 100644 --- 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 @@ -26,7 +26,6 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; -import java.util.Collections; import java.util.List; @Configuration diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaServiceTranslator.java b/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaServiceTranslator.java new file mode 100644 index 00000000..21082fa1 --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaServiceTranslator.java @@ -0,0 +1,105 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 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========================================================= + */ +package org.onap.aai.setup; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.onap.aai.restclient.RestClient; +import org.onap.aai.restclient.RestClientFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.Resource; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; + +import javax.ws.rs.HttpMethod; +import java.io.*; +import java.util.*; + +/** + * <b>AAIConfigTranslator</b> is responsible for looking at the schema files and + * edge files based on the available versions Also has the ability to exclude + * them based on the node.exclusion.pattern + */ +public class SchemaServiceTranslator extends Translator { + + private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(SchemaServiceTranslator.class); + + private static final String SchemaServiceClientType = "schema.service"; + + @Value("${schema.service.nodes.endpoint}") + private String nodeSchemaUri; + + @Value("${schema.service.edges.endpoint}") + private String edgeSchemaUri; + + @Autowired + private RestClientFactory restClientFactory; + + public SchemaServiceTranslator(SchemaVersions schemaVersions) { + super(schemaVersions); + } + + /* + * (non-Javadoc) + * + * @see org.onap.aai.setup.ConfigTranslator#getNodeFiles() + */ + + @Override + public List<InputStream> getVersionNodeStream(SchemaVersion version) throws IOException { + + List<InputStream> inputStreams = new ArrayList<>(); + String content = ""; + String uri = nodeSchemaUri + version.toString(); + Map<String, String> headersMap = new HashMap<>(); + + headersMap.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML.toString()); + headersMap.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML.toString()); + RestClient restClient = restClientFactory.getRestClient(SchemaServiceClientType); + ResponseEntity<Resource> schemaResponse = restClient.getGetResource(content, uri, + headersMap); + LOGGER.debug("SchemaResponse Status code" + schemaResponse.getStatusCode()); + inputStreams.add(schemaResponse.getBody().getInputStream()); + return inputStreams; + } + + @Override + public List<String> getJsonPayload(SchemaVersion version) throws IOException { + /* + * Call Schema MS to get versions using RestTemplate + */ + List<String> inputStreams = new ArrayList<>(); + String content = ""; + String uri = edgeSchemaUri + version.toString(); + Map<String, String> headersMap = new HashMap<>(); + + RestClient restClient = restClientFactory.getRestClient(SchemaServiceClientType); + + ResponseEntity<String> schemaResponse = restClient.getGetRequest(content, uri, + headersMap); + LOGGER.debug("SchemaResponse Status code" + schemaResponse.getStatusCode()); + inputStreams.add(schemaResponse.getBody()); + return inputStreams; + + } + +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaServiceVersions.java b/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaServiceVersions.java new file mode 100644 index 00000000..6594f232 --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaServiceVersions.java @@ -0,0 +1,48 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-18 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========================================================= + */ +package org.onap.aai.setup; + +import javax.annotation.PostConstruct; +import java.util.List; +import java.util.stream.Collectors; + +public class SchemaServiceVersions extends SchemaVersions { + private List<String> versions; + private String edgeVersion; + private String defaultVersion; + private String depthVersion; + private String appRootVersion; + private String relatedLinkVersion; + private String namespaceChangeVersion; + + + @PostConstruct + public void initializeFromSchemaService() { + versionsValue = versions.stream().map(SchemaVersion::new).collect(Collectors.toList()); + edgeLabelVersionValue = new SchemaVersion(edgeVersion); + defaultVersionValue = new SchemaVersion(defaultVersion); + depthVersionValue = new SchemaVersion(depthVersion); + appRootVersionValue = new SchemaVersion(appRootVersion); + relatedLinkVersionValue = new SchemaVersion(relatedLinkVersion); + namespaceChangeVersionValue = new SchemaVersion(namespaceChangeVersion); + this.validate(); + } + +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaVersion.java b/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaVersion.java index c744c5a3..a1a40e69 100644 --- a/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaVersion.java +++ b/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaVersion.java @@ -21,7 +21,6 @@ package org.onap.aai.setup; import org.onap.aai.validation.AAISchemaValidationException; -import java.util.Comparator; import java.util.regex.Pattern; public class SchemaVersion implements Comparable<SchemaVersion> { @@ -31,7 +30,6 @@ public class SchemaVersion implements Comparable<SchemaVersion> { private final String value; public SchemaVersion(String value){ - if(!VERSION_PATTERN.matcher(value).matches()){ throw new AAISchemaValidationException("Invalid Schema Version " + value + ", value doesn't match the expected regex: " + VERSION_PATTERN); } diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaVersions.java b/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaVersions.java index 2205b14b..028ebdeb 100644 --- a/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaVersions.java +++ b/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaVersions.java @@ -2,13 +2,13 @@ * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-18 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 + * 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, @@ -25,7 +25,6 @@ import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; -import javax.xml.validation.Schema; import java.util.List; import java.util.stream.Collectors; @@ -36,124 +35,99 @@ public class SchemaVersions { @Value("#{'${schema.version.list}'.split(',')}") private List<String> apiVersions; - @Value("${schema.version.api.default}") private String defaultApiVersion; - @Value("${schema.version.edge.label.start}") private String edgeLabelStartVersion; - @Value("${schema.version.depth.start}") private String depthStartVersion; - @Value("${schema.version.app.root.start}") private String appRootStartVersion; - @Value("${schema.version.related.link.start}") private String relatedLinkStartVersion; - @Value("${schema.version.namespace.change.start}") - private String namespaceChangeStartVersion; - private List<SchemaVersion> versions; - - private SchemaVersion edgeLabelVersion; - private SchemaVersion defaultVersion; - private SchemaVersion depthVersion; - private SchemaVersion appRootVersion; - private SchemaVersion relatedLinkVersion; - private SchemaVersion namespaceChangeVersion; + protected String namespaceChangeStartVersion; + protected List<SchemaVersion> versionsValue; + protected SchemaVersion edgeLabelVersionValue; + protected SchemaVersion defaultVersionValue; + protected SchemaVersion depthVersionValue; + protected SchemaVersion appRootVersionValue; + protected SchemaVersion relatedLinkVersionValue; + protected SchemaVersion namespaceChangeVersionValue; @PostConstruct public void initialize() { - versions = apiVersions.stream().map(SchemaVersion::new).collect(Collectors.toList()); - - edgeLabelVersion = new SchemaVersion(edgeLabelStartVersion); - defaultVersion = new SchemaVersion(defaultApiVersion); - depthVersion = new SchemaVersion(depthStartVersion); - appRootVersion = new SchemaVersion(appRootStartVersion); - relatedLinkVersion = new SchemaVersion(relatedLinkStartVersion); - namespaceChangeVersion = new SchemaVersion(namespaceChangeStartVersion); - - if (!versions.contains(edgeLabelVersion)) { - throw new AAISchemaValidationException( - "Invalid, edge label version is not in the api versions list" + versionsValue = apiVersions.stream().map(SchemaVersion::new).collect(Collectors.toList()); + edgeLabelVersionValue = new SchemaVersion(edgeLabelStartVersion); + defaultVersionValue = new SchemaVersion(defaultApiVersion); + depthVersionValue = new SchemaVersion(depthStartVersion); + appRootVersionValue = new SchemaVersion(appRootStartVersion); + relatedLinkVersionValue = new SchemaVersion(relatedLinkStartVersion); + namespaceChangeVersionValue = new SchemaVersion(namespaceChangeStartVersion); + this.validate(); + } + + + protected void validate() { + String errorMessage = "Invalid, edge label version is not in the api versions list" + ", please check schema.version.list and ensure that the" - + " schema.version.edge.label.start is in that list" - ); + + " schema.version.edge.label.start is in that list"; + if (!versionsValue.contains(edgeLabelVersionValue)) { + throw new AAISchemaValidationException(errorMessage); } - if (!versions.contains(defaultVersion)) { - throw new AAISchemaValidationException( - "Invalid, default version is not in the api versions list" - + ", please check schema.version.list and ensure that the" - + " schema.version.api.default is in that list" - ); + if (!versionsValue.contains(defaultVersionValue)) { + throw new AAISchemaValidationException(errorMessage); } - if (!versions.contains(depthVersion)) { - throw new AAISchemaValidationException( - "Invalid, depth version is not in the api versions list" - + ", please check schema.version.list and ensure that the" - + " schema.version.depth.start is in that list" - ); + if (!versionsValue.contains(depthVersionValue)) { + throw new AAISchemaValidationException(errorMessage); } - if(!versions.contains(appRootVersion)){ - throw new AAISchemaValidationException( - "Invalid, app root version is not in the api versions list" - + ", please check schema.version.list and ensure that the" - + " schema.version.app.root.start is in that list" - ); + if (!versionsValue.contains(appRootVersionValue)) { + throw new AAISchemaValidationException(errorMessage); } - if(!versions.contains(relatedLinkVersion)){ - throw new AAISchemaValidationException( - "Invalid, related link version is not in the api versions list" - + ", please check schema.version.list and ensure that the" - + " schema.version.related.link.start is in that list" - ); + if (!versionsValue.contains(relatedLinkVersionValue)) { + throw new AAISchemaValidationException(errorMessage); } - if(!versions.contains(namespaceChangeVersion)){ - throw new AAISchemaValidationException( - "Invalid, namespace change start version is not in the api versions list" - + ", please check schema.version.list and ensure that the" - + " schema.version.related.link.start is in that list" - ); + if (!versionsValue.contains(namespaceChangeVersionValue)) { + throw new AAISchemaValidationException(errorMessage); } } - + public List<SchemaVersion> getVersions() { - return versions; + return versionsValue; } public SchemaVersion getEdgeLabelVersion() { - return edgeLabelVersion; + return edgeLabelVersionValue; } public SchemaVersion getDefaultVersion() { - return defaultVersion; + return defaultVersionValue; } public SchemaVersion getDepthVersion() { - return depthVersion; + return depthVersionValue; } - public SchemaVersion getAppRootVersion(){ - return appRootVersion; + public SchemaVersion getAppRootVersion() { + return appRootVersionValue; } - public SchemaVersion getRelatedLinkVersion(){ - return relatedLinkVersion; + public SchemaVersion getRelatedLinkVersion() { + return relatedLinkVersionValue; } public SchemaVersion getNamespaceChangeVersion() { - return namespaceChangeVersion; + return namespaceChangeVersionValue; } public void setNamespaceChangeVersion(SchemaVersion namespaceChangeVersion) { - this.namespaceChangeVersion = namespaceChangeVersion; + this.namespaceChangeVersionValue = namespaceChangeVersion; } } diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaVersionsBean.java b/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaVersionsBean.java new file mode 100644 index 00000000..119669d3 --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaVersionsBean.java @@ -0,0 +1,83 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-18 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========================================================= + */ +package org.onap.aai.setup; + +import com.google.gson.FieldNamingPolicy; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import org.onap.aai.restclient.RestClient; +import org.onap.aai.restclient.RestClientFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.ResponseEntity; + +import javax.annotation.PostConstruct; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class SchemaVersionsBean { + + private String SCHEMA_SERVICE = "schema-service"; + private SchemaServiceVersions schemaVersions; + + @Value("${schema.service.versions.endpoint}") + private String versionsUri; + + @Autowired + private RestClientFactory restClientFactory; + + @PostConstruct + public void initialize() { + //Call SchemaService to get versions + retrieveAllSchemaVersions(); + } + + public void retrieveAllSchemaVersions() { + /* + Call Schema MS to get versions using RestTemplate + */ + String content = ""; + Map<String, String> headersMap = new HashMap<>(); + RestClient restClient = restClientFactory + .getRestClient(SCHEMA_SERVICE); + + ResponseEntity<String> schemaResponse = restClient.getGetRequest( content, versionsUri, headersMap); + Gson gson = new GsonBuilder() + .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES) + .create(); + schemaVersions = gson.fromJson(schemaResponse.getBody(), SchemaServiceVersions.class); + schemaVersions.initializeFromSchemaService(); + + } + + public SchemaServiceVersions getSchemaVersions() { + return schemaVersions; + } + + public void setSchemaVersions(SchemaServiceVersions schemaVersions) { + this.schemaVersions = schemaVersions; + } + + public List<SchemaVersion> getVersions() { + return getSchemaVersions().getVersions(); + } + +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/setup/Translator.java b/aai-schema-ingest/src/main/java/org/onap/aai/setup/Translator.java new file mode 100644 index 00000000..09062e1c --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/setup/Translator.java @@ -0,0 +1,67 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-18 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========================================================= + */ + +package org.onap.aai.setup; + +import org.springframework.beans.factory.annotation.Autowired; + +import java.io.*; +import java.util.List; +/** + * 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 Translator { + + protected SchemaVersions schemaVersions; + + public Translator(SchemaVersions schemaVersions) { + this.schemaVersions = schemaVersions; + } + + /** + * 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 List<InputStream> getVersionNodeStream(SchemaVersion version) throws IOException; + + public abstract List<String> + getJsonPayload(SchemaVersion version) throws IOException; + + /** + * 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 SchemaVersions getSchemaVersions(){ + return this.schemaVersions; + } +} diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/validation/DefaultVersionValidationModule.java b/aai-schema-ingest/src/main/java/org/onap/aai/validation/DefaultVersionValidationModule.java index 7eae7508..36176587 100644 --- a/aai-schema-ingest/src/main/java/org/onap/aai/validation/DefaultVersionValidationModule.java +++ b/aai-schema-ingest/src/main/java/org/onap/aai/validation/DefaultVersionValidationModule.java @@ -38,39 +38,41 @@ import org.springframework.stereotype.Component; */ @Component public class DefaultVersionValidationModule implements VersionValidationModule { - private ConfigTranslator config; - + + private ConfigTranslator config; + @Autowired public DefaultVersionValidationModule(ConfigTranslator config) { - this.config = config; + + this.config = config; } /* (non-Javadoc) * @see org.onap.aai.validation.VersionValidationModule#validate(org.onap.aai.setup.ConfigTranslator) */ - @Override - public String validate() { - Map<SchemaVersion, List<String>> nodeConfig = config.getNodeFiles(); - Map<SchemaVersion, List<String>> edgeConfig = config.getEdgeFiles(); - - StringBuilder missingVers = new StringBuilder().append("Missing schema for the following versions: "); - boolean isMissing = false; - for (SchemaVersion v : config.getSchemaVersions().getVersions()) { - if (nodeConfig.get(v) == null) { - isMissing = true; - missingVers.append(v.toString()).append(" has no OXM configured. "); - } - if (edgeConfig.get(v) == null) { - isMissing = true; - missingVers.append(v.toString()).append(" has no edge rules configured. "); - } - } - - if (isMissing) { - return missingVers.toString(); - } else { - return ""; - } - } + @Override + public String validate() { + Map<SchemaVersion, List<String>> nodeConfig = config.getNodeFiles(); + Map<SchemaVersion, List<String>> edgeConfig = config.getEdgeFiles(); + + StringBuilder missingVers = new StringBuilder().append("Missing schema for the following versions: "); + boolean isMissing = false; + for (SchemaVersion v : config.getSchemaVersions().getVersions()) { + if (nodeConfig.get(v) == null) { + isMissing = true; + missingVers.append(v.toString()).append(" has no OXM configured. "); + } + if (edgeConfig.get(v) == null) { + isMissing = true; + missingVers.append(v.toString()).append(" has no edge rules configured. "); + } + } + + if (isMissing) { + return missingVers.toString(); + } else { + return ""; + } + } } diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/CousinDefaultingValidationModule.java b/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/CousinDefaultingValidationModule.java index 05b79753..d4ab7aeb 100644 --- a/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/CousinDefaultingValidationModule.java +++ b/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/CousinDefaultingValidationModule.java @@ -20,18 +20,13 @@ package org.onap.aai.validation.edges; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - +import com.jayway.jsonpath.DocumentContext; import org.onap.aai.edges.EdgeRuleQuery; import org.onap.aai.edges.EdgeRuleQuery.Builder; import org.onap.aai.edges.enums.EdgeField; import org.onap.aai.edges.enums.EdgeType; -import com.jayway.jsonpath.DocumentContext; +import java.util.*; /** * Validates that in the collection of cousin rules between a given node type pair, diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/DefaultEdgeFieldsValidationModule.java b/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/DefaultEdgeFieldsValidationModule.java index 01e9e296..9a7288b0 100644 --- a/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/DefaultEdgeFieldsValidationModule.java +++ b/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/DefaultEdgeFieldsValidationModule.java @@ -23,12 +23,11 @@ */ package org.onap.aai.validation.edges; +import org.onap.aai.edges.enums.EdgeField; + import java.util.EnumSet; import java.util.Map; import java.util.Map.Entry; -import java.util.Set; - -import org.onap.aai.edges.enums.EdgeField; /** * Default core A&AI edge field validation diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/EdgeRuleValidator.java b/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/EdgeRuleValidator.java index 309d8945..38dcbb7e 100644 --- a/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/EdgeRuleValidator.java +++ b/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/EdgeRuleValidator.java @@ -20,22 +20,18 @@ package org.onap.aai.validation.edges; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - +import com.jayway.jsonpath.DocumentContext; import org.onap.aai.edges.JsonIngestor; import org.onap.aai.edges.TypeAlphabetizer; import org.onap.aai.edges.enums.EdgeField; + import org.onap.aai.setup.ConfigTranslator; import org.onap.aai.setup.SchemaVersion; import org.onap.aai.validation.SchemaErrorStrategy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import com.jayway.jsonpath.DocumentContext; +import java.util.*; /** * Runs all validations against the ingested schema @@ -49,21 +45,22 @@ public class EdgeRuleValidator { protected final SingleContainmentValidationModule containsValidator; protected final CousinDefaultingValidationModule defaultsValidator; protected final NodeTypesValidationModule typeValidator; - - @Autowired - public EdgeRuleValidator(ConfigTranslator config, SchemaErrorStrategy strat, - EdgeFieldsValidationModule fieldValidator, UniqueLabelValidationModule labelValidator, - SingleContainmentValidationModule containsValidator, CousinDefaultingValidationModule defaultsValidator, - NodeTypesValidationModule typeValidator) { - this.versionJsonFilesMap = new JsonIngestor().ingest(config.getEdgeFiles()); - this.strat = strat; - this.fieldValidator = fieldValidator; - this.labelValidator = labelValidator; - this.containsValidator = containsValidator; - this.defaultsValidator = defaultsValidator; - this.typeValidator = typeValidator; - } - + + @Autowired + public EdgeRuleValidator(ConfigTranslator config, SchemaErrorStrategy strat, + EdgeFieldsValidationModule fieldValidator, UniqueLabelValidationModule labelValidator, + SingleContainmentValidationModule containsValidator, CousinDefaultingValidationModule defaultsValidator, + NodeTypesValidationModule typeValidator) { + //TODO - Need to change this to use files/schemaservice + this.versionJsonFilesMap = new JsonIngestor().ingest(config.getEdgeFiles()); + this.strat = strat; + this.fieldValidator = fieldValidator; + this.labelValidator = labelValidator; + this.containsValidator = containsValidator; + this.defaultsValidator = defaultsValidator; + this.typeValidator = typeValidator; + } + public boolean validate() { for (Map.Entry<SchemaVersion, List<DocumentContext>> verEntry : versionJsonFilesMap.entrySet()) { diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/NodeTypesValidationModule.java b/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/NodeTypesValidationModule.java index b4ed3782..e8c45063 100644 --- a/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/NodeTypesValidationModule.java +++ b/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/NodeTypesValidationModule.java @@ -20,15 +20,16 @@ package org.onap.aai.validation.edges; -import java.util.Collection; -import java.util.HashSet; -import java.util.Set; import org.onap.aai.nodes.NodeIngestor; import org.onap.aai.setup.SchemaVersion; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + /** * Validates that the node types appearing in the edge rules are valid * against the ingested OXM. diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/SingleContainmentValidationModule.java b/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/SingleContainmentValidationModule.java index 4586ccce..ad2cffeb 100644 --- a/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/SingleContainmentValidationModule.java +++ b/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/SingleContainmentValidationModule.java @@ -20,14 +20,13 @@ package org.onap.aai.validation.edges; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - +import com.jayway.jsonpath.DocumentContext; import org.onap.aai.edges.EdgeRuleQuery; import org.onap.aai.edges.enums.EdgeType; -import com.jayway.jsonpath.DocumentContext; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; /** * Validates that the given node type pair has at most one containment relationship diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/UniqueLabelValidationModule.java b/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/UniqueLabelValidationModule.java index 103baaba..35d74667 100644 --- a/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/UniqueLabelValidationModule.java +++ b/aai-schema-ingest/src/main/java/org/onap/aai/validation/edges/UniqueLabelValidationModule.java @@ -20,17 +20,12 @@ package org.onap.aai.validation.edges; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - +import com.jayway.jsonpath.DocumentContext; import org.onap.aai.edges.EdgeRuleQuery; import org.onap.aai.edges.EdgeRuleQuery.Builder; import org.onap.aai.edges.enums.EdgeField; -import com.jayway.jsonpath.DocumentContext; +import java.util.*; /** * Applies label validation rules diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/validation/nodes/NodeValidator.java b/aai-schema-ingest/src/main/java/org/onap/aai/validation/nodes/NodeValidator.java index 27a69f9b..38b3a70c 100644 --- a/aai-schema-ingest/src/main/java/org/onap/aai/validation/nodes/NodeValidator.java +++ b/aai-schema-ingest/src/main/java/org/onap/aai/validation/nodes/NodeValidator.java @@ -31,13 +31,14 @@ import org.springframework.stereotype.Component; @Component public class NodeValidator { - private ConfigTranslator translator; + + private ConfigTranslator translator; private SchemaErrorStrategy strat; private DuplicateNodeDefinitionValidationModule dupChecker; @Autowired - public NodeValidator(ConfigTranslator translator, SchemaErrorStrategy strategy, DuplicateNodeDefinitionValidationModule dupChecker) { - this.translator = translator; + public NodeValidator( ConfigTranslator translator, SchemaErrorStrategy strategy, DuplicateNodeDefinitionValidationModule dupChecker) { + this.translator = translator; this.strat = strategy; this.dupChecker = dupChecker; } |