]>
Commit | Line | Data |
---|---|---|
9a1955a7 | 1 | /* |
406b689d | 2 | * Copyright (c) 2015-2020 Red Hat, Inc. |
9a1955a7 JF |
3 | * |
4 | * All rights reserved. | |
5 | * | |
6 | * Author: Jan Friesse (jfriesse@redhat.com) | |
7 | * | |
8 | * This software licensed under BSD license, the text of which follows: | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions are met: | |
12 | * | |
13 | * - Redistributions of source code must retain the above copyright notice, | |
14 | * this list of conditions and the following disclaimer. | |
15 | * - Redistributions in binary form must reproduce the above copyright notice, | |
16 | * this list of conditions and the following disclaimer in the documentation | |
17 | * and/or other materials provided with the distribution. | |
18 | * - Neither the name of the Red Hat, Inc. nor the names of its | |
19 | * contributors may be used to endorse or promote products derived from this | |
20 | * software without specific prior written permission. | |
21 | * | |
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
32 | * THE POSSIBILITY OF SUCH DAMAGE. | |
33 | */ | |
34 | ||
313d42d1 | 35 | #include "log.h" |
9a1955a7 JF |
36 | #include "qdevice-model.h" |
37 | #include "qdevice-model-net.h" | |
38 | ||
39 | static struct qdevice_model *qdevice_model_array[QDEVICE_MODEL_TYPE_ARRAY_SIZE]; | |
40 | ||
41 | int | |
42 | qdevice_model_init(struct qdevice_instance *instance) | |
43 | { | |
44 | ||
45 | if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE || | |
46 | qdevice_model_array[instance->model_type] == NULL) { | |
c8d19612 | 47 | log(LOG_CRIT, "qdevice_model_init unhandled model"); |
406b689d | 48 | exit(EXIT_FAILURE); |
9a1955a7 JF |
49 | } |
50 | ||
51 | return (qdevice_model_array[instance->model_type]->init(instance)); | |
52 | } | |
53 | ||
54 | int | |
55 | qdevice_model_destroy(struct qdevice_instance *instance) | |
56 | { | |
57 | ||
58 | if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE || | |
59 | qdevice_model_array[instance->model_type] == NULL) { | |
c8d19612 | 60 | log(LOG_CRIT, "qdevice_model_destroy unhandled model"); |
406b689d | 61 | exit(EXIT_FAILURE); |
9a1955a7 JF |
62 | } |
63 | ||
64 | return (qdevice_model_array[instance->model_type]->destroy(instance)); | |
65 | } | |
66 | ||
67 | int | |
68 | qdevice_model_run(struct qdevice_instance *instance) | |
69 | { | |
70 | ||
71 | if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE || | |
72 | qdevice_model_array[instance->model_type] == NULL) { | |
c8d19612 | 73 | log(LOG_CRIT, "qdevice_model_run unhandled model"); |
406b689d | 74 | exit(EXIT_FAILURE); |
9a1955a7 JF |
75 | } |
76 | ||
77 | return (qdevice_model_array[instance->model_type]->run(instance)); | |
78 | } | |
79 | ||
80 | int | |
81 | qdevice_model_get_config_node_list_failed(struct qdevice_instance *instance) | |
82 | { | |
83 | ||
84 | if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE || | |
85 | qdevice_model_array[instance->model_type] == NULL) { | |
c8d19612 | 86 | log(LOG_CRIT, "qdevice_model_run unhandled model"); |
406b689d | 87 | exit(EXIT_FAILURE); |
9a1955a7 JF |
88 | } |
89 | ||
90 | return (qdevice_model_array[instance->model_type]->get_config_node_list_failed(instance)); | |
91 | } | |
92 | ||
93 | int | |
94 | qdevice_model_config_node_list_changed(struct qdevice_instance *instance, | |
95 | const struct node_list *nlist, int config_version_set, uint64_t config_version) | |
96 | { | |
97 | ||
98 | if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE || | |
99 | qdevice_model_array[instance->model_type] == NULL) { | |
c8d19612 | 100 | log(LOG_CRIT, "qdevice_model_run unhandled model"); |
406b689d | 101 | exit(EXIT_FAILURE); |
9a1955a7 JF |
102 | } |
103 | ||
104 | return (qdevice_model_array[instance->model_type]-> | |
105 | config_node_list_changed(instance, nlist, config_version_set, config_version)); | |
106 | } | |
107 | ||
108 | int | |
109 | qdevice_model_votequorum_quorum_notify(struct qdevice_instance *instance, | |
110 | uint32_t quorate, uint32_t node_list_entries, votequorum_node_t node_list[]) | |
111 | { | |
112 | ||
113 | if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE || | |
114 | qdevice_model_array[instance->model_type] == NULL) { | |
c8d19612 | 115 | log(LOG_CRIT, "qdevice_model_votequorum_quorum_notify unhandled model"); |
406b689d | 116 | exit(EXIT_FAILURE); |
9a1955a7 JF |
117 | } |
118 | ||
119 | return (qdevice_model_array[instance->model_type]-> | |
120 | votequorum_quorum_notify(instance, quorate, node_list_entries, node_list)); | |
121 | } | |
122 | ||
123 | int | |
124 | qdevice_model_votequorum_node_list_notify(struct qdevice_instance *instance, | |
125 | votequorum_ring_id_t votequorum_ring_id, uint32_t node_list_entries, uint32_t node_list[]) | |
126 | { | |
127 | ||
128 | if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE || | |
129 | qdevice_model_array[instance->model_type] == NULL) { | |
c8d19612 | 130 | log(LOG_CRIT, "qdevice_model_votequorum_node_list_notify unhandled model"); |
406b689d | 131 | exit(EXIT_FAILURE); |
9a1955a7 JF |
132 | } |
133 | ||
134 | return (qdevice_model_array[instance->model_type]-> | |
135 | votequorum_node_list_notify(instance, votequorum_ring_id, node_list_entries, node_list)); | |
136 | } | |
137 | ||
138 | int | |
139 | qdevice_model_votequorum_node_list_heuristics_notify(struct qdevice_instance *instance, | |
140 | votequorum_ring_id_t votequorum_ring_id, uint32_t node_list_entries, uint32_t node_list[], | |
141 | enum qdevice_heuristics_exec_result heuristics_exec_result) | |
142 | { | |
143 | ||
144 | if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE || | |
145 | qdevice_model_array[instance->model_type] == NULL) { | |
c8d19612 | 146 | log(LOG_CRIT, "qdevice_model_votequorum_node_list_heuristics_notify unhandled model"); |
406b689d | 147 | exit(EXIT_FAILURE); |
9a1955a7 JF |
148 | } |
149 | ||
150 | return (qdevice_model_array[instance->model_type]-> | |
151 | votequorum_node_list_heuristics_notify(instance, votequorum_ring_id, node_list_entries, | |
152 | node_list, heuristics_exec_result)); | |
153 | } | |
154 | ||
155 | int | |
156 | qdevice_model_votequorum_expected_votes_notify(struct qdevice_instance *instance, | |
157 | uint32_t expected_votes) | |
158 | { | |
159 | ||
160 | if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE || | |
161 | qdevice_model_array[instance->model_type] == NULL) { | |
c8d19612 | 162 | log(LOG_CRIT, "qdevice_model_votequorum_expected_votes_notify unhandled model"); |
406b689d | 163 | exit(EXIT_FAILURE); |
9a1955a7 JF |
164 | } |
165 | ||
166 | return (qdevice_model_array[instance->model_type]-> | |
167 | votequorum_expected_votes_notify(instance, expected_votes)); | |
168 | } | |
169 | ||
170 | int | |
171 | qdevice_model_ipc_cmd_status(struct qdevice_instance *instance, struct dynar *outbuf, int verbose) | |
172 | { | |
173 | ||
174 | if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE || | |
175 | qdevice_model_array[instance->model_type] == NULL) { | |
c8d19612 | 176 | log(LOG_CRIT, "qdevice_model_ipc_cmd_status unhandled model"); |
406b689d | 177 | exit(EXIT_FAILURE); |
9a1955a7 JF |
178 | } |
179 | ||
180 | return (qdevice_model_array[instance->model_type]-> | |
181 | ipc_cmd_status(instance, outbuf, verbose)); | |
182 | } | |
183 | ||
184 | int | |
185 | qdevice_model_cmap_changed(struct qdevice_instance *instance, | |
186 | const struct qdevice_cmap_change_events *events) | |
187 | { | |
188 | ||
189 | if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE || | |
190 | qdevice_model_array[instance->model_type] == NULL) { | |
c8d19612 | 191 | log(LOG_CRIT, "qdevice_model_cmap_chaged unhandled model"); |
406b689d | 192 | exit(EXIT_FAILURE); |
9a1955a7 JF |
193 | } |
194 | ||
195 | return (qdevice_model_array[instance->model_type]-> | |
196 | cmap_changed(instance, events)); | |
197 | } | |
198 | ||
199 | int | |
200 | qdevice_model_register(enum qdevice_model_type model_type, | |
201 | struct qdevice_model *model) | |
202 | { | |
203 | ||
204 | if (model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE) { | |
205 | return (-1); | |
206 | } | |
207 | ||
208 | if (qdevice_model_array[model_type] != NULL) { | |
209 | return (-1); | |
210 | } | |
211 | ||
212 | qdevice_model_array[model_type] = model; | |
213 | ||
214 | return (0); | |
215 | } | |
216 | ||
217 | void | |
218 | qdevice_model_register_all(void) | |
219 | { | |
220 | ||
221 | if (qdevice_model_net_register() != 0) { | |
c8d19612 | 222 | log(LOG_CRIT, "Failed to register model 'net' "); |
406b689d | 223 | exit(EXIT_FAILURE); |
9a1955a7 JF |
224 | } |
225 | } | |
226 | ||
227 | int | |
228 | qdevice_model_str_to_type(const char *str, enum qdevice_model_type *model_type) | |
229 | { | |
230 | int i; | |
231 | ||
232 | for (i = 0; i < QDEVICE_MODEL_TYPE_ARRAY_SIZE; i++) { | |
233 | if (qdevice_model_array[i] != NULL && | |
234 | strcmp(qdevice_model_array[i]->name, str) == 0) { | |
235 | *model_type = i; | |
236 | ||
237 | return (0); | |
238 | } | |
239 | } | |
240 | ||
241 | return (-1); | |
242 | } | |
243 | ||
244 | const char * | |
245 | qdevice_model_type_to_str(enum qdevice_model_type model_type) | |
246 | { | |
247 | ||
248 | switch (model_type) { | |
249 | case QDEVICE_MODEL_TYPE_NET: return ("Net"); break; | |
250 | case QDEVICE_MODEL_TYPE_ARRAY_SIZE: return ("Unknown model"); break; | |
251 | /* | |
252 | * Default is not defined intentionally. Compiler shows warning when new model is added | |
253 | */ | |
254 | } | |
255 | ||
256 | return ("Unknown model"); | |
257 | } |