blob: 8545b43afa82cc3c8479f57d7acc38186286cae5 [file] [log] [blame]
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.ovsdb.rfc.operations;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.Collection;
21import java.util.Map;
22
23import org.onosproject.ovsdb.rfc.notation.Column;
24import org.onosproject.ovsdb.rfc.notation.Row;
25import org.onosproject.ovsdb.rfc.schema.TableSchema;
26import org.onosproject.ovsdb.rfc.utils.TransValueUtil;
27
28import com.fasterxml.jackson.annotation.JsonIgnore;
29import com.fasterxml.jackson.annotation.JsonProperty;
30import com.google.common.collect.Maps;
31
32/**
33 * insert operation.Refer to RFC 7047 Section 5.2.
34 */
35public final class Insert implements Operation {
36
37 @JsonIgnore
38 private final TableSchema tableSchema;
39 private final String op;
40 @JsonProperty("uuid-name")
41 private final String uuidName;
42 private final Map<String, Object> row;
43
44 /**
45 * Constructs a Insert object.
46 * @param schema TableSchema entity
47 * @param uuidName uuid-name
48 * @param row Row entity
49 */
50 public Insert(TableSchema schema, String uuidName, Row row) {
51 checkNotNull(schema, "TableSchema cannot be null");
52 checkNotNull(uuidName, "uuid name cannot be null");
53 checkNotNull(row, "row cannot be null");
54 this.tableSchema = schema;
55 this.op = Operations.INSERT.op();
56 this.uuidName = uuidName;
57 this.row = Maps.newHashMap();
58 generateOperationRow(row);
59 }
60
61 /**
62 * Row entity convert into the row format of insert operation. Refer to RFC
63 * 7047 Section 5.2.
64 * @param row Row entity
65 */
66 private void generateOperationRow(Row row) {
67 Collection<Column> columns = row.getColumns();
68 for (Column column : columns) {
69 String columnName = column.columnName();
70 Object value = column.data();
71 Object formatValue = TransValueUtil.getFormatData(value);
72 this.row.put(columnName, formatValue);
73 }
74 }
75
76 /**
77 * Returns the uuid-name member of insert operation.
78 * @return the uuid-name member of insert operation
79 */
80 public String getUuidName() {
81 return uuidName;
82 }
83
84 /**
85 * Returns the row member of insert operation.
86 * @return the row member of insert operation
87 */
88 public Map<String, Object> getRow() {
89 return row;
90 }
91
92 @Override
93 public String getOp() {
94 return op;
95 }
96
97 @Override
98 public TableSchema getTableSchema() {
99 return tableSchema;
100 }
101
102 /**
103 * For the use of serialization.
104 * @return the table member of update operation
105 */
106 @JsonProperty
107 public String getTable() {
108 return tableSchema.name();
109 }
110}