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
示例#2
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
示例#3
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
示例#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
示例#5
0
    def __init__(self, settings):
        self.settings = settings
        self.settings['cookie_secret'] = cookiesecret.generate_cookie_secret()
        self.manager = OvsdbConnectionManager(self.settings.get('ovs_remote'),
                                              self.settings.get('ovs_schema'))
        schema = self.settings.get('ext_schema')
        self.restschema = restparser.parseSchema(schema)
        self._url_patterns = self._get_url_patterns()
        Application.__init__(self, self._url_patterns, **self.settings)

        # connect to OVSDB using a callback
        IOLoop.current().add_callback(self.manager.start)

        # Load all custom validators
        validator.init_plugins(constants.OPSPLUGIN_DIR)
def get_opsidl():
    extschema = restparser.parseSchema(settings.get('ext_schema'))
    ovsschema = settings.get('ovs_schema')
    ovsremote = settings.get('ovs_remote')
    opsidl = ops.dc.register(extschema, ovsschema, ovsremote)

    init_seqno = opsidl.change_seqno
    while True:
        opsidl.run()
        if init_seqno != opsidl.change_seqno:
            break
        poller = ovs.poller.Poller()
        opsidl.wait(poller)
        poller.block()

    return (extschema, opsidl)
示例#7
0
def get_opsidl():
    extschema = restparser.parseSchema(settings.get('ext_schema'))
    ovsschema = settings.get('ovs_schema')
    ovsremote = settings.get('ovs_remote')
    opsidl = ops.dc.register(extschema, ovsschema, ovsremote)

    init_seqno = opsidl.change_seqno
    while True:
        opsidl.run()
        if init_seqno != opsidl.change_seqno:
            break
        poller = ovs.poller.Poller()
        opsidl.wait(poller)
        poller.block()

    return (extschema, opsidl)
示例#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)
示例#9
0
    def __init__(self, settings):
        self.settings = settings
        self.settings['cookie_secret'] = cookiesecret.generate_cookie_secret()
        schema = self.settings.get('ext_schema')
        self.restschema = restparser.parseSchema(schema)
        self.manager = OvsdbConnectionManager(self.settings.get('ovs_remote'),
                                              self.settings.get('ovs_schema'),
                                              self.restschema)
        self._url_patterns = self._get_url_patterns()
        Application.__init__(self, self._url_patterns, **self.settings)

        # We must block the application start until idl connection
        # and replica is ready
        self.manager.start()

        # Load all custom validators
        validator.init_plugins(constants.OPSPLUGIN_DIR)

        self.notification_handler = NotificationHandler(
            self.restschema, self.manager)
示例#10
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
示例#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
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)
示例#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)
示例#14
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)
示例#15
0
def get_schema():
    return restparser.parseSchema(settings.get('ext_schema'))
示例#16
0
        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


def copy_config(args):
    ret = True
    if (args[0] == "running-config" and args[1] == "startup-config"):
        ret = copy_running_startup()
    elif (args[0] == "startup-config" and args[1] == "running-config"):
        ret = copy_startup_running()
    else:
        print("Unknow config (use --help for help)")
        ret = False
示例#17
0
def get_schema():
    return restparser.parseSchema(settings.get('ext_schema'))
示例#18
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)