/** * Delete transact config. * * @param childTableName child table name * @param childColumnName child column name * @param childUuid child row uuid * @param parentTableName parent table name * @param parentColumnName parent column */ private void deleteConfig( String childTableName, String childColumnName, String childUuid, String parentTableName, String parentColumnName) { DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); TableSchema childTableSchema = dbSchema.getTableSchema(childTableName); ArrayList<Operation> operations = Lists.newArrayList(); if (parentTableName != null && parentColumnName != null) { TableSchema parentTableSchema = dbSchema.getTableSchema(parentTableName); ColumnSchema parentColumnSchema = parentTableSchema.getColumnSchema(parentColumnName); List<Mutation> mutations = Lists.newArrayList(); Mutation mutation = MutationUtil.delete(parentColumnSchema.name(), UUID.uuid(childUuid)); mutations.add(mutation); List<Condition> conditions = Lists.newArrayList(); Condition condition = ConditionUtil.includes(parentColumnName, UUID.uuid(childUuid)); conditions.add(condition); Mutate op = new Mutate(parentTableSchema, conditions, mutations); operations.add(op); } List<Condition> conditions = Lists.newArrayList(); Condition condition = ConditionUtil.equals(childColumnName, UUID.uuid(childUuid)); conditions.add(condition); Delete del = new Delete(childTableSchema, conditions); operations.add(del); transactConfig(OvsdbConstant.DATABASENAME, operations); return; }
/** * Insert transact config. * * @param childTableName child table name * @param childColumnName child column name * @param parentTableName parent table name * @param parentColumnName parent column * @param parentUuid parent uuid * @param row the config data * @return uuid, empty if no uuid is find */ private String insertConfig( String childTableName, String childColumnName, String parentTableName, String parentColumnName, String parentUuid, Row row) { DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); TableSchema tableSchema = dbSchema.getTableSchema(childTableName); String namedUuid = childTableName; Insert insert = new Insert(tableSchema, namedUuid, row); ArrayList<Operation> operations = Lists.newArrayList(); operations.add(insert); if (parentTableName != null && parentColumnName != null) { TableSchema parentTableSchema = dbSchema.getTableSchema(parentTableName); ColumnSchema parentColumnSchema = parentTableSchema.getColumnSchema(parentColumnName); List<Mutation> mutations = Lists.newArrayList(); Mutation mutation = MutationUtil.insert(parentColumnSchema.name(), UUID.uuid(namedUuid)); mutations.add(mutation); List<Condition> conditions = Lists.newArrayList(); Condition condition = ConditionUtil.equals("_uuid", UUID.uuid(parentUuid)); conditions.add(condition); Mutate op = new Mutate(parentTableSchema, conditions, mutations); operations.add(op); } if (childTableName.equalsIgnoreCase(OvsdbConstant.PORT)) { log.info("Handle port insert"); Insert intfInsert = handlePortInsertTable(OvsdbConstant.INTERFACE, row); if (intfInsert != null) { operations.add(intfInsert); } Insert ins = (Insert) operations.get(0); ins.getRow().put("interfaces", UUID.uuid(OvsdbConstant.INTERFACE)); } List<OperationResult> results; try { results = transactConfig(OvsdbConstant.DATABASENAME, operations).get(); return results.get(0).getUuid().value(); } catch (InterruptedException e) { log.warn("Interrupted while waiting to get result"); Thread.currentThread().interrupt(); } catch (ExecutionException e) { log.error("Exception thrown while to get result"); } return null; }
/** * Update transact config. * * @param tableName table name * @param columnName column name * @param uuid uuid * @param row the config data */ private void updateConfig(String tableName, String columnName, String uuid, Row row) { DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); TableSchema tableSchema = dbSchema.getTableSchema(tableName); List<Condition> conditions = Lists.newArrayList(); Condition condition = ConditionUtil.equals(columnName, UUID.uuid(uuid)); conditions.add(condition); Update update = new Update(tableSchema, row, conditions); ArrayList<Operation> operations = Lists.newArrayList(); operations.add(update); transactConfig(OvsdbConstant.DATABASENAME, operations); }
/** * Handles port insert. * * @param tableName ovsdb table interface * @param portRow row of port * @return insert, empty if null */ private Insert handlePortInsertTable(String tableName, Row portRow) { DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME); TableSchema portTableSchema = dbSchema.getTableSchema(OvsdbConstant.PORT); ColumnSchema portColumnSchema = portTableSchema.getColumnSchema("name"); String portName = (String) portRow.getColumn(portColumnSchema.name()).data(); Interface inf = (Interface) TableGenerator.createTable(dbSchema, OvsdbTable.INTERFACE); inf.setName(portName); TableSchema intfTableSchema = dbSchema.getTableSchema(OvsdbConstant.INTERFACE); Insert insert = new Insert(intfTableSchema, OvsdbConstant.INTERFACE, inf.getRow()); return insert; }
/** * 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; }
/** * 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; }
/** * 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 the table version. */ private void checkTableSchemaVersion() { String fromVersion = tableDesc.fromVersion(); String untilVersion = tableDesc.untilVersion(); String schemaVersion = dbSchema.version(); checkVersion(schemaVersion, fromVersion, untilVersion); }
/** * Returns TableSchema from dbSchema by table name. * * @return TableSchema */ private TableSchema getTableSchema() { String tableName = tableDesc.name(); return dbSchema.getTableSchema(tableName); }