aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/ovsdb/rfc/src/main/java/org/onosproject/ovsdb/rfc/tableservice/AbstractOvsdbTableService.java
blob: af1a01d6bec4c725b0c85a8469cb66fb430cfa2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * 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.tableservice;

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.exception.ColumnSchemaNotFoundException;
import org.onosproject.ovsdb.rfc.exception.TableSchemaNotFoundException;
import org.onosproject.ovsdb.rfc.exception.VersionMismatchException;
import org.onosproject.ovsdb.rfc.notation.Column;
import org.onosproject.ovsdb.rfc.notation.Row;
import org.onosproject.ovsdb.rfc.notation.UUID;
import org.onosproject.ovsdb.rfc.schema.ColumnSchema;
import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
import org.onosproject.ovsdb.rfc.schema.TableSchema;
import org.onosproject.ovsdb.rfc.table.OvsdbTable;
import org.onosproject.ovsdb.rfc.table.VersionNum;
import org.onosproject.ovsdb.rfc.utils.VersionUtil;

/**
 * Representation of conversion between Ovsdb table and Row.
 */
public abstract class AbstractOvsdbTableService implements OvsdbTableService {

    private final DatabaseSchema dbSchema;
    private final Row row;
    private final TableDescription tableDesc;

    /**
     * Constructs a AbstractOvsdbTableService object.
     * @param dbSchema DatabaseSchema entity
     * @param row Row entity
     * @param table table name
     * @param formVersion the initial version
     */
    public AbstractOvsdbTableService(DatabaseSchema dbSchema, Row row, OvsdbTable table,
                                     VersionNum formVersion) {
        checkNotNull(dbSchema, "database schema cannot be null");
        checkNotNull(row, "row cannot be null");
        checkNotNull(table, "table cannot be null");
        checkNotNull(formVersion, "the initial version cannot be null");
        this.dbSchema = dbSchema;
        row.setTableName(table.tableName());
        this.row = row;
        TableDescription tableDesc = new TableDescription(table, formVersion);
        this.tableDesc = tableDesc;
    }

    /**
     * Check whether the parameter of dbSchema is valid and check whether the
     * table is existent in Database Schema.
     */
    private boolean isValid() {
        if (dbSchema == null) {
            return false;
        }
        if (!dbSchema.name().equalsIgnoreCase(tableDesc.database())) {
            return false;
        }
        checkTableSchemaVersion();
        return true;
    }

    /**
     * Check the table version.
     */
    private void checkTableSchemaVersion() {
        String fromVersion = tableDesc.fromVersion();
        String untilVersion = tableDesc.untilVersion();
        String schemaVersion = dbSchema.version();
        checkVersion(schemaVersion, fromVersion, untilVersion);
    }

    /**
     * Check the column version.
     * @param columnDesc ColumnDescription entity
     */
    private void checkColumnSchemaVersion(ColumnDescription columnDesc) {
        String fromVersion = columnDesc.fromVersion();
        String untilVersion = columnDesc.untilVersion();
        String schemaVersion = dbSchema.version();
        checkVersion(schemaVersion, fromVersion, untilVersion);
    }

    /**
     * Check whether the DatabaseSchema version between the initial version and
     * the end of the version.
     * @param schemaVersion DatabaseSchema version
     * @param fromVersion The initial version
     * @param untilVersion The end of the version
     * @throws VersionMismatchException this is a version mismatch exception
     */
    private void checkVersion(String schemaVersion, String fromVersion, String untilVersion) {
        VersionUtil.versionMatch(fromVersion);
        VersionUtil.versionMatch(untilVersion);
        if (!fromVersion.equals(VersionUtil.DEFAULT_VERSION_STRING)) {
            if (VersionUtil.versionCompare(schemaVersion, fromVersion) < 0) {
                String message = VersionMismatchException.createFromMessage(schemaVersion,
                                                                            fromVersion);
                throw new VersionMismatchException(message);
            }
        }
        if (!untilVersion.equals(VersionUtil.DEFAULT_VERSION_STRING)) {
            if (VersionUtil.versionCompare(untilVersion, schemaVersion) < 0) {
                String message = VersionMismatchException.createToMessage(schemaVersion,
                                                                          untilVersion);
                throw new VersionMismatchException(message);
            }
        }
    }

    /**
     * Returns TableSchema from dbSchema by table name.
     * @return TableSchema
     */
    private TableSchema getTableSchema() {
        String tableName = tableDesc.name();
        return dbSchema.getTableSchema(tableName);
    }

    /**
     * Returns ColumnSchema from TableSchema by column name.
     * @param columnName column name
     * @return ColumnSchema
     */
    private ColumnSchema getColumnSchema(String columnName) {
        TableSchema tableSchema = getTableSchema();
        if (tableSchema == null) {
            String message = TableSchemaNotFoundException.createMessage(tableDesc.name(),
                                                                        dbSchema.name());
            throw new TableSchemaNotFoundException(message);
        }
        ColumnSchema columnSchema = tableSchema.getColumnSchema(columnName);
        if (columnSchema == null) {
            String message = ColumnSchemaNotFoundException.createMessage(columnName,
                                                                         tableSchema.name());
            throw new ColumnSchemaNotFoundException(message);
        }
        return columnSchema;
    }

    @Override
    public Column getColumnHandler(ColumnDescription columnDesc) {
        if (!isValid()) {
            return null;
        }
        String columnName = columnDesc.name();
        checkColumnSchemaVersion(columnDesc);
        ColumnSchema columnSchema = getColumnSchema(columnName);
        if (row == null) {
            return null;
        }
        return row.getColumn(columnSchema.name());
    }

    @Override
    public Object getDataHandler(ColumnDescription columnDesc) {
        if (!isValid()) {
            return null;
        }
        String columnName = columnDesc.name();
        checkColumnSchemaVersion(columnDesc);
        ColumnSchema columnSchema = getColumnSchema(columnName);
        if (row == null || row.getColumn(columnSchema.name()) == null) {
            return null;
        }
        return row.getColumn(columnSchema.name()).data();
    }

    @Override
    public void setDataHandler(ColumnDescription columnDesc, Object obj) {
        if (!isValid()) {
            return;
        }
        String columnName = columnDesc.name();
        checkColumnSchemaVersion(columnDesc);
        ColumnSchema columnSchema = getColumnSchema(columnName);
        Column column = new Column(columnSchema.name(), obj);
        row.addColumn(columnName, column);
    }

    @Override
    public UUID getTableUuid() {
        if (!isValid()) {
            return null;
        }
        ColumnDescription columnDesc = new ColumnDescription("_uuid", "getTableUuid");
        return (UUID) getDataHandler(columnDesc);
    }

    @Override
    public Column getTableUuidColumn() {
        if (!isValid()) {
            return null;
        }
        ColumnDescription columnDesc = new ColumnDescription("_uuid", "getTableUuidColumn");
        return getColumnHandler(columnDesc);
    }

    @Override
    public UUID getTableVersion() {
        if (!isValid()) {
            return null;
        }
        ColumnDescription columnDesc = new ColumnDescription("_version", "getTableVersion");
        return (UUID) getDataHandler(columnDesc);
    }

    @Override
    public Column getTableVersionColumn() {
        if (!isValid()) {
            return null;
        }
        ColumnDescription columnDesc = new ColumnDescription("_version", "getTableVersionColumn");
        return getColumnHandler(columnDesc);
    }

    /**
     * Get DatabaseSchema entity.
     * @return DatabaseSchema entity
     */
    public DatabaseSchema dbSchema() {
        return dbSchema;
    }

    /**
     * Get Row entity.
     * @return Row entity
     */
    public Row getRow() {
        if (!isValid()) {
            return null;
        }
        return this.row;
    }

    /**
     * Get TableDescription entity.
     * @return TableDescription entity
     */
    public TableDescription tableDesc() {
        return tableDesc;
    }

    @Override
    public int hashCode() {
        return row.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof AbstractOvsdbTableService) {
            final AbstractOvsdbTableService other = (AbstractOvsdbTableService) obj;
            return Objects.equals(this.row, other.row);
        }
        return false;
    }

    @Override
    public String toString() {
        TableSchema schema = getTableSchema();
        String tableName = schema.name();
        return toStringHelper(this).add("tableName", tableName).add("row", row).toString();
    }
}