2163df4b8cd16a4831022be7d171266af862a698
[ncclient] / ncclient / manager.py
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
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    
39 ])
40
41
42 OPERATIONS = {
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
57 }
58
59
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)
65
66 #: Same as :meth:`connect_ssh`
67 connect = connect_ssh
68
69
70 class Manager(object):
71
72     def __init__(self, session):
73         self._session = session
74         self._async_mode = False
75         self._timeout = None
76         self._raise_mode = 'all'
77
78     def __enter__(self):
79         return self
80
81     def __exit__(self, *argss):
82         self.close_session()
83         return False
84
85     def __getattr__(self, name):
86         op = OPERATIONS.get(name, None)
87         if op is None:
88             raise AttributeError
89         else:
90             return op(self.session,
91                       async=self._async_mode,
92                       timeout=self._timeout,
93                       raise_mode=self._raise_mode).request
94     
95     def locked(self, target):
96         return operations.LockContext(self._session, target)
97     
98     @property
99     def client_capabilities(self):
100         return self._session._client_capabilities
101
102     @property
103     def server_capabilities(self):
104         return self._session._server_capabilities
105
106     @property
107     def session_id(self):
108         return self._session.id
109
110     @property
111     def connected(self):
112         return self._session.connected
113
114     def set_async_mode(self, mode):
115         self._async_mode = mode
116
117     def set_raise_mode(self, mode):
118         assert(choice in ("all", "errors", "none"))
119         self._raise_mode = mode
120
121     async_mode = property(fget=lambda self: self._async_mode, fset=set_async_mode)
122
123     raise_mode = property(fget=lambda self: self._raise_mode, fset=set_raise_mode)