68678b83373878e389e497cc29fabe0fc650625b
[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
21 package org.onap.aai.setup;
22
23 import com.google.gson.FieldNamingPolicy;
24 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
26
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import javax.annotation.PostConstruct;
32
33 import org.onap.aai.restclient.RestClient;
34 import org.onap.aai.restclient.RestClientFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.beans.factory.annotation.Value;
37 import org.springframework.http.ResponseEntity;
38
39 public class SchemaVersionsBean {
40
41     private String SCHEMA_SERVICE = "schema-service";
42     private SchemaServiceVersions schemaVersions;
43
44     @Value("${schema.service.versions.endpoint}")
45     private String versionsUri;
46
47     @Value("${schema.service.versions.override:false}")
48     private String overrideSchemaService;
49
50     @Autowired
51     private RestClientFactory restClientFactory;
52
53     @Autowired(required = false)
54     private SchemaConfigVersions schemaConfigVersions;
55
56     @PostConstruct
57     public void initialize() {
58         // Call SchemaService to get versions
59         retrieveAllSchemaVersions();
60     }
61
62     public void retrieveAllSchemaVersions() throws ExceptionInInitializerError {
63         /*
64          * Call Schema MS to get versions using RestTemplate
65          */
66         String content = "";
67         Map<String, String> headersMap = new HashMap<>();
68         RestClient restClient = restClientFactory.getRestClient(SCHEMA_SERVICE);
69
70         ResponseEntity<String> schemaResponse = restClient.getGetRequest(content, versionsUri, headersMap);
71         Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).create();
72         schemaVersions = gson.fromJson(schemaResponse.getBody(), SchemaServiceVersions.class);
73         if (!validateOverrides(schemaVersions)) {
74             throw new ExceptionInInitializerError("The versions requested is not supported by SchemaService");
75         }
76         if ("true".equals(overrideSchemaService)) {
77             schemaVersions.initializeFromSchemaConfig(schemaConfigVersions);
78         } else {
79             schemaVersions.initializeFromSchemaService();
80         }
81
82     }
83
84     public boolean validateOverrides(SchemaServiceVersions schemaVersions1) {
85         boolean versionsAvailable = true;
86         if ("true".equals(overrideSchemaService)) {
87             versionsAvailable = schemaConfigVersions.getApiVersions().stream()
88                     .allMatch((s) -> schemaVersions1.getVersionsAll().contains(s));
89
90         }
91         return versionsAvailable;
92     }
93
94     public SchemaServiceVersions getSchemaVersions() {
95         return schemaVersions;
96     }
97
98     public void setSchemaVersions(SchemaServiceVersions schemaVersions) {
99         this.schemaVersions = schemaVersions;
100     }
101
102     public List<SchemaVersion> getVersions() {
103         return getSchemaVersions().getVersions();
104     }
105
106 }