p4utils.utils.helper module

This module includes all the functions that are frequently used in different parts of the code. These functions usually perform low level operations on data.

class p4utils.utils.helper.WrapFunc(func)[source]

Bases: object

Wraps a function is such a way that they can be executed across different Python interpreters in the same system.

Parameters:func (types.FunctionType) – function to wrap
unwrap()[source]

Unwraps function and returns it.

p4utils.utils.helper.check_listening_on_port(port)[source]

Checks if the given port is listening in the main namespace.

Parameters:port (int) – port number
Returns:True if the port is listening, False otherwise.
Return type:bool
p4utils.utils.helper.cksum(filename)[source]

Returns the md5 checksum of a file.

Parameters:filename (str) – path to the file
Returns:md5 checksum of the file.
Return type:str
p4utils.utils.helper.dpidToStr(id)[source]

Compute a string dpid from an integer id.

Parameters:id (int) – integer device id
Returns:device dpid.
Return type:str
p4utils.utils.helper.get_by_attr(attr_name, attr_value, obj_list)[source]

Return the first object in the list that has an attribute matching with the attribute name and value provided.

Parameters:
  • attr_name (string) – attribute name
  • attr_value – attrubute value
  • obj_list (list) – list of objects
Returns:

the requested object or None.

Return type:

object

p4utils.utils.helper.get_node_attr(node, attr_name, default=None)[source]

Finds the value of the specified attribute of a Mininet node by looking also inside its unparsed parameters.

Parameters:
  • node (object) – Mininet node object
  • attr_name (string) – attribute to look for
Returns:

the value of the requested attribute.

p4utils.utils.helper.ip_address_to_mac(ip)[source]

Generate MAC from IP address.

Parameters:ip (str) – IPv4 address
Returns:MAC address obtained from the IPv4 value.
Return type:str
p4utils.utils.helper.is_compiled(p4_src, compilers)[source]

Check if a file has been already compiled by at least one compiler in the list.

Parameters:
  • p4_src (string) – P4 file path
  • compilers (list) – list of P4 compiler objects (see compiler.py)
Returns:

True if the file has been already compiled, False otherwise.

Return type:

bool

p4utils.utils.helper.kill_proc_tree(pid, sig=<Signals.SIGKILL: 9>, include_parent=True, timeout=None, on_terminate=None)[source]

Kills a process tree (including children).

Parameters:
  • pid (int) – PID of the parent process
  • sig (int) – signal used to kill the tree
  • include_parent (bool) – whether to kill the parent process or not
  • timeout (int or float) – time to wait for a process to terminate
  • on_terminate (types.FunctionType) – callback function executed as soon as a child terminates.
Returns:

(gone, still_alive).

Return type:

tuple

p4utils.utils.helper.load_conf(conf_file)[source]

Load JSON application configuration file.

Parameters:conf_file (str) – path to the JSON network configuration file
Returns:network configuration dictionary.
Return type:dict
p4utils.utils.helper.load_custom_object(obj)[source]

Loads object from module.

Parameters:dict – JSON object to load
Returns:Python object retrieved from the module.
Return type:object

Example

This function takes as input a module JSON object:

{
    "file_path": <path to module> (string) (*),
    "module_name": <module file_name> (string),
    "object_name": <module object name> (string),
}

Note

None of the fields marked with (*) is mandatory. The file_path field is optional and has to be used if the module is not present in sys.path.

p4utils.utils.helper.load_topo(json_path)[source]

Load the topology from the path provided.

Parameters:json_path (string) – path of the JSON file to load
Returns:the topology graph.
Return type:p4utils.utils.topology.NetworkGraph
p4utils.utils.helper.merge_dict(dst, src)[source]

Merges source dictionary fields and subfields into destionation dictionary.

Parameters:
  • dst (dict) – destination dictionary
  • src (dict) – source dictionary
p4utils.utils.helper.next_element(elems, minimum=None, maximum=None)[source]

Given a list of integers, return the lowest number not already present in the set, starting from minimum and ending in maximum.

Parameters:
  • elems (list) – list of integers
  • minimum (int) – minimum value allowed for elements
  • maximum (int) – maximum value allowed for elements
Returns:

the lowest number not already present in the set.

Return type:

int

p4utils.utils.helper.old_run_command(command)[source]

Execute command in the main namespace.

Parameters:command (str) – command to execute
Returns:an integer value used by a process.
Return type:int
p4utils.utils.helper.parse_line(line)[source]

Parse text line returning a list of substrings.

Parameters:line (str) – line to parse
Returns:list of args obtained from the parsing.
Return type:list

Example

As an example, consider the following string:

'ahjdjf djdfkfo1 --jdke hdjejeek --dfjfj "vneovn rijvtg"'

The function will parse it and give as output the following list:

["ahjdjf", "djdfkfo1", "--jdke", "hdjejeek", "--dfjfj", "vneovn rijvtg"]
p4utils.utils.helper.parse_task_line(line, def_mod='p4utils.utils.traffic_utils')[source]

Parse text line and return all the parameters needed to create a task with p4utils.mininetlib.network_API.NetworkAPI.addTask().

Parameters:
  • line (str) – string containing all the task information
  • def_mod (str) – default module where to look for exe functions
Returns:

a tuple (args, kwargs) where args is a list of arguments and kwargs is a dictionary of key-word pairs.

Return type:

tuple

Example

The file has to be a set of lines, where each has the following syntax:

<node> <start> <duration> <exe> [<arg1>] ... [<argN>] [--mod <module>] [--<key1> <kwarg1>] ... [--<keyM> <kwargM>]

Note

A non-default module can be specified in the command with --mod <module>.

p4utils.utils.helper.rand_mac()[source]

Generate a random, non-multicas MAC address.

Returns:MAC address.
Return type:str
p4utils.utils.helper.run_command(command, out_file=None)[source]

Execute command in the main namespace.

Parameters:
  • command (str) – command to execute
  • out_file (str) – where to redirect stdout and stderr
Returns:

returns parent pid.

Return type:

int

p4utils.utils.helper.wait_condition(func, value, args=[], kwargs={}, timeout=0)[source]

Waits for the function to return the specified value.

Parameters:
  • func (types.FunctionType) – function to check
  • value – condition to meet
  • args (list) – positional arguments of the function
  • kwargs (dict) – key-word arguments of the function
  • timeout (float) – time to wait for condition in seconds
Returns:

True if the condition is met before the timeout expires, False otherwise.

Return type:

bool

Note

If timeout is set to 0, this function will wait forever.