2 * Copyright (c) 2013, 2015 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.northbound;
11 import java.io.IOException;
12 import java.util.Iterator;
13 import java.util.concurrent.ExecutionException;
15 import org.opendaylight.ovsdb.lib.OvsdbClient;
16 import org.opendaylight.ovsdb.lib.notation.Row;
17 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
18 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import com.fasterxml.jackson.databind.JsonNode;
23 import com.fasterxml.jackson.databind.node.ObjectNode;
25 public class OvsdbRow {
26 private static final Logger LOG = LoggerFactory.getLogger(OvsdbRow.class);
27 private static final String PARENTUUID = "parent_uuid";
28 private static final String PARENTTABLE = "parent_table";
29 private static final String PARENTCOLUMN = "parent_column";
30 private static final String ROW = "row";
32 private String parentUuid;
33 private String parentTable;
34 private String parentColumn;
35 private String tableName;
36 private Row<GenericTableSchema> row;
41 public OvsdbRow(String parentUuid, String tableName, Row<GenericTableSchema> row) {
42 this.parentUuid = parentUuid;
43 this.tableName = tableName;
47 public OvsdbRow(String parentTable, String parentUuid, String parentColumn, String tableName, Row<GenericTableSchema> row) {
48 this.parentTable = parentTable;
49 this.parentColumn = parentColumn;
50 this.parentUuid = parentUuid;
51 this.tableName = tableName;
55 public static OvsdbRow fromJsonNode(OvsdbClient client, String dbName, JsonNode json) {
56 JsonNode parentUuidNode = json.get(PARENTUUID);
57 String parentUuid = null;
58 if (parentUuidNode != null) {
59 parentUuid = parentUuidNode.asText();
61 JsonNode parentTableNode = json.get(PARENTTABLE);
62 String parentTable = null;
63 if (parentTableNode != null) {
64 parentTable = parentTableNode.asText();
66 JsonNode parentColumnNode = json.get(PARENTCOLUMN);
67 String parentColumn = null;
68 if (parentColumnNode != null) {
69 parentColumn = parentColumnNode.asText();
71 JsonNode rowNode = json.get(ROW);
72 if (rowNode == null) {
75 Iterator<String> fieldNames = rowNode.fieldNames();
76 if (fieldNames.hasNext()) {
77 String tableName = fieldNames.next();
79 Row<GenericTableSchema> row = getRow(client, dbName, tableName, rowNode.get(tableName));
80 return new OvsdbRow(parentTable, parentUuid, parentColumn, tableName, row);
81 } catch (InterruptedException | ExecutionException | IOException e) {
82 LOG.error("Error retrieving the row for {}", tableName, e);
89 public static Row<GenericTableSchema> getRow(OvsdbClient client, String dbName, String tableName, JsonNode rowJson) throws InterruptedException, ExecutionException, IOException {
90 DatabaseSchema dbSchema = client.getSchema(dbName).get();
91 GenericTableSchema schema = dbSchema.table(tableName, GenericTableSchema.class);
92 return schema.createRow((ObjectNode)rowJson);
95 public String getParentTable() {
99 public String getParentUuid() {
103 public String getParentColumn() {
107 public String getTableName() {
111 public Row<GenericTableSchema> getRow() {