amfsi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. /** @file amfsi.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. * - Component/SU restart, SU failover
  7. * - Constructors/destructors
  8. * - Serializers/deserializers
  9. *
  10. * All rights reserved.
  11. *
  12. *
  13. * This software licensed under BSD license, the text of which follows:
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. * - Redistributions in binary form must reproduce the above copyright notice,
  21. * this list of conditions and the following disclaimer in the documentation
  22. * and/or other materials provided with the distribution.
  23. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  24. * contributors may be used to endorse or promote products derived from this
  25. * software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  28. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  31. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  35. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  37. * THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * AMF Workload related classes Implementation
  40. *
  41. * This file contains functions for handling :
  42. * - AMF service instances(SI)
  43. * - AMF SI Dependency
  44. * - AMF SI Ranked SU
  45. * - AMF SI Assignment
  46. * - AMF component service instances (CSI)
  47. * - AMF CSI Assignment
  48. * - AMF CSI Type
  49. * - AMF CSI Attribute
  50. * The file can be viewed as the implementation of the classes listed above
  51. * as described in SAI-Overview-B.02.01. The SA Forum specification
  52. * SAI-AIS-AMF-B.02.01 has been used as specification of the behaviour
  53. * and is referred to as 'the spec' below.
  54. *
  55. * The functions in this file are responsible for:
  56. * - calculating and storing an SI_dependency_level integer per SI
  57. * - calculating and storing a csi_dependency_level integer per CSI
  58. * - on request change HA state of an SI or CSI in such a way that the
  59. * requirements regarding SI -> SI dependencies (paragraphs 3.9.1.1 and
  60. * 3.9.1.2) and CSI -> CSI dependencies (paragraph 3.9.1.3) are fully
  61. * respected
  62. *
  63. * The si_dependency_level is an attribute calculated at init (in the future
  64. * also at reconfiguration) which indicates dependencies between SIs as
  65. * an integer. The si_dependency level indicates to which extent an SI depends
  66. * on other SIs such that an SI that depends on no other SI is on
  67. * si_dependecy_level == 1, an SI that depends only on an SI on
  68. * si_dependency_level == 1 is on si_dependency-level == 2.
  69. * An SI that depends on several SIs gets a si_dependency_level that is one
  70. * unit higher than the SI with the highest si_dependency_level it depends on.
  71. *
  72. * The csi_dependency_level attribute works the same way.
  73. *
  74. * According to paragraph 3.9.1 of the spec, a change to or from the ACTIVE
  75. * HA state is not always allowed without first deactivate dependent SI and CSI
  76. * assignments. Dependencies between CSIs are absolute while an SI that depends
  77. * on another SI may tolerate that the SI on which it depends is inactive for a
  78. * configurable time (the tolerance time). The consequence of this is that a
  79. * request to change the SI state may require a sequence of requests to
  80. * components to assume a new HA state for a CSI-assignment and to guarantee
  81. * the dependency rules, the active response from the component has to be
  82. * awaited before next HA state can be set.
  83. *
  84. * This file implements an SI state machine that fully implements these rules.
  85. * This state machine is called SI Dependency Control State Machine (dcsm)
  86. * and has the following states:
  87. * - DEACTIVATED (there is no SI-assignment with active HA state)
  88. * - ACTIVATING (a request to set the ACTIVE HA state has been received and
  89. * setting ACTIVE HA states to the appropriate components are
  90. * in progress)
  91. * - ACTIVATED (there is at least one SI-assignment with the ACTIVE HA-state)
  92. * - DEACTIVATING (a request to de-activate an SI or only a specific CSI
  93. * within an SI has been received and setting the QUISCED
  94. * HA states to the appropriate components are in progress)
  95. * - DEPENDENCY_DEACTIVATING (the SI-SI dependency tolerance timer has expired
  96. * and setting the QUISCED HA states to the
  97. * appropriate components are in progress)
  98. * - DEPENDENCY_DEACTIVATED (as state DEACTIVATED but will automatically
  99. * transition to state ACTIVATING when the
  100. * dependency problem is solved, i.e. the SI on
  101. * which it depends has re-assumed the ACTIVE HA
  102. * state)
  103. * - SETTING (a request to change the HA state when neither the existing
  104. * nor the requested state is ACTIVE)
  105. *
  106. * This file also implements:
  107. * - SI: Assignment state (for report purposes)
  108. * - SI Assignment: HA state
  109. * - CSI Assignment: HA state
  110. *
  111. */
  112. #include <assert.h>
  113. #include <stdio.h>
  114. #include "amf.h"
  115. #include "print.h"
  116. #include "util.h"
  117. #include "aispoll.h"
  118. #include "main.h"
  119. /**
  120. * Check if any CSI assignment belonging to SU has the requested
  121. * state.
  122. * @param su
  123. * @param hastate
  124. *
  125. * @return int
  126. */
  127. static int any_csi_has_hastate_in_su (struct amf_su *su, SaAmfHAStateT hastate)
  128. {
  129. struct amf_comp *component;
  130. struct amf_csi_assignment *csi_assignment;
  131. int exist = 0;
  132. for (component = su->comp_head; component != NULL;
  133. component = component->next) {
  134. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  135. while (csi_assignment != NULL) {
  136. if (csi_assignment->saAmfCSICompHAState == hastate) {
  137. exist = 1;
  138. goto done;
  139. }
  140. csi_assignment =
  141. amf_comp_get_next_csi_assignment (component, csi_assignment);
  142. }
  143. }
  144. done:
  145. return exist;
  146. }
  147. /**
  148. * Check if all CSI assignments belonging to a
  149. * an SI assignemnt has the requested state.
  150. * @param su
  151. * @param hastate
  152. *
  153. * @return int
  154. */
  155. static int all_csi_has_hastate_for_si (
  156. struct amf_si_assignment *si_assignment, SaAmfHAStateT hastate)
  157. {
  158. struct amf_comp *component;
  159. struct amf_csi_assignment *tmp_csi_assignment;
  160. int all = 1;
  161. for (component = si_assignment->su->comp_head; component != NULL;
  162. component = component->next) {
  163. tmp_csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  164. while (tmp_csi_assignment != NULL) {
  165. if ((tmp_csi_assignment->si_assignment == si_assignment) &&
  166. (tmp_csi_assignment->saAmfCSICompHAState != hastate)) {
  167. all = 0;
  168. goto done;
  169. }
  170. tmp_csi_assignment =
  171. amf_comp_get_next_csi_assignment (component, tmp_csi_assignment);
  172. }
  173. }
  174. done:
  175. return all;
  176. }
  177. /**
  178. * Implements table 6 in 3.3.2.4
  179. * TODO: active & standby is not correct calculated acc. to
  180. * table. This knowledge is e.g. used in assign_si_assumed_cbfn
  181. * (sg.c)
  182. * @param csi_assignment
  183. */
  184. static void set_si_ha_state (struct amf_csi_assignment *csi_assignment)
  185. {
  186. SaAmfHAStateT old_ha_state =
  187. csi_assignment->si_assignment->saAmfSISUHAState;
  188. SaAmfAssignmentStateT old_assigment_state =
  189. amf_si_get_saAmfSIAssignmentState (csi_assignment->csi->si);
  190. if (all_csi_has_hastate_for_si (
  191. csi_assignment->si_assignment, SA_AMF_HA_ACTIVE)) {
  192. csi_assignment->si_assignment->saAmfSISUHAState = SA_AMF_HA_ACTIVE;
  193. }
  194. if (all_csi_has_hastate_for_si (
  195. csi_assignment->si_assignment, SA_AMF_HA_STANDBY)) {
  196. csi_assignment->si_assignment->saAmfSISUHAState = SA_AMF_HA_STANDBY;
  197. }
  198. if (any_csi_has_hastate_in_su (
  199. csi_assignment->comp->su, SA_AMF_HA_QUIESCING)) {
  200. csi_assignment->si_assignment->saAmfSISUHAState = SA_AMF_HA_QUIESCING;
  201. }
  202. if (any_csi_has_hastate_in_su (
  203. csi_assignment->comp->su, SA_AMF_HA_QUIESCED)) {
  204. csi_assignment->si_assignment->saAmfSISUHAState = SA_AMF_HA_QUIESCED;
  205. }
  206. /* log changes to HA state */
  207. if (old_ha_state != csi_assignment->si_assignment->saAmfSISUHAState) {
  208. log_printf (LOG_NOTICE, "SU HA state changed to '%s' for:\n"
  209. "\t\tSI '%s', SU '%s'",
  210. amf_ha_state (csi_assignment->si_assignment->saAmfSISUHAState),
  211. csi_assignment->si_assignment->si->name.value,
  212. csi_assignment->si_assignment->name.value);
  213. }
  214. /* log changes to assignment state */
  215. if (old_assigment_state !=
  216. amf_si_get_saAmfSIAssignmentState (csi_assignment->csi->si)) {
  217. log_printf (LOG_NOTICE, "SI Assignment state changed to '%s' for:\n"
  218. "\t\tSI '%s', SU '%s'",
  219. amf_assignment_state (
  220. amf_si_get_saAmfSIAssignmentState (csi_assignment->csi->si)),
  221. csi_assignment->si_assignment->si->name.value,
  222. csi_assignment->si_assignment->name.value);
  223. }
  224. }
  225. char *amf_csi_dn_make (struct amf_csi *csi, SaNameT *name)
  226. {
  227. int i = snprintf((char*) name->value, SA_MAX_NAME_LENGTH,
  228. "safCsi=%s,safSi=%s,safApp=%s",
  229. csi->name.value, csi->si->name.value,
  230. csi->si->application->name.value);
  231. assert (i <= SA_MAX_NAME_LENGTH);
  232. name->length = i;
  233. return(char *)name->value;
  234. }
  235. void amf_si_init (void)
  236. {
  237. log_init ("AMF");
  238. }
  239. void amf_si_comp_set_ha_state_done (
  240. struct amf_si *si, struct amf_csi_assignment *csi_assignment)
  241. {
  242. ENTER ("'%s', '%s'", si->name.value, csi_assignment->csi->name.value);
  243. set_si_ha_state (csi_assignment);
  244. assert (csi_assignment->si_assignment->assumed_callback_fn != NULL);
  245. /*
  246. * Report to caller when the requested SI assignment state is
  247. * confirmed.
  248. */
  249. if (csi_assignment->si_assignment->requested_ha_state ==
  250. csi_assignment->si_assignment->saAmfSISUHAState) {
  251. csi_assignment->si_assignment->assumed_callback_fn (
  252. csi_assignment->si_assignment, 0);
  253. csi_assignment->si_assignment->assumed_callback_fn = NULL;
  254. }
  255. }
  256. void amf_si_activate (
  257. struct amf_si *si,
  258. void (*activated_callback_fn)(struct amf_si *si, int result))
  259. {
  260. struct amf_csi *csi;
  261. ENTER ("'%s'", si->name.value);
  262. for (csi = si->csi_head; csi != NULL; csi = csi->next) {
  263. struct amf_csi_assignment *csi_assignment;
  264. for (csi_assignment = csi->assigned_csis; csi_assignment != NULL;
  265. csi_assignment = csi_assignment->next) {
  266. csi_assignment->si_assignment->requested_ha_state =
  267. SA_AMF_HA_ACTIVE;
  268. /*
  269. * TODO: only active assignments should be set when dependency
  270. * levels are used.
  271. */
  272. csi_assignment->requested_ha_state = SA_AMF_HA_ACTIVE;
  273. amf_comp_hastate_set (csi_assignment->comp, csi_assignment);
  274. }
  275. }
  276. }
  277. void amf_si_comp_set_ha_state_failed (
  278. struct amf_si *si, struct amf_csi_assignment *csi_assignment)
  279. {
  280. ENTER ("");
  281. assert (0);
  282. }
  283. static void timer_function_ha_state_assumed (void *_si_assignment)
  284. {
  285. struct amf_si_assignment *si_assignment = _si_assignment;
  286. ENTER ("");
  287. si_assignment->saAmfSISUHAState = si_assignment->requested_ha_state;
  288. si_assignment->assumed_callback_fn (si_assignment, 0);
  289. }
  290. void amf_si_ha_state_assume (
  291. struct amf_si_assignment *si_assignment,
  292. void (*assumed_ha_state_callback_fn)(struct amf_si_assignment *si_assignment,
  293. int result))
  294. {
  295. struct amf_csi_assignment *csi_assignment;
  296. struct amf_csi *csi;
  297. int csi_assignment_cnt = 0;
  298. int hastate_set_done_cnt = 0;
  299. ENTER ("SI '%s' SU '%s' state %s", si_assignment->si->name.value,
  300. si_assignment->su->name.value,
  301. amf_ha_state (si_assignment->requested_ha_state));
  302. si_assignment->assumed_callback_fn = assumed_ha_state_callback_fn;
  303. for (csi = si_assignment->si->csi_head; csi != NULL; csi = csi->next) {
  304. for (csi_assignment = csi->assigned_csis; csi_assignment != NULL;
  305. csi_assignment = csi_assignment->next) {
  306. /*
  307. * If the CSI assignment and the SI assignment belongs to the
  308. * same SU, we have a match and can request the component to
  309. * change HA state.
  310. */
  311. if (name_match (&csi_assignment->comp->su->name,
  312. &si_assignment->su->name) &&
  313. (csi_assignment->saAmfCSICompHAState !=
  314. si_assignment->requested_ha_state)) {
  315. csi_assignment_cnt++;
  316. csi_assignment->requested_ha_state =
  317. si_assignment->requested_ha_state;
  318. amf_comp_hastate_set (csi_assignment->comp, csi_assignment);
  319. if (csi_assignment->saAmfCSICompHAState ==
  320. csi_assignment->requested_ha_state) {
  321. hastate_set_done_cnt++;
  322. }
  323. }
  324. }
  325. }
  326. /*
  327. * If the SU has only one component which is the faulty one, we
  328. * will not get an asynchronous response from the component.
  329. * This response (amf_si_comp_set_ha_state_done) is used to do
  330. * the next state transition. The asynchronous response is
  331. * simulated using a timeout instead.
  332. */
  333. if (csi_assignment_cnt == hastate_set_done_cnt) {
  334. poll_timer_handle handle;
  335. poll_timer_add (aisexec_poll_handle, 0, si_assignment,
  336. timer_function_ha_state_assumed, &handle);
  337. }
  338. }
  339. /**
  340. * Get number of active assignments for the specified SI
  341. * @param si
  342. *
  343. * @return int
  344. */
  345. int amf_si_get_saAmfSINumCurrActiveAssignments (struct amf_si *si)
  346. {
  347. int cnt = 0;
  348. struct amf_si_assignment *si_assignment;
  349. for (si_assignment = si->assigned_sis; si_assignment != NULL;
  350. si_assignment = si_assignment->next) {
  351. if (si_assignment->saAmfSISUHAState == SA_AMF_HA_ACTIVE) {
  352. cnt++;
  353. }
  354. }
  355. return cnt;
  356. }
  357. int amf_si_get_saAmfSINumCurrStandbyAssignments (struct amf_si *si)
  358. {
  359. int cnt = 0;
  360. struct amf_si_assignment *si_assignment;
  361. for (si_assignment = si->assigned_sis; si_assignment != NULL;
  362. si_assignment = si_assignment->next) {
  363. if (si_assignment->saAmfSISUHAState == SA_AMF_HA_STANDBY) {
  364. cnt++;
  365. }
  366. }
  367. return cnt;
  368. }
  369. SaAmfAssignmentStateT amf_si_get_saAmfSIAssignmentState (struct amf_si *si)
  370. {
  371. if ((amf_si_get_saAmfSINumCurrActiveAssignments (si) ==
  372. si->saAmfSIPrefActiveAssignments) &&
  373. (amf_si_get_saAmfSINumCurrStandbyAssignments (si) ==
  374. si->saAmfSIPrefStandbyAssignments)) {
  375. return SA_AMF_ASSIGNMENT_FULLY_ASSIGNED;
  376. } else if (amf_si_get_saAmfSINumCurrActiveAssignments (si) == 0) {
  377. return SA_AMF_ASSIGNMENT_UNASSIGNED;
  378. } else {
  379. return SA_AMF_ASSIGNMENT_PARTIALLY_ASSIGNED;
  380. }
  381. }
  382. void amf_csi_delete_assignments (struct amf_csi *csi, struct amf_su *su)
  383. {
  384. struct amf_csi_assignment *csi_assignment;
  385. ENTER ("'%s'", su->name.value);
  386. /*
  387. * TODO: this only works for n+m where each CSI list has only
  388. * two assignments, one active and one standby.
  389. * TODO: use DN instead
  390. */
  391. if (csi->assigned_csis->comp->su == su) {
  392. csi_assignment = csi->assigned_csis;
  393. csi->assigned_csis = csi_assignment->next;
  394. } else {
  395. csi_assignment = csi->assigned_csis->next;
  396. csi->assigned_csis->next = NULL;
  397. assert (csi_assignment != NULL && csi_assignment->comp->su == su);
  398. }
  399. assert (csi_assignment != NULL);
  400. free (csi_assignment);
  401. }
  402. /**
  403. * Constructor for SI objects. Adds SI last in the ordered
  404. * list owned by the specified application. Always returns a
  405. * valid SI object, out-of-memory problems are handled here.
  406. * Default values are initialized.
  407. * @param app
  408. *
  409. * @return struct amf_si*
  410. */
  411. struct amf_si *amf_si_new (struct amf_application *app, char *name)
  412. {
  413. struct amf_si *tail = app->si_head;
  414. struct amf_si *si = calloc (1, sizeof (struct amf_si));
  415. if (si == NULL) {
  416. openais_exit_error (AIS_DONE_OUT_OF_MEMORY);
  417. }
  418. while (tail != NULL) {
  419. if (tail->next == NULL) {
  420. break;
  421. }
  422. tail = tail->next;
  423. }
  424. if (tail == NULL) {
  425. app->si_head = si;
  426. } else {
  427. tail->next = si;
  428. }
  429. si->application = app;
  430. /* setup default values from spec. */
  431. si->saAmfSIAdminState = SA_AMF_ADMIN_UNLOCKED;
  432. si->saAmfSIRank = 0;
  433. si->saAmfSIPrefActiveAssignments = 1;
  434. si->saAmfSIPrefStandbyAssignments = 1;
  435. si->assigned_sis = NULL;
  436. si->csi_head = NULL;
  437. setSaNameT (&si->name, name);
  438. return si;
  439. }
  440. void amf_si_delete (struct amf_si *si)
  441. {
  442. struct amf_si_assignment *si_assignment;
  443. struct amf_csi *csi;
  444. for (csi = si->csi_head; csi != NULL;) {
  445. struct amf_csi *tmp = csi;
  446. csi = csi->next;
  447. amf_csi_delete (tmp);
  448. }
  449. for (si_assignment = si->assigned_sis; si_assignment != NULL;) {
  450. struct amf_si_assignment *tmp = si_assignment;
  451. si_assignment = si_assignment->next;
  452. free (tmp);
  453. }
  454. free (si);
  455. }
  456. void *amf_si_serialize (struct amf_si *si, int *len)
  457. {
  458. int objsz = sizeof (struct amf_si);
  459. struct amf_si *copy;
  460. copy = amf_malloc (objsz);
  461. memcpy (copy, si, objsz);
  462. *len = objsz;
  463. TRACE8 ("%s", copy->name.value);
  464. return copy;
  465. }
  466. struct amf_si *amf_si_deserialize (struct amf_application *app, char *buf, int size)
  467. {
  468. int objsz = sizeof (struct amf_si);
  469. if (objsz > size) {
  470. return NULL;
  471. } else {
  472. struct amf_si *tmp = (struct amf_si*) buf;
  473. struct amf_si *si = amf_si_new (app, (char*)tmp->name.value);
  474. TRACE8 ("%s", si->name.value);
  475. memcpy (&si->saAmfSIProtectedbySG, &tmp->saAmfSIProtectedbySG,
  476. sizeof (SaNameT));
  477. si->saAmfSIRank = tmp->saAmfSIRank;
  478. si->saAmfSINumCSIs = tmp->saAmfSINumCSIs;
  479. si->saAmfSIPrefActiveAssignments = tmp->saAmfSIPrefActiveAssignments;
  480. si->saAmfSIPrefStandbyAssignments = tmp->saAmfSIPrefStandbyAssignments;
  481. si->saAmfSIAdminState = tmp->saAmfSIAdminState;
  482. return si;
  483. }
  484. }
  485. /*****************************************************************************
  486. * SI Assignment class implementation *
  487. ****************************************************************************/
  488. struct amf_si_assignment *amf_si_assignment_new (struct amf_si *si)
  489. {
  490. struct amf_si_assignment *si_assignment;
  491. si_assignment = amf_malloc (sizeof (struct amf_si_assignment));
  492. si_assignment->si = si;
  493. return si_assignment;
  494. }
  495. void *amf_si_assignment_serialize (
  496. struct amf_si_assignment *si_assignment, int *len)
  497. {
  498. int objsz = sizeof (struct amf_si_assignment);
  499. struct amf_si_assignment *copy;
  500. copy = amf_malloc (objsz);
  501. memcpy (copy, si_assignment, objsz);
  502. *len = objsz;
  503. TRACE8 ("%s", copy->name.value);
  504. return copy;
  505. }
  506. struct amf_si_assignment *amf_si_assignment_deserialize (
  507. struct amf_si *si, char *buf, int size)
  508. {
  509. int objsz = sizeof (struct amf_si_assignment);
  510. if (objsz > size) {
  511. return NULL;
  512. } else {
  513. struct amf_si_assignment *obj = amf_si_assignment_new (si);
  514. if (obj == NULL) {
  515. return NULL;
  516. }
  517. memcpy (obj, buf, objsz);
  518. TRACE8 ("%s", obj->name.value);
  519. obj->si = si;
  520. obj->su = amf_su_find (si->application->cluster, &obj->name);
  521. obj->next = si->assigned_sis;
  522. si->assigned_sis = obj;
  523. return obj;
  524. }
  525. }
  526. struct amf_si *amf_si_find (struct amf_application *app, char *name)
  527. {
  528. struct amf_si *si;
  529. ENTER ("%s", name);
  530. for (si = app->si_head; si != NULL; si = si->next) {
  531. if (strncmp (name, (char*)si->name.value, si->name.length) == 0) {
  532. break;
  533. }
  534. }
  535. return si;
  536. }
  537. /*****************************************************************************
  538. * CSI class implementation *
  539. ****************************************************************************/
  540. struct amf_csi *amf_csi_new (struct amf_si *si)
  541. {
  542. struct amf_csi *csi;
  543. csi = amf_malloc (sizeof (struct amf_csi));
  544. csi->si = si;
  545. return csi;
  546. }
  547. void amf_csi_delete (struct amf_csi *csi)
  548. {
  549. struct amf_csi_assignment *csi_assignment;
  550. for (csi_assignment = csi->assigned_csis; csi_assignment != NULL;) {
  551. struct amf_csi_assignment *tmp = csi_assignment;
  552. csi_assignment = csi_assignment->next;
  553. free (tmp);
  554. }
  555. free (csi);
  556. }
  557. void *amf_csi_serialize (struct amf_csi *csi, int *len)
  558. {
  559. int objsz = sizeof (struct amf_csi);
  560. struct amf_csi *copy;
  561. copy = amf_malloc (objsz);
  562. memcpy (copy, csi, objsz);
  563. *len = objsz;
  564. TRACE8 ("%s", copy->name.value);
  565. return copy;
  566. }
  567. struct amf_csi *amf_csi_deserialize (struct amf_si *si, char *buf, int size)
  568. {
  569. int objsz = sizeof (struct amf_csi);
  570. if (objsz > size) {
  571. return NULL;
  572. } else {
  573. struct amf_csi *obj = amf_csi_new (si);
  574. if (obj == NULL) {
  575. return NULL;
  576. }
  577. memcpy (obj, buf, objsz);
  578. TRACE8 ("%s", obj->name.value);
  579. obj->si = si;
  580. obj->assigned_csis = NULL;
  581. obj->attributes_head = NULL;
  582. obj->next = si->csi_head;
  583. si->csi_head = obj;
  584. return obj;
  585. }
  586. }
  587. struct amf_csi *amf_csi_find (struct amf_si *si, char *name)
  588. {
  589. struct amf_csi *csi;
  590. ENTER ("%s", name);
  591. for (csi = si->csi_head; csi != NULL; csi = csi->next) {
  592. if (strncmp (name, (char*)csi->name.value, csi->name.length) == 0) {
  593. break;
  594. }
  595. }
  596. return csi;
  597. }
  598. /*****************************************************************************
  599. * CSI Assignment class implementation *
  600. ****************************************************************************/
  601. struct amf_csi_assignment *amf_csi_assignment_new (struct amf_csi *csi)
  602. {
  603. struct amf_csi_assignment *csi_assignment;
  604. csi_assignment = amf_malloc (sizeof (struct amf_csi_assignment));
  605. csi_assignment->csi = csi;
  606. return csi_assignment;
  607. }
  608. void *amf_csi_assignment_serialize (
  609. struct amf_csi_assignment *csi_assignment, int *len)
  610. {
  611. int objsz = sizeof (struct amf_csi_assignment);
  612. struct amf_csi_assignment *copy;
  613. copy = amf_malloc (objsz);
  614. memcpy (copy, csi_assignment, objsz);
  615. *len = objsz;
  616. TRACE8 ("%s", copy->name.value);
  617. return copy;
  618. }
  619. struct amf_csi_assignment *amf_csi_assignment_deserialize (
  620. struct amf_csi *csi, char *buf, int size)
  621. {
  622. int objsz = sizeof (struct amf_csi_assignment);
  623. if (objsz > size) {
  624. return NULL;
  625. } else {
  626. struct amf_csi_assignment *obj = amf_csi_assignment_new (csi);
  627. if (obj == NULL) {
  628. return NULL;
  629. }
  630. memcpy (obj, buf, objsz);
  631. TRACE8 ("%s", obj->name.value);
  632. obj->csi = csi;
  633. obj->comp = amf_comp_find (csi->si->application->cluster, &obj->name);
  634. obj->next = csi->assigned_csis;
  635. csi->assigned_csis = obj;
  636. return obj;
  637. }
  638. }
  639. char *amf_csi_assignment_dn_make (
  640. struct amf_csi_assignment *csi_assignment, SaNameT *name)
  641. {
  642. SaNameT comp_name;
  643. struct amf_csi *csi = csi_assignment->csi;
  644. int i;
  645. amf_comp_dn_make (csi_assignment->comp, &comp_name);
  646. i = snprintf((char*) name->value, SA_MAX_NAME_LENGTH,
  647. "safCSIComp=%s,safCsi=%s,safSi=%s,safApp=%s",
  648. comp_name.value,
  649. csi->name.value, csi->si->name.value,
  650. csi->si->application->name.value);
  651. assert (i <= SA_MAX_NAME_LENGTH);
  652. name->length = i;
  653. return(char *)name->value;
  654. }
  655. struct amf_csi_assignment *amf_csi_assignment_find (
  656. struct amf_cluster *cluster, SaNameT *name)
  657. {
  658. struct amf_application *app;
  659. struct amf_si *si;
  660. struct amf_csi *csi;
  661. struct amf_csi_assignment *csi_assignment = NULL;
  662. char *app_name;
  663. char *si_name;
  664. char *csi_name;
  665. char *csi_assignment_name;
  666. char *buf;
  667. ENTER ("%s", name->value);
  668. /* malloc new buffer since we need to write to the buffer */
  669. buf = amf_malloc (name->length + 1);
  670. memcpy (buf, name->value, name->length + 1);
  671. csi_assignment_name = strstr (buf, "safCSIComp=");
  672. csi_name = strstr (buf, "safCsi=");
  673. si_name = strstr (buf, "safSi=");
  674. app_name = strstr (buf, "safApp=");
  675. app_name++;
  676. app_name = strstr (app_name, "safApp=");
  677. if (csi_assignment_name == NULL || csi_name == NULL || si_name == NULL ||
  678. app_name == NULL) {
  679. goto end;
  680. }
  681. *(csi_name - 1) = '\0';
  682. *(si_name - 1) = '\0';
  683. *(app_name - 1) = '\0';
  684. /* jump to value */
  685. csi_assignment_name += 11;
  686. csi_name += 7;
  687. si_name += 6;
  688. app_name += 7;
  689. app = amf_application_find (cluster, app_name);
  690. if (app == NULL) {
  691. goto end;
  692. }
  693. si = amf_si_find (app, si_name);
  694. if (si == NULL) {
  695. goto end;
  696. }
  697. csi = amf_csi_find (si, csi_name);
  698. if (csi == NULL) {
  699. goto end;
  700. }
  701. for (csi_assignment = csi->assigned_csis; csi_assignment != NULL;
  702. csi_assignment = csi_assignment->next) {
  703. if (strncmp (csi_assignment_name,
  704. (char*)csi_assignment->name.value,
  705. csi_assignment->name.length) == 0) {
  706. goto end;
  707. }
  708. }
  709. end:
  710. free (buf);
  711. return csi_assignment;
  712. }
  713. struct amf_csi_attribute *amf_csi_attribute_new (struct amf_csi *csi)
  714. {
  715. struct amf_csi_attribute *csi_attribute;
  716. csi_attribute = amf_malloc (sizeof (struct amf_csi_assignment));
  717. csi_attribute->next = csi->attributes_head;
  718. csi->attributes_head = csi_attribute;
  719. return csi_attribute;
  720. }
  721. void *amf_csi_attribute_serialize (
  722. struct amf_csi_attribute *csi_attribute, int *len)
  723. {
  724. char *buf = NULL;
  725. int i, offset = 0, size = 0;
  726. TRACE8 ("%s", csi_attribute->name);
  727. buf = amf_serialize_SaStringT (buf, &size, &offset, csi_attribute->name);
  728. /* count value and write to buf */
  729. for (i = 0; csi_attribute->value &&
  730. csi_attribute->value[i] != NULL; i++);
  731. buf = amf_serialize_SaUint32T (buf, &size, &offset, i);
  732. for (i = 0; csi_attribute->value &&
  733. csi_attribute->value[i] != NULL; i++) {
  734. buf = amf_serialize_SaStringT (
  735. buf, &size, &offset, csi_attribute->value[i]);
  736. }
  737. *len = offset;
  738. return buf;
  739. }
  740. struct amf_csi_attribute *amf_csi_attribute_deserialize (
  741. struct amf_csi *csi, char *buf, int size)
  742. {
  743. char *tmp = buf;
  744. struct amf_csi_attribute *csi_attribute;
  745. int i;
  746. SaUint32T cnt;
  747. csi_attribute = amf_csi_attribute_new (csi);
  748. tmp = amf_deserialize_SaStringT (tmp, &csi_attribute->name);
  749. tmp = amf_deserialize_SaUint32T (tmp, &cnt);
  750. csi_attribute->value = amf_malloc ((cnt + 1) * sizeof (SaStringT*));
  751. for (i = 0; i < cnt; i++) {
  752. tmp = amf_deserialize_SaStringT (tmp, &csi_attribute->value[i]);
  753. }
  754. csi_attribute->value[i] = NULL;
  755. return csi_attribute;
  756. }