Update schema ingest library call schema service
[aai/aai-common.git] / aai-schema-ingest / src / main / java / org / onap / aai / setup / SchemaVersions.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 org.onap.aai.validation.AAISchemaValidationException;
23 import org.springframework.beans.factory.annotation.Value;
24 import org.springframework.context.annotation.PropertySource;
25 import org.springframework.stereotype.Component;
26
27 import javax.annotation.PostConstruct;
28 import java.util.List;
29 import java.util.stream.Collectors;
30
31 @Component
32 @PropertySource(value = "classpath:schema-ingest.properties", ignoreResourceNotFound = true)
33 @PropertySource(value = "file:${schema.ingest.file}", ignoreResourceNotFound = true)
34 public class SchemaVersions {
35
36     @Value("#{'${schema.version.list}'.split(',')}")
37     private List<String> apiVersions;
38     @Value("${schema.version.api.default}")
39     private String defaultApiVersion;
40     @Value("${schema.version.edge.label.start}")
41     private String edgeLabelStartVersion;
42     @Value("${schema.version.depth.start}")
43     private String depthStartVersion;
44     @Value("${schema.version.app.root.start}")
45     private String appRootStartVersion;
46     @Value("${schema.version.related.link.start}")
47     private String relatedLinkStartVersion;
48     @Value("${schema.version.namespace.change.start}")
49
50     protected String namespaceChangeStartVersion;
51     protected List<SchemaVersion> versionsValue;
52     protected SchemaVersion edgeLabelVersionValue;
53     protected SchemaVersion defaultVersionValue;
54     protected SchemaVersion depthVersionValue;
55     protected SchemaVersion appRootVersionValue;
56     protected SchemaVersion relatedLinkVersionValue;
57     protected SchemaVersion namespaceChangeVersionValue;
58
59     @PostConstruct
60     public void initialize() {
61         versionsValue = apiVersions.stream().map(SchemaVersion::new).collect(Collectors.toList());
62         edgeLabelVersionValue = new SchemaVersion(edgeLabelStartVersion);
63         defaultVersionValue = new SchemaVersion(defaultApiVersion);
64         depthVersionValue = new SchemaVersion(depthStartVersion);
65         appRootVersionValue = new SchemaVersion(appRootStartVersion);
66         relatedLinkVersionValue = new SchemaVersion(relatedLinkStartVersion);
67         namespaceChangeVersionValue = new SchemaVersion(namespaceChangeStartVersion);
68         this.validate();
69     }
70
71
72     protected void validate() {
73         String errorMessage = "Invalid, edge label version is not in the api versions list"
74                     + ", please check schema.version.list and ensure that the"
75                     + " schema.version.edge.label.start is in that list";
76         if (!versionsValue.contains(edgeLabelVersionValue)) {
77             throw new AAISchemaValidationException(errorMessage);
78         }
79
80         if (!versionsValue.contains(defaultVersionValue)) {
81             throw new AAISchemaValidationException(errorMessage);
82         }
83
84         if (!versionsValue.contains(depthVersionValue)) {
85             throw new AAISchemaValidationException(errorMessage);
86         }
87
88         if (!versionsValue.contains(appRootVersionValue)) {
89             throw new AAISchemaValidationException(errorMessage);
90         }
91
92         if (!versionsValue.contains(relatedLinkVersionValue)) {
93             throw new AAISchemaValidationException(errorMessage);
94         }
95
96         if (!versionsValue.contains(namespaceChangeVersionValue)) {
97             throw new AAISchemaValidationException(errorMessage);
98         }
99     }
100     
101     public List<SchemaVersion> getVersions() {
102         return versionsValue;
103     }
104
105     public SchemaVersion getEdgeLabelVersion() {
106         return edgeLabelVersionValue;
107     }
108
109     public SchemaVersion getDefaultVersion() {
110         return defaultVersionValue;
111     }
112
113     public SchemaVersion getDepthVersion() {
114         return depthVersionValue;
115     }
116
117     public SchemaVersion getAppRootVersion() {
118         return appRootVersionValue;
119     }
120
121     public SchemaVersion getRelatedLinkVersion() {
122         return relatedLinkVersionValue;
123     }
124
125     public SchemaVersion getNamespaceChangeVersion() {
126         return namespaceChangeVersionValue;
127     }
128
129     public void setNamespaceChangeVersion(SchemaVersion namespaceChangeVersion) {
130         this.namespaceChangeVersionValue = namespaceChangeVersion;
131     }
132
133 }