amfapp.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. struct amf_su *su;
  80. int all_su_instantiated = 1;
  81. /* TODO: spare SUs... */
  82. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  83. for (su = sg->su_head; su != NULL; su = su->next) {
  84. if (su->saAmfSUPresenceState != SA_AMF_PRESENCE_INSTANTIATED) {
  85. all_su_instantiated = 0;
  86. break;
  87. }
  88. }
  89. }
  90. return all_su_instantiated;
  91. }
  92. void amf_application_start (
  93. struct amf_application *app, struct amf_node *node)
  94. {
  95. struct amf_sg *sg;
  96. ENTER ("'%s'", app->name.value);
  97. /* TODO: Calculate and set SI dependency levels */
  98. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  99. amf_sg_start (sg, node);
  100. }
  101. }
  102. void amf_application_assign_workload (
  103. struct amf_application *app, struct amf_node *node)
  104. {
  105. struct amf_sg *sg;
  106. /*
  107. * TODO: dependency level ignored
  108. * Each dependency level should be looped and amf_sg_assign_si
  109. * called several times.
  110. */
  111. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  112. amf_sg_assign_si (sg, 0);
  113. }
  114. }
  115. void amf_application_init (void)
  116. {
  117. log_init ("AMF");
  118. }
  119. void amf_application_sg_started (
  120. struct amf_application *app, struct amf_sg *sg, struct amf_node *node)
  121. {
  122. ENTER ("'%s'", app->name.value);
  123. if (all_sg_started (app)) {
  124. amf_cluster_application_started (app->cluster, app);
  125. }
  126. }
  127. void amf_application_sg_assigned (
  128. struct amf_application *app, struct amf_sg *sg)
  129. {
  130. ENTER ("'%s'", app->name.value);
  131. amf_cluster_application_workload_assigned (app->cluster, app);
  132. }
  133. struct amf_application *amf_application_new (struct amf_cluster *cluster)
  134. {
  135. struct amf_application *app = amf_malloc (sizeof (struct amf_application));
  136. app->cluster = cluster;
  137. return app;
  138. }
  139. void amf_application_delete (struct amf_application *app)
  140. {
  141. struct amf_sg *sg;
  142. struct amf_si *si;
  143. for (sg = app->sg_head; sg != NULL;) {
  144. struct amf_sg *tmp = sg;
  145. sg = sg->next;
  146. amf_sg_delete (tmp);
  147. }
  148. for (si = app->si_head; si != NULL;) {
  149. struct amf_si *tmp = si;
  150. si = si->next;
  151. amf_si_delete (tmp);
  152. }
  153. free (app);
  154. }
  155. void *amf_application_serialize (
  156. struct amf_application *app, int *len)
  157. {
  158. int objsz = sizeof (struct amf_application);
  159. struct amf_application *copy;
  160. copy = amf_malloc (objsz);
  161. memcpy (copy, app, objsz);
  162. *len = objsz;
  163. TRACE8 ("%s", copy->name.value);
  164. return copy;
  165. }
  166. struct amf_application *amf_application_deserialize (
  167. struct amf_cluster *cluster, char *buf, int size)
  168. {
  169. int objsz = sizeof (struct amf_application);
  170. if (objsz > size) {
  171. return NULL;
  172. } else {
  173. struct amf_application *obj = amf_application_new (cluster);
  174. assert (obj);
  175. memcpy (obj, buf, objsz);
  176. TRACE8 ("%s", obj->name.value);
  177. obj->cluster = cluster;
  178. obj->sg_head = NULL;
  179. obj->si_head = NULL;
  180. obj->next = cluster->application_head;
  181. cluster->application_head = obj;
  182. return obj;
  183. }
  184. }
  185. struct amf_application *amf_application_find (
  186. struct amf_cluster *cluster, char *name)
  187. {
  188. struct amf_application *app;
  189. ENTER ("%s", name);
  190. for (app = cluster->application_head; app != NULL; app = app->next) {
  191. if (strncmp (name, (char*)app->name.value, app->name.length) == 0) {
  192. break;
  193. }
  194. }
  195. return app;
  196. }