blob: af1a01d6bec4c725b0c85a8469cb66fb430cfa2b [file] [log] [blame]
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.ovsdb.rfc.tableservice;
17
18import static com.google.common.base.MoreObjects.toStringHelper;
19import static com.google.common.base.Preconditions.checkNotNull;
20
21import java.util.Objects;
22
Sho SHIMIZU8700d422015-08-26 18:13:44 -070023import org.onosproject.ovsdb.rfc.exception.ColumnSchemaNotFoundException;
24import org.onosproject.ovsdb.rfc.exception.TableSchemaNotFoundException;
25import org.onosproject.ovsdb.rfc.exception.VersionMismatchException;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070026import org.onosproject.ovsdb.rfc.notation.Column;
27import org.onosproject.ovsdb.rfc.notation.Row;
28import org.onosproject.ovsdb.rfc.notation.UUID;
29import org.onosproject.ovsdb.rfc.schema.ColumnSchema;
30import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
31import org.onosproject.ovsdb.rfc.schema.TableSchema;
32import org.onosproject.ovsdb.rfc.table.OvsdbTable;
33import org.onosproject.ovsdb.rfc.table.VersionNum;
34import org.onosproject.ovsdb.rfc.utils.VersionUtil;
35
36/**
37 * Representation of conversion between Ovsdb table and Row.
38 */
39public abstract class AbstractOvsdbTableService implements OvsdbTableService {
40
41 private final DatabaseSchema dbSchema;
42 private final Row row;
43 private final TableDescription tableDesc;
44
45 /**
46 * Constructs a AbstractOvsdbTableService object.
47 * @param dbSchema DatabaseSchema entity
48 * @param row Row entity
49 * @param table table name
50 * @param formVersion the initial version
51 */
52 public AbstractOvsdbTableService(DatabaseSchema dbSchema, Row row, OvsdbTable table,
53 VersionNum formVersion) {
54 checkNotNull(dbSchema, "database schema cannot be null");
55 checkNotNull(row, "row cannot be null");
56 checkNotNull(table, "table cannot be null");
57 checkNotNull(formVersion, "the initial version cannot be null");
58 this.dbSchema = dbSchema;
59 row.setTableName(table.tableName());
60 this.row = row;
61 TableDescription tableDesc = new TableDescription(table, formVersion);
62 this.tableDesc = tableDesc;
63 }
64
65 /**
66 * Check whether the parameter of dbSchema is valid and check whether the
67 * table is existent in Database Schema.
68 */
69 private boolean isValid() {
70 if (dbSchema == null) {
71 return false;
72 }
73 if (!dbSchema.name().equalsIgnoreCase(tableDesc.database())) {
74 return false;
75 }
76 checkTableSchemaVersion();
77 return true;
78 }
79
80 /**
81 * Check the table version.
82 */
83 private void checkTableSchemaVersion() {
84 String fromVersion = tableDesc.fromVersion();
85 String untilVersion = tableDesc.untilVersion();
86 String schemaVersion = dbSchema.version();
87 checkVersion(schemaVersion, fromVersion, untilVersion);
88 }
89
90 /**
91 * Check the column version.
92 * @param columnDesc ColumnDescription entity
93 */
94 private void checkColumnSchemaVersion(ColumnDescription columnDesc) {
95 String fromVersion = columnDesc.fromVersion();
96 String untilVersion = columnDesc.untilVersion();
97 String schemaVersion = dbSchema.version();
98 checkVersion(schemaVersion, fromVersion, untilVersion);
99 }
100
101 /**
102 * Check whether the DatabaseSchema version between the initial version and
103 * the end of the version.
104 * @param schemaVersion DatabaseSchema version
105 * @param fromVersion The initial version
106 * @param untilVersion The end of the version
107 * @throws VersionMismatchException this is a version mismatch exception
108 */
109 private void checkVersion(String schemaVersion, String fromVersion, String untilVersion) {
110 VersionUtil.versionMatch(fromVersion);
111 VersionUtil.versionMatch(untilVersion);
112 if (!fromVersion.equals(VersionUtil.DEFAULT_VERSION_STRING)) {
113 if (VersionUtil.versionCompare(schemaVersion, fromVersion) < 0) {
114 String message = VersionMismatchException.createFromMessage(schemaVersion,
115 fromVersion);
116 throw new VersionMismatchException(message);
117 }
118 }
119 if (!untilVersion.equals(VersionUtil.DEFAULT_VERSION_STRING)) {
120 if (VersionUtil.versionCompare(untilVersion, schemaVersion) < 0) {
121 String message = VersionMismatchException.createToMessage(schemaVersion,
122 untilVersion);
123 throw new VersionMismatchException(message);
124 }
125 }
126 }
127
128 /**
129 * Returns TableSchema from dbSchema by table name.
130 * @return TableSchema
131 */
132 private TableSchema getTableSchema() {
133 String tableName = tableDesc.name();
134 return dbSchema.getTableSchema(tableName);
135 }
136
137 /**
138 * Returns ColumnSchema from TableSchema by column name.
139 * @param columnName column name
140 * @return ColumnSchema
141 */
142 private ColumnSchema getColumnSchema(String columnName) {
143 TableSchema tableSchema = getTableSchema();
144 if (tableSchema == null) {
145 String message = TableSchemaNotFoundException.createMessage(tableDesc.name(),
146 dbSchema.name());
147 throw new TableSchemaNotFoundException(message);
148 }
149 ColumnSchema columnSchema = tableSchema.getColumnSchema(columnName);
150 if (columnSchema == null) {
151 String message = ColumnSchemaNotFoundException.createMessage(columnName,
152 tableSchema.name());
153 throw new ColumnSchemaNotFoundException(message);
154 }
155 return columnSchema;
156 }
157
158 @Override
159 public Column getColumnHandler(ColumnDescription columnDesc) {
160 if (!isValid()) {
161 return null;
162 }
163 String columnName = columnDesc.name();
164 checkColumnSchemaVersion(columnDesc);
165 ColumnSchema columnSchema = getColumnSchema(columnName);
166 if (row == null) {
167 return null;
168 }
169 return row.getColumn(columnSchema.name());
170 }
171
172 @Override
173 public Object getDataHandler(ColumnDescription columnDesc) {
174 if (!isValid()) {
175 return null;
176 }
177 String columnName = columnDesc.name();
178 checkColumnSchemaVersion(columnDesc);
179 ColumnSchema columnSchema = getColumnSchema(columnName);
180 if (row == null || row.getColumn(columnSchema.name()) == null) {
181 return null;
182 }
183 return row.getColumn(columnSchema.name()).data();
184 }
185
186 @Override
187 public void setDataHandler(ColumnDescription columnDesc, Object obj) {
188 if (!isValid()) {
189 return;
190 }
191 String columnName = columnDesc.name();
192 checkColumnSchemaVersion(columnDesc);
193 ColumnSchema columnSchema = getColumnSchema(columnName);
194 Column column = new Column(columnSchema.name(), obj);
195 row.addColumn(columnName, column);
196 }
197
198 @Override
199 public UUID getTableUuid() {
200 if (!isValid()) {
201 return null;
202 }
203 ColumnDescription columnDesc = new ColumnDescription("_uuid", "getTableUuid");
204 return (UUID) getDataHandler(columnDesc);
205 }
206
207 @Override
208 public Column getTableUuidColumn() {
209 if (!isValid()) {
210 return null;
211 }
212 ColumnDescription columnDesc = new ColumnDescription("_uuid", "getTableUuidColumn");
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700213 return getColumnHandler(columnDesc);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700214 }
215
216 @Override
217 public UUID getTableVersion() {
218 if (!isValid()) {
219 return null;
220 }
221 ColumnDescription columnDesc = new ColumnDescription("_version", "getTableVersion");
222 return (UUID) getDataHandler(columnDesc);
223 }
224
225 @Override
226 public Column getTableVersionColumn() {
227 if (!isValid()) {
228 return null;
229 }
230 ColumnDescription columnDesc = new ColumnDescription("_version", "getTableVersionColumn");
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700231 return getColumnHandler(columnDesc);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700232 }
233
234 /**
235 * Get DatabaseSchema entity.
236 * @return DatabaseSchema entity
237 */
238 public DatabaseSchema dbSchema() {
239 return dbSchema;
240 }
241
242 /**
243 * Get Row entity.
244 * @return Row entity
245 */
246 public Row getRow() {
247 if (!isValid()) {
248 return null;
249 }
250 return this.row;
251 }
252
253 /**
254 * Get TableDescription entity.
255 * @return TableDescription entity
256 */
257 public TableDescription tableDesc() {
258 return tableDesc;
259 }
260
261 @Override
262 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700263 return row.hashCode();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700264 }
265
266 @Override
267 public boolean equals(Object obj) {
268 if (this == obj) {
269 return true;
270 }
271 if (obj instanceof AbstractOvsdbTableService) {
272 final AbstractOvsdbTableService other = (AbstractOvsdbTableService) obj;
273 return Objects.equals(this.row, other.row);
274 }
275 return false;
276 }
277
278 @Override
279 public String toString() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700280 TableSchema schema = getTableSchema();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700281 String tableName = schema.name();
282 return toStringHelper(this).add("tableName", tableName).add("row", row).toString();
283 }
284}