Exemple #1
0
    def _setup_jobs(self):
        """
        "Maps device hostnames to `~trigger.netdevices.NetDevice` objects and
        populates the job queue.
        """
        for dev in self.devices:
            log.msg('Adding', dev)
            if self.verbose:
                print 'Adding', dev

            # Make sure that devices are actually in netdevices and keep going
            try:
                devobj = self.nd.find(str(dev))
            except KeyError:
                msg = 'Device not found in NetDevices: %s' % dev
                log.err(msg)
                if self.verbose:
                    print 'ERROR:', msg

                # Track the errors and keep moving
                self.store_error(dev, msg)
                continue

            # We only want to add devices for which we've enabled support in
            # this class
            if devobj.vendor not in self.vendors:
                raise exceptions.UnsupportedVendor(
                    "The vendor '%s' is not specified in ``vendors``. Could not add %s to job queue. Please check the attribute in the class object."
                    % (devobj.vendor, devobj))

            self.jobs.append(devobj)
Exemple #2
0
    def _lookup_method(self, device, method):
        """
        Base lookup method. Looks up stuff by device manufacturer like:

            from_juniper
            to_foundry

        and defaults to ``self.from_base`` and ``self.to_base`` methods if
        customized methods not found.

        :param device:
            A `~trigger.netdevices.NetDevice` object

        :param method:
            One of 'generate', 'parse'
        """

        METHOD_MAP = {
            'generate': 'to_%s',
            'parse': 'from_%s',
        }
        assert method in METHOD_MAP

        desired_method = None

        # Select the desired vendor name.
        desired_vendor = device.vendor.name

        vendor_types = self.platforms.get(desired_vendor)
        method_name = METHOD_MAP[method] % desired_vendor  # => 'to_cisco'
        device_type = device.deviceType

        if device_type in vendor_types:
            if hasattr(self, method_name):
                log.msg('[%s] Found %r method: %s' %
                        (device, method, method_name))
                desired_method = method_name
            else:
                log.msg('[%s] Did not find %r method: %s' %
                        (device, method, method_name))
        else:
            raise exceptions.UnsupportedDeviceType(
                'Device %r has an invalid type %r for vendor %r. Must be '
                'one of %r.' %
                (device.nodeName, device_type, desired_vendor, vendor_types))

        if desired_method is None:
            if self.allow_fallback:
                desired_method = METHOD_MAP[method] % 'base'
                log.msg('[%s] Fallback enabled. Using base method: %r' %
                        (device, desired_method))
            else:
                raise exceptions.UnsupportedVendor(
                    'The vendor %r had no available %s method. Please check '
                    'your `vendors` and `platforms` attributes in your class '
                    'object.' % (device.vendor.name, method))

        func = getattr(self, desired_method)
        return func
Exemple #3
0
    def _lookup_method(self, device, method):
        """
        Base lookup method. Looks up stuff by device manufacturer like:

            from_juniper
            to_foundry

        :param device:
            A `~trigger.netdevices.NetDevice` object

        :param method:
            One of 'generate', 'parse'
        """
        METHOD_MAP = {
            'generate': 'to_%s',
            'parse': 'from_%s',
        }
        assert method in METHOD_MAP

        desired_attr = None

        # Select the desired vendor name.
        desired_vendor = device.vendor
        if device.is_netscreen(
        ):  # Workaround until we implement device drivers
            desired_vendor = 'netscreen'

        for vendor, types in self.platforms.iteritems():
            meth_attr = METHOD_MAP[method] % desired_vendor
            if device.deviceType in types:
                if hasattr(self, meth_attr):
                    desired_attr = meth_attr
                    break

        if desired_attr is None:
            if self.allow_fallback:
                desired_attr = METHOD_MAP[method] % 'base'
            else:
                raise exceptions.UnsupportedVendor(
                    "The vendor '%s' had no available %s method. Please check your ``vendors`` and ``platforms`` attributes in your class object."
                    % (device.vendor, method))

        func = getattr(self, desired_attr)
        return func