Merge "[AAI-2404]add aai-schema-abstraction library"
[aai/aai-common.git] / aai-schema-abstraction / src / main / java / org / onap / aai / schemaif / definitions / PropertySchema.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2019 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.aai.schemaif.definitions;
23
24 import java.util.Map;
25
26 import org.onap.aai.schemaif.SchemaProviderException;
27 import org.onap.aai.schemaif.definitions.types.DataType;
28
29
30 public class PropertySchema {
31     protected String name;
32     protected DataType dataType;
33     protected Boolean required;
34     protected String defaultValue;
35     protected Boolean unique;
36     protected Boolean isReserved;
37     protected Map<String,String> annotations;
38
39     public String getName() {
40         return name;
41     }
42
43     public DataType getDataType() {
44         return dataType;
45     }
46
47     public Boolean isRequired() {
48         return required;
49     }
50     
51     public Boolean isKey() {
52         return (unique && required);
53     }
54     
55     public Boolean isUnique() {
56         return unique;
57     }
58
59     public String getDefaultValue() {
60         return defaultValue;
61     }
62     
63     public Boolean isReserved() {
64         return isReserved;
65     }
66     
67     public String getAnnotationValue(String annotation) {
68         return annotations.get(annotation.toLowerCase());
69     }
70     
71     public Map<String, String> getAnnotations() {
72       return annotations;
73     }
74
75     public Object validateValue(String value) throws SchemaProviderException {
76         Object obj = dataType.validateValue(value);
77         if (obj == null) {
78             throw new SchemaProviderException("Invalid value for porperty '" + name + "': " + value);
79         }
80         
81         return obj; 
82     }
83     
84     public String toString() {
85         StringBuilder sb = new StringBuilder();
86         sb.append("    property: " + getName() + "\n");
87         sb.append("      datatype: " + dataType.toString() + "\n");
88         sb.append("      required: " + isRequired() + "\n");
89         sb.append("      key: " + isKey() + "\n");
90         sb.append("      reserved: " + isReserved() + "\n");
91         sb.append("      default: " + getDefaultValue() + "\n");
92         sb.append("      annotations: " + "\n");
93         
94         for (String annotation : annotations.keySet()) {
95             sb.append("        " + annotation + ": " + annotations.get(annotation) + "\n");
96         }
97                 
98         return sb.toString();
99     }
100 }