node-list.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 <inttypes.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include "node-list.h"
  38. void
  39. node_list_init(struct node_list *list)
  40. {
  41. TAILQ_INIT(list);
  42. }
  43. struct node_list_entry *
  44. node_list_add(struct node_list *list, uint32_t node_id, uint32_t data_center_id,
  45. enum tlv_node_state node_state)
  46. {
  47. struct node_list_entry *node;
  48. node = (struct node_list_entry *)malloc(sizeof(*node));
  49. if (node == NULL) {
  50. return (NULL);
  51. }
  52. memset(node, 0, sizeof(*node));
  53. node->node_id = node_id;
  54. node->data_center_id = data_center_id;
  55. node->node_state = node_state;
  56. TAILQ_INSERT_TAIL(list, node, entries);
  57. return (node);
  58. }
  59. struct node_list_entry *
  60. node_list_add_from_node_info(struct node_list *list, const struct tlv_node_info *node_info)
  61. {
  62. return (node_list_add(list, node_info->node_id, node_info->data_center_id,
  63. node_info->node_state));
  64. }
  65. int
  66. node_list_clone(struct node_list *dst_list, const struct node_list *src_list)
  67. {
  68. struct node_list_entry *node_entry;
  69. node_list_init(dst_list);
  70. TAILQ_FOREACH(node_entry, src_list, entries) {
  71. if (node_list_add(dst_list, node_entry->node_id, node_entry->data_center_id,
  72. node_entry->node_state) == NULL) {
  73. node_list_free(dst_list);
  74. return (-1);
  75. }
  76. }
  77. return (0);
  78. }
  79. void
  80. node_list_entry_to_tlv_node_info(const struct node_list_entry *node,
  81. struct tlv_node_info *node_info)
  82. {
  83. node_info->node_id = node->node_id;
  84. node_info->data_center_id = node->data_center_id;
  85. node_info->node_state = node->node_state;
  86. }
  87. void
  88. node_list_free(struct node_list *list)
  89. {
  90. struct node_list_entry *node;
  91. struct node_list_entry *node_next;
  92. node = TAILQ_FIRST(list);
  93. while (node != NULL) {
  94. node_next = TAILQ_NEXT(node, entries);
  95. free(node);
  96. node = node_next;
  97. }
  98. TAILQ_INIT(list);
  99. }
  100. void
  101. node_list_del(struct node_list *list, struct node_list_entry *node)
  102. {
  103. TAILQ_REMOVE(list, node, entries);
  104. free(node);
  105. }
  106. int
  107. node_list_is_empty(const struct node_list *list)
  108. {
  109. return (TAILQ_EMPTY(list));
  110. }
  111. struct node_list_entry *
  112. node_list_find_node_id(const struct node_list *list, uint32_t node_id)
  113. {
  114. struct node_list_entry *node_entry;
  115. TAILQ_FOREACH(node_entry, list, entries) {
  116. if (node_entry->node_id == node_id) {
  117. return (node_entry);
  118. }
  119. }
  120. return (NULL);
  121. }
  122. int
  123. node_list_eq(const struct node_list *list1, const struct node_list *list2)
  124. {
  125. struct node_list_entry *node1_entry;
  126. struct node_list_entry *node2_entry;
  127. struct node_list tmp_list;
  128. int res;
  129. res = 1;
  130. if (node_list_clone(&tmp_list, list2) != 0) {
  131. return (-1);
  132. }
  133. TAILQ_FOREACH(node1_entry, list1, entries) {
  134. node2_entry = node_list_find_node_id(&tmp_list, node1_entry->node_id);
  135. if (node2_entry == NULL) {
  136. res = 0;
  137. goto return_res;
  138. }
  139. if (node1_entry->node_id != node2_entry->node_id ||
  140. node1_entry->data_center_id != node2_entry->data_center_id ||
  141. node1_entry->node_state != node2_entry->node_state) {
  142. res = 0;
  143. goto return_res;
  144. }
  145. node_list_del(&tmp_list, node2_entry);
  146. }
  147. if (!node_list_is_empty(&tmp_list)) {
  148. res = 0;
  149. goto return_res;
  150. }
  151. return_res:
  152. node_list_free(&tmp_list);
  153. return (res);
  154. }
  155. size_t
  156. node_list_size(const struct node_list *nlist)
  157. {
  158. struct node_list_entry *node_entry;
  159. size_t res;
  160. res = 0;
  161. TAILQ_FOREACH(node_entry, nlist, entries) {
  162. res++;
  163. }
  164. return (res);
  165. }