Statistics
| Branch: | Tag: | Revision:

root / ncclient / manager.py @ dd225c7a

History | View | Annotate | Download (3.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
"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

    
48
    def __enter__(self):
49
        return self
50

    
51
    def __exit__(self, *args):
52
        self.close()
53
        return False
54

    
55
    def __getattr__(self, name):
56
        try:
57
            return operations.INDEX[name](self.session).request
58
        except KeyError:
59
            raise AttributeError
60

    
61
    def locked(self, target):
62
        """Returns a context manager for the *with* statement.
63

64
        :arg target: name of the datastore to lock
65
        :type target: `string`
66
        :rtype: :class:`~ncclient.operations.LockContext`
67
        """
68
        return operations.LockContext(self._session, target)
69

    
70
    def close(self):
71
        """Closes the NETCONF session. First does *<close-session>* RPC."""
72
        try: # try doing it clean
73
            self.close_session()
74
        except Exception as e:
75
            logger.debug('error doing <close-session> -- %r' % e)
76
        if self._session.connected: # if that didn't work...
77
            self._session.close()
78

    
79
    @property
80
    def session(self):
81
        ":class:`~ncclient.transport.Session` instance"
82
        return self._session
83

    
84
    @property
85
    def client_capabilities(self):
86
        ":class:`~ncclient.capabilities.Capabilities` object for client"
87
        return self._session._client_capabilities
88

    
89
    @property
90
    def server_capabilities(self):
91
        ":class:`~ncclient.capabilities.Capabilities` object for server"
92
        return self._session._server_capabilities
93

    
94
    @property
95
    def session_id(self):
96
        "*<session-id>* as assigned by NETCONF server"
97
        return self._session.id
98

    
99
    @property
100
    def connected(self):
101
        "Whether currently connected to NETCONF server"
102
        return self._session.connected