Merge 'origin/topic/schema' branch into merge-branch
[ovsdb.git] / northbound / src / main / java / org / opendaylight / ovsdb / northbound / OvsdbRow.java
1 /*
2  * Copyright (C) 2013 Red Hat, Inc.
3  *
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
7  *
8  * Authors : Madhu Venugopal, Brent Salisbury
9  */
10 package org.opendaylight.ovsdb.northbound;
11
12 import java.io.IOException;
13 import java.util.Iterator;
14 import java.util.concurrent.ExecutionException;
15
16 import javax.xml.bind.annotation.XmlElement;
17
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;
22
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;
29
30 public class OvsdbRow {
31     private static final String PARENTUUID = "parent_uuid";
32     private static final String ROW = "row";
33
34     @XmlElement(name=PARENTUUID)
35     String parentUuid;
36
37     String tableName;
38
39     @XmlElement(name=ROW)
40     Row<GenericTableSchema> row;
41
42     public OvsdbRow() {
43     }
44
45     public OvsdbRow(String parentUuid, String tableName, Row<GenericTableSchema> row) {
46         this.parentUuid = parentUuid;
47         this.tableName = tableName;
48         this.row = row;
49     }
50
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();
55
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;
61             try {
62                 row = getRow(client, dbName, tableName, rowNode.get(tableName));
63             } catch (InterruptedException | ExecutionException | IOException e) {
64                 e.printStackTrace();
65                 return null;
66             }
67             return new OvsdbRow(parentUuid, tableName, row);
68         }
69         return null;
70     }
71
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);
79         return row;
80     }
81
82     public String getParentUuid() {
83         return parentUuid;
84     }
85
86     public String getTableName() {
87         return tableName;
88     }
89
90     public Row<GenericTableSchema> getRow() {
91         return row;
92     }
93
94     @Override
95     public String toString() {
96         return "OVSDBRow [parentUuid=" + parentUuid + ", tableName="
97                 + tableName + ", row=" + row + "]";
98     }
99 }