2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.ovsdb.rfc.tableservice;
18 import static com.google.common.base.MoreObjects.toStringHelper;
19 import static com.google.common.base.Preconditions.checkNotNull;
21 import java.util.Objects;
23 import org.onosproject.ovsdb.rfc.exception.ColumnSchemaNotFoundException;
24 import org.onosproject.ovsdb.rfc.exception.TableSchemaNotFoundException;
25 import org.onosproject.ovsdb.rfc.exception.VersionMismatchException;
26 import org.onosproject.ovsdb.rfc.notation.Column;
27 import org.onosproject.ovsdb.rfc.notation.Row;
28 import org.onosproject.ovsdb.rfc.notation.UUID;
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;
37 * Representation of conversion between Ovsdb table and Row.
39 public abstract class AbstractOvsdbTableService implements OvsdbTableService {
41 private final DatabaseSchema dbSchema;
42 private final Row row;
43 private final TableDescription tableDesc;
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
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());
61 TableDescription tableDesc = new TableDescription(table, formVersion);
62 this.tableDesc = tableDesc;
66 * Check whether the parameter of dbSchema is valid and check whether the
67 * table is existent in Database Schema.
69 private boolean isValid() {
70 if (dbSchema == null) {
73 if (!dbSchema.name().equalsIgnoreCase(tableDesc.database())) {
76 checkTableSchemaVersion();
81 * Check the table version.
83 private void checkTableSchemaVersion() {
84 String fromVersion = tableDesc.fromVersion();
85 String untilVersion = tableDesc.untilVersion();
86 String schemaVersion = dbSchema.version();
87 checkVersion(schemaVersion, fromVersion, untilVersion);
91 * Check the column version.
92 * @param columnDesc ColumnDescription entity
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);
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
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,
116 throw new VersionMismatchException(message);
119 if (!untilVersion.equals(VersionUtil.DEFAULT_VERSION_STRING)) {
120 if (VersionUtil.versionCompare(untilVersion, schemaVersion) < 0) {
121 String message = VersionMismatchException.createToMessage(schemaVersion,
123 throw new VersionMismatchException(message);
129 * Returns TableSchema from dbSchema by table name.
130 * @return TableSchema
132 private TableSchema getTableSchema() {
133 String tableName = tableDesc.name();
134 return dbSchema.getTableSchema(tableName);
138 * Returns ColumnSchema from TableSchema by column name.
139 * @param columnName column name
140 * @return ColumnSchema
142 private ColumnSchema getColumnSchema(String columnName) {
143 TableSchema tableSchema = getTableSchema();
144 if (tableSchema == null) {
145 String message = TableSchemaNotFoundException.createMessage(tableDesc.name(),
147 throw new TableSchemaNotFoundException(message);
149 ColumnSchema columnSchema = tableSchema.getColumnSchema(columnName);
150 if (columnSchema == null) {
151 String message = ColumnSchemaNotFoundException.createMessage(columnName,
153 throw new ColumnSchemaNotFoundException(message);
159 public Column getColumnHandler(ColumnDescription columnDesc) {
163 String columnName = columnDesc.name();
164 checkColumnSchemaVersion(columnDesc);
165 ColumnSchema columnSchema = getColumnSchema(columnName);
169 return row.getColumn(columnSchema.name());
173 public Object getDataHandler(ColumnDescription columnDesc) {
177 String columnName = columnDesc.name();
178 checkColumnSchemaVersion(columnDesc);
179 ColumnSchema columnSchema = getColumnSchema(columnName);
180 if (row == null || row.getColumn(columnSchema.name()) == null) {
183 return row.getColumn(columnSchema.name()).data();
187 public void setDataHandler(ColumnDescription columnDesc, Object obj) {
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);
199 public UUID getTableUuid() {
203 ColumnDescription columnDesc = new ColumnDescription("_uuid", "getTableUuid");
204 return (UUID) getDataHandler(columnDesc);
208 public Column getTableUuidColumn() {
212 ColumnDescription columnDesc = new ColumnDescription("_uuid", "getTableUuidColumn");
213 return getColumnHandler(columnDesc);
217 public UUID getTableVersion() {
221 ColumnDescription columnDesc = new ColumnDescription("_version", "getTableVersion");
222 return (UUID) getDataHandler(columnDesc);
226 public Column getTableVersionColumn() {
230 ColumnDescription columnDesc = new ColumnDescription("_version", "getTableVersionColumn");
231 return getColumnHandler(columnDesc);
235 * Get DatabaseSchema entity.
236 * @return DatabaseSchema entity
238 public DatabaseSchema dbSchema() {
246 public Row getRow() {
254 * Get TableDescription entity.
255 * @return TableDescription entity
257 public TableDescription tableDesc() {
262 public int hashCode() {
263 return row.hashCode();
267 public boolean equals(Object obj) {
271 if (obj instanceof AbstractOvsdbTableService) {
272 final AbstractOvsdbTableService other = (AbstractOvsdbTableService) obj;
273 return Objects.equals(this.row, other.row);
279 public String toString() {
280 TableSchema schema = getTableSchema();
281 String tableName = schema.name();
282 return toStringHelper(this).add("tableName", tableName).add("row", row).toString();