Example #1
0
def init_bf():
    bf_session.host = bf_host

    bf_set_network("nova_candidate")
    snapshots = bf_list_snapshots()
    pprint(snapshots)
    bf_init_snapshot(SNAPSHOT_DIR, name=SNAPSHOT_NAME, overwrite=True)

    bf_set_snapshot(SNAPSHOT_NAME)

    load_questions()

    print(bfq.initIssues().answer())
    print(
        bfq.nodeProperties(properties="Configuration_Format").answer().frame())
def Batfish_parses_config_files(intermediate_scenario_directory, DEBUG,
                                NETWORK_NAME, SNAPSHOT_NAME):
    print(
        "NETWORK_NAME",
        NETWORK_NAME,
        "SNAPSHOT_NAME",
        SNAPSHOT_NAME,
        "intermediate_scenario_directory",
        intermediate_scenario_directory,
    )
    bf_set_network(NETWORK_NAME)
    bf_init_snapshot(intermediate_scenario_directory,
                     name=SNAPSHOT_NAME,
                     overwrite=True)

    load_questions()

    pd.options.display.max_columns = 6

    if DEBUG:
        print_debugging_info()
Example #3
0
# Importing required libraries, setting up logging, and loading questions
import logging

import pandas as pd
from IPython.display import display
from pandas.io.formats.style import Styler

from pybatfish.client.commands import *
# noinspection PyUnresolvedReferences
from pybatfish.datamodel.flow import HeaderConstraints, PathConstraints
from pybatfish.question import bfq, load_questions  # noqa: F401
from pybatfish.util import get_html

bf_logger.setLevel(logging.WARN)

load_questions()

pd.compat.PY3 = True
pd.set_option('display.max_colwidth', -1)
pd.set_option('display.max_columns', None)

# UUID for CSS styles used by pandas styler.
# Keeps our notebook HTML deterministic when displaying dataframes
_STYLE_UUID = "pybfstyle"


class MyStyler(Styler):
    """A custom styler for displaying DataFrames in HTML"""
    def __repr__(self):
        return repr(self.data)
Example #4
0
def reset():
    subprocess.call(["docker", "restart", "batfish"])
    load_questions()
Example #5
0
def run_module():
    # define the available arguments/parameters that a user can pass to
    # the module
    module_args = dict(action=dict(type='str',
                                   required=False,
                                   default='permit'),
                       destination_ips=dict(type='str',
                                            required=False,
                                            default=None),
                       destination_ports=dict(type='str',
                                              required=False,
                                              default=None),
                       filters=dict(type='str', required=False, default=".*"),
                       host=dict(type='str',
                                 required=False,
                                 default='localhost'),
                       invert_search=dict(type='bool',
                                          required=False,
                                          default=False),
                       ip_protocols=dict(type='list',
                                         required=False,
                                         default=None),
                       network=dict(type='str', required=True),
                       nodes=dict(type='str', required=False, default=".*"),
                       reference_snapshot=dict(type='str',
                                               required=False,
                                               default=None),
                       source_ips=dict(type='str',
                                       required=False,
                                       default=None),
                       source_ports=dict(type='str',
                                         required=False,
                                         default=None),
                       name=dict(type='str', required=True))

    # seed the result dict in the object
    # we primarily care about changed and state
    # change is if this module effectively modified the target
    # state will include any data that you want your module to pass back
    # for consumption, for example, in a subsequent task
    result = dict(
        changed=False,
        result_verbose='',
    )

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=True,
        mutually_exclusive=[['action', 'reference_snapshot']])

    if not pybatfish_found:
        module.fail_json(msg='Python module Pybatfish is required')

    if module.check_mode:
        return result

    snapshot = module.params['name']
    reference_snapshot = module.params['reference_snapshot']

    try:
        bf_session.coordinatorHost = module.params['host']
        network = bf_set_network(module.params['network'])
    except Exception as e:
        module.fail_json(msg='Failed to set network: {}'.format(e), **result)

    try:
        load_questions()
    except Exception as e:
        module.fail_json(msg='Failed to load questions: {}'.format(e),
                         **result)

    try:
        headers = HeaderConstraints(
            srcIps=module.params['source_ips'],
            dstIps=module.params['destination_ips'],
            ipProtocols=module.params['ip_protocols'],
            srcPorts=module.params['source_ports'],
            dstPorts=module.params['destination_ports'])
    except Exception as e:
        module.fail_json(
            msg='Failed to create header constraint: {}'.format(e), **result)

    try:
        filters = module.params['filters']
        nodes = module.params['nodes']
        action = module.params['action']
        invert_search = module.params['invert_search']
        q = bfq.searchfilters(headers=headers,
                              filters=filters,
                              nodes=nodes,
                              action=action,
                              invertSearch=invert_search)
        q_name = q.get_name()
        answer_obj = q.answer(snapshot=snapshot,
                              reference_snapshot=reference_snapshot)
        answer_dict = bf_get_answer(questionName=q_name,
                                    snapshot=snapshot,
                                    reference_snapshot=reference_snapshot)
    except Exception as e:
        module.fail_json(msg='Failed to answer question: {}'.format(e),
                         **result)

    answer_element = answer_dict["answerElements"][0]
    result['result_verbose'] = answer_element[
        "rows"] if "rows" in answer_element else []

    module.exit_json(**result)
Example #6
0
 def __init__(self, batfish_host):
     self.batfish_host = batfish_host
     bf_session.host = batfish_host
     load_questions()