@@ -48,6 +48,10 @@ class MfbGenerator(nfb.BaseComp):
48
48
def __init__ (self , * args , ** kwargs ):
49
49
super ().__init__ (* args , ** kwargs )
50
50
self ._logger = logging .getLogger ("MfbGenerator" )
51
+ self ._cfg_type_d = {
52
+ f .name : f .type for f in fields (GeneratorConfig )
53
+ if "Optional" not in str (f .type ) # filter out read only values
54
+ }
51
55
52
56
# ################
53
57
# Command register
@@ -347,6 +351,57 @@ def configure(self, conf: GeneratorConfig) -> None:
347
351
self .src_ip_address_mask = conf .src_ip_address_mask
348
352
self .enabled = conf .enabled
349
353
354
+ def _convert_attr (self , attr_name : str , attr_value : str ) -> Any :
355
+ if "mac" in attr_name :
356
+ return MfbGenerator .convert_mac2bytes (attr_value )
357
+
358
+ if "mask" in attr_name :
359
+ return int (attr_value , 16 )
360
+
361
+ prop_type = self ._cfg_type_d [attr_name ]
362
+ return prop_type (attr_value )
363
+
364
+ def configure_attr (self , attr_name : str , str_value : str ):
365
+ """Configure a config attribute.
366
+
367
+ This method aims to enable dynamic configuration using string names of data class
368
+ arguments and string values, that are going to be converted to int/bytes/bool.
369
+ For other usages, property setters are recommended.
370
+
371
+ Args:
372
+ attr_name: Attribute name of GeneratorConfig data class.
373
+ NOTE that read-only attributes are not allowed.
374
+ str_value: Value to be set for given attribute.
375
+
376
+ Raises:
377
+ ValueError: If an invalid attribute name is passed.
378
+ AttributeError: If there is an inconsistency between this class's properties and
379
+ GeneratorConfig attributes.
380
+ IOError: If the given attribute cannot be configured.
381
+ """
382
+
383
+ if attr_name not in self ._cfg_type_d .keys ():
384
+ attrs_str = ",\n \t " .join (self ._cfg_type_d .keys ())
385
+ raise ValueError (
386
+ f"Cannot configure { attr_name } ,"
387
+ f" not in supported list of attributes:\n \t { attrs_str } "
388
+ )
389
+
390
+ if attr_name not in dir (self ):
391
+ raise AttributeError ("Internal error!" )
392
+
393
+ if attr_name == "src_ip_address_mask" :
394
+ version = self ._node .get_property ("version" ).value
395
+ if version < 2 :
396
+ raise IOError (
397
+ f"Cannot set src_ip_address_mask, version of MFB generator is { version } ,"
398
+ f" but minimal version 2 is required"
399
+ )
400
+
401
+ prop_obj = getattr (type (self ), attr_name )
402
+ value = self ._convert_attr (attr_name , str_value )
403
+ prop_obj .fset (self , value )
404
+
350
405
def get_fconfiguration (self ) -> List :
351
406
"""Returns formatted configuration of the generator as a list of [item, value] lists."""
352
407
conf = self .get_configuration ()
0 commit comments