119669d3a2ec03f29afad08715cc19823f0ee3bc
[aai/aai-common.git] / aai-schema-ingest / src / main / java / org / onap / aai / setup / SchemaVersionsBean.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-18 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.google.gson.FieldNamingPolicy;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import org.onap.aai.restclient.RestClient;
26 import org.onap.aai.restclient.RestClientFactory;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.beans.factory.annotation.Value;
29 import org.springframework.http.ResponseEntity;
30
31 import javax.annotation.PostConstruct;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35
36 public class SchemaVersionsBean {
37
38     private String SCHEMA_SERVICE = "schema-service";
39     private SchemaServiceVersions schemaVersions;
40
41     @Value("${schema.service.versions.endpoint}")
42     private String versionsUri;
43
44     @Autowired
45     private RestClientFactory restClientFactory;
46
47     @PostConstruct
48     public void initialize() {
49         //Call SchemaService to get versions
50         retrieveAllSchemaVersions();
51     }
52
53     public void retrieveAllSchemaVersions() {
54             /*
55             Call Schema MS to get versions using RestTemplate
56              */
57         String content = "";
58         Map<String, String> headersMap = new HashMap<>();
59         RestClient restClient = restClientFactory
60             .getRestClient(SCHEMA_SERVICE);
61
62         ResponseEntity<String> schemaResponse = restClient.getGetRequest( content, versionsUri, headersMap);
63         Gson gson = new GsonBuilder()
64             .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
65             .create();
66         schemaVersions = gson.fromJson(schemaResponse.getBody(), SchemaServiceVersions.class);
67         schemaVersions.initializeFromSchemaService();
68
69     }
70
71     public SchemaServiceVersions getSchemaVersions() {
72         return schemaVersions;
73     }
74
75     public void setSchemaVersions(SchemaServiceVersions schemaVersions) {
76         this.schemaVersions = schemaVersions;
77     }
78
79     public List<SchemaVersion> getVersions() {
80         return getSchemaVersions().getVersions();
81     }
82
83 }