def _render_areas(self, attr, want, have, opr=True): """ This function forms the set/delete commands based on the 'opr' type for ospf area attributes. :param attr: attribute name. :param w: the desired config. :param h: the target config. :param opr: True/False. :return: generated commands list. """ commands = [] h_lst = {} w_lst = want.get(attr) or [] l_set = ("area_id", "export_list", "import_list") if have: h_lst = have.get(attr) or [] if not opr and not h_lst: commands.append(self._form_attr_cmd(attr="area", opr=opr)) elif w_lst: for w_area in w_lst: cmd = (self._compute_command( key="area", attr=_bool_to_str(w_area["area_id"]), opr=opr, ) + " ") h_area = search_obj_in_list(w_area["area_id"], h_lst, "area_id") if not opr and not h_area: commands.append( self._form_attr_cmd(key="area", attr=w_area["area_id"], opr=opr)) else: for key, val in iteritems(w_area): if (opr and key in l_set and not _is_w_same(w_area, h_area, key)): if key == "area_id": commands.append( self._form_attr_cmd( attr="area", val=_bool_to_str(val), opr=opr, )) else: commands.append( cmd + key.replace("_", "-") + " " + _bool_to_str(val).replace("_", "-")) elif not opr and key in l_set: if key == "area_id" and not _in_target( h_area, key): commands.append(cmd) continue if key != "area_id" and not _in_target( h_area, key): commands.append(cmd + val + " " + key) elif key == "range": commands.extend( self._render_list_dict_param( key, w_area, h_area, cmd, opr)) return commands
def _render_ospf_param(self, want, have, opr=True): """ This function forms the set/delete commands for ospf leaf attributes and triggers the process for other child attributes. for firewall_global attributes. :param w: the desired config. :param h: the target config. :param opr: True/False. :return: generated commands list. """ commands = [] w = deepcopy(remove_empties(want)) leaf = ("default_metric", "log_adjacency_changes") if w: for (key, val) in iteritems(w): if opr and key in leaf and not _is_w_same(w, have, key): commands.append( self._form_attr_cmd(attr=key, val=_bool_to_str(val), opr=opr)) elif not opr and key in leaf and not _in_target(have, key): commands.append( self._form_attr_cmd(attr=key, val=_bool_to_str(val), opr=opr)) else: commands.extend(self._render_child_param( w, have, key, opr)) return commands
def _render_list_dict_param(self, attr, want, have, cmd=None, opr=True): """ This function forms the set/delete commands based on the 'opr' type for attributes with in desired list of dictionary. :param attr: attribute name. :param w: the desired config. :param h: the target config. :param cmd: commands to be prepend. :param opr: True/False. :return: generated commands list. """ commands = [] h = [] name = { "redistribute": "route_type", "range": "address", } leaf_dict = { "redistribute": ("route_map", "route_type"), "range": ("address", "advertise", "not_advertise"), } leaf = leaf_dict[attr] w = want.get(attr) or [] if have: h = have.get(attr) or [] if not opr and not h: commands.append(self._compute_command(attr=attr, opr=opr)) elif w: for w_item in w: for key, val in iteritems(w_item): if not cmd: cmd = self._compute_command(opr=opr) h_item = search_obj_in_list(w_item[name[attr]], h, name[attr]) if (opr and key in leaf and not _is_w_same(w_item, h_item, key)): if key == "route_type" or ( key == "address" and "advertise" not in w_item and "not-advertise" not in w_item): if not val: cmd = cmd.replace("set", "delete") commands.append(cmd + attr + " " + str(val)) elif key in leaf_dict["range"] and key != "address": commands.append(cmd + attr + " " + w_item[name[attr]] + " " + key.replace("_", "-")) elif key == "route_map": commands.append(cmd + attr + " " + w_item[name[attr]] + " " + key.replace("_", "-") + " " + str(val)) elif (not opr and key in leaf and not _in_target(h_item, key)): if key in ("route_type", "address"): commands.append(cmd + attr + " " + str(val)) else: commands.append(cmd + (attr + " " + w_item[name[attr]] + " " + key)) return commands
def _render_dict_param(self, attr, want, have, opr=True): """ This function generate the commands for dictionary elements. :param attr: attribute name. :param w: the desired configuration. :param h: the target config. :param opr: True/False. :return: generated list of commands. """ commands = [] h = {} if have: h = have.get(attr) or {} if not opr and not h: commands.append(self._form_attr_cmd(attr=attr, opr=opr)) elif want[attr]: leaf_dict = {"parameters": "router_id"} leaf = leaf_dict[attr] for item, value in iteritems(want[attr]): if (opr and item in leaf and not _is_w_same(want[attr], h, item)): commands.append( self._form_attr_cmd(key=attr, attr=item, val=value, opr=opr)) elif not opr and item in leaf and not _in_target(h, item): commands.append( self._form_attr_cmd(key=attr, attr=item, opr=opr)) return commands
def _render_nested_dict_param(self, attr, want, have, opr=True): """ This function forms the set/delete commands based on the 'opr' type for attributes with in desired nested dicts. :param attr: attribute name. :param w: the desired config. :param h: the target config. :param cmd: commands to be prepend. :param opr: True/False. :return: generated commands list. """ commands = [] attr_dict = { "default_information": "originate", "max_metric": "router_lsa", } leaf_dict = { "default_information": ( "always", "metric", "metric_type", "route_map", ), "max_metric": ("administrative", "on_startup", "on_shutdown"), } h = {} w = want.get(attr) or {} if have: h = have.get(attr) or {} if not opr and not h: commands.append(self._form_attr_cmd(attr=attr, opr=opr)) elif w: key = attr_dict[attr] w_attrib = want[attr].get(key) or {} cmd = self._compute_command(opr=opr) h_attrib = {} if w_attrib: leaf = leaf_dict[attr] if h and key in h.keys(): h_attrib = h.get(key) or {} for (item, val) in iteritems(w[key]): if (opr and item in leaf and not _is_w_same(w[key], h_attrib, item)): if item in ("administrative", "always") and val: commands.append(cmd + attr.replace("_", "-") + " " + key.replace("_", "-") + " " + item.replace("_", "-")) elif item not in ("administrative", "always"): commands.append(cmd + attr.replace("_", "-") + " " + key.replace("_", "-") + " " + item.replace("_", "-") + " " + str(val)) elif (not opr and item in leaf and not _in_target(h_attrib, item)): commands.append(cmd + attr + " " + item) return commands
def _render_dict_param(self, attr, want, have, opr=True): """ This function generate the commands for dictionary elements. :param attr: attribute name. :param w: the desired configuration. :param h: the target config. :param opr: True/False. :return: generated list of commands. """ commands = [] h = {} if have: h = have.get(attr) or {} if not opr and not h: commands.append(self._form_attr_cmd(attr=attr, opr=opr)) elif want[attr]: leaf_dict = { "auto_cost": "reference_bandwidth", "mpls_te": ("enabled", "router_address"), "parameters": ( "router_id", "abr_type", "opaque_lsa", "rfc1583_compatibility", ), } leaf = leaf_dict[attr] for (item, value) in iteritems(want[attr]): if (opr and item in leaf and not _is_w_same(want[attr], h, item)): if item == "enabled": item = "enable" if item in ( "opaque_lsa", "enable", "rfc1583_compatibility", ): commands.append( self._form_attr_cmd(key=attr, attr=item, opr=opr)) else: commands.append( self._form_attr_cmd(key=attr, attr=item, val=value, opr=opr)) elif not opr and item in leaf and not _in_target(h, item): if item == "enabled": commands.append( self._form_attr_cmd(key=attr, attr="enable", opr=opr)) else: commands.append( self._form_attr_cmd(key=attr, attr=item, opr=opr)) return commands
def _render_list_dict_param(self, attr, want, have, cmd=None, opr=True): """ This function forms the set/delete commands based on the 'opr' type for attributes with in desired list of dictionary. :param attr: attribute name. :param w: the desired config. :param h: the target config. :param cmd: commands to be prepend. :param opr: True/False. :return: generated commands list. """ commands = [] h = [] name = { "redistribute": "route_type", "neighbor": "neighbor_id", "range": "address", "md5": "key_id", "vlink": "address", } leaf_dict = { "md5": "md5_key", "redistribute": ( "metric", "route_map", "route_type", "metric_type", ), "neighbor": ("priority", "poll_interval", "neighbor_id"), "range": ("cost", "address", "substitute", "not_advertise"), "vlink": ( "address", "dead_interval", "transmit_delay", "hello_interval", "retransmit_interval", ), } leaf = leaf_dict[attr] w = want.get(attr) or [] if have: h = have.get(attr) or [] if not opr and not h: commands.append(self._compute_command(attr=attr, opr=opr)) elif w: for w_item in w: for (key, val) in iteritems(w_item): if not cmd: cmd = self._compute_command(opr=opr) h_item = self.search_obj_in_have(h, w_item, name[attr]) if (opr and key in leaf and not _is_w_same(w_item, h_item, key)): if key in ( "route_type", "neighbor_id", "address", "key_id", ): commands.append(cmd + attr + " " + str(val)) elif key == "cost": commands.append(cmd + attr + " " + w_item[name[attr]] + " " + key + " " + str(val)) elif key == "not_advertise": commands.append(cmd + attr + " " + w_item[name[attr]] + " " + key.replace("_", "-")) elif key == "md5_key": commands.append(cmd + attr + " " + "key-id" + " " + str(w_item[name[attr]]) + " " + key.replace("_", "-") + " " + w_item[key]) else: commands.append(cmd + attr + " " + w_item[name[attr]] + " " + key.replace("_", "-") + " " + str(val)) elif (not opr and key in leaf and not _in_target(h_item, key)): if key in ( "route_type", "neighbor_id", "address", "key_id", ): commands.append(cmd + attr + " " + str(val)) else: commands.append(cmd + attr + " " + w_item[name[attr]] + " " + key) return commands
def _render_vlink(self, attr, want, have, cmd=None, opr=True): """ This function forms the set/delete commands based on the 'opr' type for attributes with in desired list of dictionary. :param attr: attribute name. :param w: the desired config. :param h: the target config. :param cmd: commands to be prepend. :param opr: True/False. :return: generated commands list. """ commands = [] h = [] name = {"virtual_link": "address"} leaf_dict = { "virtual_link": ( "address", "dead_interval", "transmit_delay", "hello_interval", "retransmit_interval", ) } leaf = leaf_dict[attr] w = want.get(attr) or [] if have: h = have.get(attr) or [] if not opr and not h: commands.append(cmd + attr.replace("_", "-")) elif w: for w_item in w: for (key, val) in iteritems(w_item): if not cmd: cmd = self._compute_command(opr=opr) h_item = self.search_obj_in_have(h, w_item, name[attr]) if (opr and key in leaf and not _is_w_same(w_item, h_item, key)): if key in "address": commands.append(cmd + attr.replace("_", "-") + " " + str(val)) else: commands.append(cmd + attr.replace("_", "-") + " " + w_item[name[attr]] + " " + key.replace("_", "-") + " " + str(val)) elif (not opr and key in leaf and not _in_target(h_item, key)): if key in "address": commands.append(cmd + attr.replace("_", "-") + " " + str(val)) else: commands.append(cmd + attr.replace("_", "-") + " " + w_item[name[attr]] + " " + key) elif key == "authentication": commands.extend( self._render_vlink_auth( attr, key, w_item, h_item, w_item["address"], cmd, opr, )) return commands