2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.schemaif.json.definitions;
23 import java.util.List;
25 import org.onap.aai.schemaif.SchemaProviderException;
27 import com.google.gson.Gson;
28 import com.google.gson.GsonBuilder;
29 import com.google.gson.annotations.SerializedName;
31 public class JsonSchema {
32 private static final Gson gson = new GsonBuilder().create();
34 @SerializedName("relationship_types")
35 private List<JsonEdgeSchema> relationshipTypes;
37 @SerializedName("node_types")
38 private List<JsonVertexSchema> nodeTypes;
40 @SerializedName("data_types")
41 private List<DataTypeDefinition> dataTypes;
43 @SerializedName("common_node_properties")
44 private List<JsonPropertySchema> commonNodeProps;
47 public void setRelationshipTypes(List<JsonEdgeSchema> relationshipTypes) {
48 this.relationshipTypes = relationshipTypes;
51 public void setNodeTypes(List<JsonVertexSchema> nodeTypes) {
52 this.nodeTypes = nodeTypes;
55 public void setDataTypes(List<DataTypeDefinition> dataTypes) {
56 this.dataTypes = dataTypes;
59 public List<JsonEdgeSchema> getRelationshipTypes() {
60 return relationshipTypes;
63 public List<JsonVertexSchema> getNodeTypes() {
67 public List<DataTypeDefinition> getDataTypes() {
71 public List<JsonPropertySchema> getCommonProperties() {
72 return commonNodeProps;
75 public void setCommonProperties(List<JsonPropertySchema> properties) {
76 this.commonNodeProps = properties;
79 public void validate() throws SchemaProviderException {
80 if (getNodeTypes() != null) {
81 for (JsonVertexSchema vertexSchema : getNodeTypes()) {
82 vertexSchema.validate();
87 if (getRelationshipTypes() != null) {
88 for (JsonEdgeSchema edgeSchema : getRelationshipTypes()) {
89 edgeSchema.validate();
93 // Validate data types
94 if (getDataTypes() != null) {
95 for (DataTypeDefinition typeSchema : getDataTypes()) {
96 typeSchema.validate();
101 public String toJson() {
102 return gson.toJson(this);
105 public static JsonSchema fromJson(String json) throws SchemaProviderException {
107 if (json == null || json.isEmpty()) {
108 throw new SchemaProviderException("Empty schema definition");
111 return gson.fromJson(json, JsonSchema.class);
112 } catch (Exception ex) {
113 throw new SchemaProviderException("Invalid json: " + ex.getMessage());
118 public String toString() {
119 return "JsonSchema [relationshipTypes=" + relationshipTypes + ", nodeTypes=" + nodeTypes
120 + ", dataTypes=" + dataTypes + ", commonNodeProps=" + commonNodeProps + "]";
124 public int hashCode() {
125 final int prime = 31;
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());
135 public boolean equals(Object obj) {
140 if (getClass() != obj.getClass())
142 JsonSchema other = (JsonSchema) obj;
143 if (commonNodeProps == null) {
144 if (other.commonNodeProps != null)
146 } else if (!commonNodeProps.equals(other.commonNodeProps))
148 if (dataTypes == null) {
149 if (other.dataTypes != null)
151 } else if (!dataTypes.equals(other.dataTypes))
153 if (nodeTypes == null) {
154 if (other.nodeTypes != null)
156 } else if (!nodeTypes.equals(other.nodeTypes))
158 if (relationshipTypes == null) {
159 if (other.relationshipTypes != null)
161 } else if (!relationshipTypes.equals(other.relationshipTypes))