[AAI] Fix doc config files
[aai/aai-common.git] / aai-schema-abstraction / src / main / java / org / onap / aai / schemaif / json / definitions / JsonSchema.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.List;
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 JsonSchema {
32     private static final Gson gson = new GsonBuilder().create();
33
34     @SerializedName("relationship_types")
35     private List<JsonEdgeSchema> relationshipTypes;
36     
37     @SerializedName("node_types")
38     private List<JsonVertexSchema> nodeTypes;
39     
40     @SerializedName("data_types")
41     private List<DataTypeDefinition> dataTypes;
42     
43     @SerializedName("common_node_properties")
44     private List<JsonPropertySchema> commonNodeProps;
45
46     
47     public void setRelationshipTypes(List<JsonEdgeSchema> relationshipTypes) {
48                 this.relationshipTypes = relationshipTypes;
49         }
50
51         public void setNodeTypes(List<JsonVertexSchema> nodeTypes) {
52                 this.nodeTypes = nodeTypes;
53         }
54
55         public void setDataTypes(List<DataTypeDefinition> dataTypes) {
56                 this.dataTypes = dataTypes;
57         }
58
59         public List<JsonEdgeSchema> getRelationshipTypes() {
60         return relationshipTypes;
61     }
62     
63     public List<JsonVertexSchema> getNodeTypes() {
64         return nodeTypes;
65     }
66     
67     public List<DataTypeDefinition> getDataTypes() {
68         return dataTypes;
69     }
70     
71     public List<JsonPropertySchema> getCommonProperties() {
72         return commonNodeProps;
73     }
74
75     public void setCommonProperties(List<JsonPropertySchema> properties) {
76         this.commonNodeProps = properties;
77     }
78     
79     public void validate() throws SchemaProviderException {
80         if (getNodeTypes() != null) {
81             for (JsonVertexSchema vertexSchema : getNodeTypes()) {
82                 vertexSchema.validate();
83             }
84         }
85         
86         // Validate edges
87         if (getRelationshipTypes() != null) {
88             for (JsonEdgeSchema edgeSchema : getRelationshipTypes()) {
89                 edgeSchema.validate();
90             }
91         }
92         
93         // Validate data types
94         if (getDataTypes() != null) {
95             for (DataTypeDefinition typeSchema : getDataTypes()) {
96                 typeSchema.validate();
97             }
98         }
99     }
100     
101     public String toJson() {
102         return gson.toJson(this);
103     }
104
105     public static JsonSchema fromJson(String json) throws SchemaProviderException {
106         try {
107             if (json == null || json.isEmpty()) {
108                 throw new SchemaProviderException("Empty schema definition");
109             }
110             
111             return gson.fromJson(json, JsonSchema.class);
112         } catch (Exception ex) {
113             throw new SchemaProviderException("Invalid json: " + ex.getMessage());
114         }
115     }
116
117     @Override
118     public String toString() {
119         return "JsonSchema [relationshipTypes=" + relationshipTypes + ", nodeTypes=" + nodeTypes
120             + ", dataTypes=" + dataTypes + ", commonNodeProps=" + commonNodeProps + "]";
121     }
122
123     @Override
124     public int hashCode() {
125         final int prime = 31;
126         int result = 1;
127         result = prime * result + ((commonNodeProps == null) ? 0 : commonNodeProps.hashCode());
128         result = prime * result + ((dataTypes == null) ? 0 : dataTypes.hashCode());
129         result = prime * result + ((nodeTypes == null) ? 0 : nodeTypes.hashCode());
130         result = prime * result + ((relationshipTypes == null) ? 0 : relationshipTypes.hashCode());
131         return result;
132     }
133
134     @Override
135     public boolean equals(Object obj) {
136         if (this == obj)
137             return true;
138         if (obj == null)
139             return false;
140         if (getClass() != obj.getClass())
141             return false;
142         JsonSchema other = (JsonSchema) obj;
143         if (commonNodeProps == null) {
144             if (other.commonNodeProps != null)
145                 return false;
146         } else if (!commonNodeProps.equals(other.commonNodeProps))
147             return false;
148         if (dataTypes == null) {
149             if (other.dataTypes != null)
150                 return false;
151         } else if (!dataTypes.equals(other.dataTypes))
152             return false;
153         if (nodeTypes == null) {
154             if (other.nodeTypes != null)
155                 return false;
156         } else if (!nodeTypes.equals(other.nodeTypes))
157             return false;
158         if (relationshipTypes == null) {
159             if (other.relationshipTypes != null)
160                 return false;
161         } else if (!relationshipTypes.equals(other.relationshipTypes))
162             return false;
163         return true;
164     }
165
166     
167 }