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