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 function signature."""
27 session = transport.SSHSession(capabilities.CAPABILITIES)
28 session.load_known_hosts()
29 session.connect(*args, **kwds)
30 return Manager(session)
32 #: Same as :meth:`connect_ssh`
35 #: Raise all :class:`~ncclient.operations.rpc.RPCError`
37 #: Only raise when *error-severity* is "error" i.e. no warnings
44 """API for NETCONF operations. Currently only supports making synchronous
47 It is also a context manager, so a :class:`Manager` instance can be used
48 with the *with* statement. The session is closed when the context ends. """
50 def __init__(self, session):
51 self._session = session
52 self._raise = RAISE_ALL
54 def set_rpc_error_action(self, action):
55 """Specify the action to take when an *<rpc-error>* element is encountered.
57 :arg action: one of :attr:`RAISE_ALL`, :attr:`RAISE_ERR`, :attr:`RAISE_NONE`
64 def __exit__(self, *args):
68 def do(self, op, *args, **kwds):
69 op = operations.OPERATIONS[op](self._session)
70 reply = op.request(*args, **kwds)
72 if self._raise == RAISE_ALL:
74 elif self._raise == RAISE_ERROR:
75 for error in reply.errors:
76 if error.severity == 'error':
80 #: :see: :meth:`Get.request() <ncclient.operations.Get.request>`
81 get = lambda self, *args, **kwds: self.do('get', *args, **kwds)
83 #: :see: :meth:`GetConfig.request() <ncclient.operations.GetConfig.request>`
84 get_config = lambda self, *args, **kwds: self.do('get-config', *args, **kwds)
86 #: :see: :meth:`EditConfig.request() <ncclient.operations.EditConfig.request>`
87 edit_config = lambda self, *args, **kwds: self.do('edit-config', *args, **kwds)
89 #: :see: :meth:`CopyConfig.request() <ncclient.operations.CopyConfig.request>`
90 copy_config = lambda self, *args, **kwds: self.do('copy-config', *args, **kwds)
92 #: :see: :meth:`GetConfig.request() <ncclient.operations.Validate.request>`
93 validate = lambda self, *args, **kwds: self.do('validate', *args, **kwds)
95 #: :see: :meth:`Commit.request() <ncclient.operations.Commit.request>`
96 commit = lambda self, *args, **kwds: self.do('commit', *args, **kwds)
98 #: :see: :meth:`DiscardChanges.request() <ncclient.operations.DiscardChanges.request>`
99 discard_changes = lambda self, *args, **kwds: self.do('discard-changes', *args, **kwds)
101 #: :see: :meth:`DeleteConfig.request() <ncclient.operations.DeleteConfig.request>`
102 delete_config = lambda self, *args, **kwds: self.do('delete-config', *args, **kwds)
104 #: :see: :meth:`Lock.request() <ncclient.operations.Lock.request>`
105 lock = lambda self, *args, **kwds: self.do('lock', *args, **kwds)
107 #: :see: :meth:`DiscardChanges.request() <ncclient.operations.Unlock.request>`
108 unlock = lambda self, *args, **kwds: self.do('unlock', *args, **kwds)
110 #: :see: :meth:`CloseSession.request() <ncclient.operations.CloseSession.request>`
111 close_session = lambda self, *args, **kwds: self.do('close-session', *args, **kwds)
113 #: :see: :meth:`KillSession.request() <ncclient.operations.KillSession.request>`
114 kill_session = lambda self, *args, **kwds: self.do('kill-session', *args, **kwds)
116 def locked(self, target):
117 """Returns a context manager for the *with* statement.
119 :arg target: name of the datastore to lock
120 :type target: `string`
121 :rtype: :class:`~ncclient.operations.LockContext`
123 return operations.LockContext(self._session, target)
126 """Closes the NETCONF session. First does *<close-session>* RPC."""
127 try: # try doing it clean
129 except Exception as e:
130 logger.debug('error doing <close-session> -- %r' % e)
131 if self._session.connected: # if that didn't work...
132 self._session.close()
135 def session(self, session):
136 ":class:`~ncclient.transport.Session` instance"
140 def client_capabilities(self):
141 ":class:`~ncclient.capabilities.Capabilities` object for client"
142 return self._session._client_capabilities
145 def server_capabilities(self):
146 ":class:`~ncclient.capabilities.Capabilities` object for server"
147 return self._session._server_capabilities
150 def session_id(self):
151 "*<session-id>* as assigned by NETCONF server"
152 return self._session.id
156 "Whether currently connected to NETCONF server"
157 return self._session.connected