8c6399bdeb463e27353595214a959fb4c5f0a849
[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 package org.onap.aai.schemaservice.nodeschema;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.onap.aai.logging.LogFormatTools;
25 import org.springframework.stereotype.Service;
26
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;
35 import java.util.Map;
36 import java.util.Optional;
37
38 @Service
39 public class NodeSchemaService {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(NodeSchemaResource.class);
42
43     private SchemaVersions schemaVersions;
44
45     private NodeIngestor nodeIngestor;
46
47     private Map<String, String> versionMap = new HashMap<>();
48
49     public NodeSchemaService(NodeIngestor nodeIngestor, SchemaVersions schemaVersions) {
50         this.nodeIngestor = nodeIngestor;
51         this.schemaVersions = schemaVersions;
52     }
53
54     @PostConstruct
55     public void initialize() {
56
57         schemaVersions.getVersions().forEach((schemaVersion -> {
58
59             TransformerFactory tf = TransformerFactory.newInstance();
60
61             Transformer transformer = null;
62             try {
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));
67             }
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");
72
73             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
74             try {
75                 DOMSource domSource = new DOMSource(nodeIngestor.getSchema(schemaVersion));
76
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));
83             }
84
85         }));
86     }
87
88     public Optional<String> fetch(String version) {
89         return Optional.ofNullable(versionMap.get(version));
90     }
91
92 }