amfsi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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_su_get_saAmfSINumCurrActiveAssignments (struct amf_si *si,
  358. struct amf_su *su)
  359. {
  360. int cnt = 0;
  361. struct amf_si_assignment *si_assignment;
  362. for (si_assignment = si->assigned_sis; si_assignment != NULL;
  363. si_assignment = si_assignment->next) {
  364. if (si_assignment->su == su &&
  365. si_assignment->saAmfSISUHAState == SA_AMF_HA_ACTIVE) {
  366. cnt++;
  367. }
  368. }
  369. return cnt;
  370. }
  371. int amf_si_get_saAmfSINumCurrStandbyAssignments (struct amf_si *si)
  372. {
  373. int cnt = 0;
  374. struct amf_si_assignment *si_assignment;
  375. for (si_assignment = si->assigned_sis; si_assignment != NULL;
  376. si_assignment = si_assignment->next) {
  377. if (si_assignment->saAmfSISUHAState == SA_AMF_HA_STANDBY) {
  378. cnt++;
  379. }
  380. }
  381. return cnt;
  382. }
  383. int amf_si_su_get_saAmfSINumCurrStandbyAssignments (struct amf_si *si,
  384. struct amf_su *su)
  385. {
  386. int cnt = 0;
  387. struct amf_si_assignment *si_assignment;
  388. for (si_assignment = si->assigned_sis; si_assignment != NULL;
  389. si_assignment = si_assignment->next) {
  390. if (si_assignment->su == su &&
  391. si_assignment->saAmfSISUHAState == SA_AMF_HA_STANDBY) {
  392. cnt++;
  393. }
  394. }
  395. return cnt;
  396. }
  397. SaAmfAssignmentStateT amf_si_get_saAmfSIAssignmentState (struct amf_si *si)
  398. {
  399. if ((amf_si_get_saAmfSINumCurrActiveAssignments (si) ==
  400. si->saAmfSIPrefActiveAssignments) &&
  401. (amf_si_get_saAmfSINumCurrStandbyAssignments (si) ==
  402. si->saAmfSIPrefStandbyAssignments)) {
  403. return SA_AMF_ASSIGNMENT_FULLY_ASSIGNED;
  404. } else if (amf_si_get_saAmfSINumCurrActiveAssignments (si) == 0) {
  405. return SA_AMF_ASSIGNMENT_UNASSIGNED;
  406. } else {
  407. return SA_AMF_ASSIGNMENT_PARTIALLY_ASSIGNED;
  408. }
  409. }
  410. void amf_csi_delete_assignments (struct amf_csi *csi, struct amf_su *su)
  411. {
  412. struct amf_csi_assignment *csi_assignment;
  413. ENTER ("'%s'", su->name.value);
  414. struct amf_csi_assignment **prev = &csi->assigned_csis;
  415. for (csi_assignment = csi->assigned_csis; csi_assignment != NULL;
  416. csi_assignment = csi_assignment->next) {
  417. if (csi_assignment->comp->su == su) {
  418. struct amf_csi_assignment *tmp = csi_assignment;
  419. *prev = csi_assignment->next;
  420. dprintf ("CSI assignment %s unlinked", tmp->name.value);
  421. free (tmp);
  422. } else {
  423. prev = &csi_assignment->next;
  424. }
  425. }
  426. }
  427. /**
  428. * Constructor for SI objects. Adds SI last in the ordered
  429. * list owned by the specified application. Always returns a
  430. * valid SI object, out-of-memory problems are handled here.
  431. * Default values are initialized.
  432. * @param app
  433. *
  434. * @return struct amf_si*
  435. */
  436. struct amf_si *amf_si_new (struct amf_application *app, char *name)
  437. {
  438. struct amf_si *tail = app->si_head;
  439. struct amf_si *si = calloc (1, sizeof (struct amf_si));
  440. if (si == NULL) {
  441. openais_exit_error (AIS_DONE_OUT_OF_MEMORY);
  442. }
  443. while (tail != NULL) {
  444. if (tail->next == NULL) {
  445. break;
  446. }
  447. tail = tail->next;
  448. }
  449. if (tail == NULL) {
  450. app->si_head = si;
  451. } else {
  452. tail->next = si;
  453. }
  454. si->application = app;
  455. /* setup default values from spec. */
  456. si->saAmfSIAdminState = SA_AMF_ADMIN_UNLOCKED;
  457. si->saAmfSIRank = 0;
  458. si->saAmfSIPrefActiveAssignments = 1;
  459. si->saAmfSIPrefStandbyAssignments = 1;
  460. si->assigned_sis = NULL;
  461. si->csi_head = NULL;
  462. setSaNameT (&si->name, name);
  463. return si;
  464. }
  465. void amf_si_delete (struct amf_si *si)
  466. {
  467. struct amf_si_assignment *si_assignment;
  468. struct amf_csi *csi;
  469. for (csi = si->csi_head; csi != NULL;) {
  470. struct amf_csi *tmp = csi;
  471. csi = csi->next;
  472. amf_csi_delete (tmp);
  473. }
  474. for (si_assignment = si->assigned_sis; si_assignment != NULL;) {
  475. struct amf_si_assignment *tmp = si_assignment;
  476. si_assignment = si_assignment->next;
  477. free (tmp);
  478. }
  479. free (si);
  480. }
  481. void *amf_si_serialize (struct amf_si *si, int *len)
  482. {
  483. int objsz = sizeof (struct amf_si);
  484. struct amf_si *copy;
  485. copy = amf_malloc (objsz);
  486. memcpy (copy, si, objsz);
  487. *len = objsz;
  488. TRACE8 ("%s", copy->name.value);
  489. return copy;
  490. }
  491. struct amf_si *amf_si_deserialize (
  492. struct amf_application *app, char *buf, int size)
  493. {
  494. int objsz = sizeof (struct amf_si);
  495. if (objsz > size) {
  496. return NULL;
  497. } else {
  498. struct amf_si *tmp = (struct amf_si*) buf;
  499. struct amf_si *si = amf_si_new (app, (char*)tmp->name.value);
  500. TRACE8 ("%s", si->name.value);
  501. memcpy (&si->saAmfSIProtectedbySG, &tmp->saAmfSIProtectedbySG,
  502. sizeof (SaNameT));
  503. si->saAmfSIRank = tmp->saAmfSIRank;
  504. si->saAmfSINumCSIs = tmp->saAmfSINumCSIs;
  505. si->saAmfSIPrefActiveAssignments = tmp->saAmfSIPrefActiveAssignments;
  506. si->saAmfSIPrefStandbyAssignments = tmp->saAmfSIPrefStandbyAssignments;
  507. si->saAmfSIAdminState = tmp->saAmfSIAdminState;
  508. return si;
  509. }
  510. }
  511. /*****************************************************************************
  512. * SI Assignment class implementation *
  513. ****************************************************************************/
  514. struct amf_si_assignment *amf_si_assignment_new (struct amf_si *si)
  515. {
  516. struct amf_si_assignment *si_assignment;
  517. si_assignment = amf_malloc (sizeof (struct amf_si_assignment));
  518. si_assignment->si = si;
  519. return si_assignment;
  520. }
  521. void *amf_si_assignment_serialize (
  522. struct amf_si_assignment *si_assignment, int *len)
  523. {
  524. int objsz = sizeof (struct amf_si_assignment);
  525. struct amf_si_assignment *copy;
  526. copy = amf_malloc (objsz);
  527. memcpy (copy, si_assignment, objsz);
  528. *len = objsz;
  529. TRACE8 ("%s", copy->name.value);
  530. return copy;
  531. }
  532. struct amf_si_assignment *amf_si_assignment_deserialize (
  533. struct amf_si *si, char *buf, int size)
  534. {
  535. int objsz = sizeof (struct amf_si_assignment);
  536. if (objsz > size) {
  537. return NULL;
  538. } else {
  539. struct amf_si_assignment *obj = amf_si_assignment_new (si);
  540. if (obj == NULL) {
  541. return NULL;
  542. }
  543. memcpy (obj, buf, objsz);
  544. TRACE8 ("%s", obj->name.value);
  545. obj->si = si;
  546. obj->su = amf_su_find (si->application->cluster, &obj->name);
  547. obj->next = si->assigned_sis;
  548. si->assigned_sis = obj;
  549. return obj;
  550. }
  551. }
  552. struct amf_si *amf_si_find (struct amf_application *app, char *name)
  553. {
  554. struct amf_si *si;
  555. for (si = app->si_head; si != NULL; si = si->next) {
  556. if (strncmp (name, (char*)si->name.value, si->name.length) == 0) {
  557. break;
  558. }
  559. }
  560. if (si == NULL) {
  561. dprintf ("SI %s not found!", name);
  562. }
  563. return si;
  564. }
  565. /*****************************************************************************
  566. * CSI class implementation *
  567. ****************************************************************************/
  568. struct amf_csi *amf_csi_new (struct amf_si *si)
  569. {
  570. struct amf_csi *csi;
  571. csi = amf_malloc (sizeof (struct amf_csi));
  572. csi->si = si;
  573. return csi;
  574. }
  575. void amf_csi_delete (struct amf_csi *csi)
  576. {
  577. struct amf_csi_assignment *csi_assignment;
  578. for (csi_assignment = csi->assigned_csis; csi_assignment != NULL;) {
  579. struct amf_csi_assignment *tmp = csi_assignment;
  580. csi_assignment = csi_assignment->next;
  581. free (tmp);
  582. }
  583. free (csi);
  584. }
  585. void *amf_csi_serialize (struct amf_csi *csi, int *len)
  586. {
  587. int objsz = sizeof (struct amf_csi);
  588. struct amf_csi *copy;
  589. copy = amf_malloc (objsz);
  590. memcpy (copy, csi, objsz);
  591. *len = objsz;
  592. TRACE8 ("%s", copy->name.value);
  593. return copy;
  594. }
  595. struct amf_csi *amf_csi_deserialize (struct amf_si *si, char *buf, int size)
  596. {
  597. int objsz = sizeof (struct amf_csi);
  598. if (objsz > size) {
  599. return NULL;
  600. } else {
  601. struct amf_csi *obj = amf_csi_new (si);
  602. if (obj == NULL) {
  603. return NULL;
  604. }
  605. memcpy (obj, buf, objsz);
  606. TRACE8 ("%s", obj->name.value);
  607. obj->si = si;
  608. obj->assigned_csis = NULL;
  609. obj->attributes_head = NULL;
  610. obj->next = si->csi_head;
  611. si->csi_head = obj;
  612. return obj;
  613. }
  614. }
  615. struct amf_csi *amf_csi_find (struct amf_si *si, char *name)
  616. {
  617. struct amf_csi *csi;
  618. for (csi = si->csi_head; csi != NULL; csi = csi->next) {
  619. if (strncmp (name, (char*)csi->name.value, csi->name.length) == 0) {
  620. break;
  621. }
  622. }
  623. if (csi == NULL) {
  624. dprintf ("CSI %s not found!", name);
  625. }
  626. return csi;
  627. }
  628. /*****************************************************************************
  629. * CSI Assignment class implementation *
  630. ****************************************************************************/
  631. struct amf_csi_assignment *amf_csi_assignment_new (struct amf_csi *csi)
  632. {
  633. struct amf_csi_assignment *csi_assignment;
  634. csi_assignment = amf_malloc (sizeof (struct amf_csi_assignment));
  635. csi_assignment->csi = csi;
  636. return csi_assignment;
  637. }
  638. void *amf_csi_assignment_serialize (
  639. struct amf_csi_assignment *csi_assignment, int *len)
  640. {
  641. int objsz = sizeof (struct amf_csi_assignment);
  642. struct amf_csi_assignment *copy;
  643. copy = amf_malloc (objsz);
  644. memcpy (copy, csi_assignment, objsz);
  645. *len = objsz;
  646. TRACE8 ("%s", copy->name.value);
  647. return copy;
  648. }
  649. struct amf_si_assignment *si_assignment_find (
  650. struct amf_csi_assignment *csi_assignment)
  651. {
  652. struct amf_comp *component;
  653. struct amf_si_assignment *si_assignment = NULL;
  654. component = amf_comp_find(csi_assignment->csi->si->application->cluster,
  655. &csi_assignment->name);
  656. for (si_assignment = csi_assignment->csi->si->assigned_sis;
  657. si_assignment != NULL; si_assignment = si_assignment->next) {
  658. SaNameT su_name;
  659. amf_su_dn_make (component->su,&su_name);
  660. if (name_match(&su_name, &si_assignment->name)) {
  661. break;
  662. }
  663. }
  664. return si_assignment;
  665. }
  666. struct amf_csi_assignment *amf_csi_assignment_deserialize (
  667. struct amf_csi *csi, char *buf, int size)
  668. {
  669. int objsz = sizeof (struct amf_csi_assignment);
  670. if (objsz > size) {
  671. return NULL;
  672. } else {
  673. struct amf_csi_assignment *obj = amf_csi_assignment_new (csi);
  674. if (obj == NULL) {
  675. return NULL;
  676. }
  677. memcpy (obj, buf, objsz);
  678. TRACE8 ("%s", obj->name.value);
  679. obj->csi = csi;
  680. obj->comp = amf_comp_find (csi->si->application->cluster, &obj->name);
  681. assert (obj->comp != NULL);
  682. obj->next = csi->assigned_csis;
  683. csi->assigned_csis = obj;
  684. obj->si_assignment = si_assignment_find(obj);
  685. return obj;
  686. }
  687. }
  688. char *amf_csi_assignment_dn_make (
  689. struct amf_csi_assignment *csi_assignment, SaNameT *name)
  690. {
  691. SaNameT comp_name;
  692. struct amf_csi *csi = csi_assignment->csi;
  693. int i;
  694. amf_comp_dn_make (csi_assignment->comp, &comp_name);
  695. i = snprintf((char*) name->value, SA_MAX_NAME_LENGTH,
  696. "safCSIComp=%s,safCsi=%s,safSi=%s,safApp=%s",
  697. comp_name.value,
  698. csi->name.value, csi->si->name.value,
  699. csi->si->application->name.value);
  700. assert (i <= SA_MAX_NAME_LENGTH);
  701. name->length = i;
  702. return(char *)name->value;
  703. }
  704. struct amf_csi_assignment *amf_csi_assignment_find (
  705. struct amf_cluster *cluster, SaNameT *name)
  706. {
  707. struct amf_application *app;
  708. struct amf_si *si;
  709. struct amf_csi *csi;
  710. struct amf_csi_assignment *csi_assignment = NULL;
  711. char *app_name;
  712. char *si_name;
  713. char *csi_name;
  714. char *csi_assignment_name;
  715. char *buf;
  716. /* malloc new buffer since we need to write to the buffer */
  717. buf = amf_malloc (name->length + 1);
  718. memcpy (buf, name->value, name->length + 1);
  719. csi_assignment_name = strstr (buf, "safCSIComp=");
  720. csi_name = strstr (buf, "safCsi=");
  721. si_name = strstr (buf, "safSi=");
  722. app_name = strstr (buf, "safApp=");
  723. app_name++;
  724. app_name = strstr (app_name, "safApp=");
  725. if (csi_assignment_name == NULL || csi_name == NULL || si_name == NULL ||
  726. app_name == NULL) {
  727. goto end;
  728. }
  729. *(csi_name - 1) = '\0';
  730. *(si_name - 1) = '\0';
  731. *(app_name - 1) = '\0';
  732. /* jump to value */
  733. csi_assignment_name += 11;
  734. csi_name += 7;
  735. si_name += 6;
  736. app_name += 7;
  737. app = amf_application_find (cluster, app_name);
  738. if (app == NULL) {
  739. goto end;
  740. }
  741. si = amf_si_find (app, si_name);
  742. if (si == NULL) {
  743. goto end;
  744. }
  745. csi = amf_csi_find (si, csi_name);
  746. if (csi == NULL) {
  747. goto end;
  748. }
  749. for (csi_assignment = csi->assigned_csis; csi_assignment != NULL;
  750. csi_assignment = csi_assignment->next) {
  751. if (strncmp (csi_assignment_name,
  752. (char*)csi_assignment->name.value,
  753. csi_assignment->name.length) == 0) {
  754. goto end;
  755. }
  756. }
  757. end:
  758. free (buf);
  759. return csi_assignment;
  760. }
  761. struct amf_csi_attribute *amf_csi_attribute_new (struct amf_csi *csi)
  762. {
  763. struct amf_csi_attribute *csi_attribute;
  764. csi_attribute = amf_malloc (sizeof (struct amf_csi_assignment));
  765. csi_attribute->next = csi->attributes_head;
  766. csi->attributes_head = csi_attribute;
  767. return csi_attribute;
  768. }
  769. void *amf_csi_attribute_serialize (
  770. struct amf_csi_attribute *csi_attribute, int *len)
  771. {
  772. char *buf = NULL;
  773. int i, offset = 0, size = 0;
  774. TRACE8 ("%s", csi_attribute->name);
  775. buf = amf_serialize_SaStringT (buf, &size, &offset, csi_attribute->name);
  776. /* count value and write to buf */
  777. for (i = 0; csi_attribute->value &&
  778. csi_attribute->value[i] != NULL; i++);
  779. buf = amf_serialize_SaUint32T (buf, &size, &offset, i);
  780. for (i = 0; csi_attribute->value &&
  781. csi_attribute->value[i] != NULL; i++) {
  782. buf = amf_serialize_SaStringT (
  783. buf, &size, &offset, csi_attribute->value[i]);
  784. }
  785. *len = offset;
  786. return buf;
  787. }
  788. struct amf_csi_attribute *amf_csi_attribute_deserialize (
  789. struct amf_csi *csi, char *buf, int size)
  790. {
  791. char *tmp = buf;
  792. struct amf_csi_attribute *csi_attribute;
  793. int i;
  794. SaUint32T cnt;
  795. csi_attribute = amf_csi_attribute_new (csi);
  796. tmp = amf_deserialize_SaStringT (tmp, &csi_attribute->name);
  797. tmp = amf_deserialize_SaUint32T (tmp, &cnt);
  798. csi_attribute->value = amf_malloc ((cnt + 1) * sizeof (SaStringT*));
  799. for (i = 0; i < cnt; i++) {
  800. tmp = amf_deserialize_SaStringT (tmp, &csi_attribute->value[i]);
  801. }
  802. csi_attribute->value[i] = NULL;
  803. return csi_attribute;
  804. }