summaryrefslogtreecommitdiff
path: root/pycadf/cadftype.py
blob: 80cec829036f81d70f9dc3c8d5fd9d6c01a24d92 (plain)
1
2
3
4
5
6
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Copyright (c) 2013 IBM Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

import abc

from oslo_serialization import jsonutils
import six

CADF_SCHEMA_1_0_0 = 'cadf:'
CADF_VERSION_1_0_0 = 'http://schemas.dmtf.org/cloud/audit/1.0/'

# Valid cadf:Event record "types"
EVENTTYPE_ACTIVITY = 'activity'
EVENTTYPE_MONITOR = 'monitor'
EVENTTYPE_CONTROL = 'control'

VALID_EVENTTYPES = frozenset([
    EVENTTYPE_ACTIVITY,
    EVENTTYPE_MONITOR,
    EVENTTYPE_CONTROL
])


def is_valid_eventType(value):
    return value in VALID_EVENTTYPES

# valid cadf:Event record "Reporter" roles
REPORTER_ROLE_OBSERVER = 'observer'
REPORTER_ROLE_MODIFIER = 'modifier'
REPORTER_ROLE_RELAY = 'relay'

VALID_REPORTER_ROLES = frozenset([
    REPORTER_ROLE_OBSERVER,
    REPORTER_ROLE_MODIFIER,
    REPORTER_ROLE_RELAY
])


def is_valid_reporter_role(value):
    return value in VALID_REPORTER_ROLES


class ValidatorDescriptor(object):
    def __init__(self, name, func=None):
        self.name = name
        self.func = func

    def __set__(self, instance, value):
        if value is not None:
            if self.func is not None:
                if self.func(value):
                    instance.__dict__[self.name] = value
                else:
                    raise ValueError('%s failed validation: %s' %
                                     (self.name, self.func))
            else:
                instance.__dict__[self.name] = value
        else:
            raise ValueError('%s must not be None.' % self.name)


@six.add_metaclass(abc.ABCMeta)
class CADFAbstractType(object):
    """The abstract base class for all CADF (complex) data types (classes)."""

    @abc.abstractmethod
    def is_valid(self, value):
        pass

    def as_dict(self):
        """Return dict representation of Event."""
        return jsonutils.to_primitive(self, convert_instances=True)

    def _isset(self, attr):
        """Check to see if attribute is defined."""
        try:
            if isinstance(getattr(self, attr), ValidatorDescriptor):
                return False
            return True
        except AttributeError:
            return False

    # TODO(mrutkows): Eventually, we want to use the OrderedDict (introduced
    # in Python 2.7) type for all CADF classes to store attributes in a
    # canonical form.  Currently, OpenStack/Jenkins requires 2.6 compatibility
    # The reason is that we want to be able to support signing all or parts
    # of the event record and need to guarantee order.
    # def to_ordered_dict(self, value):
    #    pass