Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | /** |
nitinanand | 9e8f836 | 2018-05-31 15:11:04 +0530 | [diff] [blame^] | 66 | * Constructs a AbstractOvsdbTableService object. |
| 67 | * @param dbSchema DatabaseSchema entity |
| 68 | * @param row Row entity |
| 69 | * @param table table name |
| 70 | * @param formVersion the initial version |
| 71 | * @param untilVersion the until version |
| 72 | */ |
| 73 | public AbstractOvsdbTableService(DatabaseSchema dbSchema, Row row, OvsdbTable table, |
| 74 | VersionNum formVersion, VersionNum untilVersion) { |
| 75 | checkNotNull(dbSchema, "database schema cannot be null"); |
| 76 | checkNotNull(row, "row cannot be null"); |
| 77 | checkNotNull(table, "table cannot be null"); |
| 78 | checkNotNull(formVersion, "the initial version cannot be null"); |
| 79 | checkNotNull(untilVersion, "the end of the version cannot be null"); |
| 80 | this.dbSchema = dbSchema; |
| 81 | row.setTableName(table.tableName()); |
| 82 | this.row = row; |
| 83 | TableDescription tableDesc = new TableDescription(table, dbSchema.name(), formVersion, untilVersion); |
| 84 | this.tableDesc = tableDesc; |
| 85 | } |
| 86 | |
| 87 | /** |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 88 | * Check whether the parameter of dbSchema is valid and check whether the |
| 89 | * table is existent in Database Schema. |
| 90 | */ |
| 91 | private boolean isValid() { |
| 92 | if (dbSchema == null) { |
| 93 | return false; |
| 94 | } |
| 95 | if (!dbSchema.name().equalsIgnoreCase(tableDesc.database())) { |
| 96 | return false; |
| 97 | } |
| 98 | checkTableSchemaVersion(); |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Check the table version. |
| 104 | */ |
| 105 | private void checkTableSchemaVersion() { |
| 106 | String fromVersion = tableDesc.fromVersion(); |
| 107 | String untilVersion = tableDesc.untilVersion(); |
| 108 | String schemaVersion = dbSchema.version(); |
| 109 | checkVersion(schemaVersion, fromVersion, untilVersion); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Check the column version. |
| 114 | * @param columnDesc ColumnDescription entity |
| 115 | */ |
| 116 | private void checkColumnSchemaVersion(ColumnDescription columnDesc) { |
| 117 | String fromVersion = columnDesc.fromVersion(); |
| 118 | String untilVersion = columnDesc.untilVersion(); |
| 119 | String schemaVersion = dbSchema.version(); |
| 120 | checkVersion(schemaVersion, fromVersion, untilVersion); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Check whether the DatabaseSchema version between the initial version and |
| 125 | * the end of the version. |
| 126 | * @param schemaVersion DatabaseSchema version |
| 127 | * @param fromVersion The initial version |
| 128 | * @param untilVersion The end of the version |
| 129 | * @throws VersionMismatchException this is a version mismatch exception |
| 130 | */ |
| 131 | private void checkVersion(String schemaVersion, String fromVersion, String untilVersion) { |
| 132 | VersionUtil.versionMatch(fromVersion); |
| 133 | VersionUtil.versionMatch(untilVersion); |
| 134 | if (!fromVersion.equals(VersionUtil.DEFAULT_VERSION_STRING)) { |
| 135 | if (VersionUtil.versionCompare(schemaVersion, fromVersion) < 0) { |
| 136 | String message = VersionMismatchException.createFromMessage(schemaVersion, |
| 137 | fromVersion); |
| 138 | throw new VersionMismatchException(message); |
| 139 | } |
| 140 | } |
| 141 | if (!untilVersion.equals(VersionUtil.DEFAULT_VERSION_STRING)) { |
| 142 | if (VersionUtil.versionCompare(untilVersion, schemaVersion) < 0) { |
| 143 | String message = VersionMismatchException.createToMessage(schemaVersion, |
| 144 | untilVersion); |
| 145 | throw new VersionMismatchException(message); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Returns TableSchema from dbSchema by table name. |
| 152 | * @return TableSchema |
| 153 | */ |
| 154 | private TableSchema getTableSchema() { |
| 155 | String tableName = tableDesc.name(); |
| 156 | return dbSchema.getTableSchema(tableName); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Returns ColumnSchema from TableSchema by column name. |
| 161 | * @param columnName column name |
| 162 | * @return ColumnSchema |
| 163 | */ |
| 164 | private ColumnSchema getColumnSchema(String columnName) { |
| 165 | TableSchema tableSchema = getTableSchema(); |
| 166 | if (tableSchema == null) { |
| 167 | String message = TableSchemaNotFoundException.createMessage(tableDesc.name(), |
| 168 | dbSchema.name()); |
| 169 | throw new TableSchemaNotFoundException(message); |
| 170 | } |
| 171 | ColumnSchema columnSchema = tableSchema.getColumnSchema(columnName); |
| 172 | if (columnSchema == null) { |
| 173 | String message = ColumnSchemaNotFoundException.createMessage(columnName, |
| 174 | tableSchema.name()); |
| 175 | throw new ColumnSchemaNotFoundException(message); |
| 176 | } |
| 177 | return columnSchema; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public Column getColumnHandler(ColumnDescription columnDesc) { |
| 182 | if (!isValid()) { |
| 183 | return null; |
| 184 | } |
| 185 | String columnName = columnDesc.name(); |
| 186 | checkColumnSchemaVersion(columnDesc); |
| 187 | ColumnSchema columnSchema = getColumnSchema(columnName); |
| 188 | if (row == null) { |
| 189 | return null; |
| 190 | } |
| 191 | return row.getColumn(columnSchema.name()); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public Object getDataHandler(ColumnDescription columnDesc) { |
| 196 | if (!isValid()) { |
| 197 | return null; |
| 198 | } |
| 199 | String columnName = columnDesc.name(); |
| 200 | checkColumnSchemaVersion(columnDesc); |
| 201 | ColumnSchema columnSchema = getColumnSchema(columnName); |
| 202 | if (row == null || row.getColumn(columnSchema.name()) == null) { |
| 203 | return null; |
| 204 | } |
| 205 | return row.getColumn(columnSchema.name()).data(); |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public void setDataHandler(ColumnDescription columnDesc, Object obj) { |
| 210 | if (!isValid()) { |
| 211 | return; |
| 212 | } |
| 213 | String columnName = columnDesc.name(); |
| 214 | checkColumnSchemaVersion(columnDesc); |
| 215 | ColumnSchema columnSchema = getColumnSchema(columnName); |
| 216 | Column column = new Column(columnSchema.name(), obj); |
| 217 | row.addColumn(columnName, column); |
| 218 | } |
| 219 | |
| 220 | @Override |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 221 | public Uuid getTableUuid() { |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 222 | if (!isValid()) { |
| 223 | return null; |
| 224 | } |
| 225 | ColumnDescription columnDesc = new ColumnDescription("_uuid", "getTableUuid"); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 226 | return (Uuid) getDataHandler(columnDesc); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public Column getTableUuidColumn() { |
| 231 | if (!isValid()) { |
| 232 | return null; |
| 233 | } |
| 234 | ColumnDescription columnDesc = new ColumnDescription("_uuid", "getTableUuidColumn"); |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 235 | return getColumnHandler(columnDesc); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Override |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 239 | public Uuid getTableVersion() { |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 240 | if (!isValid()) { |
| 241 | return null; |
| 242 | } |
| 243 | ColumnDescription columnDesc = new ColumnDescription("_version", "getTableVersion"); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 244 | return (Uuid) getDataHandler(columnDesc); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public Column getTableVersionColumn() { |
| 249 | if (!isValid()) { |
| 250 | return null; |
| 251 | } |
| 252 | ColumnDescription columnDesc = new ColumnDescription("_version", "getTableVersionColumn"); |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 253 | return getColumnHandler(columnDesc); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Get DatabaseSchema entity. |
| 258 | * @return DatabaseSchema entity |
| 259 | */ |
| 260 | public DatabaseSchema dbSchema() { |
| 261 | return dbSchema; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Get Row entity. |
| 266 | * @return Row entity |
| 267 | */ |
| 268 | public Row getRow() { |
| 269 | if (!isValid()) { |
| 270 | return null; |
| 271 | } |
| 272 | return this.row; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Get TableDescription entity. |
| 277 | * @return TableDescription entity |
| 278 | */ |
| 279 | public TableDescription tableDesc() { |
| 280 | return tableDesc; |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public int hashCode() { |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 285 | return row.hashCode(); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public boolean equals(Object obj) { |
| 290 | if (this == obj) { |
| 291 | return true; |
| 292 | } |
| 293 | if (obj instanceof AbstractOvsdbTableService) { |
| 294 | final AbstractOvsdbTableService other = (AbstractOvsdbTableService) obj; |
| 295 | return Objects.equals(this.row, other.row); |
| 296 | } |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | @Override |
| 301 | public String toString() { |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 302 | TableSchema schema = getTableSchema(); |
Sho SHIMIZU | e4efe45 | 2015-08-26 15:06:55 -0700 | [diff] [blame] | 303 | String tableName = schema.name(); |
| 304 | return toStringHelper(this).add("tableName", tableName).add("row", row).toString(); |
| 305 | } |
| 306 | } |