Resolved the Serialization and Deserialization issues for MultiValued Columns with...
[ovsdb.git] / library / src / main / java / org / opendaylight / ovsdb / lib / operations / Insert.java
1 /*
2  * Copyright (C) 2014 EBay Software Foundation
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 : Ashwin Raveendran
9  */
10 package org.opendaylight.ovsdb.lib.operations;
11
12 import java.util.Map;
13 import java.util.Set;
14
15 import org.opendaylight.ovsdb.lib.notation.OvsDBMap;
16 import org.opendaylight.ovsdb.lib.notation.OvsDBSet;
17 import org.opendaylight.ovsdb.lib.schema.ColumnSchema;
18 import org.opendaylight.ovsdb.lib.schema.TableSchema;
19
20 import com.fasterxml.jackson.annotation.JsonProperty;
21 import com.google.common.collect.Maps;
22
23
24 public class Insert<E extends TableSchema<E>> extends Operation<E> {
25
26     public static final String INSERT = "insert";
27
28     String uuid;
29
30     @JsonProperty("uuid-name")
31     private String uuidName;
32
33     private Map<String, Object> row = Maps.newHashMap();
34
35     public Insert<E> on(TableSchema schema){
36         this.setTableSchema(schema);
37         return this;
38     }
39
40     public Insert<E> withId(String name) {
41         this.uuidName = name;
42         this.setOp(INSERT);
43         return this;
44     }
45
46
47     public Insert(TableSchema<E> schema) {
48         super(schema, INSERT);
49     }
50
51     public <D, C extends TableSchema<C>> Insert<E> value(ColumnSchema<C, D> columnSchema, D value) {
52         Object untypedValue = null;
53         if (columnSchema.getType().isMultiValued()) {
54             if (value instanceof Set) {
55                 untypedValue = OvsDBSet.fromSet((Set) value);
56             } else if (value instanceof Map) {
57                 untypedValue = OvsDBMap.fromMap((Map)value);
58             }
59         } else {
60             untypedValue = value;
61         }
62         row.put(columnSchema.getName(), untypedValue);
63         return this;
64     }
65
66     public String getUuid() {
67         return uuid;
68     }
69
70     public void setUuid(String uuid) {
71         this.uuid = uuid;
72     }
73
74     public String getUuidName() {
75         return uuidName;
76     }
77
78     public void setUuidName(String uuidName) {
79         this.uuidName = uuidName;
80     }
81
82     public Map<String, Object> getRow() {
83         return row;
84     }
85
86     public void setRow(Map<String, Object> row) {
87         this.row = row;
88     }
89
90 }