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

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

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

# coding=utf-8 

# 

# Copyright (C) 2010-2012 Platform Computing 

# 

# This library is free software; you can redistribute it and/or 

# modify it under the terms of the GNU Lesser General Public 

# License as published by the Free Software Foundation; either 

# version 2.1 of the License, or (at your option) any later version. 

# 

# This library is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 

# Lesser General Public License for more details. 

# 

# You should have received a copy of the GNU Lesser General Public 

# License along with this library; if not, write to the Free Software 

# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA 

# 

''' 

OCCI's core model. 

 

Created on Jun 27, 2011 

 

@author: tmetsch 

''' 

 

# disabling 'Too few public methods' pylint check (It's a model...) 

# disabling 'Too many arguments' pylint check (It's more elegant this way...) 

# pylint: disable=R0903, R0913 

 

#============================================================================== 

# Categories 

#============================================================================== 

 

 

class Category(object): 

    ''' 

    OCCI Category. 

    ''' 

 

    def __init__(self, scheme, term, title, attributes, location): 

        self.scheme = scheme 

        self.term = term 

        self.title = title 

        self.attributes = attributes 

 

        # location 

        self.location = location 

        self.extras = None 

 

    def __eq__(self, instance): 

        if instance is None or not isinstance(instance, Category): 

            return False 

        if self.term == instance.term and self.scheme == instance.scheme \ 

            and self.extras == instance.extras: 

            return True 

        else: 

            return False 

 

    def __hash__(self): 

        return hash(self.scheme) ^ hash(self.term) ^ hash(str(self.extras)) 

 

    def __str__(self): 

        return self.scheme + self.term 

 

 

class Kind(Category): 

    ''' 

    OCCI Kind. 

    ''' 

 

    def __init__(self, scheme, term, related=None, actions=None, title='', 

                 attributes=None, location=None): 

        super(Kind, self).__init__(scheme, term, title, attributes or {}, 

                                   location or '/' + term + '/') 

        self.related = related or [] 

        self.actions = actions or [] 

 

    def __repr__(self): 

        return 'kind' 

 

 

class Action(Category): 

    ''' 

    OCCI Action. 

    ''' 

 

    def __init__(self, scheme, term, title='', attributes=None): 

        super(Action, self).__init__(scheme, term, title, attributes or {}, 

                                     location=None) 

 

    def __repr__(self): 

        return 'action' 

 

 

class Mixin(Category): 

    ''' 

    OCCI Mixin. 

    ''' 

 

    def __init__(self, scheme, term, related=None, actions=None, title='', 

                 attributes=None, location=None): 

        super(Mixin, self).__init__(scheme, term, title, attributes or {}, 

                                    location or '/' + term + '/') 

        self.related = related or [] 

        self.actions = actions or [] 

 

    def __repr__(self): 

        return 'mixin' 

 

 

#============================================================================== 

# Entities 

#============================================================================== 

 

 

class Entity(object): 

    ''' 

    OCCI Entity. 

    ''' 

 

    def __init__(self, identifier, title, kind, mixins): 

        self.identifier = identifier 

        self.title = title 

        self.kind = kind 

        self.mixins = mixins 

 

        # Attributes of resource entities 

        self.attributes = {} 

        self.actions = [] 

        self.extras = None 

 

 

class Resource(Entity): 

    ''' 

    OCCI Resource. 

    ''' 

 

    kind = Kind('http://schemas.ogf.org/occi/core#', 'resource') 

 

    def __init__(self, identifier, kind, mixins, links=None, summary=None, 

                 title=None): 

        super(Resource, self).__init__(identifier, title, kind, mixins) 

        self.links = links or [] 

        self.summary = summary 

 

 

class Link(Entity): 

    ''' 

    OCCI Link. 

    ''' 

 

    kind = Kind('http://schemas.ogf.org/occi/core#', 'link') 

 

    def __init__(self, identifier, kind, mixins, source, target, title=None): 

        super(Link, self).__init__(identifier, title, kind, mixins) 

        self.source = source 

        self.target = target