d620c502a03bfdcf571b11c8606508498556ef72
[ovsdb.git] / ovsdb / src / main / java / org / opendaylight / ovsdb / lib / schema / ColumnSchema.java
1 /*
2  * Copyright (C) 2014 EBay Software Foundation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Authors : Ashwin Raveendran
9  */
10 package org.opendaylight.ovsdb.lib.schema;
11
12 import com.fasterxml.jackson.databind.JsonNode;
13 import org.opendaylight.ovsdb.lib.notation.Condition;
14 import org.opendaylight.ovsdb.lib.notation.Function;
15
16
17 public class ColumnSchema<E extends TableSchema<E>, D> {
18     String name;
19     ColumnType type;
20     boolean ephemeral;
21     boolean mutable;
22
23     public ColumnSchema(String name, ColumnType columnType) {
24         this.name = name;
25         this.type = columnType;
26     }
27
28     public static ColumnSchema fromJson(String name, JsonNode json) {
29         if (!json.isObject() || !json.has("type")) {
30             //todo specific types of exception
31             throw new RuntimeException("bad column schema root, expected \"type\" as child");
32         }
33
34         return new ColumnSchema(name, ColumnType.fromJson(json.get("type")));
35     }
36
37     public String getName() {
38         return name;
39     }
40
41     public ColumnType getType() { return type; }
42
43     // --- Operations on the column ----------//
44
45     public Condition opEqual(D data) {
46         return new Condition(this.getName(), Function.EQUALS, data);
47     }
48
49     public Condition opGreaterThan(D data) {
50         return new Condition(this.getName(), Function.GREATER_THAN, data);
51     }
52
53     public Condition opLesserThan(D data) {
54         return new Condition(this.getName(), Function.GREATER_THAN, data);
55     }
56
57     public Condition opLesserThanOrEquals(D data) {
58         return new Condition(this.getName(), Function.LESS_THAN_OR_EQUALS, data);
59     }
60
61     // --- Operations on the column ----------//:w
62
63     @Override
64     public String toString() {
65         return "ColumnType [type=" + type + ", ephemeral=" + ephemeral
66                 + ", mutable=" + mutable + "]";
67     }
68
69     /**
70      * Validates the passed in value against the constraints set for this ColumnSchema
71      * @param value
72      * @throws java.lang.RuntimeException (validation exception)
73      */
74     public void validate(Object value)throws RuntimeException {
75         //todo(type check and validate based on constraints set)
76     }
77 }