Merge "AAI-1523 checkstyle warnings for aai-core auth"
[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 package org.onap.aai.setup;
21
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import org.onap.aai.restclient.RestClient;
25 import org.onap.aai.restclient.RestClientFactory;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.beans.factory.annotation.Value;
28 import org.springframework.core.io.Resource;
29 import org.springframework.http.HttpHeaders;
30 import org.springframework.http.MediaType;
31 import org.springframework.http.ResponseEntity;
32
33 import javax.ws.rs.HttpMethod;
34 import java.io.*;
35 import java.util.*;
36
37 /**
38  * <b>AAIConfigTranslator</b> is responsible for looking at the schema files and
39  * edge files based on the available versions Also has the ability to exclude
40  * them based on the node.exclusion.pattern
41  */
42 public class SchemaServiceTranslator extends Translator {
43
44         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(SchemaServiceTranslator.class);
45
46         private static final String SchemaServiceClientType = "schema.service";
47
48         @Value("${schema.service.nodes.endpoint}")
49         private String nodeSchemaUri;
50
51         @Value("${schema.service.edges.endpoint}")
52         private String edgeSchemaUri;
53
54         @Autowired
55         private RestClientFactory restClientFactory;
56
57         public SchemaServiceTranslator(SchemaVersions schemaVersions) {
58                 super(schemaVersions);
59         }
60
61         /*
62          * (non-Javadoc)
63          * 
64          * @see org.onap.aai.setup.ConfigTranslator#getNodeFiles()
65          */
66
67         @Override
68         public List<InputStream> getVersionNodeStream(SchemaVersion version) throws IOException {
69
70                 List<InputStream> inputStreams = new ArrayList<>();
71                 String content = "";
72                 String uri = nodeSchemaUri + version.toString();
73                 Map<String, String> headersMap = new HashMap<>();
74
75                 headersMap.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML.toString());
76         headersMap.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML.toString());
77                 RestClient restClient = restClientFactory.getRestClient(SchemaServiceClientType);
78                 ResponseEntity<Resource> schemaResponse = restClient.getGetResource(content, uri,
79                                 headersMap);
80                 LOGGER.debug("SchemaResponse Status code" + schemaResponse.getStatusCode());
81                 inputStreams.add(schemaResponse.getBody().getInputStream());
82                 return inputStreams;
83         }
84
85         @Override
86         public List<String> getJsonPayload(SchemaVersion version) throws IOException {
87                 /*
88                  * Call Schema MS to get versions using RestTemplate
89                  */
90                 List<String> inputStreams = new ArrayList<>();
91                 String content = "";
92                 String uri = edgeSchemaUri + version.toString();
93                 Map<String, String> headersMap = new HashMap<>();
94
95                 RestClient restClient = restClientFactory.getRestClient(SchemaServiceClientType);
96
97         ResponseEntity<String> schemaResponse = restClient.getGetRequest(content, uri,
98                                 headersMap);
99                 LOGGER.debug("SchemaResponse Status code" + schemaResponse.getStatusCode());
100                 inputStreams.add(schemaResponse.getBody());
101                 return inputStreams;
102
103         }
104
105 }