Statistics
| Branch: | Tag: | Revision:

root / ncclient / manager.py @ 0a718026

History | View | Annotate | Download (3.7 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
"Thin layer of abstraction around NCClient"
16

    
17
import capabilities
18
import operations
19
import transport
20

    
21
import logging
22
logger = logging.getLogger('ncclient.manager')
23

    
24
def connect_ssh(*args, **kwds):
25
    """Connect to NETCONF server over SSH. See :meth:`SSHSession.connect()
26
    <ncclient.transport.SSHSession.connect>` for argument details.
27

28
    :rtype: :class:`Manager`
29
    """
30
    session = transport.SSHSession(capabilities.CAPABILITIES)
31
    session.load_known_hosts()
32
    session.connect(*args, **kwds)
33
    return Manager(session)
34

    
35
#: Same as :meth:`connect_ssh`
36
connect = connect_ssh
37

    
38
class Manager(object):
39

    
40
    """API for NETCONF operations.
41

42
    It is also a context manager, so a :class:`Manager` instance can be used
43
    with the *with* statement. The session is closed when the context ends. """
44

    
45
    def __init__(self, session):
46
        self._session = session
47
        self._async_mode = False
48
        self._timeout = None
49
        self._raise_mode = 'all'
50

    
51
    def __enter__(self):
52
        return self
53

    
54
    def __exit__(self, *argss):
55
        self.close()
56
        return False
57

    
58
    def __getattr__(self, name):
59
        try:
60
            op = operations.INDEX[name]
61
        except KeyError:
62
            raise AttributeError
63
        else:
64
            reply = op(self.session,
65
                      async=self._async_mode,
66
                      timeout=self.timeout,
67
                      raise_mode=self._raise_mode).request
68
            return op if self._async_mode else reply
69

    
70
    def locked(self, target):
71
        """Returns a context manager for the *with* statement.
72

73
        :arg target: name of the datastore to lock
74
        :type target: `string`
75
        :rtype: :class:`~ncclient.operations.LockContext`
76
        """
77
        return operations.LockContext(self._session, target)
78

    
79
    def close(self):
80
        """Closes the NETCONF session. First does *<close-session>* RPC."""
81
        try: # try doing it clean
82
            self.close_session()
83
        except Exception as e:
84
            logger.debug('error doing <close-session> -- %r' % e)
85
        if self._session.connected: # if that didn't work...
86
            self._session.close()
87

    
88
    @property
89
    def session(self):
90
        ":class:`~ncclient.transport.Session` instance"
91
        return self._session
92

    
93
    @property
94
    def client_capabilities(self):
95
        ":class:`~ncclient.capabilities.Capabilities` object for client"
96
        return self._session._client_capabilities
97

    
98
    @property
99
    def server_capabilities(self):
100
        ":class:`~ncclient.capabilities.Capabilities` object for server"
101
        return self._session._server_capabilities
102

    
103
    @property
104
    def session_id(self):
105
        "*<session-id>* as assigned by NETCONF server"
106
        return self._session.id
107

    
108
    @property
109
    def connected(self):
110
        "Whether currently connected to NETCONF server"
111
        return self._session.connected
112

    
113
    def set_async_mode(self, bool=True):
114
        self._async_mode = bool
115

    
116
    def set_raise_mode(self, mode):
117
        assert(choice in ('all', 'errors', 'none'))
118
        self._raise_mode = mode
119

    
120
    async_mode = property(fget=lambda self: self._async_mode, fset=set_async_mode)
121

    
122
    raise_mode = property(fget=set_raise_mode, fset=set_raise_mode)