def populate_facts(self, connection, ansible_facts, data=None):
        """ Populate the facts for Bgp_address_family network resource
        :param connection: the device connection
        :param ansible_facts: Facts dictionary
        :param data: previously collected conf
        :rtype: dictionary
        :returns: facts
        """
        facts = {}
        objs = []
        if not data:
            data = self.get_config(connection)

        nb_data = flatten_config(data, "neighbor")
        data = flatten_config(nb_data, "vrf")
        # parse native config using the Bgp_global template
        bgp_global_parser = Bgp_address_familyTemplate(lines=data.splitlines())
        objs = bgp_global_parser.parse()

        af = objs.get("address_family")
        if af:
            self._post_parse(objs)
        else:
            objs["address_family"] = []

        ansible_facts["ansible_network_resources"].pop("bgp_address_family",
                                                       None)

        params = utils.remove_empties(
            utils.validate_config(self.argument_spec, {"config": objs}))

        facts["bgp_address_family"] = params.get("config", {})
        ansible_facts["ansible_network_resources"].update(facts)

        return ansible_facts
    def populate_facts(self, connection, ansible_facts, data=None):
        """Populate the facts for Bgp_address_family network resource
        :param connection: the device connection
        :param ansible_facts: Facts dictionary
        :param data: previously collected conf
        :rtype: dictionary
        :returns: facts
        """
        facts = {}
        objs = []
        if not data:
            data = self.get_config(connection)
        nbr_data = flatten_config(data, "neighbor")
        data = flatten_config(nbr_data, "vrf")
        # parse native config using the Bgp_global template
        bgp_global_parser = Bgp_neighbor_address_familyTemplate(
            lines=data.splitlines(), )
        objs = bgp_global_parser.parse()

        if objs:
            top_lvl_nbrs = objs.get("vrfs", {}).pop("vrf_", {})
            objs["neighbors"] = self._post_parse(top_lvl_nbrs).get(
                "neighbors",
                [],
            )

            if "vrfs" in objs:
                for vrf in objs["vrfs"].values():
                    vrf["neighbors"] = self._post_parse(vrf)["neighbors"]
                objs["vrfs"] = list(objs["vrfs"].values())

        ansible_facts["ansible_network_resources"].pop(
            "bgp_neighbor_address_family",
            None,
        )

        params = utils.remove_empties(
            utils.validate_config(self.argument_spec, {"config": objs}), )

        facts["bgp_neighbor_address_family"] = params.get("config", {})
        ansible_facts["ansible_network_resources"].update(facts)

        return ansible_facts
    def populate_facts(self, connection, ansible_facts, data=None):
        """ Populate the facts for Logging_global network resource

        :param connection: the device connection
        :param ansible_facts: Facts dictionary
        :param data: previously collected conf

        :rtype: dictionary
        :returns: facts
        """
        facts = {}
        objs = []

        if not data:
            data = self.get_config(connection)

        flatten_context_list = [
            "logging archive",
            "logging tls-server",
            "logging correlator rule",
            "logging correlator ruleset",
            "logging events filter",
            "logging buffered discriminator",
            "logging monitor discriminator",
            "logging console discriminator",
        ]

        for x in flatten_context_list:
            data = flatten_config(data, x)
        # parse native config using the Logging_global template
        logging_global_parser = Logging_globalTemplate(lines=data.splitlines(),
                                                       module=self._module)
        objs = logging_global_parser.parse()
        objs["tls_servers"] = list(objs.get("tls_servers", {}).values())
        if objs.get("correlator"):
            objs["correlator"]["rules"] = list(
                objs.get("correlator", {}).get("rules", {}).values())
            objs["correlator"]["rule_sets"] = list(
                objs.get("correlator", {}).get("rule_sets", {}).values())
            for i, x in enumerate(objs["correlator"]["rule_sets"]):
                if None in x["rulename"]:
                    objs["correlator"]["rule_sets"][i]["rulename"].remove(None)

        ansible_facts["ansible_network_resources"].pop("logging_global", None)

        params = utils.remove_empties(
            logging_global_parser.validate_config(self.argument_spec,
                                                  {"config": objs},
                                                  redact=True))

        facts["logging_global"] = params.get("config", {})
        ansible_facts["ansible_network_resources"].update(facts)

        return ansible_facts
    def populate_facts(self, connection, ansible_facts, data=None):
        """Populate the facts for Ntp_global network resource

        :param connection: the device connection
        :param ansible_facts: Facts dictionary
        :param data: previously collected conf

        :rtype: dictionary
        :returns: facts
        """
        facts = {}
        objs = []

        if not data:
            data = self.get_config(connection)

        flatten_context_list = ["interface", "ntp"]

        for x in flatten_context_list:
            data = flatten_config(data, x)
        # parse native config using the Ntp_global template
        ntp_global_parser = Ntp_globalTemplate(
            lines=data.splitlines(),
            module=self._module,
        )
        objs = ntp_global_parser.parse()
        if "access_group" in objs:
            objs["access_group"]["vrfs"] = list(
                objs.get("access_group", {}).get("vrfs", {}).values(), )
            objs["access_group"]["vrfs"] = sorted(
                objs["access_group"]["vrfs"],
                key=lambda k: k["name"],
            )
        if "interfaces" in objs:
            objs["interfaces"] = list(objs.get("interfaces", {}).values())
        if "peers" in objs:
            objs["peers"] = list(objs.get("peers", {}).values())
        if "servers" in objs:
            objs["servers"] = list(objs.get("servers", {}).values())
        if "source_vrfs" in objs:
            objs["source_vrfs"] = list(objs.get("source_vrfs", {}).values())

        pkey = {
            "authentication_keys": "id",
            "peers": "peer",
            "servers": "server",
            "trusted_keys": "key_id",
            "source_vrfs": "name",
            "interfaces": "name",
        }

        for x in pkey.keys():
            if x in objs:
                objs[x] = sorted(objs[x], key=lambda k: k[pkey[x]])

        ansible_facts["ansible_network_resources"].pop("ntp_global", None)

        params = utils.remove_empties(
            ntp_global_parser.validate_config(
                self.argument_spec,
                {"config": objs},
                redact=True,
            ), )

        facts["ntp_global"] = params.get("config", {})
        ansible_facts["ansible_network_resources"].update(facts)

        return ansible_facts
Beispiel #5
0
    def populate_facts(self, connection, ansible_facts, data=None):
        """Populate the facts for Snmp_server network resource

        :param connection: the device connection
        :param ansible_facts: Facts dictionary
        :param data: previously collected conf

        :rtype: dictionary
        :returns: facts
        """
        facts = {}
        objs = []

        if not data:
            data = self.get_config(connection)

        flatten_context_list = [
            "snmp-server vrf",
            "snmp-server mib bulkstat schema",
            "snmp-server mib bulkstat transfer-id",
            "snmp-server correlator rule",
            "snmp-server interface",
            "snmp-server correlator rule",
            "snmp-server correlator ruleset",
        ]

        for x in flatten_context_list:
            data = flatten_config(data, x)
        # parse native config using the Snmp_server template
        snmp_server_parser = Snmp_serverTemplate(
            lines=data.splitlines(),
            module=self._module,
        )
        objs = snmp_server_parser.parse()

        dict_to_list = [
            "context",
            "mib_object_lists",
            "mib_schema",
            "mib_bulkstat_transfer_ids",
            "vrfs",
            "interfaces",
        ]
        for i in dict_to_list:
            if i in objs:
                objs[i] = list(objs[i].values())
                if i == "vrfs":
                    for j in objs[i]:
                        j["hosts"].remove({})
                        j["context"] = list(j["context"].values())

        ansible_facts["ansible_network_resources"].pop("snmp_server", None)

        params = utils.remove_empties(
            snmp_server_parser.validate_config(
                self.argument_spec,
                {"config": objs},
                redact=True,
            ), )
        facts["snmp_server"] = params.get("config", {})
        ansible_facts["ansible_network_resources"].update(facts)

        return ansible_facts
    def populate_facts(self, connection, ansible_facts, data=None):
        """Populate the facts for Bgp_global network resource

        :param connection: the device connection
        :param ansible_facts: Facts dictionary
        :param data: previously collected conf

        :rtype: dictionary
        :returns: facts
        """
        facts = {}
        objs = []
        bgp_global_config = []
        if not data:
            data = self.get_config(connection)
        neighbor_data = flatten_config(data, "neighbor")
        rpki_server_data = flatten_config(neighbor_data, "rpki server")
        data = flatten_config(rpki_server_data, "bgp confederation peers")

        # remove address_family configs from bgp_global

        start = False
        for bgp_line in data.splitlines():
            if "address-family" in bgp_line:
                start = True
            if not start:
                bgp_global_config.append(bgp_line)
            if start and "!" in bgp_line:
                start = False

        # parse native config using the Bgp_global template
        bgp_global_parser = Bgp_globalTemplate(
            lines=bgp_global_config,
            module=self._module,
        )
        objs = bgp_global_parser.parse()

        conf_peers = objs.get("bgp", {}).get("confederation",
                                             {}).get("peers", {})
        if conf_peers:
            objs["bgp"]["confederation"]["peers"] = list(conf_peers.values())

        vrfs = objs.get("vrfs", {})

        # move global vals to their correct position in facts tree
        # this is only needed for keys that are common between both global
        # and VRF contexts
        global_vals = vrfs.pop("vrf_", {})
        for key, value in iteritems(global_vals):
            if objs.get(key):
                objs[key].update(value)
            else:
                objs[key] = value
        # transform vrfs into a list
        if vrfs:
            objs["vrfs"] = sorted(
                list(objs["vrfs"].values()),
                key=lambda k, sk="vrf": k[sk],
            )
            for vrf in objs["vrfs"]:
                self._post_parse(vrf)
        else:
            objs["vrfs"] = []

        self._post_parse(objs)

        ansible_facts["ansible_network_resources"].pop("bgp_global", None)

        params = utils.remove_empties(
            bgp_global_parser.validate_config(
                self.argument_spec,
                {"config": objs},
                redact=True,
            ), )

        facts["bgp_global"] = params.get("config", {})
        ansible_facts["ansible_network_resources"].update(facts)

        return ansible_facts