Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

# Copyright 2014 - Rackspace Hosting 

# 

#    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. 

 

"""Common RPC service and API tools for Magnum.""" 

 

import eventlet 

from oslo_config import cfg 

import oslo_messaging as messaging 

 

from magnum.common import context as magnum_context 

from magnum.common import rpc 

from magnum.objects import base as objects_base 

 

 

# NOTE(paulczar): 

# Ubuntu 14.04 forces librabbitmq when kombu is used 

# Unfortunately it forces a version that has a crash 

# bug.  Calling eventlet.monkey_patch() tells kombu 

# to use libamqp instead. 

eventlet.monkey_patch() 

 

# NOTE(asalkeld): 

# The magnum.openstack.common.rpc entries are for compatibility 

# with devstack rpc_backend configuration values. 

TRANSPORT_ALIASES = { 

    'magnum.openstack.common.rpc.impl_kombu': 'rabbit', 

    'magnum.openstack.common.rpc.impl_qpid': 'qpid', 

    'magnum.openstack.common.rpc.impl_zmq': 'zmq', 

} 

 

 

class RequestContextSerializer(messaging.Serializer): 

 

    def __init__(self, base): 

        self._base = base 

 

    def serialize_entity(self, context, entity): 

        if not self._base: 

            return entity 

        return self._base.serialize_entity(context, entity) 

 

    def deserialize_entity(self, context, entity): 

        if not self._base: 

            return entity 

        return self._base.deserialize_entity(context, entity) 

 

    def serialize_context(self, context): 

        return context.to_dict() 

 

    def deserialize_context(self, context): 

        return magnum_context.RequestContext.from_dict(context) 

 

 

class Service(object): 

    _server = None 

 

    def __init__(self, topic, server, handlers): 

        serializer = RequestContextSerializer( 

            objects_base.MagnumObjectSerializer()) 

        transport = messaging.get_transport(cfg.CONF, 

                                            aliases=TRANSPORT_ALIASES) 

        # TODO(asalkeld) add support for version='x.y' 

        target = messaging.Target(topic=topic, server=server) 

        self._server = messaging.get_rpc_server(transport, target, handlers, 

                                                serializer=serializer) 

 

    def serve(self): 

        self._server.start() 

        self._server.wait() 

 

 

class API(object): 

    def __init__(self, transport=None, context=None, topic=None, server=None, 

                 timeout=None): 

        serializer = RequestContextSerializer( 

            objects_base.MagnumObjectSerializer()) 

93        if transport is None: 

            exmods = rpc.get_allowed_exmods() 

            transport = messaging.get_transport(cfg.CONF, 

                                                allowed_remote_exmods=exmods, 

                                                aliases=TRANSPORT_ALIASES) 

        self._context = context 

95        if topic is None: 

            topic = '' 

        target = messaging.Target(topic=topic, server=server) 

        self._client = messaging.RPCClient(transport, target, 

                                           serializer=serializer, 

                                           timeout=timeout) 

 

    def _call(self, method, *args, **kwargs): 

        return self._client.call(self._context, method, *args, **kwargs) 

 

    def _cast(self, method, *args, **kwargs): 

        self._client.cast(self._context, method, *args, **kwargs) 

 

    def echo(self, message): 

        self._cast('echo', message=message)