Merge "Initial pass at netvirt model"
[netvirt.git] / northbound / src / main / java / org / opendaylight / ovsdb / northbound / OvsdbRow.java
1 /*
2  * Copyright (c) 2013, 2015 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.northbound;
10
11 import java.io.IOException;
12 import java.util.Iterator;
13 import java.util.concurrent.ExecutionException;
14
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;
21
22 import com.fasterxml.jackson.databind.JsonNode;
23 import com.fasterxml.jackson.databind.node.ObjectNode;
24
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";
31
32     private String parentUuid;
33     private String parentTable;
34     private String parentColumn;
35     private String tableName;
36     private Row<GenericTableSchema> row;
37
38     public OvsdbRow() {
39     }
40
41     public OvsdbRow(String parentUuid, String tableName, Row<GenericTableSchema> row) {
42         this.parentUuid = parentUuid;
43         this.tableName = tableName;
44         this.row = row;
45     }
46
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;
52         this.row = row;
53     }
54
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();
60         }
61         JsonNode parentTableNode = json.get(PARENTTABLE);
62         String parentTable = null;
63         if (parentTableNode != null) {
64             parentTable = parentTableNode.asText();
65         }
66         JsonNode parentColumnNode = json.get(PARENTCOLUMN);
67         String parentColumn = null;
68         if (parentColumnNode != null) {
69             parentColumn = parentColumnNode.asText();
70         }
71         JsonNode rowNode = json.get(ROW);
72         if (rowNode == null) {
73             return null;
74         }
75         Iterator<String> fieldNames = rowNode.fieldNames();
76         if (fieldNames.hasNext()) {
77             String tableName = fieldNames.next();
78             try {
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);
83                 return null;
84             }
85         }
86         return null;
87     }
88
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);
93     }
94
95     public String getParentTable() {
96         return parentTable;
97     }
98
99     public String getParentUuid() {
100         return parentUuid;
101     }
102
103     public String getParentColumn() {
104         return parentColumn;
105     }
106
107     public String getTableName() {
108         return tableName;
109     }
110
111     public Row<GenericTableSchema> getRow() {
112         return row;
113     }
114 }