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     boolean ephemeral;
27     boolean mutable;
28
29     public ColumnSchema(String name, ColumnType columnType) {
30         this.name = name;
31         this.type = columnType;
32     }
33
34     public static ColumnSchema fromJson(String name, JsonNode json) {
35         if (!json.isObject() || !json.has("type")) {
36             throw new BadSchemaException("bad column schema root, expected \"type\" as child");
37         }
38
39         return new ColumnSchema(name, ColumnType.fromJson(json.get("type")));
40     }
41
42     public String getName() {
43         return name;
44     }
45
46     public ColumnType getType() {
47         return type;
48     }
49
50     // --- Operations on the column ----------//
51
52     public Condition opEqual(D data) {
53         return new Condition(this.getName(), Function.EQUALS, data);
54     }
55
56     public Condition opGreaterThan(D data) {
57         return new Condition(this.getName(), Function.GREATER_THAN, data);
58     }
59
60     public Condition opLesserThan(D data) {
61         return new Condition(this.getName(), Function.GREATER_THAN, data);
62     }
63
64     public Condition opLesserThanOrEquals(D data) {
65         return new Condition(this.getName(), Function.LESS_THAN_OR_EQUALS, data);
66     }
67
68     public Condition opIncludes(D data) {
69         return new Condition(this.getName(), Function.INCLUDES, data);
70     }
71
72     public Condition opExcludes(D data) {
73         return new Condition(this.getName(), Function.EXCLUDES, data);
74     }
75
76     // --- Operations on the column ----------//:w
77
78
79     @Override
80     public String toString() {
81         return "ColumnSchema{"
82                 + "name='" + name + '\''
83                 + ", type=" + type
84                 + '}';
85     }
86
87     @Override
88     public int hashCode() {
89         final int prime = 31;
90         int result = 1;
91         result = prime * result + ((name == null) ? 0 : name.hashCode());
92         result = prime * result + ((type == null) ? 0 : type.hashCode());
93         return result;
94     }
95
96     @Override
97     public boolean equals(Object obj) {
98         if (this == obj) {
99             return true;
100         }
101         if (obj == null) {
102             return false;
103         }
104         if (getClass() != obj.getClass()) {
105             return false;
106         }
107         ColumnSchema other = (ColumnSchema) obj;
108         if (name == null) {
109             if (other.name != null) {
110                 return false;
111             }
112         } else if (!name.equals(other.name)) {
113             return false;
114         }
115         if (type == null) {
116             if (other.type != null) {
117                 return false;
118             }
119         } else if (!type.equals(other.type)) {
120             return false;
121         }
122         return true;
123     }
124
125     /**
126      * Validates the passed in value against the constraints set for this ColumnSchema
127      * @param value
128      * @throws java.lang.RuntimeException (validation exception)
129      */
130     public D validate(Object value)throws RuntimeException {
131         //todo(type check and validate based on constraints set)
132         this.type.validate(value);
133         return (D) value;
134     }
135
136     /**
137      * Verifies if this Column if of the specified type
138      * @param type the type to check for
139      */
140     public void validateType(Class<?> type) {
141
142     }
143
144     public D valueFromJson(JsonNode value) {
145         return (D) this.getType().valueFromJson(value);
146     }
147
148     public Object getNormalizeData(D value) {
149         Object untypedValue = null;
150         if (value instanceof Set) {
151             untypedValue = OvsdbSet.fromSet((Set) value);
152         } else if (value instanceof Map) {
153             untypedValue = OvsdbMap.fromMap((Map) value);
154         } else {
155             untypedValue = value;
156         }
157         return untypedValue;
158     }
159 }