AAI-1523 Batch reformat aai-schema-ingest
[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 com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25
26 import java.io.*;
27 import java.util.*;
28
29 import javax.ws.rs.HttpMethod;
30
31 import org.onap.aai.restclient.RestClient;
32 import org.onap.aai.restclient.RestClientFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.beans.factory.annotation.Value;
35 import org.springframework.core.io.Resource;
36 import org.springframework.http.HttpHeaders;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.http.MediaType;
39 import org.springframework.http.ResponseEntity;
40
41 /**
42  * <b>AAIConfigTranslator</b> is responsible for looking at the schema files and
43  * edge files based on the available versions Also has the ability to exclude
44  * them based on the node.exclusion.pattern
45  */
46 public class SchemaServiceTranslator extends Translator {
47
48     private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(SchemaServiceTranslator.class);
49
50     private static final String SchemaServiceClientType = "schema.service";
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 RestClientFactory restClientFactory;
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         List<InputStream> inputStreams = new ArrayList<>();
75         String content = "";
76         String uri = nodeSchemaUri + version.toString();
77         Map<String, String> headersMap = new HashMap<>();
78
79         headersMap.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML.toString());
80         headersMap.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML.toString());
81         RestClient restClient = restClientFactory.getRestClient(SchemaServiceClientType);
82         ResponseEntity<Resource> schemaResponse = restClient.getGetResource(content, uri, headersMap);
83         verifySchemaServiceResponse(schemaResponse.getStatusCode());
84         LOGGER.debug("SchemaResponse Status code" + schemaResponse.getStatusCode());
85         inputStreams.add(schemaResponse.getBody().getInputStream());
86         return inputStreams;
87     }
88
89     @Override
90     public List<String> getJsonPayload(SchemaVersion version) throws IOException {
91         /*
92          * Call Schema MS to get versions using RestTemplate
93          */
94         List<String> inputStreams = new ArrayList<>();
95         String content = "";
96         String uri = edgeSchemaUri + version.toString();
97         Map<String, String> headersMap = new HashMap<>();
98
99         RestClient restClient = restClientFactory.getRestClient(SchemaServiceClientType);
100
101         ResponseEntity<String> schemaResponse = restClient.getGetRequest(content, uri, headersMap);
102         verifySchemaServiceResponse(schemaResponse.getStatusCode());
103         LOGGER.debug("SchemaResponse Status code" + schemaResponse.getStatusCode());
104         inputStreams.add(schemaResponse.getBody());
105         return inputStreams;
106
107     }
108
109     private void verifySchemaServiceResponse(HttpStatus statusCode) throws IOException {
110         if (statusCode != HttpStatus.OK) {
111             LOGGER.error("Please check the Schema Service. It returned with the status code {}", statusCode);
112             throw new IOException("SchemaService is not available");
113         }
114     }
115
116 }