aci.py 9.37 KB
Newer Older
1 2 3 4 5
"""Parse and Represent the APPC ACI specification."""
import collections
import logging
import json

6
logger = logging.getLogger('nrm')
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
spec = collections.namedtuple('Field', ['cls', 'required'])


class SpecField(object):

    """Object part of the ACI Image Manifest fields."""

    fields = {}

    def __init__(self):
        """Create empty field."""
        pass

    def load(self, data):
        """Load fields."""
        for key in self.fields:
            spec = self.fields[key]
            if key not in data:
                if spec.required:
                    logger.error("Missing key from manifest: %s", key)
                    return False
            else:
                ok, v = self.loadfield(data[key], spec.cls)
                if not ok:
                    logger.error("Error for key %s in %s", key, self.__class__)
                    return False
                setattr(self, key, v)
        return True

    def loadfield(self, data, cls):
        """load data as if from a field of the provided cls.

        Make sure the basic types are also respected.
        """
        ret = cls()
        if not hasattr(ret, 'load'):
            if not isinstance(data, cls):
                logger.error("Wrong data type %s, expected: %s", cls,
                             data.__class__)
                return (False, None)
            else:
                return (True, data)
        else:
            return (ret.load(data), ret)


class Scheduler(SpecField):

    """Scheduler information for a container."""

    classes = ['SCHED_FIFO', 'SCHED_HPC', 'SCHED_OTHER']

    fields = {"policy": spec(unicode, True),
60
              "priority": spec(unicode, False),
61
              "enabled": spec(unicode, False),
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
              }

    def __init__(self):
        """Create scheduler object."""
        pass

    def load(self, data):
        """Load configuration from json text."""
        ret = super(Scheduler, self).load(data)
        if not ret:
            return ret
        # check scheduler class & prio
        if self.policy not in self.classes:
            logger.error("Wrong scheduling class %s, not any of %r", data,
                         Scheduler.classes)
            return False
        if self.policy != "SCHED_OTHER":
            logger.warning("scheduler priority forced as 0 " +
                           "for non default policies")
            self.priority = "0"
82 83 84 85
        if getattr(self, "enabled", "1") not in ["0", "False", "1", "True"]:
            logger.error("Invalid value for scheduler enabled: %s",
                         self.enabled)
            return False
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
        return True


class CPUSet(SpecField):

    """Represent a CPUSet field."""

    def __init__(self):
        """Create an empty set."""
        pass

    def load(self, data):
        """Load from json object."""
        self.value = data
        return True


class MemSet(SpecField):

    """Represent a MemSet field."""

    def __init__(self):
        """Create an empty set."""
        pass

    def load(self, data):
        """Load from json object."""
        self.value = data
        return True


class Container(SpecField):

    """Container Information."""

    fields = {"cpus": spec(CPUSet, True),
              "mems": spec(MemSet, True)
              }

    def __init__(self):
        """Create empty container."""
        pass

    def load(self, data):
        """Load container information."""
        return super(Container, self).load(data)

133

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
class PerfWrapper(SpecField):

    """Information on whether to use perf for a container."""

    fields = {"enabled": spec(unicode, False)
              }

    def __init__(self):
        """Create empty perf wrapper."""
        pass

    def load(self, data):
        """Load perf wrapper information."""
        ret = super(PerfWrapper, self).load(data)
        if not ret:
            return ret
150
        if getattr(self, "enabled", "1") not in ["0", "False", "1", "True"]:
151
            logger.error("Invalid value for perfwrapper enabled: %s",
152 153 154
                         self.enabled)
            return False
        return True
155

156

157
class Power(SpecField):
158

159
    """Power settings for a container."""
160 161 162 163

    policies = ['NONE', 'DDCM', 'DVFS', 'COMBINED']

    fields = {"enabled": spec(unicode, False),
164
              "profile": spec(unicode, False),
165 166 167
              "policy": spec(unicode, False),
              "damper": spec(unicode, False),
              "slowdown": spec(unicode, False)
168 169 170
              }

    def __init__(self):
171
        """Create empty power settings object."""
172 173 174
        pass

    def load(self, data):
175 176
        """Load power settings."""
        ret = super(Power, self).load(data)
177 178 179
        if not ret:
            return ret
        if self.enabled not in ["0", "False", "1", "True"]:
180 181 182 183 184
            logger.error("Invalid value for power enabled: %s",
                         self.enabled)
            return False
        if self.profile not in ["0", "False", "1", "True"]:
            logger.error("Invalid value for power profile: %s",
185 186 187
                         self.enabled)
            return False
        if self.policy not in self.policies:
188
            logger.error("Invalid value for power policy: %s",
189
                         self.policy)
190 191
            return False
        if self.damper < 0.0:
192
            logger.error("Invalid value for power policy damper: %s",
193 194 195
                         self.policy)
            return False
        if self.slowdown < 1.0:
196
            logger.error("Invalid value for power policy slowdown: %s",
197 198 199 200 201 202 203 204
                         self.policy)
            return False
        if self.damper < 0.0:
            logger.error("Invalid value of powerpolicy damper: %s",
                         self.policy)
            return False
        if self.slowdown < 1.0:
            logger.error("Invalid value of powerpolicy slowdown: %s",
205
                         self.policy)
206 207 208 209
            return False
        return True


210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
class HwBind(SpecField):

    """Hardware bindings for a container."""

    fields = {"enabled": spec(unicode, False),
              }

    def __init__(self):
        """Create empty hardware bindings  settings object."""
        pass

    def load(self, data):
        """Load hardware bindings settings."""
        ret = super(HwBind, self).load(data)
        if not ret:
            return ret
        if self.enabled not in ["0", "False", "1", "True"]:
            logger.error("Invalid value for hardware bindings enabled: %s",
                         self.enabled)
            return False
        return True


233 234 235 236 237
class IsolatorList(SpecField):

    """Represent the list of isolator in a Manifest."""

    types = {"argo/scheduler": spec(Scheduler, False),
238
             "argo/container": spec(Container, True),
239
             "argo/perfwrapper": spec(PerfWrapper, False),
240
             "argo/power": spec(Power, False),
241
             "argo/hwbind": spec(HwBind, False),
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
             }

    def __init__(self):
        """Create empty list."""
        pass

    def load(self, data):
        """Load from json struct."""
        for e in data:
            name = e['name']
            if name in self.types:
                t = self.types[name]
                ok, v = super(IsolatorList, self).loadfield(e['value'], t.cls)
                if not ok:
                    logger.error("Error with %s in %s", name, self.__class__)
                    return False
                setattr(self, name.lstrip("argo/"), v)
        for k in self.types:
            if self.types[k].required:
261 262 263
                if not hasattr(self, k.lstrip("argo/")):
                    logger.error("Missing mandatory isolator: %s", k)
                    return False
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
        return True


class App(SpecField):

    """Represent the App part of an Image Manifest."""

    # attribute, subclass, required
    fields = {"environment": spec(list, False),
              "isolators": spec(IsolatorList, True),
              }

    def __init__(self):
        """Create empty container."""
        pass

    def load(self, data):
        """Load from json dict."""
        return super(App, self).load(data)


class ImageManifest(SpecField):

    """Represent an ACI Image Manifest."""

    fields = {"acKind": spec(unicode, True),
              "acVersion": spec(unicode, True),
              "name": spec(unicode, True),
292
              "app": spec(App, True),
293 294 295 296 297 298 299 300 301 302 303
              }

    def __init__(self):
        """Create empty manifest."""
        pass

    def load(self, filename):
        """Load a manifest from JSON file."""
        with open(filename, 'r') as f:
            data = json.load(f)
        return super(ImageManifest, self).load(data)
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

    def load_dict(self, data):
        """Load a manifest in dictionary form."""
        return super(ImageManifest, self).load(data)

    def is_feature_enabled(self, feature, true_values=["1", "True"]):
        """Check if a specific feature is enabled.

        Since the enabled field itself is optional, we return true if an
        isolator is present in a manifest or the enabled field is not true."""
        typename = "argo/{}".format(feature)
        assert typename in IsolatorList.types, \
            "{} in not a valid feature".format(feature)
        logger.debug(repr(self))
        if hasattr(self.app.isolators, feature):
            isolator = getattr(self.app.isolators, feature)
            if hasattr(isolator, 'enabled'):
                if isolator.enabled not in true_values:
                    return False
323 324 325
            return True
        else:
            return False