2 * Copyright (C) 2013 Red Hat, Inc.
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
8 * Authors : Madhu Venugopal, Brent Salisbury
10 package org.opendaylight.ovsdb.northbound;
12 import java.io.IOException;
13 import java.util.Iterator;
14 import java.util.concurrent.ExecutionException;
16 import javax.xml.bind.annotation.XmlElement;
18 import org.opendaylight.ovsdb.lib.OvsdbClient;
19 import org.opendaylight.ovsdb.lib.notation.Row;
20 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
21 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
23 import com.fasterxml.jackson.annotation.JsonInclude.Include;
24 import com.fasterxml.jackson.core.JsonParseException;
25 import com.fasterxml.jackson.databind.DeserializationFeature;
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.fasterxml.jackson.databind.node.ObjectNode;
30 public class OvsdbRow {
31 private static final String PARENTUUID = "parent_uuid";
32 private static final String ROW = "row";
34 @XmlElement(name=PARENTUUID)
40 Row<GenericTableSchema> row;
45 public OvsdbRow(String parentUuid, String tableName, Row<GenericTableSchema> row) {
46 this.parentUuid = parentUuid;
47 this.tableName = tableName;
51 public static OvsdbRow fromJsonNode(OvsdbClient client, String dbName, JsonNode json) {
52 JsonNode parentUuidNode = json.get(PARENTUUID);
53 String parentUuid = null;
54 if (parentUuidNode != null) parentUuid = parentUuidNode.asText();
56 JsonNode rowNode = json.get(ROW);
57 if (rowNode == null) return null;
58 for(Iterator<String> fieldNames = rowNode.fieldNames(); fieldNames.hasNext();) {
59 String tableName = fieldNames.next();
60 Row<GenericTableSchema> row = null;
62 row = getRow(client, dbName, tableName, rowNode.get(tableName));
63 } catch (InterruptedException | ExecutionException | IOException e) {
67 return new OvsdbRow(parentUuid, tableName, row);
72 public static Row<GenericTableSchema> getRow(OvsdbClient client, String dbName, String tableName, JsonNode rowJson) throws InterruptedException, ExecutionException, JsonParseException, IOException {
73 DatabaseSchema dbSchema = client.getSchema(dbName).get();
74 GenericTableSchema schema = dbSchema.table(tableName, GenericTableSchema.class);
75 ObjectMapper objectMapper = new ObjectMapper();
76 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
77 objectMapper.setSerializationInclusion(Include.NON_NULL);
78 Row<GenericTableSchema> row = schema.createRow((ObjectNode)rowJson);
82 public String getParentUuid() {
86 public String getTableName() {
90 public Row<GenericTableSchema> getRow() {
95 public String toString() {
96 return "OVSDBRow [parentUuid=" + parentUuid + ", tableName="
97 + tableName + ", row=" + row + "]";