Removed unused ObjectMapper which is potentially confusing
[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.core.JsonParseException;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.fasterxml.jackson.databind.node.ObjectNode;
26
27 @Deprecated
28 public class OvsdbRow {
29     private static final String PARENTUUID = "parent_uuid";
30     private static final String ROW = "row";
31
32     @XmlElement(name=PARENTUUID)
33     String parentUuid;
34
35     String tableName;
36
37     @XmlElement(name=ROW)
38     Row<GenericTableSchema> row;
39
40     public OvsdbRow() {
41     }
42
43     public OvsdbRow(String parentUuid, String tableName, Row<GenericTableSchema> row) {
44         this.parentUuid = parentUuid;
45         this.tableName = tableName;
46         this.row = row;
47     }
48
49     public static OvsdbRow fromJsonNode(OvsdbClient client, String dbName, JsonNode json) {
50         JsonNode parentUuidNode = json.get(PARENTUUID);
51         String parentUuid = null;
52         if (parentUuidNode != null) parentUuid = parentUuidNode.asText();
53
54         JsonNode rowNode = json.get(ROW);
55         if (rowNode == null) return null;
56         for(Iterator<String> fieldNames = rowNode.fieldNames(); fieldNames.hasNext();) {
57             String tableName = fieldNames.next();
58             Row<GenericTableSchema> row = null;
59             try {
60                 row = getRow(client, dbName, tableName, rowNode.get(tableName));
61             } catch (InterruptedException | ExecutionException | IOException e) {
62                 e.printStackTrace();
63                 return null;
64             }
65             return new OvsdbRow(parentUuid, tableName, row);
66         }
67         return null;
68     }
69
70     public static Row<GenericTableSchema> getRow(OvsdbClient client, String dbName, String tableName, JsonNode rowJson) throws InterruptedException, ExecutionException, JsonParseException, IOException {
71         DatabaseSchema dbSchema = client.getSchema(dbName).get();
72         GenericTableSchema schema = dbSchema.table(tableName, GenericTableSchema.class);
73         return schema.createRow((ObjectNode)rowJson);
74     }
75
76     public String getParentUuid() {
77         return parentUuid;
78     }
79
80     public String getTableName() {
81         return tableName;
82     }
83
84     public Row<GenericTableSchema> getRow() {
85         return row;
86     }
87
88     @Override
89     public String toString() {
90         return "OVSDBRow [parentUuid=" + parentUuid + ", tableName="
91                 + tableName + ", row=" + row + "]";
92     }
93 }