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')
25 #: :class:`Capabilities` object representing the capabilities currently supported by NCClient
26 CAPABILITIES = capabilities.Capabilities([
27 "urn:ietf:params:netconf:base:1.0",
28 "urn:ietf:params:netconf:capability:writable-running:1.0",
29 "urn:ietf:params:netconf:capability:candidate:1.0",
30 "urn:ietf:params:netconf:capability:confirmed-commit:1.0",
31 "urn:ietf:params:netconf:capability:rollback-on-error:1.0",
32 "urn:ietf:params:netconf:capability:startup:1.0",
33 "urn:ietf:params:netconf:capability:url:1.0?scheme=http,ftp,file,https,sftp",
34 "urn:ietf:params:netconf:capability:validate:1.0",
35 "urn:ietf:params:netconf:capability:xpath:1.0",
36 "urn:liberouter:params:netconf:capability:power-control:1.0"
37 "urn:ietf:params:netconf:capability:interleave:1.0"
38 #'urn:ietf:params:netconf:capability:notification:1.0', # TODO
43 "get": operations.Get,
44 "get_config": operations.GetConfig,
45 "edit_config": operations.EditConfig,
46 "copy_config": operations.CopyConfig,
47 "validate": operations.Validate,
48 "commit": operations.Commit,
49 "discard_changes": operations.DiscardChanges,
50 "delete_config": operations.DeleteConfig,
51 "lock": operations.Lock,
52 "unlock": operations.Unlock,
53 "close_session": operations.CloseSession,
54 "kill_session": operations.KillSession,
55 "poweroff_machine": operations.PoweroffMachine,
56 "reboot_machine": operations.RebootMachine
60 def connect_ssh(*args, **kwds):
61 session = transport.SSHSession(CAPABILITIES)
62 session.load_known_hosts()
63 session.connect(*args, **kwds)
64 return Manager(session)
66 #: Same as :meth:`connect_ssh`
70 class Manager(object):
72 def __init__(self, session):
73 self._session = session
74 self._async_mode = False
76 self._raise_mode = 'all'
81 def __exit__(self, *argss):
85 def __getattr__(self, name):
86 op = OPERATIONS.get(name, None)
90 return op(self._session,
91 async=self._async_mode,
92 timeout=self._timeout,
93 raise_mode=self._raise_mode).request
95 def locked(self, target):
96 return operations.LockContext(self._session, target)
99 def client_capabilities(self):
100 return self._session._client_capabilities
103 def server_capabilities(self):
104 return self._session._server_capabilities
107 def session_id(self):
108 return self._session.id
112 return self._session.connected
114 def set_async_mode(self, mode):
115 self._async_mode = mode
117 def set_raise_mode(self, mode):
118 assert(choice in ("all", "errors", "none"))
119 self._raise_mode = mode
121 async_mode = property(fget=lambda self: self._async_mode, fset=set_async_mode)
123 raise_mode = property(fget=lambda self: self._raise_mode, fset=set_raise_mode)