{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "nbsphinx": "hidden" }, "outputs": [], "source": [ "import pandas as pd\n", "from pybatfish.client.session import Session\n", "from pybatfish.datamodel import Edge, Interface\n", "from pybatfish.datamodel.answer import TableAnswer\n", "from pybatfish.datamodel.flow import (HeaderConstraints,\n", " PathConstraints)\n", "from pybatfish.datamodel.referencelibrary import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "nbsphinx": "hidden", "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "pd.set_option(\"display.width\", 300) \n", "pd.set_option(\"display.max_columns\", 30) \n", "pd.set_option(\"display.max_rows\", 1000) \n", "pd.set_option(\"display.max_colwidth\", None)\n", "\n", "# Configure all pybatfish loggers to use WARN level\n", "import logging\n", "logging.getLogger('pybatfish').setLevel(logging.WARN)\n", "\n", "bf=Session(host=\"localhost\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "nbsphinx": "hidden" }, "outputs": [ { "data": { "text/plain": [ "'generate_questions'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bf.set_network('generate_questions')\n", "bf.set_snapshot('generate_questions')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Using Reference Books" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Reference book allows users to create groups of information that can be used in Batfish queries. They can contain:\n", "\n", "* [InterfaceGroup](../datamodel.rst#pybatfish.datamodel.referencelibrary.InterfaceGroup)\n", "* [AddressGroup](../datamodel.rst#pybatfish.datamodel.referencelibrary.AddressGroup)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "| Name | Description | Type |\n", "|----------------|----------------------------|------------------------------------------------------------------------------------------------|\n", "| name | Name of the reference book | str |\n", "| addressGroup | A list of addressGroups | List of [addressGroup](../datamodel.rst#pybatfish.datamodel.referencelibrary.AddressGroup) |\n", "| interfaceGroup | A list of interfaceGroups | List of [interfaceGroup](../datamodel.rst#pybatfish.datamodel.referencelibrary.InterfaceGroup) |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Example usage**\n", "\n", "Create a reference book for information about border routers and add an `InterfaceGroup` for the as2 border interfaces\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "interfacegroup = [InterfaceGroup('as2-border', \n", " interfaces = [Interface('as2border1','GigabitEthernet2/0'), \n", " Interface('as2border2', 'GigabitEthernet2/0')])]\n", "refbook = ReferenceBook(name='border', interfaceGroups= interfacegroup)\n", "\n", "bf.put_reference_book(refbook)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Example usage**\n", "\n", "Add an `AddressGroup` for the IP addresses of all as2 border interfaces to the `ReferenceBook('border')`" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "refbook = bf.get_reference_book('border')\n", "addressGroup = [\n", " AddressGroup(name='as2-border', addresses = ['2.12.21.1', '2.12.12.1'])\n", "]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "refbook.addressGroups.extend(addressGroup)\n", "bf.put_reference_book(refbook)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "ReferenceBook(name='border', addressGroups=[AddressGroup(name='as2-border', addresses=['2.12.12.1', '2.12.21.1'], childGroupNames=[])], interfaceGroups=[InterfaceGroup(name='as2-border', interfaces=[Interface(hostname='as2border1', interface='GigabitEthernet2/0'), Interface(hostname='as2border2', interface='GigabitEthernet2/0')])])" ], "text/plain": [ "ReferenceBook(name='border', addressGroups=[AddressGroup(name='as2-border', addresses=['2.12.12.1', '2.12.21.1'], childGroupNames=[])], interfaceGroups=[InterfaceGroup(name='as2-border', interfaces=[Interface(hostname='as2border1', interface='GigabitEthernet2/0'), Interface(hostname='as2border2', interface='GigabitEthernet2/0')])])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bf.get_reference_book('border')" ] } ], "metadata": { "celltoolbar": "Edit Metadata", "hide_input": false, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }