[AAI-2404]add aai-schema-abstraction library
[aai/aai-common.git] / aai-schema-abstraction / src / main / java / org / onap / aai / schemaif / json / definitions / JsonPropertySchema.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 package org.onap.aai.schemaif.json.definitions;
22
23 import java.util.Map;
24
25 import org.onap.aai.schemaif.SchemaProviderException;
26
27 import com.google.gson.Gson;
28 import com.google.gson.GsonBuilder;
29 import com.google.gson.annotations.SerializedName;
30
31 public class JsonPropertySchema {
32     private static final Gson gson = new GsonBuilder().create();
33     
34     private String name;
35     private Boolean required;
36     private Boolean unique;
37     
38     @SerializedName("type")
39     private String dataType;
40     
41     private String description;
42     
43     @SerializedName("default")
44     private String defaultValue;
45     
46     private Map<String,String> annotations;
47     
48     public String getName() {
49         return name;
50     }
51
52     public void setName(String name) {
53         this.name = name;
54     }
55
56     public Boolean getRequired() {
57         return required;
58     }
59
60     public void setRequired(Boolean required) {
61         this.required = required;
62     }
63
64     public Boolean getUnique() {
65         return unique;
66     }
67
68     public void setUnique(Boolean unique) {
69         this.unique = unique;
70     }
71
72     public String getDataType() {
73         return dataType;
74     }
75
76     public void setDataType(String dataType) {
77         this.dataType = dataType;
78     }
79     
80     public String getDefaultValue() {
81         return defaultValue;
82     }
83
84     public void setDefaultValue(String defaultValue) {
85         this.defaultValue = defaultValue;
86     }
87
88     public String getDescription() {
89         return description;
90     }
91
92     public void setDescription(String description) {
93         this.description = description;
94     }
95     
96     public Map<String,String> getAnnotations() {
97         return annotations;
98     }
99
100     public void setAnnotations(Map<String,String> annotations) {
101         this.annotations = annotations;
102     }
103
104     public void validate() throws SchemaProviderException {
105         if ( (getName() == null) || (getName().isEmpty()) ) {
106             throw new SchemaProviderException(getName() + " property has no name");
107         }
108
109         if ( (getDataType() == null) || (getDataType().isEmpty()) ) {
110             throw new SchemaProviderException(getName() + " property has no type");
111         }
112     }
113
114
115     public String toJson() {
116         return gson.toJson(this);
117     }
118     
119     public static JsonVertexSchema fromJson(String json) {
120         return gson.fromJson(json, JsonVertexSchema.class);
121     }
122
123     @Override
124     public String toString() {
125         return "JsonPropertySchema [name=" + name + ", required=" + required + ", unique=" + unique
126             + ", dataType=" + dataType + ", description=" + description + ", defaultValue="
127             + defaultValue + ", annotations=" + annotations + "]";
128     }
129
130     @Override
131     public int hashCode() {
132         final int prime = 31;
133         int result = 1;
134         result = prime * result + ((annotations == null) ? 0 : annotations.hashCode());
135         result = prime * result + ((dataType == null) ? 0 : dataType.hashCode());
136         result = prime * result + ((defaultValue == null) ? 0 : defaultValue.hashCode());
137         result = prime * result + ((description == null) ? 0 : description.hashCode());
138         result = prime * result + ((name == null) ? 0 : name.hashCode());
139         result = prime * result + ((required == null) ? 0 : required.hashCode());
140         result = prime * result + ((unique == null) ? 0 : unique.hashCode());
141         return result;
142     }
143
144     @Override
145     public boolean equals(Object obj) {
146         if (this == obj)
147             return true;
148         if (obj == null)
149             return false;
150         if (getClass() != obj.getClass())
151             return false;
152         JsonPropertySchema other = (JsonPropertySchema) obj;
153         if (annotations == null) {
154             if (other.annotations != null)
155                 return false;
156         } else if (!annotations.equals(other.annotations))
157             return false;
158         if (dataType == null) {
159             if (other.dataType != null)
160                 return false;
161         } else if (!dataType.equals(other.dataType))
162             return false;
163         if (defaultValue == null) {
164             if (other.defaultValue != null)
165                 return false;
166         } else if (!defaultValue.equals(other.defaultValue))
167             return false;
168         if (description == null) {
169             if (other.description != null)
170                 return false;
171         } else if (!description.equals(other.description))
172             return false;
173         if (name == null) {
174             if (other.name != null)
175                 return false;
176         } else if (!name.equals(other.name))
177             return false;
178         if (required == null) {
179             if (other.required != null)
180                 return false;
181         } else if (!required.equals(other.required))
182             return false;
183         if (unique == null) {
184             if (other.unique != null)
185                 return false;
186         } else if (!unique.equals(other.unique))
187             return false;
188         return true;
189     }
190     
191     
192 }