680bdd12932a6a3c2e947bbbbdba6eda1eb0b12e
[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.ArrayList;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.onap.aai.restclient.RestClient;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.beans.factory.annotation.Qualifier;
36 import org.springframework.beans.factory.annotation.Value;
37 import org.springframework.core.io.Resource;
38 import org.springframework.http.HttpHeaders;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.http.MediaType;
41 import org.springframework.http.ResponseEntity;
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 public class SchemaServiceTranslator extends Translator {
49
50     private static final Logger LOGGER = LoggerFactory.getLogger(SchemaServiceTranslator.class);
51
52     @Value("${schema.service.nodes.endpoint}")
53     private String nodeSchemaUri;
54
55     @Value("${schema.service.edges.endpoint}")
56     private String edgeSchemaUri;
57
58     @Autowired
59     private RestClient restClient;
60
61     public SchemaServiceTranslator(SchemaVersions schemaVersions) {
62         super(schemaVersions);
63     }
64
65     /*
66      * (non-Javadoc)
67      *
68      * @see org.onap.aai.setup.ConfigTranslator#getNodeFiles()
69      */
70
71     @Override
72     public List<InputStream> getVersionNodeStream(SchemaVersion version) throws IOException {
73
74         final Map<String, String> headersMap = new HashMap<>();
75         headersMap.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML.toString());
76         headersMap.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML.toString());
77         final String content = "";
78         final String uri = nodeSchemaUri + version.toString();
79         ResponseEntity<Resource> schemaResponse = restClient.getGetResource(content, uri, headersMap);
80
81         verifySchemaServiceResponse(schemaResponse.getStatusCode());
82         LOGGER.debug("SchemaResponse Status code" + schemaResponse.getStatusCode());
83         
84         Resource resultBody = schemaResponse.getBody();
85         return resultBody != null
86             ? Collections.singletonList(resultBody.getInputStream())
87             : Collections.emptyList();
88     }
89
90     @Override
91     public List<String> getJsonPayload(SchemaVersion version) throws IOException {
92         /*
93          * Call Schema MS to get versions using RestTemplate
94          */
95         final String content = "";
96         final String uri = edgeSchemaUri + version.toString();
97         final Map<String, String> headersMap = new HashMap<>();
98
99         ResponseEntity<String> schemaResponse = restClient.getGetRequest(content, uri, headersMap);
100         verifySchemaServiceResponse(schemaResponse.getStatusCode());
101         LOGGER.debug("SchemaResponse Status code" + schemaResponse.getStatusCode());
102         return Collections.singletonList(schemaResponse.getBody());
103
104     }
105
106     private void verifySchemaServiceResponse(HttpStatus statusCode) throws IOException {
107         if (statusCode != HttpStatus.OK) {
108             LOGGER.error("Please check the Schema Service. It returned with the status code {}", statusCode);
109             throw new IOException("SchemaService is not available");
110         }
111     }
112
113 }