libamxo  4.3.4
Object Definition Language (ODL) parsing
ODL Parser MIBS
Collaboration diagram for ODL Parser MIBS:

Functions

int amxo_parser_scan_mib_dir (amxo_parser_t *parser, const char *path)
 Scans a directory for MIB odl files. More...
 
int amxo_parser_scan_mib_dirs (amxo_parser_t *parser, amxc_var_t *dirs)
 Scans multiple directories for MIB odl files. More...
 
const char * amxo_parser_get_mib_file (amxo_parser_t *parser, const char *mib_name)
 Get full path and file name of odl file describing a mib. More...
 
int amxo_parser_load_mib (amxo_parser_t *parser, amxd_dm_t *dm, const char *mib_name)
 Loads the mib definition. More...
 
int amxo_parser_apply_mib (amxo_parser_t *parser, amxd_object_t *object, const char *mib_name)
 Unconditionally applies a MIB to a data model object. More...
 
int amxo_parser_add_mibs (amxo_parser_t *parser, amxd_object_t *object, amxo_evaluate_expr_fn_t fn)
 Adds zero, one or more MIBs to a data model object. More...
 
int amxo_parser_remove_mibs (amxo_parser_t *parser, amxd_object_t *object, amxo_evaluate_expr_fn_t fn)
 Removes zero, one or more MIBs from a data model object. More...
 
int amxo_parser_apply_mibs (amxo_parser_t *parser, amxd_object_t *object, amxo_evaluate_expr_fn_t fn)
 Applies zero, one or more MIBs to a data model object. More...
 

Detailed Description

Ambiorix ODL parser mibs API

MIBS are data mode extention objects, A MIB can be applied to any object in the data model. The object get extended with the parameters, functions and objects defined in the MIB.

MIBS can be defined in separate ODL files. Each MIB odl file contains exactly one MIB definition. The file must have the same name as the MIB definition.

The MIB odl files can contain on the first line a expression definition.

The MIBS are not loaded, until they are needed. Loading the MIBS and applying them on objects in the data model can be done using amxo_parser_apply_mib or amxo_parser_apply_mibs

Function Documentation

◆ amxo_parser_add_mibs()

int amxo_parser_add_mibs ( amxo_parser_t parser,
amxd_object_t *  object,
amxo_evaluate_expr_fn_t  fn 
)

Adds zero, one or more MIBs to a data model object.

This function loops over all known MIB odl files, and will apply each MIB to the provided data model object if the object is matching the MIB expression.

When an expression evaluation function is provided, the MIB expression will be passed to that function together with the data model object. When the function returns true the MIB will be applied on the data model object. If the MIB does not have an expression defined, the name of the MIB is used as an expression.

No MIBs are applied to the data model object if no expression evaluation function is provided.

To unconditionally apply a single MIB to a data model object use amxo_parser_apply_mib

Note
Before calling this function one or more directories must be scanned for MIB odl files. Scanning a directory can be done using amxo_parser_scan_mib_dir or amxo_parser_scan_mib_dirs
Parameters
parserthe odl parser instance
objectthe data model object
fnfunction that evaluates the MIB expression.
Returns
Total number of MIBs added on the object

Definition at line 315 of file amxo_parser_mibs.c.

317  {
318  int retval = 0;
319 
320  when_null(parser, exit);
321  when_null(object, exit);
322  when_null(fn, exit);
323 
324  amxc_htable_for_each(it, (&parser->mibs)) {
325  const char* mib_name = amxc_htable_it_get_key(it);
326  mib_info_t* info = amxc_htable_it_get_data(it, mib_info_t, hit);
327  amxp_expr_t expr;
328  amxp_expr_init(&expr, info->expression);
329  if(fn(object, &expr)) {
330  if(amxo_parser_apply_mib(parser, object, mib_name) == 0) {
331  retval++;
332  }
333  }
334  amxp_expr_clean(&expr);
335  }
336 
337 exit:
338  return retval;
339 }
int amxo_parser_apply_mib(amxo_parser_t *parser, amxd_object_t *object, const char *mib_name)
Unconditionally applies a MIB to a data model object.
amxc_htable_t mibs
Definition: amxo_types.h:282
char * expression

◆ amxo_parser_apply_mib()

int amxo_parser_apply_mib ( amxo_parser_t parser,
amxd_object_t *  object,
const char *  mib_name 
)

Unconditionally applies a MIB to a data model object.

Loads the MIB odl file, if not already loaded and applies the MIB on the provided data model object. If an expression was set in the MIB odl file, the expression is ignored. The MIB is unconditionally applied on the data model object.

Parameters
parserthe odl parser instance
objectthe data model object
mib_namethe name of the mib.
Returns
Returns 0 when success, any other value indicates failure.

Definition at line 291 of file amxo_parser_mibs.c.

293  {
294  int retval = -1;
295  amxd_status_t status = amxd_status_ok;
296  amxd_dm_t* dm = NULL;
297 
298  when_null(parser, exit);
299  when_null(object, exit);
300  when_str_empty(mib_name, exit);
301 
302  dm = amxd_object_get_dm(object);
303  when_null(dm, exit);
304 
305  retval = amxo_parser_load_mib(parser, dm, mib_name);
306  when_failed(retval, exit);
307 
308  status = amxd_object_add_mib(object, mib_name);
309  retval = status == amxd_status_ok ? 0 : -1;
310 
311 exit:
312  return retval;
313 }
int amxo_parser_load_mib(amxo_parser_t *parser, amxd_dm_t *dm, const char *mib_name)
Loads the mib definition.

◆ amxo_parser_apply_mibs()

int amxo_parser_apply_mibs ( amxo_parser_t parser,
amxd_object_t *  object,
amxo_evaluate_expr_fn_t  fn 
)

Applies zero, one or more MIBs to a data model object.

This function loops over all known MIB odl files, and will add each MIB to the provided data model object if the object matches the MIB expression or removes the MIB from the data model object of the object is not matching the expression anymore.

When an expression evaluation function is provided, the MIB expression will be passed to that function together with the data model object. When the function returns true the MIB will be added on the data model object otherwise removed. If the MIB ODL files does not have an expression defined, the name of the MIB is used as an expression.

No MIBs are applied if no expression evaluation function is provided.

Parameters
parserthe odl parser instance
objectthe data model object
fnfunction that evaluates the MIB expression.
Returns
Total number of MIBs applied on the object

Definition at line 370 of file amxo_parser_mibs.c.

372  {
373  int retval = 0;
374 
375  when_null(parser, exit);
376  when_null(object, exit);
377  when_null(fn, exit);
378 
379  amxc_htable_for_each(it, (&parser->mibs)) {
380  const char* mib_name = amxc_htable_it_get_key(it);
381  mib_info_t* info = amxc_htable_it_get_data(it, mib_info_t, hit);
382  amxp_expr_t expr;
383  amxp_expr_init(&expr, info->expression);
384  if(fn(object, &expr)) {
385  if(amxo_parser_apply_mib(parser, object, mib_name) == 0) {
386  retval++;
387  }
388  } else {
389  if(amxd_object_remove_mib(object, mib_name) == amxd_status_ok) {
390  retval++;
391  }
392  }
393  amxp_expr_clean(&expr);
394  }
395 
396 exit:
397  return retval;
398 }

◆ amxo_parser_get_mib_file()

const char* amxo_parser_get_mib_file ( amxo_parser_t parser,
const char *  mib_name 
)

Get full path and file name of odl file describing a mib.

After scanning the mib directories using amxo_parser_scan_mib_dir or amxo_parser_scan_mib_dirs, this function returns the file name for a mib definition.

The mib can be loaded using amxo_parser_parse_file

Parameters
parserthe odl parser instance
mib_namename of the mib.
Returns
Returns a string containing the full path and file name of the file containing the mib definition or NULL when no file found

Definition at line 242 of file amxo_parser_mibs.c.

243  {
244  const char* file = NULL;
245  mib_info_t* info = NULL;
246  amxc_htable_it_t* it = NULL;
247 
248  when_null(parser, exit);
249  when_str_empty(mib_name, exit);
250 
251  it = amxc_htable_get(&parser->mibs, mib_name);
252  while(it == NULL && parser->parent != NULL) {
253  parser = parser->parent;
254  it = amxc_htable_get(&parser->mibs, mib_name);
255  }
256  when_null(it, exit);
257  info = amxc_htable_it_get_data(it, mib_info_t, hit);
258 
259  file = info->file;
260 
261 exit:
262  return file;
263 }
amxo_parser_t * parent
Definition: amxo_types.h:285

◆ amxo_parser_load_mib()

int amxo_parser_load_mib ( amxo_parser_t parser,
amxd_dm_t *  dm,
const char *  mib_name 
)

Loads the mib definition.

Mibs can be loaded by mib name after amxo_parser_scan_mib_dir or amxo_parser_scan_mib_dirs was called.

This function calls:

If the mib was already loaded nothing is done.

Parameters
parserthe odl parser instance
dmdata model for which the mib must be loaded
mib_namename of the mib.
Returns
Returns 0 when success, any other value indicates failure.

Definition at line 265 of file amxo_parser_mibs.c.

267  {
268  int retval = -1;
269  amxd_status_t status = amxd_status_ok;
270  amxd_object_t* mib = NULL;
271 
272  when_null(parser, exit);
273  when_str_empty(mib_name, exit);
274  when_null(dm, exit);
275 
276  mib = amxd_dm_get_mib(dm, mib_name);
277  if(mib == NULL) {
278  const char* file = amxo_parser_get_mib_file(parser, mib_name);
279  amxd_object_t* root = amxd_dm_get_root(dm);
280  retval = amxo_parser_parse_file(parser, file, root);
281  when_true(retval != 0, exit);
282  }
283 
284  retval = status == amxd_status_ok ? 0 : -1;
285 
286 exit:
287  return retval;
288 
289 }
const char * amxo_parser_get_mib_file(amxo_parser_t *parser, const char *mib_name)
Get full path and file name of odl file describing a mib.
int amxo_parser_parse_file(amxo_parser_t *parser, const char *file_path, amxd_object_t *object)
Parses an odl file.

◆ amxo_parser_remove_mibs()

int amxo_parser_remove_mibs ( amxo_parser_t parser,
amxd_object_t *  object,
amxo_evaluate_expr_fn_t  fn 
)

Removes zero, one or more MIBs from a data model object.

This function loops over all known MIB odl files, and will remove each MIB from the provided data model object if the MIB was added to the object and the object is not matching the MIB expression any more.

When an expression evaluation function is provided, the MIB expression will be passed to that function together with the data model object. When the function returns false the MIB will be removed from the data model object. If the MIB does not have an expression defined, the name of the MIB is used as an expression.

No MIBs are removed from the data model object if no expression evaluation function is provided.

To unconditionally remove a single MIB from a data model object use the data model function amxd_object_remove_mib.

Note
Before calling this function one or more directories must be scanned for MIB odl files. Scanning a directory can be done using amxo_parser_scan_mib_dir or amxo_parser_scan_mib_dirs
Parameters
parserthe odl parser instance
objectthe data model object
fnfunction that evaluates the MIB expression.
Returns
Total number of MIBs removed from the object

Definition at line 341 of file amxo_parser_mibs.c.

343  {
344  int retval = 0;
345 
346  when_null(parser, exit);
347  when_null(object, exit);
348  when_null(fn, exit);
349 
350  amxc_htable_for_each(it, (&parser->mibs)) {
351  const char* mib_name = amxc_htable_it_get_key(it);
352  mib_info_t* info = amxc_htable_it_get_data(it, mib_info_t, hit);
353  amxp_expr_t expr;
354  if(!amxd_object_has_mib(object, mib_name)) {
355  continue;
356  }
357  amxp_expr_init(&expr, info->expression);
358  if(!fn(object, &expr)) {
359  if(amxd_object_remove_mib(object, mib_name) == amxd_status_ok) {
360  retval++;
361  }
362  }
363  amxp_expr_clean(&expr);
364  }
365 
366 exit:
367  return retval;
368 }

◆ amxo_parser_scan_mib_dir()

int amxo_parser_scan_mib_dir ( amxo_parser_t parser,
const char *  path 
)

Scans a directory for MIB odl files.

Each odl file found in the given path must contain exactly one MIB definition. The name of the MIB and the name of the file must be the same.

The MIB odl definition can contain at the first line an expression. The expression must be added as a comment and must start with "expr:"

Parameters
parserthe odl parser instance
paththe path that needs to be scanned
Returns
Returns 0 when success, any other value indicates failure.

Definition at line 186 of file amxo_parser_mibs.c.

187  {
188  int retval = -1;
189  char* current_wd = getcwd(NULL, 0);
190  char* real_path = NULL;
191  amxc_string_t res_path;
192  amxc_string_init(&res_path, 0);
193 
194  when_null(parser, exit);
195  when_str_empty(path, exit);
196 
197  real_path = realpath(path, NULL);
198  if(real_path != NULL) {
199  retval = amxo_parser_scan(parser, real_path);
200  }
201 
202 exit:
203  amxc_string_clean(&res_path);
204  free(current_wd);
205  free(real_path);
206  return retval;
207 }
static int amxo_parser_scan(amxo_parser_t *parser, const char *path)

◆ amxo_parser_scan_mib_dirs()

int amxo_parser_scan_mib_dirs ( amxo_parser_t parser,
amxc_var_t *  dirs 
)

Scans multiple directories for MIB odl files.

The directories must be provided as a variant containing a list of variants, where each variant contains a path.

Parameters
parserthe odl parser instance
dirsa variant containing a list of variants, each containing a path.
Returns
Returns 0 when success, any other value indicates failure.

Definition at line 209 of file amxo_parser_mibs.c.

210  {
211  int retval = -1;
212  amxc_string_t res_path;
213  amxc_string_init(&res_path, 0);
214 
215  when_null(parser, exit);
216  if(dirs == NULL) {
217  dirs = amxo_parser_get_config(parser, "mib-dirs");
218  }
219  when_null(dirs, exit);
220  when_true(amxc_var_type_of(dirs) != AMXC_VAR_ID_LIST, exit);
221 
222  amxc_var_for_each(var_path, dirs) {
223  const char* path = amxc_var_constcast(cstring_t, var_path);
224  if(path == NULL) {
225  continue;
226  }
227  if(amxc_string_set_resolved(&res_path, path, &parser->config) > 0) {
228  path = amxc_string_get(&res_path, 0);
229  }
230  retval = amxo_parser_scan_mib_dir(parser, path);
231  if(retval != 0) {
232  break;
233  }
234  amxc_string_reset(&res_path);
235  }
236 
237 exit:
238  amxc_string_clean(&res_path);
239  return retval;
240 }
amxc_var_t * amxo_parser_get_config(amxo_parser_t *parser, const char *path)
Gets a configuration option.
int amxo_parser_scan_mib_dir(amxo_parser_t *parser, const char *path)
Scans a directory for MIB odl files.
amxc_var_t config
Definition: amxo_types.h:250