Update schema service to fail to start
[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 import javax.annotation.PostConstruct;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34
35 public class SchemaVersionsBean {
36
37     private String SCHEMA_SERVICE = "schema-service";
38     private SchemaServiceVersions schemaVersions;
39
40     @Value("${schema.service.versions.endpoint}")
41     private String versionsUri;
42
43     @Value("${schema.service.versions.override:false}")
44     private String overrideSchemaService;
45
46     @Autowired
47     private RestClientFactory restClientFactory;
48
49     @Autowired(required = false)
50     private SchemaConfigVersions schemaConfigVersions;
51
52     @PostConstruct
53     public void initialize() {
54         //Call SchemaService to get versions
55         retrieveAllSchemaVersions();
56     }
57
58     public void retrieveAllSchemaVersions() throws ExceptionInInitializerError{
59             /*
60             Call Schema MS to get versions using RestTemplate
61              */
62         String content = "";
63         Map<String, String> headersMap = new HashMap<>();
64         RestClient restClient = restClientFactory
65             .getRestClient(SCHEMA_SERVICE);
66
67         ResponseEntity<String> schemaResponse = restClient.getGetRequest( content, versionsUri, headersMap);
68         Gson gson = new GsonBuilder()
69             .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
70             .create();
71         schemaVersions = gson.fromJson(schemaResponse.getBody(), SchemaServiceVersions.class);
72        if(!validateOverrides(schemaVersions)){
73            throw new ExceptionInInitializerError("The versions requested is not supported by SchemaService");
74        }
75        if("true".equals(overrideSchemaService)){
76            schemaVersions.initializeFromSchemaConfig(schemaConfigVersions);
77        }
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(
89                     (s) -> schemaVersions1.getVersionsAll().contains(s)
90                 );
91
92         }
93         return versionsAvailable;
94     }
95
96     public SchemaServiceVersions getSchemaVersions() {
97         return schemaVersions;
98     }
99
100     public void setSchemaVersions(SchemaServiceVersions schemaVersions) {
101         this.schemaVersions = schemaVersions;
102     }
103
104     public List<SchemaVersion> getVersions() {
105         return getSchemaVersions().getVersions();
106     }
107
108 }