26214aae8016629e35f85be08aa1a31cfe7570d9
[aai/schema-service.git] / aai-schema-service / src / main / java / org / onap / aai / schemaservice / nodeschema / NodeSchemaService.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.schemaservice.nodeschema;
22
23 import java.io.ByteArrayOutputStream;
24 import java.io.OutputStreamWriter;
25 import java.io.UnsupportedEncodingException;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Optional;
29
30 import javax.annotation.PostConstruct;
31 import javax.xml.transform.*;
32 import javax.xml.transform.dom.DOMSource;
33 import javax.xml.transform.stream.StreamResult;
34
35 import org.onap.aai.logging.LogFormatTools;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.stereotype.Service;
39
40 @Service
41 public class NodeSchemaService {
42
43     private static final Logger LOGGER = LoggerFactory.getLogger(NodeSchemaResource.class);
44
45     private SchemaVersions schemaVersions;
46
47     private NodeIngestor nodeIngestor;
48
49     private Map<String, String> versionMap = new HashMap<>();
50
51     public NodeSchemaService(NodeIngestor nodeIngestor, SchemaVersions schemaVersions) {
52         this.nodeIngestor = nodeIngestor;
53         this.schemaVersions = schemaVersions;
54     }
55
56     @PostConstruct
57     public void initialize() {
58
59         schemaVersions.getVersions().forEach((schemaVersion -> {
60
61             TransformerFactory tf = TransformerFactory.newInstance();
62
63             Transformer transformer = null;
64             try {
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));
69             }
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");
74
75             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
76             try {
77                 DOMSource domSource = new DOMSource(nodeIngestor.getSchema(schemaVersion));
78
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));
86             }
87
88         }));
89     }
90
91     public Optional<String> fetch(String version) {
92         return Optional.ofNullable(versionMap.get(version));
93     }
94
95 }