diff options
Diffstat (limited to 'aai-schema-ingest/src/main')
18 files changed, 340 insertions, 100 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 index 9f28cf8f..11f0da6a 100644 --- 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 @@ -21,6 +21,7 @@ */ package org.onap.aai.config; +import org.onap.aai.setup.SchemaConfigVersions; import org.onap.aai.setup.SchemaLocationsBean; import org.onap.aai.setup.SchemaVersions; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -34,9 +35,14 @@ import org.springframework.context.annotation.PropertySource; @PropertySource(value = "file:${schema.ingest.file}", ignoreResourceNotFound = true) public class ConfigConfiguration { + @Bean(name = "schemaConfigVersions") + public SchemaConfigVersions schemaConfigVersions() { + return new SchemaConfigVersions(); + } + @Bean(name = "schemaVersions") public SchemaVersions schemaVersions() { - return new SchemaVersions(); + return schemaConfigVersions(); } @Bean(name = "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 index e492d213..605dc0cf 100644 --- 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 @@ -22,10 +22,10 @@ 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.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -39,6 +39,7 @@ 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 { @@ -70,6 +71,7 @@ public class EdgesConfiguration { } @Bean(name = "edgeIngestor") + @ConditionalOnExpression("'${schema.translators.needed:all}'.contains('edges') || '${schema.translators.needed:all}'.contains('all')") 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 index 50738df9..8c520aaa 100644 --- 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 @@ -24,10 +24,10 @@ 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.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -38,9 +38,11 @@ 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 { @@ -75,6 +77,7 @@ public class NodesConfiguration { } @Bean(name = "nodeIngestor") + @ConditionalOnExpression("'${schema.translators.needed:all}'.contains('nodes') || '${schema.translators.needed:all}'.contains('all')") public NodeIngestor nodeIngestor() { return new NodeIngestor(translators()); } 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 index 81ef02a8..c6decb03 100644 --- 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 @@ -21,10 +21,7 @@ */ 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.onap.aai.setup.*; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -41,14 +38,19 @@ public class SchemaServiceConfiguration { return new SchemaVersionsBean(); } + @Bean(name = "schemaServiceVersions") + public SchemaServiceVersions schemaServiceVersions() { + return schemaVersionsBean().getSchemaVersions(); + } + @Bean(name = "schemaVersions") public SchemaVersions schemaVersions() { - return schemaVersionsBean().getSchemaVersions(); + return schemaServiceVersions(); } @Bean(name = "schemaServiceTranslator") public Translator schemaServiceTranslator() { - return new SchemaServiceTranslator(schemaVersions()); + return new SchemaServiceTranslator(schemaServiceVersions()); } } 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 213ff590..5d8c3d55 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 @@ -76,7 +76,6 @@ public class EdgeIngestor { @Autowired public EdgeIngestor(Set<Translator> translatorSet) { - LOGGER.debug("Local Schema files will be fetched"); this.translators = translatorSet; } @@ -89,8 +88,8 @@ public class EdgeIngestor { translateAll(translator); } catch (Exception e) { - LOGGER.debug("Error while Processing the translator" + e.getMessage()); - continue; + LOGGER.error("Error while Processing the translator" + e.getMessage()); + throw new ExceptionInInitializerError("EdgeIngestor could not ingest schema"); } } if (versionJsonFilesMap.isEmpty() || schemaVersions==null ) { @@ -98,7 +97,7 @@ public class EdgeIngestor { } } - public void translateAll(Translator translator) { + public void translateAll(Translator translator) throws ExceptionInInitializerError{ /* Use SchemaVersions from the Translator */ @@ -115,7 +114,8 @@ public class EdgeIngestor { 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()); + LOGGER.error("Error in retrieving the JSON Payload" + e.getMessage()); + throw new ExceptionInInitializerError("EdgeIngestor could not ingest schema"); } if (jsonPayloads == null || jsonPayloads.isEmpty()) { continue; diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/nodes/CaseFormatStore.java b/aai-schema-ingest/src/main/java/org/onap/aai/nodes/CaseFormatStore.java new file mode 100644 index 00000000..69a153da --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/nodes/CaseFormatStore.java @@ -0,0 +1,105 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2019 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.nodes; + +import com.google.common.base.CaseFormat; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +/** + * CaseFormatStore stores the converted strings from + * lower hyphen (example-object) to lower camel case (exampleObject) + * so it avoids the creation of the object for every single request + * and cause an issue with taking too much memory just for the conversion + */ +public class CaseFormatStore { + + private final Map<String, String> lowerHyphenToLowerCamel = new HashMap<>(); + private final Map<String, String> lowerHyphenToUpperCamel = new HashMap<>(); + private final Map<String, String> lowerCamelToLowerHyphen = new HashMap<>(); + private final Map<String, String> upperCamelToLowerHyphen = new HashMap<>(); + + CaseFormatStore(){} + + /** + * Parses the document and creates a lower camel case string + * upper camel string, lower hyphen and lower camel case + * + * @param doc Takes an xml document and adds it to the hash maps as appropriate + */ + void parse(Document doc){ + + // Get the xml-root-element and add those nodes + // with the attribute name and it to the hashmaps + // For the attribute with name, it is going to be lower-hyphen + // If the attribute is javaAttribute then it will be lower camel case + NodeList list = doc.getElementsByTagName("xml-root-element"); + addCaseFormatForNodesAndProperties(list, "name"); + + list = doc.getElementsByTagName("xml-element"); + addCaseFormatForNodesAndProperties(list, "java-attribute"); + + list = doc.getElementsByTagName("xml-any-element"); + addCaseFormatForNodesAndProperties(list, "java-attribute"); + } + + private void addCaseFormatForNodesAndProperties(NodeList list, String attributeName) { + for (int i = 0; i < list.getLength(); i++) { + + String lowerCamel = null; + String lowerHyphen = null; + + if ("java-attribute".equals(attributeName)) { + lowerCamel = list.item(i).getAttributes().getNamedItem(attributeName).getNodeValue(); + lowerHyphen = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, lowerCamel); + } else { + lowerHyphen = list.item(i).getAttributes().getNamedItem(attributeName).getNodeValue(); + lowerCamel = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, lowerHyphen); + } + + String upperCamel = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, lowerHyphen); + lowerHyphenToLowerCamel.put(lowerHyphen, lowerCamel); + lowerHyphenToUpperCamel.put(lowerHyphen, upperCamel); + upperCamelToLowerHyphen.put(upperCamel, lowerHyphen); + lowerCamelToLowerHyphen.put(lowerCamel, lowerHyphen); + } + } + + public Optional<String> fromLowerHyphenToLowerCamel(String value){ + return Optional.ofNullable(lowerHyphenToLowerCamel.get(value)); + } + + public Optional<String> fromLowerHyphenToUpperCamel(String value){ + return Optional.ofNullable(lowerHyphenToUpperCamel.get(value)); + } + + public Optional<String> fromUpperCamelToLowerHyphen(String value){ + return Optional.ofNullable(upperCamelToLowerHyphen.get(value)); + } + + public Optional<String> fromLowerCamelToLowerHyphen(String value){ + return Optional.ofNullable(lowerCamelToLowerHyphen.get(value)); + } + +} 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 69cd51ab..2428a6dd 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 @@ -62,26 +62,25 @@ 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 Map<SchemaVersion, DynamicJAXBContext> versionContextMap = new HashMap<>(); + private Map<SchemaVersion, Set<String>> typesPerVersion = new HashMap<>(); + private Map<SchemaVersion, Document> schemaPerVersion = new HashMap<>(); private String localSchema; private SchemaVersions schemaVersions; private Set<Translator> translators; - + + private CaseFormatStore caseFormatStore; //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. + * @param translatorSet */ @Autowired public NodeIngestor(Set<Translator> translatorSet) { - LOGGER.debug("Local Schema files will be fetched"); this.translators = translatorSet; + this.caseFormatStore = new CaseFormatStore(); } @PostConstruct @@ -93,8 +92,8 @@ public class NodeIngestor { translateAll(translator); } catch (Exception e) { - LOGGER.info("Error while Processing the translator" + e.getMessage()); - continue; + LOGGER.error("Error while Processing the translator" + e.getMessage()); + throw new ExceptionInInitializerError("NodeIngestor could not ingest schema"); } } if (versionContextMap.isEmpty() || schemaPerVersion.isEmpty() || typesPerVersion.isEmpty()) { @@ -128,7 +127,7 @@ public class NodeIngestor { final DynamicJAXBContext ctx = ingest(inputStreams); versionContextMap.put(version, ctx); - typesPerVersion.put(version, getAllNodeTypes(inputStreams)); + setAllTypesAndProperties(version, inputStreams); schemaPerVersion.put(version, createCombinedSchema(inputStreams, version, retrieveLocalSchema)); } } catch (JAXBException | ParserConfigurationException | SAXException | IOException e) { @@ -152,8 +151,7 @@ public class NodeIngestor { 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 + private void setAllTypesAndProperties(SchemaVersion version, List<InputStream> inputStreams) throws ParserConfigurationException, IOException, SAXException { Set<String> types = new HashSet<>(); final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); @@ -164,15 +162,20 @@ public class NodeIngestor { 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)); - } + getAllNodeTypes(list, types); + caseFormatStore.parse(doc); } - LOGGER.debug("Types size" + types.size()); - return types; + LOGGER.debug("Types size {}", types.size()); + typesPerVersion.put(version, types); + } + + private void getAllNodeTypes(NodeList list, Set<String> types){ + + 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)); + } } private Document createCombinedSchema(List<InputStream> inputStreams, SchemaVersion version, boolean localSchema) throws ParserConfigurationException, SAXException, IOException { @@ -274,4 +277,8 @@ public class NodeIngestor { "</xml-bindings>"; return new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8)); } + + public CaseFormatStore getCaseFormatStore(){ + return caseFormatStore; + } } 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 index 07f522b4..83687a76 100644 --- 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 @@ -68,8 +68,4 @@ public class SchemaServiceNoAuthClient extends NoAuthRestClient{ 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 index 3aa3b1da..a3e4de54 100644 --- 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 @@ -80,8 +80,4 @@ public class SchemaServiceOneWayClient extends OneWaySSLRestClient{ 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 index db1a50b5..300c5d83 100644 --- 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 @@ -95,8 +95,4 @@ public class SchemaServiceRestClient extends TwoWaySSLRestClient { 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 899a520d..4454ad27 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 @@ -37,7 +37,7 @@ public class AAIConfigTranslator extends ConfigTranslator { private static final String FILESEP = (System.getProperty("file.separator") == null) ? "/" : System.getProperty("file.separator"); - public AAIConfigTranslator(SchemaLocationsBean bean, SchemaVersions schemaVersions) { + public AAIConfigTranslator(SchemaLocationsBean bean, SchemaConfigVersions schemaVersions) { super(bean, schemaVersions); } 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 4e6a6bdf..3ef76d75 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 @@ -25,7 +25,6 @@ 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; @@ -45,7 +44,7 @@ public abstract class ConfigTranslator extends Translator{ @Autowired - public ConfigTranslator(SchemaLocationsBean schemaLocationbean, SchemaVersions schemaVersions) { + public ConfigTranslator(SchemaLocationsBean schemaLocationbean, SchemaConfigVersions schemaVersions) { super(schemaVersions); this.bean = schemaLocationbean; diff --git a/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaConfigVersions.java b/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaConfigVersions.java new file mode 100644 index 00000000..e23fea26 --- /dev/null +++ b/aai-schema-ingest/src/main/java/org/onap/aai/setup/SchemaConfigVersions.java @@ -0,0 +1,92 @@ +/** + * ============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.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.util.List; +import java.util.stream.Collectors; + +@Component("schemaConfigVersions") +@ConditionalOnExpression("'${schema.translator.list:config}'.contains('config') || '${schema.service.versions.override:false}'.equals('true')") +@PropertySource(value = "classpath:schema-ingest.properties", ignoreResourceNotFound = true) +@PropertySource(value = "file:${schema.ingest.file}", ignoreResourceNotFound = true) +public class SchemaConfigVersions extends 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:''}") + protected String namespaceChangeStartVersion; + + public List<String> getApiVersions() { + return apiVersions; + } + + public String getDefaultApiVersion() { + return defaultApiVersion; + } + + public String getEdgeLabelStartVersion() { + return edgeLabelStartVersion; + } + + public String getDepthStartVersion() { + return depthStartVersion; + } + + public String getAppRootStartVersion() { + return appRootStartVersion; + } + + public String getRelatedLinkStartVersion() { + return relatedLinkStartVersion; + } + + public String getNamespaceChangeStartVersion() { + return namespaceChangeStartVersion; + } + + @PostConstruct + public void initialize() { + 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(); + } + +} 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 index 21082fa1..2d2411fb 100644 --- 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 @@ -27,6 +27,7 @@ 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.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; @@ -77,12 +78,13 @@ public class SchemaServiceTranslator extends Translator { RestClient restClient = restClientFactory.getRestClient(SchemaServiceClientType); ResponseEntity<Resource> schemaResponse = restClient.getGetResource(content, uri, headersMap); - LOGGER.debug("SchemaResponse Status code" + schemaResponse.getStatusCode()); + verifySchemaServiceResponse(schemaResponse.getStatusCode()); + LOGGER.debug("SchemaResponse Status code" + schemaResponse.getStatusCode()); inputStreams.add(schemaResponse.getBody().getInputStream()); return inputStreams; } - @Override + @Override public List<String> getJsonPayload(SchemaVersion version) throws IOException { /* * Call Schema MS to get versions using RestTemplate @@ -96,10 +98,18 @@ public class SchemaServiceTranslator extends Translator { ResponseEntity<String> schemaResponse = restClient.getGetRequest(content, uri, headersMap); - LOGGER.debug("SchemaResponse Status code" + schemaResponse.getStatusCode()); + verifySchemaServiceResponse(schemaResponse.getStatusCode()); + LOGGER.debug("SchemaResponse Status code" + schemaResponse.getStatusCode()); inputStreams.add(schemaResponse.getBody()); return inputStreams; } + private void verifySchemaServiceResponse(HttpStatus statusCode) throws IOException { + if (statusCode != HttpStatus.OK) { + LOGGER.error("Please check the Schema Service. It returned with the status code {}", statusCode); + throw new IOException("SchemaService is not available"); + } + } + } 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 index 6594f232..5518ccb2 100644 --- 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 @@ -19,10 +19,17 @@ */ package org.onap.aai.setup; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.util.List; import java.util.stream.Collectors; +@Component("schemaServiceVersions") +@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 SchemaServiceVersions extends SchemaVersions { private List<String> versions; private String edgeVersion; @@ -32,9 +39,17 @@ public class SchemaServiceVersions extends SchemaVersions { private String relatedLinkVersion; private String namespaceChangeVersion; + public List<String> getVersionsAll() { + return versions; + } + + public void setVersions(List<String> versions) { + this.versions = versions; + } @PostConstruct - public void initializeFromSchemaService() { + public void initializeFromSchemaService() throws ExceptionInInitializerError{ + versionsValue = versions.stream().map(SchemaVersion::new).collect(Collectors.toList()); edgeLabelVersionValue = new SchemaVersion(edgeVersion); defaultVersionValue = new SchemaVersion(defaultVersion); @@ -42,7 +57,23 @@ public class SchemaServiceVersions extends SchemaVersions { appRootVersionValue = new SchemaVersion(appRootVersion); relatedLinkVersionValue = new SchemaVersion(relatedLinkVersion); namespaceChangeVersionValue = new SchemaVersion(namespaceChangeVersion); + this.validate(); } + /* + * TODO Change Method names + */ + public void initializeFromSchemaConfig(SchemaConfigVersions schemaConfigVersion) throws ExceptionInInitializerError{ + + versions = schemaConfigVersion.getApiVersions(); + appRootVersion = schemaConfigVersion.getAppRootStartVersion(); + defaultVersion = schemaConfigVersion.getDefaultApiVersion(); + depthVersion = schemaConfigVersion.getDepthStartVersion(); + edgeVersion = schemaConfigVersion.getEdgeLabelStartVersion(); + namespaceChangeVersion = schemaConfigVersion.getNamespaceChangeStartVersion(); + relatedLinkVersion = schemaConfigVersion.getRelatedLinkStartVersion(); + this.initializeFromSchemaService(); + } + } 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 a1a40e69..8143b5e1 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,20 +21,23 @@ package org.onap.aai.setup; import org.onap.aai.validation.AAISchemaValidationException; +import java.util.regex.Matcher; import java.util.regex.Pattern; public class SchemaVersion implements Comparable<SchemaVersion> { - public static final Pattern VERSION_PATTERN = Pattern.compile("v[1-9][0-9]*"); + public static final Pattern VERSION_PATTERN = Pattern.compile("v([1-9][0-9]*)"); - private final String value; + private final Integer value; public SchemaVersion(String value){ - if(!VERSION_PATTERN.matcher(value).matches()){ + Matcher matcher = VERSION_PATTERN.matcher(value); + + if(!matcher.find()){ throw new AAISchemaValidationException("Invalid Schema Version " + value + ", value doesn't match the expected regex: " + VERSION_PATTERN); + } else { + this.value = Integer.parseInt(matcher.group(1)); } - - this.value = value; } @Override @@ -44,6 +47,7 @@ public class SchemaVersion implements Comparable<SchemaVersion> { @Override public boolean equals(Object other){ + if(this == other){ return true; } @@ -62,7 +66,7 @@ public class SchemaVersion implements Comparable<SchemaVersion> { @Override public String toString(){ - return value; + return String.valueOf("v" + value); } @Override @@ -72,12 +76,6 @@ public class SchemaVersion implements Comparable<SchemaVersion> { return -1; } - // Requires to convert to integer to match the past behavior - // Otherwise the string comparison of versions aren't working as expected - - Integer tVal = Integer.parseInt(this.value.replaceAll("v", "")); - Integer oVal = Integer.parseInt(o.value.replaceAll("v", "")); - - return tVal.compareTo(oVal); + return this.value.compareTo(o.value); } } 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 028ebdeb..e7f599c6 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 @@ -28,26 +28,11 @@ import javax.annotation.PostConstruct; import java.util.List; import java.util.stream.Collectors; -@Component + @PropertySource(value = "classpath:schema-ingest.properties", ignoreResourceNotFound = true) @PropertySource(value = "file:${schema.ingest.file}", ignoreResourceNotFound = true) 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}") - - protected String namespaceChangeStartVersion; protected List<SchemaVersion> versionsValue; protected SchemaVersion edgeLabelVersionValue; protected SchemaVersion defaultVersionValue; @@ -56,19 +41,6 @@ public class SchemaVersions { protected SchemaVersion relatedLinkVersionValue; protected SchemaVersion namespaceChangeVersionValue; - @PostConstruct - public void initialize() { - 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" 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 index 119669d3..dde7cad6 100644 --- 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 @@ -27,7 +27,6 @@ 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; @@ -41,16 +40,22 @@ public class SchemaVersionsBean { @Value("${schema.service.versions.endpoint}") private String versionsUri; + @Value("${schema.service.versions.override:false}") + private String overrideSchemaService; + @Autowired private RestClientFactory restClientFactory; + @Autowired(required = false) + private SchemaConfigVersions schemaConfigVersions; + @PostConstruct public void initialize() { //Call SchemaService to get versions retrieveAllSchemaVersions(); } - public void retrieveAllSchemaVersions() { + public void retrieveAllSchemaVersions() throws ExceptionInInitializerError{ /* Call Schema MS to get versions using RestTemplate */ @@ -64,8 +69,28 @@ public class SchemaVersionsBean { .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES) .create(); schemaVersions = gson.fromJson(schemaResponse.getBody(), SchemaServiceVersions.class); - schemaVersions.initializeFromSchemaService(); + if(!validateOverrides(schemaVersions)){ + throw new ExceptionInInitializerError("The versions requested is not supported by SchemaService"); + } + if("true".equals(overrideSchemaService)){ + schemaVersions.initializeFromSchemaConfig(schemaConfigVersions); + } + else{ + schemaVersions.initializeFromSchemaService(); + } + + } + + public boolean validateOverrides(SchemaServiceVersions schemaVersions1){ + boolean versionsAvailable = true; + if("true".equals(overrideSchemaService)){ + versionsAvailable = schemaConfigVersions.getApiVersions().stream(). + allMatch( + (s) -> schemaVersions1.getVersionsAll().contains(s) + ); + } + return versionsAvailable; } public SchemaServiceVersions getSchemaVersions() { |