blob: 298afc682c205f25155b48db226cd5d61df9ffd8 [file] [log] [blame]
lishuai91d986c2015-07-28 09:45:20 +08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.ovsdb.rfc.schema;
17
18import static com.google.common.base.MoreObjects.toStringHelper;
19import static com.google.common.base.Preconditions.checkNotNull;
20
21import java.util.Map;
22import java.util.Objects;
23import java.util.Set;
24
25/**
26 * A schema for the database represented by database-schema, which consists of
27 * a set of tables.
28 */
29public final class DatabaseSchema {
30
31 private final String name;
32 private final String version;
33 private final Map<String, TableSchema> tableSchemas;
34
35 /**
36 * Constructs a DatabaseSchema object.
37 * @param name the name of database
38 * @param version the version of database
39 * @param tableSchemas a map of TableSchema
40 */
41 public DatabaseSchema(String name, String version,
42 Map<String, TableSchema> tableSchemas) {
lishuai2f197432015-07-31 16:27:58 +080043 checkNotNull(name, "name cannot be null");
44 checkNotNull(version, "version cannot be null");
45 checkNotNull(tableSchemas, "tableSchemas cannot be null");
lishuai91d986c2015-07-28 09:45:20 +080046 this.name = name;
47 this.version = version;
48 this.tableSchemas = tableSchemas;
49 }
50
51 /**
52 * Returns the name of database.
53 * @return the name of database
54 */
55 public String name() {
56 return name;
57 }
58
59 /**
60 * Returns the version of database.
61 * @return the version of database
62 */
63 public String version() {
64 return version;
65 }
66
67 /**
68 * Returns a map of TableSchema.
69 * @return a map of TableSchema
70 */
71 public Map<String, TableSchema> tableSchemas() {
72 return tableSchemas;
73 }
74
75 /**
76 * Returns a set of table name.
77 * @return a set of table name
78 */
79 public Set<String> getTableNames() {
80 return this.tableSchemas.keySet();
81 }
82
83 /**
84 * Determine whether contain the table.
85 * @param tableName table name
86 * @return boolean
87 */
88 public boolean hasTable(String tableName) {
89 return this.getTableNames().contains(tableName);
90 }
91
92 /**
93 * Returns the TableSchema whose name is the tableName.
94 * @param tableName table name
95 * @return TableSchema
96 */
97 public TableSchema getTableSchema(String tableName) {
98 TableSchema table = tableSchemas.get(tableName);
99 return table;
100 }
101
102 /**
103 * generate initialization columns in each table namely _uuid and _version.
104 */
105 public void generateInitializationColumns() {
106 for (TableSchema tableSchema : tableSchemas.values()) {
107 tableSchema.generateInitializationColumns();
108 }
109 }
110
111 @Override
112 public int hashCode() {
113 return Objects.hash(name, version, tableSchemas);
114 }
115
116 @Override
117 public boolean equals(Object obj) {
118 if (this == obj) {
119 return true;
120 }
121 if (obj instanceof DatabaseSchema) {
122 final DatabaseSchema other = (DatabaseSchema) obj;
123 return Objects.equals(this.name, other.name)
124 && Objects.equals(this.version, other.version)
125 && Objects.equals(this.tableSchemas, other.tableSchemas);
126 }
127 return false;
128 }
129
130 @Override
131 public String toString() {
132 return toStringHelper(this).add("name", name).add("version", version)
133 .add("tableSchemas", tableSchemas).toString();
134 }
135}