blob: cc3333841a703de2258386ca3b07274a9e897986 [file] [log] [blame]
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07003 *
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.List;
22import java.util.Map;
23
24import org.onosproject.ovsdb.rfc.notation.Column;
25import org.onosproject.ovsdb.rfc.notation.Condition;
26import org.onosproject.ovsdb.rfc.notation.Row;
27import org.onosproject.ovsdb.rfc.schema.TableSchema;
28import org.onosproject.ovsdb.rfc.utils.TransValueUtil;
29
30import com.fasterxml.jackson.annotation.JsonIgnore;
31import com.fasterxml.jackson.annotation.JsonProperty;
32import com.google.common.collect.Maps;
33
34/**
35 * update operation.Refer to RFC 7047 Section 5.2.
36 */
37public final class Update implements Operation {
38
39 @JsonIgnore
40 private final TableSchema tableSchema;
41 private final String op;
42 private final Map<String, Object> row;
43 private final List<Condition> where;
44
45 /**
46 * Constructs a Update object.
47 * @param schema TableSchema entity
48 * @param row Row entity
49 * @param where the List of Condition entity
50 */
51 public Update(TableSchema schema, Row row, List<Condition> where) {
52 checkNotNull(schema, "TableSchema cannot be null");
53 checkNotNull(row, "row cannot be null");
54 checkNotNull(where, "where cannot be null");
55 this.tableSchema = schema;
56 this.op = Operations.UPDATE.op();
57 this.row = Maps.newHashMap();
58 this.where = where;
59 generateOperationRow(row);
60 }
61
62 /**
63 * Row entity convert into the row format of update operation. Refer to RFC
64 * 7047 Section 5.2.
65 * @param row Row entity
66 */
67 private void generateOperationRow(Row row) {
68 Collection<Column> columns = row.getColumns();
69 for (Column column : columns) {
70 String columnName = column.columnName();
71 Object value = column.data();
72 Object formatValue = TransValueUtil.getFormatData(value);
73 this.row.put(columnName, formatValue);
74 }
75 }
76
77 /**
78 * Returns the row member of update operation.
79 * @return the row member of update operation
80 */
81 public Map<String, Object> getRow() {
82 return row;
83 }
84
85 /**
86 * Returns the where member of update operation.
87 * @return the where member of update operation
88 */
89 public List<Condition> getWhere() {
90 return where;
91 }
92
93 @Override
94 public String getOp() {
95 return op;
96 }
97
98 @Override
99 public TableSchema getTableSchema() {
100 return tableSchema;
101 }
102
103 /**
104 * For the use of serialization.
105 * @return the table member of update operation
106 */
107 @JsonProperty
108 public String getTable() {
109 return tableSchema.name();
110 }
111}