diff options
Diffstat (limited to 'framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice')
5 files changed, 599 insertions, 0 deletions
diff --git a/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/AbstractOvsdbTableService.java b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/AbstractOvsdbTableService.java new file mode 100644 index 00000000..af1a01d6 --- /dev/null +++ b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/AbstractOvsdbTableService.java @@ -0,0 +1,284 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.ovsdb.rfc.tableservice; + +import static com.google.common.base.MoreObjects.toStringHelper; +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.Objects; + +import org.onosproject.ovsdb.rfc.exception.ColumnSchemaNotFoundException; +import org.onosproject.ovsdb.rfc.exception.TableSchemaNotFoundException; +import org.onosproject.ovsdb.rfc.exception.VersionMismatchException; +import org.onosproject.ovsdb.rfc.notation.Column; +import org.onosproject.ovsdb.rfc.notation.Row; +import org.onosproject.ovsdb.rfc.notation.UUID; +import org.onosproject.ovsdb.rfc.schema.ColumnSchema; +import org.onosproject.ovsdb.rfc.schema.DatabaseSchema; +import org.onosproject.ovsdb.rfc.schema.TableSchema; +import org.onosproject.ovsdb.rfc.table.OvsdbTable; +import org.onosproject.ovsdb.rfc.table.VersionNum; +import org.onosproject.ovsdb.rfc.utils.VersionUtil; + +/** + * Representation of conversion between Ovsdb table and Row. + */ +public abstract class AbstractOvsdbTableService implements OvsdbTableService { + + private final DatabaseSchema dbSchema; + private final Row row; + private final TableDescription tableDesc; + + /** + * Constructs a AbstractOvsdbTableService object. + * @param dbSchema DatabaseSchema entity + * @param row Row entity + * @param table table name + * @param formVersion the initial version + */ + public AbstractOvsdbTableService(DatabaseSchema dbSchema, Row row, OvsdbTable table, + VersionNum formVersion) { + checkNotNull(dbSchema, "database schema cannot be null"); + checkNotNull(row, "row cannot be null"); + checkNotNull(table, "table cannot be null"); + checkNotNull(formVersion, "the initial version cannot be null"); + this.dbSchema = dbSchema; + row.setTableName(table.tableName()); + this.row = row; + TableDescription tableDesc = new TableDescription(table, formVersion); + this.tableDesc = tableDesc; + } + + /** + * Check whether the parameter of dbSchema is valid and check whether the + * table is existent in Database Schema. + */ + private boolean isValid() { + if (dbSchema == null) { + return false; + } + if (!dbSchema.name().equalsIgnoreCase(tableDesc.database())) { + return false; + } + checkTableSchemaVersion(); + return true; + } + + /** + * Check the table version. + */ + private void checkTableSchemaVersion() { + String fromVersion = tableDesc.fromVersion(); + String untilVersion = tableDesc.untilVersion(); + String schemaVersion = dbSchema.version(); + checkVersion(schemaVersion, fromVersion, untilVersion); + } + + /** + * Check the column version. + * @param columnDesc ColumnDescription entity + */ + private void checkColumnSchemaVersion(ColumnDescription columnDesc) { + String fromVersion = columnDesc.fromVersion(); + String untilVersion = columnDesc.untilVersion(); + String schemaVersion = dbSchema.version(); + checkVersion(schemaVersion, fromVersion, untilVersion); + } + + /** + * Check whether the DatabaseSchema version between the initial version and + * the end of the version. + * @param schemaVersion DatabaseSchema version + * @param fromVersion The initial version + * @param untilVersion The end of the version + * @throws VersionMismatchException this is a version mismatch exception + */ + private void checkVersion(String schemaVersion, String fromVersion, String untilVersion) { + VersionUtil.versionMatch(fromVersion); + VersionUtil.versionMatch(untilVersion); + if (!fromVersion.equals(VersionUtil.DEFAULT_VERSION_STRING)) { + if (VersionUtil.versionCompare(schemaVersion, fromVersion) < 0) { + String message = VersionMismatchException.createFromMessage(schemaVersion, + fromVersion); + throw new VersionMismatchException(message); + } + } + if (!untilVersion.equals(VersionUtil.DEFAULT_VERSION_STRING)) { + if (VersionUtil.versionCompare(untilVersion, schemaVersion) < 0) { + String message = VersionMismatchException.createToMessage(schemaVersion, + untilVersion); + throw new VersionMismatchException(message); + } + } + } + + /** + * Returns TableSchema from dbSchema by table name. + * @return TableSchema + */ + private TableSchema getTableSchema() { + String tableName = tableDesc.name(); + return dbSchema.getTableSchema(tableName); + } + + /** + * Returns ColumnSchema from TableSchema by column name. + * @param columnName column name + * @return ColumnSchema + */ + private ColumnSchema getColumnSchema(String columnName) { + TableSchema tableSchema = getTableSchema(); + if (tableSchema == null) { + String message = TableSchemaNotFoundException.createMessage(tableDesc.name(), + dbSchema.name()); + throw new TableSchemaNotFoundException(message); + } + ColumnSchema columnSchema = tableSchema.getColumnSchema(columnName); + if (columnSchema == null) { + String message = ColumnSchemaNotFoundException.createMessage(columnName, + tableSchema.name()); + throw new ColumnSchemaNotFoundException(message); + } + return columnSchema; + } + + @Override + public Column getColumnHandler(ColumnDescription columnDesc) { + if (!isValid()) { + return null; + } + String columnName = columnDesc.name(); + checkColumnSchemaVersion(columnDesc); + ColumnSchema columnSchema = getColumnSchema(columnName); + if (row == null) { + return null; + } + return row.getColumn(columnSchema.name()); + } + + @Override + public Object getDataHandler(ColumnDescription columnDesc) { + if (!isValid()) { + return null; + } + String columnName = columnDesc.name(); + checkColumnSchemaVersion(columnDesc); + ColumnSchema columnSchema = getColumnSchema(columnName); + if (row == null || row.getColumn(columnSchema.name()) == null) { + return null; + } + return row.getColumn(columnSchema.name()).data(); + } + + @Override + public void setDataHandler(ColumnDescription columnDesc, Object obj) { + if (!isValid()) { + return; + } + String columnName = columnDesc.name(); + checkColumnSchemaVersion(columnDesc); + ColumnSchema columnSchema = getColumnSchema(columnName); + Column column = new Column(columnSchema.name(), obj); + row.addColumn(columnName, column); + } + + @Override + public UUID getTableUuid() { + if (!isValid()) { + return null; + } + ColumnDescription columnDesc = new ColumnDescription("_uuid", "getTableUuid"); + return (UUID) getDataHandler(columnDesc); + } + + @Override + public Column getTableUuidColumn() { + if (!isValid()) { + return null; + } + ColumnDescription columnDesc = new ColumnDescription("_uuid", "getTableUuidColumn"); + return getColumnHandler(columnDesc); + } + + @Override + public UUID getTableVersion() { + if (!isValid()) { + return null; + } + ColumnDescription columnDesc = new ColumnDescription("_version", "getTableVersion"); + return (UUID) getDataHandler(columnDesc); + } + + @Override + public Column getTableVersionColumn() { + if (!isValid()) { + return null; + } + ColumnDescription columnDesc = new ColumnDescription("_version", "getTableVersionColumn"); + return getColumnHandler(columnDesc); + } + + /** + * Get DatabaseSchema entity. + * @return DatabaseSchema entity + */ + public DatabaseSchema dbSchema() { + return dbSchema; + } + + /** + * Get Row entity. + * @return Row entity + */ + public Row getRow() { + if (!isValid()) { + return null; + } + return this.row; + } + + /** + * Get TableDescription entity. + * @return TableDescription entity + */ + public TableDescription tableDesc() { + return tableDesc; + } + + @Override + public int hashCode() { + return row.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj instanceof AbstractOvsdbTableService) { + final AbstractOvsdbTableService other = (AbstractOvsdbTableService) obj; + return Objects.equals(this.row, other.row); + } + return false; + } + + @Override + public String toString() { + TableSchema schema = getTableSchema(); + String tableName = schema.name(); + return toStringHelper(this).add("tableName", tableName).add("row", row).toString(); + } +} diff --git a/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/ColumnDescription.java b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/ColumnDescription.java new file mode 100644 index 00000000..910b3296 --- /dev/null +++ b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/ColumnDescription.java @@ -0,0 +1,117 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.ovsdb.rfc.tableservice; + +import static com.google.common.base.Preconditions.checkNotNull; + +import org.onosproject.ovsdb.rfc.table.VersionNum; +import org.onosproject.ovsdb.rfc.utils.VersionUtil; + +/** + * Column description. + */ +public class ColumnDescription { + + // The column name + private final String name; + // The method name + private final String method; + // The initial version + private final String fromVersion; + // The end of the version + private final String untilVersion; + + /** + * Constructs a MonitorRequest object. + * @param name column name + * @param method method name + */ + public ColumnDescription(String name, String method) { + checkNotNull(name, "name cannot be null"); + checkNotNull(method, "method cannot be null"); + this.name = name; + this.method = method; + this.fromVersion = VersionUtil.DEFAULT_VERSION_STRING; + this.untilVersion = VersionUtil.DEFAULT_VERSION_STRING; + } + + /** + * Constructs a MonitorRequest object. + * @param name column name + * @param method method name + * @param fromVersion the initial version + */ + public ColumnDescription(String name, String method, VersionNum fromVersion) { + checkNotNull(name, "name cannot be null"); + checkNotNull(method, "method cannot be null"); + checkNotNull(fromVersion, "the initial version cannot be null"); + this.name = name; + this.method = method; + this.fromVersion = fromVersion.versionNum(); + this.untilVersion = VersionUtil.DEFAULT_VERSION_STRING; + } + + /** + * Constructs a MonitorRequest object. + * @param name column name + * @param method method name + * @param fromVersion the initial version + * @param untilVersion the end of the version + */ + public ColumnDescription(String name, String method, VersionNum fromVersion, + VersionNum untilVersion) { + checkNotNull(name, "name cannot be null"); + checkNotNull(method, "method cannot be null"); + checkNotNull(fromVersion, "the initial version cannot be null"); + checkNotNull(untilVersion, "the end of the version cannot be null"); + this.name = name; + this.method = method; + this.fromVersion = fromVersion.versionNum(); + this.untilVersion = untilVersion.versionNum(); + } + + /** + * Returns the column name. + * @return the column name + */ + public String name() { + return name; + } + + /** + * Returns the method name. + * @return the method name + */ + public String method() { + return method; + } + + /** + * Returns the initial version. + * @return the initial version + */ + public String fromVersion() { + return fromVersion; + } + + /** + * Returns the end of the version. + * @return the end of the version + */ + public String untilVersion() { + return untilVersion; + } +} diff --git a/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/OvsdbTableService.java b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/OvsdbTableService.java new file mode 100644 index 00000000..58b656ef --- /dev/null +++ b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/OvsdbTableService.java @@ -0,0 +1,70 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.ovsdb.rfc.tableservice; + +import org.onosproject.ovsdb.rfc.notation.Column; +import org.onosproject.ovsdb.rfc.notation.UUID; + +/** + * Representation of conversion between Ovsdb table and Row. + */ +public interface OvsdbTableService { + + /** + * Get Column from row. + * @param columndesc Column description + * @return Column + */ + public Column getColumnHandler(ColumnDescription columndesc); + + /** + * Get Data from row. + * @param columndesc Column description + * @return Object column data + */ + public Object getDataHandler(ColumnDescription columndesc); + + /** + * Set column data of row. + * @param columndesc Column description + * @param obj column data + */ + public void setDataHandler(ColumnDescription columndesc, Object obj); + + /** + * Returns UUID which column name is _uuid. + * @return UUID + */ + public UUID getTableUuid(); + + /** + * Returns UUID Column which column name is _uuid. + * @return UUID Column + */ + public Column getTableUuidColumn(); + + /** + * Returns UUID which column name is _version. + * @return UUID + */ + public UUID getTableVersion(); + + /** + * Returns UUID Column which column name is _version. + * @return UUID Column + */ + public Column getTableVersionColumn(); +} diff --git a/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/TableDescription.java b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/TableDescription.java new file mode 100644 index 00000000..d120cab8 --- /dev/null +++ b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/TableDescription.java @@ -0,0 +1,108 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.ovsdb.rfc.tableservice; + +import static com.google.common.base.Preconditions.checkNotNull; + +import org.onosproject.ovsdb.rfc.table.OvsdbTable; +import org.onosproject.ovsdb.rfc.table.VersionNum; +import org.onosproject.ovsdb.rfc.utils.VersionUtil; + +/** + * Table description. + */ +public class TableDescription { + + // The table name + private final String name; + // The database name + private final String database = "Open_vSwitch"; + // The initial version + private final String fromVersion; + // The end of the version + private final String untilVersion; + + /** + * Constructs a MonitorRequest object. + * @param table OvsdbTable entity + */ + public TableDescription(OvsdbTable table) { + checkNotNull(table, "table cannot be null"); + this.name = table.tableName(); + this.fromVersion = VersionUtil.DEFAULT_VERSION_STRING; + this.untilVersion = VersionUtil.DEFAULT_VERSION_STRING; + } + + /** + * Constructs a MonitorRequest object. + * @param table OvsdbTable entity + * @param fromVersion the initial version + */ + public TableDescription(OvsdbTable table, VersionNum fromVersion) { + checkNotNull(table, "table cannot be null"); + checkNotNull(fromVersion, "the initial version cannot be null"); + this.name = table.tableName(); + this.fromVersion = fromVersion.versionNum(); + this.untilVersion = VersionUtil.DEFAULT_VERSION_STRING; + } + + /** + * Constructs a MonitorRequest object. + * @param table OvsdbTable entity + * @param fromVersion the initial version + * @param untilVersion the end of the version + */ + public TableDescription(OvsdbTable table, VersionNum fromVersion, VersionNum untilVersion) { + checkNotNull(table, "table cannot be null"); + checkNotNull(fromVersion, "the initial version cannot be null"); + checkNotNull(untilVersion, "the end of the version cannot be null"); + this.name = table.tableName(); + this.fromVersion = fromVersion.versionNum(); + this.untilVersion = untilVersion.versionNum(); + } + + /** + * Returns the column name. + * @return the column name + */ + public String name() { + return name; + } + + /** + * Returns the database name. + * @return the database name + */ + public String database() { + return database; + } + + /** + * Returns the initial version. + * @return the initial version + */ + public String fromVersion() { + return fromVersion; + } + + /** + * Returns the end of the version. + * @return the end of the version + */ + public String untilVersion() { + return untilVersion; + } +} diff --git a/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/package-info.java b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/package-info.java new file mode 100644 index 00000000..cc607da7 --- /dev/null +++ b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * OVSDB table service definitions. + */ +package org.onosproject.ovsdb.rfc.tableservice; |