1 /*
2   Copyright (c) DataStax, Inc.
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 */
16 
17 #include "event_response.hpp"
18 
19 #include "serialization.hpp"
20 
21 using namespace datastax::internal::core;
22 
decode(Decoder & decoder)23 bool EventResponse::decode(Decoder& decoder) {
24   decoder.set_type("event");
25   StringRef event_type;
26   CHECK_RESULT(decoder.decode_string(&event_type));
27 
28   if (event_type == "TOPOLOGY_CHANGE") {
29     event_type_ = CASS_EVENT_TOPOLOGY_CHANGE;
30 
31     StringRef topology_change;
32     CHECK_RESULT(decoder.decode_string(&topology_change));
33     if (topology_change == "NEW_NODE") {
34       topology_change_ = NEW_NODE;
35     } else if (topology_change == "REMOVED_NODE") {
36       topology_change_ = REMOVED_NODE;
37     } else if (topology_change == "MOVED_NODE") {
38       topology_change_ = MOVED_NODE;
39     } else {
40       return false;
41     }
42     CHECK_RESULT(decoder.decode_inet(&affected_node_));
43   } else if (event_type == "STATUS_CHANGE") {
44     event_type_ = CASS_EVENT_STATUS_CHANGE;
45 
46     StringRef status_change;
47     CHECK_RESULT(decoder.decode_string(&status_change));
48     if (status_change == "UP") {
49       status_change_ = UP;
50     } else if (status_change == "DOWN") {
51       status_change_ = DOWN;
52     } else {
53       return false;
54     }
55     CHECK_RESULT(decoder.decode_inet(&affected_node_));
56   } else if (event_type == "SCHEMA_CHANGE") {
57     event_type_ = CASS_EVENT_SCHEMA_CHANGE;
58 
59     // Version 1+: <change>... (all start with change type)
60     StringRef schema_change;
61     CHECK_RESULT(decoder.decode_string(&schema_change));
62     if (schema_change == "CREATED") {
63       schema_change_ = CREATED;
64     } else if (schema_change == "UPDATED") {
65       schema_change_ = UPDATED;
66     } else if (schema_change == "DROPPED") {
67       schema_change_ = DROPPED;
68     } else {
69       return false;
70     }
71 
72     // Version 3+: ...<target><options>
73     // <target> = [string]
74     // <options> = [string] OR [string][string]
75 
76     StringRef target;
77     CHECK_RESULT(decoder.decode_string(&target));
78     if (target == "KEYSPACE") {
79       schema_change_target_ = KEYSPACE;
80     } else if (target == "TABLE") {
81       schema_change_target_ = TABLE;
82     } else if (target == "TYPE") {
83       schema_change_target_ = TYPE;
84     } else if (target == "FUNCTION") {
85       schema_change_target_ = FUNCTION;
86     } else if (target == "AGGREGATE") {
87       schema_change_target_ = AGGREGATE;
88     } else {
89       return false;
90     }
91 
92     CHECK_RESULT(decoder.decode_string(&keyspace_));
93 
94     if (schema_change_target_ == TABLE || schema_change_target_ == TYPE) {
95       CHECK_RESULT(decoder.decode_string(&target_));
96     } else if (schema_change_target_ == FUNCTION || schema_change_target_ == AGGREGATE) {
97       CHECK_RESULT(decoder.decode_string(&target_));
98       CHECK_RESULT(decoder.decode_stringlist(arg_types_));
99     }
100   } else {
101     return false;
102   }
103 
104   decoder.maybe_log_remaining();
105   return true;
106 }
107