Only load SchemaService related beans in aai-common when schema.translator.list=schem...
[aai/aai-common.git] / aai-schema-ingest / src / main / java / org / onap / aai / setup / SchemaServiceTranslator.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.setup;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.onap.aai.restclient.RestClient;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.beans.factory.annotation.Value;
35 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
36 import org.springframework.core.io.Resource;
37 import org.springframework.http.HttpHeaders;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.MediaType;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.stereotype.Component;
42
43 /**
44  * <b>AAIConfigTranslator</b> is responsible for looking at the schema files and
45  * edge files based on the available versions Also has the ability to exclude
46  * them based on the node.exclusion.pattern
47  */
48 @Component
49 @ConditionalOnProperty(name = "schema.translator.list", havingValue = "schema-service", matchIfMissing = false)
50 public class SchemaServiceTranslator extends Translator {
51
52     private static final Logger LOGGER = LoggerFactory.getLogger(SchemaServiceTranslator.class);
53
54     @Value("${schema.service.nodes.endpoint}")
55     private String nodeSchemaUri;
56
57     @Value("${schema.service.edges.endpoint}")
58     private String edgeSchemaUri;
59
60     @Autowired
61     private RestClient restClient;
62
63     public SchemaServiceTranslator(SchemaVersions schemaVersions) {
64         super(schemaVersions);
65     }
66
67     /*
68      * (non-Javadoc)
69      *
70      * @see org.onap.aai.setup.ConfigTranslator#getNodeFiles()
71      */
72
73     @Override
74     public List<InputStream> getVersionNodeStream(SchemaVersion version) throws IOException {
75
76         final Map<String, String> headersMap = new HashMap<>();
77         headersMap.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML.toString());
78         headersMap.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML.toString());
79         final String content = "";
80         final String uri = nodeSchemaUri + version.toString();
81         ResponseEntity<Resource> schemaResponse = restClient.getGetResource(content, uri, headersMap);
82
83         verifySchemaServiceResponse(schemaResponse.getStatusCode());
84         LOGGER.debug("SchemaResponse Status code" + schemaResponse.getStatusCode());
85         
86         Resource resultBody = schemaResponse.getBody();
87         return resultBody != null
88             ? Collections.singletonList(resultBody.getInputStream())
89             : Collections.emptyList();
90     }
91
92     @Override
93     public List<String> getJsonPayload(SchemaVersion version) throws IOException {
94         /*
95          * Call Schema MS to get versions using RestTemplate
96          */
97         final String content = "";
98         final String uri = edgeSchemaUri + version.toString();
99         final Map<String, String> headersMap = new HashMap<>();
100
101         ResponseEntity<String> schemaResponse = restClient.getGetRequest(content, uri, headersMap);
102         verifySchemaServiceResponse(schemaResponse.getStatusCode());
103         LOGGER.debug("SchemaResponse Status code" + schemaResponse.getStatusCode());
104         return Collections.singletonList(schemaResponse.getBody());
105
106     }
107
108     private void verifySchemaServiceResponse(HttpStatus statusCode) throws IOException {
109         if (statusCode != HttpStatus.OK) {
110             LOGGER.error("Please check the Schema Service. It returned with the status code {}", statusCode);
111             throw new IOException("SchemaService is not available");
112         }
113     }
114
115 }