blob: 1802e511792d4d919bdd7375c8dbd4d0364e1c6e [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
2 * Copyright 2014 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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.service.impl;
Madan Jampani12390c12014-11-12 00:35:56 -080017
Brian O'Connorabafb502014-12-02 22:26:20 -080018import org.onosproject.store.service.VersionedValue;
Madan Jampani9b37d572014-11-12 11:53:24 -080019
Madan Jampanif5d263b2014-11-13 10:04:40 -080020import com.google.common.base.MoreObjects;
21
Madan Jampani44e6a542014-11-12 01:06:51 -080022/**
23 * A table modification event.
24 */
25public final class TableModificationEvent {
Madan Jampani12390c12014-11-12 00:35:56 -080026
Madan Jampani44e6a542014-11-12 01:06:51 -080027 /**
28 * Type of table modification event.
Madan Jampani44e6a542014-11-12 01:06:51 -080029 */
Madan Jampani12390c12014-11-12 00:35:56 -080030 public enum Type {
31 ROW_ADDED,
32 ROW_DELETED,
33 ROW_UPDATED
34 }
Madan Jampani44e6a542014-11-12 01:06:51 -080035
Madan Jampani12390c12014-11-12 00:35:56 -080036 private final String tableName;
37 private final String key;
Madan Jampani9b37d572014-11-12 11:53:24 -080038 private final VersionedValue value;
Madan Jampani12390c12014-11-12 00:35:56 -080039 private final Type type;
40
Madan Jampani44e6a542014-11-12 01:06:51 -080041 /**
42 * Creates a new row deleted table modification event.
43 * @param tableName table name.
44 * @param key row key
Madan Jampani9b37d572014-11-12 11:53:24 -080045 * @param value value associated with the key when it was deleted.
Madan Jampani44e6a542014-11-12 01:06:51 -080046 * @return table modification event.
47 */
Madan Jampani9b37d572014-11-12 11:53:24 -080048 public static TableModificationEvent rowDeleted(String tableName, String key, VersionedValue value) {
49 return new TableModificationEvent(tableName, key, value, Type.ROW_DELETED);
Madan Jampani12390c12014-11-12 00:35:56 -080050 }
Madan Jampani44e6a542014-11-12 01:06:51 -080051
52 /**
53 * Creates a new row added table modification event.
54 * @param tableName table name.
55 * @param key row key
Madan Jampani9b37d572014-11-12 11:53:24 -080056 * @param value value associated with the key
Madan Jampani44e6a542014-11-12 01:06:51 -080057 * @return table modification event.
58 */
Madan Jampani9b37d572014-11-12 11:53:24 -080059 public static TableModificationEvent rowAdded(String tableName, String key, VersionedValue value) {
60 return new TableModificationEvent(tableName, key, value, Type.ROW_ADDED);
Madan Jampani12390c12014-11-12 00:35:56 -080061 }
Madan Jampani44e6a542014-11-12 01:06:51 -080062
63 /**
64 * Creates a new row updated table modification event.
65 * @param tableName table name.
66 * @param key row key
Madan Jampani9b37d572014-11-12 11:53:24 -080067 * @param newValue value
Madan Jampani44e6a542014-11-12 01:06:51 -080068 * @return table modification event.
69 */
Madan Jampani9b37d572014-11-12 11:53:24 -080070 public static TableModificationEvent rowUpdated(String tableName, String key, VersionedValue newValue) {
71 return new TableModificationEvent(tableName, key, newValue, Type.ROW_UPDATED);
Madan Jampani12390c12014-11-12 00:35:56 -080072 }
73
Madan Jampani9b37d572014-11-12 11:53:24 -080074 private TableModificationEvent(String tableName, String key, VersionedValue value, Type type) {
Madan Jampani12390c12014-11-12 00:35:56 -080075 this.tableName = tableName;
76 this.key = key;
Madan Jampani9b37d572014-11-12 11:53:24 -080077 this.value = value;
Madan Jampani12390c12014-11-12 00:35:56 -080078 this.type = type;
79 }
80
Madan Jampani44e6a542014-11-12 01:06:51 -080081 /**
82 * Returns name of table this event is for.
83 * @return table name
84 */
Madan Jampani12390c12014-11-12 00:35:56 -080085 public String tableName() {
86 return tableName;
87 }
88
Madan Jampani44e6a542014-11-12 01:06:51 -080089 /**
90 * Returns the row key this event is for.
91 * @return row key
92 */
Madan Jampani12390c12014-11-12 00:35:56 -080093 public String key() {
94 return key;
95 }
96
Madan Jampani44e6a542014-11-12 01:06:51 -080097 /**
Madan Jampani9b37d572014-11-12 11:53:24 -080098 * Returns the value associated with the key. If the event for a deletion, this
99 * method returns value that was deleted.
100 * @return row value
101 */
102 public VersionedValue value() {
103 return value;
104 }
105
106 /**
Madan Jampani44e6a542014-11-12 01:06:51 -0800107 * Returns the type of table modification event.
108 * @return event type.
109 */
Madan Jampani12390c12014-11-12 00:35:56 -0800110 public Type type() {
111 return type;
112 }
Madan Jampanif5d263b2014-11-13 10:04:40 -0800113
114 @Override
115 public String toString() {
116 return MoreObjects.toStringHelper(getClass())
117 .add("type", type)
118 .add("tableName", tableName)
119 .add("key", key)
120 .add("version", value.version())
121 .toString();
122 }
Madan Jampani12390c12014-11-12 00:35:56 -0800123}