def get_bgp_config(self, group="<STR_LIT>", neighbor="<STR_LIT>"):
def _check_nhs(policies, nhs_policies):<EOL><INDENT>if not isinstance(policies, list):<EOL><INDENT>policies = [policies]<EOL><DEDENT>for p in policies:<EOL><INDENT>if nhs_policies[p] is True:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>return False<EOL><DEDENT>def update_dict(d, u):  <EOL><INDENT>for k, v in u.items():<EOL><INDENT>if isinstance(d, collections.Mapping):<EOL><INDENT>if isinstance(v, collections.Mapping):<EOL><INDENT>r = update_dict(d.get(k, {}), v)<EOL>d[k] = r<EOL><DEDENT>else:<EOL><INDENT>d[k] = u[k]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>d = {k: u[k]}<EOL><DEDENT><DEDENT>return d<EOL><DEDENT>def build_prefix_limit(**args):<EOL><INDENT>"""<STR_LIT>"""<EOL>prefix_limit = {}<EOL>for key, value in args.items():<EOL><INDENT>key_levels = key.split("<STR_LIT:_>")<EOL>length = len(key_levels) - <NUM_LIT:1><EOL>temp_dict = {key_levels[length]: value}<EOL>for index in reversed(range(length)):<EOL><INDENT>level = key_levels[index]<EOL>temp_dict = {level: temp_dict}<EOL><DEDENT>update_dict(prefix_limit, temp_dict)<EOL><DEDENT>return prefix_limit<EOL><DEDENT>_COMMON_FIELDS_DATATYPE_ = {<EOL>"<STR_LIT:description>": py23_compat.text_type,<EOL>"<STR_LIT>": py23_compat.text_type,<EOL>"<STR_LIT>": int,<EOL>"<STR_LIT>": int,<EOL>"<STR_LIT>": py23_compat.text_type,<EOL>"<STR_LIT>": py23_compat.text_type,<EOL>"<STR_LIT>": int,<EOL>"<STR_LIT>": int,<EOL>"<STR_LIT>": int,<EOL>"<STR_LIT>": int,<EOL>"<STR_LIT>": int,<EOL>"<STR_LIT>": int,<EOL>"<STR_LIT>": int,<EOL>"<STR_LIT>": py23_compat.text_type,<EOL>"<STR_LIT>": int,<EOL>"<STR_LIT>": int,<EOL>"<STR_LIT>": int,<EOL>"<STR_LIT>": int,<EOL>"<STR_LIT>": int,<EOL>"<STR_LIT>": int,<EOL>"<STR_LIT>": int,<EOL>"<STR_LIT>": py23_compat.text_type,<EOL>}<EOL>_PEER_FIELDS_DATATYPE_MAP_ = {<EOL>"<STR_LIT>": py23_compat.text_type,<EOL>"<STR_LIT>": bool,<EOL>"<STR_LIT>": bool,<EOL>}<EOL>_PEER_FIELDS_DATATYPE_MAP_.update(_COMMON_FIELDS_DATATYPE_)<EOL>_GROUP_FIELDS_DATATYPE_MAP_ = {<EOL>"<STR_LIT:type>": py23_compat.text_type,<EOL>"<STR_LIT>": list,<EOL>"<STR_LIT>": bool,<EOL>"<STR_LIT>": bool,<EOL>"<STR_LIT>": int,<EOL>}<EOL>_GROUP_FIELDS_DATATYPE_MAP_.update(_COMMON_FIELDS_DATATYPE_)<EOL>_DATATYPE_DEFAULT_ = {py23_compat.text_type: "<STR_LIT>", int: <NUM_LIT:0>, bool: False, list: []}<EOL>bgp_config = {}<EOL>if group:<EOL><INDENT>bgp = junos_views.junos_bgp_config_group_table(self.device)<EOL>bgp.get(group=group)<EOL><DEDENT>else:<EOL><INDENT>bgp = junos_views.junos_bgp_config_table(self.device)<EOL>bgp.get()<EOL>neighbor = "<STR_LIT>"  <EOL><DEDENT>bgp_items = bgp.items()<EOL>if neighbor:<EOL><INDENT>neighbor_ip = napalm.base.helpers.ip(neighbor)<EOL><DEDENT>policy = junos_views.junos_policy_nhs_config_table(self.device)<EOL>policy.get()<EOL>nhs_policies = dict()<EOL>for policy_name, is_nhs_list in policy.items():<EOL><INDENT>is_nhs, boolean = is_nhs_list[<NUM_LIT:0>]<EOL>nhs_policies[policy_name] = boolean if boolean is not None else False<EOL><DEDENT>for bgp_group in bgp_items:<EOL><INDENT>bgp_group_name = bgp_group[<NUM_LIT:0>]<EOL>bgp_group_details = bgp_group[<NUM_LIT:1>]<EOL>bgp_config[bgp_group_name] = {<EOL>field: _DATATYPE_DEFAULT_.get(datatype)<EOL>for field, datatype in _GROUP_FIELDS_DATATYPE_MAP_.items()<EOL>if "<STR_LIT>" not in field<EOL>}<EOL>for elem in bgp_group_details:<EOL><INDENT>if not ("<STR_LIT>" not in elem[<NUM_LIT:0>] and elem[<NUM_LIT:1>] is not None):<EOL><INDENT>continue<EOL><DEDENT>datatype = _GROUP_FIELDS_DATATYPE_MAP_.get(elem[<NUM_LIT:0>])<EOL>default = _DATATYPE_DEFAULT_.get(datatype)<EOL>key = elem[<NUM_LIT:0>]<EOL>value = elem[<NUM_LIT:1>]<EOL>if key in ["<STR_LIT>", "<STR_LIT>"]:<EOL><INDENT>if isinstance(value, list):<EOL><INDENT>value = "<STR_LIT:U+0020>".join(value)<EOL><DEDENT><DEDENT>if key == "<STR_LIT>":<EOL><INDENT>value = napalm.base.helpers.convert(<EOL>napalm.base.helpers.ip, value, value<EOL>)<EOL><DEDENT>if key == "<STR_LIT>":<EOL><INDENT>bgp_group_peers = value<EOL>continue<EOL><DEDENT>bgp_config[bgp_group_name].update(<EOL>{key: napalm.base.helpers.convert(datatype, value, default)}<EOL>)<EOL><DEDENT>prefix_limit_fields = {}<EOL>for elem in bgp_group_details:<EOL><INDENT>if "<STR_LIT>" in elem[<NUM_LIT:0>] and elem[<NUM_LIT:1>] is not None:<EOL><INDENT>datatype = _GROUP_FIELDS_DATATYPE_MAP_.get(elem[<NUM_LIT:0>])<EOL>default = _DATATYPE_DEFAULT_.get(datatype)<EOL>prefix_limit_fields.update(<EOL>{<EOL>elem[<NUM_LIT:0>].replace(<EOL>"<STR_LIT>", "<STR_LIT>"<EOL>): napalm.base.helpers.convert(datatype, elem[<NUM_LIT:1>], default)<EOL>}<EOL>)<EOL><DEDENT><DEDENT>bgp_config[bgp_group_name]["<STR_LIT>"] = build_prefix_limit(<EOL>**prefix_limit_fields<EOL>)<EOL>if "<STR_LIT>" in bgp_config[bgp_group_name].keys():<EOL><INDENT>del bgp_config[bgp_group_name]["<STR_LIT>"]<EOL>if bgp_config[bgp_group_name]["<STR_LIT>"] == <NUM_LIT:0>:<EOL><INDENT>bgp_config[bgp_group_name]["<STR_LIT>"] = <NUM_LIT:64><EOL><DEDENT><DEDENT>bgp_config[bgp_group_name]["<STR_LIT>"] = {}<EOL>for bgp_group_neighbor in bgp_group_peers.items():<EOL><INDENT>bgp_peer_address = napalm.base.helpers.ip(bgp_group_neighbor[<NUM_LIT:0>])<EOL>if neighbor and bgp_peer_address != neighbor:<EOL><INDENT>continue  <EOL><DEDENT>bgp_group_details = bgp_group_neighbor[<NUM_LIT:1>]<EOL>bgp_peer_details = {<EOL>field: _DATATYPE_DEFAULT_.get(datatype)<EOL>for field, datatype in _PEER_FIELDS_DATATYPE_MAP_.items()<EOL>if "<STR_LIT>" not in field<EOL>}<EOL>for elem in bgp_group_details:<EOL><INDENT>if not ("<STR_LIT>" not in elem[<NUM_LIT:0>] and elem[<NUM_LIT:1>] is not None):<EOL><INDENT>continue<EOL><DEDENT>datatype = _PEER_FIELDS_DATATYPE_MAP_.get(elem[<NUM_LIT:0>])<EOL>default = _DATATYPE_DEFAULT_.get(datatype)<EOL>key = elem[<NUM_LIT:0>]<EOL>value = elem[<NUM_LIT:1>]<EOL>if key in ["<STR_LIT>"]:<EOL><INDENT>bgp_peer_details["<STR_LIT>"] = _check_nhs(value, nhs_policies)<EOL><DEDENT>if key in ["<STR_LIT>", "<STR_LIT>"]:<EOL><INDENT>if isinstance(value, list):<EOL><INDENT>value = "<STR_LIT:U+0020>".join(value)<EOL><DEDENT><DEDENT>if key == "<STR_LIT>":<EOL><INDENT>value = napalm.base.helpers.convert(<EOL>napalm.base.helpers.ip, value, value<EOL>)<EOL><DEDENT>bgp_peer_details.update(<EOL>{key: napalm.base.helpers.convert(datatype, value, default)}<EOL>)<EOL>bgp_peer_details["<STR_LIT>"] = napalm.base.helpers.as_number(<EOL>bgp_peer_details["<STR_LIT>"]<EOL>)<EOL>bgp_peer_details["<STR_LIT>"] = napalm.base.helpers.as_number(<EOL>bgp_peer_details["<STR_LIT>"]<EOL>)<EOL>if key == "<STR_LIT>":<EOL><INDENT>bgp_peer_details["<STR_LIT>"] = True<EOL>del bgp_peer_details["<STR_LIT>"]<EOL><DEDENT><DEDENT>if "<STR_LIT>" in bgp_config[bgp_group_name].keys():<EOL><INDENT>bgp_peer_details["<STR_LIT>"] = True<EOL><DEDENT>prefix_limit_fields = {}<EOL>for elem in bgp_group_details:<EOL><INDENT>if "<STR_LIT>" in elem[<NUM_LIT:0>] and elem[<NUM_LIT:1>] is not None:<EOL><INDENT>datatype = _PEER_FIELDS_DATATYPE_MAP_.get(elem[<NUM_LIT:0>])<EOL>default = _DATATYPE_DEFAULT_.get(datatype)<EOL>prefix_limit_fields.update(<EOL>{<EOL>elem[<NUM_LIT:0>].replace(<EOL>"<STR_LIT>", "<STR_LIT>"<EOL>): napalm.base.helpers.convert(<EOL>datatype, elem[<NUM_LIT:1>], default<EOL>)<EOL>}<EOL>)<EOL><DEDENT><DEDENT>bgp_peer_details["<STR_LIT>"] = build_prefix_limit(<EOL>**prefix_limit_fields<EOL>)<EOL>bgp_config[bgp_group_name]["<STR_LIT>"][<EOL>bgp_peer_address<EOL>] = bgp_peer_details<EOL>if neighbor and bgp_peer_address == neighbor_ip:<EOL><INDENT>break  <EOL><DEDENT><DEDENT>if "<STR_LIT>" in bgp_config[bgp_group_name].keys():<EOL><INDENT>del bgp_config[bgp_group_name]["<STR_LIT>"]<EOL><DEDENT><DEDENT>return bgp_config<EOL>
Return BGP configuration.