~ubuntu-branches/ubuntu/raring/cinder/raring-updates

« back to all changes in this revision

Viewing changes to cinder/openstack/common/scheduler/filter.py

Tags: upstream-2013.1~g2
ImportĀ upstreamĀ versionĀ 2013.1~g2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2011-2012 OpenStack, LLC.
 
2
# All Rights Reserved.
 
3
#
 
4
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
5
#    not use this file except in compliance with the License. You may obtain
 
6
#    a copy of the License at
 
7
#
 
8
#         http://www.apache.org/licenses/LICENSE-2.0
 
9
#
 
10
#    Unless required by applicable law or agreed to in writing, software
 
11
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
12
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
13
#    License for the specific language governing permissions and limitations
 
14
#    under the License.
 
15
 
 
16
"""
 
17
Filter support
 
18
"""
 
19
 
 
20
import inspect
 
21
 
 
22
from stevedore import extension
 
23
 
 
24
 
 
25
class BaseFilter(object):
 
26
    """Base class for all filter classes."""
 
27
    def _filter_one(self, obj, filter_properties):
 
28
        """Return True if it passes the filter, False otherwise.
 
29
        Override this in a subclass.
 
30
        """
 
31
        return True
 
32
 
 
33
    def filter_all(self, filter_obj_list, filter_properties):
 
34
        """Yield objects that pass the filter.
 
35
 
 
36
        Can be overriden in a subclass, if you need to base filtering
 
37
        decisions on all objects.  Otherwise, one can just override
 
38
        _filter_one() to filter a single object.
 
39
        """
 
40
        for obj in filter_obj_list:
 
41
            if self._filter_one(obj, filter_properties):
 
42
                yield obj
 
43
 
 
44
 
 
45
class BaseFilterHandler(object):
 
46
    """ Base class to handle loading filter classes.
 
47
 
 
48
    This class should be subclassed where one needs to use filters.
 
49
    """
 
50
    def __init__(self, filter_class_type, filter_namespace):
 
51
        self.namespace = filter_namespace
 
52
        self.filter_class_type = filter_class_type
 
53
        self.filter_manager = extension.ExtensionManager(filter_namespace)
 
54
 
 
55
    def _is_correct_class(self, obj):
 
56
        """Return whether an object is a class of the correct type and
 
57
        is not prefixed with an underscore.
 
58
        """
 
59
        return (inspect.isclass(obj) and
 
60
                not obj.__name__.startswith('_') and
 
61
                issubclass(obj, self.filter_class_type))
 
62
 
 
63
    def get_all_classes(self):
 
64
        return [x.plugin for x in self.filter_manager
 
65
                if self._is_correct_class(x.plugin)]
 
66
 
 
67
    def get_filtered_objects(self, filter_classes, objs,
 
68
                             filter_properties):
 
69
        for filter_cls in filter_classes:
 
70
            objs = filter_cls().filter_all(objs, filter_properties)
 
71
        return list(objs)