Statistics
| Branch: | Tag: | Revision:

root / ncclient / rpc.py @ 2acc860a

History | View | Annotate | Download (2.1 kB)

1
# Copyright 2009 Shikhar Bhushan
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
#    http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14

    
15
from threading import Event, Lock
16
from uuid import uuid1
17

    
18
import content
19
from listeners import session_listener_factory
20

    
21
class RPC:
22
    
23
    metadata = {
24
        'tag': 'rpc',
25
        'xmlns': 'urn:ietf:params:xml:ns:netconf:base:1.0',
26
        }
27
    
28
    def __init__(self, session, async=False, parse=True):
29
        self._session = session
30
        self._async = async
31
        self._id = uuid1().urn
32
        listener = session_listener_factory(self._session)
33
        listener.register(self._id, self)
34
        session.add_listener(listener)
35
        self._reply = None
36
        self._reply_event = Event()
37

    
38
    def _response_cb(self, reply):
39
        self._reply = reply
40
        self._event.set()
41
    
42
    def _do_request(self, op):
43
        self._session.send(content.make_rpc(self._id, op))
44
        # content.make(RPC, attrs={'message-id': self._id}, children=(op,))
45
        if not self._async:
46
            self._reply_event.wait()
47
        return self._reply
48
    
49
    def request(self):
50
        raise NotImplementedError
51
    
52
    def wait_for_reply(self, timeout=None):
53
        self._reply_event.wait(timeout)
54
    
55
    @property
56
    def has_reply(self):
57
        return self._reply_event.isSet()
58
    
59
    @property
60
    def is_async(self):
61
        return self._async
62
    
63
    @property
64
    def reply(self):
65
        return self._reply
66
    
67
    @property
68
    def id(self):
69
        return self._id
70
    
71
    @property
72
    def session(self):
73
        return self._session
74

    
75
class RPCReply:
76
    
77
    class RPCError:
78
        
79
        pass