Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 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.tableservice; |
| 17 | |
| 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | |
| 21 | import java.util.Objects; |
| 22 | |
Sho SHIMIZU | 8700d42 | 2015-08-26 18:13:44 -0700 | [diff] [blame] | 23 | import org.onosproject.ovsdb.rfc.exception.ColumnSchemaNotFoundException; |
| 24 | import org.onosproject.ovsdb.rfc.exception.TableSchemaNotFoundException; |
| 25 | import org.onosproject.ovsdb.rfc.exception.VersionMismatchException; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 26 | import org.onosproject.ovsdb.rfc.notation.Column; |
| 27 | import org.onosproject.ovsdb.rfc.notation.Row; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 28 | import org.onosproject.ovsdb.rfc.notation.Uuid; |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 29 | import org.onosproject.ovsdb.rfc.schema.ColumnSchema; |
| 30 | import org.onosproject.ovsdb.rfc.schema.DatabaseSchema; |
| 31 | import org.onosproject.ovsdb.rfc.schema.TableSchema; |
| 32 | import org.onosproject.ovsdb.rfc.table.OvsdbTable; |
| 33 | import org.onosproject.ovsdb.rfc.table.VersionNum; |
| 34 | import org.onosproject.ovsdb.rfc.utils.VersionUtil; |
| 35 | |
| 36 | /** |
| 37 | * Representation of conversion between Ovsdb table and Row. |
| 38 | */ |
| 39 | public abstract class AbstractOvsdbTableService implements OvsdbTableService { |
| 40 | |
| 41 | private final DatabaseSchema dbSchema; |
| 42 | private final Row row; |
| 43 | private final TableDescription tableDesc; |
| 44 | |
| 45 | /** |
| 46 | * Constructs a AbstractOvsdbTableService object. |
| 47 | * @param dbSchema DatabaseSchema entity |
| 48 | * @param row Row entity |
| 49 | * @param table table name |
| 50 | * @param formVersion the initial version |
| 51 | */ |
| 52 | public AbstractOvsdbTableService(DatabaseSchema dbSchema, Row row, OvsdbTable table, |
| 53 | VersionNum formVersion) { |
| 54 | checkNotNull(dbSchema, "database schema cannot be null"); |
| 55 | checkNotNull(row, "row cannot be null"); |
| 56 | checkNotNull(table, "table cannot be null"); |
| 57 | checkNotNull(formVersion, "the initial version cannot be null"); |
| 58 | this.dbSchema = dbSchema; |
| 59 | row.setTableName(table.tableName()); |
| 60 | this.row = row; |
| 61 | TableDescription tableDesc = new TableDescription(table, formVersion); |
| 62 | this.tableDesc = tableDesc; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Check whether the parameter of dbSchema is valid and check whether the |
| 67 | * table is existent in Database Schema. |
| 68 | */ |
| 69 | private boolean isValid() { |
| 70 | if (dbSchema == null) { |
| 71 | return false; |
| 72 | } |
| 73 | if (!dbSchema.name().equalsIgnoreCase(tableDesc.database())) { |
| 74 | return false; |
| 75 | } |
| 76 | checkTableSchemaVersion(); |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Check the table version. |
| 82 | */ |
| 83 | private void checkTableSchemaVersion() { |
| 84 | String fromVersion = tableDesc.fromVersion(); |
| 85 | String untilVersion = tableDesc.untilVersion(); |
| 86 | String schemaVersion = dbSchema.version(); |
| 87 | checkVersion(schemaVersion, fromVersion, untilVersion); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Check the column version. |
| 92 | * @param columnDesc ColumnDescription entity |
| 93 | */ |
| 94 | private void checkColumnSchemaVersion(ColumnDescription columnDesc) { |
| 95 | String fromVersion = columnDesc.fromVersion(); |
| 96 | String untilVersion = columnDesc.untilVersion(); |
| 97 | String schemaVersion = dbSchema.version(); |
| 98 | checkVersion(schemaVersion, fromVersion, untilVersion); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Check whether the DatabaseSchema version between the initial version and |
| 103 | * the end of the version. |
| 104 | * @param schemaVersion DatabaseSchema version |
| 105 | * @param fromVersion The initial version |
| 106 | * @param untilVersion The end of the version |
| 107 | * @throws VersionMismatchException this is a version mismatch exception |
| 108 | */ |
| 109 | private void checkVersion(String schemaVersion, String fromVersion, String untilVersion) { |
| 110 | VersionUtil.versionMatch(fromVersion); |
| 111 | VersionUtil.versionMatch(untilVersion); |
| 112 | if (!fromVersion.equals(VersionUtil.DEFAULT_VERSION_STRING)) { |
| 113 | if (VersionUtil.versionCompare(schemaVersion, fromVersion) < 0) { |
| 114 | String message = VersionMismatchException.createFromMessage(schemaVersion, |
| 115 | fromVersion); |
| 116 | throw new VersionMismatchException(message); |
| 117 | } |
| 118 | } |
| 119 | if (!untilVersion.equals(VersionUtil.DEFAULT_VERSION_STRING)) { |
| 120 | if (VersionUtil.versionCompare(untilVersion, schemaVersion) < 0) { |
| 121 | String message = VersionMismatchException.createToMessage(schemaVersion, |
| 122 | untilVersion); |
| 123 | throw new VersionMismatchException(message); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Returns TableSchema from dbSchema by table name. |
| 130 | * @return TableSchema |
| 131 | */ |
| 132 | private TableSchema getTableSchema() { |
| 133 | String tableName = tableDesc.name(); |
| 134 | return dbSchema.getTableSchema(tableName); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Returns ColumnSchema from TableSchema by column name. |
| 139 | * @param columnName column name |
| 140 | * @return ColumnSchema |
| 141 | */ |
| 142 | private ColumnSchema getColumnSchema(String columnName) { |
| 143 | TableSchema tableSchema = getTableSchema(); |
| 144 | if (tableSchema == null) { |
| 145 | String message = TableSchemaNotFoundException.createMessage(tableDesc.name(), |
| 146 | dbSchema.name()); |
| 147 | throw new TableSchemaNotFoundException(message); |
| 148 | } |
| 149 | ColumnSchema columnSchema = tableSchema.getColumnSchema(columnName); |
| 150 | if (columnSchema == null) { |
| 151 | String message = ColumnSchemaNotFoundException.createMessage(columnName, |
| 152 | tableSchema.name()); |
| 153 | throw new ColumnSchemaNotFoundException(message); |
| 154 | } |
| 155 | return columnSchema; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public Column getColumnHandler(ColumnDescription columnDesc) { |
| 160 | if (!isValid()) { |
| 161 | return null; |
| 162 | } |
| 163 | String columnName = columnDesc.name(); |
| 164 | checkColumnSchemaVersion(columnDesc); |
| 165 | ColumnSchema columnSchema = getColumnSchema(columnName); |
| 166 | if (row == null) { |
| 167 | return null; |
| 168 | } |
| 169 | return row.getColumn(columnSchema.name()); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public Object getDataHandler(ColumnDescription columnDesc) { |
| 174 | if (!isValid()) { |
| 175 | return null; |
| 176 | } |
| 177 | String columnName = columnDesc.name(); |
| 178 | checkColumnSchemaVersion(columnDesc); |
| 179 | ColumnSchema columnSchema = getColumnSchema(columnName); |
| 180 | if (row == null || row.getColumn(columnSchema.name()) == null) { |
| 181 | return null; |
| 182 | } |
| 183 | return row.getColumn(columnSchema.name()).data(); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public void setDataHandler(ColumnDescription columnDesc, Object obj) { |
| 188 | if (!isValid()) { |
| 189 | return; |
| 190 | } |
| 191 | String columnName = columnDesc.name(); |
| 192 | checkColumnSchemaVersion(columnDesc); |
| 193 | ColumnSchema columnSchema = getColumnSchema(columnName); |
| 194 | Column column = new Column(columnSchema.name(), obj); |
| 195 | row.addColumn(columnName, column); |
| 196 | } |
| 197 | |
| 198 | @Override |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 199 | public Uuid getTableUuid() { |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 200 | if (!isValid()) { |
| 201 | return null; |
| 202 | } |
| 203 | ColumnDescription columnDesc = new ColumnDescription("_uuid", "getTableUuid"); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 204 | return (Uuid) getDataHandler(columnDesc); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public Column getTableUuidColumn() { |
| 209 | if (!isValid()) { |
| 210 | return null; |
| 211 | } |
| 212 | ColumnDescription columnDesc = new ColumnDescription("_uuid", "getTableUuidColumn"); |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 213 | return getColumnHandler(columnDesc); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Override |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 217 | public Uuid getTableVersion() { |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 218 | if (!isValid()) { |
| 219 | return null; |
| 220 | } |
| 221 | ColumnDescription columnDesc = new ColumnDescription("_version", "getTableVersion"); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 222 | return (Uuid) getDataHandler(columnDesc); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public Column getTableVersionColumn() { |
| 227 | if (!isValid()) { |
| 228 | return null; |
| 229 | } |
| 230 | ColumnDescription columnDesc = new ColumnDescription("_version", "getTableVersionColumn"); |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 231 | return getColumnHandler(columnDesc); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Get DatabaseSchema entity. |
| 236 | * @return DatabaseSchema entity |
| 237 | */ |
| 238 | public DatabaseSchema dbSchema() { |
| 239 | return dbSchema; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Get Row entity. |
| 244 | * @return Row entity |
| 245 | */ |
| 246 | public Row getRow() { |
| 247 | if (!isValid()) { |
| 248 | return null; |
| 249 | } |
| 250 | return this.row; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Get TableDescription entity. |
| 255 | * @return TableDescription entity |
| 256 | */ |
| 257 | public TableDescription tableDesc() { |
| 258 | return tableDesc; |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public int hashCode() { |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 263 | return row.hashCode(); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public boolean equals(Object obj) { |
| 268 | if (this == obj) { |
| 269 | return true; |
| 270 | } |
| 271 | if (obj instanceof AbstractOvsdbTableService) { |
| 272 | final AbstractOvsdbTableService other = (AbstractOvsdbTableService) obj; |
| 273 | return Objects.equals(this.row, other.row); |
| 274 | } |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public String toString() { |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 280 | TableSchema schema = getTableSchema(); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 281 | String tableName = schema.name(); |
| 282 | return toStringHelper(this).add("tableName", tableName).add("row", row).toString(); |
| 283 | } |
| 284 | } |