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 4707 additions and 0 deletions
......@@ -39,5 +39,6 @@
<modules>
<module>api</module>
<module>rfc</module>
</modules>
</project>
......
<?xml version="1.0" encoding="UTF-8"?>
<!-- ~ Copyright 2014 Open Networking Laboratory ~ ~ Licensed under the Apache
License, Version 2.0 (the "License"); ~ you may not use this file except
in compliance with the License. ~ You may obtain a copy of the License at
~ ~ http://www.apache.org/licenses/LICENSE-2.0 ~ ~ Unless required by applicable
law or agreed to in writing, software ~ distributed under the License is
distributed on an "AS IS" BASIS, ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. ~ See the License for the specific language
governing permissions and ~ limitations under the License. -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.onosproject</groupId>
<artifactId>onos-ovsdb</artifactId>
<version>1.3.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>onos-ovsdb-rfc</artifactId>
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.error;
/**
* AbnormalSchema exception is thrown when the received schema is invalid.
*/
public class AbnormalSchemaException extends RuntimeException {
private static final long serialVersionUID = 8328377718334680368L;
/**
* Constructs a AbnormalSchemaException object.
* @param message error message
*/
public AbnormalSchemaException(String message) {
super(message);
}
/**
* Constructs a AbnormalSchemaException object.
* @param message error message
* @param cause Throwable
*/
public AbnormalSchemaException(String message, Throwable cause) {
super(message, cause);
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.error;
/**
* This exception is thrown when the argument is not supported.
*/
public class ArgumentException extends RuntimeException {
private static final long serialVersionUID = 4950089877540156797L;
/**
* Constructs a ArgumentException object.
* @param message error message
*/
public ArgumentException(String message) {
super(message);
}
/**
* Constructs a ArgumentException object.
* @param message error message
* @param cause Throwable
*/
public ArgumentException(String message, Throwable cause) {
super(message, cause);
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.error;
import static com.google.common.base.MoreObjects.toStringHelper;
/**
* This exception is thrown when a ColumnSchema cannot be found.
*/
public class ColumnSchemaNotFoundException extends RuntimeException {
private static final long serialVersionUID = -4325190659387339524L;
/**
* Constructs a ColumnSchemaNotFoundException object.
* @param message error message
*/
public ColumnSchemaNotFoundException(String message) {
super(message);
}
/**
* Constructs a ColumnSchemaNotFoundException object.
* @param message error message
* @param cause Throwable
*/
public ColumnSchemaNotFoundException(String message, Throwable cause) {
super(message, cause);
}
/**
* Create error message.
* @param columnName column name
* @param tableName table name
* @return message
*/
public static String createMessage(String columnName, String tableName) {
String message = toStringHelper("ColumnSchemaNotFoundException")
.addValue("Could not find ColumnSchema for " + columnName
+ " in " + tableName).toString();
return message;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.error;
/**
* The JsonParsingException is thrown when JSON could not be successfully
* parsed.
*/
public class JsonParsingException extends RuntimeException {
private static final long serialVersionUID = 1424752181911923235L;
/**
* Constructs a JsonParsingException object.
* @param message error message
*/
public JsonParsingException(String message) {
super(message);
}
/**
* Constructs a JsonParsingException object.
* @param message error message
* @param cause Throwable
*/
public JsonParsingException(String message, Throwable cause) {
super(message, cause);
}
/**
* Constructs a JsonParsingException object.
* @param cause Throwable
*/
public JsonParsingException(Throwable cause) {
super(cause);
}
/**
* Constructs a JsonParsingException object.
* @param message error message
* @param cause Throwable
* @param enableSuppression enable Suppression
* @param writableStackTrace writable StackTrace
*/
public JsonParsingException(String message, Throwable cause,
boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.error;
import static com.google.common.base.MoreObjects.toStringHelper;
/**
* This exception is thrown when a TableSchema cannot be found.
*/
public class TableSchemaNotFoundException extends RuntimeException {
private static final long serialVersionUID = 8431894450061740838L;
/**
* Constructs a TableSchemaNotFoundException object.
* @param message error message
*/
public TableSchemaNotFoundException(String message) {
super(message);
}
/**
* Constructs a TableSchemaNotFoundException object.
* @param message error message
* @param cause Throwable
*/
public TableSchemaNotFoundException(String message, Throwable cause) {
super(message, cause);
}
/**
* Create error message.
* @param tableName table name
* @param schemaName database name
* @return message
*/
public static String createMessage(String tableName, String schemaName) {
String message = toStringHelper("TableSchemaNotFoundException")
.addValue("Can not find TableSchema for " + tableName + " in "
+ schemaName).toString();
return message;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.error;
/**
* This is a generic exception thrown by the Typed Schema utilities.
*/
public class TypedSchemaException extends RuntimeException {
private static final long serialVersionUID = -1452257990783176715L;
/**
* Constructs a TypedSchemaException object.
* @param message error message
*/
public TypedSchemaException(String message) {
super(message);
}
/**
* Constructs a TypedSchemaException object.
* @param message error message
* @param cause Throwable
*/
public TypedSchemaException(String message, Throwable cause) {
super(message, cause);
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.error;
/**
* This exception is thrown when a result does not meet any of the known formats
* in RFC7047.
*/
public class UnknownResultException extends RuntimeException {
private static final long serialVersionUID = 1377011546616825375L;
/**
* Constructs a UnknownResultException object.
* @param message error message
*/
public UnknownResultException(String message) {
super(message);
}
/**
* Constructs a UnknownResultException object.
* @param message error message
* @param cause Throwable
*/
public UnknownResultException(String message, Throwable cause) {
super(message, cause);
}
}
package org.onosproject.ovsdb.rfc.error;
/**
* This exception is thrown when the encoding does not meet UTF-8 in RFC7047.
*/
public class UnsupportedEncodingException extends RuntimeException {
private static final long serialVersionUID = -4865311369828520666L;
/**
* Constructs a UnsupportedEncodingException object.
* @param message error message
*/
public UnsupportedEncodingException(String message) {
super(message);
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.error;
import static com.google.common.base.MoreObjects.toStringHelper;
/**
* This exception is used when the a table or row is accessed though a typed
* interface and the version requirements are not met.
*/
public class VersionMismatchException extends RuntimeException {
private static final long serialVersionUID = -8439624321110133595L;
/**
* Constructs a VersionMismatchException object.
* @param message error message
*/
public VersionMismatchException(String message) {
super(message);
}
/**
* Constructs a VersionMismatchException object.
* @param message error message
* @param cause Throwable
*/
public VersionMismatchException(String message, Throwable cause) {
super(message, cause);
}
/**
* Create error message.
* @param actualVersion the actual version
* @param fromVersion the initial version
* @return message
*/
public static String createFromMessage(String actualVersion,
String fromVersion) {
String message = toStringHelper("VersionMismatchException")
.addValue("The fromVersion should less than the actualVersion.\n"
+ "fromVersion: "
+ fromVersion
+ ".\n"
+ "actualVersion: " + actualVersion)
.toString();
return message;
}
/**
* Create error message.
* @param actualVersion the actual version
* @param toVersion the end version
* @return message
*/
public static String createToMessage(String actualVersion, String toVersion) {
String message = toStringHelper("VersionMismatchException")
.addValue("The toVersion should greater than the required version.\n"
+ "toVersion: "
+ toVersion
+ ".\n"
+ "Actual Version: " + actualVersion)
.toString();
return message;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.message;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Objects;
import java.util.Set;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
/**
* Monitor Requst information that need to monitor table.
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public final class MonitorRequest {
@JsonIgnore
private final String tableName;
private final Set<String> columns;
private final MonitorSelect select;
/**
* Constructs a MonitorRequest object.
* @param tableName table name
* @param columns a set of column name
* @param select monitor action
*/
public MonitorRequest(String tableName, Set<String> columns,
MonitorSelect select) {
checkNotNull(tableName, "tableName is not null");
checkNotNull(columns, "columns is not null");
checkNotNull(select, "select is not null");
this.tableName = tableName;
this.columns = columns;
this.select = select;
}
/**
* Returns tableName.
* @return tableName
*/
public String getTableName() {
return tableName;
}
/**
* Returns select.
* @return select
*/
public MonitorSelect getSelect() {
return select;
}
/**
* Returns columns.
* @return columns
*/
public Set<String> getColumns() {
return columns;
}
@Override
public int hashCode() {
return Objects.hash(tableName, select, columns);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof MonitorRequest) {
final MonitorRequest other = (MonitorRequest) obj;
return Objects.equals(this.tableName, other.tableName)
&& Objects.equals(this.select, other.select)
&& Objects.equals(this.columns, other.columns);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("tableName", tableName)
.add("select", select).add("columns", columns).toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.message;
import static com.google.common.base.MoreObjects.toStringHelper;
import java.util.Objects;
/**
* The contents of this object specify how the columns or table are to be
* monitored.
*/
public final class MonitorSelect {
private final boolean initial;
private final boolean insert;
private final boolean delete;
private final boolean modify;
/**
* Constructs a MonitorSelect object.
* @param initial whether monitor the initial action
* @param insert whether monitor the insert action
* @param delete whether monitor the delete action
* @param modify whether monitor the modify action
*/
public MonitorSelect(boolean initial, boolean insert, boolean delete,
boolean modify) {
this.initial = initial;
this.insert = insert;
this.delete = delete;
this.modify = modify;
}
/**
* Returns initial.
* @return initial
*/
public boolean isInitial() {
return initial;
}
/**
* Returns insert.
* @return insert
*/
public boolean isInsert() {
return insert;
}
/**
* Returns delete.
* @return delete
*/
public boolean isDelete() {
return delete;
}
/**
* Returns modify.
* @return modify
*/
public boolean isModify() {
return modify;
}
@Override
public int hashCode() {
return Objects.hash(initial, insert, delete, modify);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof MonitorSelect) {
final MonitorSelect other = (MonitorSelect) obj;
return Objects.equals(this.initial, other.initial)
&& Objects.equals(this.insert, other.insert)
&& Objects.equals(this.delete, other.delete)
&& Objects.equals(this.modify, other.modify);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("initial", initial)
.add("insert", insert).add("delete", delete)
.add("modify", modify).toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.message;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.List;
import org.onosproject.ovsdb.rfc.notation.Row;
import org.onosproject.ovsdb.rfc.notation.UUID;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* All results of ovs table operations. refer to RFC7047 5.2.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public final class OperationResult {
private int count;
@JsonIgnore
private UUID uuid;
private List<Row> rows;
private String error;
private String details;
/**
* Constructs a OperationResult object. When JsonNode is converted into
* OperationResult, need this constructor and also need setter method.
*/
public OperationResult() {
}
/**
* Constructs a OperationResult object.
* @param rows List of Row entity
*/
public OperationResult(List<Row> rows) {
checkNotNull(rows, "rows is not null");
this.rows = rows;
}
/**
* Constructs a OperationResult object.
* @param count the count node of result
* @param uuid UUID entity
* @param rows List of Row entity
* @param error error message
* @param details details of error message
*/
public OperationResult(int count, UUID uuid, List<Row> rows, String error,
String details) {
checkNotNull(uuid, "uuid is not null");
checkNotNull(rows, "rows is not null");
checkNotNull(error, "error is not null");
checkNotNull(details, "details is not null");
this.count = count;
this.uuid = uuid;
this.rows = rows;
this.error = error;
this.details = details;
}
/**
* Return count.
* @return count
*/
public int getCount() {
return count;
}
/**
* Set count value.
* @param count the Operation message of count
*/
public void setCount(int count) {
this.count = count;
}
/**
* Return uuid.
* @return uuid
*/
@JsonProperty("uuid")
public UUID getUuid() {
return uuid;
}
/**
* Set uuid value.
* @param uuid the Operation message of uuid
*/
public void setUuid(String uuid) {
checkNotNull(uuid, "uuid is not null");
this.uuid = UUID.uuid(uuid);
}
/**
* Return rows.
* @return List of Row
*/
public List<Row> getRows() {
return rows;
}
/**
* Set rows value.
* @param rows the Operation message of rows
*/
public void setRows(List<Row> rows) {
checkNotNull(rows, "rows is not null");
this.rows = rows;
}
/**
* Return error.
* @return error
*/
public String getError() {
return error;
}
/**
* Set error value.
* @param error the Operation message of error
*/
public void setError(String error) {
checkNotNull(error, "error is not null");
this.error = error;
}
/**
* Return details.
* @return details
*/
public String getDetails() {
return details;
}
/**
* Set details value.
* @param details the Operation message of details
*/
public void setDetails(String details) {
checkNotNull(details, "details is not null");
this.details = details;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.message;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Objects;
import org.onosproject.ovsdb.rfc.notation.Row;
import org.onosproject.ovsdb.rfc.notation.UUID;
/**
* A TableUpdate is an object that maps from the row's UUID to a RowUpdate object.
* A RowUpdate is an object with the following members: "old": row, "new": row.
* Refer to RFC 7047 Section 4.1.6.
*/
public final class RowUpdate {
private final UUID uuid;
private final Row oldRow;
private final Row newRow;
/**
* Constructs a RowUpdate object.
* @param uuid UUID
* @param oldRow present for "delete" and "modify" updates
* @param newRow present for "initial", "insert", and "modify" updates
*/
public RowUpdate(UUID uuid, Row oldRow, Row newRow) {
checkNotNull(uuid, "uuid is not null");
this.uuid = uuid;
this.oldRow = oldRow;
this.newRow = newRow;
}
/**
* Return uuid.
* @return uuid
*/
public UUID uuid() {
return this.uuid;
}
/**
* Return oldRow.
* @return oldRow
*/
public Row oldRow() {
return oldRow;
}
/**
* Return newRow.
* @return newRow
*/
public Row newRow() {
return newRow;
}
@Override
public int hashCode() {
return Objects.hash(uuid, oldRow, newRow);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof RowUpdate) {
final RowUpdate other = (RowUpdate) obj;
return Objects.equals(this.uuid, other.uuid)
&& Objects.equals(this.oldRow, other.oldRow)
&& Objects.equals(this.newRow, other.newRow);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("uuid", uuid).add("oldRow", oldRow)
.add("newRow", newRow).toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.message;
import static com.google.common.base.MoreObjects.toStringHelper;
import java.util.Map;
import java.util.Objects;
import org.onosproject.ovsdb.rfc.notation.Row;
import org.onosproject.ovsdb.rfc.notation.UUID;
/**
* TableUpdate is an object that maps from the row's UUID to a RowUpdate object.
*/
public final class TableUpdate {
private final Map<UUID, RowUpdate> rows;
/**
* Constructs a TableUpdate object.
* @param rows the parameter of TableUpdate entity
*/
private TableUpdate(Map<UUID, RowUpdate> rows) {
this.rows = rows;
}
/**
* Get TableUpdate entity.
* @param rows the parameter of TableUpdate entity
* @return TableUpdate entity
*/
public static TableUpdate tableUpdate(Map<UUID, RowUpdate> rows) {
return new TableUpdate(rows);
}
/**
* Return old row.
* @param uuid the key of rows
* @return Row old row
*/
public Row getOld(UUID uuid) {
RowUpdate rowUpdate = rows.get(uuid);
if (rowUpdate == null) {
return null;
}
return rowUpdate.oldRow();
}
/**
* Return new row.
* @param uuid the key of rows
* @return Row new row
*/
public Row getNew(UUID uuid) {
RowUpdate rowUpdate = rows.get(uuid);
if (rowUpdate == null) {
return null;
}
return rowUpdate.newRow();
}
/**
* Return rows.
* @return rows
*/
public Map<UUID, RowUpdate> rows() {
return rows;
}
@Override
public int hashCode() {
return Objects.hash(rows);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof TableUpdate) {
final TableUpdate other = (TableUpdate) obj;
return Objects.equals(this.rows, other.rows);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("rows", rows).toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.message;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
import java.util.Objects;
import org.onosproject.ovsdb.rfc.schema.TableSchema;
/**
* TableUpdates is an object that maps from a table name to a TableUpdate.
*/
public final class TableUpdates {
private final Map<String, TableUpdate> result;
/**
* Constructs a TableUpdates object.
* @param result the parameter of TableUpdates entity
*/
private TableUpdates(Map<String, TableUpdate> result) {
this.result = result;
}
/**
* Get TableUpdates.
* @param result the parameter of TableUpdates entity
* @return TableUpdates
*/
public static TableUpdates tableUpdates(Map<String, TableUpdate> result) {
checkNotNull(result, "result is not null");
return new TableUpdates(result);
}
/**
* Return TableUpdate.
* @param table the TableSchema of TableUpdates
* @return TableUpdate
*/
public TableUpdate tableUpdate(TableSchema table) {
return this.result.get(table.name());
}
/**
* Return the map of TableUpdate.
* @return result
*/
public Map<String, TableUpdate> result() {
return result;
}
@Override
public int hashCode() {
return Objects.hash(result);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof TableUpdates) {
final TableUpdates other = (TableUpdates) obj;
return Objects.equals(this.result, other.result);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("result", result).toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.message;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Objects;
import org.onosproject.ovsdb.rfc.notation.json.UpdateNotificationConverter;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
/**
* The "update" notification is sent by the server to the client to report
* changes in tables that are being monitored following a "monitor" request. The
* "params" of the result JsonNode.
*/
@JsonDeserialize(converter = UpdateNotificationConverter.class)
public final class UpdateNotification {
private final Object context;
private final JsonNode tbUpdatesJsonNode;
/**
* Constructs a UpdateNotification object.
* @param context the "json-value" in "params" of the result JsonNode
* @param tbUpdatesJsonNode the "table-updates" in "params" of the result JsonNode
*/
public UpdateNotification(Object context, JsonNode tbUpdatesJsonNode) {
checkNotNull(context, "context is not null");
checkNotNull(tbUpdatesJsonNode, "tbUpdatesJsonNode is not null");
this.context = context;
this.tbUpdatesJsonNode = tbUpdatesJsonNode;
}
/**
* Return context.
* @return context
*/
public Object context() {
return context;
}
/**
* Return tbUpdatesJsonNode.
* @return tbUpdatesJsonNode
*/
public JsonNode tbUpdatesJsonNode() {
return tbUpdatesJsonNode;
}
@Override
public int hashCode() {
return Objects.hash(context, tbUpdatesJsonNode);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof UpdateNotification) {
final UpdateNotification other = (UpdateNotification) obj;
return Objects.equals(this.context, other.context)
&& Objects.equals(this.tbUpdatesJsonNode,
other.tbUpdatesJsonNode);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("context", context)
.add("tbUpdatesJsonNode", tbUpdatesJsonNode).toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.notation;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Objects;
import org.onosproject.ovsdb.rfc.schema.ColumnSchema;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* Column is the basic element of the OpenVswitch database.
*/
public final class Column {
@JsonIgnore
private final ColumnSchema schema;
private final Object data;
/**
* Column constructor.
* @param schema the column schema
* @param obj the data of the column
*/
public Column(ColumnSchema schema, Object obj) {
checkNotNull(schema, "schema is not null");
checkNotNull(obj, "data is not null");
this.schema = schema;
this.data = obj;
}
/**
* Returns column data.
* @return column data
*/
public Object data() {
return data;
}
/**
* Returns ColumnSchema.
* @return ColumnSchema
*/
public ColumnSchema schema() {
return schema;
}
@Override
public int hashCode() {
return Objects.hash(schema, data);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof Column) {
final Column other = (Column) obj;
return Objects.equals(this.schema, other.schema)
&& Objects.equals(this.data, other.data);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("schema", schema).add("data", data)
.toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.notation;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Objects;
import org.onosproject.ovsdb.rfc.notation.json.ConditionSerializer;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
/**
* Condition is a 3-element JSON array of the form [column, function, value]
* that represents a test on a column value.
*/
@JsonSerialize(using = ConditionSerializer.class)
public final class Condition {
/**
* Function of Notation. Refer to RFC 7047 Section 5.1.
*/
public enum Function {
LESS_THAN("<"), LESS_THAN_OR_EQUALS("<="), EQUALS("=="),
NOT_EQUALS("!="), GREATER_THAN(">"), GREATER_THAN_OR_EQUALS(">="),
INCLUDES("includes"), EXCLUDES("excludes");
private final String function;
private Function(String function) {
this.function = function;
}
/**
* Returns the function for Function.
* @return the function
*/
public String function() {
return function;
}
}
private final String column;
private final Function function;
private final Object value;
/**
* Constructs a Condition object.
* @param column the column name
* @param function Function
* @param value column data
*/
public Condition(String column, Function function, Object value) {
checkNotNull(column, "column is not null");
checkNotNull(function, "function is not null");
checkNotNull(value, "value is not null");
this.column = column;
this.function = function;
this.value = value;
}
/**
* Returns column name.
* @return column name
*/
public String getColumn() {
return column;
}
/**
* Returns Function.
* @return Function
*/
public Function getFunction() {
return function;
}
/**
* Returns column data.
* @return column data
*/
public Object getValue() {
return value;
}
@Override
public int hashCode() {
return Objects.hash(column, function, value);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof Condition) {
final Condition other = (Condition) obj;
return Objects.equals(this.column, other.column)
&& Objects.equals(this.function, other.function)
&& Objects.equals(this.value, other.value);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("column", column)
.add("function", function).add("value", value).toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.notation;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Objects;
import org.onosproject.ovsdb.rfc.notation.json.MutationSerializer;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
/**
* Mutation is s 3-element JSON array of the form [column, mutator, value] that
* represents a change to a column value.
*/
@JsonSerialize(using = MutationSerializer.class)
public final class Mutation {
/**
* Mutator must be "+=", "-=", "*=", "/=", or (integer only) "%=". The value
* of column is changed to the sum, difference, product, quotient, or
* remainder, respectively, of column and value.
*/
public enum Mutator {
SUM("+="), DIFFERENCE("-="), PRODUCT("*="), QUOTIENT("/="),
REMAINDER("%="), INSERT("insert"), DELETE("delete");
private final String mutator;
private Mutator(String mutator) {
this.mutator = mutator;
}
/**
* Returns the mutator for Mutator.
* @return the mutator
*/
public String mutator() {
return mutator;
}
}
private final String column;
private final Mutator mutator;
private final Object value;
/**
* Mutation constructor.
* @param column the column name
* @param mutator Mutator
* @param value column data
*/
public Mutation(String column, Mutator mutator, Object value) {
checkNotNull(column, "column is not null");
checkNotNull(mutator, "mutator is not null");
checkNotNull(value, "value is not null");
this.column = column;
this.mutator = mutator;
this.value = value;
}
/**
* Returns column name.
* @return column name
*/
public String getColumn() {
return column;
}
/**
* Returns Mutator.
* @return Mutator
*/
public Mutator getMutator() {
return mutator;
}
/**
* Returns column data.
* @return column data
*/
public Object getValue() {
return value;
}
@Override
public int hashCode() {
return Objects.hash(column, mutator, value);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof Mutation) {
final Mutation other = (Mutation) obj;
return Objects.equals(this.column, other.column)
&& Objects.equals(this.mutator, other.mutator)
&& Objects.equals(this.value, other.value);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("column", column)
.add("mutator", mutator).add("value", value).toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.notation;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
import java.util.Objects;
import org.onosproject.ovsdb.rfc.notation.json.OvsdbMapSerializer;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
/**
* OvsdbMap is a 2-element JSON array that represents a database map value.
*/
@JsonSerialize(using = OvsdbMapSerializer.class)
public final class OvsdbMap {
private final Map map;
/**
* OvsdbMap constructor.
* @param map java.util.Map
*/
private OvsdbMap(Map map) {
checkNotNull(map, "map is not null");
this.map = map;
}
/**
* Returns map.
* @return map
*/
public Map map() {
return map;
}
/**
* convert Map into OvsdbMap.
* @param map java.util.Map
* @return OvsdbMap
*/
public static OvsdbMap ovsdbMap(Map map) {
return new OvsdbMap(map);
}
@Override
public int hashCode() {
return Objects.hash(map);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof OvsdbMap) {
final OvsdbMap other = (OvsdbMap) obj;
return Objects.equals(this.map, other.map);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("map", map).toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.notation;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Objects;
import java.util.Set;
import org.onosproject.ovsdb.rfc.notation.json.OvsdbSetSerializer;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
/**
* OvsdbSet is either an atom, representing a set with exactly one element, or
* a 2-element JSON array that represents a database set value.
*
*/
@JsonSerialize(using = OvsdbSetSerializer.class)
public final class OvsdbSet {
private final Set set;
/**
* OvsdbSet constructor.
* @param set java.util.Set
*/
private OvsdbSet(Set set) {
checkNotNull(set, "set is not null");
this.set = set;
}
/**
* Returns set.
* @return set
*/
public Set set() {
return set;
}
/**
* convert Set into OvsdbSet.
* @param set java.util.Set
* @return OvsdbSet
*/
public static OvsdbSet ovsdbSet(Set set) {
return new OvsdbSet(set);
}
@Override
public int hashCode() {
return Objects.hash(set);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof OvsdbSet) {
final OvsdbSet other = (OvsdbSet) obj;
return Objects.equals(this.set, other.set);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("set", set).toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.notation;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Objects;
import com.fasterxml.jackson.databind.JsonNode;
/**
* The RefTable type that can be expanded to Row. Refer to RFC 7047 Section 3.2.
*/
public final class RefTableRow {
private final String refTable;
private final JsonNode jsonNode;
/**
* RefTableRow constructor.
* @param refTable the refTable value of JsonNode
* @param jsonNode JsonNode
*/
public RefTableRow(String refTable, JsonNode jsonNode) {
checkNotNull(refTable, "refTable is not null");
checkNotNull(jsonNode, "jsonNode is not null");
this.refTable = refTable;
this.jsonNode = jsonNode;
}
/**
* Returns JsonNode.
* @return JsonNode
*/
public JsonNode jsonNode() {
return jsonNode;
}
/**
* Returns refTable.
* @return refTable
*/
public String refTable() {
return refTable;
}
@Override
public int hashCode() {
return Objects.hash(refTable, jsonNode);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof RefTableRow) {
final RefTableRow other = (RefTableRow) obj;
return Objects.equals(this.refTable, other.refTable)
&& Objects.equals(this.jsonNode, other.jsonNode);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("refTable", refTable)
.add("jsonNode", jsonNode).toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.notation;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.onosproject.ovsdb.rfc.schema.ColumnSchema;
import org.onosproject.ovsdb.rfc.schema.TableSchema;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Maps;
/**
* Row is the basic element of the OpenVswitch's table.
*/
public final class Row {
@JsonIgnore
private TableSchema tableSchema;
private Map<String, Column> columns;
/**
* Row constructor.
*/
public Row() {
this.columns = Maps.newHashMap();
}
/**
* Row constructor.
* @param tableSchema TableSchema entity
*/
public Row(TableSchema tableSchema) {
checkNotNull(tableSchema, "tableSchema is not null");
this.tableSchema = tableSchema;
this.columns = Maps.newHashMap();
}
/**
* Row constructor.
* @param tableSchema TableSchema entity
* @param columns List of Column entity
*/
public Row(TableSchema tableSchema, List<Column> columns) {
checkNotNull(tableSchema, "tableSchema is not null");
checkNotNull(columns, "columns is not null");
this.tableSchema = tableSchema;
this.columns = Maps.newHashMap();
for (Column column : columns) {
this.columns.put(column.schema().name(), column);
}
}
/**
* Returns tableSchema.
* @return tableSchema
*/
public TableSchema getTableSchema() {
return tableSchema;
}
/**
* Set tableSchema value.
* @param tableSchema TableSchema entity
*/
public void setTableSchema(TableSchema tableSchema) {
this.tableSchema = tableSchema;
}
/**
* Returns Column by ColumnSchema.
* @param schema ColumnSchema entity
* @return Column
*/
public Column getColumn(ColumnSchema schema) {
return (Column) columns.get(schema.name());
}
/**
* Returns Collection of Column.
* @return Collection of Column
*/
public Collection<Column> getColumns() {
return columns.values();
}
/**
* add Column.
* @param columnName column name
* @param data Column entity
*/
public void addColumn(String columnName, Column data) {
this.columns.put(columnName, data);
}
@Override
public int hashCode() {
return Objects.hash(tableSchema, columns);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof Row) {
final Row other = (Row) obj;
return Objects.equals(this.tableSchema, other.tableSchema)
&& Objects.equals(this.columns, other.columns);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("tableSchema", tableSchema).add("columns", columns).toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.notation;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Objects;
import org.onosproject.ovsdb.rfc.notation.json.UUIDSerializer;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
/**
* Handles both uuid and named-uuid.
*/
@JsonSerialize(using = UUIDSerializer.class)
public final class UUID {
private final String value;
/**
* UUID constructor.
* @param value UUID value
*/
private UUID(String value) {
checkNotNull(value, "value is not null");
this.value = value;
}
/**
* Get UUID.
* @param value UUID value
* @return UUID
*/
public static UUID uuid(String value) {
return new UUID(value);
}
/**
* Returns value.
* @return value
*/
public String value() {
return value;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof UUID) {
final UUID other = (UUID) obj;
return Objects.equals(this.value, other.value);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("value", value).toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.notation.json;
import java.io.IOException;
import org.onosproject.ovsdb.rfc.notation.Condition;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
/**
* Condition Serializer.
*/
public class ConditionSerializer extends JsonSerializer<Condition> {
@Override
public void serialize(Condition condition, JsonGenerator generator,
SerializerProvider provider)
throws IOException, JsonProcessingException {
generator.writeStartArray();
generator.writeString(condition.getColumn());
generator.writeString(condition.getFunction().function());
generator.writeObject(condition.getValue());
generator.writeEndArray();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.notation.json;
import java.io.IOException;
import org.onosproject.ovsdb.rfc.notation.Mutation;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
/**
* Mutation Serializer.
*/
public class MutationSerializer extends JsonSerializer<Mutation> {
@Override
public void serialize(Mutation condition, JsonGenerator generator,
SerializerProvider provider)
throws IOException, JsonProcessingException {
generator.writeStartArray();
generator.writeString(condition.getColumn());
generator.writeString(condition.getMutator().mutator());
generator.writeObject(condition.getValue());
generator.writeEndArray();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.notation.json;
import java.io.IOException;
import java.util.Map;
import org.onosproject.ovsdb.rfc.notation.OvsdbMap;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
/**
* OvsdbMap Serializer.
*/
public class OvsdbMapSerializer extends JsonSerializer<OvsdbMap> {
@Override
public void serialize(OvsdbMap map, JsonGenerator generator,
SerializerProvider provider)
throws IOException, JsonProcessingException {
generator.writeStartArray();
generator.writeString("map");
generator.writeStartArray();
Map javaMap = map.map();
for (Object key : javaMap.keySet()) {
generator.writeStartArray();
generator.writeObject(key);
generator.writeObject(javaMap.get(key));
generator.writeEndArray();
}
generator.writeEndArray();
generator.writeEndArray();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.notation.json;
import java.io.IOException;
import java.util.Set;
import org.onosproject.ovsdb.rfc.notation.OvsdbSet;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
/**
* OvsdbSet Serializer.
*/
public class OvsdbSetSerializer extends JsonSerializer<OvsdbSet> {
@Override
public void serialize(OvsdbSet set, JsonGenerator generator,
SerializerProvider provider)
throws IOException, JsonProcessingException {
generator.writeStartArray();
generator.writeString("set");
generator.writeStartArray();
Set javaSet = set.set();
for (Object key : javaSet) {
generator.writeObject(key);
}
generator.writeEndArray();
generator.writeEndArray();
}
}
\ No newline at end of file
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.notation.json;
import java.io.IOException;
import org.onosproject.ovsdb.rfc.notation.UUID;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
/**
* UUID Serializer.
*/
public class UUIDSerializer extends JsonSerializer<UUID> {
@Override
public void serialize(UUID value, JsonGenerator generator,
SerializerProvider provider)
throws IOException, JsonProcessingException {
generator.writeStartArray();
try {
java.util.UUID.fromString(value.value());
generator.writeString("uuid");
} catch (IllegalArgumentException ex) {
generator.writeString("named-uuid");
}
generator.writeString(value.value());
generator.writeEndArray();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.notation.json;
import org.onosproject.ovsdb.rfc.message.UpdateNotification;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.util.StdConverter;
/**
* UpdateNotificationDeser Converter.
*/
public class UpdateNotificationConverter
extends StdConverter<JsonNode, UpdateNotification> {
@Override
public UpdateNotification convert(JsonNode value) {
return deserialize(value);
}
/**
* JsonNode convert into UpdateNotification.
* @param node the "params" node of UpdateNotification JsonNode
*/
private UpdateNotification deserialize(JsonNode node) {
if (node.isArray()) {
if (node.size() == 2) {
return new UpdateNotification(node.get(0).asText(), node.get(1));
}
}
return null;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.operations;
import org.onosproject.ovsdb.rfc.schema.TableSchema;
/**
* assert operation.Refer to RFC 7047 Section 5.2.
*/
public final class Abort implements Operation {
private final String op;
/**
* Constructs a Abort object.
*/
public Abort() {
this.op = Operations.ABORT.op();
}
@Override
public String getOp() {
return op;
}
@Override
public TableSchema getTableSchema() {
return null;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.operations;
import static com.google.common.base.Preconditions.checkNotNull;
import org.onosproject.ovsdb.rfc.schema.TableSchema;
/**
* assert operation.Refer to RFC 7047 Section 5.2.
*/
public final class Assert implements Operation {
private final String op;
private final String lock;
/**
* Constructs a Assert object.
* @param lock the lock member of assert operation
*/
public Assert(String lock) {
checkNotNull(lock, "lock is not null");
this.op = Operations.ASSERT.op();
this.lock = lock;
}
/**
* Returns the lock member of assert operation.
* @return the lock member of assert operation
*/
public String getLock() {
return lock;
}
@Override
public String getOp() {
return op;
}
@Override
public TableSchema getTableSchema() {
return null;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.operations;
import static com.google.common.base.Preconditions.checkNotNull;
import org.onosproject.ovsdb.rfc.schema.TableSchema;
/**
* comment operation.Refer to RFC 7047 Section 5.2.
*/
public final class Comment implements Operation {
private final String op;
private final String comment;
/**
* Constructs a Comment object.
* @param comment the comment member of comment operation
*/
public Comment(String comment) {
checkNotNull(comment, "comment is not null");
this.op = Operations.COMMENT.op();
this.comment = comment;
}
/**
* Returns the comment member of comment operation.
* @return the comment member of comment operation
*/
public String getComment() {
return comment;
}
@Override
public String getOp() {
return op;
}
@Override
public TableSchema getTableSchema() {
return null;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.operations;
import static com.google.common.base.Preconditions.checkNotNull;
import org.onosproject.ovsdb.rfc.schema.TableSchema;
/**
* commit operation.Refer to RFC 7047 Section 5.2.
*/
public final class Commit implements Operation {
private final String op;
private final Boolean durable;
/**
* Constructs a Commit object.
* @param durable the durable member of commit operation
*/
public Commit(Boolean durable) {
checkNotNull(durable, "durable is not null");
this.op = Operations.COMMIT.op();
this.durable = durable;
}
/**
* Returns the durable member of commit operation.
* @return the durable member of commit operation
*/
public Boolean isDurable() {
return durable;
}
@Override
public String getOp() {
return op;
}
@Override
public TableSchema getTableSchema() {
return null;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.operations;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.List;
import org.onosproject.ovsdb.rfc.notation.Condition;
import org.onosproject.ovsdb.rfc.schema.TableSchema;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* delete operation.Refer to RFC 7047 Section 5.2.
*/
public final class Delete implements Operation {
@JsonIgnore
private final TableSchema tableSchema;
private final String op;
private final List<Condition> where;
/**
* Constructs a Delete object.
* @param schema TableSchema entity
* @param where the List of Condition entity
*/
public Delete(TableSchema schema, List<Condition> where) {
checkNotNull(schema, "TableSchema is not null");
checkNotNull(where, "where is not null");
this.tableSchema = schema;
this.op = Operations.DELETE.op();
this.where = where;
}
/**
* Returns the where member of delete operation.
* @return the where member of delete operation
*/
public List<Condition> getWhere() {
return where;
}
@Override
public String getOp() {
return op;
}
@Override
public TableSchema getTableSchema() {
return tableSchema;
}
/**
* For the use of serialization.
* @return the table member of update operation
*/
@JsonProperty
public String getTable() {
return (tableSchema == null) ? null : tableSchema.name();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.operations;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Collection;
import java.util.Map;
import org.onosproject.ovsdb.rfc.notation.Column;
import org.onosproject.ovsdb.rfc.notation.Row;
import org.onosproject.ovsdb.rfc.schema.ColumnSchema;
import org.onosproject.ovsdb.rfc.schema.TableSchema;
import org.onosproject.ovsdb.rfc.utils.TransValueUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.Maps;
/**
* insert operation.Refer to RFC 7047 Section 5.2.
*/
public final class Insert implements Operation {
@JsonIgnore
private final TableSchema tableSchema;
private final String op;
@JsonProperty("uuid-name")
private final String uuidName;
private final Map<String, Object> row;
/**
* Constructs a Insert object.
* @param schema TableSchema entity
* @param uuidName uuid-name
* @param row Row entity
*/
public Insert(TableSchema schema, String uuidName, Row row) {
checkNotNull(schema, "TableSchema is not null");
checkNotNull(uuidName, "uuidName is not null");
checkNotNull(row, "row is not null");
this.tableSchema = schema;
this.op = Operations.INSERT.op();
this.uuidName = uuidName;
this.row = Maps.newHashMap();
generateOperationRow(row);
}
/**
* Row entity convert into the row format of insert operation. Refer to RFC
* 7047 Section 5.2.
* @param row Row entity
*/
private void generateOperationRow(Row row) {
Collection<Column> columns = row.getColumns();
for (Column column : columns) {
ColumnSchema columnSchema = column.schema();
Object value = column.data();
Object untypedValue = TransValueUtil.getFormatData(value);
this.row.put(columnSchema.name(), untypedValue);
}
}
/**
* Returns the uuid-name member of insert operation.
* @return the uuid-name member of insert operation
*/
public String getUuidName() {
return uuidName;
}
/**
* Returns the row member of insert operation.
* @return the row member of insert operation
*/
public Map<String, Object> getRow() {
return row;
}
@Override
public String getOp() {
return op;
}
@Override
public TableSchema getTableSchema() {
return tableSchema;
}
/**
* For the use of serialization.
* @return the table member of update operation
*/
@JsonProperty
public String getTable() {
return (tableSchema == null) ? null : tableSchema.name();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.operations;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.List;
import org.onosproject.ovsdb.rfc.notation.Condition;
import org.onosproject.ovsdb.rfc.notation.Mutation;
import org.onosproject.ovsdb.rfc.schema.TableSchema;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* mutate operation.Refer to RFC 7047 Section 5.2.
*/
public final class Mutate implements Operation {
@JsonIgnore
private final TableSchema tableSchema;
private final String op;
private final List<Condition> where;
private final List<Mutation> mutations;
/**
* Constructs a Mutate object.
* @param schema TableSchema entity
* @param where the List of Condition entity
* @param mutations the List of Mutation entity
*/
public Mutate(TableSchema schema, List<Condition> where,
List<Mutation> mutations) {
checkNotNull(schema, "TableSchema is not null");
checkNotNull(mutations, "mutations is not null");
checkNotNull(where, "where is not null");
this.tableSchema = schema;
this.op = Operations.MUTATE.op();
this.where = where;
this.mutations = mutations;
}
/**
* Returns the mutations member of mutate operation.
* @return the mutations member of mutate operation
*/
public List<Mutation> getMutations() {
return mutations;
}
/**
* Returns the where member of mutate operation.
* @return the where member of mutate operation
*/
public List<Condition> getWhere() {
return where;
}
@Override
public String getOp() {
return op;
}
@Override
public TableSchema getTableSchema() {
return tableSchema;
}
/**
* For the use of serialization.
* @return the table member of update operation
*/
@JsonProperty
public String getTable() {
return (tableSchema == null) ? null : tableSchema.name();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.operations;
import org.onosproject.ovsdb.rfc.schema.TableSchema;
/**
* Operation interface.
*/
public interface Operation {
/**
* Returns the op member of update operation.
* @return the op member of update operation
*/
String getOp();
/**
* Returns TableSchema entity.
* @return TableSchema entity
*/
TableSchema getTableSchema();
/**
* Operations must be "insert", "select", "update", "mutate", "delete",
* "commit", "abort", "comment", "assert". Refer to RFC 7047 Section 5.2.
*/
public enum Operations {
INSERT("insert"), SELECT("select"), UPDATE("update"), MUTATE("mutate"),
DELETE("delete"), COMMIT("commit"), ABORT("abort"), COMMENT("comment"),
ASSERT("assert");
private String op;
private Operations(String op) {
this.op = op;
}
/**
* Returns the op for Operations.
* @return the op
*/
public String op() {
return op;
}
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.operations;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.List;
import org.onosproject.ovsdb.rfc.notation.Condition;
import org.onosproject.ovsdb.rfc.schema.TableSchema;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* select operation.Refer to RFC 7047 Section 5.2.
*/
public final class Select implements Operation {
@JsonIgnore
private final TableSchema tableSchema;
private final String op;
private final List<Condition> where;
private final List<String> columns;
/**
* Constructs a Select object.
* @param schema TableSchema entity
* @param where the List of Condition entity
* @param columns the List of column name
*/
public Select(TableSchema schema, List<Condition> where, List<String> columns) {
checkNotNull(schema, "TableSchema is not null");
checkNotNull(where, "where is not null");
checkNotNull(columns, "columns is not null");
this.tableSchema = schema;
this.op = Operations.SELECT.op();
this.where = where;
this.columns = columns;
}
/**
* Returns the columns member of select operation.
* @return the columns member of select operation
*/
public List<String> getColumns() {
return columns;
}
/**
* Returns the where member of select operation.
* @return the where member of select operation
*/
public List<Condition> getWhere() {
return where;
}
@Override
public String getOp() {
return op;
}
@Override
public TableSchema getTableSchema() {
return tableSchema;
}
/**
* For the use of serialization.
* @return the table member of update operation
*/
@JsonProperty
public String getTable() {
return (tableSchema == null) ? null : tableSchema.name();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.operations;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.onosproject.ovsdb.rfc.notation.Column;
import org.onosproject.ovsdb.rfc.notation.Condition;
import org.onosproject.ovsdb.rfc.notation.Row;
import org.onosproject.ovsdb.rfc.schema.ColumnSchema;
import org.onosproject.ovsdb.rfc.schema.TableSchema;
import org.onosproject.ovsdb.rfc.utils.TransValueUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.Maps;
/**
* update operation.Refer to RFC 7047 Section 5.2.
*/
public final class Update implements Operation {
@JsonIgnore
private final TableSchema tableSchema;
private final String op;
private final Map<String, Object> row;
private final List<Condition> where;
/**
* Constructs a Update object.
* @param schema TableSchema entity
* @param row Row entity
* @param where the List of Condition entity
*/
public Update(TableSchema schema, Row row, List<Condition> where) {
checkNotNull(schema, "TableSchema is not null");
checkNotNull(row, "row is not null");
checkNotNull(where, "where is not null");
this.tableSchema = schema;
this.op = Operations.UPDATE.op();
this.row = Maps.newHashMap();
this.where = where;
generateOperationRow(row);
}
/**
* Row entity convert into the row format of update operation. Refer to RFC
* 7047 Section 5.2.
* @param row Row entity
*/
private void generateOperationRow(Row row) {
Collection<Column> columns = row.getColumns();
for (Column column : columns) {
ColumnSchema columnSchema = column.schema();
Object value = column.data();
Object untypedValue = TransValueUtil.getFormatData(value);
this.row.put(columnSchema.name(), untypedValue);
}
}
/**
* Returns the row member of update operation.
* @return the row member of update operation
*/
public Map<String, Object> getRow() {
return row;
}
/**
* Returns the where member of update operation.
* @return the where member of update operation
*/
public List<Condition> getWhere() {
return where;
}
@Override
public String getOp() {
return op;
}
@Override
public TableSchema getTableSchema() {
return tableSchema;
}
/**
* For the use of serialization.
* @return the table member of update operation
*/
@JsonProperty
public String getTable() {
return (tableSchema == null) ? null : tableSchema.name();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.schema;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Objects;
import org.onosproject.ovsdb.rfc.schema.type.ColumnType;
/**
* A schema for the column represented by column-schema.
*/
public final class ColumnSchema {
private final String name;
private final ColumnType type;
/**
* Constructs a ColumnSchema object.
* @param name the column name
* @param columnType the column type
*/
public ColumnSchema(String name, ColumnType columnType) {
checkNotNull(name, "name is not null");
checkNotNull(columnType, "columnType is not null");
this.name = name;
this.type = columnType;
}
/**
* Returns the name of column.
* @return the name of column
*/
public String name() {
return name;
}
/**
* Returns the type of column.
* @return the type of column
*/
public ColumnType type() {
return type;
}
@Override
public int hashCode() {
return Objects.hash(name, type);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof ColumnSchema) {
final ColumnSchema other = (ColumnSchema) obj;
return Objects.equals(this.name, other.name)
&& Objects.equals(this.type, other.type);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("name", name).add("type", type)
.toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.schema;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
/**
* A schema for the database represented by database-schema, which consists of
* a set of tables.
*/
public final class DatabaseSchema {
private final String name;
private final String version;
private final Map<String, TableSchema> tableSchemas;
/**
* Constructs a DatabaseSchema object.
* @param name the name of database
* @param version the version of database
* @param tableSchemas a map of TableSchema
*/
public DatabaseSchema(String name, String version,
Map<String, TableSchema> tableSchemas) {
checkNotNull(name, "name is not null");
checkNotNull(version, "version is not null");
checkNotNull(tableSchemas, "tableSchemas is not null");
this.name = name;
this.version = version;
this.tableSchemas = tableSchemas;
}
/**
* Returns the name of database.
* @return the name of database
*/
public String name() {
return name;
}
/**
* Returns the version of database.
* @return the version of database
*/
public String version() {
return version;
}
/**
* Returns a map of TableSchema.
* @return a map of TableSchema
*/
public Map<String, TableSchema> tableSchemas() {
return tableSchemas;
}
/**
* Returns a set of table name.
* @return a set of table name
*/
public Set<String> getTableNames() {
return this.tableSchemas.keySet();
}
/**
* Determine whether contain the table.
* @param tableName table name
* @return boolean
*/
public boolean hasTable(String tableName) {
return this.getTableNames().contains(tableName);
}
/**
* Returns the TableSchema whose name is the tableName.
* @param tableName table name
* @return TableSchema
*/
public TableSchema getTableSchema(String tableName) {
TableSchema table = tableSchemas.get(tableName);
return table;
}
/**
* generate initialization columns in each table namely _uuid and _version.
*/
public void generateInitializationColumns() {
for (TableSchema tableSchema : tableSchemas.values()) {
tableSchema.generateInitializationColumns();
}
}
@Override
public int hashCode() {
return Objects.hash(name, version, tableSchemas);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof DatabaseSchema) {
final DatabaseSchema other = (DatabaseSchema) obj;
return Objects.equals(this.name, other.name)
&& Objects.equals(this.version, other.version)
&& Objects.equals(this.tableSchemas, other.tableSchemas);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("name", name).add("version", version)
.add("tableSchemas", tableSchemas).toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.schema;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.onosproject.ovsdb.rfc.schema.type.AtomicColumnType;
import org.onosproject.ovsdb.rfc.schema.type.UuidBaseType;
/**
* A schema for the table represented by table-schema, which consists of a set
* of columns.
*/
public final class TableSchema {
private final String name;
private final Map<String, ColumnSchema> columnSchemas;
/**
* Constructs a TableSchema object.
* @param name the name of table
* @param columnSchemas a map of ColumnSchema
*/
public TableSchema(String name, Map<String, ColumnSchema> columnSchemas) {
checkNotNull(name, "name is not null");
checkNotNull(columnSchemas, "columnSchemas is not null");
this.name = name;
this.columnSchemas = columnSchemas;
}
/**
* Returns the name of table.
* @return the name of table
*/
public String name() {
return name;
}
/**
* Returns a map of ColumnSchema.
* @return a map of ColumnSchema
*/
public Map<String, ColumnSchema> columnSchemas() {
return this.columnSchemas;
}
/**
* Returns a set of column name.
* @return a set of column name
*/
public Set<String> getColumnNames() {
return this.columnSchemas.keySet();
}
/**
* Determine whether contain the column.
* @param columnName column name
* @return boolean
*/
public boolean hasColumn(String columnName) {
return this.getColumnNames().contains(columnName);
}
/**
* Returns the ColumnSchema whose name is the columnName.
* @param columnName column name
* @return ColumnSchema
*/
public ColumnSchema getColumnSchema(String columnName) {
return this.columnSchemas.get(columnName);
}
/**
* Refer to RFC 7047 Section 3.2. generate initialization columns in each
* table namely _uuid and _version.
*/
public void generateInitializationColumns() {
columnSchemas
.put("_uuid",
new ColumnSchema("_uuid",
new AtomicColumnType(new UuidBaseType())));
columnSchemas
.put("_version",
new ColumnSchema("_version",
new AtomicColumnType(new UuidBaseType())));
}
@Override
public int hashCode() {
return Objects.hash(name, columnSchemas);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof TableSchema) {
final TableSchema other = (TableSchema) obj;
return Objects.equals(this.name, other.name)
&& Objects.equals(this.columnSchemas, other.columnSchemas);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("name", name)
.add("columnSchemas", columnSchemas).toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.schema.type;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Objects;
/**
* The "atomic-type" specifies the type of data stored in this column. Refer
* to RFC 7047 Section 3.2.
*/
public final class AtomicColumnType implements ColumnType {
private final BaseType baseType;
private final int min;
private final int max;
/**
* Constructs a AtomicColumnType object.
* @param baseType BaseType entity
*/
public AtomicColumnType(BaseType baseType) {
checkNotNull(baseType, "BaseType is not null");
this.baseType = baseType;
this.min = 1;
this.max = 1;
}
/**
* Constructs a AtomicColumnType object.
* @param baseType BaseType entity
* @param min min constraint
* @param max max constraint
*/
public AtomicColumnType(BaseType baseType, int min, int max) {
checkNotNull(baseType, "BaseType is not null");
this.baseType = baseType;
this.min = min;
this.max = max;
}
/**
* Get baseType.
* @return baseType
*/
public BaseType baseType() {
return baseType;
}
/**
* Get min.
* @return min
*/
public int min() {
return min;
}
/**
* Get max.
* @return max
*/
public int max() {
return max;
}
@Override
public int hashCode() {
return Objects.hash(baseType, min, max);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof AtomicColumnType) {
final AtomicColumnType other = (AtomicColumnType) obj;
return Objects.equals(this.baseType, other.baseType)
&& Objects.equals(this.min, other.min)
&& Objects.equals(this.max, other.max);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("baseType", baseType).add("min", min)
.add("max", max).toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.schema.type;
/**
* One of the strings "integer", "real", "boolean", "string", or "uuid",
* representing the specified scalar type. Refer to RFC 7047 Section 3.2.
*/
public interface BaseType {
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.schema.type;
import java.util.Set;
import org.onosproject.ovsdb.rfc.error.TypedSchemaException;
import org.onosproject.ovsdb.rfc.schema.type.UuidBaseType.RefType;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.Sets;
/**
* BaseType Factory class.
*/
public final class BaseTypeFactory {
/**
* Constructs a BaseTypeFactory object.
* This class should not be instantiated.
*/
private BaseTypeFactory() {
}
/**
* Create a BaseType from the JsonNode.
* @param json the BaseType JsonNode
* @param keyorval the key node or value node
* @return BaseType
*/
public static BaseType getBaseTypeFromJson(JsonNode json, String keyorval) {
if (json.isValueNode()) {
String type = json.asText().trim();
return fromTypeStr(type);
} else {
if (!json.has(keyorval)) {
throw new TypedSchemaException("not a type");
}
return fromJsonNode(json.get(keyorval));
}
}
/**
* Get BaseType by the type value of JsonNode.
* @param type the type value of JsonNode
* @return BaseType
*/
private static BaseType fromTypeStr(String type) {
switch (type) {
case "boolean":
return new BooleanBaseType();
case "integer":
return new IntegerBaseType();
case "real":
return new RealBaseType();
case "string":
return new StringBaseType();
case "uuid":
return new UuidBaseType();
default:
return null;
}
}
/**
* json like "string" or json like {"type" : "string", "enum": ["set",
* ["access", "native-tagged"]]}" for key or value.
* @param type JsonNode
*/
private static BaseType fromJsonNode(JsonNode type) {
if (type.isTextual()) {
return fromTypeStr(type.asText());
} else if (type.isObject() && type.has("type")) {
String typeStr = type.get("type").asText();
switch (typeStr) {
case "boolean":
return new BooleanBaseType();
case "integer":
return getIntegerBaseType(type);
case "real":
return getRealBaseType(type);
case "string":
return getStringBaseType(type);
case "uuid":
return getUuidBaseType(type);
default:
return null;
}
}
return null;
}
/**
* Get IntegerBaseType by the type value of JsonNode which contains the
* constraints.
* @param type the type value of JsonNode
* @return IntegerBaseType
*/
private static IntegerBaseType getIntegerBaseType(JsonNode type) {
int min = Integer.MIN_VALUE;
int max = Integer.MAX_VALUE;
Set<Integer> enums = Sets.newHashSet();
JsonNode node = type.get("minInteger");
if (node != null) {
min = node.asInt();
}
node = type.get("maxInteger");
if (node != null) {
max = node.asInt();
}
if (node.has("enum")) {
JsonNode anEnum = node.get("enum").get(1);
for (JsonNode n : anEnum) {
enums.add(n.asInt());
}
}
return new IntegerBaseType(min, max, enums);
}
/**
* Get RealBaseType by the type value of JsonNode which contains the
* constraints.
* @param type the type value of JsonNode
* @return RealBaseType
*/
private static RealBaseType getRealBaseType(JsonNode type) {
double min = Double.MIN_VALUE;
double max = Double.MAX_VALUE;
Set<Double> enums = Sets.newHashSet();
JsonNode node = type.get("minReal");
if (node != null) {
min = node.asDouble();
}
node = type.get("maxReal");
if (node != null) {
max = node.asDouble();
}
if (node.has("enum")) {
JsonNode anEnum = node.get("enum").get(1);
for (JsonNode n : anEnum) {
enums.add(n.asDouble());
}
}
return new RealBaseType(min, max, enums);
}
/**
* Get StringBaseType by the type value of JsonNode which contains the
* constraints.
* @param type the type value of JsonNode
* @return StringBaseType
*/
private static StringBaseType getStringBaseType(JsonNode type) {
int minLength = Integer.MIN_VALUE;
int maxLength = Integer.MAX_VALUE;
Set<String> enums = Sets.newHashSet();
JsonNode node = type.get("minLength");
if (node != null) {
minLength = node.asInt();
}
node = type.get("maxLength");
if (node != null) {
maxLength = node.asInt();
}
if (node.has("enum")) {
JsonNode enumVal = node.get("enum");
if (enumVal.isArray()) {
JsonNode anEnum = enumVal.get(1);
for (JsonNode n : anEnum) {
enums.add(n.asText());
}
} else if (enumVal.isTextual()) {
enums.add(enumVal.asText());
}
}
return new StringBaseType(minLength, maxLength, enums);
}
/**
* Get UuidBaseType by the type value of JsonNode which contains the
* constraints.
* @param type the type value of JsonNode
* @return UuidBaseType
*/
private static UuidBaseType getUuidBaseType(JsonNode type) {
String refTable = null;
String refType = RefType.STRONG.refType();
JsonNode node = type.get("refTable");
if (node != null) {
refTable = node.asText();
}
node = type.get("refType");
if (node != null) {
refType = node.asText();
}
return new UuidBaseType(refTable, refType);
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.schema.type;
import static com.google.common.base.MoreObjects.toStringHelper;
/**
* One of the strings "integer", "real", "boolean", "string", or "uuid",
* representing the specified scalar type. Refer to RFC 7047 Section 3.2.
* Because BooleanBaseType has no constraint conditions, and in order to be
* consistent with other BaseType, so this class is empty except for the
* toString method.
*/
public final class BooleanBaseType implements BaseType {
@Override
public String toString() {
return toStringHelper(this).toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.schema.type;
/**
* The "type" specifies the type of data stored in this column. Refer to RFC
* 7047 Section 3.2.
*/
public interface ColumnType {
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.schema.type;
import org.onosproject.ovsdb.rfc.error.TypedSchemaException;
import org.onosproject.ovsdb.rfc.utils.ObjectMapperUtil;
import com.fasterxml.jackson.databind.JsonNode;
/**
* ColumnType Factory class.
*/
public final class ColumnTypeFactory {
/**
* Constructs a ColumnTypeFactory object.
* This class should not be instantiated.
*/
private ColumnTypeFactory() {
}
/**
* Those Json's key/value pairs.
*/
public enum Type {
KEY("key"), VALUE("value");
private final String type;
private Type(String type) {
this.type = type;
}
/**
* Returns the type for Type.
* @return the type
*/
public String type() {
return type;
}
}
/**
* JsonNode like "flow_tables":{"type":{"key":{"maxInteger":254,"minInteger":0,"type":
* "integer"},"min":0,"value":{"type":"uuid","refTable":"Flow_Table"},"max":
* "unlimited"}}.
* @param json the ColumnType JsonNode
* @return ColumnType
*/
public static ColumnType getColumnTypeFromJson(JsonNode json) {
if (json.isObject() && !json.has(Type.VALUE.type())) {
return createAtomicColumnType(json);
} else if (!json.isValueNode() && json.has(Type.VALUE.type())) {
return createKeyValuedColumnType(json);
}
throw new TypedSchemaException("could not find the right column type :"
+ ObjectMapperUtil.convertToString(json));
}
/**
* Create AtomicColumnType entity.
* @param json JsonNode
* @return AtomicColumnType entity
*/
private static AtomicColumnType createAtomicColumnType(JsonNode json) {
BaseType baseType = BaseTypeFactory
.getBaseTypeFromJson(json, Type.KEY.type());
int min = 1;
int max = 1;
JsonNode node = json.get("min");
if (node != null) {
min = node.asInt();
}
node = json.get("max");
if (node != null) {
if (node.isNumber()) {
max = node.asInt();
} else if (node.isTextual() && "unlimited".equals(node.asText())) {
max = Integer.MAX_VALUE;
}
}
return new AtomicColumnType(baseType, min, max);
}
/**
* Create KeyValuedColumnType entity.
* @param json JsonNode
* @return KeyValuedColumnType entity
*/
private static KeyValuedColumnType createKeyValuedColumnType(JsonNode json) {
BaseType keyType = BaseTypeFactory.getBaseTypeFromJson(json,
Type.KEY.type());
BaseType valueType = BaseTypeFactory
.getBaseTypeFromJson(json, Type.VALUE.type());
int min = 1;
int max = 1;
JsonNode node = json.get("min");
if (node != null) {
min = node.asInt();
}
node = json.get("max");
if (node != null) {
if (node.isNumber()) {
max = node.asInt();
} else if (node.isTextual() && "unlimited".equals(node.asText())) {
max = Integer.MAX_VALUE;
}
}
return new KeyValuedColumnType(keyType, valueType, min, max);
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.schema.type;
import static com.google.common.base.MoreObjects.toStringHelper;
import java.util.Objects;
import java.util.Set;
import com.google.common.collect.Sets;
/**
* One of the strings "integer", "real", "boolean", "string", or "uuid",
* representing the specified scalar type. Refer to RFC 7047 Section 3.2.
*/
public final class IntegerBaseType implements BaseType {
private final int min;
private final int max;
private final Set<Integer> enums;
/**
* Constructs a IntegerBaseType object.
*/
public IntegerBaseType() {
this.min = Integer.MIN_VALUE;
this.max = Integer.MAX_VALUE;
this.enums = Sets.newHashSet();
}
/**
* Constructs a IntegerBaseType object.
* @param min min constraint
* @param max max constraint
* @param enums enums constraint
*/
public IntegerBaseType(int min, int max, Set<Integer> enums) {
this.min = min;
this.max = max;
this.enums = enums;
}
/**
* Get min.
* @return min
*/
public int min() {
return min;
}
/**
* Get max.
* @return max
*/
public int max() {
return max;
}
/**
* Get enums.
* @return enums
*/
public Set<Integer> enums() {
return enums;
}
@Override
public int hashCode() {
return Objects.hash(min, max, enums);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof IntegerBaseType) {
final IntegerBaseType other = (IntegerBaseType) obj;
return Objects.equals(this.enums, other.enums)
&& Objects.equals(this.min, other.min)
&& Objects.equals(this.max, other.max);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("min", min).add("max", max)
.add("enums", enums).toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.schema.type;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Objects;
/**
* a JSON object that describes the type of a database column, with key and
* value. Refer to RFC 7047 Section 3.2.
*/
public final class KeyValuedColumnType implements ColumnType {
private final BaseType keyType;
private final BaseType valueType;
private final int min;
private final int max;
/**
* Constructs a KeyValuedColumnType object.
* @param keyType BaseType entity
* @param valueType BaseType entity
* @param min min constraint
* @param max max constraint
*/
public KeyValuedColumnType(BaseType keyType, BaseType valueType, int min,
int max) {
checkNotNull(keyType, "keyType is not null");
checkNotNull(valueType, "valueType is not null");
this.keyType = keyType;
this.valueType = valueType;
this.min = min;
this.max = max;
}
/**
* Get keyType.
* @return keyType
*/
public BaseType keyType() {
return keyType;
}
/**
* Get valueType.
* @return valueType
*/
public BaseType valueType() {
return valueType;
}
/**
* Get min.
* @return min
*/
public int min() {
return min;
}
/**
* Get max.
* @return max
*/
public int max() {
return max;
}
@Override
public int hashCode() {
return Objects.hash(keyType, valueType, min, max);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof KeyValuedColumnType) {
final KeyValuedColumnType other = (KeyValuedColumnType) obj;
return Objects.equals(this.keyType, other.keyType)
&& Objects.equals(this.valueType, other.valueType)
&& Objects.equals(this.min, other.min)
&& Objects.equals(this.max, other.max);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("keyType", keyType)
.add("valueType", valueType).add("min", min).add("max", max)
.toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.schema.type;
import static com.google.common.base.MoreObjects.toStringHelper;
import java.util.Objects;
import java.util.Set;
import com.google.common.collect.Sets;
/**
* One of the strings "integer", "real", "boolean", "string", or "uuid",
* representing the specified scalar type. Refer to RFC 7047 Section 3.2.
*/
public final class RealBaseType implements BaseType {
private final double min;
private final double max;
private final Set<Double> enums;
/**
* Constructs a RealBaseType object.
*/
public RealBaseType() {
this.min = Double.MIN_VALUE;
this.max = Double.MAX_VALUE;
this.enums = Sets.newHashSet();
}
/**
* Constructs a RealBaseType object.
* @param min min constraint
* @param max max constraint
* @param enums enums constraint
*/
public RealBaseType(double min, double max, Set<Double> enums) {
this.min = min;
this.max = max;
this.enums = enums;
}
/**
* Get min.
* @return min
*/
public double getMin() {
return min;
}
/**
* Get max.
* @return max
*/
public double getMax() {
return max;
}
/**
* Get enums.
* @return enums
*/
public Set<Double> getEnums() {
return enums;
}
@Override
public int hashCode() {
return Objects.hash(min, max, enums);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof RealBaseType) {
final RealBaseType other = (RealBaseType) obj;
return Objects.equals(this.enums, other.enums)
&& Objects.equals(this.min, other.min)
&& Objects.equals(this.max, other.max);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("min", min).add("max", max)
.add("enums", enums).toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.schema.type;
import static com.google.common.base.MoreObjects.toStringHelper;
import java.util.Objects;
import java.util.Set;
import com.google.common.collect.Sets;
/**
* One of the strings "integer", "real", "boolean", "string", or "uuid",
* representing the specified scalar type. Refer to RFC 7047 Section 3.2.
*/
public final class StringBaseType implements BaseType {
private final int minLength;
private final int maxLength;
private final Set<String> enums;
/**
* Constructs a StringBaseType object.
*/
public StringBaseType() {
this.minLength = Integer.MIN_VALUE;
this.maxLength = Integer.MAX_VALUE;
this.enums = Sets.newHashSet();
}
/**
* Constructs a StringBaseType object.
* @param minLength minLength constraint
* @param maxLength maxLength constraint
* @param enums enums constraint
*/
public StringBaseType(int minLength, int maxLength, Set<String> enums) {
this.minLength = minLength;
this.maxLength = maxLength;
this.enums = enums;
}
/**
* Get minLength.
* @return minLength
*/
public int getMinLength() {
return minLength;
}
/**
* Get maxLength.
* @return maxLength
*/
public int getMaxLength() {
return maxLength;
}
/**
* Get enums.
* @return enums
*/
public Set<String> getEnums() {
return enums;
}
@Override
public int hashCode() {
return Objects.hash(minLength, maxLength, enums);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof StringBaseType) {
final StringBaseType other = (StringBaseType) obj;
return Objects.equals(this.enums, other.enums)
&& Objects.equals(this.minLength, other.minLength)
&& Objects.equals(this.maxLength, other.maxLength);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("minLength", minLength)
.add("maxLength", maxLength).add("enums", enums).toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.schema.type;
import static com.google.common.base.MoreObjects.toStringHelper;
import java.util.Objects;
/**
* One of the strings "integer", "real", "boolean", "string", or "uuid",
* representing the specified scalar type. Refer to RFC 7047 Section 3.2.
*/
public final class UuidBaseType implements BaseType {
/**
* RefType is strong or weak. refer to base-type of RFC 7047 Section 3.2.
*/
public enum RefType {
STRONG("strong"), WEAK("weak");
private String refType;
private RefType(String refType) {
this.refType = refType;
}
/**
* Returns the refType for RefType.
* @return the refType
*/
public String refType() {
return refType;
}
}
private final String refTable;
private final String refType;
/**
* Constructs a UuidBaseType object.
*/
public UuidBaseType() {
this.refTable = null;
this.refType = RefType.STRONG.refType();
}
/**
* Constructs a UuidBaseType object.
* @param refTable refTable constraint
* @param refType refType constraint
*/
public UuidBaseType(String refTable, String refType) {
this.refTable = refTable;
this.refType = refType;
}
/**
* Get refTable.
* @return refTable
*/
public String getRefTable() {
return refTable;
}
/**
* Get refType.
* @return refType
*/
public String getRefType() {
return refType;
}
@Override
public int hashCode() {
return Objects.hash(refTable, refType);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof UuidBaseType) {
final UuidBaseType other = (UuidBaseType) obj;
return Objects.equals(this.refTable, other.refTable)
&& Objects.equals(this.refType, other.refType);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("refTable", refTable)
.add("refType", refType).toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* ObjectMapper utility class.
*/
public final class ObjectMapperUtil {
private static final Logger log = LoggerFactory
.getLogger(ObjectMapperUtil.class);
/**
* Constructs a ObjectMapperUtil object. Utility classes should not have a
* public or default constructor, otherwise it will compile failed. This
* class should not be instantiated.
*/
private ObjectMapperUtil() {
}
/**
* get ObjectMapper entity.
* @return ObjectMapper entity
*/
public static ObjectMapper getObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
objectMapper.setSerializationInclusion(Include.NON_NULL);
return objectMapper;
}
/**
* get ObjectMapper entity.
* @param flag configure
* @return ObjectMapper entity
*/
public static ObjectMapper getObjectMapper(boolean flag) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
flag);
return objectMapper;
}
/**
* get ObjectMapper entity.
* @param flag configure
* @param incl setSerializationInclusion
* @return ObjectMapper entity
*/
public static ObjectMapper getObjectMapper(boolean flag, Include incl) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
flag);
objectMapper.setSerializationInclusion(incl);
return objectMapper;
}
/**
* convert Object into String.
* @param obj Object
* @return String
*/
public static String convertToString(Object obj) {
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
log.error("JsonProcessingException while converting Entity into string", e);
}
return null;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ovsdb.rfc.utils;
import java.util.Map;
import java.util.Set;
import org.onosproject.ovsdb.rfc.notation.OvsdbMap;
import org.onosproject.ovsdb.rfc.notation.OvsdbSet;
import org.onosproject.ovsdb.rfc.notation.RefTableRow;
import org.onosproject.ovsdb.rfc.notation.UUID;
import org.onosproject.ovsdb.rfc.schema.type.AtomicColumnType;
import org.onosproject.ovsdb.rfc.schema.type.BaseType;
import org.onosproject.ovsdb.rfc.schema.type.BooleanBaseType;
import org.onosproject.ovsdb.rfc.schema.type.ColumnType;
import org.onosproject.ovsdb.rfc.schema.type.IntegerBaseType;
import org.onosproject.ovsdb.rfc.schema.type.KeyValuedColumnType;
import org.onosproject.ovsdb.rfc.schema.type.RealBaseType;
import org.onosproject.ovsdb.rfc.schema.type.StringBaseType;
import org.onosproject.ovsdb.rfc.schema.type.UuidBaseType;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
/**
* Object value utility class.
*/
public final class TransValueUtil {
/**
* Constructs a TransValueUtil object. Utility classes should not have a
* public or default constructor, otherwise it will compile failed.
* This class should not be instantiated.
*/
private TransValueUtil() {
}
/**
* if the type is Set, convert into OvsdbSet, if Map, convert into OvsdbMap.
* @param value Object
* @return Object
*/
public static Object getFormatData(Object value) {
if (value instanceof Map) {
return OvsdbMap.ovsdbMap((Map) value);
} else if (value instanceof Set) {
return OvsdbSet.ovsdbSet((Set) value);
} else {
return value;
}
}
/**
* Transform JsonNode to corresponding value.
* @param json the ColumnType JsonNode
* @param columnType AtomicColumnType or KeyValuedColumnType
* @return Object OvsdbMap or OvsdbSet
*/
public static Object getValueFromJson(JsonNode json, ColumnType columnType) {
if (columnType instanceof AtomicColumnType) {
AtomicColumnType atoType = (AtomicColumnType) columnType;
return getValueFromAtoType(json, atoType);
} else if (columnType instanceof KeyValuedColumnType) {
KeyValuedColumnType kvType = (KeyValuedColumnType) columnType;
return getValueFromKvType(json, kvType);
}
return null;
}
// Convert AtomicColumnType JsonNode into OvsdbSet value
private static Object getValueFromAtoType(JsonNode json,
AtomicColumnType atoType) {
BaseType baseType = atoType.baseType();
// If "min" or "max" is not specified, If "min" is not 1 or "max" is not
// 1,
// or both, and "value" is not specified, the type is a set of scalar
// type "key".
// Refer to RFC 7047, Section 3.2 <type>.
if (atoType.min() != atoType.max()) {
Set set = Sets.newHashSet();
if (json.isArray()) {
if (json.size() == 2) {
if (json.get(0).isTextual()
&& "set".equals(json.get(0).asText())) {
for (JsonNode node : json.get(1)) {
set.add(TransValueUtil.transToValue(node, baseType));
}
} else {
set.add(TransValueUtil.transToValue(json, baseType));
}
}
} else {
set.add(TransValueUtil.transToValue(json, baseType));
}
return OvsdbSet.ovsdbSet(set);
} else {
return TransValueUtil.transToValue(json, baseType);
}
}
// Convert KeyValuedColumnType JsonNode into OvsdbMap value
private static Object getValueFromKvType(JsonNode json,
KeyValuedColumnType kvType) {
if (json.isArray()) {
if (json.size() == 2) {
if (json.get(0).isTextual()
&& "map".equals(json.get(0).asText())) {
Map map = Maps.newHashMap();
for (JsonNode pairNode : json.get(1)) {
if (pairNode.isArray() && json.size() == 2) {
Object key = TransValueUtil.transToValue(pairNode
.get(0), kvType.keyType());
Object value = TransValueUtil.transToValue(pairNode
.get(1), kvType.valueType());
map.put(key, value);
}
}
return OvsdbMap.ovsdbMap(map);
}
}
}
return null;
}
/**
* convert into value.
* @param valueNode the BaseType JsonNode
* @param baseType BooleanBaseType or IntegerBaseType or RealBaseType or
* StringBaseType or UuidBaseType
* @return Object the value of JsonNode
*/
public static Object transToValue(JsonNode valueNode, BaseType baseType) {
if (baseType instanceof BooleanBaseType) {
return valueNode.asBoolean();
} else if (baseType instanceof IntegerBaseType) {
return valueNode.asInt();
} else if (baseType instanceof RealBaseType) {
return valueNode.asDouble();
} else if (baseType instanceof StringBaseType) {
return valueNode.asText();
} else if (baseType instanceof UuidBaseType) {
if (valueNode.isArray()) {
if (valueNode.size() == 2) {
if (valueNode.get(0).isTextual()
&& "uuid".equals(valueNode.get(0).asText())
|| "named-uuid".equals(valueNode.get(0).asText())) {
return UUID.uuid(valueNode.get(1).asText());
}
}
} else {
return new RefTableRow(((UuidBaseType) baseType).getRefTable(),
valueNode);
}
}
return null;
}
}