Statistics
| Branch: | Tag: | Revision:

root / ncclient / manager.py @ 0b7d3b31

History | View | Annotate | Download (5.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
def connect_ssh(*args, **kwds):
22
    """Connect to NETCONF server over SSH. See :meth:`SSHSession.connect()
23
    <ncclient.transport.SSHSession.connect>` for function signature."""
24
    session = transport.SSHSession(capabilities.CAPABILITIES)
25
    session.load_known_hosts()
26
    session.connect(*args, **kwds)
27
    return Manager(session)
28

    
29
#: Same as :meth:`connect_ssh`
30
connect = connect_ssh
31

    
32
#: Raise all :class:`~ncclient.operations.rpc.RPCError`
33
RAISE_ALL = 0
34
#: Only raise when *error-severity* is "error" i.e. no warnings
35
RAISE_ERR = 1
36
#: Don't raise any
37
RAISE_NONE = 2
38

    
39
class Manager:
40

    
41
    """API for NETCONF operations. Currently only supports making synchronous
42
    RPC requests.
43

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

    
47
    def __init__(self, session):
48
        self._session = session
49
        self._raise = RAISE_ALL
50

    
51
    def set_rpc_error_action(self, action):
52
        """Specify the action to take when an *<rpc-error>* element is encountered.
53

54
        :arg action: one of :attr:`RAISE_ALL`, :attr:`RAISE_ERR`, :attr:`RAISE_NONE`
55
        """
56
        self._raise = action
57

    
58
    #def __enter__(self):
59
    #    return self
60
    #
61
    #def __exit__(self, *args):
62
    #    self.close()
63
    #    return False
64

    
65
    def do(self, op, *args, **kwds):
66
        op = operations.OPERATIONS[op](self._session)
67
        reply = op.request(*args, **kwds)
68
        if not reply.ok:
69
            if self._raise == RAISE_ALL:
70
                raise reply.error
71
            elif self._raise == RAISE_ERROR:
72
                for error in reply.errors:
73
                    if error.severity == 'error':
74
                        raise error
75
        return reply
76

    
77
    #: :see: :meth:`Get.request() <ncclient.operations.Get.request>`
78
    get = lambda self, *args, **kwds: self.do('get', *args, **kwds)
79

    
80
    #: :see: :meth:`GetConfig.request() <ncclient.operations.GetConfig.request>`
81
    get_config = lambda self, *args, **kwds: self.do('get-config', *args, **kwds)
82

    
83
    #: :see: :meth:`EditConfig.request() <ncclient.operations.EditConfig.request>`
84
    edit_config = lambda self, *args, **kwds: self.do('edit-config', *args, **kwds)
85

    
86
    #: :see: :meth:`CopyConfig.request() <ncclient.operations.CopyConfig.request>`
87
    copy_config = lambda self, *args, **kwds: self.do('copy-config', *args, **kwds)
88

    
89
    #: :see: :meth:`GetConfig.request() <ncclient.operations.Validate.request>`
90
    validate = lambda self, *args, **kwds: self.do('validate', *args, **kwds)
91

    
92
    #: :see: :meth:`Commit.request() <ncclient.operations.Commit.request>`
93
    commit = lambda self, *args, **kwds: self.do('commit', *args, **kwds)
94

    
95
    #: :see: :meth:`DiscardChanges.request() <ncclient.operations.DiscardChanges.request>`
96
    discard_changes = lambda self, *args, **kwds: self.do('discard-changes', *args, **kwds)
97

    
98
    #: :see: :meth:`DeleteConfig.request() <ncclient.operations.DeleteConfig.request>`
99
    delete_config = lambda self, *args, **kwds: self.do('delete-config', *args, **kwds)
100

    
101
    #: :see: :meth:`Lock.request() <ncclient.operations.Lock.request>`
102
    lock = lambda self, *args, **kwds: self.do('lock', *args, **kwds)
103

    
104
    #: :see: :meth:`DiscardChanges.request() <ncclient.operations.Unlock.request>`
105
    unlock = lambda self, *args, **kwds: self.do('unlock', *args, **kwds)
106

    
107
    #: :see: :meth:`CloseSession.request() <ncclient.operations.CloseSession.request>`
108
    close_session = lambda self, *args, **kwds: self.do('close-session', *args, **kwds)
109

    
110
    #: :see: :meth:`KillSession.request() <ncclient.operations.KillSession.request>`
111
    kill_session = lambda self, *args, **kwds: self.do('kill-session', *args, **kwds)
112

    
113
    def locked(self, target):
114
        """Returns a context manager for the *with* statement.
115

116
        :arg target: name of the datastore to lock
117
        :type target: `string`
118
        :rtype: :class:`~ncclient.operations.LockContext`
119
        """
120
        return operations.LockContext(self._session, target)
121

    
122
    def close(self):
123
        """Closes the NETCONF session. First does *<close-session>* RPC."""
124
        try: # try doing it clean
125
            self.close_session()
126
        except Exception as e:
127
            logger.debug('error doing <close-session> -- %r' % e)
128
        if self._session.connected: # if that didn't work...
129
            self._session.close()
130

    
131
    @property
132
    def session(self, session):
133
        ":class:`~ncclient.transport.Session` instance"
134
        return self._session
135

    
136
    @property
137
    def client_capabilities(self):
138
        ":class:`~ncclient.capabilities.Capabilities` object for client"
139
        return self._session._client_capabilities
140

    
141
    @property
142
    def server_capabilities(self):
143
        ":class:`~ncclient.capabilities.Capabilities` object for server"
144
        return self._session._server_capabilities
145

    
146
    @property
147
    def session_id(self):
148
        "*<session-id>* as assigned by NETCONF server"
149
        return self._session.id
150

    
151
    @property
152
    def connected(self):
153
        "Whether currently connected to NETCONF server"
154
        return self._session.connected