5.1. J2CONFIG
5.1.1. DATA COLLECTION
- class j2config.data_collect.ABSRegion(device_details, custom_data_file)
Bases:
ABC
Abstract Base Class Template to define custom/regional dataframe
- Args:
device_details (DataFrame): Pandas DataFrame with device var information custom_data_file (str): custom datafile.
- Inherits:
ABC (ABC): abstract base class
- Abstract Properties:
frames(list) : must be defined in custom class method, which should return a list of DataFrame(s) to override var attributes.
- abstract property frames
must be defined in custom class method, which should return a list of DataFrame(s) to override var attributes.
- class j2config.data_collect.DeviceDetails(device_file)
Bases:
object
Device details operations
- Args:
device_file (str): Excel device database file
- Raises:
Exception: Raised for input error: if device filename missing Exception: Raised for input error: if provided device file missing or read fails.
- Returns:
DeviceDetails: DeviceDetails object
- merge_vars(frames)
merges var details from two different dataframes ( region and device - databases ) (support definition)
- Args:
frames (list): list of DataFrame(s) to be merged
- Returns:
DataFrame: merged DataFrame (var)
- merge_with_var_frames(regional_frames)
merge device var details with provided custom regional DataFrame(s) custom regional frame variables/values overrides device var variables/values.
- Args:
regional_frames (list): list of custom regional DataFrames to be added to var.
- merged_table_columns()
merges all different type of interfaces/protocols details in to a single dataframe.
- Returns:
DataFrame: Pandas DataFrame object collecting all interfaces/protocols details
- read_device()
reads device database
- Returns:
dict: dictionary of dataframe
- read_table()
reads table dataframe and add var/table to dataframe dictionary
- verify_input_dev_file()
check if provided input device file
- Raises:
Exception: Raised for input error: if device filename missing Exception: Raised for input error: if provided device file missing or read fails.
- j2config.data_collect.read_excel(file)
read excel file, all worksheet
- Args:
file (str): excel file name
- Returns:
dict: dictionary of dataframes.
- j2config.data_collect.read_worksheet(file, wks)
read an excel worksheet
- Args:
file (str): excel file name wks (str): worksheet name
- Raises:
Exception: Raised for file read fail, or worksheet missing
- Returns:
DataFrame: Pandas DataFrame Object
- j2config.data_collect.to_int(item)
try to converts item to integer
- Args:
item (str): input string
- Returns:
int, str: returns integer value if it is number, else same as input string
5.1.2. Common Functions/Classes for Jinja tables Processing
helper functions/classes for jinja variables ( aka: filters )
- class j2config.func.Aggregated(table)
Bases:
Common
device Aggregated/instances
- Args:
table (dict): dataframe dictionary
- Returns:
Aggregated: Instance of Aggregated
- Yields:
Aggregated: Instance of Aggregated
- class j2config.func.Bgp(table)
Bases:
Common
device Bgp/instances
- Args:
table (dict): dataframe dictionary
- Returns:
Bgp: Instance of Bgp
- Yields:
Bgp: Instance of Bgp
- bgp_peers(vrf)
yields data slice(s) for the bgp information where bgp_vrf matches with provided vrf name
- Args:
vrf (str): vrf name
- Yields:
data_slice: of matching bgp details
- vrf_not_none()
yields data slice(s) for the bgp information where bgp_vrf is not none
- Yields:
data_slice: of matching bgp details
- class j2config.func.Common(table)
Bases:
object
- class j2config.func.Loopback(table)
Bases:
Common
device Loopback/instances
- Args:
table (dict): dataframe dictionary
- Returns:
Loopback: Instance of Loopback
- Yields:
Loopback: Instance of Loopback
- class j2config.func.Ospf(table)
Bases:
Common
device Ospf
- Args:
table (dict): dataframe dictionary
- Returns:
Ospf: Instance of Ospf
- Yields:
Ospf: Instance of Ospf
- area_summary_tupples()
- vrf(vrf='')
- class j2config.func.Physical(table)
Bases:
Common
device Physical/instances
- Args:
table (dict): dataframe dictionary
- Returns:
Physical: Instance of Physical
- Yields:
Physical: Instance of Physical
- interface(n)
yields data slice(s) for the sorted physical interfaces informations
- Args:
n (int): interface number
- Yields:
data_slice: of matched interface
- interface_type(intf_type)
condition: is provided dataslice is of given interface type
- Args:
data (data_slice): Pandas DataFrame slice intf_type (str): interface type to be verify
- Returns:
bool: result of condition
- interface_type_ends(x)
condition: is provided dataslice ends with given argument x
- Args:
data (data_slice): Pandas DataFrame slice x (str): interface type ending identifier to be verify with
- Returns:
bool: result of condition
- sorted()
provides list of sorted interface numbers
- Returns:
list: of interface numbers sorted
- sorted_interfaces()
yields data slice(s) for the sorted physical interfaces informations
- Yields:
data_slice: sorted physical interfaces
- uplinks()
yields data slice(s) for the physical interface information, where int_filter information starts with uplink.
- Yields:
data_slice: of matching physical interfaces details
- class j2config.func.Static(table)
Bases:
Common
device static
- Args:
table (dict): dataframe dictionary
- Returns:
Static: Instance of Static
- Yields:
Static: Instance of Static
- default_route(default_route=True)
- has_nexthop()
- version(ver)
- vrf(vrf)
- class j2config.func.Vlan(table)
Bases:
Common
device Vlan/instances
- Args:
table (dict): dataframe dictionary
- Returns:
Vlan: Instance of Vlan
- Yields:
Vlan: Instance of Vlan
- of_instance(vrf)
yields data slice(s) for the vrf matching with intvrf column
- Args:
vrf (str): vrf name
- Yields:
data_slice: of matching vrf with intvrf
- vlan(n)
returns data slice for the matching vlan number
- Args:
n (int): vlan number
- Yields:
data_slice: of matching vlan number
- vlans_sorted_range(start, stop)
yields data slice(s) for the vlans matching for the provided range
- Args:
start (int): starting vlan number stop (int): ends vlan number
- Yields:
data_slice: of matching vlan numbers
- class j2config.func.Vrf(table)
Bases:
Common
device vrf/instances
- Args:
table (dict): dataframe dictionary
- Returns:
Vrf: Instance of VRF
- Yields:
Vrf: Instance of VRF
- sorted()
list of available vrfs sorted by vrf field.
- Returns:
list: sorted vrfs
- sorted_vpnids()
list of available vpnids sorted.
- Returns:
list: sorted vpnids
- sorted_vrf_data()
vrf data generator, sorted by vrf names
- Yields:
data_slice: data for all vrf rows except vrf is none, sorted by vrf names
- sorted_vrf_data_by_vpnid()
vrf data generator, sorted by vpnids
- Yields:
data_slice: data for all vrf rows except vrf is none, sorted by vpnids
- vrf_get(vrf)
get a particular vrf data
- Args:
vrf (str): vrf name
- Yields:
data_slice: data for matching vrf row
- vrf_not_none()
condition: vrf is not None
- Yields:
data_slice: data from Row that matches condition
- j2config.func.sort(obj)
exectes sorted method on provided object
- Args:
obj (dynamic): Any object object instance declaring sorted method
- Returns:
dynamic: sorted method output from object.
5.1.3. GENERAL PYTHON FUNCTIONS
- j2config.general.get_model_template_version(template_file)
returns the template version/model from template file if defined with variables template_version and set model. return dash (-) if none.
- Args:
template_file (str): jinja template file
- Returns:
tuple: with values of template model and version details
5.1.4. j2
- class j2config.j2.PrepareConfig(data_file, jtemplate_file, output_folder='.', regional_file=None, regional_class=None)
Bases:
object
boiler plate code class for start configuration preparation
- Args:
data_file (str): Excel database jtemplate_file (str): Jinja Template output_folder (str, optional): output path. Defaults to “.”. regional_file (str, optional): custom static regional variable file. Defaults to None. (overrides device var) regional_class (class, optional): custom class returning frames to be merge with device var . Defaults to None.
- Raises:
Exception: Raise for Custom class insertion Exception: Raise for Custom module insertion
- custom_class_add_to_filter(**kwargs)
add custom classes and its methods as jinja filters. External callable.
- custom_module_methods_add_to_filter(*modules)
add custom methods from module(s) as jinja filters. External callable.
- filters = {'Aggregated': <class 'j2config.func.Aggregated'>, 'Bgp': <class 'j2config.func.Bgp'>, 'Loopback': <class 'j2config.func.Loopback'>, 'Ospf': <class 'j2config.func.Ospf'>, 'Physical': <class 'j2config.func.Physical'>, 'Static': <class 'j2config.func.Static'>, 'Vlan': <class 'j2config.func.Vlan'>, 'Vrf': <class 'j2config.func.Vrf'>, '_Vlan__vlans_range': <function Vlan.__vlans_range>, '__doc__': 'device Ospf\n\n\tArgs:\n\t\ttable (dict): dataframe dictionary\n\n\tReturns:\n\t\tOspf: Instance of Ospf\n\n\tYields:\n\t\tOspf: Instance of Ospf\n\t', '__module__': 'j2config.func', '_sorted_vl_range': <function Vlan._sorted_vl_range>, 'addressing': <function addressing>, 'area_summary_tupples': <function Ospf.area_summary_tupples>, 'as_path_repeat': <function as_path_repeat>, 'bgp_peers': <function Bgp.bgp_peers>, 'comma_separated': <function comma_separated>, 'convert_to_int': <function convert_to_int>, 'default_route': <function Static.default_route>, 'get_item': <function get_item>, 'get_summaries': <function get_summaries>, 'groups_of_nine': <function groups_of_nine>, 'has_nexthop': <function Static.has_nexthop>, 'int_to_str': <function int_to_str>, 'interface': <function Physical.interface>, 'interface_type': <function Physical.interface_type>, 'interface_type_ends': <function Physical.interface_type_ends>, 'invmask': <function invmask>, 'ip_addressing': <function addressing>, 'iprint': <function iprint>, 'ipv6_urpf_acl_network': <function ipv6_urpf_acl_network>, 'list_append': <function list_append>, 'list_extend': <function list_extend>, 'list_sorted': <function list_sorted>, 'mask': <function mask>, 'netmask': <function netmask>, 'nt_get_summaries': <function get_summaries>, 'nth_ip': <function nth_ip>, 'of_instance': <function Vlan.of_instance>, 'physical_if_allowed': <function physical_if_allowed>, 'remove_trailing_zeros': <function remove_trailing_zeros>, 'sort': <function sort>, 'sorted': <function Physical.sorted>, 'sorted_interfaces': <function Physical.sorted_interfaces>, 'sorted_vpnids': <function Vrf.sorted_vpnids>, 'sorted_vrf_data': <function Vrf.sorted_vrf_data>, 'sorted_vrf_data_by_vpnid': <function Vrf.sorted_vrf_data_by_vpnid>, 'space_separated': <function space_separated>, 'str_to_list': <function str_to_list>, 'uplinks': <function Physical.uplinks>, 'v4addressing': <function v4addressing>, 'version': <function Static.version>, 'vlan': <function Vlan.vlan>, 'vlans_sorted_range': <function Vlan.vlans_sorted_range>, 'vrf': <function Ospf.vrf>, 'vrf_get': <function Vrf.vrf_get>, 'vrf_not_none': <function Bgp.vrf_not_none>}
- start()
kick start generation
5.1.5. Conditions
- j2config.read_conditions.get_conditions(jinja_flie)
get all conditional statements from jinja file
- Args:
jinja_flie (str): jinja template file
- Returns:
dict: dictionary with list of jinja variables, conditions, and loops.
- j2config.read_conditions.get_variables(jinja_flie)
get all jinja variables defined in jinja file
- Args:
jinja_flie (str): jinja template file
- Returns:
set: set of jinja variables
5.1.6. Common Functions for Jinja processing
- j2config.cmn.common_fn.addressing(net)
get the ip of given subnet
- Args:
net (str): input ipv4 address
- Returns:
str: ip address
- j2config.cmn.common_fn.as_path_repeat(asn, times)
- j2config.cmn.common_fn.comma_separated(items)
joins provided items (iterables) by comma
- Args:
items (list): input items
- Returns:
str: joins items by comma
- j2config.cmn.common_fn.convert_to_int(lst)
convert numeric string type elements to integer type in a list.
- Args:
lst (list): converts numeric eliments as integer
- Returns:
list: updated list
- j2config.cmn.common_fn.get_item(lst, n)
get the nth item from list
- Args:
lst (list): list containing various items n (int): index of item to be retrived
- Returns:
str: n-th item from list
- j2config.cmn.common_fn.get_summaries(lst_of_pfxs)
get the summaries for provided prefixes.
- Args:
lst_of_pfxs (list): list of prefixes to be summarized
- Returns:
list: list of summarized prefixes
- j2config.cmn.common_fn.groups_of_nine(lst)
breaks down provided list in to multiple groups with max. nine elements in each group
- Args:
lst (list): input list
- Returns:
list: updated list of (lists: containing 9 elements)
- j2config.cmn.common_fn.int_to_str(data)
get the actual physical interface value by removing training sub interfaces.
- Args:
data (str): input interface string
- Returns:
str: trunkated interface (after removal of sub-interface value)
- j2config.cmn.common_fn.invmask(net)
get inverse mask for given network (eg: 0.0.0.255)
- Args:
net (str): input ipv4 address
- Returns:
str: subnet mask
- j2config.cmn.common_fn.iprint(x)
i print function to be use withing jinja template for debug.
- Args:
x (str): value to be print during junja process
- j2config.cmn.common_fn.ipv6_urpf_acl_network(subnet)
provides ipv6 address network ip
- Args:
subnet (str): ipv6 address
- Returns:
str: network address value for provided ipv6 address
- j2config.cmn.common_fn.list_append(lst, item)
append an item to list
- Args:
lst (list): input list item (str, number): item to be appeneded to list
- Returns:
list: updated list
- j2config.cmn.common_fn.list_extend(lst, item)
Extend the list of items to list
- Args:
lst (list): input list item (list): list of items to be extended to input list
- Returns:
list: updated list
- j2config.cmn.common_fn.list_sorted(lst)
provided sorted elements in list
- Args:
lst (list): input list
- Returns:
list: updated list
- j2config.cmn.common_fn.mask(net)
get the subnet mask for given network (eg: 24)
- Args:
net (str): input ipv4 address
- Returns:
str: subnet mask
- j2config.cmn.common_fn.netmask(net)
get network mask for given network (eg: 255.255.255.0)
- Args:
net (str): input ipv4 address
- Returns:
str: subnet mask
- j2config.cmn.common_fn.nth_ip(net, n, withMask=False)
get n-th ip address of given network. withMask: will return value along with mask otherwise only subnet
- Args:
net (str): input ipv4 address n (int): number (number of ip address to be return from subnet) withMask (bool, optional): return with mask or without mask. Defaults to False.
- Returns:
str: nth ip address from subnet
- j2config.cmn.common_fn.physical_if_allowed(vlan, table)
condition: checks for filter==physical and vlan in vlan_members
- Args:
vlan (str, int): vlan number table (dict): dataframe dictionary
- Returns:
int: interface value of matching row
- j2config.cmn.common_fn.remove_trailing_zeros(net)
removes the trailing zeros from given ipv6 address
- Args:
net (str): input ipv6 address
- Returns:
str: updated ipv6 address by removing trailing zeros
- j2config.cmn.common_fn.space_separated(items)
joins provided items (iterables) by spaces
- Args:
items (list): input items
- Returns:
str: joins items by space
- j2config.cmn.common_fn.str_to_list(item)
splits string and returns list, items separated by either comma, enter
- Args:
item (str, int, float): input string or number value(s)
- Returns:
list: separated list of input items
- j2config.cmn.common_fn.v4addressing(ip, mask='32')
get the IPv4 objetct for given ip/mask (default mask=32)
- Args:
ip (str): input ip address/mask value mask (str, optional): ip_mask. Defaults to “32”.
- Returns:
IPv4: IPv4 object