1# Derived from portconquery.py
2#
3# This file is part of SETools.
4#
5# SETools is free software: you can redistribute it and/or modify
6# it under the terms of the GNU Lesser General Public License as
7# published by the Free Software Foundation, either version 2.1 of
8# the License, or (at your option) any later version.
9#
10# SETools is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU Lesser General Public License for more details.
14#
15# You should have received a copy of the GNU Lesser General Public
16# License along with SETools.  If not, see
17# <http://www.gnu.org/licenses/>.
18#
19import logging
20
21from .mixins import MatchContext
22from .policyrep.xencontext import addr_range
23from .query import PolicyQuery
24from .util import match_range
25
26
27class IomemconQuery(MatchContext, PolicyQuery):
28
29    """
30    Iomemcon context query.
31
32    Parameter:
33    policy          The policy to query.
34
35    Keyword Parameters/Class attributes:
36    addr            A 2-tuple of the memory addr range to match. (Set both to
37                    the same value for a single mem addr)
38    addr_subset     If true, the criteria will match if it is a subset
39                    of the iomemcon's range.
40    addr_overlap    If true, the criteria will match if it overlaps
41                    any of the iomemcon's range.
42    addr_superset   If true, the criteria will match if it is a superset
43                    of the iomemcon's range.
44    addr_proper     If true, use proper superset/subset operations.
45                    No effect if not using set operations.
46
47    user            The criteria to match the context's user.
48    user_regex      If true, regular expression matching
49                    will be used on the user.
50
51    role            The criteria to match the context's role.
52    role_regex      If true, regular expression matching
53                    will be used on the role.
54
55    type_           The criteria to match the context's type.
56    type_regex      If true, regular expression matching
57                    will be used on the type.
58
59    range_          The criteria to match the context's range.
60    range_subset    If true, the criteria will match if it is a subset
61                    of the context's range.
62    range_overlap   If true, the criteria will match if it overlaps
63                    any of the context's range.
64    range_superset  If true, the criteria will match if it is a superset
65                    of the context's range.
66    range_proper    If true, use proper superset/subset operations.
67                    No effect if not using set operations.
68    """
69
70    _addr = None
71    addr_subset = False
72    addr_overlap = False
73    addr_superset = False
74    addr_proper = False
75
76    @property
77    def addr(self):
78        return self._addr
79
80    @addr.setter
81    def addr(self, value):
82        pending_addr = addr_range(*value)
83
84        if all(pending_addr):
85            if pending_addr.low < 1 or pending_addr.high < 1:
86                raise ValueError("Memory address must be positive: {0.low}-{0.high}".
87                                 format(pending_addr))
88
89            if pending_addr.low > pending_addr.high:
90                raise ValueError(
91                    "The low mem addr must be smaller than the high mem addr: {0.low}-{0.high}".
92                    format(pending_addr))
93
94            self._addr = pending_addr
95        else:
96            self._addr = None
97
98    def __init__(self, policy, **kwargs):
99        super(IomemconQuery, self).__init__(policy, **kwargs)
100        self.log = logging.getLogger(__name__)
101
102    def results(self):
103        """Generator which yields all matching iomemcons."""
104        self.log.info("Generating results from {0.policy}".format(self))
105        self.log.debug("Address: {0.addr!r}, overlap: {0.addr_overlap}, "
106                       "subset: {0.addr_subset}, superset: {0.addr_superset}, "
107                       "proper: {0.addr_proper}".format(self))
108        self._match_context_debug(self.log)
109
110        for iomemcon in self.policy.iomemcons():
111
112            if self.addr and not match_range(
113                    iomemcon.addr,
114                    self.addr,
115                    self.addr_subset,
116                    self.addr_overlap,
117                    self.addr_superset,
118                    self.addr_proper):
119                continue
120
121            if not self._match_context(iomemcon.context):
122                continue
123
124            yield iomemcon
125