示例#1
0
文件: kvstore.py 项目: wlm328cs/openr
    def print_adj(self, publications: Dict[str, openr_types.Publication],
                  nodes, bidir, json):
        adjs_list = defaultdict(list)
        # get list of adjancencies from each area and add it to the final
        # adjacency DB.
        adjs_map = {}
        for area, publication in publications.items():
            adjs = utils.adj_dbs_to_dict(publication, nodes, bidir,
                                         self.iter_publication)
            # handle case when area has no adjacencies
            if len(adjs):
                adjs_map = adjs
            else:
                continue
            for key, val in adjs_map.items():
                for adj_entry in val["adjacencies"]:
                    adj_entry["area"] = area
                adjs_list[key].extend(val["adjacencies"])

        for key, val in adjs_list.items():
            adjs_map[key]["adjacencies"] = val

        if json:
            utils.print_json(adjs_map)
        else:
            utils.print_adjs_table(adjs_map)
示例#2
0
文件: decision.py 项目: MOYUMax/openr
 def run(self, nodes, bidir, json):
     adj_dbs = self.client.get_adj_dbs()
     adjs_map = utils.adj_dbs_to_dict(adj_dbs, nodes, bidir, self.iter_dbs)
     if json:
         utils.print_json(adjs_map)
     else:
         utils.print_adjs_table(adjs_map, self.enable_color, None, None)
示例#3
0
    def run(
        self,
        prefixes: List[str],
        labels: List[int] = (),
        json_opt: bool = False,
        client_id: Optional[int] = None,
    ):
        routes = []
        mpls_routes = []
        client_id = client_id if client_id is not None else self.client.client_id

        try:
            routes = self.client.getRouteTableByClient(client_id)
        except Exception as e:
            print("Failed to get routes from Fib.")
            print("Exception: {}".format(e))
            return 1

        try:
            mpls_routes = self.client.getMplsRouteTableByClient(client_id)
        except Exception:
            pass

        if json_opt:
            utils.print_json(
                utils.get_routes_json("", client_id, routes, prefixes,
                                      mpls_routes, labels))
        else:
            caption = f"FIB routes by client {client_id}"
            utils.print_unicast_routes(caption, routes, prefixes)
            caption = f"MPLS routes by client {client_id}"
            utils.print_mpls_routes(caption, mpls_routes, labels)

        return 0
示例#4
0
    def run(self, prefixes: List[str], labels: List[int], json_opt: bool = False):
        routes = []
        mpls_routes = []

        try:
            routes = self.client.getRouteTableByClient(self.client.client_id)
        except Exception as e:
            print("Failed to get routes from Fib.")
            print("Exception: {}".format(e))
            return 1

        host_id = utils.get_connected_node_name(self.cli_opts)
        client_id = self.client.client_id

        try:
            mpls_routes = self.client.getMplsRouteTableByClient(self.client.client_id)
        except platform_types.PlatformError as e:
            print("Pls check Open/R version. Exception: {}".format(e))

        if json_opt:
            utils.print_json(
                utils.get_routes_json(
                    host_id, client_id, routes, prefixes, mpls_routes, labels
                )
            )
        else:
            caption = f"{host_id}'s FIB routes by client {client_id}"
            utils.print_unicast_routes(caption, routes, prefixes)
            caption = f"{host_id}'s MPLS routes by client {client_id}"
            utils.print_mpls_routes(caption, mpls_routes, labels)

        return 0
示例#5
0
 def _run(self, client: OpenrCtrl.Client, nodes: set, bidir: bool,
          json: bool) -> None:
     adj_dbs = client.getDecisionAdjacencyDbs()
     adjs_map = utils.adj_dbs_to_dict(adj_dbs, nodes, bidir, self.iter_dbs)
     if json:
         utils.print_json(adjs_map)
     else:
         utils.print_adjs_table(adjs_map, self.enable_color, None, None)
示例#6
0
 def run(self, nodes, bidir, json):
     publication = self.client.dump_all_with_filter(Consts.ADJ_DB_MARKER)
     adjs_map = utils.adj_dbs_to_dict(publication, nodes, bidir,
                                      self.iter_publication)
     if json:
         utils.print_json(adjs_map)
     else:
         utils.print_adjs_table(adjs_map, self.enable_color)
示例#7
0
    def _run(self, client: OpenrCtrl.Client, file: str):
        try:
            file_conf = client.dryrunConfig(file)
        except OpenrError as ex:
            click.echo(click.style("FAILED: {}".format(ex), fg="red"))
            return

        config = json.loads(file_conf)
        utils.print_json(config)
示例#8
0
 def _run(self, client: OpenrCtrl.Client, nodes: set, bidir: bool,
          json: bool) -> None:
     keyDumpParams = self.buildKvStoreKeyDumpParams(Consts.ADJ_DB_MARKER)
     publication = client.getKvStoreKeyValsFiltered(keyDumpParams)
     adjs_map = utils.adj_dbs_to_dict(publication, nodes, bidir,
                                      self.iter_publication)
     if json:
         utils.print_json(adjs_map)
     else:
         utils.print_adjs_table(adjs_map, self.enable_color)
示例#9
0
文件: lm.py 项目: guodong/openr
    def _run(self, client: OpenrCtrl.Client, nodes: set, json: bool) -> None:
        adj_db = client.getLinkMonitorAdjacencies()

        # adj_dbs is built with ONLY one single (node, adjDb). Ignpre bidir option
        adjs_map = utils.adj_dbs_to_dict({adj_db.thisNodeName: adj_db}, nodes,
                                         False, self.iter_dbs)
        if json:
            utils.print_json(adjs_map)
        else:
            utils.print_adjs_table(adjs_map, None, None)
示例#10
0
 def _run(self, client: OpenrCtrl.Client, nodes: set, bidir: bool,
          json: bool) -> None:
     adj_dbs = client.getDecisionAdjacencyDbs()
     adjs_map = utils.adj_dbs_to_dict(adj_dbs, nodes, bidir, self.iter_dbs)
     for _, val in adjs_map.items():
         for adj_entry in val["adjacencies"]:
             adj_entry["area"] = val.get("area", "N/A")
     if json:
         utils.print_json(adjs_map)
     else:
         utils.print_adjs_table(adjs_map, None, None)
示例#11
0
    def print_counters(self, counters, json_opt):
        ''' print the Fib counters '''

        host_id = utils.get_connected_node_name(self.client.host, self.lm_cmd_port)
        caption = '{}\'s Fib counters'.format(host_id)

        if json_opt:
            utils.print_json(counters)
        else:
            rows = []
            for key in counters:
                rows.append(['{} : {}'.format(key, counters[key])])
            print(printing.render_horizontal_table(
                rows, caption=caption, tablefmt='plain'))
            print()
示例#12
0
文件: decision.py 项目: MOYUMax/openr
    def print_db_diff(self, nodes_set_a, nodes_set_b, db_sources, db_type, json):
        """ Returns a status code, 0 = success, 1 = failure"""
        a_minus_b = sorted(nodes_set_a - nodes_set_b)
        b_minus_a = sorted(nodes_set_b - nodes_set_a)
        return_code = 0

        if json:
            diffs_up = []
            diffs_down = []
            for node in a_minus_b:
                diffs_up.append(node)
            for node in b_minus_a:
                diffs_down.append(node)

            if diffs_up or diffs_down:
                diffs = {
                    "db_type": db_type,
                    "db_up": db_sources[0],
                    "db_down": db_sources[1],
                    "nodes_up": diffs_up,
                    "nodes_down": diffs_down,
                }
                return_code = 1
                utils.print_json(diffs)

        else:
            rows = []
            for node in a_minus_b:
                rows.append(
                    [
                        "node {}'s {} db in {} but not in {}".format(
                            node, db_type, *db_sources
                        )
                    ]
                )
            for node in b_minus_a:
                rows.append(
                    [
                        "node {}'s {} db in {} but not in {}".format(
                            node, db_type, *reversed(db_sources)
                        )
                    ]
                )
            if rows:
                print(printing.render_vertical_table(rows))
                return_code = 1

        return return_code
示例#13
0
    def _run(self, client: OpenrCtrl.Client, nodes: set, areas: set,
             bidir: bool, json: bool) -> None:

        adj_dbs = client.getDecisionAdjacenciesFiltered(
            ctrl_types.AdjacenciesFilter(selectAreas=areas))

        # convert list<adjDb> from server to a two level map: {area: {node: adjDb}}
        adjs_map_all_areas = utils.adj_dbs_to_area_dict(adj_dbs, nodes, bidir)

        if json:
            utils.print_json(adjs_map_all_areas)
        else:
            # print per-node adjDb tables on a per-area basis
            for area, adjs_map in sorted(adjs_map_all_areas.items()):
                print("\n== Area:", area, "==\n")
                utils.print_adjs_table(adjs_map, None, None)
示例#14
0
    def print_db_delta_adj(
        self, key, value, kvstore_adj_node_names, decision_adj_dbs, json
    ):
        """ Returns status code. 0 = success, 1 = failure"""

        kvstore_adj_db = deserialize_thrift_object(
            value.value, lsdb_types.AdjacencyDatabase
        )
        node_name = kvstore_adj_db.thisNodeName
        kvstore_adj_node_names.add(node_name)
        if node_name not in decision_adj_dbs:
            print(
                printing.render_vertical_table(
                    [["node {}'s adj db is missing in Decision".format(node_name)]]
                )
            )
            return 1
        decision_adj_db = decision_adj_dbs[node_name]

        return_code = 0
        if json:
            tags = ("in_decision", "in_kvstore", "changed_in_decision_and_kvstore")
            adj_list_deltas = utils.find_adj_list_deltas(
                decision_adj_db.adjacencies, kvstore_adj_db.adjacencies, tags=tags
            )
            deltas_json, return_code = utils.adj_list_deltas_json(
                adj_list_deltas, tags=tags
            )
            if return_code:
                utils.print_json(deltas_json)
        else:
            lines = utils.sprint_adj_db_delta(kvstore_adj_db, decision_adj_db)
            if lines:
                print(
                    printing.render_vertical_table(
                        [
                            [
                                "node {}'s adj db in Decision out of sync with "
                                "KvStore's".format(node_name)
                            ]
                        ]
                    )
                )
                print("\n".join(lines))
                return_code = 1

        return return_code
示例#15
0
文件: fib.py 项目: shadowkun/openr
    def print_counters(self, counters, json_opt):
        """ print the Fib counters """

        host_id = utils.get_connected_node_name(self.cli_opts)
        caption = "{}'s Fib counters".format(host_id)

        if json_opt:
            utils.print_json(counters)
        else:
            rows = []
            for key in counters:
                rows.append(["{} : {}".format(key, counters[key])])
            print(
                printing.render_horizontal_table(rows,
                                                 caption=caption,
                                                 tablefmt="plain"))
            print()
示例#16
0
    def run(self, prefixes, json_opt=False):
        try:
            routes = self.client.getRouteTableByClient(self.client.client_id)
        except Exception as e:
            print('Failed to get routes from Fib.')
            print('Exception: {}'.format(e))
            return 1

        host_id = utils.get_connected_node_name(self.client.host, self.lm_cmd_port)
        client_id = self.client.client_id

        if json_opt:
            utils.print_json(utils.get_routes_json(
                host_id, client_id, routes, prefixes))
        else:
            caption = '{}\'s FIB routes by client {}'.format(host_id, client_id)
            utils.print_routes(caption, routes, prefixes)

        return 0
示例#17
0
文件: fib.py 项目: shadowkun/openr
    def run(self, prefixes, json_opt=False):
        try:
            routes = self.client.getRouteTableByClient(self.client.client_id)
        except Exception as e:
            print("Failed to get routes from Fib.")
            print("Exception: {}".format(e))
            return 1

        host_id = utils.get_connected_node_name(self.cli_opts)
        client_id = self.client.client_id

        if json_opt:
            utils.print_json(
                utils.get_routes_json(host_id, client_id, routes, prefixes))
        else:
            caption = "{}'s FIB routes by client {}".format(host_id, client_id)
            utils.print_unicast_routes(caption, routes, prefixes)

        return 0
示例#18
0
    def print_counters(self, counters, json_opt):
        """ print the Fib counters """

        with utils.get_openr_ctrl_client(self.cli_opts.host,
                                         self.cli_opts) as client:
            host_id = client.getMyNodeName()
        caption = "{}'s Fib counters".format(host_id)

        if json_opt:
            utils.print_json(counters)
        else:
            rows = []
            for key in counters:
                rows.append(["{} : {}".format(key, counters[key])])
            print(
                printing.render_horizontal_table(rows,
                                                 caption=caption,
                                                 tablefmt="plain"))
            print()
示例#19
0
    def run(
        self,
        prefixes: List[str],
        labels: List[int],
        json_opt: bool = False,
        client_id: Optional[int] = None,
    ):
        routes = []
        mpls_routes = []
        client_id = client_id if client_id is not None else self.client.client_id

        try:
            routes = self.client.getRouteTableByClient(client_id)
        except Exception as e:
            print("Failed to get routes from Fib.")
            print("Exception: {}".format(e))
            return 1

        with utils.get_openr_ctrl_client(self.cli_opts.host,
                                         self.cli_opts) as client:
            host_id = client.getMyNodeName()

        try:
            mpls_routes = self.client.getMplsRouteTableByClient(client_id)
        except Exception as e:
            print("Pls check Open/R version. Exception: {}".format(e))

        if json_opt:
            utils.print_json(
                utils.get_routes_json(host_id, client_id, routes, prefixes,
                                      mpls_routes, labels))
        else:
            caption = f"{host_id}'s FIB routes by client {client_id}"
            utils.print_unicast_routes(caption, routes, prefixes)
            caption = f"{host_id}'s MPLS routes by client {client_id}"
            utils.print_mpls_routes(caption, mpls_routes, labels)

        return 0
示例#20
0
文件: decision.py 项目: edi2212/openr
    def print_db_diff(
        self,
        nodes_set_a: set,
        nodes_set_b: set,
        db_sources: List[str],
        db_type: str,
        json: bool,
    ) -> int:
        """Returns a status code, 0 = success, 1 = failure"""
        a_minus_b = sorted(nodes_set_a - nodes_set_b)
        b_minus_a = sorted(nodes_set_b - nodes_set_a)
        return_code = 0

        if json:
            diffs_up = []
            diffs_down = []
            for node in a_minus_b:
                diffs_up.append(node)
            for node in b_minus_a:
                diffs_down.append(node)

            if diffs_up or diffs_down:
                diffs = {
                    "db_type": db_type,
                    "db_up": db_sources[0],
                    "db_down": db_sources[1],
                    "nodes_up": diffs_up,
                    "nodes_down": diffs_down,
                }
                return_code = 1
                utils.print_json(diffs)

        else:
            rows = []
            for node in a_minus_b:
                rows.append([
                    "node {}'s {} db in {} but not in {}".format(
                        node, db_type, *db_sources)
                ])
            for node in b_minus_a:
                rows.append([
                    "node {}'s {} db in {} but not in {}".format(
                        node, db_type, *reversed(db_sources))
                ])
            if rows:
                print(printing.render_vertical_table(rows))
                return_code = 1

        if return_code == 1:
            if utils.is_color_output_supported():
                click.echo(click.style("FAIL", bg="red", fg="black"))
            else:
                click.echo("FAIL")
            print("{} table for {} and {} do not match".format(
                db_type, *db_sources))
        else:
            if utils.is_color_output_supported():
                click.echo(click.style("PASS", bg="green", fg="black"))
            else:
                click.echo("PASS")
            print("{} table for {} and {} match".format(db_type, *db_sources))

        return return_code
示例#21
0
    def print_db_delta(self, key, value, kvstore_adj_node_names,
                       kvstore_prefix_node_names, decision_adj_dbs,
                       decision_prefix_dbs, json):
        ''' Returns status code. 0 = success, 1 = failure'''

        if key.startswith(Consts.ADJ_DB_MARKER):
            kvstore_adj_db = deserialize_thrift_object(
                value.value, lsdb_types.AdjacencyDatabase)
            node_name = kvstore_adj_db.thisNodeName
            kvstore_adj_node_names.add(node_name)
            if node_name not in decision_adj_dbs:
                print(
                    printing.render_vertical_table([[
                        "node {}'s adj db is missing in Decision".format(
                            node_name)
                    ]]))
                return 1
            decision_adj_db = decision_adj_dbs[node_name]

            return_code = 0
            if json:
                tags = ('in_decision', 'in_kvstore',
                        'changed_in_decision_and_kvstore')
                adj_list_deltas = utils.find_adj_list_deltas(
                    decision_adj_db.adjacencies,
                    kvstore_adj_db.adjacencies,
                    tags=tags)
                deltas_json, return_code = utils.adj_list_deltas_json(
                    adj_list_deltas, tags=tags)
                if return_code:
                    utils.print_json(deltas_json)
            else:
                lines = utils.sprint_adj_db_delta(kvstore_adj_db,
                                                  decision_adj_db)
                if lines:
                    print(
                        printing.render_vertical_table([[
                            "node {}'s adj db in Decision out of sync with KvStore's"
                            .format(node_name)
                        ]]))
                    print("\n".join(lines))
                    return_code = 1

            return return_code

        if key.startswith(Consts.PREFIX_DB_MARKER):
            kvstore_prefix_db = deserialize_thrift_object(
                value.value, lsdb_types.PrefixDatabase)
            node_name = kvstore_prefix_db.thisNodeName
            kvstore_prefix_node_names.add(node_name)
            if node_name not in decision_prefix_dbs:
                print(
                    printing.render_vertical_table([[
                        "node {}'s prefix db is missing in Decision".format(
                            node_name)
                    ]]))
                return 1
            decision_prefix_db = decision_prefix_dbs[node_name]
            decision_prefix_set = {}
            utils.update_global_prefix_db(decision_prefix_set,
                                          decision_prefix_db)
            lines = utils.sprint_prefixes_db_delta(decision_prefix_set,
                                                   kvstore_prefix_db)
            if lines:
                print(
                    printing.render_vertical_table([[
                        "node {}'s prefix db in Decision out of sync with KvStore's"
                        .format(node_name)
                    ]]))
                print("\n".join(lines))
                return 1

            return 0
示例#22
0
 def _run(self, client: OpenrCtrl.Client):
     resp = client.getRunningConfig()
     config = json.loads(resp)
     utils.print_json(config)