lishuai
Committed by Gerrit Code Review

[ONOS-2447]RFC7047's API and its implementation and five main tables.

provide RFC7047's API and its implementation, and five main table which
contain Bridge, Controller, Interface, Open_vSwitch and Port 

Change-Id: I9e995a056fd55c986f5866c85ac712f1792cff4f
Showing 26 changed files with 1619 additions and 0 deletions
......@@ -26,6 +26,14 @@
<groupId>org.onosproject</groupId>
<artifactId>onlab-junit</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-buffer</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId>
</dependency>
</dependencies>
<build>
......
/*
* 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.jsonrpc;
import java.util.List;
import org.onosproject.ovsdb.rfc.message.UpdateNotification;
/**
* The callback function interface will be used when the server send to the
* client report changes.
*/
public interface Callback {
/**
* The "update" notification is sent by the server to the client to report
* changes in tables that are being monitored following a "*monitor"
* request.
* @param updateNotification the information of the update
*/
void update(UpdateNotification updateNotification);
/**
* The "locked" notification is provided to notify a client that it has been
* granted a lock that it had previously requested with the "lock" method.
* @param ids the locked ids
*/
void locked(List<String> ids);
/**
* The "stolen" notification is provided to notify a client, which had
* previously obtained a lock, that another client has stolen ownership of
* that lock.
* @param ids the stolen ids
*/
void stolen(List<String> ids);
}
/*
* 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.jsonrpc;
import java.util.Stack;
/**
* Context for decode parameters.
*/
public class JsonReadContext {
private Stack<Byte> bufStack;
private boolean isStartMatch;
private int lastReadBytes;
/**
* Constructs a JsonReadContext object. This class only need initial
* parameter value for the readToJsonNode method of JsonRpcReaderUtil
* entity.
*/
public JsonReadContext() {
bufStack = new Stack<Byte>();
isStartMatch = false;
lastReadBytes = 0;
}
/**
* Return bufStack.
* @return bufStack
*/
public Stack<Byte> getBufStack() {
return bufStack;
}
/**
* Set bufStack, used for match the braces and double quotes.
* @param bufStack Stack of Byte
*/
public void setBufStack(Stack<Byte> bufStack) {
this.bufStack = bufStack;
}
/**
* Return isStartMatch.
* @return isStartMatch
*/
public boolean isStartMatch() {
return isStartMatch;
}
/**
* Set isStartMatch.
* @param isStartMatch mark whether the matching has started
*/
public void setStartMatch(boolean isStartMatch) {
this.isStartMatch = isStartMatch;
}
/**
* Return lastReadBytes.
* @return lastReadBytes
*/
public int getLastReadBytes() {
return lastReadBytes;
}
/**
* Set lastReadBytes.
* @param lastReadBytes the bytes for last decoding incomplete record
*/
public void setLastReadBytes(int lastReadBytes) {
this.lastReadBytes = lastReadBytes;
}
}
/*
* 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.jsonrpc;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.List;
import java.util.Objects;
import com.google.common.collect.Lists;
/**
* Json Rpc Request information that include id,method,params.
*/
public class JsonRpcRequest {
private final String id;
private final String method;
private final List<Object> params;
/**
* JsonRpcRequest Constructor.
* @param id the id node of request information
* @param method the method node of request information
*/
public JsonRpcRequest(String id, String method) {
checkNotNull(id, "id cannot be null");
checkNotNull(method, "method cannot be null");
this.id = id;
this.method = method;
this.params = Lists.newArrayList();
}
/**
* JsonRpcRequest Constructor.
* @param id the id node of request information
* @param method the method node of request information
* @param params the params node of request information
*/
public JsonRpcRequest(String id, String method, List<Object> params) {
checkNotNull(id, "id cannot be null");
checkNotNull(method, "method cannot be null");
checkNotNull(params, "params cannot be null");
this.id = id;
this.method = method;
this.params = params;
}
/**
* Returns id.
* @return id
*/
public String getId() {
return id;
}
/**
* Returns method.
* @return method
*/
public String getMethod() {
return method;
}
/**
* Returns params.
* @return params
*/
public List<Object> getParams() {
return params;
}
@Override
public int hashCode() {
return Objects.hash(id, method, params);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof JsonRpcRequest) {
final JsonRpcRequest other = (JsonRpcRequest) obj;
return Objects.equals(this.id, other.id)
&& Objects.equals(this.method, other.method)
&& Objects.equals(this.params, other.params);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("id", id).add("method", method)
.add("params", params).toString();
}
}
/*
* 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.jsonrpc;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.List;
import java.util.Objects;
import com.google.common.collect.Lists;
/**
* Json Rpc Response information that include id,error,result.
*/
public class JsonRpcResponse {
private final String id;
private final String error;
private final List<Object> result;
/**
* JsonRpcResponse Constructor.
* @param id the id node of response information
*/
public JsonRpcResponse(String id) {
checkNotNull(id, "id cannot be null");
this.id = id;
this.error = null;
this.result = Lists.newArrayList();
}
/**
* JsonRpcResponse Constructor.
* @param id the id node of response information
* @param error the error node of response information
*/
public JsonRpcResponse(String id, String error) {
checkNotNull(id, "id cannot be null");
checkNotNull(error, "error cannot be null");
this.id = id;
this.error = error;
this.result = Lists.newArrayList();
}
/**
* JsonRpcResponse Constructor.
* @param id the id node of response information
* @param error the error node of response information
* @param result the result node of response information
*/
public JsonRpcResponse(String id, String error, List<Object> result) {
checkNotNull(id, "id cannot be null");
checkNotNull(error, "error cannot be null");
checkNotNull(result, "result cannot be null");
this.id = id;
this.error = error;
this.result = result;
}
/**
* Returns id.
* @return id
*/
public String getId() {
return id;
}
/**
* Returns error.
* @return error
*/
public String getError() {
return error;
}
/**
* Returns result.
* @return result
*/
public List<Object> getResult() {
return result;
}
@Override
public int hashCode() {
return Objects.hash(id, error, result);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof JsonRpcResponse) {
final JsonRpcResponse other = (JsonRpcResponse) obj;
return Objects.equals(this.id, other.id)
&& Objects.equals(this.error, other.error)
&& Objects.equals(this.result, other.result);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("id", id).add("error", error)
.add("result", result).toString();
}
}
/*
* 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.jsonrpc;
import java.util.List;
import org.onosproject.ovsdb.rfc.operations.Operation;
import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.util.concurrent.ListenableFuture;
/**
* The following interface describe the RPC7047's methods that are supported.
*/
public interface OvsdbRPC {
/**
* This operation retrieves a database-schema that describes hosted database
* db-name.
* @param dbnames database name
* @return ListenableFuture of JsonNode
*/
ListenableFuture<JsonNode> getSchema(List<String> dbnames);
/**
* The "echo" method can be used by both clients and servers to verify the
* liveness of a database connection.
* @return return info
*/
ListenableFuture<List<String>> echo();
/**
* The "monitor" request enables a client to replicate tables or subsets of
* tables within an OVSDB database by requesting notifications of changes to
* those tables and by receiving the complete initial state of a table or a
* subset of a table.
* @param dbSchema databse schema
* @param monitorId a id for monitor
* @return ListenableFuture of JsonNode
*/
ListenableFuture<JsonNode> monitor(DatabaseSchema dbSchema, String monitorId);
/**
* This operation retrieves an array whose elements are the names of the
* databases that can be accessed over this management protocol connection.
* @return database names
*/
ListenableFuture<List<String>> listDbs();
/**
* This RPC method causes the database server to execute a series of
* operations in the specified order on a given database.
* @param dbSchema database schema
* @param operations the operations to execute
* @return result the transact result
*/
ListenableFuture<List<JsonNode>> transact(DatabaseSchema dbSchema,
List<Operation> operations);
}
/*
* 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.table;
/**
* Ovsdb table name. Refer to RFC7047's Section 9.2.
*/
public enum OvsdbTable {
INTERFACE("Interface"), BRIDGE("Bridge"), CONTROLLER("Controller"),
PORT("Port"), OPENVSWITCH("Open_vSwitch"), FLWTABLE("Flow_Table"),
QOS("Qos"), QUEUE("Queue"), MIRROR("Mirror"), MANAGER("Manager"),
NETFLOW("NetFlow"), SSL("SSL"), SFLOW("sFlow"), IPFIX("IPFIX"),
FLOWSAMPLECOLLECTORSET("Flow_Sample_Collector_Set");
private final String tableName;
private OvsdbTable(String tableName) {
this.tableName = tableName;
}
/**
* Returns the table name for OvsdbTable.
* @return the table name
*/
public String tableName() {
return tableName;
}
}
/*
* 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.table;
import org.onosproject.ovsdb.rfc.notation.Row;
import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
/**
* Table generator.
*/
public final class TableGenerator {
/**
* Constructs a TableGenerator object. Utility classes should not have a
* public or default constructor, otherwise it will compile failed. This
* class should not be instantiated.
*/
private TableGenerator() {
}
/**
* Create table.
* @param dbSchema DatabaseSchema entity
* @param tableName table name
* @return Object table entity
*/
public static Object createTable(DatabaseSchema dbSchema,
OvsdbTable tableName) {
Row row = new Row();
return generateTable(dbSchema, row, tableName);
}
/**
* Get table from Row.
* @param dbSchema DatabaseSchema entity
* @param row Row entity
* @param tableName table name
* @return Object table entity
*/
public static Object getTable(DatabaseSchema dbSchema, Row row,
OvsdbTable tableName) {
return generateTable(dbSchema, row, tableName);
}
/**
* Generate the table by table name.
* @param dbSchema DatabaseSchema entity
* @param row Row entity
* @param tableName table name
* @return Object Table entity
*/
private static Object generateTable(DatabaseSchema dbSchema, Row row,
OvsdbTable tableName) {
switch (tableName) {
case INTERFACE:
return new Interface(dbSchema, row);
case BRIDGE:
return new Bridge(dbSchema, row);
case CONTROLLER:
return new Controller(dbSchema, row);
case OPENVSWITCH:
return new OpenVSwitch(dbSchema, row);
case PORT:
return new Port(dbSchema, row);
default:
return null;
}
}
}
/*
* 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.table;
/**
* The version number of tables and columns.
*/
public enum VersionNum {
VERSION100("1.0.0"), VERSION102("1.0.2"), VERSION103("1.0.3"),
VERSION104("1.0.4"), VERSION106("1.0.6"), VERSION110("1.1.0"),
VERSION130("1.3.0"), VERSION200("2.0.0"), VERSION300("3.0.0"),
VERSION330("3.3.0"), VERSION350("3.5.0"), VERSION400("4.0.0"),
VERSION510("5.1.0"), VERSION520("5.2.0"), VERSION600("6.0.0"),
VERSION610("6.1.0"), VERSION620("6.2.0"), VERSION630("6.3.0"),
VERSION640("6.4.0"), VERSION650("6.5.0"), VERSION660("6.6.0"),
VERSION670("6.7.0"), VERSION680("6.8.0"), VERSION690("6.9.0"),
VERSION6100("6.10.0"), VERSION6111("6.11.1"), VERSION710("7.1.0"),
VERSION720("7.2.0"), VERSION721("7.2.1"), VERSION730("7.3.0"),
VERSION740("7.4.0"), VERSION750("7.5.0"), VERSION770("7.7.0");
private final String versionNum;
private VersionNum(String versionNum) {
this.versionNum = versionNum;
}
/**
* Returns the version number for VersionNum.
* @return the version number
*/
public String versionNum() {
return versionNum;
}
}
/*
* 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;
}
}
/*
* 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 the TableSchema from row.
* @return Object TableSchema
*/
public Object getTbSchema();
/**
* Returns UUID which column name is _uuid.
* @return UUID
*/
public UUID getUuid();
/**
* Returns UUID Column which column name is _uuid.
* @return UUID Column
*/
public Column getUuidColumn();
/**
* Returns UUID which column name is _version.
* @return UUID
*/
public UUID getVersion();
/**
* Returns UUID Column which column name is _version.
* @return UUID Column
*/
public Column getVersionColumn();
}
/*
* 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;
}
}
/*
* 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.utils;
import org.onosproject.ovsdb.rfc.notation.Condition;
import org.onosproject.ovsdb.rfc.notation.Condition.Function;
/**
* Condition utility class.
*/
public final class ConditionUtil {
/**
* Constructs a ConditionUtil object. Utility classes should not have a
* public or default constructor, otherwise IDE will compile unsuccessfully. This
* class should not be instantiated.
*/
private ConditionUtil() {
}
/**
* Returns a Condition that means Function.EQUALS .
* @param columnName column name
* @param data column value
* @return Condition
*/
public static Condition equals(String columnName, Object data) {
Object value = TransValueUtil.getFormatData(data);
return new Condition(columnName, Function.EQUALS, value);
}
/**
* Returns a Condition that means Function.NOT_EQUALS .
* @param columnName column name
* @param data column value
* @return Condition
*/
public static Condition unEquals(String columnName, Object data) {
Object value = TransValueUtil.getFormatData(data);
return new Condition(columnName, Function.NOT_EQUALS, value);
}
/**
* Returns a Condition that means Function.GREATER_THAN .
* @param columnName column name
* @param data column value
* @return Condition
*/
public static Condition greaterThan(String columnName, Object data) {
Object value = TransValueUtil.getFormatData(data);
return new Condition(columnName, Function.GREATER_THAN, value);
}
/**
* Returns a Condition that means Function.GREATER_THAN_OR_EQUALS .
* @param columnName column name
* @param data column value
* @return Condition
*/
public static Condition greaterThanOrEquals(String columnName, Object data) {
Object value = TransValueUtil.getFormatData(data);
return new Condition(columnName, Function.GREATER_THAN_OR_EQUALS, value);
}
/**
* Returns a Condition that means Function.LESS_THAN .
* @param columnName column name
* @param data column value
* @return Condition
*/
public static Condition lesserThan(String columnName, Object data) {
Object value = TransValueUtil.getFormatData(data);
return new Condition(columnName, Function.LESS_THAN, value);
}
/**
* Returns a Condition that means Function.LESS_THAN_OR_EQUALS .
* @param columnName column name
* @param data column value
* @return Condition
*/
public static Condition lesserThanOrEquals(String columnName, Object data) {
Object value = TransValueUtil.getFormatData(data);
return new Condition(columnName, Function.LESS_THAN_OR_EQUALS, value);
}
/**
* Returns a Condition that means Function.INCLUDES .
* @param columnName column name
* @param data column value
* @return Condition
*/
public static Condition includes(String columnName, Object data) {
Object value = TransValueUtil.getFormatData(data);
return new Condition(columnName, Function.INCLUDES, value);
}
/**
* Returns a Condition that means Function.EXCLUDES .
* @param columnName column name
* @param data column value
* @return Condition
*/
public static Condition excludes(String columnName, Object data) {
Object value = TransValueUtil.getFormatData(data);
return new Condition(columnName, Function.EXCLUDES, value);
}
}
/*
* 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.utils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Stack;
import org.onosproject.ovsdb.rfc.error.UnsupportedEncodingException;
import org.onosproject.ovsdb.rfc.jsonrpc.JsonReadContext;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.core.json.ByteSourceJsonBootstrapper;
import com.fasterxml.jackson.core.util.BufferRecycler;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MappingJsonFactory;
/**
* Decoder utility class.
*/
public final class JsonRpcReaderUtil {
/**
* Constructs a JsonRpcReaderUtil object. Utility classes should not have a
* public or default constructor, otherwise IDE will compile unsuccessfully.
* This class should not be instantiated.
*/
private JsonRpcReaderUtil() {
}
/**
* Decode the bytes to Json object.
* @param in input of bytes
* @param out ouput of Json object list
* @param jrContext context for the last decoding process
* @throws IOException IOException
* @throws JsonParseException JsonParseException
*/
public static void readToJsonNode(ByteBuf in, List<Object> out,
JsonReadContext jrContext)
throws JsonParseException, IOException {
int lastReadBytes = jrContext.getLastReadBytes();
if (lastReadBytes == 0) {
if (in.readableBytes() < 4) {
return;
}
checkEncoding(in);
}
int i = lastReadBytes + in.readerIndex();
Stack<Byte> bufStack = jrContext.getBufStack();
for (; i < in.writerIndex(); i++) {
byte b = in.getByte(i);
switch (b) {
case '{':
if (!isDoubleQuote(bufStack)) {
bufStack.push(b);
jrContext.setStartMatch(true);
}
break;
case '}':
if (!isDoubleQuote(bufStack)) {
bufStack.pop();
}
break;
case '"':
if (in.getByte(i - 1) != '\\') {
if (!bufStack.isEmpty() && bufStack.peek() != '"') {
bufStack.push(b);
} else {
bufStack.pop();
}
}
break;
default:
break;
}
if (jrContext.isStartMatch() && bufStack.isEmpty()) {
ByteBuf buf = in.readSlice(i - in.readerIndex() + 1);
JsonParser jf = new MappingJsonFactory()
.createParser(new ByteBufInputStream(buf));
JsonNode jsonNode = jf.readValueAsTree();
out.add(jsonNode);
lastReadBytes = 0;
jrContext.setLastReadBytes(lastReadBytes);
break;
}
}
if (i >= in.writerIndex()) {
lastReadBytes = in.readableBytes();
jrContext.setLastReadBytes(lastReadBytes);
}
}
/**
* Filter the invalid characters before decoding.
* @param in input of bytes
* @param lastReadBytes the bytes for last decoding incomplete record
*/
private static void fliterCharaters(ByteBuf in) {
while (in.isReadable()) {
int ch = in.getByte(in.readerIndex());
if ((ch != ' ') && (ch != '\n') && (ch != '\t') && (ch != '\r')) {
break;
} else {
in.readByte();
}
}
}
/**
* Check whether the peek of the stack element is double quote.
* @param jrContext context for the last decoding process
* @return boolean
*/
private static boolean isDoubleQuote(Stack<Byte> bufStack) {
if (!bufStack.isEmpty() && bufStack.peek() == '"') {
return true;
}
return false;
}
/**
* 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
*/
private static void checkEncoding(ByteBuf in) throws IOException {
int inputStart = 0;
int inputLength = 4;
fliterCharaters(in);
byte[] buff = new byte[4];
in.getBytes(in.readerIndex(), buff);
ByteSourceJsonBootstrapper strapper = new ByteSourceJsonBootstrapper(
new IOContext(
new BufferRecycler(),
null,
false),
buff,
inputStart,
inputLength);
JsonEncoding jsonEncoding = strapper.detectEncoding();
if (!JsonEncoding.UTF8.equals(jsonEncoding)) {
throw new UnsupportedEncodingException(
"Only UTF-8 encoding is supported.");
}
}
}
/*
* 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.utils;
import java.util.List;
import org.onosproject.ovsdb.rfc.jsonrpc.JsonRpcRequest;
import org.onosproject.ovsdb.rfc.operations.Operation;
import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
/**
* RPC Methods request utility class. Refer to RFC7047's Section 4.1.
*/
public final class JsonRpcWriterUtil {
/**
* Constructs a JsonRpcWriterUtil object. Utility classes should not have a
* public or default constructor, otherwise IDE will compile unsuccessfully.
* This class should not be instantiated.
*/
private JsonRpcWriterUtil() {
}
/**
* Returns string of RPC request.
* @param uuid id of request object
* @param methodName method of request object
* @param params params of request object
* @return RPC Request String
*/
private static String getRequestStr(String uuid, String methodName,
List params) {
JsonRpcRequest request;
if (params != null) {
request = new JsonRpcRequest(uuid, methodName, params);
} else {
request = new JsonRpcRequest(uuid, methodName);
}
String str = ObjectMapperUtil.convertToString(request);
return str;
}
/**
* Returns string of get_schema request.
* @param uuid id of get_schema request
* @param dbnames params of get_schema request
* @return get_schema Request String
*/
public static String getSchemaStr(String uuid, List<String> dbnames) {
String methodName = "get_schema";
return getRequestStr(uuid, methodName, dbnames);
}
/**
* Returns string of echo request.
* @param uuid id of echo request
* @return echo Request String
*/
public static String echoStr(String uuid) {
String methodName = "echo";
return getRequestStr(uuid, methodName, null);
}
/**
* Returns string of monitor request.
* @param uuid id of monitor request
* @param monotorId json-value in params of monitor request
* @param dbSchema DatabaseSchema entity
* @return monitor Request String
*/
public static String monitorStr(String uuid, String monotorId,
DatabaseSchema dbSchema) {
String methodName = "monitor";
return getRequestStr(uuid, methodName,
ParamUtil.getMonitorParams(monotorId, dbSchema));
}
/**
* Returns string of list_dbs request.
* @param uuid id of list_dbs request
* @return list_dbs Request String
*/
public static String listDbsStr(String uuid) {
String methodName = "list_dbs";
return getRequestStr(uuid, methodName, null);
}
/**
* Returns string of transact request.
* @param uuid id of transact request
* @param dbSchema DatabaseSchema entity
* @param operations operation* in params of transact request
* @return transact Request String
*/
public static String transactStr(String uuid, DatabaseSchema dbSchema,
List<Operation> operations) {
String methodName = "transact";
return getRequestStr(uuid, methodName,
ParamUtil.getTransactParams(dbSchema, operations));
}
}
package org.onosproject.ovsdb.rfc.utils;
import org.onosproject.ovsdb.rfc.notation.Mutation;
import org.onosproject.ovsdb.rfc.notation.Mutation.Mutator;
public final class MutationUtil {
/**
* Constructs a MutationUtil object. Utility classes should not have a
* public or default constructor, otherwise IDE will compile unsuccessfully. This
* class should not be instantiated.
*/
private MutationUtil() {
}
/**
* Returns a Mutation that means += .
* @param columnName column name
* @param data column value
* @return Mutation
*/
public static Mutation sum(String columnName, Object data) {
Object value = TransValueUtil.getFormatData(data);
return new Mutation(columnName, Mutator.SUM, value);
}
/**
* Returns a Mutation that means -= .
* @param columnName column name
* @param data column value
* @return Mutation
*/
public static Mutation difference(String columnName, Object data) {
Object value = TransValueUtil.getFormatData(data);
return new Mutation(columnName, Mutator.DIFFERENCE, value);
}
/**
* Returns a Mutation that means *= .
* @param columnName column name
* @param data column value
* @return Mutation
*/
public static Mutation product(String columnName, Object data) {
Object value = TransValueUtil.getFormatData(data);
return new Mutation(columnName, Mutator.PRODUCT, value);
}
/**
* Returns a Mutation that means /= .
* @param columnName column name
* @param data column value
* @return Mutation
*/
public static Mutation quotient(String columnName, Object data) {
Object value = TransValueUtil.getFormatData(data);
return new Mutation(columnName, Mutator.QUOTIENT, value);
}
/**
* Returns a Mutation that means %= .
* @param columnName column name
* @param data column value
* @return Mutation
*/
public static Mutation remainder(String columnName, Object data) {
Object value = TransValueUtil.getFormatData(data);
return new Mutation(columnName, Mutator.REMAINDER, value);
}
/**
* Returns a Mutation that means insert .
* @param columnName column name
* @param data column value
* @return Mutation
*/
public static Mutation insert(String columnName, Object data) {
Object value = TransValueUtil.getFormatData(data);
return new Mutation(columnName, Mutator.INSERT, value);
}
/**
* Returns a Mutation that means delete .
* @param columnName column name
* @param data column value
* @return Mutation
*/
public static Mutation delete(String columnName, Object data) {
Object value = TransValueUtil.getFormatData(data);
return new Mutation(columnName, Mutator.DELETE, value);
}
}
/*
* 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.utils;
import java.util.List;
import java.util.Set;
import org.onosproject.ovsdb.rfc.message.MonitorRequest;
import org.onosproject.ovsdb.rfc.message.MonitorSelect;
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.
*/
public final class ParamUtil {
/**
* 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() {
}
/**
* Returns MonitorRequest, refer to RFC7047's Section 4.1.5.
* @param tableSchema entity
* @return MonitorRequest
*/
private static MonitorRequest getAllColumnsMonitorRequest(TableSchema tableSchema) {
String tableName = tableSchema.name();
Set<String> columns = tableSchema.getColumnNames();
MonitorSelect select = new MonitorSelect(true, true, true, true);
MonitorRequest monitorRequest = new MonitorRequest(tableName, columns,
select);
return monitorRequest;
}
/**
* Returns params of monitor method, refer to RFC7047's Section 4.1.5.
* @param monotorId json-value, refer to RFC7047's Section 4.1.5.
* @param dbSchema DatabaseSchema entity
* @return List of Object, the params of monitor request
*/
public static List<Object> getMonitorParams(String monotorId,
DatabaseSchema dbSchema) {
Set<String> tables = dbSchema.getTableNames();
List<MonitorRequest> monitorRequests = Lists.newArrayList();
for (String tableName : tables) {
TableSchema tableSchema = dbSchema.getTableSchema(tableName);
monitorRequests.add(getAllColumnsMonitorRequest(tableSchema));
}
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);
}
/**
* Returns params of transact method, refer to RFC7047's Section 4.1.3.
* @param dbSchema DatabaseSchema entity
* @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;
}
}
/*
* 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.utils;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;
/**
* StringEncoder utility class.Only UTF-8 encoding is supported refer to
* RFC7047's Section 3.1.
*/
public final class StringEncoderUtil {
/**
* Constructs a StringEncoderUtil object. Utility classes should not have a
* public or default constructor, otherwise IDE will compile unsuccessfully. This
* class should not be instantiated.
*/
private StringEncoderUtil() {
}
/**
* Returns StringEncoder of UTF_8 .
* @return StringEncoder
*/
public static StringEncoder getEncoder() {
return new StringEncoder(CharsetUtil.UTF_8);
}
}
/*
* 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.utils;
/**
* Version utility class.
*/
public final class VersionUtil {
/**
* Constructs a VersionUtil object. Utility classes should not have a public
* or default constructor, otherwise IDE will compile unsuccessfully. This
* class should not be instantiated.
*/
private VersionUtil() {
}
public static final String DEFAULT_VERSION_STRING = "0.0.0";
private static final String FORMAT = "(\\d+)\\.(\\d+)\\.(\\d+)";
/**
* Match version by the format.
* @param version the version String
* @throws IllegalArgumentException this is an illegal argument exception
*/
public static void versionMatch(String version) {
if (!version.matches(FORMAT)) {
throw new IllegalArgumentException("<" + version
+ "> does not match format " + FORMAT);
}
}
/**
* Compare fromVersion and toVersion.
* @param fromVersion the initial version
* @param toVersion the end of the version
* @return a long number
*/
public static long versionCompare(String fromVersion, String toVersion) {
Long fromNum = Long.parseLong(fromVersion.replace(".", ""));
Long toNum = Long.parseLong(toVersion.replace(".", ""));
return (fromNum - toNum);
}
}