2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.aai.schemaservice.nodeschema;
23 import java.io.ByteArrayOutputStream;
24 import java.io.OutputStreamWriter;
25 import java.io.UnsupportedEncodingException;
26 import java.util.HashMap;
28 import java.util.Optional;
30 import javax.annotation.PostConstruct;
31 import javax.xml.transform.*;
32 import javax.xml.transform.dom.DOMSource;
33 import javax.xml.transform.stream.StreamResult;
35 import org.onap.aai.logging.LogFormatTools;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.stereotype.Service;
41 public class NodeSchemaService {
43 private static final Logger LOGGER = LoggerFactory.getLogger(NodeSchemaResource.class);
45 private SchemaVersions schemaVersions;
47 private NodeIngestor nodeIngestor;
49 private Map<String, String> versionMap = new HashMap<>();
51 public NodeSchemaService(NodeIngestor nodeIngestor, SchemaVersions schemaVersions) {
52 this.nodeIngestor = nodeIngestor;
53 this.schemaVersions = schemaVersions;
57 public void initialize() {
59 schemaVersions.getVersions().forEach((schemaVersion -> {
61 TransformerFactory tf = TransformerFactory.newInstance();
63 Transformer transformer = null;
65 transformer = tf.newTransformer();
66 } catch (TransformerConfigurationException e) {
67 LOGGER.warn("Encountered an transformer configuration exception"
68 + "during node schema service startup ", LogFormatTools.getStackTop(e));
70 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
71 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
72 transformer.setOutputProperty(OutputKeys.INDENT, "no");
73 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
75 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
77 DOMSource domSource = new DOMSource(nodeIngestor.getSchema(schemaVersion));
79 StreamResult streamResult =
80 new StreamResult(new OutputStreamWriter(buffer, "UTF-8"));
81 transformer.transform(domSource, streamResult);
82 versionMap.put(schemaVersion.toString(), buffer.toString("UTF-8"));
83 } catch (TransformerException | UnsupportedEncodingException e) {
84 LOGGER.warn("Encountered an transformer or unsupported encoding exception "
85 + "during node schema service startup ", LogFormatTools.getStackTop(e));
91 public Optional<String> fetch(String version) {
92 return Optional.ofNullable(versionMap.get(version));