Exemplo n.º 1
0
    def store_structure(device, feature):

        # get feature and attributes
        [(ft, attr)] = feature.items()
        log.info(
            banner("Learning '{n}' feature with "
                   "attribues {a} on device {d}".format(n=ft, a=attr,
                                                        d=device)))

        # perform lookup per device
        lib = Lookup.from_device(device)

        # attach ops and conf
        lib.conf = getattr(lib, 'conf', conf)
        lib.ops = getattr(lib, 'ops', ops)

        # create the ops/conf instance
        try:
            obj = attrgetter(ft)(lib)
        except Exception:
            raise AttributeError('Cannot load %s for '
                                 'device %s.' % (ft, device.name))
        # conf learn_config
        if issubclass(obj, ConfBase):
            ret = obj.learn_config(device=device, attributes=attr)
            ret = _to_dict(ret[0])
            # delete the non-used objects for pcall to retrun
            ret.pop('__testbed__')
            ret.pop('devices')
            ret.pop('interfaces')
            remove_parent_from_conf_dict(ret['device_attr'][device.name])

        elif issubclass(obj, OpsBase):
            ret = obj(device, attributes=attr)
            ret.learn()
            temp = AttrDict()
            temp.info = getattr(ret, 'info', {})
            ret = temp

        ret_dict = {}

        ret_dict.setdefault('lts', {}).\
            setdefault(ft, {}).setdefault(device.name, ret)

        # return the dictionary
        return ret_dict
Exemplo n.º 2
0
    def _verify_conf_2(self, device, verify_conf, reqs, missing, ops,
                       requirements):
        o = verify_conf.learn_config(device=device)

        # convert from conf instance to dictionary
        o = [_to_dict(item) for item in o]

        # Make sure that configured path is correctly configured
        try:
            self._verify_finds_ops(ops=o[0], requirements=reqs['list'],
                                   missing=missing, obj_mod=ops,
                                   org_req=requirements)
        except ValueError as e:
            raise e
        except Exception as e:
            raise Exception("Could not verify the configuration was "
                            "applied correctly as per the exception: "
                            "{e}".format(e=e))
Exemplo n.º 3
0
    def _learn_base(self, device, name, step, abstracted_base, is_ops, base,
            requirements, verify=False, verify_find=None, **kwargs):

        try:
            # Populate the kwargs
            kwargs.update(requirements.get('kwargs', {}))
            requirements_ = requirements.get('requirements', [])
            exclude = requirements.get('exclude', [])
            all_keys = requirements.get('all_keys', False)

            if requirements_:
                # Populate the keys into R object
                # Useful if want to learn from previously learnt requirements
                requirements_ = self._populate_path(requirements_, device,
                                                    keys=self.keys)

            # process ops requirements
            if is_ops:
                o = abstracted_base(device=device, **kwargs)
                learn_poll = {}

                if not verify:
                    if not verify_find:
                        verify_find = self._verify_find

                    # First time we learn, no need to loop
                    learn_poll['attempt'] = 1
                    learn_poll['sleep'] = 0
                    learn_poll['verify'] = verify_find
                    learn_poll['all_keys'] = all_keys
                    learn_poll['requirements'] = requirements_
                else:
                    learn_poll = {'timeout': self.timeout}
                    if not verify_find:
                        verify_find = self._verify_same
                    name = o.__class__ if hasattr(o, '__class__') else o
                    log.info("Learning '{n}' feature".format(n=name))

                    learn_poll['initial'] = self._ops_ret[base]
                    learn_poll['verify'] = verify_find
                    learn_poll['exclude'] = self._populate_exclude(exclude)

                    learn_poll.update(kwargs)

                    # skip the learning for verify
                    if base not in self._ops_ret:
                        return o

                # Learn and verify the ops
                o.learn_poll(**learn_poll)
                return o

            # process conf requirements
            else:
                # import conf module
                ## get conf class
                cls = abstracted_base
                if issubclass(cls, ConfBase):
                    if verify:
                        name = cls.__name__ if hasattr(cls, '__name__') else cls
                        log.info("Learning '{n}' feature".format(n=name))
                        # skip the learning for verify
                        if base not in self._conf_ret:
                            return

                    # learn conf with show running-config
                    o = cls.learn_config(device=device)
                    # convert from conf instance to dictionary
                    o = [_to_dict(item) for item in o]

                    if verify:
                        # sort both list
                        initial = sorted(self._conf_ret[base])[:1]
                        restore = sorted(o)[:1]
                        # compare lengh to begin with
                        len_initial = len(initial)
                        len_restore = len(restore)
                        if len_initial != len_restore:
                            raise Exception("Current ops is not equal "
                                            "to the initial Snapshot "
                                            "taken\n{} != {}".format(
                                              len_initial, len_restore))
                        # zip and verify whether they are the same
                        for before, after in zip(initial, restore):
                            # verify conf
                            self._verify_same(after, before, exclude)
                    else:
                        for item in o:
                            # verify requirements
                            self._verify_find(item, requirements_, all_keys)
                    return o[:1]
        except Exception as e:
            raise