qdevice-model.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2015-2016 Red Hat, Inc.
  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. #include "qdevice-log.h"
  35. #include "qdevice-model.h"
  36. #include "qdevice-model-net.h"
  37. static struct qdevice_model *qdevice_model_array[QDEVICE_MODEL_TYPE_ARRAY_SIZE];
  38. int
  39. qdevice_model_init(struct qdevice_instance *instance)
  40. {
  41. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  42. qdevice_model_array[instance->model_type] == NULL) {
  43. qdevice_log(LOG_CRIT, "qdevice_model_init unhandled model");
  44. exit(1);
  45. }
  46. return (qdevice_model_array[instance->model_type]->init(instance));
  47. }
  48. int
  49. qdevice_model_destroy(struct qdevice_instance *instance)
  50. {
  51. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  52. qdevice_model_array[instance->model_type] == NULL) {
  53. qdevice_log(LOG_CRIT, "qdevice_model_destroy unhandled model");
  54. exit(1);
  55. }
  56. return (qdevice_model_array[instance->model_type]->destroy(instance));
  57. }
  58. int
  59. qdevice_model_run(struct qdevice_instance *instance)
  60. {
  61. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  62. qdevice_model_array[instance->model_type] == NULL) {
  63. qdevice_log(LOG_CRIT, "qdevice_model_run unhandled model");
  64. exit(1);
  65. }
  66. return (qdevice_model_array[instance->model_type]->run(instance));
  67. }
  68. int
  69. qdevice_model_get_config_node_list_failed(struct qdevice_instance *instance)
  70. {
  71. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  72. qdevice_model_array[instance->model_type] == NULL) {
  73. qdevice_log(LOG_CRIT, "qdevice_model_run unhandled model");
  74. exit(1);
  75. }
  76. return (qdevice_model_array[instance->model_type]->get_config_node_list_failed(instance));
  77. }
  78. int
  79. qdevice_model_config_node_list_changed(struct qdevice_instance *instance,
  80. const struct node_list *nlist, int config_version_set, uint64_t config_version)
  81. {
  82. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  83. qdevice_model_array[instance->model_type] == NULL) {
  84. qdevice_log(LOG_CRIT, "qdevice_model_run unhandled model");
  85. exit(1);
  86. }
  87. return (qdevice_model_array[instance->model_type]->
  88. config_node_list_changed(instance, nlist, config_version_set, config_version));
  89. }
  90. int
  91. qdevice_model_votequorum_quorum_notify(struct qdevice_instance *instance,
  92. uint32_t quorate, uint32_t node_list_entries, votequorum_node_t node_list[])
  93. {
  94. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  95. qdevice_model_array[instance->model_type] == NULL) {
  96. qdevice_log(LOG_CRIT, "qdevice_model_votequorum_quorum_notify unhandled model");
  97. exit(1);
  98. }
  99. return (qdevice_model_array[instance->model_type]->
  100. votequorum_quorum_notify(instance, quorate, node_list_entries, node_list));
  101. }
  102. int
  103. qdevice_model_votequorum_node_list_notify(struct qdevice_instance *instance,
  104. votequorum_ring_id_t votequorum_ring_id, uint32_t node_list_entries, uint32_t node_list[])
  105. {
  106. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  107. qdevice_model_array[instance->model_type] == NULL) {
  108. qdevice_log(LOG_CRIT, "qdevice_model_votequorum_node_list_notify unhandled model");
  109. exit(1);
  110. }
  111. return (qdevice_model_array[instance->model_type]->
  112. votequorum_node_list_notify(instance, votequorum_ring_id, node_list_entries, node_list));
  113. }
  114. int
  115. qdevice_model_votequorum_expected_votes_notify(struct qdevice_instance *instance,
  116. uint32_t expected_votes)
  117. {
  118. if (instance->model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE ||
  119. qdevice_model_array[instance->model_type] == NULL) {
  120. qdevice_log(LOG_CRIT, "qdevice_model_votequorum_expected_votes_notify unhandled model");
  121. exit(1);
  122. }
  123. return (qdevice_model_array[instance->model_type]->
  124. votequorum_expected_votes_notify(instance, expected_votes));
  125. }
  126. int
  127. qdevice_model_register(enum qdevice_model_type model_type,
  128. struct qdevice_model *model)
  129. {
  130. if (model_type >= QDEVICE_MODEL_TYPE_ARRAY_SIZE) {
  131. return (-1);
  132. }
  133. if (qdevice_model_array[model_type] != NULL) {
  134. return (-1);
  135. }
  136. qdevice_model_array[model_type] = model;
  137. return (0);
  138. }
  139. void
  140. qdevice_model_register_all(void)
  141. {
  142. if (qdevice_model_net_register() != 0) {
  143. qdevice_log(LOG_CRIT, "Failed to register model 'net' ");
  144. exit(1);
  145. }
  146. }
  147. int
  148. qdevice_model_str_to_type(const char *str, enum qdevice_model_type *model_type)
  149. {
  150. int i;
  151. for (i = 0; i < QDEVICE_MODEL_TYPE_ARRAY_SIZE; i++) {
  152. if (qdevice_model_array[i] != NULL &&
  153. strcmp(qdevice_model_array[i]->name, str) == 0) {
  154. *model_type = i;
  155. return (0);
  156. }
  157. }
  158. return (-1);
  159. }