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

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

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

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

# 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 

# 

''' 

Module which holds a abstract registry definition class and one simple 

implementation. 

 

Created on Aug 22, 2011 

 

@author: tmetsch 

''' 

 

# disabling 'Abstract class only ref. once' pylint check (designed for ext.) 

# disabling 'Unsued argument' pylint check (is there to be overwritten) 

# disabling 'Method could be function' pylint check (see above) 

# pylint: disable=R0922,W0613,R0201 

 

from occi.backend import KindBackend, ActionBackend, MixinBackend 

from occi.exceptions import HTTPError 

from occi.protocol.occi_rendering import Rendering 

 

 

class Registry(object): 

    ''' 

    Abstract class so users can implement registries themselves. 

    ''' 

 

    hostname = '' 

 

    default_mime_type = 'text/plain' 

 

    def get_hostname(self): 

        ''' 

        Returns the hostname of the service. 

        ''' 

        return self.hostname 

 

    def set_hostname(self, hostname): 

        ''' 

        Set the hostname of the service. 

        ''' 

        self.hostname = hostname 

 

    def get_default_type(self): 

        ''' 

        Returns the default mime type. 

        ''' 

        return self.default_mime_type 

 

    def get_renderer(self, mime_type): 

        ''' 

        Retrieve a rendering for a given mime type. 

 

        mime_type -- The mime type you a looking for. 

        ''' 

        raise NotImplementedError('Registry implementation seems to be' 

                                  ' incomplete.') 

 

    def set_renderer(self, mime_type, renderer): 

        ''' 

        Retrieve a rendering for a given mime type. 

 

        mime_type -- The mime type you want to add a rendering for. 

        renderer -- Instance of an Rendering class. 

        ''' 

        raise NotImplementedError('Registry implementation seems to be' 

                                  ' incomplete.') 

 

    def get_backend(self, category, extras): 

        """ 

        Retrieve a backend which is able to deal with the given category. 

 

        category -- The category a backend is needed for. 

        extras -- Extras object - same as the one passed on to the backends. 

        """ 

        raise NotImplementedError('Registry implementation seems to be' 

                                  ' incomplete.') 

 

    def get_all_backends(self, entity, extras): 

        """ 

        Retrieve all backends associated with a resource instance 

 

        entity -- The resource instance. 

        extras -- Extras object - same as the one passed on to the backends. 

        """ 

        raise NotImplementedError('Registry implementation seems to be' 

                                  ' incomplete.') 

 

    def set_backend(self, category, backend, extras): 

        """ 

        Set a backend which is able to deal with the given category. 

 

        category -- The category a backend is needed for. 

        backend -- The backend which should handle this category. 

        extras -- Extras object - same as the one passed on to the backends. 

        """ 

        raise NotImplementedError('Registry implementation seems to be' 

                                  ' incomplete.') 

 

    def delete_mixin(self, mixin, extras): 

        ''' 

        Remove a mixin from the service. 

 

        mixin -- The mixin 

        extras -- Extras object - same as the one passed on to the backends. 

        ''' 

        raise NotImplementedError('Registry implementation seems to be' 

                                  ' incomplete.') 

 

    def get_category(self, path, extras): 

        ''' 

        Return the category which is associated with an Location. 

 

        path -- The location which the category should define. 

        extras -- Extras object - same as the one passed on to the backends. 

        ''' 

        raise NotImplementedError('Registry implementation seems to be' 

                                  ' incomplete.') 

 

    def get_categories(self, extras): 

        ''' 

        Return all registered categories. 

 

        extras -- Extras object - same as the one passed on to the backends. 

        ''' 

        raise NotImplementedError('Registry implementation seems to be' 

                                  ' incomplete.') 

 

    def get_resource(self, key, extras): 

        ''' 

        Return a certain resource. 

 

        key -- Unique identifier of the resource. 

        extras -- Extras object - same as the one passed on to the backends. 

        ''' 

        raise NotImplementedError('Registry implementation seems to be' 

                                  ' incomplete.') 

 

    def add_resource(self, key, entity, extras): 

        ''' 

        Add a resource. 

 

        key -- the unique identifier. 

        entity -- the OCCI representation. 

        extras -- Extras object - same as the one passed on to the backends. 

        ''' 

        raise NotImplementedError('Registry implementation seems to be' 

                                  ' incomplete.') 

 

    def delete_resource(self, key, extras): 

        ''' 

        Delete a resource. 

 

        key -- Unique identifier of the resource. 

        extras -- Extras object - same as the one passed on to the backends. 

        ''' 

        raise NotImplementedError('Registry implementation seems to be' 

                                  ' incomplete.') 

 

    def get_resource_keys(self, extras): 

        ''' 

        Return all keys of all resources. 

 

        extras -- Extras object - same as the one passed on to the backends. 

        ''' 

        raise NotImplementedError('Registry implementation seems to be' 

                                  ' incomplete.') 

 

    def get_resources(self, extras): 

        ''' 

        Return all resources. 

 

        extras -- Extras object - same as the one passed on to the backends. 

        ''' 

        raise NotImplementedError('Registry implementation seems to be' 

                                  ' incomplete.') 

 

    def get_extras(self, extras): 

        ''' 

        Will return what goes into the extras attribute of the entity and 

        category. 

        ''' 

        return None 

 

 

class NonePersistentRegistry(Registry): 

    ''' 

    None optimized/persistent registry for the OCCI service. 

 

    It is encouraged to write an own registry with e.g. DB lookups. 

 

    Created on Jun 28, 2011 

 

    @author: tmetsch 

    ''' 

 

    def __init__(self): 

        self.backends = {} 

        self.renderings = {} 

        self.resources = {} 

        self.host = '' 

        super(NonePersistentRegistry, self).__init__() 

 

    def get_renderer(self, mime_type): 

        parser = None 

 

        for tmp in mime_type.split(','): 

            type_str = tmp.strip() 

            if type_str.find(';q=') > -1: 

                type_str = type_str[:type_str.find(';q=')] 

 

            if type_str in self.renderings: 

                parser = self.renderings[type_str] 

                break 

            elif type_str == '*/*': 

                parser = self.renderings[self.get_default_type()] 

                break 

 

        if parser is None: 

            raise HTTPError(406, 'This service is unable to understand the ' + 

                            ' mime type: ' + repr(mime_type)) 

        else: 

            return parser 

 

    def set_renderer(self, mime_type, renderer): 

        if not isinstance(renderer, Rendering): 

            raise AttributeError('renderer needs to derive from Rendering.') 

        self.renderings[mime_type] = renderer 

 

    def get_backend(self, category, extras): 

        # no need to check - a get_categories or get_categroy will be called 

        # first. 

        try: 

            back = self.backends[category] 

            if repr(category) == 'kind' and isinstance(back, KindBackend): 

                return back 

            if repr(category) == 'action' and isinstance(back, 

                                                       ActionBackend): 

                return back 

            if repr(category) == 'mixin' and isinstance(back, MixinBackend): 

                return back 

        except KeyError: 

            raise AttributeError('Cannot find corresponding Backend.') 

 

    def get_all_backends(self, entity, extras): 

        res = [self.get_backend(entity.kind, extras)] 

        for mixin in entity.mixins: 

            res.append(self.get_backend(mixin, extras)) 

        # remove duplicates - only need to call backs once - right? 

        return list(set(res)) 

 

    def set_backend(self, category, backend, extras): 

        if extras is not None: 

            # category belongs to single user... 

            category.extras = self.get_extras(extras) 

        self.backends[category] = backend 

 

    def delete_mixin(self, mixin, extras): 

        # no need to check because in get_category in renderer it is assured 

        # that the user only sees own. Will get not found if he tries to delete 

        # mixin from other user. 

        self.backends.pop(mixin) 

 

    def get_category(self, path, extras): 

        # no need for ownership check - paths cannot overlap! 

        for category in self.backends.keys(): 

            if category.location == path: 

                return category 

        return None 

 

    def get_categories(self, extras): 

        result = [] 

        for item in self.backends.keys(): 

            if item.extras is None: 

                # categories visible to all! 

                result.append(item) 

            elif extras is not None and self.get_extras(extras) == item.extras: 

                # categories visible to this user! 

                result.append(item) 

        return result 

 

    def get_resource(self, key, extras): 

        if self.resources[key].extras is not None and self.resources[key]\ 

                                            .extras != self.get_extras(extras): 

            raise KeyError 

        return self.resources[key] 

 

    def add_resource(self, key, resource, extras): 

        if extras is not None: 

            resource.extras = self.get_extras(extras) 

        self.resources[key] = resource 

 

    def delete_resource(self, key, extras): 

        # get_resources and get_resource is called before this - no need for 

        # ownership checking. 

        self.resources.pop(key) 

 

    def get_resource_keys(self, extras): 

        result = [] 

        for key in self.resources.keys(): 

            if self.resources[key].extras is None: 

                result.append(key) 

            elif self.resources[key].extras is not None and \ 

                 self.resources[key].extras == self.get_extras(extras): 

                result.append(key) 

        return result 

 

    def get_resources(self, extras): 

        result = [] 

        for item in self.resources.values(): 

            if item.extras is None: 

                result.append(item) 

            elif item.extras is not None and \ 

                 item.extras == self.get_extras(extras): 

                result.append(item) 

        return result