lishuai
Committed by Gerrit Code Review

[ONOS-2408]Data model and utility class about RFC 7047 (ovsdb protocol).

The whole lib of ovsdb provides the function of encode to jasonRPC and
decode to Pojo to consumer, the bussiness of coordination of
communication is implemented by adapter.

Change-Id: I4c35426273394c1699207e5a4f2e98cead59f1e1
Showing 58 changed files with 4750 additions and 43 deletions
1 -<?xml version="1.0" encoding="UTF-8"?> 1 +<?xml version="1.0" encoding="UTF-8"?>
2 -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 - <modelVersion>4.0.0</modelVersion> 4 + <modelVersion>4.0.0</modelVersion>
5 - <parent> 5 + <parent>
6 - <groupId>org.onosproject</groupId> 6 + <groupId>org.onosproject</groupId>
7 - <artifactId>onos</artifactId> 7 + <artifactId>onos</artifactId>
8 - <version>1.3.0-SNAPSHOT</version> 8 + <version>1.3.0-SNAPSHOT</version>
9 - </parent> 9 + </parent>
10 - <artifactId>onos-ovsdb</artifactId> 10 + <artifactId>onos-ovsdb</artifactId>
11 - <name>onos-ovsdb</name> 11 + <name>onos-ovsdb</name>
12 - <packaging>pom</packaging> 12 + <packaging>pom</packaging>
13 - 13 +
14 - <description>ONOS OVSDB southbound plugin</description> 14 + <description>ONOS OVSDB southbound plugin</description>
15 - <dependencies> 15 + <dependencies>
16 - <dependency> 16 + <dependency>
17 - <groupId>junit</groupId> 17 + <groupId>junit</groupId>
18 - <artifactId>junit</artifactId> 18 + <artifactId>junit</artifactId>
19 - <scope>test</scope> 19 + <scope>test</scope>
20 - </dependency> 20 + </dependency>
21 - <dependency> 21 + <dependency>
22 - <groupId>org.onosproject</groupId> 22 + <groupId>org.onosproject</groupId>
23 - <artifactId>onlab-misc</artifactId> 23 + <artifactId>onlab-misc</artifactId>
24 - </dependency> 24 + </dependency>
25 - <dependency> 25 + <dependency>
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 - </dependencies> 29 + </dependencies>
30 - 30 +
31 - <build> 31 + <build>
32 - <plugins> 32 + <plugins>
33 - <plugin> 33 + <plugin>
34 - <groupId>org.apache.felix</groupId> 34 + <groupId>org.apache.felix</groupId>
35 - <artifactId>maven-bundle-plugin</artifactId> 35 + <artifactId>maven-bundle-plugin</artifactId>
36 - </plugin> 36 + </plugin>
37 - </plugins> 37 + </plugins>
38 - </build> 38 + </build>
39 - 39 +
40 - <modules> 40 + <modules>
41 - <module>api</module> 41 + <module>api</module>
42 - </modules> 42 + <module>rfc</module>
43 -</project> 43 + </modules>
44 +</project>
......
1 +<?xml version="1.0" encoding="UTF-8"?>
2 +<!-- ~ Copyright 2014 Open Networking Laboratory ~ ~ Licensed under the Apache
3 + License, Version 2.0 (the "License"); ~ you may not use this file except
4 + in compliance with the License. ~ You may obtain a copy of the License at
5 + ~ ~ http://www.apache.org/licenses/LICENSE-2.0 ~ ~ Unless required by applicable
6 + law or agreed to in writing, software ~ distributed under the License is
7 + distributed on an "AS IS" BASIS, ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY
8 + KIND, either express or implied. ~ See the License for the specific language
9 + governing permissions and ~ limitations under the License. -->
10 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
11 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
12 + <modelVersion>4.0.0</modelVersion>
13 + <parent>
14 + <groupId>org.onosproject</groupId>
15 + <artifactId>onos-ovsdb</artifactId>
16 + <version>1.3.0-SNAPSHOT</version>
17 + <relativePath>../pom.xml</relativePath>
18 + </parent>
19 + <artifactId>onos-ovsdb-rfc</artifactId>
20 + <packaging>bundle</packaging>
21 +
22 + <dependencies>
23 + <dependency>
24 + <groupId>org.apache.felix</groupId>
25 + <artifactId>org.apache.felix.scr.annotations</artifactId>
26 + </dependency>
27 + <dependency>
28 + <groupId>org.osgi</groupId>
29 + <artifactId>org.osgi.compendium</artifactId>
30 + </dependency>
31 + </dependencies>
32 +
33 + <build>
34 + <plugins>
35 + <plugin>
36 + <groupId>org.apache.felix</groupId>
37 + <artifactId>maven-scr-plugin</artifactId>
38 + </plugin>
39 + </plugins>
40 + </build>
41 +
42 +</project>
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.error;
17 +
18 +/**
19 + * AbnormalSchema exception is thrown when the received schema is invalid.
20 + */
21 +public class AbnormalSchemaException extends RuntimeException {
22 + private static final long serialVersionUID = 8328377718334680368L;
23 +
24 + /**
25 + * Constructs a AbnormalSchemaException object.
26 + * @param message error message
27 + */
28 + public AbnormalSchemaException(String message) {
29 + super(message);
30 + }
31 +
32 + /**
33 + * Constructs a AbnormalSchemaException object.
34 + * @param message error message
35 + * @param cause Throwable
36 + */
37 + public AbnormalSchemaException(String message, Throwable cause) {
38 + super(message, cause);
39 + }
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.error;
17 +
18 +/**
19 + * This exception is thrown when the argument is not supported.
20 + */
21 +public class ArgumentException extends RuntimeException {
22 + private static final long serialVersionUID = 4950089877540156797L;
23 +
24 + /**
25 + * Constructs a ArgumentException object.
26 + * @param message error message
27 + */
28 + public ArgumentException(String message) {
29 + super(message);
30 + }
31 +
32 + /**
33 + * Constructs a ArgumentException object.
34 + * @param message error message
35 + * @param cause Throwable
36 + */
37 + public ArgumentException(String message, Throwable cause) {
38 + super(message, cause);
39 + }
40 +}
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.error;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +
20 +/**
21 + * This exception is thrown when a ColumnSchema cannot be found.
22 + */
23 +public class ColumnSchemaNotFoundException extends RuntimeException {
24 + private static final long serialVersionUID = -4325190659387339524L;
25 +
26 + /**
27 + * Constructs a ColumnSchemaNotFoundException object.
28 + * @param message error message
29 + */
30 + public ColumnSchemaNotFoundException(String message) {
31 + super(message);
32 + }
33 +
34 + /**
35 + * Constructs a ColumnSchemaNotFoundException object.
36 + * @param message error message
37 + * @param cause Throwable
38 + */
39 + public ColumnSchemaNotFoundException(String message, Throwable cause) {
40 + super(message, cause);
41 + }
42 +
43 + /**
44 + * Create error message.
45 + * @param columnName column name
46 + * @param tableName table name
47 + * @return message
48 + */
49 + public static String createMessage(String columnName, String tableName) {
50 + String message = toStringHelper("ColumnSchemaNotFoundException")
51 + .addValue("Could not find ColumnSchema for " + columnName
52 + + " in " + tableName).toString();
53 + return message;
54 + }
55 +}
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.error;
17 +
18 +/**
19 + * The JsonParsingException is thrown when JSON could not be successfully
20 + * parsed.
21 + */
22 +public class JsonParsingException extends RuntimeException {
23 + private static final long serialVersionUID = 1424752181911923235L;
24 +
25 + /**
26 + * Constructs a JsonParsingException object.
27 + * @param message error message
28 + */
29 + public JsonParsingException(String message) {
30 + super(message);
31 + }
32 +
33 + /**
34 + * Constructs a JsonParsingException object.
35 + * @param message error message
36 + * @param cause Throwable
37 + */
38 + public JsonParsingException(String message, Throwable cause) {
39 + super(message, cause);
40 + }
41 +
42 + /**
43 + * Constructs a JsonParsingException object.
44 + * @param cause Throwable
45 + */
46 + public JsonParsingException(Throwable cause) {
47 + super(cause);
48 + }
49 +
50 + /**
51 + * Constructs a JsonParsingException object.
52 + * @param message error message
53 + * @param cause Throwable
54 + * @param enableSuppression enable Suppression
55 + * @param writableStackTrace writable StackTrace
56 + */
57 + public JsonParsingException(String message, Throwable cause,
58 + boolean enableSuppression,
59 + boolean writableStackTrace) {
60 + super(message, cause, enableSuppression, writableStackTrace);
61 + }
62 +}
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.error;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +
20 +/**
21 + * This exception is thrown when a TableSchema cannot be found.
22 + */
23 +public class TableSchemaNotFoundException extends RuntimeException {
24 + private static final long serialVersionUID = 8431894450061740838L;
25 +
26 + /**
27 + * Constructs a TableSchemaNotFoundException object.
28 + * @param message error message
29 + */
30 + public TableSchemaNotFoundException(String message) {
31 + super(message);
32 + }
33 +
34 + /**
35 + * Constructs a TableSchemaNotFoundException object.
36 + * @param message error message
37 + * @param cause Throwable
38 + */
39 + public TableSchemaNotFoundException(String message, Throwable cause) {
40 + super(message, cause);
41 + }
42 +
43 + /**
44 + * Create error message.
45 + * @param tableName table name
46 + * @param schemaName database name
47 + * @return message
48 + */
49 + public static String createMessage(String tableName, String schemaName) {
50 + String message = toStringHelper("TableSchemaNotFoundException")
51 + .addValue("Can not find TableSchema for " + tableName + " in "
52 + + schemaName).toString();
53 + return message;
54 + }
55 +
56 +}
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.error;
17 +
18 +/**
19 + * This is a generic exception thrown by the Typed Schema utilities.
20 + */
21 +public class TypedSchemaException extends RuntimeException {
22 + private static final long serialVersionUID = -1452257990783176715L;
23 +
24 + /**
25 + * Constructs a TypedSchemaException object.
26 + * @param message error message
27 + */
28 + public TypedSchemaException(String message) {
29 + super(message);
30 + }
31 +
32 + /**
33 + * Constructs a TypedSchemaException object.
34 + * @param message error message
35 + * @param cause Throwable
36 + */
37 + public TypedSchemaException(String message, Throwable cause) {
38 + super(message, cause);
39 + }
40 +}
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.error;
17 +
18 +/**
19 + * This exception is thrown when a result does not meet any of the known formats
20 + * in RFC7047.
21 + */
22 +public class UnknownResultException extends RuntimeException {
23 + private static final long serialVersionUID = 1377011546616825375L;
24 +
25 + /**
26 + * Constructs a UnknownResultException object.
27 + * @param message error message
28 + */
29 + public UnknownResultException(String message) {
30 + super(message);
31 + }
32 +
33 + /**
34 + * Constructs a UnknownResultException object.
35 + * @param message error message
36 + * @param cause Throwable
37 + */
38 + public UnknownResultException(String message, Throwable cause) {
39 + super(message, cause);
40 + }
41 +}
1 +package org.onosproject.ovsdb.rfc.error;
2 +
3 +/**
4 + * This exception is thrown when the encoding does not meet UTF-8 in RFC7047.
5 + */
6 +public class UnsupportedEncodingException extends RuntimeException {
7 + private static final long serialVersionUID = -4865311369828520666L;
8 +
9 + /**
10 + * Constructs a UnsupportedEncodingException object.
11 + * @param message error message
12 + */
13 + public UnsupportedEncodingException(String message) {
14 + super(message);
15 + }
16 +}
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.error;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +
20 +/**
21 + * This exception is used when the a table or row is accessed though a typed
22 + * interface and the version requirements are not met.
23 + */
24 +public class VersionMismatchException extends RuntimeException {
25 + private static final long serialVersionUID = -8439624321110133595L;
26 +
27 + /**
28 + * Constructs a VersionMismatchException object.
29 + * @param message error message
30 + */
31 + public VersionMismatchException(String message) {
32 + super(message);
33 + }
34 +
35 + /**
36 + * Constructs a VersionMismatchException object.
37 + * @param message error message
38 + * @param cause Throwable
39 + */
40 + public VersionMismatchException(String message, Throwable cause) {
41 + super(message, cause);
42 + }
43 +
44 + /**
45 + * Create error message.
46 + * @param actualVersion the actual version
47 + * @param fromVersion the initial version
48 + * @return message
49 + */
50 + public static String createFromMessage(String actualVersion,
51 + String fromVersion) {
52 + String message = toStringHelper("VersionMismatchException")
53 + .addValue("The fromVersion should less than the actualVersion.\n"
54 + + "fromVersion: "
55 + + fromVersion
56 + + ".\n"
57 + + "actualVersion: " + actualVersion)
58 + .toString();
59 + return message;
60 + }
61 +
62 + /**
63 + * Create error message.
64 + * @param actualVersion the actual version
65 + * @param toVersion the end version
66 + * @return message
67 + */
68 + public static String createToMessage(String actualVersion, String toVersion) {
69 + String message = toStringHelper("VersionMismatchException")
70 + .addValue("The toVersion should greater than the required version.\n"
71 + + "toVersion: "
72 + + toVersion
73 + + ".\n"
74 + + "Actual Version: " + actualVersion)
75 + .toString();
76 + return message;
77 + }
78 +}
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.message;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +import static com.google.common.base.Preconditions.checkNotNull;
20 +
21 +import java.util.Objects;
22 +import java.util.Set;
23 +
24 +import com.fasterxml.jackson.annotation.JsonIgnore;
25 +import com.fasterxml.jackson.annotation.JsonInclude;
26 +
27 +/**
28 + * Monitor Requst information that need to monitor table.
29 + */
30 +@JsonInclude(JsonInclude.Include.NON_NULL)
31 +public final class MonitorRequest {
32 + @JsonIgnore
33 + private final String tableName;
34 + private final Set<String> columns;
35 + private final MonitorSelect select;
36 +
37 + /**
38 + * Constructs a MonitorRequest object.
39 + * @param tableName table name
40 + * @param columns a set of column name
41 + * @param select monitor action
42 + */
43 + public MonitorRequest(String tableName, Set<String> columns,
44 + MonitorSelect select) {
45 + checkNotNull(tableName, "tableName is not null");
46 + checkNotNull(columns, "columns is not null");
47 + checkNotNull(select, "select is not null");
48 + this.tableName = tableName;
49 + this.columns = columns;
50 + this.select = select;
51 + }
52 +
53 + /**
54 + * Returns tableName.
55 + * @return tableName
56 + */
57 + public String getTableName() {
58 + return tableName;
59 + }
60 +
61 + /**
62 + * Returns select.
63 + * @return select
64 + */
65 + public MonitorSelect getSelect() {
66 + return select;
67 + }
68 +
69 + /**
70 + * Returns columns.
71 + * @return columns
72 + */
73 + public Set<String> getColumns() {
74 + return columns;
75 + }
76 +
77 + @Override
78 + public int hashCode() {
79 + return Objects.hash(tableName, select, columns);
80 + }
81 +
82 + @Override
83 + public boolean equals(Object obj) {
84 + if (this == obj) {
85 + return true;
86 + }
87 + if (obj instanceof MonitorRequest) {
88 + final MonitorRequest other = (MonitorRequest) obj;
89 + return Objects.equals(this.tableName, other.tableName)
90 + && Objects.equals(this.select, other.select)
91 + && Objects.equals(this.columns, other.columns);
92 + }
93 + return false;
94 + }
95 +
96 + @Override
97 + public String toString() {
98 + return toStringHelper(this).add("tableName", tableName)
99 + .add("select", select).add("columns", columns).toString();
100 + }
101 +}
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.message;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +
20 +import java.util.Objects;
21 +
22 +/**
23 + * The contents of this object specify how the columns or table are to be
24 + * monitored.
25 + */
26 +public final class MonitorSelect {
27 +
28 + private final boolean initial;
29 + private final boolean insert;
30 + private final boolean delete;
31 + private final boolean modify;
32 +
33 + /**
34 + * Constructs a MonitorSelect object.
35 + * @param initial whether monitor the initial action
36 + * @param insert whether monitor the insert action
37 + * @param delete whether monitor the delete action
38 + * @param modify whether monitor the modify action
39 + */
40 + public MonitorSelect(boolean initial, boolean insert, boolean delete,
41 + boolean modify) {
42 + this.initial = initial;
43 + this.insert = insert;
44 + this.delete = delete;
45 + this.modify = modify;
46 + }
47 +
48 + /**
49 + * Returns initial.
50 + * @return initial
51 + */
52 + public boolean isInitial() {
53 + return initial;
54 + }
55 +
56 + /**
57 + * Returns insert.
58 + * @return insert
59 + */
60 + public boolean isInsert() {
61 + return insert;
62 + }
63 +
64 + /**
65 + * Returns delete.
66 + * @return delete
67 + */
68 + public boolean isDelete() {
69 + return delete;
70 + }
71 +
72 + /**
73 + * Returns modify.
74 + * @return modify
75 + */
76 + public boolean isModify() {
77 + return modify;
78 + }
79 +
80 + @Override
81 + public int hashCode() {
82 + return Objects.hash(initial, insert, delete, modify);
83 + }
84 +
85 + @Override
86 + public boolean equals(Object obj) {
87 + if (this == obj) {
88 + return true;
89 + }
90 + if (obj instanceof MonitorSelect) {
91 + final MonitorSelect other = (MonitorSelect) obj;
92 + return Objects.equals(this.initial, other.initial)
93 + && Objects.equals(this.insert, other.insert)
94 + && Objects.equals(this.delete, other.delete)
95 + && Objects.equals(this.modify, other.modify);
96 + }
97 + return false;
98 + }
99 +
100 + @Override
101 + public String toString() {
102 + return toStringHelper(this).add("initial", initial)
103 + .add("insert", insert).add("delete", delete)
104 + .add("modify", modify).toString();
105 + }
106 +}
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.message;
17 +
18 +import static com.google.common.base.Preconditions.checkNotNull;
19 +
20 +import java.util.List;
21 +
22 +import org.onosproject.ovsdb.rfc.notation.Row;
23 +import org.onosproject.ovsdb.rfc.notation.UUID;
24 +
25 +import com.fasterxml.jackson.annotation.JsonIgnore;
26 +import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
27 +import com.fasterxml.jackson.annotation.JsonProperty;
28 +
29 +/**
30 + * All results of ovs table operations. refer to RFC7047 5.2.
31 + */
32 +@JsonIgnoreProperties(ignoreUnknown = true)
33 +public final class OperationResult {
34 + private int count;
35 + @JsonIgnore
36 + private UUID uuid;
37 + private List<Row> rows;
38 + private String error;
39 + private String details;
40 +
41 + /**
42 + * Constructs a OperationResult object. When JsonNode is converted into
43 + * OperationResult, need this constructor and also need setter method.
44 + */
45 + public OperationResult() {
46 + }
47 +
48 + /**
49 + * Constructs a OperationResult object.
50 + * @param rows List of Row entity
51 + */
52 + public OperationResult(List<Row> rows) {
53 + checkNotNull(rows, "rows is not null");
54 + this.rows = rows;
55 + }
56 +
57 + /**
58 + * Constructs a OperationResult object.
59 + * @param count the count node of result
60 + * @param uuid UUID entity
61 + * @param rows List of Row entity
62 + * @param error error message
63 + * @param details details of error message
64 + */
65 + public OperationResult(int count, UUID uuid, List<Row> rows, String error,
66 + String details) {
67 + checkNotNull(uuid, "uuid is not null");
68 + checkNotNull(rows, "rows is not null");
69 + checkNotNull(error, "error is not null");
70 + checkNotNull(details, "details is not null");
71 + this.count = count;
72 + this.uuid = uuid;
73 + this.rows = rows;
74 + this.error = error;
75 + this.details = details;
76 + }
77 +
78 + /**
79 + * Return count.
80 + * @return count
81 + */
82 + public int getCount() {
83 + return count;
84 + }
85 +
86 + /**
87 + * Set count value.
88 + * @param count the Operation message of count
89 + */
90 + public void setCount(int count) {
91 + this.count = count;
92 + }
93 +
94 + /**
95 + * Return uuid.
96 + * @return uuid
97 + */
98 + @JsonProperty("uuid")
99 + public UUID getUuid() {
100 + return uuid;
101 + }
102 +
103 + /**
104 + * Set uuid value.
105 + * @param uuid the Operation message of uuid
106 + */
107 + public void setUuid(String uuid) {
108 + checkNotNull(uuid, "uuid is not null");
109 + this.uuid = UUID.uuid(uuid);
110 + }
111 +
112 + /**
113 + * Return rows.
114 + * @return List of Row
115 + */
116 + public List<Row> getRows() {
117 + return rows;
118 + }
119 +
120 + /**
121 + * Set rows value.
122 + * @param rows the Operation message of rows
123 + */
124 + public void setRows(List<Row> rows) {
125 + checkNotNull(rows, "rows is not null");
126 + this.rows = rows;
127 + }
128 +
129 + /**
130 + * Return error.
131 + * @return error
132 + */
133 + public String getError() {
134 + return error;
135 + }
136 +
137 + /**
138 + * Set error value.
139 + * @param error the Operation message of error
140 + */
141 + public void setError(String error) {
142 + checkNotNull(error, "error is not null");
143 + this.error = error;
144 + }
145 +
146 + /**
147 + * Return details.
148 + * @return details
149 + */
150 + public String getDetails() {
151 + return details;
152 + }
153 +
154 + /**
155 + * Set details value.
156 + * @param details the Operation message of details
157 + */
158 + public void setDetails(String details) {
159 + checkNotNull(details, "details is not null");
160 + this.details = details;
161 + }
162 +}
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.message;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +import static com.google.common.base.Preconditions.checkNotNull;
20 +
21 +import java.util.Objects;
22 +
23 +import org.onosproject.ovsdb.rfc.notation.Row;
24 +import org.onosproject.ovsdb.rfc.notation.UUID;
25 +
26 +/**
27 + * A TableUpdate is an object that maps from the row's UUID to a RowUpdate object.
28 + * A RowUpdate is an object with the following members: "old": row, "new": row.
29 + * Refer to RFC 7047 Section 4.1.6.
30 + */
31 +public final class RowUpdate {
32 + private final UUID uuid;
33 + private final Row oldRow;
34 + private final Row newRow;
35 +
36 + /**
37 + * Constructs a RowUpdate object.
38 + * @param uuid UUID
39 + * @param oldRow present for "delete" and "modify" updates
40 + * @param newRow present for "initial", "insert", and "modify" updates
41 + */
42 + public RowUpdate(UUID uuid, Row oldRow, Row newRow) {
43 + checkNotNull(uuid, "uuid is not null");
44 + this.uuid = uuid;
45 + this.oldRow = oldRow;
46 + this.newRow = newRow;
47 + }
48 +
49 + /**
50 + * Return uuid.
51 + * @return uuid
52 + */
53 + public UUID uuid() {
54 + return this.uuid;
55 + }
56 +
57 + /**
58 + * Return oldRow.
59 + * @return oldRow
60 + */
61 + public Row oldRow() {
62 + return oldRow;
63 + }
64 +
65 + /**
66 + * Return newRow.
67 + * @return newRow
68 + */
69 + public Row newRow() {
70 + return newRow;
71 + }
72 +
73 + @Override
74 + public int hashCode() {
75 + return Objects.hash(uuid, oldRow, newRow);
76 + }
77 +
78 + @Override
79 + public boolean equals(Object obj) {
80 + if (this == obj) {
81 + return true;
82 + }
83 + if (obj instanceof RowUpdate) {
84 + final RowUpdate other = (RowUpdate) obj;
85 + return Objects.equals(this.uuid, other.uuid)
86 + && Objects.equals(this.oldRow, other.oldRow)
87 + && Objects.equals(this.newRow, other.newRow);
88 + }
89 + return false;
90 + }
91 +
92 + @Override
93 + public String toString() {
94 + return toStringHelper(this).add("uuid", uuid).add("oldRow", oldRow)
95 + .add("newRow", newRow).toString();
96 + }
97 +}
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.message;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +
20 +import java.util.Map;
21 +import java.util.Objects;
22 +
23 +import org.onosproject.ovsdb.rfc.notation.Row;
24 +import org.onosproject.ovsdb.rfc.notation.UUID;
25 +
26 +/**
27 + * TableUpdate is an object that maps from the row's UUID to a RowUpdate object.
28 + */
29 +public final class TableUpdate {
30 +
31 + private final Map<UUID, RowUpdate> rows;
32 +
33 + /**
34 + * Constructs a TableUpdate object.
35 + * @param rows the parameter of TableUpdate entity
36 + */
37 + private TableUpdate(Map<UUID, RowUpdate> rows) {
38 + this.rows = rows;
39 + }
40 +
41 + /**
42 + * Get TableUpdate entity.
43 + * @param rows the parameter of TableUpdate entity
44 + * @return TableUpdate entity
45 + */
46 + public static TableUpdate tableUpdate(Map<UUID, RowUpdate> rows) {
47 + return new TableUpdate(rows);
48 + }
49 +
50 + /**
51 + * Return old row.
52 + * @param uuid the key of rows
53 + * @return Row old row
54 + */
55 + public Row getOld(UUID uuid) {
56 + RowUpdate rowUpdate = rows.get(uuid);
57 + if (rowUpdate == null) {
58 + return null;
59 + }
60 + return rowUpdate.oldRow();
61 + }
62 +
63 + /**
64 + * Return new row.
65 + * @param uuid the key of rows
66 + * @return Row new row
67 + */
68 + public Row getNew(UUID uuid) {
69 + RowUpdate rowUpdate = rows.get(uuid);
70 + if (rowUpdate == null) {
71 + return null;
72 + }
73 + return rowUpdate.newRow();
74 + }
75 +
76 + /**
77 + * Return rows.
78 + * @return rows
79 + */
80 + public Map<UUID, RowUpdate> rows() {
81 + return rows;
82 + }
83 +
84 + @Override
85 + public int hashCode() {
86 + return Objects.hash(rows);
87 + }
88 +
89 + @Override
90 + public boolean equals(Object obj) {
91 + if (this == obj) {
92 + return true;
93 + }
94 + if (obj instanceof TableUpdate) {
95 + final TableUpdate other = (TableUpdate) obj;
96 + return Objects.equals(this.rows, other.rows);
97 + }
98 + return false;
99 + }
100 +
101 + @Override
102 + public String toString() {
103 + return toStringHelper(this).add("rows", rows).toString();
104 + }
105 +}
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.message;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +import static com.google.common.base.Preconditions.checkNotNull;
20 +
21 +import java.util.Map;
22 +import java.util.Objects;
23 +
24 +import org.onosproject.ovsdb.rfc.schema.TableSchema;
25 +
26 +/**
27 + * TableUpdates is an object that maps from a table name to a TableUpdate.
28 + */
29 +public final class TableUpdates {
30 +
31 + private final Map<String, TableUpdate> result;
32 +
33 + /**
34 + * Constructs a TableUpdates object.
35 + * @param result the parameter of TableUpdates entity
36 + */
37 + private TableUpdates(Map<String, TableUpdate> result) {
38 + this.result = result;
39 + }
40 +
41 + /**
42 + * Get TableUpdates.
43 + * @param result the parameter of TableUpdates entity
44 + * @return TableUpdates
45 + */
46 + public static TableUpdates tableUpdates(Map<String, TableUpdate> result) {
47 + checkNotNull(result, "result is not null");
48 + return new TableUpdates(result);
49 + }
50 +
51 + /**
52 + * Return TableUpdate.
53 + * @param table the TableSchema of TableUpdates
54 + * @return TableUpdate
55 + */
56 + public TableUpdate tableUpdate(TableSchema table) {
57 + return this.result.get(table.name());
58 + }
59 +
60 + /**
61 + * Return the map of TableUpdate.
62 + * @return result
63 + */
64 + public Map<String, TableUpdate> result() {
65 + return result;
66 + }
67 +
68 + @Override
69 + public int hashCode() {
70 + return Objects.hash(result);
71 + }
72 +
73 + @Override
74 + public boolean equals(Object obj) {
75 + if (this == obj) {
76 + return true;
77 + }
78 + if (obj instanceof TableUpdates) {
79 + final TableUpdates other = (TableUpdates) obj;
80 + return Objects.equals(this.result, other.result);
81 + }
82 + return false;
83 + }
84 +
85 + @Override
86 + public String toString() {
87 + return toStringHelper(this).add("result", result).toString();
88 + }
89 +}
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.message;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +import static com.google.common.base.Preconditions.checkNotNull;
20 +
21 +import java.util.Objects;
22 +
23 +import org.onosproject.ovsdb.rfc.notation.json.UpdateNotificationConverter;
24 +
25 +import com.fasterxml.jackson.databind.JsonNode;
26 +import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
27 +
28 +/**
29 + * The "update" notification is sent by the server to the client to report
30 + * changes in tables that are being monitored following a "monitor" request. The
31 + * "params" of the result JsonNode.
32 + */
33 +@JsonDeserialize(converter = UpdateNotificationConverter.class)
34 +public final class UpdateNotification {
35 + private final Object context;
36 + private final JsonNode tbUpdatesJsonNode;
37 +
38 + /**
39 + * Constructs a UpdateNotification object.
40 + * @param context the "json-value" in "params" of the result JsonNode
41 + * @param tbUpdatesJsonNode the "table-updates" in "params" of the result JsonNode
42 + */
43 + public UpdateNotification(Object context, JsonNode tbUpdatesJsonNode) {
44 + checkNotNull(context, "context is not null");
45 + checkNotNull(tbUpdatesJsonNode, "tbUpdatesJsonNode is not null");
46 + this.context = context;
47 + this.tbUpdatesJsonNode = tbUpdatesJsonNode;
48 + }
49 +
50 + /**
51 + * Return context.
52 + * @return context
53 + */
54 + public Object context() {
55 + return context;
56 + }
57 +
58 + /**
59 + * Return tbUpdatesJsonNode.
60 + * @return tbUpdatesJsonNode
61 + */
62 + public JsonNode tbUpdatesJsonNode() {
63 + return tbUpdatesJsonNode;
64 + }
65 +
66 + @Override
67 + public int hashCode() {
68 + return Objects.hash(context, tbUpdatesJsonNode);
69 + }
70 +
71 + @Override
72 + public boolean equals(Object obj) {
73 + if (this == obj) {
74 + return true;
75 + }
76 + if (obj instanceof UpdateNotification) {
77 + final UpdateNotification other = (UpdateNotification) obj;
78 + return Objects.equals(this.context, other.context)
79 + && Objects.equals(this.tbUpdatesJsonNode,
80 + other.tbUpdatesJsonNode);
81 + }
82 + return false;
83 + }
84 +
85 + @Override
86 + public String toString() {
87 + return toStringHelper(this).add("context", context)
88 + .add("tbUpdatesJsonNode", tbUpdatesJsonNode).toString();
89 + }
90 +}
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.notation;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +import static com.google.common.base.Preconditions.checkNotNull;
20 +
21 +import java.util.Objects;
22 +
23 +import org.onosproject.ovsdb.rfc.schema.ColumnSchema;
24 +
25 +import com.fasterxml.jackson.annotation.JsonIgnore;
26 +
27 +/**
28 + * Column is the basic element of the OpenVswitch database.
29 + */
30 +public final class Column {
31 + @JsonIgnore
32 + private final ColumnSchema schema;
33 + private final Object data;
34 +
35 + /**
36 + * Column constructor.
37 + * @param schema the column schema
38 + * @param obj the data of the column
39 + */
40 + public Column(ColumnSchema schema, Object obj) {
41 + checkNotNull(schema, "schema is not null");
42 + checkNotNull(obj, "data is not null");
43 + this.schema = schema;
44 + this.data = obj;
45 + }
46 +
47 + /**
48 + * Returns column data.
49 + * @return column data
50 + */
51 + public Object data() {
52 + return data;
53 + }
54 +
55 + /**
56 + * Returns ColumnSchema.
57 + * @return ColumnSchema
58 + */
59 + public ColumnSchema schema() {
60 + return schema;
61 + }
62 +
63 + @Override
64 + public int hashCode() {
65 + return Objects.hash(schema, data);
66 + }
67 +
68 + @Override
69 + public boolean equals(Object obj) {
70 + if (this == obj) {
71 + return true;
72 + }
73 + if (obj instanceof Column) {
74 + final Column other = (Column) obj;
75 + return Objects.equals(this.schema, other.schema)
76 + && Objects.equals(this.data, other.data);
77 + }
78 + return false;
79 + }
80 +
81 + @Override
82 + public String toString() {
83 + return toStringHelper(this).add("schema", schema).add("data", data)
84 + .toString();
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.notation;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +import static com.google.common.base.Preconditions.checkNotNull;
20 +
21 +import java.util.Objects;
22 +
23 +import org.onosproject.ovsdb.rfc.notation.json.ConditionSerializer;
24 +
25 +import com.fasterxml.jackson.databind.annotation.JsonSerialize;
26 +
27 +/**
28 + * Condition is a 3-element JSON array of the form [column, function, value]
29 + * that represents a test on a column value.
30 + */
31 +@JsonSerialize(using = ConditionSerializer.class)
32 +public final class Condition {
33 + /**
34 + * Function of Notation. Refer to RFC 7047 Section 5.1.
35 + */
36 + public enum Function {
37 + LESS_THAN("<"), LESS_THAN_OR_EQUALS("<="), EQUALS("=="),
38 + NOT_EQUALS("!="), GREATER_THAN(">"), GREATER_THAN_OR_EQUALS(">="),
39 + INCLUDES("includes"), EXCLUDES("excludes");
40 +
41 + private final String function;
42 +
43 + private Function(String function) {
44 + this.function = function;
45 + }
46 +
47 + /**
48 + * Returns the function for Function.
49 + * @return the function
50 + */
51 + public String function() {
52 + return function;
53 + }
54 + }
55 +
56 + private final String column;
57 + private final Function function;
58 + private final Object value;
59 +
60 + /**
61 + * Constructs a Condition object.
62 + * @param column the column name
63 + * @param function Function
64 + * @param value column data
65 + */
66 + public Condition(String column, Function function, Object value) {
67 + checkNotNull(column, "column is not null");
68 + checkNotNull(function, "function is not null");
69 + checkNotNull(value, "value is not null");
70 + this.column = column;
71 + this.function = function;
72 + this.value = value;
73 + }
74 +
75 + /**
76 + * Returns column name.
77 + * @return column name
78 + */
79 + public String getColumn() {
80 + return column;
81 + }
82 +
83 + /**
84 + * Returns Function.
85 + * @return Function
86 + */
87 + public Function getFunction() {
88 + return function;
89 + }
90 +
91 + /**
92 + * Returns column data.
93 + * @return column data
94 + */
95 + public Object getValue() {
96 + return value;
97 + }
98 +
99 + @Override
100 + public int hashCode() {
101 + return Objects.hash(column, function, value);
102 + }
103 +
104 + @Override
105 + public boolean equals(Object obj) {
106 + if (this == obj) {
107 + return true;
108 + }
109 + if (obj instanceof Condition) {
110 + final Condition other = (Condition) obj;
111 + return Objects.equals(this.column, other.column)
112 + && Objects.equals(this.function, other.function)
113 + && Objects.equals(this.value, other.value);
114 + }
115 + return false;
116 + }
117 +
118 + @Override
119 + public String toString() {
120 + return toStringHelper(this).add("column", column)
121 + .add("function", function).add("value", value).toString();
122 + }
123 +}
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.notation;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +import static com.google.common.base.Preconditions.checkNotNull;
20 +
21 +import java.util.Objects;
22 +
23 +import org.onosproject.ovsdb.rfc.notation.json.MutationSerializer;
24 +
25 +import com.fasterxml.jackson.databind.annotation.JsonSerialize;
26 +
27 +/**
28 + * Mutation is s 3-element JSON array of the form [column, mutator, value] that
29 + * represents a change to a column value.
30 + */
31 +@JsonSerialize(using = MutationSerializer.class)
32 +public final class Mutation {
33 + /**
34 + * Mutator must be "+=", "-=", "*=", "/=", or (integer only) "%=". The value
35 + * of column is changed to the sum, difference, product, quotient, or
36 + * remainder, respectively, of column and value.
37 + */
38 + public enum Mutator {
39 + SUM("+="), DIFFERENCE("-="), PRODUCT("*="), QUOTIENT("/="),
40 + REMAINDER("%="), INSERT("insert"), DELETE("delete");
41 +
42 + private final String mutator;
43 +
44 + private Mutator(String mutator) {
45 + this.mutator = mutator;
46 + }
47 +
48 + /**
49 + * Returns the mutator for Mutator.
50 + * @return the mutator
51 + */
52 + public String mutator() {
53 + return mutator;
54 + }
55 + }
56 +
57 + private final String column;
58 + private final Mutator mutator;
59 + private final Object value;
60 +
61 + /**
62 + * Mutation constructor.
63 + * @param column the column name
64 + * @param mutator Mutator
65 + * @param value column data
66 + */
67 + public Mutation(String column, Mutator mutator, Object value) {
68 + checkNotNull(column, "column is not null");
69 + checkNotNull(mutator, "mutator is not null");
70 + checkNotNull(value, "value is not null");
71 + this.column = column;
72 + this.mutator = mutator;
73 + this.value = value;
74 + }
75 +
76 + /**
77 + * Returns column name.
78 + * @return column name
79 + */
80 + public String getColumn() {
81 + return column;
82 + }
83 +
84 + /**
85 + * Returns Mutator.
86 + * @return Mutator
87 + */
88 + public Mutator getMutator() {
89 + return mutator;
90 + }
91 +
92 + /**
93 + * Returns column data.
94 + * @return column data
95 + */
96 + public Object getValue() {
97 + return value;
98 + }
99 +
100 + @Override
101 + public int hashCode() {
102 + return Objects.hash(column, mutator, value);
103 + }
104 +
105 + @Override
106 + public boolean equals(Object obj) {
107 + if (this == obj) {
108 + return true;
109 + }
110 + if (obj instanceof Mutation) {
111 + final Mutation other = (Mutation) obj;
112 + return Objects.equals(this.column, other.column)
113 + && Objects.equals(this.mutator, other.mutator)
114 + && Objects.equals(this.value, other.value);
115 + }
116 + return false;
117 + }
118 +
119 + @Override
120 + public String toString() {
121 + return toStringHelper(this).add("column", column)
122 + .add("mutator", mutator).add("value", value).toString();
123 + }
124 +}
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.notation;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +import static com.google.common.base.Preconditions.checkNotNull;
20 +
21 +import java.util.Map;
22 +import java.util.Objects;
23 +
24 +import org.onosproject.ovsdb.rfc.notation.json.OvsdbMapSerializer;
25 +
26 +import com.fasterxml.jackson.databind.annotation.JsonSerialize;
27 +
28 +/**
29 + * OvsdbMap is a 2-element JSON array that represents a database map value.
30 + */
31 +@JsonSerialize(using = OvsdbMapSerializer.class)
32 +public final class OvsdbMap {
33 +
34 + private final Map map;
35 +
36 + /**
37 + * OvsdbMap constructor.
38 + * @param map java.util.Map
39 + */
40 + private OvsdbMap(Map map) {
41 + checkNotNull(map, "map is not null");
42 + this.map = map;
43 + }
44 +
45 + /**
46 + * Returns map.
47 + * @return map
48 + */
49 + public Map map() {
50 + return map;
51 + }
52 +
53 + /**
54 + * convert Map into OvsdbMap.
55 + * @param map java.util.Map
56 + * @return OvsdbMap
57 + */
58 + public static OvsdbMap ovsdbMap(Map map) {
59 + return new OvsdbMap(map);
60 + }
61 +
62 + @Override
63 + public int hashCode() {
64 + return Objects.hash(map);
65 + }
66 +
67 + @Override
68 + public boolean equals(Object obj) {
69 + if (this == obj) {
70 + return true;
71 + }
72 + if (obj instanceof OvsdbMap) {
73 + final OvsdbMap other = (OvsdbMap) obj;
74 + return Objects.equals(this.map, other.map);
75 + }
76 + return false;
77 + }
78 +
79 + @Override
80 + public String toString() {
81 + return toStringHelper(this).add("map", map).toString();
82 + }
83 +}
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.notation;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +import static com.google.common.base.Preconditions.checkNotNull;
20 +
21 +import java.util.Objects;
22 +import java.util.Set;
23 +
24 +import org.onosproject.ovsdb.rfc.notation.json.OvsdbSetSerializer;
25 +
26 +import com.fasterxml.jackson.databind.annotation.JsonSerialize;
27 +
28 +/**
29 + * OvsdbSet is either an atom, representing a set with exactly one element, or
30 + * a 2-element JSON array that represents a database set value.
31 + *
32 + */
33 +@JsonSerialize(using = OvsdbSetSerializer.class)
34 +public final class OvsdbSet {
35 +
36 + private final Set set;
37 +
38 + /**
39 + * OvsdbSet constructor.
40 + * @param set java.util.Set
41 + */
42 + private OvsdbSet(Set set) {
43 + checkNotNull(set, "set is not null");
44 + this.set = set;
45 + }
46 +
47 + /**
48 + * Returns set.
49 + * @return set
50 + */
51 + public Set set() {
52 + return set;
53 + }
54 +
55 + /**
56 + * convert Set into OvsdbSet.
57 + * @param set java.util.Set
58 + * @return OvsdbSet
59 + */
60 + public static OvsdbSet ovsdbSet(Set set) {
61 + return new OvsdbSet(set);
62 + }
63 +
64 + @Override
65 + public int hashCode() {
66 + return Objects.hash(set);
67 + }
68 +
69 + @Override
70 + public boolean equals(Object obj) {
71 + if (this == obj) {
72 + return true;
73 + }
74 + if (obj instanceof OvsdbSet) {
75 + final OvsdbSet other = (OvsdbSet) obj;
76 + return Objects.equals(this.set, other.set);
77 + }
78 + return false;
79 + }
80 +
81 + @Override
82 + public String toString() {
83 + return toStringHelper(this).add("set", set).toString();
84 + }
85 +}
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.notation;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +import static com.google.common.base.Preconditions.checkNotNull;
20 +
21 +import java.util.Objects;
22 +
23 +import com.fasterxml.jackson.databind.JsonNode;
24 +
25 +/**
26 + * The RefTable type that can be expanded to Row. Refer to RFC 7047 Section 3.2.
27 + */
28 +public final class RefTableRow {
29 +
30 + private final String refTable;
31 + private final JsonNode jsonNode;
32 +
33 + /**
34 + * RefTableRow constructor.
35 + * @param refTable the refTable value of JsonNode
36 + * @param jsonNode JsonNode
37 + */
38 + public RefTableRow(String refTable, JsonNode jsonNode) {
39 + checkNotNull(refTable, "refTable is not null");
40 + checkNotNull(jsonNode, "jsonNode is not null");
41 + this.refTable = refTable;
42 + this.jsonNode = jsonNode;
43 + }
44 +
45 + /**
46 + * Returns JsonNode.
47 + * @return JsonNode
48 + */
49 + public JsonNode jsonNode() {
50 + return jsonNode;
51 + }
52 +
53 + /**
54 + * Returns refTable.
55 + * @return refTable
56 + */
57 + public String refTable() {
58 + return refTable;
59 + }
60 +
61 + @Override
62 + public int hashCode() {
63 + return Objects.hash(refTable, jsonNode);
64 + }
65 +
66 + @Override
67 + public boolean equals(Object obj) {
68 + if (this == obj) {
69 + return true;
70 + }
71 + if (obj instanceof RefTableRow) {
72 + final RefTableRow other = (RefTableRow) obj;
73 + return Objects.equals(this.refTable, other.refTable)
74 + && Objects.equals(this.jsonNode, other.jsonNode);
75 + }
76 + return false;
77 + }
78 +
79 + @Override
80 + public String toString() {
81 + return toStringHelper(this).add("refTable", refTable)
82 + .add("jsonNode", jsonNode).toString();
83 + }
84 +}
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.notation;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +import static com.google.common.base.Preconditions.checkNotNull;
20 +
21 +import java.util.Collection;
22 +import java.util.List;
23 +import java.util.Map;
24 +import java.util.Objects;
25 +
26 +import org.onosproject.ovsdb.rfc.schema.ColumnSchema;
27 +import org.onosproject.ovsdb.rfc.schema.TableSchema;
28 +
29 +import com.fasterxml.jackson.annotation.JsonIgnore;
30 +import com.google.common.collect.Maps;
31 +
32 +/**
33 + * Row is the basic element of the OpenVswitch's table.
34 + */
35 +public final class Row {
36 + @JsonIgnore
37 + private TableSchema tableSchema;
38 + private Map<String, Column> columns;
39 +
40 + /**
41 + * Row constructor.
42 + */
43 + public Row() {
44 + this.columns = Maps.newHashMap();
45 + }
46 +
47 + /**
48 + * Row constructor.
49 + * @param tableSchema TableSchema entity
50 + */
51 + public Row(TableSchema tableSchema) {
52 + checkNotNull(tableSchema, "tableSchema is not null");
53 + this.tableSchema = tableSchema;
54 + this.columns = Maps.newHashMap();
55 + }
56 +
57 + /**
58 + * Row constructor.
59 + * @param tableSchema TableSchema entity
60 + * @param columns List of Column entity
61 + */
62 + public Row(TableSchema tableSchema, List<Column> columns) {
63 + checkNotNull(tableSchema, "tableSchema is not null");
64 + checkNotNull(columns, "columns is not null");
65 + this.tableSchema = tableSchema;
66 + this.columns = Maps.newHashMap();
67 + for (Column column : columns) {
68 + this.columns.put(column.schema().name(), column);
69 + }
70 + }
71 +
72 + /**
73 + * Returns tableSchema.
74 + * @return tableSchema
75 + */
76 + public TableSchema getTableSchema() {
77 + return tableSchema;
78 + }
79 +
80 + /**
81 + * Set tableSchema value.
82 + * @param tableSchema TableSchema entity
83 + */
84 + public void setTableSchema(TableSchema tableSchema) {
85 + this.tableSchema = tableSchema;
86 + }
87 +
88 + /**
89 + * Returns Column by ColumnSchema.
90 + * @param schema ColumnSchema entity
91 + * @return Column
92 + */
93 + public Column getColumn(ColumnSchema schema) {
94 + return (Column) columns.get(schema.name());
95 + }
96 +
97 + /**
98 + * Returns Collection of Column.
99 + * @return Collection of Column
100 + */
101 + public Collection<Column> getColumns() {
102 + return columns.values();
103 + }
104 +
105 + /**
106 + * add Column.
107 + * @param columnName column name
108 + * @param data Column entity
109 + */
110 + public void addColumn(String columnName, Column data) {
111 + this.columns.put(columnName, data);
112 + }
113 +
114 + @Override
115 + public int hashCode() {
116 + return Objects.hash(tableSchema, columns);
117 + }
118 +
119 + @Override
120 + public boolean equals(Object obj) {
121 + if (this == obj) {
122 + return true;
123 + }
124 + if (obj instanceof Row) {
125 + final Row other = (Row) obj;
126 + return Objects.equals(this.tableSchema, other.tableSchema)
127 + && Objects.equals(this.columns, other.columns);
128 + }
129 + return false;
130 + }
131 +
132 + @Override
133 + public String toString() {
134 + return toStringHelper(this).add("tableSchema", tableSchema).add("columns", columns).toString();
135 + }
136 +}
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.notation;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +import static com.google.common.base.Preconditions.checkNotNull;
20 +
21 +import java.util.Objects;
22 +
23 +import org.onosproject.ovsdb.rfc.notation.json.UUIDSerializer;
24 +
25 +import com.fasterxml.jackson.databind.annotation.JsonSerialize;
26 +
27 +/**
28 + * Handles both uuid and named-uuid.
29 + */
30 +@JsonSerialize(using = UUIDSerializer.class)
31 +public final class UUID {
32 + private final String value;
33 +
34 + /**
35 + * UUID constructor.
36 + * @param value UUID value
37 + */
38 + private UUID(String value) {
39 + checkNotNull(value, "value is not null");
40 + this.value = value;
41 + }
42 +
43 + /**
44 + * Get UUID.
45 + * @param value UUID value
46 + * @return UUID
47 + */
48 + public static UUID uuid(String value) {
49 + return new UUID(value);
50 + }
51 +
52 + /**
53 + * Returns value.
54 + * @return value
55 + */
56 + public String value() {
57 + return value;
58 + }
59 +
60 + @Override
61 + public int hashCode() {
62 + return Objects.hash(value);
63 + }
64 +
65 + @Override
66 + public boolean equals(Object obj) {
67 + if (this == obj) {
68 + return true;
69 + }
70 + if (obj instanceof UUID) {
71 + final UUID other = (UUID) obj;
72 + return Objects.equals(this.value, other.value);
73 + }
74 + return false;
75 + }
76 +
77 + @Override
78 + public String toString() {
79 + return toStringHelper(this).add("value", value).toString();
80 + }
81 +}
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.notation.json;
17 +
18 +import java.io.IOException;
19 +
20 +import org.onosproject.ovsdb.rfc.notation.Condition;
21 +
22 +import com.fasterxml.jackson.core.JsonGenerator;
23 +import com.fasterxml.jackson.core.JsonProcessingException;
24 +import com.fasterxml.jackson.databind.JsonSerializer;
25 +import com.fasterxml.jackson.databind.SerializerProvider;
26 +
27 +/**
28 + * Condition Serializer.
29 + */
30 +public class ConditionSerializer extends JsonSerializer<Condition> {
31 + @Override
32 + public void serialize(Condition condition, JsonGenerator generator,
33 + SerializerProvider provider)
34 + throws IOException, JsonProcessingException {
35 + generator.writeStartArray();
36 + generator.writeString(condition.getColumn());
37 + generator.writeString(condition.getFunction().function());
38 + generator.writeObject(condition.getValue());
39 + generator.writeEndArray();
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.notation.json;
17 +
18 +import java.io.IOException;
19 +
20 +import org.onosproject.ovsdb.rfc.notation.Mutation;
21 +
22 +import com.fasterxml.jackson.core.JsonGenerator;
23 +import com.fasterxml.jackson.core.JsonProcessingException;
24 +import com.fasterxml.jackson.databind.JsonSerializer;
25 +import com.fasterxml.jackson.databind.SerializerProvider;
26 +
27 +/**
28 + * Mutation Serializer.
29 + */
30 +public class MutationSerializer extends JsonSerializer<Mutation> {
31 + @Override
32 + public void serialize(Mutation condition, JsonGenerator generator,
33 + SerializerProvider provider)
34 + throws IOException, JsonProcessingException {
35 + generator.writeStartArray();
36 + generator.writeString(condition.getColumn());
37 + generator.writeString(condition.getMutator().mutator());
38 + generator.writeObject(condition.getValue());
39 + generator.writeEndArray();
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.notation.json;
17 +
18 +import java.io.IOException;
19 +import java.util.Map;
20 +
21 +import org.onosproject.ovsdb.rfc.notation.OvsdbMap;
22 +
23 +import com.fasterxml.jackson.core.JsonGenerator;
24 +import com.fasterxml.jackson.core.JsonProcessingException;
25 +import com.fasterxml.jackson.databind.JsonSerializer;
26 +import com.fasterxml.jackson.databind.SerializerProvider;
27 +
28 +/**
29 + * OvsdbMap Serializer.
30 + */
31 +public class OvsdbMapSerializer extends JsonSerializer<OvsdbMap> {
32 + @Override
33 + public void serialize(OvsdbMap map, JsonGenerator generator,
34 + SerializerProvider provider)
35 + throws IOException, JsonProcessingException {
36 + generator.writeStartArray();
37 + generator.writeString("map");
38 + generator.writeStartArray();
39 + Map javaMap = map.map();
40 + for (Object key : javaMap.keySet()) {
41 + generator.writeStartArray();
42 + generator.writeObject(key);
43 + generator.writeObject(javaMap.get(key));
44 + generator.writeEndArray();
45 + }
46 + generator.writeEndArray();
47 + generator.writeEndArray();
48 + }
49 +}
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.notation.json;
17 +
18 +import java.io.IOException;
19 +import java.util.Set;
20 +
21 +import org.onosproject.ovsdb.rfc.notation.OvsdbSet;
22 +
23 +import com.fasterxml.jackson.core.JsonGenerator;
24 +import com.fasterxml.jackson.core.JsonProcessingException;
25 +import com.fasterxml.jackson.databind.JsonSerializer;
26 +import com.fasterxml.jackson.databind.SerializerProvider;
27 +
28 +/**
29 + * OvsdbSet Serializer.
30 + */
31 +public class OvsdbSetSerializer extends JsonSerializer<OvsdbSet> {
32 + @Override
33 + public void serialize(OvsdbSet set, JsonGenerator generator,
34 + SerializerProvider provider)
35 + throws IOException, JsonProcessingException {
36 + generator.writeStartArray();
37 + generator.writeString("set");
38 + generator.writeStartArray();
39 + Set javaSet = set.set();
40 + for (Object key : javaSet) {
41 + generator.writeObject(key);
42 + }
43 + generator.writeEndArray();
44 + generator.writeEndArray();
45 + }
46 +}
...\ No newline at end of file ...\ No newline at end of file
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.notation.json;
17 +
18 +import java.io.IOException;
19 +
20 +import org.onosproject.ovsdb.rfc.notation.UUID;
21 +
22 +import com.fasterxml.jackson.core.JsonGenerator;
23 +import com.fasterxml.jackson.core.JsonProcessingException;
24 +import com.fasterxml.jackson.databind.JsonSerializer;
25 +import com.fasterxml.jackson.databind.SerializerProvider;
26 +
27 +/**
28 + * UUID Serializer.
29 + */
30 +public class UUIDSerializer extends JsonSerializer<UUID> {
31 + @Override
32 + public void serialize(UUID value, JsonGenerator generator,
33 + SerializerProvider provider)
34 + throws IOException, JsonProcessingException {
35 + generator.writeStartArray();
36 + try {
37 + java.util.UUID.fromString(value.value());
38 + generator.writeString("uuid");
39 + } catch (IllegalArgumentException ex) {
40 + generator.writeString("named-uuid");
41 + }
42 + generator.writeString(value.value());
43 + generator.writeEndArray();
44 + }
45 +}
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.notation.json;
17 +
18 +import org.onosproject.ovsdb.rfc.message.UpdateNotification;
19 +
20 +import com.fasterxml.jackson.databind.JsonNode;
21 +import com.fasterxml.jackson.databind.util.StdConverter;
22 +
23 +/**
24 + * UpdateNotificationDeser Converter.
25 + */
26 +public class UpdateNotificationConverter
27 + extends StdConverter<JsonNode, UpdateNotification> {
28 +
29 + @Override
30 + public UpdateNotification convert(JsonNode value) {
31 + return deserialize(value);
32 + }
33 +
34 + /**
35 + * JsonNode convert into UpdateNotification.
36 + * @param node the "params" node of UpdateNotification JsonNode
37 + */
38 + private UpdateNotification deserialize(JsonNode node) {
39 + if (node.isArray()) {
40 + if (node.size() == 2) {
41 + return new UpdateNotification(node.get(0).asText(), node.get(1));
42 + }
43 + }
44 + return null;
45 + }
46 +}
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.operations;
17 +
18 +import org.onosproject.ovsdb.rfc.schema.TableSchema;
19 +
20 +/**
21 + * assert operation.Refer to RFC 7047 Section 5.2.
22 + */
23 +public final class Abort implements Operation {
24 +
25 + private final String op;
26 +
27 + /**
28 + * Constructs a Abort object.
29 + */
30 + public Abort() {
31 + this.op = Operations.ABORT.op();
32 + }
33 +
34 + @Override
35 + public String getOp() {
36 + return op;
37 + }
38 +
39 + @Override
40 + public TableSchema getTableSchema() {
41 + return null;
42 + }
43 +}
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.operations;
17 +
18 +import static com.google.common.base.Preconditions.checkNotNull;
19 +
20 +import org.onosproject.ovsdb.rfc.schema.TableSchema;
21 +
22 +/**
23 + * assert operation.Refer to RFC 7047 Section 5.2.
24 + */
25 +public final class Assert implements Operation {
26 +
27 + private final String op;
28 + private final String lock;
29 +
30 + /**
31 + * Constructs a Assert object.
32 + * @param lock the lock member of assert operation
33 + */
34 + public Assert(String lock) {
35 + checkNotNull(lock, "lock is not null");
36 + this.op = Operations.ASSERT.op();
37 + this.lock = lock;
38 + }
39 +
40 + /**
41 + * Returns the lock member of assert operation.
42 + * @return the lock member of assert operation
43 + */
44 + public String getLock() {
45 + return lock;
46 + }
47 +
48 + @Override
49 + public String getOp() {
50 + return op;
51 + }
52 +
53 + @Override
54 + public TableSchema getTableSchema() {
55 + return null;
56 + }
57 +}
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.operations;
17 +
18 +import static com.google.common.base.Preconditions.checkNotNull;
19 +
20 +import org.onosproject.ovsdb.rfc.schema.TableSchema;
21 +
22 +/**
23 + * comment operation.Refer to RFC 7047 Section 5.2.
24 + */
25 +public final class Comment implements Operation {
26 +
27 + private final String op;
28 + private final String comment;
29 +
30 + /**
31 + * Constructs a Comment object.
32 + * @param comment the comment member of comment operation
33 + */
34 + public Comment(String comment) {
35 + checkNotNull(comment, "comment is not null");
36 + this.op = Operations.COMMENT.op();
37 + this.comment = comment;
38 + }
39 +
40 + /**
41 + * Returns the comment member of comment operation.
42 + * @return the comment member of comment operation
43 + */
44 + public String getComment() {
45 + return comment;
46 + }
47 +
48 + @Override
49 + public String getOp() {
50 + return op;
51 + }
52 +
53 + @Override
54 + public TableSchema getTableSchema() {
55 + return null;
56 + }
57 +}
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.operations;
17 +
18 +import static com.google.common.base.Preconditions.checkNotNull;
19 +
20 +import org.onosproject.ovsdb.rfc.schema.TableSchema;
21 +
22 +/**
23 + * commit operation.Refer to RFC 7047 Section 5.2.
24 + */
25 +public final class Commit implements Operation {
26 +
27 + private final String op;
28 + private final Boolean durable;
29 +
30 + /**
31 + * Constructs a Commit object.
32 + * @param durable the durable member of commit operation
33 + */
34 + public Commit(Boolean durable) {
35 + checkNotNull(durable, "durable is not null");
36 + this.op = Operations.COMMIT.op();
37 + this.durable = durable;
38 + }
39 +
40 + /**
41 + * Returns the durable member of commit operation.
42 + * @return the durable member of commit operation
43 + */
44 + public Boolean isDurable() {
45 + return durable;
46 + }
47 +
48 + @Override
49 + public String getOp() {
50 + return op;
51 + }
52 +
53 + @Override
54 + public TableSchema getTableSchema() {
55 + return null;
56 + }
57 +}
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.operations;
17 +
18 +import static com.google.common.base.Preconditions.checkNotNull;
19 +
20 +import java.util.List;
21 +
22 +import org.onosproject.ovsdb.rfc.notation.Condition;
23 +import org.onosproject.ovsdb.rfc.schema.TableSchema;
24 +
25 +import com.fasterxml.jackson.annotation.JsonIgnore;
26 +import com.fasterxml.jackson.annotation.JsonProperty;
27 +
28 +/**
29 + * delete operation.Refer to RFC 7047 Section 5.2.
30 + */
31 +public final class Delete implements Operation {
32 +
33 + @JsonIgnore
34 + private final TableSchema tableSchema;
35 + private final String op;
36 + private final List<Condition> where;
37 +
38 + /**
39 + * Constructs a Delete object.
40 + * @param schema TableSchema entity
41 + * @param where the List of Condition entity
42 + */
43 + public Delete(TableSchema schema, List<Condition> where) {
44 + checkNotNull(schema, "TableSchema is not null");
45 + checkNotNull(where, "where is not null");
46 + this.tableSchema = schema;
47 + this.op = Operations.DELETE.op();
48 + this.where = where;
49 + }
50 +
51 + /**
52 + * Returns the where member of delete operation.
53 + * @return the where member of delete operation
54 + */
55 + public List<Condition> getWhere() {
56 + return where;
57 + }
58 +
59 + @Override
60 + public String getOp() {
61 + return op;
62 + }
63 +
64 + @Override
65 + public TableSchema getTableSchema() {
66 + return tableSchema;
67 + }
68 +
69 + /**
70 + * For the use of serialization.
71 + * @return the table member of update operation
72 + */
73 + @JsonProperty
74 + public String getTable() {
75 + return (tableSchema == null) ? null : tableSchema.name();
76 + }
77 +}
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.operations;
17 +
18 +import static com.google.common.base.Preconditions.checkNotNull;
19 +
20 +import java.util.Collection;
21 +import java.util.Map;
22 +
23 +import org.onosproject.ovsdb.rfc.notation.Column;
24 +import org.onosproject.ovsdb.rfc.notation.Row;
25 +import org.onosproject.ovsdb.rfc.schema.ColumnSchema;
26 +import org.onosproject.ovsdb.rfc.schema.TableSchema;
27 +import org.onosproject.ovsdb.rfc.utils.TransValueUtil;
28 +
29 +import com.fasterxml.jackson.annotation.JsonIgnore;
30 +import com.fasterxml.jackson.annotation.JsonProperty;
31 +import com.google.common.collect.Maps;
32 +
33 +/**
34 + * insert operation.Refer to RFC 7047 Section 5.2.
35 + */
36 +public final class Insert implements Operation {
37 +
38 + @JsonIgnore
39 + private final TableSchema tableSchema;
40 + private final String op;
41 + @JsonProperty("uuid-name")
42 + private final String uuidName;
43 + private final Map<String, Object> row;
44 +
45 + /**
46 + * Constructs a Insert object.
47 + * @param schema TableSchema entity
48 + * @param uuidName uuid-name
49 + * @param row Row entity
50 + */
51 + public Insert(TableSchema schema, String uuidName, Row row) {
52 + checkNotNull(schema, "TableSchema is not null");
53 + checkNotNull(uuidName, "uuidName is not null");
54 + checkNotNull(row, "row is not null");
55 + this.tableSchema = schema;
56 + this.op = Operations.INSERT.op();
57 + this.uuidName = uuidName;
58 + this.row = Maps.newHashMap();
59 + generateOperationRow(row);
60 + }
61 +
62 + /**
63 + * Row entity convert into the row format of insert operation. Refer to RFC
64 + * 7047 Section 5.2.
65 + * @param row Row entity
66 + */
67 + private void generateOperationRow(Row row) {
68 + Collection<Column> columns = row.getColumns();
69 + for (Column column : columns) {
70 + ColumnSchema columnSchema = column.schema();
71 + Object value = column.data();
72 + Object untypedValue = TransValueUtil.getFormatData(value);
73 + this.row.put(columnSchema.name(), untypedValue);
74 + }
75 + }
76 +
77 + /**
78 + * Returns the uuid-name member of insert operation.
79 + * @return the uuid-name member of insert operation
80 + */
81 + public String getUuidName() {
82 + return uuidName;
83 + }
84 +
85 + /**
86 + * Returns the row member of insert operation.
87 + * @return the row member of insert operation
88 + */
89 + public Map<String, Object> getRow() {
90 + return row;
91 + }
92 +
93 + @Override
94 + public String getOp() {
95 + return op;
96 + }
97 +
98 + @Override
99 + public TableSchema getTableSchema() {
100 + return tableSchema;
101 + }
102 +
103 + /**
104 + * For the use of serialization.
105 + * @return the table member of update operation
106 + */
107 + @JsonProperty
108 + public String getTable() {
109 + return (tableSchema == null) ? null : tableSchema.name();
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.operations;
17 +
18 +import static com.google.common.base.Preconditions.checkNotNull;
19 +
20 +import java.util.List;
21 +
22 +import org.onosproject.ovsdb.rfc.notation.Condition;
23 +import org.onosproject.ovsdb.rfc.notation.Mutation;
24 +import org.onosproject.ovsdb.rfc.schema.TableSchema;
25 +
26 +import com.fasterxml.jackson.annotation.JsonIgnore;
27 +import com.fasterxml.jackson.annotation.JsonProperty;
28 +
29 +/**
30 + * mutate operation.Refer to RFC 7047 Section 5.2.
31 + */
32 +public final class Mutate implements Operation {
33 +
34 + @JsonIgnore
35 + private final TableSchema tableSchema;
36 + private final String op;
37 + private final List<Condition> where;
38 + private final List<Mutation> mutations;
39 +
40 + /**
41 + * Constructs a Mutate object.
42 + * @param schema TableSchema entity
43 + * @param where the List of Condition entity
44 + * @param mutations the List of Mutation entity
45 + */
46 + public Mutate(TableSchema schema, List<Condition> where,
47 + List<Mutation> mutations) {
48 + checkNotNull(schema, "TableSchema is not null");
49 + checkNotNull(mutations, "mutations is not null");
50 + checkNotNull(where, "where is not null");
51 + this.tableSchema = schema;
52 + this.op = Operations.MUTATE.op();
53 + this.where = where;
54 + this.mutations = mutations;
55 + }
56 +
57 + /**
58 + * Returns the mutations member of mutate operation.
59 + * @return the mutations member of mutate operation
60 + */
61 + public List<Mutation> getMutations() {
62 + return mutations;
63 + }
64 +
65 + /**
66 + * Returns the where member of mutate operation.
67 + * @return the where member of mutate operation
68 + */
69 + public List<Condition> getWhere() {
70 + return where;
71 + }
72 +
73 + @Override
74 + public String getOp() {
75 + return op;
76 + }
77 +
78 + @Override
79 + public TableSchema getTableSchema() {
80 + return tableSchema;
81 + }
82 +
83 + /**
84 + * For the use of serialization.
85 + * @return the table member of update operation
86 + */
87 + @JsonProperty
88 + public String getTable() {
89 + return (tableSchema == null) ? null : tableSchema.name();
90 + }
91 +}
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.operations;
17 +
18 +import org.onosproject.ovsdb.rfc.schema.TableSchema;
19 +
20 +/**
21 + * Operation interface.
22 + */
23 +public interface Operation {
24 +
25 + /**
26 + * Returns the op member of update operation.
27 + * @return the op member of update operation
28 + */
29 + String getOp();
30 +
31 + /**
32 + * Returns TableSchema entity.
33 + * @return TableSchema entity
34 + */
35 + TableSchema getTableSchema();
36 +
37 + /**
38 + * Operations must be "insert", "select", "update", "mutate", "delete",
39 + * "commit", "abort", "comment", "assert". Refer to RFC 7047 Section 5.2.
40 + */
41 + public enum Operations {
42 + INSERT("insert"), SELECT("select"), UPDATE("update"), MUTATE("mutate"),
43 + DELETE("delete"), COMMIT("commit"), ABORT("abort"), COMMENT("comment"),
44 + ASSERT("assert");
45 +
46 + private String op;
47 +
48 + private Operations(String op) {
49 + this.op = op;
50 + }
51 +
52 + /**
53 + * Returns the op for Operations.
54 + * @return the op
55 + */
56 + public String op() {
57 + return op;
58 + }
59 + }
60 +}
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.operations;
17 +
18 +import static com.google.common.base.Preconditions.checkNotNull;
19 +
20 +import java.util.List;
21 +
22 +import org.onosproject.ovsdb.rfc.notation.Condition;
23 +import org.onosproject.ovsdb.rfc.schema.TableSchema;
24 +
25 +import com.fasterxml.jackson.annotation.JsonIgnore;
26 +import com.fasterxml.jackson.annotation.JsonProperty;
27 +
28 +/**
29 + * select operation.Refer to RFC 7047 Section 5.2.
30 + */
31 +public final class Select implements Operation {
32 +
33 + @JsonIgnore
34 + private final TableSchema tableSchema;
35 + private final String op;
36 + private final List<Condition> where;
37 + private final List<String> columns;
38 +
39 + /**
40 + * Constructs a Select object.
41 + * @param schema TableSchema entity
42 + * @param where the List of Condition entity
43 + * @param columns the List of column name
44 + */
45 + public Select(TableSchema schema, List<Condition> where, List<String> columns) {
46 + checkNotNull(schema, "TableSchema is not null");
47 + checkNotNull(where, "where is not null");
48 + checkNotNull(columns, "columns is not null");
49 + this.tableSchema = schema;
50 + this.op = Operations.SELECT.op();
51 + this.where = where;
52 + this.columns = columns;
53 + }
54 +
55 + /**
56 + * Returns the columns member of select operation.
57 + * @return the columns member of select operation
58 + */
59 + public List<String> getColumns() {
60 + return columns;
61 + }
62 +
63 + /**
64 + * Returns the where member of select operation.
65 + * @return the where member of select operation
66 + */
67 + public List<Condition> getWhere() {
68 + return where;
69 + }
70 +
71 + @Override
72 + public String getOp() {
73 + return op;
74 + }
75 +
76 + @Override
77 + public TableSchema getTableSchema() {
78 + return tableSchema;
79 + }
80 +
81 + /**
82 + * For the use of serialization.
83 + * @return the table member of update operation
84 + */
85 + @JsonProperty
86 + public String getTable() {
87 + return (tableSchema == null) ? null : tableSchema.name();
88 + }
89 +}
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.operations;
17 +
18 +import static com.google.common.base.Preconditions.checkNotNull;
19 +
20 +import java.util.Collection;
21 +import java.util.List;
22 +import java.util.Map;
23 +
24 +import org.onosproject.ovsdb.rfc.notation.Column;
25 +import org.onosproject.ovsdb.rfc.notation.Condition;
26 +import org.onosproject.ovsdb.rfc.notation.Row;
27 +import org.onosproject.ovsdb.rfc.schema.ColumnSchema;
28 +import org.onosproject.ovsdb.rfc.schema.TableSchema;
29 +import org.onosproject.ovsdb.rfc.utils.TransValueUtil;
30 +
31 +import com.fasterxml.jackson.annotation.JsonIgnore;
32 +import com.fasterxml.jackson.annotation.JsonProperty;
33 +import com.google.common.collect.Maps;
34 +
35 +/**
36 + * update operation.Refer to RFC 7047 Section 5.2.
37 + */
38 +public final class Update implements Operation {
39 +
40 + @JsonIgnore
41 + private final TableSchema tableSchema;
42 + private final String op;
43 + private final Map<String, Object> row;
44 + private final List<Condition> where;
45 +
46 + /**
47 + * Constructs a Update object.
48 + * @param schema TableSchema entity
49 + * @param row Row entity
50 + * @param where the List of Condition entity
51 + */
52 + public Update(TableSchema schema, Row row, List<Condition> where) {
53 + checkNotNull(schema, "TableSchema is not null");
54 + checkNotNull(row, "row is not null");
55 + checkNotNull(where, "where is not null");
56 + this.tableSchema = schema;
57 + this.op = Operations.UPDATE.op();
58 + this.row = Maps.newHashMap();
59 + this.where = where;
60 + generateOperationRow(row);
61 + }
62 +
63 + /**
64 + * Row entity convert into the row format of update operation. Refer to RFC
65 + * 7047 Section 5.2.
66 + * @param row Row entity
67 + */
68 + private void generateOperationRow(Row row) {
69 + Collection<Column> columns = row.getColumns();
70 + for (Column column : columns) {
71 + ColumnSchema columnSchema = column.schema();
72 + Object value = column.data();
73 + Object untypedValue = TransValueUtil.getFormatData(value);
74 + this.row.put(columnSchema.name(), untypedValue);
75 + }
76 + }
77 +
78 + /**
79 + * Returns the row member of update operation.
80 + * @return the row member of update operation
81 + */
82 + public Map<String, Object> getRow() {
83 + return row;
84 + }
85 +
86 + /**
87 + * Returns the where member of update operation.
88 + * @return the where member of update operation
89 + */
90 + public List<Condition> getWhere() {
91 + return where;
92 + }
93 +
94 + @Override
95 + public String getOp() {
96 + return op;
97 + }
98 +
99 + @Override
100 + public TableSchema getTableSchema() {
101 + return tableSchema;
102 + }
103 +
104 + /**
105 + * For the use of serialization.
106 + * @return the table member of update operation
107 + */
108 + @JsonProperty
109 + public String getTable() {
110 + return (tableSchema == null) ? null : tableSchema.name();
111 + }
112 +}
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.schema;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +import static com.google.common.base.Preconditions.checkNotNull;
20 +
21 +import java.util.Objects;
22 +
23 +import org.onosproject.ovsdb.rfc.schema.type.ColumnType;
24 +
25 +/**
26 + * A schema for the column represented by column-schema.
27 + */
28 +public final class ColumnSchema {
29 + private final String name;
30 + private final ColumnType type;
31 +
32 + /**
33 + * Constructs a ColumnSchema object.
34 + * @param name the column name
35 + * @param columnType the column type
36 + */
37 + public ColumnSchema(String name, ColumnType columnType) {
38 + checkNotNull(name, "name is not null");
39 + checkNotNull(columnType, "columnType is not null");
40 + this.name = name;
41 + this.type = columnType;
42 + }
43 +
44 + /**
45 + * Returns the name of column.
46 + * @return the name of column
47 + */
48 + public String name() {
49 + return name;
50 + }
51 +
52 + /**
53 + * Returns the type of column.
54 + * @return the type of column
55 + */
56 + public ColumnType type() {
57 + return type;
58 + }
59 +
60 + @Override
61 + public int hashCode() {
62 + return Objects.hash(name, type);
63 + }
64 +
65 + @Override
66 + public boolean equals(Object obj) {
67 + if (this == obj) {
68 + return true;
69 + }
70 + if (obj instanceof ColumnSchema) {
71 + final ColumnSchema other = (ColumnSchema) obj;
72 + return Objects.equals(this.name, other.name)
73 + && Objects.equals(this.type, other.type);
74 + }
75 + return false;
76 + }
77 +
78 + @Override
79 + public String toString() {
80 + return toStringHelper(this).add("name", name).add("type", type)
81 + .toString();
82 + }
83 +}
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.schema;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +import static com.google.common.base.Preconditions.checkNotNull;
20 +
21 +import java.util.Map;
22 +import java.util.Objects;
23 +import java.util.Set;
24 +
25 +/**
26 + * A schema for the database represented by database-schema, which consists of
27 + * a set of tables.
28 + */
29 +public final class DatabaseSchema {
30 +
31 + private final String name;
32 + private final String version;
33 + private final Map<String, TableSchema> tableSchemas;
34 +
35 + /**
36 + * Constructs a DatabaseSchema object.
37 + * @param name the name of database
38 + * @param version the version of database
39 + * @param tableSchemas a map of TableSchema
40 + */
41 + public DatabaseSchema(String name, String version,
42 + Map<String, TableSchema> tableSchemas) {
43 + checkNotNull(name, "name is not null");
44 + checkNotNull(version, "version is not null");
45 + checkNotNull(tableSchemas, "tableSchemas is not null");
46 + this.name = name;
47 + this.version = version;
48 + this.tableSchemas = tableSchemas;
49 + }
50 +
51 + /**
52 + * Returns the name of database.
53 + * @return the name of database
54 + */
55 + public String name() {
56 + return name;
57 + }
58 +
59 + /**
60 + * Returns the version of database.
61 + * @return the version of database
62 + */
63 + public String version() {
64 + return version;
65 + }
66 +
67 + /**
68 + * Returns a map of TableSchema.
69 + * @return a map of TableSchema
70 + */
71 + public Map<String, TableSchema> tableSchemas() {
72 + return tableSchemas;
73 + }
74 +
75 + /**
76 + * Returns a set of table name.
77 + * @return a set of table name
78 + */
79 + public Set<String> getTableNames() {
80 + return this.tableSchemas.keySet();
81 + }
82 +
83 + /**
84 + * Determine whether contain the table.
85 + * @param tableName table name
86 + * @return boolean
87 + */
88 + public boolean hasTable(String tableName) {
89 + return this.getTableNames().contains(tableName);
90 + }
91 +
92 + /**
93 + * Returns the TableSchema whose name is the tableName.
94 + * @param tableName table name
95 + * @return TableSchema
96 + */
97 + public TableSchema getTableSchema(String tableName) {
98 + TableSchema table = tableSchemas.get(tableName);
99 + return table;
100 + }
101 +
102 + /**
103 + * generate initialization columns in each table namely _uuid and _version.
104 + */
105 + public void generateInitializationColumns() {
106 + for (TableSchema tableSchema : tableSchemas.values()) {
107 + tableSchema.generateInitializationColumns();
108 + }
109 + }
110 +
111 + @Override
112 + public int hashCode() {
113 + return Objects.hash(name, version, tableSchemas);
114 + }
115 +
116 + @Override
117 + public boolean equals(Object obj) {
118 + if (this == obj) {
119 + return true;
120 + }
121 + if (obj instanceof DatabaseSchema) {
122 + final DatabaseSchema other = (DatabaseSchema) obj;
123 + return Objects.equals(this.name, other.name)
124 + && Objects.equals(this.version, other.version)
125 + && Objects.equals(this.tableSchemas, other.tableSchemas);
126 + }
127 + return false;
128 + }
129 +
130 + @Override
131 + public String toString() {
132 + return toStringHelper(this).add("name", name).add("version", version)
133 + .add("tableSchemas", tableSchemas).toString();
134 + }
135 +}
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.schema;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +import static com.google.common.base.Preconditions.checkNotNull;
20 +
21 +import java.util.Map;
22 +import java.util.Objects;
23 +import java.util.Set;
24 +
25 +import org.onosproject.ovsdb.rfc.schema.type.AtomicColumnType;
26 +import org.onosproject.ovsdb.rfc.schema.type.UuidBaseType;
27 +
28 +/**
29 + * A schema for the table represented by table-schema, which consists of a set
30 + * of columns.
31 + */
32 +public final class TableSchema {
33 +
34 + private final String name;
35 + private final Map<String, ColumnSchema> columnSchemas;
36 +
37 + /**
38 + * Constructs a TableSchema object.
39 + * @param name the name of table
40 + * @param columnSchemas a map of ColumnSchema
41 + */
42 + public TableSchema(String name, Map<String, ColumnSchema> columnSchemas) {
43 + checkNotNull(name, "name is not null");
44 + checkNotNull(columnSchemas, "columnSchemas is not null");
45 + this.name = name;
46 + this.columnSchemas = columnSchemas;
47 + }
48 +
49 + /**
50 + * Returns the name of table.
51 + * @return the name of table
52 + */
53 + public String name() {
54 + return name;
55 + }
56 +
57 + /**
58 + * Returns a map of ColumnSchema.
59 + * @return a map of ColumnSchema
60 + */
61 + public Map<String, ColumnSchema> columnSchemas() {
62 + return this.columnSchemas;
63 + }
64 +
65 + /**
66 + * Returns a set of column name.
67 + * @return a set of column name
68 + */
69 + public Set<String> getColumnNames() {
70 + return this.columnSchemas.keySet();
71 + }
72 +
73 + /**
74 + * Determine whether contain the column.
75 + * @param columnName column name
76 + * @return boolean
77 + */
78 + public boolean hasColumn(String columnName) {
79 + return this.getColumnNames().contains(columnName);
80 + }
81 +
82 + /**
83 + * Returns the ColumnSchema whose name is the columnName.
84 + * @param columnName column name
85 + * @return ColumnSchema
86 + */
87 + public ColumnSchema getColumnSchema(String columnName) {
88 + return this.columnSchemas.get(columnName);
89 + }
90 +
91 + /**
92 + * Refer to RFC 7047 Section 3.2. generate initialization columns in each
93 + * table namely _uuid and _version.
94 + */
95 + public void generateInitializationColumns() {
96 + columnSchemas
97 + .put("_uuid",
98 + new ColumnSchema("_uuid",
99 + new AtomicColumnType(new UuidBaseType())));
100 + columnSchemas
101 + .put("_version",
102 + new ColumnSchema("_version",
103 + new AtomicColumnType(new UuidBaseType())));
104 + }
105 +
106 + @Override
107 + public int hashCode() {
108 + return Objects.hash(name, columnSchemas);
109 + }
110 +
111 + @Override
112 + public boolean equals(Object obj) {
113 + if (this == obj) {
114 + return true;
115 + }
116 + if (obj instanceof TableSchema) {
117 + final TableSchema other = (TableSchema) obj;
118 + return Objects.equals(this.name, other.name)
119 + && Objects.equals(this.columnSchemas, other.columnSchemas);
120 + }
121 + return false;
122 + }
123 +
124 + @Override
125 + public String toString() {
126 + return toStringHelper(this).add("name", name)
127 + .add("columnSchemas", columnSchemas).toString();
128 + }
129 +}
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.schema.type;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +import static com.google.common.base.Preconditions.checkNotNull;
20 +
21 +import java.util.Objects;
22 +
23 +/**
24 + * The "atomic-type" specifies the type of data stored in this column. Refer
25 + * to RFC 7047 Section 3.2.
26 + */
27 +public final class AtomicColumnType implements ColumnType {
28 + private final BaseType baseType;
29 + private final int min;
30 + private final int max;
31 +
32 + /**
33 + * Constructs a AtomicColumnType object.
34 + * @param baseType BaseType entity
35 + */
36 + public AtomicColumnType(BaseType baseType) {
37 + checkNotNull(baseType, "BaseType is not null");
38 + this.baseType = baseType;
39 + this.min = 1;
40 + this.max = 1;
41 + }
42 +
43 + /**
44 + * Constructs a AtomicColumnType object.
45 + * @param baseType BaseType entity
46 + * @param min min constraint
47 + * @param max max constraint
48 + */
49 + public AtomicColumnType(BaseType baseType, int min, int max) {
50 + checkNotNull(baseType, "BaseType is not null");
51 + this.baseType = baseType;
52 + this.min = min;
53 + this.max = max;
54 + }
55 +
56 + /**
57 + * Get baseType.
58 + * @return baseType
59 + */
60 + public BaseType baseType() {
61 + return baseType;
62 + }
63 +
64 + /**
65 + * Get min.
66 + * @return min
67 + */
68 + public int min() {
69 + return min;
70 + }
71 +
72 + /**
73 + * Get max.
74 + * @return max
75 + */
76 + public int max() {
77 + return max;
78 + }
79 +
80 + @Override
81 + public int hashCode() {
82 + return Objects.hash(baseType, min, max);
83 + }
84 +
85 + @Override
86 + public boolean equals(Object obj) {
87 + if (this == obj) {
88 + return true;
89 + }
90 + if (obj instanceof AtomicColumnType) {
91 + final AtomicColumnType other = (AtomicColumnType) obj;
92 + return Objects.equals(this.baseType, other.baseType)
93 + && Objects.equals(this.min, other.min)
94 + && Objects.equals(this.max, other.max);
95 + }
96 + return false;
97 + }
98 +
99 + @Override
100 + public String toString() {
101 + return toStringHelper(this).add("baseType", baseType).add("min", min)
102 + .add("max", max).toString();
103 + }
104 +}
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.schema.type;
17 +
18 +/**
19 + * One of the strings "integer", "real", "boolean", "string", or "uuid",
20 + * representing the specified scalar type. Refer to RFC 7047 Section 3.2.
21 + */
22 +public interface BaseType {
23 +
24 +}
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.schema.type;
17 +
18 +import java.util.Set;
19 +
20 +import org.onosproject.ovsdb.rfc.error.TypedSchemaException;
21 +import org.onosproject.ovsdb.rfc.schema.type.UuidBaseType.RefType;
22 +
23 +import com.fasterxml.jackson.databind.JsonNode;
24 +import com.google.common.collect.Sets;
25 +
26 +/**
27 + * BaseType Factory class.
28 + */
29 +public final class BaseTypeFactory {
30 +
31 + /**
32 + * Constructs a BaseTypeFactory object.
33 + * This class should not be instantiated.
34 + */
35 + private BaseTypeFactory() {
36 + }
37 +
38 + /**
39 + * Create a BaseType from the JsonNode.
40 + * @param json the BaseType JsonNode
41 + * @param keyorval the key node or value node
42 + * @return BaseType
43 + */
44 + public static BaseType getBaseTypeFromJson(JsonNode json, String keyorval) {
45 + if (json.isValueNode()) {
46 + String type = json.asText().trim();
47 + return fromTypeStr(type);
48 + } else {
49 + if (!json.has(keyorval)) {
50 + throw new TypedSchemaException("not a type");
51 + }
52 + return fromJsonNode(json.get(keyorval));
53 + }
54 + }
55 +
56 + /**
57 + * Get BaseType by the type value of JsonNode.
58 + * @param type the type value of JsonNode
59 + * @return BaseType
60 + */
61 + private static BaseType fromTypeStr(String type) {
62 + switch (type) {
63 + case "boolean":
64 + return new BooleanBaseType();
65 + case "integer":
66 + return new IntegerBaseType();
67 + case "real":
68 + return new RealBaseType();
69 + case "string":
70 + return new StringBaseType();
71 + case "uuid":
72 + return new UuidBaseType();
73 + default:
74 + return null;
75 + }
76 + }
77 +
78 + /**
79 + * json like "string" or json like {"type" : "string", "enum": ["set",
80 + * ["access", "native-tagged"]]}" for key or value.
81 + * @param type JsonNode
82 + */
83 + private static BaseType fromJsonNode(JsonNode type) {
84 + if (type.isTextual()) {
85 + return fromTypeStr(type.asText());
86 + } else if (type.isObject() && type.has("type")) {
87 + String typeStr = type.get("type").asText();
88 + switch (typeStr) {
89 + case "boolean":
90 + return new BooleanBaseType();
91 + case "integer":
92 + return getIntegerBaseType(type);
93 + case "real":
94 + return getRealBaseType(type);
95 + case "string":
96 + return getStringBaseType(type);
97 + case "uuid":
98 + return getUuidBaseType(type);
99 + default:
100 + return null;
101 + }
102 + }
103 + return null;
104 + }
105 +
106 + /**
107 + * Get IntegerBaseType by the type value of JsonNode which contains the
108 + * constraints.
109 + * @param type the type value of JsonNode
110 + * @return IntegerBaseType
111 + */
112 + private static IntegerBaseType getIntegerBaseType(JsonNode type) {
113 + int min = Integer.MIN_VALUE;
114 + int max = Integer.MAX_VALUE;
115 + Set<Integer> enums = Sets.newHashSet();
116 + JsonNode node = type.get("minInteger");
117 + if (node != null) {
118 + min = node.asInt();
119 + }
120 + node = type.get("maxInteger");
121 + if (node != null) {
122 + max = node.asInt();
123 + }
124 + if (node.has("enum")) {
125 + JsonNode anEnum = node.get("enum").get(1);
126 + for (JsonNode n : anEnum) {
127 + enums.add(n.asInt());
128 + }
129 + }
130 + return new IntegerBaseType(min, max, enums);
131 + }
132 +
133 + /**
134 + * Get RealBaseType by the type value of JsonNode which contains the
135 + * constraints.
136 + * @param type the type value of JsonNode
137 + * @return RealBaseType
138 + */
139 + private static RealBaseType getRealBaseType(JsonNode type) {
140 + double min = Double.MIN_VALUE;
141 + double max = Double.MAX_VALUE;
142 + Set<Double> enums = Sets.newHashSet();
143 + JsonNode node = type.get("minReal");
144 + if (node != null) {
145 + min = node.asDouble();
146 + }
147 + node = type.get("maxReal");
148 + if (node != null) {
149 + max = node.asDouble();
150 + }
151 + if (node.has("enum")) {
152 + JsonNode anEnum = node.get("enum").get(1);
153 + for (JsonNode n : anEnum) {
154 + enums.add(n.asDouble());
155 + }
156 + }
157 + return new RealBaseType(min, max, enums);
158 + }
159 +
160 + /**
161 + * Get StringBaseType by the type value of JsonNode which contains the
162 + * constraints.
163 + * @param type the type value of JsonNode
164 + * @return StringBaseType
165 + */
166 + private static StringBaseType getStringBaseType(JsonNode type) {
167 + int minLength = Integer.MIN_VALUE;
168 + int maxLength = Integer.MAX_VALUE;
169 + Set<String> enums = Sets.newHashSet();
170 + JsonNode node = type.get("minLength");
171 + if (node != null) {
172 + minLength = node.asInt();
173 + }
174 + node = type.get("maxLength");
175 + if (node != null) {
176 + maxLength = node.asInt();
177 + }
178 + if (node.has("enum")) {
179 + JsonNode enumVal = node.get("enum");
180 + if (enumVal.isArray()) {
181 + JsonNode anEnum = enumVal.get(1);
182 + for (JsonNode n : anEnum) {
183 + enums.add(n.asText());
184 + }
185 + } else if (enumVal.isTextual()) {
186 + enums.add(enumVal.asText());
187 + }
188 + }
189 + return new StringBaseType(minLength, maxLength, enums);
190 + }
191 +
192 + /**
193 + * Get UuidBaseType by the type value of JsonNode which contains the
194 + * constraints.
195 + * @param type the type value of JsonNode
196 + * @return UuidBaseType
197 + */
198 + private static UuidBaseType getUuidBaseType(JsonNode type) {
199 + String refTable = null;
200 + String refType = RefType.STRONG.refType();
201 + JsonNode node = type.get("refTable");
202 + if (node != null) {
203 + refTable = node.asText();
204 + }
205 + node = type.get("refType");
206 + if (node != null) {
207 + refType = node.asText();
208 + }
209 + return new UuidBaseType(refTable, refType);
210 + }
211 +}
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.schema.type;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +
20 +/**
21 + * One of the strings "integer", "real", "boolean", "string", or "uuid",
22 + * representing the specified scalar type. Refer to RFC 7047 Section 3.2.
23 + * Because BooleanBaseType has no constraint conditions, and in order to be
24 + * consistent with other BaseType, so this class is empty except for the
25 + * toString method.
26 + */
27 +public final class BooleanBaseType implements BaseType {
28 +
29 + @Override
30 + public String toString() {
31 + return toStringHelper(this).toString();
32 + }
33 +}
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.schema.type;
17 +
18 +/**
19 + * The "type" specifies the type of data stored in this column. Refer to RFC
20 + * 7047 Section 3.2.
21 + */
22 +public interface ColumnType {
23 +
24 +}
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.schema.type;
17 +
18 +import org.onosproject.ovsdb.rfc.error.TypedSchemaException;
19 +import org.onosproject.ovsdb.rfc.utils.ObjectMapperUtil;
20 +
21 +import com.fasterxml.jackson.databind.JsonNode;
22 +
23 +/**
24 + * ColumnType Factory class.
25 + */
26 +public final class ColumnTypeFactory {
27 +
28 + /**
29 + * Constructs a ColumnTypeFactory object.
30 + * This class should not be instantiated.
31 + */
32 + private ColumnTypeFactory() {
33 + }
34 +
35 + /**
36 + * Those Json's key/value pairs.
37 + */
38 + public enum Type {
39 + KEY("key"), VALUE("value");
40 +
41 + private final String type;
42 +
43 + private Type(String type) {
44 + this.type = type;
45 + }
46 +
47 + /**
48 + * Returns the type for Type.
49 + * @return the type
50 + */
51 + public String type() {
52 + return type;
53 + }
54 + }
55 +
56 + /**
57 + * JsonNode like "flow_tables":{"type":{"key":{"maxInteger":254,"minInteger":0,"type":
58 + * "integer"},"min":0,"value":{"type":"uuid","refTable":"Flow_Table"},"max":
59 + * "unlimited"}}.
60 + * @param json the ColumnType JsonNode
61 + * @return ColumnType
62 + */
63 + public static ColumnType getColumnTypeFromJson(JsonNode json) {
64 + if (json.isObject() && !json.has(Type.VALUE.type())) {
65 + return createAtomicColumnType(json);
66 + } else if (!json.isValueNode() && json.has(Type.VALUE.type())) {
67 + return createKeyValuedColumnType(json);
68 + }
69 + throw new TypedSchemaException("could not find the right column type :"
70 + + ObjectMapperUtil.convertToString(json));
71 + }
72 +
73 + /**
74 + * Create AtomicColumnType entity.
75 + * @param json JsonNode
76 + * @return AtomicColumnType entity
77 + */
78 + private static AtomicColumnType createAtomicColumnType(JsonNode json) {
79 + BaseType baseType = BaseTypeFactory
80 + .getBaseTypeFromJson(json, Type.KEY.type());
81 + int min = 1;
82 + int max = 1;
83 + JsonNode node = json.get("min");
84 + if (node != null) {
85 + min = node.asInt();
86 + }
87 + node = json.get("max");
88 + if (node != null) {
89 + if (node.isNumber()) {
90 + max = node.asInt();
91 + } else if (node.isTextual() && "unlimited".equals(node.asText())) {
92 + max = Integer.MAX_VALUE;
93 + }
94 + }
95 + return new AtomicColumnType(baseType, min, max);
96 + }
97 +
98 + /**
99 + * Create KeyValuedColumnType entity.
100 + * @param json JsonNode
101 + * @return KeyValuedColumnType entity
102 + */
103 + private static KeyValuedColumnType createKeyValuedColumnType(JsonNode json) {
104 + BaseType keyType = BaseTypeFactory.getBaseTypeFromJson(json,
105 + Type.KEY.type());
106 + BaseType valueType = BaseTypeFactory
107 + .getBaseTypeFromJson(json, Type.VALUE.type());
108 + int min = 1;
109 + int max = 1;
110 + JsonNode node = json.get("min");
111 + if (node != null) {
112 + min = node.asInt();
113 + }
114 + node = json.get("max");
115 + if (node != null) {
116 + if (node.isNumber()) {
117 + max = node.asInt();
118 + } else if (node.isTextual() && "unlimited".equals(node.asText())) {
119 + max = Integer.MAX_VALUE;
120 + }
121 + }
122 + return new KeyValuedColumnType(keyType, valueType, min, max);
123 + }
124 +}
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.schema.type;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +
20 +import java.util.Objects;
21 +import java.util.Set;
22 +
23 +import com.google.common.collect.Sets;
24 +
25 +/**
26 + * One of the strings "integer", "real", "boolean", "string", or "uuid",
27 + * representing the specified scalar type. Refer to RFC 7047 Section 3.2.
28 + */
29 +public final class IntegerBaseType implements BaseType {
30 + private final int min;
31 + private final int max;
32 + private final Set<Integer> enums;
33 +
34 + /**
35 + * Constructs a IntegerBaseType object.
36 + */
37 + public IntegerBaseType() {
38 + this.min = Integer.MIN_VALUE;
39 + this.max = Integer.MAX_VALUE;
40 + this.enums = Sets.newHashSet();
41 + }
42 +
43 + /**
44 + * Constructs a IntegerBaseType object.
45 + * @param min min constraint
46 + * @param max max constraint
47 + * @param enums enums constraint
48 + */
49 + public IntegerBaseType(int min, int max, Set<Integer> enums) {
50 + this.min = min;
51 + this.max = max;
52 + this.enums = enums;
53 + }
54 +
55 + /**
56 + * Get min.
57 + * @return min
58 + */
59 + public int min() {
60 + return min;
61 + }
62 +
63 + /**
64 + * Get max.
65 + * @return max
66 + */
67 + public int max() {
68 + return max;
69 + }
70 +
71 + /**
72 + * Get enums.
73 + * @return enums
74 + */
75 + public Set<Integer> enums() {
76 + return enums;
77 + }
78 +
79 + @Override
80 + public int hashCode() {
81 + return Objects.hash(min, max, enums);
82 + }
83 +
84 + @Override
85 + public boolean equals(Object obj) {
86 + if (this == obj) {
87 + return true;
88 + }
89 + if (obj instanceof IntegerBaseType) {
90 + final IntegerBaseType other = (IntegerBaseType) obj;
91 + return Objects.equals(this.enums, other.enums)
92 + && Objects.equals(this.min, other.min)
93 + && Objects.equals(this.max, other.max);
94 + }
95 + return false;
96 + }
97 +
98 + @Override
99 + public String toString() {
100 + return toStringHelper(this).add("min", min).add("max", max)
101 + .add("enums", enums).toString();
102 + }
103 +}
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.schema.type;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +import static com.google.common.base.Preconditions.checkNotNull;
20 +
21 +import java.util.Objects;
22 +
23 +/**
24 + * a JSON object that describes the type of a database column, with key and
25 + * value. Refer to RFC 7047 Section 3.2.
26 + */
27 +public final class KeyValuedColumnType implements ColumnType {
28 + private final BaseType keyType;
29 + private final BaseType valueType;
30 + private final int min;
31 + private final int max;
32 +
33 + /**
34 + * Constructs a KeyValuedColumnType object.
35 + * @param keyType BaseType entity
36 + * @param valueType BaseType entity
37 + * @param min min constraint
38 + * @param max max constraint
39 + */
40 + public KeyValuedColumnType(BaseType keyType, BaseType valueType, int min,
41 + int max) {
42 + checkNotNull(keyType, "keyType is not null");
43 + checkNotNull(valueType, "valueType is not null");
44 + this.keyType = keyType;
45 + this.valueType = valueType;
46 + this.min = min;
47 + this.max = max;
48 + }
49 +
50 + /**
51 + * Get keyType.
52 + * @return keyType
53 + */
54 + public BaseType keyType() {
55 + return keyType;
56 + }
57 +
58 + /**
59 + * Get valueType.
60 + * @return valueType
61 + */
62 + public BaseType valueType() {
63 + return valueType;
64 + }
65 +
66 + /**
67 + * Get min.
68 + * @return min
69 + */
70 + public int min() {
71 + return min;
72 + }
73 +
74 + /**
75 + * Get max.
76 + * @return max
77 + */
78 + public int max() {
79 + return max;
80 + }
81 +
82 + @Override
83 + public int hashCode() {
84 + return Objects.hash(keyType, valueType, min, max);
85 + }
86 +
87 + @Override
88 + public boolean equals(Object obj) {
89 + if (this == obj) {
90 + return true;
91 + }
92 + if (obj instanceof KeyValuedColumnType) {
93 + final KeyValuedColumnType other = (KeyValuedColumnType) obj;
94 + return Objects.equals(this.keyType, other.keyType)
95 + && Objects.equals(this.valueType, other.valueType)
96 + && Objects.equals(this.min, other.min)
97 + && Objects.equals(this.max, other.max);
98 + }
99 + return false;
100 + }
101 +
102 + @Override
103 + public String toString() {
104 + return toStringHelper(this).add("keyType", keyType)
105 + .add("valueType", valueType).add("min", min).add("max", max)
106 + .toString();
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.schema.type;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +
20 +import java.util.Objects;
21 +import java.util.Set;
22 +
23 +import com.google.common.collect.Sets;
24 +
25 +/**
26 + * One of the strings "integer", "real", "boolean", "string", or "uuid",
27 + * representing the specified scalar type. Refer to RFC 7047 Section 3.2.
28 + */
29 +public final class RealBaseType implements BaseType {
30 + private final double min;
31 + private final double max;
32 + private final Set<Double> enums;
33 +
34 + /**
35 + * Constructs a RealBaseType object.
36 + */
37 + public RealBaseType() {
38 + this.min = Double.MIN_VALUE;
39 + this.max = Double.MAX_VALUE;
40 + this.enums = Sets.newHashSet();
41 + }
42 +
43 + /**
44 + * Constructs a RealBaseType object.
45 + * @param min min constraint
46 + * @param max max constraint
47 + * @param enums enums constraint
48 + */
49 + public RealBaseType(double min, double max, Set<Double> enums) {
50 + this.min = min;
51 + this.max = max;
52 + this.enums = enums;
53 + }
54 +
55 + /**
56 + * Get min.
57 + * @return min
58 + */
59 + public double getMin() {
60 + return min;
61 + }
62 +
63 + /**
64 + * Get max.
65 + * @return max
66 + */
67 + public double getMax() {
68 + return max;
69 + }
70 +
71 + /**
72 + * Get enums.
73 + * @return enums
74 + */
75 + public Set<Double> getEnums() {
76 + return enums;
77 + }
78 +
79 + @Override
80 + public int hashCode() {
81 + return Objects.hash(min, max, enums);
82 + }
83 +
84 + @Override
85 + public boolean equals(Object obj) {
86 + if (this == obj) {
87 + return true;
88 + }
89 + if (obj instanceof RealBaseType) {
90 + final RealBaseType other = (RealBaseType) obj;
91 + return Objects.equals(this.enums, other.enums)
92 + && Objects.equals(this.min, other.min)
93 + && Objects.equals(this.max, other.max);
94 + }
95 + return false;
96 + }
97 +
98 + @Override
99 + public String toString() {
100 + return toStringHelper(this).add("min", min).add("max", max)
101 + .add("enums", enums).toString();
102 + }
103 +}
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.schema.type;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +
20 +import java.util.Objects;
21 +import java.util.Set;
22 +
23 +import com.google.common.collect.Sets;
24 +
25 +/**
26 + * One of the strings "integer", "real", "boolean", "string", or "uuid",
27 + * representing the specified scalar type. Refer to RFC 7047 Section 3.2.
28 + */
29 +public final class StringBaseType implements BaseType {
30 + private final int minLength;
31 + private final int maxLength;
32 + private final Set<String> enums;
33 +
34 + /**
35 + * Constructs a StringBaseType object.
36 + */
37 + public StringBaseType() {
38 + this.minLength = Integer.MIN_VALUE;
39 + this.maxLength = Integer.MAX_VALUE;
40 + this.enums = Sets.newHashSet();
41 + }
42 +
43 + /**
44 + * Constructs a StringBaseType object.
45 + * @param minLength minLength constraint
46 + * @param maxLength maxLength constraint
47 + * @param enums enums constraint
48 + */
49 + public StringBaseType(int minLength, int maxLength, Set<String> enums) {
50 + this.minLength = minLength;
51 + this.maxLength = maxLength;
52 + this.enums = enums;
53 + }
54 +
55 + /**
56 + * Get minLength.
57 + * @return minLength
58 + */
59 + public int getMinLength() {
60 + return minLength;
61 + }
62 +
63 + /**
64 + * Get maxLength.
65 + * @return maxLength
66 + */
67 + public int getMaxLength() {
68 + return maxLength;
69 + }
70 +
71 + /**
72 + * Get enums.
73 + * @return enums
74 + */
75 + public Set<String> getEnums() {
76 + return enums;
77 + }
78 +
79 + @Override
80 + public int hashCode() {
81 + return Objects.hash(minLength, maxLength, enums);
82 + }
83 +
84 + @Override
85 + public boolean equals(Object obj) {
86 + if (this == obj) {
87 + return true;
88 + }
89 + if (obj instanceof StringBaseType) {
90 + final StringBaseType other = (StringBaseType) obj;
91 + return Objects.equals(this.enums, other.enums)
92 + && Objects.equals(this.minLength, other.minLength)
93 + && Objects.equals(this.maxLength, other.maxLength);
94 + }
95 + return false;
96 + }
97 +
98 + @Override
99 + public String toString() {
100 + return toStringHelper(this).add("minLength", minLength)
101 + .add("maxLength", maxLength).add("enums", enums).toString();
102 + }
103 +}
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.schema.type;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +
20 +import java.util.Objects;
21 +
22 +/**
23 + * One of the strings "integer", "real", "boolean", "string", or "uuid",
24 + * representing the specified scalar type. Refer to RFC 7047 Section 3.2.
25 + */
26 +public final class UuidBaseType implements BaseType {
27 + /**
28 + * RefType is strong or weak. refer to base-type of RFC 7047 Section 3.2.
29 + */
30 + public enum RefType {
31 + STRONG("strong"), WEAK("weak");
32 +
33 + private String refType;
34 +
35 + private RefType(String refType) {
36 + this.refType = refType;
37 + }
38 +
39 + /**
40 + * Returns the refType for RefType.
41 + * @return the refType
42 + */
43 + public String refType() {
44 + return refType;
45 + }
46 + }
47 +
48 + private final String refTable;
49 + private final String refType;
50 +
51 + /**
52 + * Constructs a UuidBaseType object.
53 + */
54 + public UuidBaseType() {
55 + this.refTable = null;
56 + this.refType = RefType.STRONG.refType();
57 + }
58 +
59 + /**
60 + * Constructs a UuidBaseType object.
61 + * @param refTable refTable constraint
62 + * @param refType refType constraint
63 + */
64 + public UuidBaseType(String refTable, String refType) {
65 + this.refTable = refTable;
66 + this.refType = refType;
67 + }
68 +
69 + /**
70 + * Get refTable.
71 + * @return refTable
72 + */
73 + public String getRefTable() {
74 + return refTable;
75 + }
76 +
77 + /**
78 + * Get refType.
79 + * @return refType
80 + */
81 + public String getRefType() {
82 + return refType;
83 + }
84 +
85 + @Override
86 + public int hashCode() {
87 + return Objects.hash(refTable, refType);
88 + }
89 +
90 + @Override
91 + public boolean equals(Object obj) {
92 + if (this == obj) {
93 + return true;
94 + }
95 + if (obj instanceof UuidBaseType) {
96 + final UuidBaseType other = (UuidBaseType) obj;
97 + return Objects.equals(this.refTable, other.refTable)
98 + && Objects.equals(this.refType, other.refType);
99 + }
100 + return false;
101 + }
102 +
103 + @Override
104 + public String toString() {
105 + return toStringHelper(this).add("refTable", refTable)
106 + .add("refType", refType).toString();
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.slf4j.Logger;
19 +import org.slf4j.LoggerFactory;
20 +
21 +import com.fasterxml.jackson.annotation.JsonInclude.Include;
22 +import com.fasterxml.jackson.core.JsonProcessingException;
23 +import com.fasterxml.jackson.databind.DeserializationFeature;
24 +import com.fasterxml.jackson.databind.ObjectMapper;
25 +
26 +/**
27 + * ObjectMapper utility class.
28 + */
29 +public final class ObjectMapperUtil {
30 +
31 + private static final Logger log = LoggerFactory
32 + .getLogger(ObjectMapperUtil.class);
33 +
34 + /**
35 + * Constructs a ObjectMapperUtil object. Utility classes should not have a
36 + * public or default constructor, otherwise it will compile failed. This
37 + * class should not be instantiated.
38 + */
39 + private ObjectMapperUtil() {
40 + }
41 +
42 + /**
43 + * get ObjectMapper entity.
44 + * @return ObjectMapper entity
45 + */
46 + public static ObjectMapper getObjectMapper() {
47 + ObjectMapper objectMapper = new ObjectMapper();
48 + objectMapper
49 + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
50 + false);
51 + objectMapper.setSerializationInclusion(Include.NON_NULL);
52 + return objectMapper;
53 + }
54 +
55 + /**
56 + * get ObjectMapper entity.
57 + * @param flag configure
58 + * @return ObjectMapper entity
59 + */
60 + public static ObjectMapper getObjectMapper(boolean flag) {
61 + ObjectMapper objectMapper = new ObjectMapper();
62 + objectMapper
63 + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
64 + flag);
65 + return objectMapper;
66 + }
67 +
68 + /**
69 + * get ObjectMapper entity.
70 + * @param flag configure
71 + * @param incl setSerializationInclusion
72 + * @return ObjectMapper entity
73 + */
74 + public static ObjectMapper getObjectMapper(boolean flag, Include incl) {
75 + ObjectMapper objectMapper = new ObjectMapper();
76 + objectMapper
77 + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
78 + flag);
79 + objectMapper.setSerializationInclusion(incl);
80 + return objectMapper;
81 + }
82 +
83 + /**
84 + * convert Object into String.
85 + * @param obj Object
86 + * @return String
87 + */
88 + public static String convertToString(Object obj) {
89 + ObjectMapper objectMapper = new ObjectMapper();
90 + try {
91 + return objectMapper.writeValueAsString(obj);
92 + } catch (JsonProcessingException e) {
93 + log.error("JsonProcessingException while converting Entity into string", e);
94 + }
95 + return null;
96 + }
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 java.util.Map;
19 +import java.util.Set;
20 +
21 +import org.onosproject.ovsdb.rfc.notation.OvsdbMap;
22 +import org.onosproject.ovsdb.rfc.notation.OvsdbSet;
23 +import org.onosproject.ovsdb.rfc.notation.RefTableRow;
24 +import org.onosproject.ovsdb.rfc.notation.UUID;
25 +import org.onosproject.ovsdb.rfc.schema.type.AtomicColumnType;
26 +import org.onosproject.ovsdb.rfc.schema.type.BaseType;
27 +import org.onosproject.ovsdb.rfc.schema.type.BooleanBaseType;
28 +import org.onosproject.ovsdb.rfc.schema.type.ColumnType;
29 +import org.onosproject.ovsdb.rfc.schema.type.IntegerBaseType;
30 +import org.onosproject.ovsdb.rfc.schema.type.KeyValuedColumnType;
31 +import org.onosproject.ovsdb.rfc.schema.type.RealBaseType;
32 +import org.onosproject.ovsdb.rfc.schema.type.StringBaseType;
33 +import org.onosproject.ovsdb.rfc.schema.type.UuidBaseType;
34 +
35 +import com.fasterxml.jackson.databind.JsonNode;
36 +import com.google.common.collect.Maps;
37 +import com.google.common.collect.Sets;
38 +
39 +/**
40 + * Object value utility class.
41 + */
42 +public final class TransValueUtil {
43 +
44 + /**
45 + * Constructs a TransValueUtil object. Utility classes should not have a
46 + * public or default constructor, otherwise it will compile failed.
47 + * This class should not be instantiated.
48 + */
49 + private TransValueUtil() {
50 + }
51 +
52 + /**
53 + * if the type is Set, convert into OvsdbSet, if Map, convert into OvsdbMap.
54 + * @param value Object
55 + * @return Object
56 + */
57 + public static Object getFormatData(Object value) {
58 + if (value instanceof Map) {
59 + return OvsdbMap.ovsdbMap((Map) value);
60 + } else if (value instanceof Set) {
61 + return OvsdbSet.ovsdbSet((Set) value);
62 + } else {
63 + return value;
64 + }
65 + }
66 +
67 + /**
68 + * Transform JsonNode to corresponding value.
69 + * @param json the ColumnType JsonNode
70 + * @param columnType AtomicColumnType or KeyValuedColumnType
71 + * @return Object OvsdbMap or OvsdbSet
72 + */
73 + public static Object getValueFromJson(JsonNode json, ColumnType columnType) {
74 + if (columnType instanceof AtomicColumnType) {
75 + AtomicColumnType atoType = (AtomicColumnType) columnType;
76 + return getValueFromAtoType(json, atoType);
77 + } else if (columnType instanceof KeyValuedColumnType) {
78 + KeyValuedColumnType kvType = (KeyValuedColumnType) columnType;
79 + return getValueFromKvType(json, kvType);
80 + }
81 + return null;
82 + }
83 +
84 + // Convert AtomicColumnType JsonNode into OvsdbSet value
85 + private static Object getValueFromAtoType(JsonNode json,
86 + AtomicColumnType atoType) {
87 + BaseType baseType = atoType.baseType();
88 + // If "min" or "max" is not specified, If "min" is not 1 or "max" is not
89 + // 1,
90 + // or both, and "value" is not specified, the type is a set of scalar
91 + // type "key".
92 + // Refer to RFC 7047, Section 3.2 <type>.
93 + if (atoType.min() != atoType.max()) {
94 + Set set = Sets.newHashSet();
95 + if (json.isArray()) {
96 + if (json.size() == 2) {
97 + if (json.get(0).isTextual()
98 + && "set".equals(json.get(0).asText())) {
99 + for (JsonNode node : json.get(1)) {
100 + set.add(TransValueUtil.transToValue(node, baseType));
101 + }
102 + } else {
103 + set.add(TransValueUtil.transToValue(json, baseType));
104 + }
105 + }
106 + } else {
107 + set.add(TransValueUtil.transToValue(json, baseType));
108 + }
109 + return OvsdbSet.ovsdbSet(set);
110 + } else {
111 + return TransValueUtil.transToValue(json, baseType);
112 + }
113 + }
114 +
115 + // Convert KeyValuedColumnType JsonNode into OvsdbMap value
116 + private static Object getValueFromKvType(JsonNode json,
117 + KeyValuedColumnType kvType) {
118 + if (json.isArray()) {
119 + if (json.size() == 2) {
120 + if (json.get(0).isTextual()
121 + && "map".equals(json.get(0).asText())) {
122 + Map map = Maps.newHashMap();
123 + for (JsonNode pairNode : json.get(1)) {
124 + if (pairNode.isArray() && json.size() == 2) {
125 + Object key = TransValueUtil.transToValue(pairNode
126 + .get(0), kvType.keyType());
127 + Object value = TransValueUtil.transToValue(pairNode
128 + .get(1), kvType.valueType());
129 + map.put(key, value);
130 + }
131 + }
132 + return OvsdbMap.ovsdbMap(map);
133 + }
134 + }
135 + }
136 + return null;
137 + }
138 +
139 + /**
140 + * convert into value.
141 + * @param valueNode the BaseType JsonNode
142 + * @param baseType BooleanBaseType or IntegerBaseType or RealBaseType or
143 + * StringBaseType or UuidBaseType
144 + * @return Object the value of JsonNode
145 + */
146 + public static Object transToValue(JsonNode valueNode, BaseType baseType) {
147 + if (baseType instanceof BooleanBaseType) {
148 + return valueNode.asBoolean();
149 + } else if (baseType instanceof IntegerBaseType) {
150 + return valueNode.asInt();
151 + } else if (baseType instanceof RealBaseType) {
152 + return valueNode.asDouble();
153 + } else if (baseType instanceof StringBaseType) {
154 + return valueNode.asText();
155 + } else if (baseType instanceof UuidBaseType) {
156 + if (valueNode.isArray()) {
157 + if (valueNode.size() == 2) {
158 + if (valueNode.get(0).isTextual()
159 + && "uuid".equals(valueNode.get(0).asText())
160 + || "named-uuid".equals(valueNode.get(0).asText())) {
161 + return UUID.uuid(valueNode.get(1).asText());
162 + }
163 + }
164 + } else {
165 + return new RefTableRow(((UuidBaseType) baseType).getRefTable(),
166 + valueNode);
167 + }
168 + }
169 + return null;
170 + }
171 +}