summaryrefslogtreecommitdiff
path: root/designate/mdns/rpcapi.py
blob: e3956af795844e6b6cd6145777d64a6d7f753e0c (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
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
# Copyright (c) 2014 Rackspace Hosting
# All Rights Reserved.
#
#    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.
from oslo_config import cfg
from oslo_log import log as logging
import oslo_messaging as messaging

from designate import rpc
from designate.loggingutils import rpc_logging

CONF = cfg.CONF
LOG = logging.getLogger(__name__)

MDNS_API = None


def reset():
    global MDNS_API
    MDNS_API = None


@rpc_logging(LOG, 'mdns')
class MdnsAPI(object):

    """
    Client side of the mdns RPC API.

    Notify API version history:

        1.0 - Added notify_zone_changed and poll_for_serial_number.
        1.1 - Added get_serial_number.
        2.0 - Changed method signatures

    XFR API version history:
        1.0 - Added perform_zone_xfr.
    """
    RPC_NOTIFY_API_VERSION = '2.0'
    RPC_XFR_API_VERSION = '1.0'

    def __init__(self, topic=None):
        self.topic = topic if topic else cfg.CONF['service:mdns'].topic

        notify_target = messaging.Target(topic=self.topic,
                                         namespace='notify',
                                         version=self.RPC_NOTIFY_API_VERSION)
        self.notify_client = rpc.get_client(notify_target, version_cap='2.0')

        xfr_target = messaging.Target(topic=self.topic,
                                      namespace='xfr',
                                      version=self.RPC_XFR_API_VERSION)
        self.xfr_client = rpc.get_client(xfr_target, version_cap='1.0')

    @classmethod
    def get_instance(cls):
        """
        The rpc.get_client() which is called upon the API object initialization
        will cause a assertion error if the designate.rpc.TRANSPORT isn't setup
        by rpc.init() before.

        This fixes that by creating the rpcapi when demanded.
        """
        global MDNS_API
        if not MDNS_API:
            MDNS_API = cls()
        return MDNS_API

    def notify_zone_changed(self, context, zone, host, port, timeout,
                            retry_interval, max_retries, delay):
        if CONF['service:worker'].notify and CONF['service:worker'].enabled:
            LOG.debug('Letting worker send NOTIFYs instead')
            return True

        LOG.info(
            "notify_zone_changed: Calling mdns for zone '%(zone)s', "
            "serial '%(serial)s' to nameserver '%(host)s:%(port)s'",
            {
                'zone': zone.name,
                'serial': zone.serial,
                'host': host,
                'port': port
            })
        # The notify_zone_changed method is a cast rather than a call since the
        # caller need not wait for the notify to complete.
        return self.notify_client.cast(
            context, 'notify_zone_changed', zone=zone,
            host=host, port=port, timeout=timeout,
            retry_interval=retry_interval, max_retries=max_retries,
            delay=delay)

    def poll_for_serial_number(self, context, zone, nameserver, timeout,
                               retry_interval, max_retries, delay):
        LOG.info(
            "poll_for_serial_number: Calling mdns for zone '%(zone)s', "
            "serial '%(serial)s' on nameserver '%(host)s:%(port)s'",
            {
                'zone': zone.name,
                'serial': zone.serial,
                'host': nameserver.host,
                'port': nameserver.port
            })
        # The poll_for_serial_number method is a cast rather than a call since
        # the caller need not wait for the poll to complete. Mdns informs pool
        # manager of the return value using update_status
        return self.notify_client.cast(
            context, 'poll_for_serial_number', zone=zone,
            nameserver=nameserver, timeout=timeout,
            retry_interval=retry_interval, max_retries=max_retries,
            delay=delay)

    def get_serial_number(self, context, zone, host, port, timeout,
                          retry_interval, max_retries, delay):
        LOG.info(
            "get_serial_number: Calling mdns for zone '%(zone)s', serial "
            "%(serial)s' on nameserver '%(host)s:%(port)s'",
            {
                'zone': zone.name,
                'serial': zone.serial,
                'host': host,
                'port': port
            })
        cctxt = self.notify_client.prepare()
        return cctxt.call(
            context, 'get_serial_number', zone=zone,
            host=host, port=port, timeout=timeout,
            retry_interval=retry_interval, max_retries=max_retries,
            delay=delay)

    def perform_zone_xfr(self, context, zone):
        LOG.info("perform_zone_xfr: Calling mdns for zone %(zone)s",
                 {"zone": zone.name})
        return self.xfr_client.cast(context, 'perform_zone_xfr', zone=zone)