amfapp.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /** @file amfapp.c
  2. *
  3. * Copyright (c) 2006 Ericsson AB.
  4. * Author: Hans Feldt, Anders Eriksson, Lars Holm
  5. * - Refactoring of code into several AMF files
  6. * - Constructors/destructors
  7. * - Serializers/deserializers
  8. *
  9. * All rights reserved.
  10. *
  11. *
  12. * This software licensed under BSD license, the text of which follows:
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  23. * contributors may be used to endorse or promote products derived from this
  24. * software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  36. * THE POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * AMF Application Class implementation
  39. *
  40. * This file contains functions for handling the AMF applications. It can
  41. * be viewed as the implementation of the AMF Application class
  42. * as described in SAI-Overview-B.02.01. The SA Forum specification
  43. * SAI-AIS-AMF-B.02.01 has been used as specification of the behaviour
  44. * and is referred to as 'the spec' below.
  45. *
  46. * The functions in this file are responsible for:
  47. * - on request start the service groups it contains
  48. * - on request order the service groups to assign workload to all
  49. * service units contained in the service group, level by level
  50. * - to handle administrative operation support for the application (FUTURE)
  51. *
  52. * The cluster class contains the following state machines:
  53. * - administrative state machine (ADSM)
  54. * - availability control state machine (ACSM)
  55. *
  56. * The administrative state machine will be implemented in the future.
  57. *
  58. * ACSM handles initial start of an application. In the future it will also
  59. * handle administrative commands on the application as described in paragraph
  60. * 7.4 of the spec. ACSM includes two stable states (UNINSTANTIATED and
  61. * STARTED) and a number of states to control the transition between the
  62. * stable states.
  63. *
  64. * The application is in state UNINSTANTIATED when the application starts.
  65. * (In the future this state will also be assumed after the LOCK_INSTANTIATION
  66. * administrative command.)
  67. *
  68. * State STARTED is assumed when the application has been initially started and
  69. * will in the future be re-assumed after the administrative command RESTART
  70. * have been executed.
  71. *
  72. */
  73. #include <assert.h>
  74. #include "amf.h"
  75. #include "print.h"
  76. static int all_sg_started (struct amf_application *app)
  77. {
  78. struct amf_sg *sg;
  79. int all_su_instantiated = 1;
  80. /* TODO: spare SUs... */
  81. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  82. if (sg->avail_state != SG_AC_Idle) {
  83. all_su_instantiated = 0;
  84. break;
  85. }
  86. }
  87. return all_su_instantiated;
  88. }
  89. void amf_application_start (
  90. struct amf_application *app, struct amf_node *node)
  91. {
  92. struct amf_sg *sg;
  93. ENTER ("'%s'", app->name.value);
  94. app->node_to_start = node;
  95. /* TODO: Calculate and set SI dependency levels */
  96. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  97. amf_sg_start (sg, node);
  98. }
  99. }
  100. void amf_application_assign_workload (
  101. struct amf_application *app, struct amf_node *node)
  102. {
  103. struct amf_sg *sg;
  104. /*
  105. * TODO: dependency level ignored
  106. * Each dependency level should be looped and amf_sg_assign_si
  107. * called several times.
  108. */
  109. app->node_to_start = node;
  110. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  111. amf_sg_assign_si (sg, 0);
  112. }
  113. }
  114. void amf_application_init (void)
  115. {
  116. log_init ("AMF");
  117. }
  118. void amf_application_sg_started (
  119. struct amf_application *app, struct amf_sg *sg, struct amf_node *node)
  120. {
  121. ENTER ("'%s'", app->name.value);
  122. if (all_sg_started (app)) {
  123. if (app->node_to_start == NULL) {
  124. amf_cluster_application_started (app->cluster, app);
  125. } else {
  126. amf_node_application_started (app->node_to_start, app);
  127. }
  128. }
  129. }
  130. void amf_application_sg_assigned (
  131. struct amf_application *app, struct amf_sg *sg)
  132. {
  133. ENTER ("'%s'", app->name.value);
  134. if (app->node_to_start == NULL) {
  135. amf_cluster_application_workload_assigned (app->cluster, app);
  136. } else {
  137. amf_node_application_workload_assigned (app->node_to_start, app);
  138. }
  139. }
  140. struct amf_application *amf_application_new (struct amf_cluster *cluster)
  141. {
  142. struct amf_application *app = amf_malloc (sizeof (struct amf_application));
  143. app->cluster = cluster;
  144. app->next = cluster->application_head;
  145. cluster->application_head = app;
  146. app->sg_head = NULL;
  147. app->si_head = NULL;
  148. return app;
  149. }
  150. void amf_application_delete (struct amf_application *app)
  151. {
  152. struct amf_sg *sg;
  153. struct amf_si *si;
  154. for (sg = app->sg_head; sg != NULL;) {
  155. struct amf_sg *tmp = sg;
  156. sg = sg->next;
  157. amf_sg_delete (tmp);
  158. }
  159. for (si = app->si_head; si != NULL;) {
  160. struct amf_si *tmp = si;
  161. si = si->next;
  162. amf_si_delete (tmp);
  163. }
  164. free (app);
  165. }
  166. void *amf_application_serialize (
  167. struct amf_application *app, int *len)
  168. {
  169. char *buf = NULL;
  170. int offset = 0, size = 0;
  171. TRACE8 ("%s", app->name.value);
  172. buf = amf_serialize_SaNameT (buf, &size, &offset, &app->name);
  173. buf = amf_serialize_SaUint32T (
  174. buf, &size, &offset, app->saAmfApplicationAdminState);
  175. buf = amf_serialize_SaUint32T (
  176. buf, &size, &offset, app->saAmfApplicationCurrNumSG);
  177. buf = amf_serialize_SaStringT (
  178. buf, &size, &offset, app->clccli_path);
  179. *len = offset;
  180. return buf;
  181. }
  182. struct amf_application *amf_application_deserialize (
  183. struct amf_cluster *cluster, char *buf, int size)
  184. {
  185. char *tmp = buf;
  186. struct amf_application *app;
  187. app = amf_application_new (cluster);
  188. tmp = amf_deserialize_SaNameT (tmp, &app->name);
  189. tmp = amf_deserialize_SaUint32T (tmp, &app->saAmfApplicationAdminState);
  190. tmp = amf_deserialize_SaUint32T (tmp, &app->saAmfApplicationCurrNumSG);
  191. tmp = amf_deserialize_SaStringT (tmp, &app->clccli_path);
  192. return app;
  193. }
  194. struct amf_application *amf_application_find (
  195. struct amf_cluster *cluster, char *name)
  196. {
  197. struct amf_application *app;
  198. for (app = cluster->application_head; app != NULL; app = app->next) {
  199. if (app->name.length == strlen(name) &&
  200. strncmp (name, (char*)app->name.value, app->name.length) == 0) {
  201. break;
  202. }
  203. }
  204. if (app == NULL) {
  205. dprintf ("App %s not found!", name);
  206. }
  207. return app;
  208. }