lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.ovsdb.rfc.schema; |
| 17 | |
| 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | |
| 21 | import java.util.Map; |
| 22 | import java.util.Objects; |
| 23 | import java.util.Set; |
| 24 | |
| 25 | import org.onosproject.ovsdb.rfc.schema.type.AtomicColumnType; |
| 26 | import org.onosproject.ovsdb.rfc.schema.type.UuidBaseType; |
| 27 | |
| 28 | /** |
| 29 | * A schema for the table represented by table-schema, which consists of a set |
| 30 | * of columns. |
| 31 | */ |
| 32 | public final class TableSchema { |
| 33 | |
| 34 | private final String name; |
| 35 | private final Map<String, ColumnSchema> columnSchemas; |
| 36 | |
| 37 | /** |
| 38 | * Constructs a TableSchema object. |
| 39 | * @param name the name of table |
| 40 | * @param columnSchemas a map of ColumnSchema |
| 41 | */ |
| 42 | public TableSchema(String name, Map<String, ColumnSchema> columnSchemas) { |
lishuai | 2f19743 | 2015-07-31 16:27:58 +0800 | [diff] [blame] | 43 | checkNotNull(name, "name cannot be null"); |
| 44 | checkNotNull(columnSchemas, "columnSchemas cannot be null"); |
lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 45 | this.name = name; |
| 46 | this.columnSchemas = columnSchemas; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns the name of table. |
| 51 | * @return the name of table |
| 52 | */ |
| 53 | public String name() { |
| 54 | return name; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns a map of ColumnSchema. |
| 59 | * @return a map of ColumnSchema |
| 60 | */ |
| 61 | public Map<String, ColumnSchema> columnSchemas() { |
| 62 | return this.columnSchemas; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns a set of column name. |
| 67 | * @return a set of column name |
| 68 | */ |
| 69 | public Set<String> getColumnNames() { |
| 70 | return this.columnSchemas.keySet(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Determine whether contain the column. |
| 75 | * @param columnName column name |
| 76 | * @return boolean |
| 77 | */ |
| 78 | public boolean hasColumn(String columnName) { |
| 79 | return this.getColumnNames().contains(columnName); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the ColumnSchema whose name is the columnName. |
| 84 | * @param columnName column name |
| 85 | * @return ColumnSchema |
| 86 | */ |
| 87 | public ColumnSchema getColumnSchema(String columnName) { |
| 88 | return this.columnSchemas.get(columnName); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Refer to RFC 7047 Section 3.2. generate initialization columns in each |
| 93 | * table namely _uuid and _version. |
| 94 | */ |
| 95 | public void generateInitializationColumns() { |
| 96 | columnSchemas |
| 97 | .put("_uuid", |
| 98 | new ColumnSchema("_uuid", |
| 99 | new AtomicColumnType(new UuidBaseType()))); |
| 100 | columnSchemas |
| 101 | .put("_version", |
| 102 | new ColumnSchema("_version", |
| 103 | new AtomicColumnType(new UuidBaseType()))); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public int hashCode() { |
| 108 | return Objects.hash(name, columnSchemas); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public boolean equals(Object obj) { |
| 113 | if (this == obj) { |
| 114 | return true; |
| 115 | } |
| 116 | if (obj instanceof TableSchema) { |
| 117 | final TableSchema other = (TableSchema) obj; |
| 118 | return Objects.equals(this.name, other.name) |
| 119 | && Objects.equals(this.columnSchemas, other.columnSchemas); |
| 120 | } |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public String toString() { |
| 126 | return toStringHelper(this).add("name", name) |
| 127 | .add("columnSchemas", columnSchemas).toString(); |
| 128 | } |
| 129 | } |