blob: 31dcaab8fba90c865efafff749bfcb9fc9846e2f [file] [log] [blame]
Madan Jampani12390c12014-11-12 00:35:56 -08001package org.onlab.onos.store.service.impl;
2
Madan Jampani9b37d572014-11-12 11:53:24 -08003import org.onlab.onos.store.service.VersionedValue;
4
Madan Jampanif5d263b2014-11-13 10:04:40 -08005import com.google.common.base.MoreObjects;
6
Madan Jampani44e6a542014-11-12 01:06:51 -08007/**
8 * A table modification event.
9 */
10public final class TableModificationEvent {
Madan Jampani12390c12014-11-12 00:35:56 -080011
Madan Jampani44e6a542014-11-12 01:06:51 -080012 /**
13 * Type of table modification event.
Madan Jampani44e6a542014-11-12 01:06:51 -080014 */
Madan Jampani12390c12014-11-12 00:35:56 -080015 public enum Type {
16 ROW_ADDED,
17 ROW_DELETED,
18 ROW_UPDATED
19 }
Madan Jampani44e6a542014-11-12 01:06:51 -080020
Madan Jampani12390c12014-11-12 00:35:56 -080021 private final String tableName;
22 private final String key;
Madan Jampani9b37d572014-11-12 11:53:24 -080023 private final VersionedValue value;
Madan Jampani12390c12014-11-12 00:35:56 -080024 private final Type type;
25
Madan Jampani44e6a542014-11-12 01:06:51 -080026 /**
27 * Creates a new row deleted table modification event.
28 * @param tableName table name.
29 * @param key row key
Madan Jampani9b37d572014-11-12 11:53:24 -080030 * @param value value associated with the key when it was deleted.
Madan Jampani44e6a542014-11-12 01:06:51 -080031 * @return table modification event.
32 */
Madan Jampani9b37d572014-11-12 11:53:24 -080033 public static TableModificationEvent rowDeleted(String tableName, String key, VersionedValue value) {
34 return new TableModificationEvent(tableName, key, value, Type.ROW_DELETED);
Madan Jampani12390c12014-11-12 00:35:56 -080035 }
Madan Jampani44e6a542014-11-12 01:06:51 -080036
37 /**
38 * Creates a new row added table modification event.
39 * @param tableName table name.
40 * @param key row key
Madan Jampani9b37d572014-11-12 11:53:24 -080041 * @param value value associated with the key
Madan Jampani44e6a542014-11-12 01:06:51 -080042 * @return table modification event.
43 */
Madan Jampani9b37d572014-11-12 11:53:24 -080044 public static TableModificationEvent rowAdded(String tableName, String key, VersionedValue value) {
45 return new TableModificationEvent(tableName, key, value, Type.ROW_ADDED);
Madan Jampani12390c12014-11-12 00:35:56 -080046 }
Madan Jampani44e6a542014-11-12 01:06:51 -080047
48 /**
49 * Creates a new row updated table modification event.
50 * @param tableName table name.
51 * @param key row key
Madan Jampani9b37d572014-11-12 11:53:24 -080052 * @param newValue value
Madan Jampani44e6a542014-11-12 01:06:51 -080053 * @return table modification event.
54 */
Madan Jampani9b37d572014-11-12 11:53:24 -080055 public static TableModificationEvent rowUpdated(String tableName, String key, VersionedValue newValue) {
56 return new TableModificationEvent(tableName, key, newValue, Type.ROW_UPDATED);
Madan Jampani12390c12014-11-12 00:35:56 -080057 }
58
Madan Jampani9b37d572014-11-12 11:53:24 -080059 private TableModificationEvent(String tableName, String key, VersionedValue value, Type type) {
Madan Jampani12390c12014-11-12 00:35:56 -080060 this.tableName = tableName;
61 this.key = key;
Madan Jampani9b37d572014-11-12 11:53:24 -080062 this.value = value;
Madan Jampani12390c12014-11-12 00:35:56 -080063 this.type = type;
64 }
65
Madan Jampani44e6a542014-11-12 01:06:51 -080066 /**
67 * Returns name of table this event is for.
68 * @return table name
69 */
Madan Jampani12390c12014-11-12 00:35:56 -080070 public String tableName() {
71 return tableName;
72 }
73
Madan Jampani44e6a542014-11-12 01:06:51 -080074 /**
75 * Returns the row key this event is for.
76 * @return row key
77 */
Madan Jampani12390c12014-11-12 00:35:56 -080078 public String key() {
79 return key;
80 }
81
Madan Jampani44e6a542014-11-12 01:06:51 -080082 /**
Madan Jampani9b37d572014-11-12 11:53:24 -080083 * Returns the value associated with the key. If the event for a deletion, this
84 * method returns value that was deleted.
85 * @return row value
86 */
87 public VersionedValue value() {
88 return value;
89 }
90
91 /**
Madan Jampani44e6a542014-11-12 01:06:51 -080092 * Returns the type of table modification event.
93 * @return event type.
94 */
Madan Jampani12390c12014-11-12 00:35:56 -080095 public Type type() {
96 return type;
97 }
Madan Jampanif5d263b2014-11-13 10:04:40 -080098
99 @Override
100 public String toString() {
101 return MoreObjects.toStringHelper(getClass())
102 .add("type", type)
103 .add("tableName", tableName)
104 .add("key", key)
105 .add("version", value.version())
106 .toString();
107 }
Madan Jampani12390c12014-11-12 00:35:56 -0800108}