lishuai
Committed by Gerrit Code Review

[ONOS-2563]Optimize RFC 7047's code.

Change-Id: I000f2b802420e4f47ce320f6ed021049bd2fd83e
Showing 23 changed files with 189 additions and 451 deletions
......@@ -16,25 +16,25 @@
package org.onosproject.ovsdb.rfc.error;
/**
* AbnormalSchema exception is thrown when the received schema is invalid.
* AbnormalJsonNodeException exception is thrown when the received JsonNode is invalid.
*/
public class AbnormalSchemaException extends RuntimeException {
public class AbnormalJsonNodeException extends RuntimeException {
private static final long serialVersionUID = 8328377718334680368L;
/**
* Constructs a AbnormalSchemaException object.
* Constructs a AbnormalJsonNodeException object.
* @param message error message
*/
public AbnormalSchemaException(String message) {
public AbnormalJsonNodeException(String message) {
super(message);
}
/**
* Constructs a AbnormalSchemaException object.
* Constructs a AbnormalJsonNodeException object.
* @param message error message
* @param cause Throwable
*/
public AbnormalSchemaException(String message, Throwable cause) {
public AbnormalJsonNodeException(String message, Throwable cause) {
super(message, cause);
}
......
/*
* 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.error;
/**
* This exception is thrown when the argument is not supported.
*/
public class ArgumentException extends RuntimeException {
private static final long serialVersionUID = 4950089877540156797L;
/**
* Constructs a ArgumentException object.
* @param message error message
*/
public ArgumentException(String message) {
super(message);
}
/**
* Constructs a ArgumentException object.
* @param message error message
* @param cause Throwable
*/
public ArgumentException(String message, Throwable cause) {
super(message, cause);
}
}
/*
* 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.error;
/**
* The JsonParsingException is thrown when JSON could not be successfully
* parsed.
*/
public class JsonParsingException extends RuntimeException {
private static final long serialVersionUID = 1424752181911923235L;
/**
* Constructs a JsonParsingException object.
* @param message error message
*/
public JsonParsingException(String message) {
super(message);
}
/**
* Constructs a JsonParsingException object.
* @param message error message
* @param cause Throwable
*/
public JsonParsingException(String message, Throwable cause) {
super(message, cause);
}
/**
* Constructs a JsonParsingException object.
* @param cause Throwable
*/
public JsonParsingException(Throwable cause) {
super(cause);
}
/**
* Constructs a JsonParsingException object.
* @param message error message
* @param cause Throwable
* @param enableSuppression enable Suppression
* @param writableStackTrace writable StackTrace
*/
public JsonParsingException(String message, Throwable cause,
boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
/*
* 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.error;
/**
* This is a generic exception thrown by the Typed Schema utilities.
*/
public class TypedSchemaException extends RuntimeException {
private static final long serialVersionUID = -1452257990783176715L;
/**
* Constructs a TypedSchemaException object.
* @param message error message
*/
public TypedSchemaException(String message) {
super(message);
}
/**
* Constructs a TypedSchemaException object.
* @param message error message
* @param cause Throwable
*/
public TypedSchemaException(String message, Throwable cause) {
super(message, cause);
}
}
package org.onosproject.ovsdb.rfc.error;
/**
* This exception is thrown when the encoding does not meet UTF-8 in RFC7047.
*/
public class UnsupportedEncodingException extends RuntimeException {
private static final long serialVersionUID = -4865311369828520666L;
/**
* Constructs a UnsupportedEncodingException object.
* @param message error message
*/
public UnsupportedEncodingException(String message) {
super(message);
}
}
......@@ -16,26 +16,26 @@
package org.onosproject.ovsdb.rfc.error;
/**
* This exception is thrown when a result does not meet any of the known formats
* in RFC7047.
* This exception is thrown when the caller invoke the unsupported method or
* use the encoding is not supported.
*/
public class UnknownResultException extends RuntimeException {
public class UnsupportedException extends RuntimeException {
private static final long serialVersionUID = 1377011546616825375L;
/**
* Constructs a UnknownResultException object.
* Constructs a UnsupportedException object.
* @param message error message
*/
public UnknownResultException(String message) {
public UnsupportedException(String message) {
super(message);
}
/**
* Constructs a UnknownResultException object.
* Constructs a UnsupportedException object.
* @param message error message
* @param cause Throwable
*/
public UnknownResultException(String message, Throwable cause) {
public UnsupportedException(String message, Throwable cause) {
super(message, cause);
}
}
......
......@@ -15,7 +15,6 @@
*/
package org.onosproject.ovsdb.rfc.error;
import static com.google.common.base.MoreObjects.toStringHelper;
/**
* This exception is used when the a table or row is accessed though a typed
......@@ -47,15 +46,9 @@ public class VersionMismatchException extends RuntimeException {
* @param fromVersion the initial version
* @return message
*/
public static String createFromMessage(String actualVersion,
String fromVersion) {
String message = toStringHelper("VersionMismatchException")
.addValue("The fromVersion should less than the actualVersion.\n"
+ "fromVersion: "
+ fromVersion
+ ".\n"
+ "actualVersion: " + actualVersion)
.toString();
public static String createFromMessage(String actualVersion, String fromVersion) {
String message = "The fromVersion should less than the actualVersion.\n fromVersion: "
+ fromVersion + ".\n" + "actualVersion: " + actualVersion;
return message;
}
......@@ -66,13 +59,8 @@ public class VersionMismatchException extends RuntimeException {
* @return message
*/
public static String createToMessage(String actualVersion, String toVersion) {
String message = toStringHelper("VersionMismatchException")
.addValue("The toVersion should greater than the required version.\n"
+ "toVersion: "
+ toVersion
+ ".\n"
+ "Actual Version: " + actualVersion)
.toString();
String message = "The toVersion should greater than the actualVersion.\n"
+ "toVersion: " + toVersion + ".\n" + " actualVersion: " + actualVersion;
return message;
}
}
......
......@@ -32,18 +32,18 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
*/
@JsonDeserialize(converter = UpdateNotificationConverter.class)
public final class UpdateNotification {
private final Object context;
private final Object jsonValue;
private final JsonNode tbUpdatesJsonNode;
/**
* Constructs a UpdateNotification object.
* @param context the "json-value" in "params" of the result JsonNode
* @param jsonValue the "json-value" in "params" of the result JsonNode
* @param tbUpdatesJsonNode the "table-updates" in "params" of the result JsonNode
*/
public UpdateNotification(Object context, JsonNode tbUpdatesJsonNode) {
checkNotNull(context, "context cannot be null");
public UpdateNotification(Object jsonValue, JsonNode tbUpdatesJsonNode) {
checkNotNull(jsonValue, "jsonValue cannot be null");
checkNotNull(tbUpdatesJsonNode, "tablebUpdates JsonNode cannot be null");
this.context = context;
this.jsonValue = jsonValue;
this.tbUpdatesJsonNode = tbUpdatesJsonNode;
}
......@@ -51,8 +51,8 @@ public final class UpdateNotification {
* Return context.
* @return context
*/
public Object context() {
return context;
public Object jsonValue() {
return jsonValue;
}
/**
......@@ -65,7 +65,7 @@ public final class UpdateNotification {
@Override
public int hashCode() {
return Objects.hash(context, tbUpdatesJsonNode);
return Objects.hash(jsonValue, tbUpdatesJsonNode);
}
@Override
......@@ -75,7 +75,7 @@ public final class UpdateNotification {
}
if (obj instanceof UpdateNotification) {
final UpdateNotification other = (UpdateNotification) obj;
return Objects.equals(this.context, other.context)
return Objects.equals(this.jsonValue, other.jsonValue)
&& Objects.equals(this.tbUpdatesJsonNode,
other.tbUpdatesJsonNode);
}
......@@ -84,7 +84,7 @@ public final class UpdateNotification {
@Override
public String toString() {
return toStringHelper(this).add("context", context)
return toStringHelper(this).add("jsonValue", jsonValue)
.add("tbUpdatesJsonNode", tbUpdatesJsonNode).toString();
}
}
......
......@@ -20,27 +20,22 @@ import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Objects;
import org.onosproject.ovsdb.rfc.schema.ColumnSchema;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* Column is the basic element of the OpenVswitch database.
*/
public final class Column {
@JsonIgnore
private final ColumnSchema schema;
private final String columnName;
private final Object data;
/**
* Column constructor.
* @param schema the column schema
* @param columnName the column name
* @param obj the data of the column
*/
public Column(ColumnSchema schema, Object obj) {
checkNotNull(schema, "schema cannot be null");
public Column(String columnName, Object obj) {
checkNotNull(columnName, "columnName cannot be null");
checkNotNull(obj, "data cannot be null");
this.schema = schema;
this.columnName = columnName;
this.data = obj;
}
......@@ -53,16 +48,16 @@ public final class Column {
}
/**
* Returns ColumnSchema.
* @return ColumnSchema
* Returns columnName.
* @return columnName
*/
public ColumnSchema schema() {
return schema;
public String columnName() {
return columnName;
}
@Override
public int hashCode() {
return Objects.hash(schema, data);
return Objects.hash(columnName, data);
}
@Override
......@@ -72,7 +67,7 @@ public final class Column {
}
if (obj instanceof Column) {
final Column other = (Column) obj;
return Objects.equals(this.schema, other.schema)
return Objects.equals(this.columnName, other.columnName)
&& Objects.equals(this.data, other.data);
}
return false;
......@@ -80,7 +75,7 @@ public final class Column {
@Override
public String toString() {
return toStringHelper(this).add("schema", schema).add("data", data)
.toString();
return toStringHelper(this).add("columnName", columnName)
.add("data", data).toString();
}
}
......
......@@ -19,22 +19,16 @@ import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.onosproject.ovsdb.rfc.schema.ColumnSchema;
import org.onosproject.ovsdb.rfc.schema.TableSchema;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Maps;
/**
* Row is the basic element of the OpenVswitch's table.
*/
public final class Row {
@JsonIgnore
private TableSchema tableSchema;
private String tableName;
private Map<String, Column> columns;
/**
......@@ -46,52 +40,49 @@ public final class Row {
/**
* Row constructor.
* @param tableSchema TableSchema entity
* @param tableName table name
*/
public Row(TableSchema tableSchema) {
checkNotNull(tableSchema, "tableSchema cannot be null");
this.tableSchema = tableSchema;
public Row(String tableName) {
checkNotNull(tableName, "tableName cannot be null");
this.tableName = tableName;
this.columns = Maps.newHashMap();
}
/**
* Row constructor.
* @param tableSchema TableSchema entity
* @param columns List of Column entity
* @param tableName table name
* @param columns Map of Column entity
*/
public Row(TableSchema tableSchema, List<Column> columns) {
checkNotNull(tableSchema, "tableSchema cannot be null");
public Row(String tableName, Map<String, Column> columns) {
checkNotNull(tableName, "table name cannot be null");
checkNotNull(columns, "columns cannot be null");
this.tableSchema = tableSchema;
this.columns = Maps.newHashMap();
for (Column column : columns) {
this.columns.put(column.schema().name(), column);
}
this.tableName = tableName;
this.columns = columns;
}
/**
* Returns tableSchema.
* @return tableSchema
* Returns tableName.
* @return tableName
*/
public TableSchema getTableSchema() {
return tableSchema;
public String tableName() {
return tableName;
}
/**
* Set tableSchema value.
* @param tableSchema TableSchema entity
* Set tableName value.
* @param tableName table name
*/
public void setTableSchema(TableSchema tableSchema) {
this.tableSchema = tableSchema;
public void setTableName(String tableName) {
this.tableName = tableName;
}
/**
* Returns Column by ColumnSchema.
* @param schema ColumnSchema entity
* @param columnName column name
* @return Column
*/
public Column getColumn(ColumnSchema schema) {
return (Column) columns.get(schema.name());
public Column getColumn(String columnName) {
return columns.get(columnName);
}
/**
......@@ -113,7 +104,7 @@ public final class Row {
@Override
public int hashCode() {
return Objects.hash(tableSchema, columns);
return Objects.hash(tableName, columns);
}
@Override
......@@ -123,7 +114,7 @@ public final class Row {
}
if (obj instanceof Row) {
final Row other = (Row) obj;
return Objects.equals(this.tableSchema, other.tableSchema)
return Objects.equals(this.tableName, other.tableName)
&& Objects.equals(this.columns, other.columns);
}
return false;
......@@ -131,6 +122,7 @@ public final class Row {
@Override
public String toString() {
return toStringHelper(this).add("tableSchema", tableSchema).add("columns", columns).toString();
return toStringHelper(this).add("tableName", tableName)
.add("columns", columns).toString();
}
}
......
......@@ -72,6 +72,6 @@ public final class Delete implements Operation {
*/
@JsonProperty
public String getTable() {
return (tableSchema == null) ? null : tableSchema.name();
return tableSchema.name();
}
}
......
......@@ -22,7 +22,6 @@ import java.util.Map;
import org.onosproject.ovsdb.rfc.notation.Column;
import org.onosproject.ovsdb.rfc.notation.Row;
import org.onosproject.ovsdb.rfc.schema.ColumnSchema;
import org.onosproject.ovsdb.rfc.schema.TableSchema;
import org.onosproject.ovsdb.rfc.utils.TransValueUtil;
......@@ -67,10 +66,10 @@ public final class Insert implements Operation {
private void generateOperationRow(Row row) {
Collection<Column> columns = row.getColumns();
for (Column column : columns) {
ColumnSchema columnSchema = column.schema();
String columnName = column.columnName();
Object value = column.data();
Object untypedValue = TransValueUtil.getFormatData(value);
this.row.put(columnSchema.name(), untypedValue);
Object formatValue = TransValueUtil.getFormatData(value);
this.row.put(columnName, formatValue);
}
}
......@@ -106,6 +105,6 @@ public final class Insert implements Operation {
*/
@JsonProperty
public String getTable() {
return (tableSchema == null) ? null : tableSchema.name();
return tableSchema.name();
}
}
......
......@@ -86,6 +86,6 @@ public final class Mutate implements Operation {
*/
@JsonProperty
public String getTable() {
return (tableSchema == null) ? null : tableSchema.name();
return tableSchema.name();
}
}
......
......@@ -84,6 +84,6 @@ public final class Select implements Operation {
*/
@JsonProperty
public String getTable() {
return (tableSchema == null) ? null : tableSchema.name();
return tableSchema.name();
}
}
......
......@@ -24,7 +24,6 @@ import java.util.Map;
import org.onosproject.ovsdb.rfc.notation.Column;
import org.onosproject.ovsdb.rfc.notation.Condition;
import org.onosproject.ovsdb.rfc.notation.Row;
import org.onosproject.ovsdb.rfc.schema.ColumnSchema;
import org.onosproject.ovsdb.rfc.schema.TableSchema;
import org.onosproject.ovsdb.rfc.utils.TransValueUtil;
......@@ -68,10 +67,10 @@ public final class Update implements Operation {
private void generateOperationRow(Row row) {
Collection<Column> columns = row.getColumns();
for (Column column : columns) {
ColumnSchema columnSchema = column.schema();
String columnName = column.columnName();
Object value = column.data();
Object untypedValue = TransValueUtil.getFormatData(value);
this.row.put(columnSchema.name(), untypedValue);
Object formatValue = TransValueUtil.getFormatData(value);
this.row.put(columnName, formatValue);
}
}
......@@ -107,6 +106,6 @@ public final class Update implements Operation {
*/
@JsonProperty
public String getTable() {
return (tableSchema == null) ? null : tableSchema.name();
return tableSchema.name();
}
}
......
......@@ -17,8 +17,9 @@ package org.onosproject.ovsdb.rfc.schema.type;
import java.util.Set;
import org.onosproject.ovsdb.rfc.error.TypedSchemaException;
import org.onosproject.ovsdb.rfc.error.AbnormalJsonNodeException;
import org.onosproject.ovsdb.rfc.schema.type.UuidBaseType.RefType;
import org.onosproject.ovsdb.rfc.utils.ObjectMapperUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.Sets;
......@@ -29,27 +30,29 @@ import com.google.common.collect.Sets;
public final class BaseTypeFactory {
/**
* Constructs a BaseTypeFactory object.
* This class should not be instantiated.
* Constructs a BaseTypeFactory object. This class should not be
* instantiated.
*/
private BaseTypeFactory() {
}
/**
* Create a BaseType from the JsonNode.
* @param json the BaseType JsonNode
* @param baseTypeJson the BaseType JsonNode
* @param keyorval the key node or value node
* @return BaseType
*/
public static BaseType getBaseTypeFromJson(JsonNode json, String keyorval) {
if (json.isValueNode()) {
String type = json.asText().trim();
public static BaseType getBaseTypeFromJson(JsonNode baseTypeJson, String keyorval) {
if (baseTypeJson.isValueNode()) {
String type = baseTypeJson.asText().trim();
return fromTypeStr(type);
} else {
if (!json.has(keyorval)) {
throw new TypedSchemaException("not a type");
if (!baseTypeJson.has(keyorval)) {
String message = "Abnormal BaseType JsonNode, it should contain 'key' or 'value' node but was not found"
+ ObjectMapperUtil.convertToString(baseTypeJson);
throw new AbnormalJsonNodeException(message);
}
return fromJsonNode(json.get(keyorval));
return fromJsonNode(baseTypeJson.get(keyorval));
}
}
......
......@@ -15,7 +15,7 @@
*/
package org.onosproject.ovsdb.rfc.schema.type;
import org.onosproject.ovsdb.rfc.error.TypedSchemaException;
import org.onosproject.ovsdb.rfc.error.AbnormalJsonNodeException;
import org.onosproject.ovsdb.rfc.utils.ObjectMapperUtil;
import com.fasterxml.jackson.databind.JsonNode;
......@@ -26,8 +26,8 @@ import com.fasterxml.jackson.databind.JsonNode;
public final class ColumnTypeFactory {
/**
* Constructs a ColumnTypeFactory object.
* This class should not be instantiated.
* Constructs a ColumnTypeFactory object. This class should not be
* instantiated.
*/
private ColumnTypeFactory() {
}
......@@ -54,20 +54,22 @@ public final class ColumnTypeFactory {
}
/**
* JsonNode like "flow_tables":{"type":{"key":{"maxInteger":254,"minInteger":0,"type":
* JsonNode like
* "flow_tables":{"type":{"key":{"maxInteger":254,"minInteger":0,"type":
* "integer"},"min":0,"value":{"type":"uuid","refTable":"Flow_Table"},"max":
* "unlimited"}}.
* @param json the ColumnType JsonNode
* @param columnTypeJson the ColumnType JsonNode
* @return ColumnType
*/
public static ColumnType getColumnTypeFromJson(JsonNode json) {
if (!json.isObject() || !json.has(Type.VALUE.type())) {
return createAtomicColumnType(json);
} else if (!json.isValueNode() && json.has(Type.VALUE.type())) {
return createKeyValuedColumnType(json);
public static ColumnType getColumnTypeFromJson(JsonNode columnTypeJson) {
if (!columnTypeJson.isObject() || !columnTypeJson.has(Type.VALUE.type())) {
return createAtomicColumnType(columnTypeJson);
} else if (!columnTypeJson.isValueNode() && columnTypeJson.has(Type.VALUE.type())) {
return createKeyValuedColumnType(columnTypeJson);
}
throw new TypedSchemaException("could not find the right column type :"
+ ObjectMapperUtil.convertToString(json));
String message = "Abnormal ColumnType JsonNode, it should be AtomicColumnType or KeyValuedColumnType"
+ ObjectMapperUtil.convertToString(columnTypeJson);
throw new AbnormalJsonNodeException(message);
}
/**
......@@ -76,12 +78,11 @@ public final class ColumnTypeFactory {
* @return AtomicColumnType entity
*/
private static AtomicColumnType createAtomicColumnType(JsonNode json) {
BaseType baseType = BaseTypeFactory
.getBaseTypeFromJson(json, Type.KEY.type());
BaseType baseType = BaseTypeFactory.getBaseTypeFromJson(json, Type.KEY.type());
int min = 1;
int max = 1;
JsonNode node = json.get("min");
if (node != null) {
if (node != null && node.isNumber()) {
min = node.asInt();
}
node = json.get("max");
......@@ -101,14 +102,12 @@ public final class ColumnTypeFactory {
* @return KeyValuedColumnType entity
*/
private static KeyValuedColumnType createKeyValuedColumnType(JsonNode json) {
BaseType keyType = BaseTypeFactory.getBaseTypeFromJson(json,
Type.KEY.type());
BaseType valueType = BaseTypeFactory
.getBaseTypeFromJson(json, Type.VALUE.type());
BaseType keyType = BaseTypeFactory.getBaseTypeFromJson(json, Type.KEY.type());
BaseType valueType = BaseTypeFactory.getBaseTypeFromJson(json, Type.VALUE.type());
int min = 1;
int max = 1;
JsonNode node = json.get("min");
if (node != null) {
if (node != null && node.isNumber()) {
min = node.asInt();
}
node = json.get("max");
......
......@@ -22,7 +22,6 @@ import java.util.Objects;
import org.onosproject.ovsdb.rfc.error.ColumnSchemaNotFoundException;
import org.onosproject.ovsdb.rfc.error.TableSchemaNotFoundException;
import org.onosproject.ovsdb.rfc.error.TypedSchemaException;
import org.onosproject.ovsdb.rfc.error.VersionMismatchException;
import org.onosproject.ovsdb.rfc.notation.Column;
import org.onosproject.ovsdb.rfc.notation.Row;
......@@ -50,17 +49,17 @@ public abstract class AbstractOvsdbTableService implements OvsdbTableService {
* @param table table name
* @param formVersion the initial version
*/
public AbstractOvsdbTableService(DatabaseSchema dbSchema, Row row,
OvsdbTable table, VersionNum formVersion) {
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;
row.setTableSchema(dbSchema.getTableSchema(table.tableName()));
}
/**
......@@ -107,21 +106,20 @@ public abstract class AbstractOvsdbTableService implements OvsdbTableService {
* @param untilVersion The end of the version
* @throws VersionMismatchException this is a version mismatch exception
*/
private void checkVersion(String schemaVersion, String fromVersion,
String untilVersion) {
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);
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);
String message = VersionMismatchException.createToMessage(schemaVersion,
untilVersion);
throw new VersionMismatchException(message);
}
}
......@@ -138,13 +136,23 @@ public abstract class AbstractOvsdbTableService implements OvsdbTableService {
/**
* Returns ColumnSchema from TableSchema by column name.
* @param tableSchema TableSchema entity
* @param columnName column name
* @return ColumnSchema
*/
private ColumnSchema getColumnSchema(TableSchema tableSchema,
String columnName) {
return tableSchema.getColumnSchema(columnName);
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
......@@ -154,26 +162,11 @@ public abstract class AbstractOvsdbTableService implements OvsdbTableService {
}
String columnName = columnDesc.name();
checkColumnSchemaVersion(columnDesc);
if (columnName == null) {
throw new TypedSchemaException("Error processing GetColumn : "
+ tableDesc.name() + "." + columnDesc.method());
}
TableSchema tableSchema = getTableSchema();
if (tableSchema == null) {
String message = TableSchemaNotFoundException
.createMessage(tableDesc.name(), dbSchema.name());
throw new TableSchemaNotFoundException(message);
}
ColumnSchema columnSchema = getColumnSchema(tableSchema, columnName);
if (columnSchema == null) {
String message = ColumnSchemaNotFoundException
.createMessage(columnName, tableSchema.name());
throw new ColumnSchemaNotFoundException(message);
}
ColumnSchema columnSchema = getColumnSchema(columnName);
if (row == null) {
return new Column(columnSchema, null);
return null;
}
return row.getColumn(columnSchema);
return row.getColumn(columnSchema.name());
}
@Override
......@@ -183,26 +176,11 @@ public abstract class AbstractOvsdbTableService implements OvsdbTableService {
}
String columnName = columnDesc.name();
checkColumnSchemaVersion(columnDesc);
if (columnName == null) {
throw new TypedSchemaException("Error processing GetColumn : "
+ tableDesc.name() + "." + columnDesc.method());
}
TableSchema tableSchema = getTableSchema();
if (tableSchema == null) {
String message = TableSchemaNotFoundException
.createMessage(tableDesc.name(), dbSchema.name());
throw new TableSchemaNotFoundException(message);
}
ColumnSchema columnSchema = getColumnSchema(tableSchema, columnName);
if (columnSchema == null) {
String message = ColumnSchemaNotFoundException
.createMessage(columnName, tableSchema.name());
throw new ColumnSchemaNotFoundException(message);
}
if (row == null || row.getColumn(columnSchema) == null) {
ColumnSchema columnSchema = getColumnSchema(columnName);
if (row == null || row.getColumn(columnSchema.name()) == null) {
return null;
}
return row.getColumn(columnSchema).data();
return row.getColumn(columnSchema.name()).data();
}
@Override
......@@ -212,64 +190,44 @@ public abstract class AbstractOvsdbTableService implements OvsdbTableService {
}
String columnName = columnDesc.name();
checkColumnSchemaVersion(columnDesc);
if (columnName == null) {
throw new TypedSchemaException("Unable to locate Column Name for "
+ tableDesc.name() + "." + columnDesc.method());
}
TableSchema tableSchema = getTableSchema();
ColumnSchema columnSchema = getColumnSchema(tableSchema, columnName);
Column column = new Column(columnSchema, obj);
ColumnSchema columnSchema = getColumnSchema(columnName);
Column column = new Column(columnSchema.name(), obj);
row.addColumn(columnName, column);
}
@Override
public Object getTbSchema() {
if (!isValid()) {
return null;
}
if (dbSchema == null) {
return null;
}
return getTableSchema();
}
@Override
public UUID getUuid() {
public UUID getTableUuid() {
if (!isValid()) {
return null;
}
ColumnDescription columnDesc = new ColumnDescription("_uuid",
"getTbUuid");
ColumnDescription columnDesc = new ColumnDescription("_uuid", "getTableUuid");
return (UUID) getDataHandler(columnDesc);
}
@Override
public Column getUuidColumn() {
public Column getTableUuidColumn() {
if (!isValid()) {
return null;
}
ColumnDescription columnDesc = new ColumnDescription("_uuid",
"getTbUuidColumn");
ColumnDescription columnDesc = new ColumnDescription("_uuid", "getTableUuidColumn");
return (Column) getColumnHandler(columnDesc);
}
@Override
public UUID getVersion() {
public UUID getTableVersion() {
if (!isValid()) {
return null;
}
ColumnDescription columnDesc = new ColumnDescription("_version",
"getTbVersion");
ColumnDescription columnDesc = new ColumnDescription("_version", "getTableVersion");
return (UUID) getDataHandler(columnDesc);
}
@Override
public Column getVersionColumn() {
public Column getTableVersionColumn() {
if (!isValid()) {
return null;
}
ColumnDescription columnDesc = new ColumnDescription("_version",
"getTbVersionColumn");
ColumnDescription columnDesc = new ColumnDescription("_version", "getTableVersionColumn");
return (Column) getColumnHandler(columnDesc);
}
......@@ -319,9 +277,8 @@ public abstract class AbstractOvsdbTableService implements OvsdbTableService {
@Override
public String toString() {
TableSchema schema = (TableSchema) getTbSchema();
TableSchema schema = (TableSchema) getTableSchema();
String tableName = schema.name();
return toStringHelper(this).add("tableName", tableName).add("row", row)
.toString();
return toStringHelper(this).add("tableName", tableName).add("row", row).toString();
}
}
......
......@@ -45,32 +45,26 @@ public interface OvsdbTableService {
public void setDataHandler(ColumnDescription columndesc, Object obj);
/**
* Returns the TableSchema from row.
* @return Object TableSchema
*/
public Object getTbSchema();
/**
* Returns UUID which column name is _uuid.
* @return UUID
*/
public UUID getUuid();
public UUID getTableUuid();
/**
* Returns UUID Column which column name is _uuid.
* @return UUID Column
*/
public Column getUuidColumn();
public Column getTableUuidColumn();
/**
* Returns UUID which column name is _version.
* @return UUID
*/
public UUID getVersion();
public UUID getTableVersion();
/**
* Returns UUID Column which column name is _version.
* @return UUID Column
*/
public Column getVersionColumn();
public Column getTableVersionColumn();
}
......
......@@ -22,7 +22,7 @@ import java.io.IOException;
import java.util.List;
import java.util.Stack;
import org.onosproject.ovsdb.rfc.error.UnsupportedEncodingException;
import org.onosproject.ovsdb.rfc.error.UnsupportedException;
import org.onosproject.ovsdb.rfc.jsonrpc.JsonReadContext;
import com.fasterxml.jackson.core.JsonEncoding;
......@@ -55,8 +55,7 @@ public final class JsonRpcReaderUtil {
* @throws IOException IOException
* @throws JsonParseException JsonParseException
*/
public static void readToJsonNode(ByteBuf in, List<Object> out,
JsonReadContext jrContext)
public static void readToJsonNode(ByteBuf in, List<Object> out, JsonReadContext jrContext)
throws JsonParseException, IOException {
int lastReadBytes = jrContext.getLastReadBytes();
if (lastReadBytes == 0) {
......@@ -97,8 +96,7 @@ public final class JsonRpcReaderUtil {
if (jrContext.isStartMatch() && bufStack.isEmpty()) {
ByteBuf buf = in.readSlice(i - in.readerIndex() + 1);
JsonParser jf = new MappingJsonFactory()
.createParser(new ByteBufInputStream(buf));
JsonParser jf = new MappingJsonFactory().createParser(new ByteBufInputStream(buf));
JsonNode jsonNode = jf.readValueAsTree();
out.add(jsonNode);
lastReadBytes = 0;
......@@ -145,8 +143,7 @@ public final class JsonRpcReaderUtil {
* Check whether the encoding is valid.
* @param in input of bytes
* @throws IOException this is an IO exception
* @throws UnsupportedEncodingException this is an unsupported encode
* exception
* @throws UnsupportedException this is an unsupported exception
*/
private static void checkEncoding(ByteBuf in) throws IOException {
int inputStart = 0;
......@@ -154,18 +151,14 @@ public final class JsonRpcReaderUtil {
fliterCharaters(in);
byte[] buff = new byte[4];
in.getBytes(in.readerIndex(), buff);
ByteSourceJsonBootstrapper strapper = new ByteSourceJsonBootstrapper(
new IOContext(
new BufferRecycler(),
ByteSourceJsonBootstrapper strapper = new ByteSourceJsonBootstrapper(new IOContext(new BufferRecycler(),
null,
false),
buff,
inputStart,
buff, inputStart,
inputLength);
JsonEncoding jsonEncoding = strapper.detectEncoding();
if (!JsonEncoding.UTF8.equals(jsonEncoding)) {
throw new UnsupportedEncodingException(
"Only UTF-8 encoding is supported.");
throw new UnsupportedException("Only UTF-8 encoding is supported.");
}
}
......
......@@ -16,6 +16,7 @@
package org.onosproject.ovsdb.rfc.utils;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.onosproject.ovsdb.rfc.message.MonitorRequest;
......@@ -24,20 +25,18 @@ import org.onosproject.ovsdb.rfc.operations.Operation;
import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
import org.onosproject.ovsdb.rfc.schema.TableSchema;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
/**
* Params utility class. Params of the request object, refer to RFC7047's Section
* 4.1.
* Params utility class. Params of the request object, refer to RFC7047's
* Section 4.1.
*/
public final class ParamUtil {
/**
* Constructs a ParamUtil object. Utility classes should not have a
* public or default constructor, otherwise IDE will compile unsuccessfully. This
* Constructs a ParamUtil object. Utility classes should not have a public
* or default constructor, otherwise IDE will compile unsuccessfully. This
* class should not be instantiated.
*/
private ParamUtil() {
......@@ -52,8 +51,7 @@ public final class ParamUtil {
String tableName = tableSchema.name();
Set<String> columns = tableSchema.getColumnNames();
MonitorSelect select = new MonitorSelect(true, true, true, true);
MonitorRequest monitorRequest = new MonitorRequest(tableName, columns,
select);
MonitorRequest monitorRequest = new MonitorRequest(tableName, columns, select);
return monitorRequest;
}
......@@ -63,24 +61,15 @@ public final class ParamUtil {
* @param dbSchema DatabaseSchema entity
* @return List of Object, the params of monitor request
*/
public static List<Object> getMonitorParams(String monotorId,
DatabaseSchema dbSchema) {
public static List<Object> getMonitorParams(String monotorId, DatabaseSchema dbSchema) {
Set<String> tables = dbSchema.getTableNames();
List<MonitorRequest> monitorRequests = Lists.newArrayList();
Map<String, MonitorRequest> mrMap = Maps.newHashMap();
for (String tableName : tables) {
TableSchema tableSchema = dbSchema.getTableSchema(tableName);
monitorRequests.add(getAllColumnsMonitorRequest(tableSchema));
MonitorRequest monitorRequest = getAllColumnsMonitorRequest(tableSchema);
mrMap.put(tableName, monitorRequest);
}
ImmutableMap<String, MonitorRequest> reqMap = Maps
.uniqueIndex(monitorRequests,
new Function<MonitorRequest, String>() {
@Override
public String apply(MonitorRequest input) {
return input.getTableName();
}
});
return Lists.<Object>newArrayList(dbSchema.name(), monotorId,
reqMap);
return Lists.newArrayList(dbSchema.name(), monotorId, mrMap);
}
/**
......@@ -89,10 +78,7 @@ public final class ParamUtil {
* @param operations operation*, refer to RFC7047's Section 4.1.3.
* @return List of Object, the params of transact request
*/
public static List<Object> getTransactParams(DatabaseSchema dbSchema,
List<Operation> operations) {
List<Object> lists = Lists.newArrayList((Object) dbSchema.name());
lists.addAll(operations);
return lists;
public static List<Object> getTransactParams(DatabaseSchema dbSchema, List<Operation> operations) {
return Lists.newArrayList(dbSchema.name(), operations);
}
}
......
......@@ -87,33 +87,29 @@ public final class TransValueUtil {
* @param atoType AtomicColumnType entity
* @return Object OvsdbSet or the value of JsonNode
*/
private static Object getValueFromAtoType(JsonNode json,
AtomicColumnType atoType) {
private static Object getValueFromAtoType(JsonNode json, AtomicColumnType atoType) {
BaseType baseType = atoType.baseType();
// If "min" or "max" is not specified, If "min" is not 1 or "max" is not
// 1,
// or both, and "value" is not specified, the type is a set of scalar
// type "key".
// Refer to RFC 7047, Section 3.2 <type>.
// 1, or both, and "value" is not specified, the type is a set of scalar
// type "key". Refer to RFC 7047, Section 3.2 <type>.
if (atoType.min() != atoType.max()) {
Set set = Sets.newHashSet();
if (json.isArray()) {
if (json.size() == 2) {
if (json.get(0).isTextual()
&& "set".equals(json.get(0).asText())) {
if (json.get(0).isTextual() && "set".equals(json.get(0).asText())) {
for (JsonNode node : json.get(1)) {
set.add(TransValueUtil.transToValue(node, baseType));
set.add(transToValue(node, baseType));
}
} else {
set.add(TransValueUtil.transToValue(json, baseType));
set.add(transToValue(json, baseType));
}
}
} else {
set.add(TransValueUtil.transToValue(json, baseType));
set.add(transToValue(json, baseType));
}
return OvsdbSet.ovsdbSet(set);
} else {
return TransValueUtil.transToValue(json, baseType);
return transToValue(json, baseType);
}
}
......@@ -123,19 +119,15 @@ public final class TransValueUtil {
* @param kvType KeyValuedColumnType entity
* @return Object OvsdbMap
*/
private static Object getValueFromKvType(JsonNode json,
KeyValuedColumnType kvType) {
private static Object getValueFromKvType(JsonNode json, KeyValuedColumnType kvType) {
if (json.isArray()) {
if (json.size() == 2) {
if (json.get(0).isTextual()
&& "map".equals(json.get(0).asText())) {
if (json.get(0).isTextual() && "map".equals(json.get(0).asText())) {
Map map = Maps.newHashMap();
for (JsonNode pairNode : json.get(1)) {
if (pairNode.isArray() && json.size() == 2) {
Object key = TransValueUtil.transToValue(pairNode
.get(0), kvType.keyType());
Object value = TransValueUtil.transToValue(pairNode
.get(1), kvType.valueType());
Object key = transToValue(pairNode.get(0), kvType.keyType());
Object value = transToValue(pairNode.get(1), kvType.valueType());
map.put(key, value);
}
}
......@@ -166,14 +158,13 @@ public final class TransValueUtil {
if (valueNode.isArray()) {
if (valueNode.size() == 2) {
if (valueNode.get(0).isTextual()
&& "uuid".equals(valueNode.get(0).asText())
|| "named-uuid".equals(valueNode.get(0).asText())) {
&& ("uuid".equals(valueNode.get(0).asText()) || "named-uuid"
.equals(valueNode.get(0).asText()))) {
return UUID.uuid(valueNode.get(1).asText());
}
}
} else {
return new RefTableRow(((UuidBaseType) baseType).getRefTable(),
valueNode);
return new RefTableRow(((UuidBaseType) baseType).getRefTable(), valueNode);
}
}
return null;
......