testamf.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright (c) 2002-2003 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.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 MontaVista Software, 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 <stdio.h>
  35. #include <stdlib.h>
  36. #include <errno.h>
  37. #include <sys/types.h>
  38. #include <sys/socket.h>
  39. #include <sys/un.h>
  40. #include <unistd.h>
  41. #include <signal.h>
  42. #include "ais_types.h"
  43. #include "ais_amf.h"
  44. #include "saClm.h"
  45. void printSaClmNodeAddressT (SaClmNodeAddressT *nodeAddress) {
  46. int i;
  47. for (i = 0; i < nodeAddress->length; i++) {
  48. printf ("%d.", nodeAddress->value[i]);
  49. }
  50. }
  51. void printSaNameT (SaNameT *name)
  52. {
  53. int i;
  54. for (i = 0; i < name->length; i++) {
  55. printf ("%c", name->value[i]);
  56. }
  57. }
  58. void setSanameT (SaNameT *name, char *str) {
  59. name->length = strlen (str);
  60. memcpy (name->value, str, name->length);
  61. }
  62. static int health_flag = -1;
  63. static unsigned int healthcheck_count = 0;
  64. static unsigned int healthcheck_no = 0;
  65. void HealthcheckCallback (SaInvocationT invocation,
  66. const SaNameT *compName,
  67. SaAmfHealthcheckT checkType)
  68. {
  69. SaErrorT res;
  70. healthcheck_no ++;
  71. if (health_flag == -1 || healthcheck_no%healthcheck_count == 0) {
  72. printf ("%u HealthcheckCallback have occured for component: ",healthcheck_no);
  73. printSaNameT ((SaNameT *)compName);
  74. printf ("\n");
  75. }
  76. res = saAmfResponse (invocation, SA_OK);
  77. if (res != SA_OK) {
  78. printf ("response res is %d\n", res);
  79. }
  80. }
  81. void ReadinessStateSetCallback (SaInvocationT invocation,
  82. const SaNameT *compName,
  83. SaAmfReadinessStateT readinessState)
  84. {
  85. switch (readinessState) {
  86. case SA_AMF_IN_SERVICE:
  87. printf ("ReadinessStateSetCallback: '");
  88. printSaNameT ((SaNameT *)compName);
  89. printf ("' requested to enter operational state SA_AMF_IN_SERVICE.\n");
  90. saAmfResponse (invocation, SA_OK);
  91. break;
  92. case SA_AMF_OUT_OF_SERVICE:
  93. printf ("ReadinessStateSetCallback: '");
  94. printSaNameT ((SaNameT *)compName);
  95. printf ("' requested to enter operational state SA_AMF_OUT_OF_SERVICE.\n");
  96. saAmfResponse (invocation, SA_OK);
  97. break;
  98. case SA_AMF_STOPPING:
  99. printf ("ReadinessStateSetCallback: '");
  100. printSaNameT ((SaNameT *)compName);
  101. printf ("' requested to stop, stopping.\n");
  102. saAmfStoppingComplete (invocation, SA_OK);
  103. break;
  104. }
  105. }
  106. void ComponentTerminateCallback (
  107. SaInvocationT invocation,
  108. const SaNameT *compName)
  109. {
  110. printf ("ComponentTerminateCallback\n");
  111. }
  112. void CSISetCallback (
  113. SaInvocationT invocation,
  114. const SaNameT *compName,
  115. const SaNameT *csiName,
  116. SaAmfCSIFlagsT csiFlags,
  117. SaAmfHAStateT *haState,
  118. SaNameT *activeCompName,
  119. SaAmfCSITransitionDescriptorT transitionDescriptor)
  120. {
  121. switch (*haState) {
  122. case SA_AMF_ACTIVE:
  123. printf ("CSISetCallback: '");
  124. printSaNameT ((SaNameT *)compName);
  125. printf ("' for CSI '");
  126. printSaNameT ((SaNameT *)compName);
  127. printf ("'");
  128. printf (" requested to enter hastate SA_AMF_ACTIVE.\n");
  129. saAmfResponse (invocation, SA_OK);
  130. break;
  131. case SA_AMF_STANDBY:
  132. printf ("CSISetCallback: '");
  133. printSaNameT ((SaNameT *)compName);
  134. printf ("' for CSI '");
  135. printSaNameT ((SaNameT *)compName);
  136. printf ("'");
  137. printf (" requested to enter hastate SA_AMF_STANDBY.\n");
  138. saAmfResponse (invocation, SA_OK);
  139. break;
  140. case SA_AMF_QUIESCED:
  141. printf ("CSISetCallback: '");
  142. printSaNameT ((SaNameT *)compName);
  143. printf ("' for CSI '");
  144. printSaNameT ((SaNameT *)compName);
  145. printf ("'");
  146. printf (" requested to enter hastate SA_AMF_QUIESCED.\n");
  147. saAmfResponse (invocation, SA_OK);
  148. break;
  149. }
  150. }
  151. void CSIRemoveCallback (
  152. SaInvocationT invocation,
  153. const SaNameT *compName,
  154. const SaNameT *csiName,
  155. const SaAmfCSIFlagsT *csiFlags)
  156. {
  157. printf ("CSIRemoveCallback invocation id %lld compName ", invocation);
  158. printSaNameT ((SaNameT *)compName);
  159. printf (" csiName ");
  160. printSaNameT ((SaNameT *)csiName);
  161. printf ("\n");
  162. saAmfResponse (invocation, SA_OK);
  163. }
  164. void ProtectionGroupTrackCallback (
  165. const SaNameT *csiName,
  166. SaAmfProtectionGroupNotificationT *notificationBuffer,
  167. SaUint32T numberOfItems,
  168. SaUint32T numberOfMembers,
  169. SaErrorT error)
  170. {
  171. int i;
  172. printf ("ProtectionGroupTrackCallback items %d members %d\n", (int)numberOfItems, (int)numberOfMembers);
  173. printf ("buffer is %p\n", notificationBuffer);
  174. for (i = 0; i < numberOfItems; i++) {
  175. printf ("component name");
  176. printSaNameT (&notificationBuffer[i].member.compName);
  177. printf ("\n");
  178. printf ("\treadiness state is %d\n", notificationBuffer[i].member.readinessState);
  179. printf ("\thastate %d\n", notificationBuffer[i].member.haState);
  180. printf ("\tchange is %d\n", notificationBuffer[i].change);
  181. }
  182. }
  183. void ExternalComponentRestartCallback (
  184. const SaInvocationT invocation,
  185. const SaNameT *externalCompName)
  186. {
  187. printf ("ExternalComponentRestartCallback\n");
  188. }
  189. void ExternalComponentControlCallback (
  190. const SaInvocationT invocation,
  191. const SaNameT *externalCompName,
  192. SaAmfExternalComponentActionT controlAction)
  193. {
  194. printf ("ExternalComponentControlCallback\n");
  195. }
  196. void PendingOperationConfirmCallback (
  197. const SaInvocationT invocation,
  198. const SaNameT *compName,
  199. SaAmfPendingOperationFlagsT pendingOperationFlags)
  200. {
  201. printf ("PendingOperationConfirmCallback\n");
  202. }
  203. void PendingOperationExpiredCallback (
  204. const SaNameT *compName,
  205. SaAmfPendingOperationFlagsT pendingOperationFlags)
  206. {
  207. printf ("PendingOperationExpiredCallback\n");
  208. }
  209. SaAmfCallbacksT amfCallbacks = {
  210. HealthcheckCallback,
  211. ReadinessStateSetCallback,
  212. ComponentTerminateCallback,
  213. CSISetCallback,
  214. CSIRemoveCallback,
  215. ProtectionGroupTrackCallback,
  216. ExternalComponentRestartCallback,
  217. ExternalComponentControlCallback,
  218. PendingOperationConfirmCallback,
  219. PendingOperationExpiredCallback
  220. };
  221. SaVersionT version = { 'A', 1, 1 };
  222. void sigintr_handler (int signum) {
  223. exit (0);
  224. }
  225. int main (int argc, char **argv) {
  226. SaAmfHandleT handle;
  227. SaAmfHandleT handleproxy;
  228. int result;
  229. SaSelectionObjectT select_fd;
  230. fd_set read_fds;
  231. SaNameT compName;
  232. SaNameT csiName;
  233. SaNameT compname_get_name;
  234. SaAmfReadinessStateT readinessState;
  235. SaAmfHAStateT HAState;
  236. SaAmfComponentCapabilityModelT componentCapabilityModel;
  237. SaAmfProtectionGroupNotificationT protectionGroupNotificationBuffer[64];
  238. SaAmfPendingOperationFlagsT pending_operation;
  239. extern char *optarg;
  240. extern int optind;
  241. int c;
  242. signal (SIGINT, sigintr_handler);
  243. for (;;) {
  244. c = getopt(argc,argv,"h:");
  245. if (c==-1) {
  246. break;
  247. }
  248. switch (c) {
  249. case 0 :
  250. break;
  251. case 'h':
  252. health_flag = 0;
  253. sscanf (optarg,"%ud" ,&healthcheck_count);
  254. break;
  255. default :
  256. break;
  257. }
  258. }
  259. result = saAmfInitialize (&handle, &amfCallbacks, &version);
  260. if (result != SA_OK) {
  261. printf ("initialize result is %d\n", result);
  262. exit (1);
  263. }
  264. result = saAmfInitialize (&handleproxy, &amfCallbacks, &version);
  265. if (result != SA_OK) {
  266. printf ("initialize result is %d\n", result);
  267. exit (1);
  268. }
  269. FD_ZERO (&read_fds);
  270. saAmfSelectionObjectGet (&handle, &select_fd);
  271. FD_SET (select_fd, &read_fds);
  272. saAmfSelectionObjectGet (&handleproxy, &select_fd);
  273. FD_SET (select_fd, &read_fds);
  274. setSanameT (&compName, "comp_a_in_su_x");
  275. setSanameT (&csiName, "pgA");
  276. result = saAmfComponentRegister (&handle, &compName, NULL);
  277. printf ("saAmfComponentRegister with result %d (should be 1) is:", result);
  278. result = saAmfCompNameGet (&handle, &compname_get_name);
  279. printf ("saAmfCompNameGet with result %d (should be 1) is:", result);
  280. printSaNameT (&compname_get_name);
  281. printf ("\n");
  282. result = saAmfPendingOperationGet (&compName, &pending_operation);
  283. printf ("saAmfPendingOperationGet with value %d result %d (should be 1) is:",
  284. pending_operation, result);
  285. result = saAmfComponentCapabilityModelGet (&compName, &componentCapabilityModel);
  286. printf ("component capability model get is %d (should be 1)\n", result);
  287. result = saAmfProtectionGroupTrackStart (&handle, &csiName, SA_TRACK_CURRENT | SA_TRACK_CHANGES, protectionGroupNotificationBuffer, 64);
  288. printf ("track start result is %d (should be 1)\n", result);
  289. result = saAmfProtectionGroupTrackStop (&handle, &csiName);
  290. printf ("track stop result is %d (should be 1)\n", result);
  291. result = saAmfReadinessStateGet (&compName, &readinessState);
  292. printf ("saAmfReadinessStateGet (%d) result %d (should be 1)\n",
  293. readinessState, result);
  294. result = saAmfHAStateGet (&compName, &csiName, &HAState);
  295. printf ("saAmfHAStateGet (%d) result %d (should be 1)\n",
  296. HAState, result);
  297. do {
  298. FD_SET (STDIN_FILENO, &read_fds);
  299. select (select_fd + 1, &read_fds, 0, 0, 0);
  300. if (FD_ISSET (STDIN_FILENO, &read_fds)) {
  301. break;
  302. }
  303. saAmfDispatch (&handle, SA_DISPATCH_ALL);
  304. } while (result);
  305. result = saAmfProtectionGroupTrackStop (&handle, &csiName);
  306. saAmfFinalize (&handle);
  307. return (0);
  308. }