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=========================================================
20 package org.onap.aai.schemaservice.nodeschema;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.onap.aai.logging.LogFormatTools;
25 import org.springframework.stereotype.Service;
27 import javax.annotation.PostConstruct;
28 import javax.xml.transform.*;
29 import javax.xml.transform.dom.DOMSource;
30 import javax.xml.transform.stream.StreamResult;
31 import java.io.ByteArrayOutputStream;
32 import java.io.OutputStreamWriter;
33 import java.io.UnsupportedEncodingException;
34 import java.util.HashMap;
36 import java.util.Optional;
39 public class NodeSchemaService {
41 private static final Logger LOGGER = LoggerFactory.getLogger(NodeSchemaResource.class);
43 private SchemaVersions schemaVersions;
45 private NodeIngestor nodeIngestor;
47 private Map<String, String> versionMap = new HashMap<>();
49 public NodeSchemaService(NodeIngestor nodeIngestor, SchemaVersions schemaVersions) {
50 this.nodeIngestor = nodeIngestor;
51 this.schemaVersions = schemaVersions;
55 public void initialize() {
57 schemaVersions.getVersions().forEach((schemaVersion -> {
59 TransformerFactory tf = TransformerFactory.newInstance();
61 Transformer transformer = null;
63 transformer = tf.newTransformer();
64 } catch (TransformerConfigurationException e) {
65 LOGGER.warn("Encountered an transformer configuration exception" +
66 "during node schema service startup ", LogFormatTools.getStackTop(e));
68 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
69 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
70 transformer.setOutputProperty(OutputKeys.INDENT, "no");
71 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
73 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
75 DOMSource domSource = new DOMSource(nodeIngestor.getSchema(schemaVersion));
77 StreamResult streamResult = new StreamResult(new OutputStreamWriter(buffer, "UTF-8"));
78 transformer.transform(domSource, streamResult);
79 versionMap.put(schemaVersion.toString(), buffer.toString("UTF-8"));
80 } catch (TransformerException | UnsupportedEncodingException e) {
81 LOGGER.warn("Encountered an transformer or unsupported encoding exception " +
82 "during node schema service startup ", LogFormatTools.getStackTop(e));
88 public Optional<String> fetch(String version) {
89 return Optional.ofNullable(versionMap.get(version));