1 # Copyright 2009 Shikhar Bhushan
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 "Thin layer of abstraction around NCClient"
22 logger = logging.getLogger('ncclient.manager')
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.
28 :rtype: :class:`Manager`
30 session = transport.SSHSession(capabilities.CAPABILITIES)
31 session.load_known_hosts()
32 session.connect(*args, **kwds)
33 return Manager(session)
35 #: Same as :meth:`connect_ssh`
38 class Manager(object):
40 """API for NETCONF operations.
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. """
45 def __init__(self, session):
46 self._session = session
47 self._async_mode = False
49 self._raise_mode = 'all'
54 def __exit__(self, *argss):
58 def __getattr__(self, name):
59 op = operations.INDEX.get(name, None)
63 return op(self.session,
64 async=self._async_mode,
65 timeout=self._timeout,
66 raise_mode=self._raise_mode).request
68 def locked(self, target):
69 """Returns a context manager for the *with* statement.
71 :arg target: name of the datastore to lock
72 :type target: `string`
73 :rtype: :class:`~ncclient.operations.LockContext`
75 return operations.LockContext(self._session, target)
78 """Closes the NETCONF session. First does *<close-session>* RPC."""
79 try: # try doing it clean
80 self._async_mode = False
82 except Exception as e:
83 logger.debug('error doing <close-session> -- %r' % e)
84 if self._session.connected: # if that didn't work, this sure will :)
89 ":class:`~ncclient.transport.Session` instance"
93 def client_capabilities(self):
94 ":class:`~ncclient.capabilities.Capabilities` object for client"
95 return self._session._client_capabilities
98 def server_capabilities(self):
99 ":class:`~ncclient.capabilities.Capabilities` object for server"
100 return self._session._server_capabilities
103 def session_id(self):
104 "*<session-id>* as assigned by NETCONF server"
105 return self._session.id
109 "Whether currently connected to NETCONF server"
110 return self._session.connected
112 def set_async_mode(self, bool=True):
113 self._async_mode = bool
115 def set_raise_mode(self, mode):
116 assert(choice in ('all', 'errors', 'none'))
117 self._raise_mode = mode
119 async_mode = property(fget=lambda self: self._async_mode, fset=set_async_mode)
121 raise_mode = property(fget=set_raise_mode, fset=set_raise_mode)