Merge "Added Security Rule for Custom ICMP"
[ovsdb.git] / library / impl / src / main / java / org / opendaylight / ovsdb / lib / schema / ColumnSchema.java
1 /*
2  * Copyright (c) 2014, 2015 EBay Software Foundation and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.lib.schema;
10
11 import java.util.Map;
12 import java.util.Set;
13
14 import org.opendaylight.ovsdb.lib.error.BadSchemaException;
15 import org.opendaylight.ovsdb.lib.notation.Condition;
16 import org.opendaylight.ovsdb.lib.notation.Function;
17 import org.opendaylight.ovsdb.lib.notation.OvsdbMap;
18 import org.opendaylight.ovsdb.lib.notation.OvsdbSet;
19
20 import com.fasterxml.jackson.databind.JsonNode;
21
22
23 public class ColumnSchema<E extends TableSchema<E>, D> {
24     String name;
25     ColumnType type;
26
27     public ColumnSchema(String name, ColumnType columnType) {
28         this.name = name;
29         this.type = columnType;
30     }
31
32     public static ColumnSchema fromJson(String name, JsonNode json) {
33         if (!json.isObject() || !json.has("type")) {
34             throw new BadSchemaException("bad column schema root, expected \"type\" as child");
35         }
36
37         return new ColumnSchema(name, ColumnType.fromJson(json.get("type")));
38     }
39
40     public String getName() {
41         return name;
42     }
43
44     public ColumnType getType() {
45         return type;
46     }
47
48     // --- Operations on the column ----------//
49
50     public Condition opEqual(D data) {
51         return new Condition(this.getName(), Function.EQUALS, data);
52     }
53
54     public Condition opGreaterThan(D data) {
55         return new Condition(this.getName(), Function.GREATER_THAN, data);
56     }
57
58     public Condition opLesserThan(D data) {
59         return new Condition(this.getName(), Function.GREATER_THAN, data);
60     }
61
62     public Condition opLesserThanOrEquals(D data) {
63         return new Condition(this.getName(), Function.LESS_THAN_OR_EQUALS, data);
64     }
65
66     public Condition opIncludes(D data) {
67         return new Condition(this.getName(), Function.INCLUDES, data);
68     }
69
70     public Condition opExcludes(D data) {
71         return new Condition(this.getName(), Function.EXCLUDES, data);
72     }
73
74     // --- Operations on the column ----------//:w
75
76
77     @Override
78     public String toString() {
79         return "ColumnSchema{"
80                 + "name='" + name + '\''
81                 + ", type=" + type
82                 + '}';
83     }
84
85     @Override
86     public int hashCode() {
87         final int prime = 31;
88         int result = 1;
89         result = prime * result + ((name == null) ? 0 : name.hashCode());
90         result = prime * result + ((type == null) ? 0 : type.hashCode());
91         return result;
92     }
93
94     @Override
95     public boolean equals(Object obj) {
96         if (this == obj) {
97             return true;
98         }
99         if (obj == null) {
100             return false;
101         }
102         if (getClass() != obj.getClass()) {
103             return false;
104         }
105         ColumnSchema other = (ColumnSchema) obj;
106         if (name == null) {
107             if (other.name != null) {
108                 return false;
109             }
110         } else if (!name.equals(other.name)) {
111             return false;
112         }
113         if (type == null) {
114             if (other.type != null) {
115                 return false;
116             }
117         } else if (!type.equals(other.type)) {
118             return false;
119         }
120         return true;
121     }
122
123     /**
124      * Validates the passed in value against the constraints set for this ColumnSchema
125      * @param value
126      */
127     public D validate(Object value) {
128         //todo(type check and validate based on constraints set)
129         this.type.validate(value);
130         return (D) value;
131     }
132
133     /**
134      * Verifies if this Column if of the specified type
135      * @param type the type to check for
136      */
137     public void validateType(Class<?> type) {
138
139     }
140
141     public D valueFromJson(JsonNode value) {
142         return (D) this.getType().valueFromJson(value);
143     }
144
145     public Object getNormalizeData(D value) {
146         Object untypedValue;
147         if (value instanceof Set) {
148             untypedValue = OvsdbSet.fromSet((Set) value);
149         } else if (value instanceof Map) {
150             untypedValue = OvsdbMap.fromMap((Map) value);
151         } else {
152             untypedValue = value;
153         }
154         return untypedValue;
155     }
156 }