diff options
Diffstat (limited to 'framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema')
16 files changed, 1436 insertions, 0 deletions
diff --git a/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/ColumnSchema.java b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/ColumnSchema.java new file mode 100644 index 00000000..23eb2be7 --- /dev/null +++ b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/ColumnSchema.java @@ -0,0 +1,83 @@ +/* + * 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 cannot be null"); + checkNotNull(columnType, "column type cannot be 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(); + } +} diff --git a/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/DatabaseSchema.java b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/DatabaseSchema.java new file mode 100644 index 00000000..298afc68 --- /dev/null +++ b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/DatabaseSchema.java @@ -0,0 +1,135 @@ +/* + * 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 cannot be null"); + checkNotNull(version, "version cannot be null"); + checkNotNull(tableSchemas, "tableSchemas cannot be 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(); + } +} diff --git a/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/TableSchema.java b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/TableSchema.java new file mode 100644 index 00000000..248745dd --- /dev/null +++ b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/TableSchema.java @@ -0,0 +1,129 @@ +/* + * 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 cannot be null"); + checkNotNull(columnSchemas, "columnSchemas cannot be 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(); + } +} diff --git a/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/package-info.java b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/package-info.java new file mode 100644 index 00000000..9a27037c --- /dev/null +++ b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/package-info.java @@ -0,0 +1,20 @@ +/* + * 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. + */ + +/** + * OVSDB schema. + */ +package org.onosproject.ovsdb.rfc.schema; diff --git a/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/AtomicColumnType.java b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/AtomicColumnType.java new file mode 100644 index 00000000..881755ae --- /dev/null +++ b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/AtomicColumnType.java @@ -0,0 +1,104 @@ +/* + * 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 cannot be 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 cannot be 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(); + } +} diff --git a/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/BaseType.java b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/BaseType.java new file mode 100644 index 00000000..d2614c12 --- /dev/null +++ b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/BaseType.java @@ -0,0 +1,24 @@ +/* + * 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 { + +} diff --git a/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/BaseTypeFactory.java b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/BaseTypeFactory.java new file mode 100644 index 00000000..86dbb2e6 --- /dev/null +++ b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/BaseTypeFactory.java @@ -0,0 +1,214 @@ +/* + * 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.exception.AbnormalJsonNodeException; +import org.onosproject.ovsdb.rfc.schema.type.UuidBaseType.RefType; +import org.onosproject.ovsdb.rfc.utils.ObjectMapperUtil; + +import com.fasterxml.jackson.databind.JsonNode; +import com.google.common.collect.Sets; + +/** + * 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 baseTypeJson the BaseType JsonNode + * @param keyorval the key node or value node + * @return BaseType + */ + public static BaseType getBaseTypeFromJson(JsonNode baseTypeJson, String keyorval) { + if (baseTypeJson.isValueNode()) { + String type = baseTypeJson.asText().trim(); + return fromTypeStr(type); + } else { + if (!baseTypeJson.has(keyorval)) { + String message = "Abnormal BaseType JsonNode, it should contain 'key' or 'value' node but was not found" + + ObjectMapperUtil.convertToString(baseTypeJson); + throw new AbnormalJsonNodeException(message); + } + return fromJsonNode(baseTypeJson.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 (type.has("enum")) { + JsonNode anEnum = type.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 (type.has("enum")) { + JsonNode anEnum = type.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 (type.has("enum")) { + JsonNode enumVal = type.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); + } +} diff --git a/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/BooleanBaseType.java b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/BooleanBaseType.java new file mode 100644 index 00000000..d42337a5 --- /dev/null +++ b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/BooleanBaseType.java @@ -0,0 +1,33 @@ +/* + * 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(); + } +} diff --git a/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/ColumnType.java b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/ColumnType.java new file mode 100644 index 00000000..20f1bbac --- /dev/null +++ b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/ColumnType.java @@ -0,0 +1,24 @@ +/* + * 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 { + +} diff --git a/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/ColumnTypeFactory.java b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/ColumnTypeFactory.java new file mode 100644 index 00000000..47b34a6b --- /dev/null +++ b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/ColumnTypeFactory.java @@ -0,0 +1,123 @@ +/* + * 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.exception.AbnormalJsonNodeException; +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 columnTypeJson the ColumnType JsonNode + * @return ColumnType + */ + public static ColumnType getColumnTypeFromJson(JsonNode columnTypeJson) { + if (!columnTypeJson.isObject() || !columnTypeJson.has(Type.VALUE.type())) { + return createAtomicColumnType(columnTypeJson); + } else if (!columnTypeJson.isValueNode() && columnTypeJson.has(Type.VALUE.type())) { + return createKeyValuedColumnType(columnTypeJson); + } + String message = "Abnormal ColumnType JsonNode, it should be AtomicColumnType or KeyValuedColumnType" + + ObjectMapperUtil.convertToString(columnTypeJson); + throw new AbnormalJsonNodeException(message); + } + + /** + * 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 && node.isNumber()) { + 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 && node.isNumber()) { + 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); + } +} diff --git a/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/IntegerBaseType.java b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/IntegerBaseType.java new file mode 100644 index 00000000..91939515 --- /dev/null +++ b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/IntegerBaseType.java @@ -0,0 +1,103 @@ +/* + * 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(); + } +} diff --git a/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/KeyValuedColumnType.java b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/KeyValuedColumnType.java new file mode 100644 index 00000000..8407457a --- /dev/null +++ b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/KeyValuedColumnType.java @@ -0,0 +1,108 @@ +/* + * 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 cannot be null"); + checkNotNull(valueType, "valueType cannot be 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(); + } +} diff --git a/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/RealBaseType.java b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/RealBaseType.java new file mode 100644 index 00000000..6704e30a --- /dev/null +++ b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/RealBaseType.java @@ -0,0 +1,103 @@ +/* + * 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(); + } +} diff --git a/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/StringBaseType.java b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/StringBaseType.java new file mode 100644 index 00000000..96d2f739 --- /dev/null +++ b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/StringBaseType.java @@ -0,0 +1,103 @@ +/* + * 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(); + } +} diff --git a/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/UuidBaseType.java b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/UuidBaseType.java new file mode 100644 index 00000000..46e0d9fa --- /dev/null +++ b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/UuidBaseType.java @@ -0,0 +1,110 @@ +/* + * 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; + +/** + * 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) { + checkNotNull(refType, "refType cannot be null"); + 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(); + } +} diff --git a/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/package-info.java b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/package-info.java new file mode 100644 index 00000000..eade2e28 --- /dev/null +++ b/framework/src/onos/protocols/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/schema/type/package-info.java @@ -0,0 +1,20 @@ +/* + * 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. + */ + +/** + * OVSDB schema types. + */ +package org.onosproject.ovsdb.rfc.schema.type; |