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 @@ ...@@ -26,6 +26,14 @@
26 <groupId>org.onosproject</groupId> 26 <groupId>org.onosproject</groupId>
27 <artifactId>onlab-junit</artifactId> 27 <artifactId>onlab-junit</artifactId>
28 </dependency> 28 </dependency>
29 + <dependency>
30 + <groupId>io.netty</groupId>
31 + <artifactId>netty-buffer</artifactId>
32 + </dependency>
33 + <dependency>
34 + <groupId>io.netty</groupId>
35 + <artifactId>netty-handler</artifactId>
36 + </dependency>
29 </dependencies> 37 </dependencies>
30 38
31 <build> 39 <build>
......
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.ovsdb.rfc.jsonrpc;
17 +
18 +import java.util.List;
19 +
20 +import org.onosproject.ovsdb.rfc.message.UpdateNotification;
21 +
22 +/**
23 + * The callback function interface will be used when the server send to the
24 + * client report changes.
25 + */
26 +public interface Callback {
27 + /**
28 + * The "update" notification is sent by the server to the client to report
29 + * changes in tables that are being monitored following a "*monitor"
30 + * request.
31 + * @param updateNotification the information of the update
32 + */
33 + void update(UpdateNotification updateNotification);
34 +
35 + /**
36 + * The "locked" notification is provided to notify a client that it has been
37 + * granted a lock that it had previously requested with the "lock" method.
38 + * @param ids the locked ids
39 + */
40 + void locked(List<String> ids);
41 +
42 + /**
43 + * The "stolen" notification is provided to notify a client, which had
44 + * previously obtained a lock, that another client has stolen ownership of
45 + * that lock.
46 + * @param ids the stolen ids
47 + */
48 + void stolen(List<String> ids);
49 +
50 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.ovsdb.rfc.jsonrpc;
17 +
18 +import java.util.Stack;
19 +
20 +/**
21 + * Context for decode parameters.
22 + */
23 +public class JsonReadContext {
24 + private Stack<Byte> bufStack;
25 + private boolean isStartMatch;
26 + private int lastReadBytes;
27 +
28 + /**
29 + * Constructs a JsonReadContext object. This class only need initial
30 + * parameter value for the readToJsonNode method of JsonRpcReaderUtil
31 + * entity.
32 + */
33 + public JsonReadContext() {
34 + bufStack = new Stack<Byte>();
35 + isStartMatch = false;
36 + lastReadBytes = 0;
37 + }
38 +
39 + /**
40 + * Return bufStack.
41 + * @return bufStack
42 + */
43 + public Stack<Byte> getBufStack() {
44 + return bufStack;
45 + }
46 +
47 + /**
48 + * Set bufStack, used for match the braces and double quotes.
49 + * @param bufStack Stack of Byte
50 + */
51 + public void setBufStack(Stack<Byte> bufStack) {
52 + this.bufStack = bufStack;
53 + }
54 +
55 + /**
56 + * Return isStartMatch.
57 + * @return isStartMatch
58 + */
59 + public boolean isStartMatch() {
60 + return isStartMatch;
61 + }
62 +
63 + /**
64 + * Set isStartMatch.
65 + * @param isStartMatch mark whether the matching has started
66 + */
67 + public void setStartMatch(boolean isStartMatch) {
68 + this.isStartMatch = isStartMatch;
69 + }
70 +
71 + /**
72 + * Return lastReadBytes.
73 + * @return lastReadBytes
74 + */
75 + public int getLastReadBytes() {
76 + return lastReadBytes;
77 + }
78 +
79 + /**
80 + * Set lastReadBytes.
81 + * @param lastReadBytes the bytes for last decoding incomplete record
82 + */
83 + public void setLastReadBytes(int lastReadBytes) {
84 + this.lastReadBytes = lastReadBytes;
85 + }
86 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.ovsdb.rfc.jsonrpc;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +import static com.google.common.base.Preconditions.checkNotNull;
20 +
21 +import java.util.List;
22 +import java.util.Objects;
23 +
24 +import com.google.common.collect.Lists;
25 +
26 +/**
27 + * Json Rpc Request information that include id,method,params.
28 + */
29 +public class JsonRpcRequest {
30 +
31 + private final String id;
32 + private final String method;
33 + private final List<Object> params;
34 +
35 + /**
36 + * JsonRpcRequest Constructor.
37 + * @param id the id node of request information
38 + * @param method the method node of request information
39 + */
40 + public JsonRpcRequest(String id, String method) {
41 + checkNotNull(id, "id cannot be null");
42 + checkNotNull(method, "method cannot be null");
43 + this.id = id;
44 + this.method = method;
45 + this.params = Lists.newArrayList();
46 + }
47 +
48 + /**
49 + * JsonRpcRequest Constructor.
50 + * @param id the id node of request information
51 + * @param method the method node of request information
52 + * @param params the params node of request information
53 + */
54 + public JsonRpcRequest(String id, String method, List<Object> params) {
55 + checkNotNull(id, "id cannot be null");
56 + checkNotNull(method, "method cannot be null");
57 + checkNotNull(params, "params cannot be null");
58 + this.id = id;
59 + this.method = method;
60 + this.params = params;
61 + }
62 +
63 + /**
64 + * Returns id.
65 + * @return id
66 + */
67 + public String getId() {
68 + return id;
69 + }
70 +
71 + /**
72 + * Returns method.
73 + * @return method
74 + */
75 + public String getMethod() {
76 + return method;
77 + }
78 +
79 + /**
80 + * Returns params.
81 + * @return params
82 + */
83 + public List<Object> getParams() {
84 + return params;
85 + }
86 +
87 + @Override
88 + public int hashCode() {
89 + return Objects.hash(id, method, params);
90 + }
91 +
92 + @Override
93 + public boolean equals(Object obj) {
94 + if (this == obj) {
95 + return true;
96 + }
97 + if (obj instanceof JsonRpcRequest) {
98 + final JsonRpcRequest other = (JsonRpcRequest) obj;
99 + return Objects.equals(this.id, other.id)
100 + && Objects.equals(this.method, other.method)
101 + && Objects.equals(this.params, other.params);
102 + }
103 + return false;
104 + }
105 +
106 + @Override
107 + public String toString() {
108 + return toStringHelper(this).add("id", id).add("method", method)
109 + .add("params", params).toString();
110 + }
111 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.ovsdb.rfc.jsonrpc;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +import static com.google.common.base.Preconditions.checkNotNull;
20 +
21 +import java.util.List;
22 +import java.util.Objects;
23 +
24 +import com.google.common.collect.Lists;
25 +
26 +/**
27 + * Json Rpc Response information that include id,error,result.
28 + */
29 +public class JsonRpcResponse {
30 +
31 + private final String id;
32 + private final String error;
33 + private final List<Object> result;
34 +
35 + /**
36 + * JsonRpcResponse Constructor.
37 + * @param id the id node of response information
38 + */
39 + public JsonRpcResponse(String id) {
40 + checkNotNull(id, "id cannot be null");
41 + this.id = id;
42 + this.error = null;
43 + this.result = Lists.newArrayList();
44 + }
45 +
46 + /**
47 + * JsonRpcResponse Constructor.
48 + * @param id the id node of response information
49 + * @param error the error node of response information
50 + */
51 + public JsonRpcResponse(String id, String error) {
52 + checkNotNull(id, "id cannot be null");
53 + checkNotNull(error, "error cannot be null");
54 + this.id = id;
55 + this.error = error;
56 + this.result = Lists.newArrayList();
57 + }
58 +
59 + /**
60 + * JsonRpcResponse Constructor.
61 + * @param id the id node of response information
62 + * @param error the error node of response information
63 + * @param result the result node of response information
64 + */
65 + public JsonRpcResponse(String id, String error, List<Object> result) {
66 + checkNotNull(id, "id cannot be null");
67 + checkNotNull(error, "error cannot be null");
68 + checkNotNull(result, "result cannot be null");
69 + this.id = id;
70 + this.error = error;
71 + this.result = result;
72 + }
73 +
74 + /**
75 + * Returns id.
76 + * @return id
77 + */
78 + public String getId() {
79 + return id;
80 + }
81 +
82 + /**
83 + * Returns error.
84 + * @return error
85 + */
86 + public String getError() {
87 + return error;
88 + }
89 +
90 + /**
91 + * Returns result.
92 + * @return result
93 + */
94 + public List<Object> getResult() {
95 + return result;
96 + }
97 +
98 + @Override
99 + public int hashCode() {
100 + return Objects.hash(id, error, result);
101 + }
102 +
103 + @Override
104 + public boolean equals(Object obj) {
105 + if (this == obj) {
106 + return true;
107 + }
108 + if (obj instanceof JsonRpcResponse) {
109 + final JsonRpcResponse other = (JsonRpcResponse) obj;
110 + return Objects.equals(this.id, other.id)
111 + && Objects.equals(this.error, other.error)
112 + && Objects.equals(this.result, other.result);
113 + }
114 + return false;
115 + }
116 +
117 + @Override
118 + public String toString() {
119 + return toStringHelper(this).add("id", id).add("error", error)
120 + .add("result", result).toString();
121 + }
122 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.ovsdb.rfc.jsonrpc;
17 +
18 +import java.util.List;
19 +
20 +import org.onosproject.ovsdb.rfc.operations.Operation;
21 +import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
22 +
23 +import com.fasterxml.jackson.databind.JsonNode;
24 +import com.google.common.util.concurrent.ListenableFuture;
25 +
26 +/**
27 + * The following interface describe the RPC7047's methods that are supported.
28 + */
29 +public interface OvsdbRPC {
30 +
31 + /**
32 + * This operation retrieves a database-schema that describes hosted database
33 + * db-name.
34 + * @param dbnames database name
35 + * @return ListenableFuture of JsonNode
36 + */
37 + ListenableFuture<JsonNode> getSchema(List<String> dbnames);
38 +
39 + /**
40 + * The "echo" method can be used by both clients and servers to verify the
41 + * liveness of a database connection.
42 + * @return return info
43 + */
44 + ListenableFuture<List<String>> echo();
45 +
46 + /**
47 + * The "monitor" request enables a client to replicate tables or subsets of
48 + * tables within an OVSDB database by requesting notifications of changes to
49 + * those tables and by receiving the complete initial state of a table or a
50 + * subset of a table.
51 + * @param dbSchema databse schema
52 + * @param monitorId a id for monitor
53 + * @return ListenableFuture of JsonNode
54 + */
55 + ListenableFuture<JsonNode> monitor(DatabaseSchema dbSchema, String monitorId);
56 +
57 + /**
58 + * This operation retrieves an array whose elements are the names of the
59 + * databases that can be accessed over this management protocol connection.
60 + * @return database names
61 + */
62 + ListenableFuture<List<String>> listDbs();
63 +
64 + /**
65 + * This RPC method causes the database server to execute a series of
66 + * operations in the specified order on a given database.
67 + * @param dbSchema database schema
68 + * @param operations the operations to execute
69 + * @return result the transact result
70 + */
71 + ListenableFuture<List<JsonNode>> transact(DatabaseSchema dbSchema,
72 + List<Operation> operations);
73 +
74 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.ovsdb.rfc.table;
17 +
18 +/**
19 + * Ovsdb table name. Refer to RFC7047's Section 9.2.
20 + */
21 +public enum OvsdbTable {
22 + INTERFACE("Interface"), BRIDGE("Bridge"), CONTROLLER("Controller"),
23 + PORT("Port"), OPENVSWITCH("Open_vSwitch"), FLWTABLE("Flow_Table"),
24 + QOS("Qos"), QUEUE("Queue"), MIRROR("Mirror"), MANAGER("Manager"),
25 + NETFLOW("NetFlow"), SSL("SSL"), SFLOW("sFlow"), IPFIX("IPFIX"),
26 + FLOWSAMPLECOLLECTORSET("Flow_Sample_Collector_Set");
27 +
28 + private final String tableName;
29 +
30 + private OvsdbTable(String tableName) {
31 + this.tableName = tableName;
32 + }
33 +
34 + /**
35 + * Returns the table name for OvsdbTable.
36 + * @return the table name
37 + */
38 + public String tableName() {
39 + return tableName;
40 + }
41 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.ovsdb.rfc.table;
17 +
18 +import org.onosproject.ovsdb.rfc.notation.Row;
19 +import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
20 +
21 +/**
22 + * Table generator.
23 + */
24 +public final class TableGenerator {
25 +
26 + /**
27 + * Constructs a TableGenerator object. Utility classes should not have a
28 + * public or default constructor, otherwise it will compile failed. This
29 + * class should not be instantiated.
30 + */
31 + private TableGenerator() {
32 + }
33 +
34 + /**
35 + * Create table.
36 + * @param dbSchema DatabaseSchema entity
37 + * @param tableName table name
38 + * @return Object table entity
39 + */
40 + public static Object createTable(DatabaseSchema dbSchema,
41 + OvsdbTable tableName) {
42 + Row row = new Row();
43 + return generateTable(dbSchema, row, tableName);
44 + }
45 +
46 + /**
47 + * Get table from Row.
48 + * @param dbSchema DatabaseSchema entity
49 + * @param row Row entity
50 + * @param tableName table name
51 + * @return Object table entity
52 + */
53 + public static Object getTable(DatabaseSchema dbSchema, Row row,
54 + OvsdbTable tableName) {
55 + return generateTable(dbSchema, row, tableName);
56 + }
57 +
58 + /**
59 + * Generate the table by table name.
60 + * @param dbSchema DatabaseSchema entity
61 + * @param row Row entity
62 + * @param tableName table name
63 + * @return Object Table entity
64 + */
65 + private static Object generateTable(DatabaseSchema dbSchema, Row row,
66 + OvsdbTable tableName) {
67 + switch (tableName) {
68 + case INTERFACE:
69 + return new Interface(dbSchema, row);
70 + case BRIDGE:
71 + return new Bridge(dbSchema, row);
72 + case CONTROLLER:
73 + return new Controller(dbSchema, row);
74 + case OPENVSWITCH:
75 + return new OpenVSwitch(dbSchema, row);
76 + case PORT:
77 + return new Port(dbSchema, row);
78 + default:
79 + return null;
80 + }
81 + }
82 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.ovsdb.rfc.table;
17 +
18 +/**
19 + * The version number of tables and columns.
20 + */
21 +public enum VersionNum {
22 + VERSION100("1.0.0"), VERSION102("1.0.2"), VERSION103("1.0.3"),
23 + VERSION104("1.0.4"), VERSION106("1.0.6"), VERSION110("1.1.0"),
24 + VERSION130("1.3.0"), VERSION200("2.0.0"), VERSION300("3.0.0"),
25 + VERSION330("3.3.0"), VERSION350("3.5.0"), VERSION400("4.0.0"),
26 + VERSION510("5.1.0"), VERSION520("5.2.0"), VERSION600("6.0.0"),
27 + VERSION610("6.1.0"), VERSION620("6.2.0"), VERSION630("6.3.0"),
28 + VERSION640("6.4.0"), VERSION650("6.5.0"), VERSION660("6.6.0"),
29 + VERSION670("6.7.0"), VERSION680("6.8.0"), VERSION690("6.9.0"),
30 + VERSION6100("6.10.0"), VERSION6111("6.11.1"), VERSION710("7.1.0"),
31 + VERSION720("7.2.0"), VERSION721("7.2.1"), VERSION730("7.3.0"),
32 + VERSION740("7.4.0"), VERSION750("7.5.0"), VERSION770("7.7.0");
33 +
34 + private final String versionNum;
35 +
36 + private VersionNum(String versionNum) {
37 + this.versionNum = versionNum;
38 + }
39 +
40 + /**
41 + * Returns the version number for VersionNum.
42 + * @return the version number
43 + */
44 + public String versionNum() {
45 + return versionNum;
46 + }
47 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.ovsdb.rfc.tableservice;
17 +
18 +import static com.google.common.base.Preconditions.checkNotNull;
19 +
20 +import org.onosproject.ovsdb.rfc.table.VersionNum;
21 +import org.onosproject.ovsdb.rfc.utils.VersionUtil;
22 +
23 +/**
24 + * Column description.
25 + */
26 +public class ColumnDescription {
27 +
28 + // The column name
29 + private final String name;
30 + // The method name
31 + private final String method;
32 + // The initial version
33 + private final String fromVersion;
34 + // The end of the version
35 + private final String untilVersion;
36 +
37 + /**
38 + * Constructs a MonitorRequest object.
39 + * @param name column name
40 + * @param method method name
41 + */
42 + public ColumnDescription(String name, String method) {
43 + checkNotNull(name, "name cannot be null");
44 + checkNotNull(method, "method cannot be null");
45 + this.name = name;
46 + this.method = method;
47 + this.fromVersion = VersionUtil.DEFAULT_VERSION_STRING;
48 + this.untilVersion = VersionUtil.DEFAULT_VERSION_STRING;
49 + }
50 +
51 + /**
52 + * Constructs a MonitorRequest object.
53 + * @param name column name
54 + * @param method method name
55 + * @param fromVersion the initial version
56 + */
57 + public ColumnDescription(String name, String method, VersionNum fromVersion) {
58 + checkNotNull(name, "name cannot be null");
59 + checkNotNull(method, "method cannot be null");
60 + checkNotNull(fromVersion, "the initial version cannot be null");
61 + this.name = name;
62 + this.method = method;
63 + this.fromVersion = fromVersion.versionNum();
64 + this.untilVersion = VersionUtil.DEFAULT_VERSION_STRING;
65 + }
66 +
67 + /**
68 + * Constructs a MonitorRequest object.
69 + * @param name column name
70 + * @param method method name
71 + * @param fromVersion the initial version
72 + * @param untilVersion the end of the version
73 + */
74 + public ColumnDescription(String name, String method, VersionNum fromVersion,
75 + VersionNum untilVersion) {
76 + checkNotNull(name, "name cannot be null");
77 + checkNotNull(method, "method cannot be null");
78 + checkNotNull(fromVersion, "the initial version cannot be null");
79 + checkNotNull(untilVersion, "the end of the version cannot be null");
80 + this.name = name;
81 + this.method = method;
82 + this.fromVersion = fromVersion.versionNum();
83 + this.untilVersion = untilVersion.versionNum();
84 + }
85 +
86 + /**
87 + * Returns the column name.
88 + * @return the column name
89 + */
90 + public String name() {
91 + return name;
92 + }
93 +
94 + /**
95 + * Returns the method name.
96 + * @return the method name
97 + */
98 + public String method() {
99 + return method;
100 + }
101 +
102 + /**
103 + * Returns the initial version.
104 + * @return the initial version
105 + */
106 + public String fromVersion() {
107 + return fromVersion;
108 + }
109 +
110 + /**
111 + * Returns the end of the version.
112 + * @return the end of the version
113 + */
114 + public String untilVersion() {
115 + return untilVersion;
116 + }
117 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.ovsdb.rfc.tableservice;
17 +
18 +import org.onosproject.ovsdb.rfc.notation.Column;
19 +import org.onosproject.ovsdb.rfc.notation.UUID;
20 +
21 +/**
22 + * Representation of conversion between Ovsdb table and Row.
23 + */
24 +public interface OvsdbTableService {
25 +
26 + /**
27 + * Get Column from row.
28 + * @param columndesc Column description
29 + * @return Column
30 + */
31 + public Column getColumnHandler(ColumnDescription columndesc);
32 +
33 + /**
34 + * Get Data from row.
35 + * @param columndesc Column description
36 + * @return Object column data
37 + */
38 + public Object getDataHandler(ColumnDescription columndesc);
39 +
40 + /**
41 + * Set column data of row.
42 + * @param columndesc Column description
43 + * @param obj column data
44 + */
45 + public void setDataHandler(ColumnDescription columndesc, Object obj);
46 +
47 + /**
48 + * Returns the TableSchema from row.
49 + * @return Object TableSchema
50 + */
51 + public Object getTbSchema();
52 +
53 + /**
54 + * Returns UUID which column name is _uuid.
55 + * @return UUID
56 + */
57 + public UUID getUuid();
58 +
59 + /**
60 + * Returns UUID Column which column name is _uuid.
61 + * @return UUID Column
62 + */
63 + public Column getUuidColumn();
64 +
65 + /**
66 + * Returns UUID which column name is _version.
67 + * @return UUID
68 + */
69 + public UUID getVersion();
70 +
71 + /**
72 + * Returns UUID Column which column name is _version.
73 + * @return UUID Column
74 + */
75 + public Column getVersionColumn();
76 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.ovsdb.rfc.tableservice;
17 +
18 +import static com.google.common.base.Preconditions.checkNotNull;
19 +
20 +import org.onosproject.ovsdb.rfc.table.OvsdbTable;
21 +import org.onosproject.ovsdb.rfc.table.VersionNum;
22 +import org.onosproject.ovsdb.rfc.utils.VersionUtil;
23 +
24 +/**
25 + * Table description.
26 + */
27 +public class TableDescription {
28 +
29 + // The table name
30 + private final String name;
31 + // The database name
32 + private final String database = "Open_vSwitch";
33 + // The initial version
34 + private final String fromVersion;
35 + // The end of the version
36 + private final String untilVersion;
37 +
38 + /**
39 + * Constructs a MonitorRequest object.
40 + * @param table OvsdbTable entity
41 + */
42 + public TableDescription(OvsdbTable table) {
43 + checkNotNull(table, "table cannot be null");
44 + this.name = table.tableName();
45 + this.fromVersion = VersionUtil.DEFAULT_VERSION_STRING;
46 + this.untilVersion = VersionUtil.DEFAULT_VERSION_STRING;
47 + }
48 +
49 + /**
50 + * Constructs a MonitorRequest object.
51 + * @param table OvsdbTable entity
52 + * @param fromVersion the initial version
53 + */
54 + public TableDescription(OvsdbTable table, VersionNum fromVersion) {
55 + checkNotNull(table, "table cannot be null");
56 + checkNotNull(fromVersion, "the initial version cannot be null");
57 + this.name = table.tableName();
58 + this.fromVersion = fromVersion.versionNum();
59 + this.untilVersion = VersionUtil.DEFAULT_VERSION_STRING;
60 + }
61 +
62 + /**
63 + * Constructs a MonitorRequest object.
64 + * @param table OvsdbTable entity
65 + * @param fromVersion the initial version
66 + * @param untilVersion the end of the version
67 + */
68 + public TableDescription(OvsdbTable table, VersionNum fromVersion, VersionNum untilVersion) {
69 + checkNotNull(table, "table cannot be null");
70 + checkNotNull(fromVersion, "the initial version cannot be null");
71 + checkNotNull(untilVersion, "the end of the version cannot be null");
72 + this.name = table.tableName();
73 + this.fromVersion = fromVersion.versionNum();
74 + this.untilVersion = untilVersion.versionNum();
75 + }
76 +
77 + /**
78 + * Returns the column name.
79 + * @return the column name
80 + */
81 + public String name() {
82 + return name;
83 + }
84 +
85 + /**
86 + * Returns the database name.
87 + * @return the database name
88 + */
89 + public String database() {
90 + return database;
91 + }
92 +
93 + /**
94 + * Returns the initial version.
95 + * @return the initial version
96 + */
97 + public String fromVersion() {
98 + return fromVersion;
99 + }
100 +
101 + /**
102 + * Returns the end of the version.
103 + * @return the end of the version
104 + */
105 + public String untilVersion() {
106 + return untilVersion;
107 + }
108 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.ovsdb.rfc.utils;
17 +
18 +import org.onosproject.ovsdb.rfc.notation.Condition;
19 +import org.onosproject.ovsdb.rfc.notation.Condition.Function;
20 +
21 +/**
22 + * Condition utility class.
23 + */
24 +public final class ConditionUtil {
25 +
26 + /**
27 + * Constructs a ConditionUtil object. Utility classes should not have a
28 + * public or default constructor, otherwise IDE will compile unsuccessfully. This
29 + * class should not be instantiated.
30 + */
31 + private ConditionUtil() {
32 + }
33 +
34 + /**
35 + * Returns a Condition that means Function.EQUALS .
36 + * @param columnName column name
37 + * @param data column value
38 + * @return Condition
39 + */
40 + public static Condition equals(String columnName, Object data) {
41 + Object value = TransValueUtil.getFormatData(data);
42 + return new Condition(columnName, Function.EQUALS, value);
43 + }
44 +
45 + /**
46 + * Returns a Condition that means Function.NOT_EQUALS .
47 + * @param columnName column name
48 + * @param data column value
49 + * @return Condition
50 + */
51 + public static Condition unEquals(String columnName, Object data) {
52 + Object value = TransValueUtil.getFormatData(data);
53 + return new Condition(columnName, Function.NOT_EQUALS, value);
54 + }
55 +
56 + /**
57 + * Returns a Condition that means Function.GREATER_THAN .
58 + * @param columnName column name
59 + * @param data column value
60 + * @return Condition
61 + */
62 + public static Condition greaterThan(String columnName, Object data) {
63 + Object value = TransValueUtil.getFormatData(data);
64 + return new Condition(columnName, Function.GREATER_THAN, value);
65 + }
66 +
67 + /**
68 + * Returns a Condition that means Function.GREATER_THAN_OR_EQUALS .
69 + * @param columnName column name
70 + * @param data column value
71 + * @return Condition
72 + */
73 + public static Condition greaterThanOrEquals(String columnName, Object data) {
74 + Object value = TransValueUtil.getFormatData(data);
75 + return new Condition(columnName, Function.GREATER_THAN_OR_EQUALS, value);
76 + }
77 +
78 + /**
79 + * Returns a Condition that means Function.LESS_THAN .
80 + * @param columnName column name
81 + * @param data column value
82 + * @return Condition
83 + */
84 + public static Condition lesserThan(String columnName, Object data) {
85 + Object value = TransValueUtil.getFormatData(data);
86 + return new Condition(columnName, Function.LESS_THAN, value);
87 + }
88 +
89 + /**
90 + * Returns a Condition that means Function.LESS_THAN_OR_EQUALS .
91 + * @param columnName column name
92 + * @param data column value
93 + * @return Condition
94 + */
95 + public static Condition lesserThanOrEquals(String columnName, Object data) {
96 + Object value = TransValueUtil.getFormatData(data);
97 + return new Condition(columnName, Function.LESS_THAN_OR_EQUALS, value);
98 + }
99 +
100 + /**
101 + * Returns a Condition that means Function.INCLUDES .
102 + * @param columnName column name
103 + * @param data column value
104 + * @return Condition
105 + */
106 + public static Condition includes(String columnName, Object data) {
107 + Object value = TransValueUtil.getFormatData(data);
108 + return new Condition(columnName, Function.INCLUDES, value);
109 + }
110 +
111 + /**
112 + * Returns a Condition that means Function.EXCLUDES .
113 + * @param columnName column name
114 + * @param data column value
115 + * @return Condition
116 + */
117 + public static Condition excludes(String columnName, Object data) {
118 + Object value = TransValueUtil.getFormatData(data);
119 + return new Condition(columnName, Function.EXCLUDES, value);
120 + }
121 +
122 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.ovsdb.rfc.utils;
17 +
18 +import io.netty.buffer.ByteBuf;
19 +import io.netty.buffer.ByteBufInputStream;
20 +
21 +import java.io.IOException;
22 +import java.util.List;
23 +import java.util.Stack;
24 +
25 +import org.onosproject.ovsdb.rfc.error.UnsupportedEncodingException;
26 +import org.onosproject.ovsdb.rfc.jsonrpc.JsonReadContext;
27 +
28 +import com.fasterxml.jackson.core.JsonEncoding;
29 +import com.fasterxml.jackson.core.JsonParseException;
30 +import com.fasterxml.jackson.core.JsonParser;
31 +import com.fasterxml.jackson.core.io.IOContext;
32 +import com.fasterxml.jackson.core.json.ByteSourceJsonBootstrapper;
33 +import com.fasterxml.jackson.core.util.BufferRecycler;
34 +import com.fasterxml.jackson.databind.JsonNode;
35 +import com.fasterxml.jackson.databind.MappingJsonFactory;
36 +
37 +/**
38 + * Decoder utility class.
39 + */
40 +public final class JsonRpcReaderUtil {
41 +
42 + /**
43 + * Constructs a JsonRpcReaderUtil object. Utility classes should not have a
44 + * public or default constructor, otherwise IDE will compile unsuccessfully.
45 + * This class should not be instantiated.
46 + */
47 + private JsonRpcReaderUtil() {
48 + }
49 +
50 + /**
51 + * Decode the bytes to Json object.
52 + * @param in input of bytes
53 + * @param out ouput of Json object list
54 + * @param jrContext context for the last decoding process
55 + * @throws IOException IOException
56 + * @throws JsonParseException JsonParseException
57 + */
58 + public static void readToJsonNode(ByteBuf in, List<Object> out,
59 + JsonReadContext jrContext)
60 + throws JsonParseException, IOException {
61 + int lastReadBytes = jrContext.getLastReadBytes();
62 + if (lastReadBytes == 0) {
63 + if (in.readableBytes() < 4) {
64 + return;
65 + }
66 + checkEncoding(in);
67 + }
68 +
69 + int i = lastReadBytes + in.readerIndex();
70 + Stack<Byte> bufStack = jrContext.getBufStack();
71 + for (; i < in.writerIndex(); i++) {
72 + byte b = in.getByte(i);
73 + switch (b) {
74 + case '{':
75 + if (!isDoubleQuote(bufStack)) {
76 + bufStack.push(b);
77 + jrContext.setStartMatch(true);
78 + }
79 + break;
80 + case '}':
81 + if (!isDoubleQuote(bufStack)) {
82 + bufStack.pop();
83 + }
84 + break;
85 + case '"':
86 + if (in.getByte(i - 1) != '\\') {
87 + if (!bufStack.isEmpty() && bufStack.peek() != '"') {
88 + bufStack.push(b);
89 + } else {
90 + bufStack.pop();
91 + }
92 + }
93 + break;
94 + default:
95 + break;
96 + }
97 +
98 + if (jrContext.isStartMatch() && bufStack.isEmpty()) {
99 + ByteBuf buf = in.readSlice(i - in.readerIndex() + 1);
100 + JsonParser jf = new MappingJsonFactory()
101 + .createParser(new ByteBufInputStream(buf));
102 + JsonNode jsonNode = jf.readValueAsTree();
103 + out.add(jsonNode);
104 + lastReadBytes = 0;
105 + jrContext.setLastReadBytes(lastReadBytes);
106 + break;
107 + }
108 + }
109 +
110 + if (i >= in.writerIndex()) {
111 + lastReadBytes = in.readableBytes();
112 + jrContext.setLastReadBytes(lastReadBytes);
113 + }
114 + }
115 +
116 + /**
117 + * Filter the invalid characters before decoding.
118 + * @param in input of bytes
119 + * @param lastReadBytes the bytes for last decoding incomplete record
120 + */
121 + private static void fliterCharaters(ByteBuf in) {
122 + while (in.isReadable()) {
123 + int ch = in.getByte(in.readerIndex());
124 + if ((ch != ' ') && (ch != '\n') && (ch != '\t') && (ch != '\r')) {
125 + break;
126 + } else {
127 + in.readByte();
128 + }
129 + }
130 + }
131 +
132 + /**
133 + * Check whether the peek of the stack element is double quote.
134 + * @param jrContext context for the last decoding process
135 + * @return boolean
136 + */
137 + private static boolean isDoubleQuote(Stack<Byte> bufStack) {
138 + if (!bufStack.isEmpty() && bufStack.peek() == '"') {
139 + return true;
140 + }
141 + return false;
142 + }
143 +
144 + /**
145 + * Check whether the encoding is valid.
146 + * @param in input of bytes
147 + * @throws IOException this is an IO exception
148 + * @throws UnsupportedEncodingException this is an unsupported encode
149 + * exception
150 + */
151 + private static void checkEncoding(ByteBuf in) throws IOException {
152 + int inputStart = 0;
153 + int inputLength = 4;
154 + fliterCharaters(in);
155 + byte[] buff = new byte[4];
156 + in.getBytes(in.readerIndex(), buff);
157 + ByteSourceJsonBootstrapper strapper = new ByteSourceJsonBootstrapper(
158 + new IOContext(
159 + new BufferRecycler(),
160 + null,
161 + false),
162 + buff,
163 + inputStart,
164 + inputLength);
165 + JsonEncoding jsonEncoding = strapper.detectEncoding();
166 + if (!JsonEncoding.UTF8.equals(jsonEncoding)) {
167 + throw new UnsupportedEncodingException(
168 + "Only UTF-8 encoding is supported.");
169 + }
170 + }
171 +
172 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.ovsdb.rfc.utils;
17 +
18 +import java.util.List;
19 +
20 +import org.onosproject.ovsdb.rfc.jsonrpc.JsonRpcRequest;
21 +import org.onosproject.ovsdb.rfc.operations.Operation;
22 +import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
23 +
24 +/**
25 + * RPC Methods request utility class. Refer to RFC7047's Section 4.1.
26 + */
27 +public final class JsonRpcWriterUtil {
28 +
29 + /**
30 + * Constructs a JsonRpcWriterUtil object. Utility classes should not have a
31 + * public or default constructor, otherwise IDE will compile unsuccessfully.
32 + * This class should not be instantiated.
33 + */
34 + private JsonRpcWriterUtil() {
35 + }
36 +
37 + /**
38 + * Returns string of RPC request.
39 + * @param uuid id of request object
40 + * @param methodName method of request object
41 + * @param params params of request object
42 + * @return RPC Request String
43 + */
44 + private static String getRequestStr(String uuid, String methodName,
45 + List params) {
46 + JsonRpcRequest request;
47 + if (params != null) {
48 + request = new JsonRpcRequest(uuid, methodName, params);
49 + } else {
50 + request = new JsonRpcRequest(uuid, methodName);
51 + }
52 + String str = ObjectMapperUtil.convertToString(request);
53 + return str;
54 + }
55 +
56 + /**
57 + * Returns string of get_schema request.
58 + * @param uuid id of get_schema request
59 + * @param dbnames params of get_schema request
60 + * @return get_schema Request String
61 + */
62 + public static String getSchemaStr(String uuid, List<String> dbnames) {
63 + String methodName = "get_schema";
64 + return getRequestStr(uuid, methodName, dbnames);
65 + }
66 +
67 + /**
68 + * Returns string of echo request.
69 + * @param uuid id of echo request
70 + * @return echo Request String
71 + */
72 + public static String echoStr(String uuid) {
73 + String methodName = "echo";
74 + return getRequestStr(uuid, methodName, null);
75 + }
76 +
77 + /**
78 + * Returns string of monitor request.
79 + * @param uuid id of monitor request
80 + * @param monotorId json-value in params of monitor request
81 + * @param dbSchema DatabaseSchema entity
82 + * @return monitor Request String
83 + */
84 + public static String monitorStr(String uuid, String monotorId,
85 + DatabaseSchema dbSchema) {
86 + String methodName = "monitor";
87 + return getRequestStr(uuid, methodName,
88 + ParamUtil.getMonitorParams(monotorId, dbSchema));
89 + }
90 +
91 + /**
92 + * Returns string of list_dbs request.
93 + * @param uuid id of list_dbs request
94 + * @return list_dbs Request String
95 + */
96 + public static String listDbsStr(String uuid) {
97 + String methodName = "list_dbs";
98 + return getRequestStr(uuid, methodName, null);
99 + }
100 +
101 + /**
102 + * Returns string of transact request.
103 + * @param uuid id of transact request
104 + * @param dbSchema DatabaseSchema entity
105 + * @param operations operation* in params of transact request
106 + * @return transact Request String
107 + */
108 + public static String transactStr(String uuid, DatabaseSchema dbSchema,
109 + List<Operation> operations) {
110 + String methodName = "transact";
111 + return getRequestStr(uuid, methodName,
112 + ParamUtil.getTransactParams(dbSchema, operations));
113 + }
114 +}
1 +package org.onosproject.ovsdb.rfc.utils;
2 +
3 +import org.onosproject.ovsdb.rfc.notation.Mutation;
4 +import org.onosproject.ovsdb.rfc.notation.Mutation.Mutator;
5 +
6 +public final class MutationUtil {
7 +
8 + /**
9 + * Constructs a MutationUtil object. Utility classes should not have a
10 + * public or default constructor, otherwise IDE will compile unsuccessfully. This
11 + * class should not be instantiated.
12 + */
13 + private MutationUtil() {
14 + }
15 +
16 + /**
17 + * Returns a Mutation that means += .
18 + * @param columnName column name
19 + * @param data column value
20 + * @return Mutation
21 + */
22 + public static Mutation sum(String columnName, Object data) {
23 + Object value = TransValueUtil.getFormatData(data);
24 + return new Mutation(columnName, Mutator.SUM, value);
25 + }
26 +
27 + /**
28 + * Returns a Mutation that means -= .
29 + * @param columnName column name
30 + * @param data column value
31 + * @return Mutation
32 + */
33 + public static Mutation difference(String columnName, Object data) {
34 + Object value = TransValueUtil.getFormatData(data);
35 + return new Mutation(columnName, Mutator.DIFFERENCE, value);
36 + }
37 +
38 + /**
39 + * Returns a Mutation that means *= .
40 + * @param columnName column name
41 + * @param data column value
42 + * @return Mutation
43 + */
44 + public static Mutation product(String columnName, Object data) {
45 + Object value = TransValueUtil.getFormatData(data);
46 + return new Mutation(columnName, Mutator.PRODUCT, value);
47 + }
48 +
49 + /**
50 + * Returns a Mutation that means /= .
51 + * @param columnName column name
52 + * @param data column value
53 + * @return Mutation
54 + */
55 + public static Mutation quotient(String columnName, Object data) {
56 + Object value = TransValueUtil.getFormatData(data);
57 + return new Mutation(columnName, Mutator.QUOTIENT, value);
58 + }
59 +
60 + /**
61 + * Returns a Mutation that means %= .
62 + * @param columnName column name
63 + * @param data column value
64 + * @return Mutation
65 + */
66 + public static Mutation remainder(String columnName, Object data) {
67 + Object value = TransValueUtil.getFormatData(data);
68 + return new Mutation(columnName, Mutator.REMAINDER, value);
69 + }
70 +
71 + /**
72 + * Returns a Mutation that means insert .
73 + * @param columnName column name
74 + * @param data column value
75 + * @return Mutation
76 + */
77 + public static Mutation insert(String columnName, Object data) {
78 + Object value = TransValueUtil.getFormatData(data);
79 + return new Mutation(columnName, Mutator.INSERT, value);
80 + }
81 +
82 + /**
83 + * Returns a Mutation that means delete .
84 + * @param columnName column name
85 + * @param data column value
86 + * @return Mutation
87 + */
88 + public static Mutation delete(String columnName, Object data) {
89 + Object value = TransValueUtil.getFormatData(data);
90 + return new Mutation(columnName, Mutator.DELETE, value);
91 + }
92 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.ovsdb.rfc.utils;
17 +
18 +import java.util.List;
19 +import java.util.Set;
20 +
21 +import org.onosproject.ovsdb.rfc.message.MonitorRequest;
22 +import org.onosproject.ovsdb.rfc.message.MonitorSelect;
23 +import org.onosproject.ovsdb.rfc.operations.Operation;
24 +import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
25 +import org.onosproject.ovsdb.rfc.schema.TableSchema;
26 +
27 +import com.google.common.base.Function;
28 +import com.google.common.collect.ImmutableMap;
29 +import com.google.common.collect.Lists;
30 +import com.google.common.collect.Maps;
31 +
32 +/**
33 + * Params utility class. Params of the request object, refer to RFC7047's Section
34 + * 4.1.
35 + */
36 +public final class ParamUtil {
37 +
38 + /**
39 + * Constructs a ParamUtil object. Utility classes should not have a
40 + * public or default constructor, otherwise IDE will compile unsuccessfully. This
41 + * class should not be instantiated.
42 + */
43 + private ParamUtil() {
44 + }
45 +
46 + /**
47 + * Returns MonitorRequest, refer to RFC7047's Section 4.1.5.
48 + * @param tableSchema entity
49 + * @return MonitorRequest
50 + */
51 + private static MonitorRequest getAllColumnsMonitorRequest(TableSchema tableSchema) {
52 + String tableName = tableSchema.name();
53 + Set<String> columns = tableSchema.getColumnNames();
54 + MonitorSelect select = new MonitorSelect(true, true, true, true);
55 + MonitorRequest monitorRequest = new MonitorRequest(tableName, columns,
56 + select);
57 + return monitorRequest;
58 + }
59 +
60 + /**
61 + * Returns params of monitor method, refer to RFC7047's Section 4.1.5.
62 + * @param monotorId json-value, refer to RFC7047's Section 4.1.5.
63 + * @param dbSchema DatabaseSchema entity
64 + * @return List of Object, the params of monitor request
65 + */
66 + public static List<Object> getMonitorParams(String monotorId,
67 + DatabaseSchema dbSchema) {
68 + Set<String> tables = dbSchema.getTableNames();
69 + List<MonitorRequest> monitorRequests = Lists.newArrayList();
70 + for (String tableName : tables) {
71 + TableSchema tableSchema = dbSchema.getTableSchema(tableName);
72 + monitorRequests.add(getAllColumnsMonitorRequest(tableSchema));
73 + }
74 + ImmutableMap<String, MonitorRequest> reqMap = Maps
75 + .uniqueIndex(monitorRequests,
76 + new Function<MonitorRequest, String>() {
77 + @Override
78 + public String apply(MonitorRequest input) {
79 + return input.getTableName();
80 + }
81 + });
82 + return Lists.<Object>newArrayList(dbSchema.name(), monotorId,
83 + reqMap);
84 + }
85 +
86 + /**
87 + * Returns params of transact method, refer to RFC7047's Section 4.1.3.
88 + * @param dbSchema DatabaseSchema entity
89 + * @param operations operation*, refer to RFC7047's Section 4.1.3.
90 + * @return List of Object, the params of transact request
91 + */
92 + public static List<Object> getTransactParams(DatabaseSchema dbSchema,
93 + List<Operation> operations) {
94 + List<Object> lists = Lists.newArrayList((Object) dbSchema.name());
95 + lists.addAll(operations);
96 + return lists;
97 + }
98 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.ovsdb.rfc.utils;
17 +
18 +import io.netty.handler.codec.string.StringEncoder;
19 +import io.netty.util.CharsetUtil;
20 +
21 +/**
22 + * StringEncoder utility class.Only UTF-8 encoding is supported refer to
23 + * RFC7047's Section 3.1.
24 + */
25 +public final class StringEncoderUtil {
26 +
27 + /**
28 + * Constructs a StringEncoderUtil object. Utility classes should not have a
29 + * public or default constructor, otherwise IDE will compile unsuccessfully. This
30 + * class should not be instantiated.
31 + */
32 + private StringEncoderUtil() {
33 + }
34 +
35 + /**
36 + * Returns StringEncoder of UTF_8 .
37 + * @return StringEncoder
38 + */
39 + public static StringEncoder getEncoder() {
40 + return new StringEncoder(CharsetUtil.UTF_8);
41 + }
42 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.ovsdb.rfc.utils;
17 +
18 +/**
19 + * Version utility class.
20 + */
21 +public final class VersionUtil {
22 +
23 + /**
24 + * Constructs a VersionUtil object. Utility classes should not have a public
25 + * or default constructor, otherwise IDE will compile unsuccessfully. This
26 + * class should not be instantiated.
27 + */
28 + private VersionUtil() {
29 + }
30 +
31 + public static final String DEFAULT_VERSION_STRING = "0.0.0";
32 + private static final String FORMAT = "(\\d+)\\.(\\d+)\\.(\\d+)";
33 +
34 + /**
35 + * Match version by the format.
36 + * @param version the version String
37 + * @throws IllegalArgumentException this is an illegal argument exception
38 + */
39 + public static void versionMatch(String version) {
40 + if (!version.matches(FORMAT)) {
41 + throw new IllegalArgumentException("<" + version
42 + + "> does not match format " + FORMAT);
43 + }
44 + }
45 +
46 + /**
47 + * Compare fromVersion and toVersion.
48 + * @param fromVersion the initial version
49 + * @param toVersion the end of the version
50 + * @return a long number
51 + */
52 + public static long versionCompare(String fromVersion, String toVersion) {
53 + Long fromNum = Long.parseLong(fromVersion.replace(".", ""));
54 + Long toNum = Long.parseLong(toVersion.replace(".", ""));
55 + return (fromNum - toNum);
56 + }
57 +}