Exemplo n.º 1
0
def test_read():

    manager = OvsdbConnectionManager(settings.get('ovs_remote'),
                                     settings.get('ovs_schema'))
    manager.start()
    idl = manager.idl

    init_seq_no = 0
    # Wait until the connection is ready
    while True:
        idl.run()
        # Print self.idl.change_seqno
        if init_seq_no != idl.change_seqno:
            break
        time.sleep(0.001)

    schema = restparser.parseSchema(settings.get('ext_schema'))

    run_config_util = runconfig.RunConfigUtil(idl, schema)
    config = run_config_util.get_config()
    '''
    TODO: Adding json.dumps and the json.loads as a workaround because
    the config returned from test_read has a missing unicode character
    for keys
    '''
    temp_config = json.dumps(config)
    config = json.loads(temp_config)
    filename = 'config.db'
    with open(filename, 'w') as fp:
        json.dump(config, fp, sort_keys=True, indent=4, separators=(',', ': '))
        fp.write('\n')
    return config
Exemplo n.º 2
0
def copy_running_startup():
    cfg = cfgdb.Cfgdb()
    manager = OvsdbConnectionManager(settings.get('ovs_remote'),
                                     settings.get('ovs_schema'))
    manager.start()
    idl = manager.idl

    init_seq_no = idl.change_seqno
    # Wait until the connection is ready
    while True:
        idl.run()
        # print self.idl.change_seqno
        if init_seq_no != idl.change_seqno:
            break
        time.sleep(1)

    restschema = restparser.parseSchema(settings.get('ext_schema'))

    run_config_util = RunConfigUtil(idl, restschema)
    config = run_config_util.get_running_config()

    cfg.config = ovs.json.to_string(config)
    cfg.type = "startup"
    row, tbl_found = cfg.find_row_by_type("startup")
    if tbl_found:
        cfg.update_row(row)
    else:
        cfg.insert_row()

    cfg.close()
    return True
Exemplo n.º 3
0
def test_read():

    manager = OvsdbConnectionManager(settings.get('ovs_remote'),
                                     settings.get('ovs_schema'))
    manager.start()
    idl = manager.idl

    init_seq_no = 0
    # Wait until the connection is ready
    while True:
        idl.run()
        # Print self.idl.change_seqno
        if init_seq_no != idl.change_seqno:
            break
        time.sleep(0.001)

    schema = restparser.parseSchema(settings.get('ext_schema'))

    run_config_util = runconfig.RunConfigUtil(idl, schema)
    config = run_config_util.get_config()
    '''
    TODO: Adding json.dumps and the json.loads as a workaround because
    the config returned from test_read has a missing unicode character
    for keys
    '''
    temp_config = json.dumps(config)
    config = json.loads(temp_config)
    filename = 'config.db'
    with open(filename, 'w') as fp:
        json.dump(config, fp, sort_keys=True, indent=4,
                  separators=(',', ': '))
        fp.write('\n')
    return config
Exemplo n.º 4
0
def show_config(args):
    ret = True
    if (args[0] != "startup-config"):
        print("Unknown config \"%s\" (Use --help for help)" % args[0])
        return False

    cfg = cfgdb.Cfgdb()

    #OPS TODO: To get confg type from user as args
    row, tbl_found = cfg.find_row_by_type("startup")

    if tbl_found:
        try:
            data = json.loads(base64.b64decode(row.config))
            print("Startup configuration:")
            if (args[1] == "json"):
                print json.dumps(data, indent=4, sort_keys=True)
            elif (args[1] == "cli"):
                # Here we copy saved configuration from config DB to temporary
                # DB and the current startup configuration command displays
                # output by traversing the temporary DB.
                extschema = restparser.parseSchema(settings.get('ext_schema'))
                ovsschema = settings.get('ovs_schema')
                ovsremote = TEMPORARY_DB_SHOW_STARTUP

                # initialize idl
                opsidl = ops.dc.register(extschema, ovsschema, ovsremote)
                curr_seqno = opsidl.change_seqno
                while True:
                    opsidl.run()
                    if curr_seqno != opsidl.change_seqno:
                        break
                    poller = ovs.poller.Poller()
                    opsidl.wait(poller)
                    poller.block()

                # write to db
                result = ops.dc.write(data, extschema, opsidl)
                error = None
                if isinstance(result, tuple):
                    error = result[1]
                    result = result[0]
                else:
                    error = result

                if result not in [
                        ovs.db.idl.Transaction.SUCCESS,
                        ovs.db.idl.Transaction.UNCHANGED
                ]:
                    print "Transaction result %s" % result
                    print "Transaction result %s" % error
                    return False

        except ValueError, e:
            print("Invalid json from configdb. Exception: %s\n" % e)
            ret = False
Exemplo n.º 5
0
def get_idl():
    manager = OvsdbConnectionManager(settings.get('ovs_remote'),
                                     settings.get('ovs_schema'))
    manager.start()
    idl = manager.idl

    init_seq_no = 0
    while (init_seq_no == idl.change_seqno):
        idl.run()
        time.sleep(1)

    return idl
Exemplo n.º 6
0
def get_idl():
    manager = OvsdbConnectionManager(settings.get('ovs_remote'),
                                     settings.get('ovs_schema'))
    manager.start()
    idl = manager.idl

    init_seq_no = 0
    while (init_seq_no == idl.change_seqno):
        idl.run()
        time.sleep(1)

    return idl
Exemplo n.º 7
0
    def __init__(self):
        manager = OvsdbConnectionManager(settings.get("ovs_remote"), settings.get("cfg_db_schema"))
        manager.start()
        self.idl = manager.idl

        init_seq_no = 0
        # Wait until the connection is ready
        while True:
            self.idl.run()
            # print self.idl.change_seqno
            if init_seq_no != self.idl.change_seqno:
                break
            time.sleep(0.001)
Exemplo n.º 8
0
def get_runconfig():
    manager = OvsdbConnectionManager(settings.get('ovs_remote'),
                                     settings.get('ovs_schema'))
    manager.start()

    timeout = 10
    interval = 0
    init_seq_no = manager.idl.change_seqno

    while (init_seq_no == manager.idl.change_seqno):
        if interval > timeout:
            raise TypeError('timeout')
        manager.idl.run()
        interval += 1
        time.sleep(1)

    schema = restparser.parseSchema(settings.get('ext_schema'))
    return runconfig.RunConfigUtil(manager.idl, schema)
Exemplo n.º 9
0
 def __init__(self, schema_file):
     self.schema_file = settings.get(schema_file)
     try:
         json_schema = None
         with open(self.schema_file, 'r') as data_file:
             json_schema = json.load(data_file)
         self.validator = Draft4Validator(json_schema)
     except IOError as e:
         app_log.debug("Cannot read schema file: %s" % e.message)
     except SchemaError as e:
         app_log.debug("Schema error: %s" % e.message)
Exemplo n.º 10
0
 def __init__(self, schema_file):
     self.schema_file = settings.get(schema_file)
     try:
         json_schema = None
         with open(self.schema_file, 'r') as data_file:
             json_schema = json.load(data_file)
         self.validator = Draft4Validator(json_schema)
     except IOError as e:
         app_log.debug("Cannot read schema file: %s" % e.message)
     except SchemaError as e:
         app_log.debug("Schema error: %s" % e.message)
Exemplo n.º 11
0
def show_config(args):
    ret = True
    if (args[0] != "startup-config"):
        print("Unknown config \"%s\" (Use --help for help)" % args[0])
        return False

    cfg = cfgdb.Cfgdb()

    #OPS TODO: To get confg type from user as args
    row, tbl_found = cfg.find_row_by_type("startup")

    if tbl_found:
        try:
            parsed = json.loads(row.config)
            print("Startup configuration:")
            if (args[1] == "json"):
                print json.dumps(parsed,  indent=4, sort_keys=True)
            elif (args[1] == "cli"):
                # Here we copy saved configuration from config DB to temporary
                # DB and the current startup configuration command displays
                # output by traversing the temporary DB.
                manager = OvsdbConnectionManager(TEMPORARY_DB_SHOW_STARTUP,
                                                 settings.get('ovs_schema'))
                manager.start()
                cnt = 30
                while not manager.idl.run() and cnt > 0:
                    time.sleep(.1)
                    cnt -= 1
                if cnt <= 0:
                    print("IDL connection timeout")
                    return False
                # read the schema
                schema = restparser.parseSchema(settings.get('ext_schema'))
                run_config_util = RunConfigUtil(manager.idl, schema)
                run_config_util.write_config_to_db(parsed)
                manager.idl.close()

        except ValueError, e:
            print("Invalid json from configdb. Exception: %s\n" % e)
            ret = False
Exemplo n.º 12
0
def copy_running_startup():

    # get running config
    extschema = restparser.parseSchema(settings.get('ext_schema'))
    ovsschema = settings.get('ovs_schema')
    ovsremote = settings.get('ovs_remote')
    print "Copy in progress ...."

    # initialize idl
    opsidl = ops.dc.register(extschema, ovsschema, ovsremote)
    curr_seqno = opsidl.change_seqno
    while True:
        opsidl.run()
        if curr_seqno != opsidl.change_seqno:
            break
        poller = ovs.poller.Poller()
        opsidl.wait(poller)
        poller.block()

    try:
        running_config = ops.dc.read(extschema, opsidl)
    except:
        print "ERROR: Copy failed"
        return False

    # base64 encode to save as startup
    config = base64.b64encode(json.dumps(running_config))
    cfg = cfgdb.Cfgdb()
    cfg.config = config
    cfg.type = "startup"
    row, tbl_found = cfg.find_row_by_type("startup")
    if tbl_found:
        cfg.update_row(row)
    else:
        cfg.insert_row()

    cfg.close()

    print "Success"
    return True
Exemplo n.º 13
0
def test_write(filename):

    with open(filename) as json_data:
        data = json.load(json_data)
        json_data.close()

    # Set up IDL
    manager = OvsdbConnectionManager(settings.get('ovs_remote'),
                                     settings.get('ovs_schema'))
    manager.start()
    manager.idl.run()

    init_seq_no = 0
    while True:
        manager.idl.run()
        if init_seq_no != manager.idl.change_seqno:
            break

    # Read the schema
    schema = restparser.parseSchema(settings.get('ext_schema'))
    run_config_util = runconfig.RunConfigUtil(manager.idl, schema)
    run_config_util.write_config_to_db(data)
Exemplo n.º 14
0
def test_write(filename):

    with open(filename) as json_data:
        data = json.load(json_data)
        json_data.close()

    # Set up IDL
    manager = OvsdbConnectionManager(settings.get('ovs_remote'),
                                     settings.get('ovs_schema'))
    manager.start()
    manager.idl.run()

    init_seq_no = 0
    while True:
        manager.idl.run()
        if init_seq_no != manager.idl.change_seqno:
            break

    # Read the schema
    schema = restparser.parseSchema(settings.get('ext_schema'))
    run_config_util = runconfig.RunConfigUtil(manager.idl, schema)
    run_config_util.write_config_to_db(data)
Exemplo n.º 15
0
def push_config_to_db():
    '''
    Take the previously discovered startup configuration and
    push it to the database.
    '''

    global saved_config

    if saved_config is None:
        vlog.info('No saved configuration exists')
    else:
        #OPS_TODO: Change this log msg to the actual push code when available
        vlog.info('Config data found')
        try:
            data = json.loads(saved_config)
        except ValueError, e:
            print("Invalid json from configdb. Exception: %s\n" % e)
            return

        # set up IDL
        manager = OvsdbConnectionManager(settings.get('ovs_remote'),
                                         settings.get('ovs_schema'))
        manager.start()
        manager.idl.run()

        init_seq_no = manager.idl.change_seqno
        while True:
            manager.idl.run()
            if init_seq_no != manager.idl.change_seqno:
                break
            sleep(1)

        # read the schema
        schema = restparser.parseSchema(settings.get('ext_schema'))
        run_config_util = RunConfigUtil(manager.idl, schema)
        run_config_util.write_config_to_db(data)
Exemplo n.º 16
0
    row, tbl_found = cfg.find_row_by_type("startup")

    if tbl_found:
        try:
            data = json.loads(base64.b64decode(row.config))
        except ValueError, e:
            print("Invalid json from configdb. Exception: %s\n" % e)
            cfg.close()
            return False
    else:
        print('No saved configuration exists')
        cfg.close()
        return False

    print "Copy in progress ...."
    extschema = restparser.parseSchema(settings.get('ext_schema'))
    ovsschema = settings.get('ovs_schema')
    ovsremote = settings.get('ovs_remote')

    # initialize idl
    opsidl = ops.dc.register(extschema, ovsschema, ovsremote)
    curr_seqno = opsidl.change_seqno
    while True:
        opsidl.run()
        if curr_seqno != opsidl.change_seqno:
            break
        poller = ovs.poller.Poller()
        opsidl.wait(poller)
        poller.block()

    result = ops.dc.write(data, extschema, opsidl)
Exemplo n.º 17
0
def get_schema():
    return restparser.parseSchema(settings.get('ext_schema'))
Exemplo n.º 18
0
    row, tbl_found = cfg.find_row_by_type("startup")

    if tbl_found:
        try:
            data = json.loads(row.config)
        except ValueError, e:
            print("Invalid json from configdb. Exception: %s\n" % e)
            cfg.close()
            return False
    else:
        print('No saved configuration exists')
        cfg.close()
        return False

    # set up IDL
    manager = OvsdbConnectionManager(settings.get('ovs_remote'),
                                     settings.get('ovs_schema'))
    manager.start()
    init_seq_no = manager.idl.change_seqno
    while True:
        manager.idl.run()
        if init_seq_no != manager.idl.change_seqno:
            break
        time.sleep(1)

    # read the schema
    schema = restparser.parseSchema(settings.get('ext_schema'))
    run_config_util = RunConfigUtil(manager.idl, schema)
    run_config_util.write_config_to_db(data)
    cfg.close()
    return True
Exemplo n.º 19
0
def get_schema():
    return restparser.parseSchema(settings.get('ext_schema'))