Debian Codegraph @

ncclient/operations/third_party/juniper/rpc.py

raw
1 from ncclient.xml_ import *
2
3 from ncclient.operations.rpc import RPC
4 from ncclient.operations.rpc import RPCReply
5 from ncclient.operations.rpc import RPCError
6 from ncclient import NCClientError
7 import math
8
9
10 class GetConfiguration(RPC):
11 def request(self, format='xml', filter=None):
12 node = new_ele('get-configuration', {'format':format})
13 if filter is not None:
14 node.append(filter)
15 if format !='xml':
16 # The entire config comes as a single text element and this requires huge_tree support for large configs
17 self._huge_tree = True
18 return self._request(node)
19
20 class LoadConfiguration(RPC):
21 def request(self, format='xml', action='merge',
22 target='candidate', config=None):
23 if config is not None:
24 if type(config) == list:
25 config = '\n'.join(config)
26 if action == 'set':
27 format = 'text'
28 node = new_ele('load-configuration', {'action':action, 'format':format})
29 if format == 'xml':
30 config_node = sub_ele(node, 'configuration')
31 config_node.append(config)
32 if format == 'json':
33 config_node = sub_ele(node, 'configuration-json').text = config
34 if format == 'text' and not action == 'set':
35 config_node = sub_ele(node, 'configuration-text').text = config
36 if action == 'set' and format == 'text':
37 config_node = sub_ele(node, 'configuration-set').text = config
38 return self._request(node)
39
40
41 class CompareConfiguration(RPC):
42 def request(self, rollback=0, format='text'):
43 node = new_ele('get-configuration', {'compare':'rollback', 'format':format, 'rollback':str(rollback)})
44 return self._request(node)
45
46
47 class ExecuteRpc(RPC):
48 def request(self, rpc, filter_xml=None):
49 if isinstance(rpc, str):
50 rpc = to_ele(rpc)
51 self._filter_xml = filter_xml
52 return self._request(rpc)
53
54
55 class Command(RPC):
56 def request(self, command=None, format='xml'):
57 node = new_ele('command', {'format':format})
58 node.text = command
59 return self._request(node)
60
61
62 class Reboot(RPC):
63 def request(self):
64 node = new_ele('request-reboot')
65 return self._request(node)
66
67
68 class Halt(RPC):
69 def request(self):
70 node = new_ele('request-halt')
71 return self._request(node)
72
73
74 class Commit(RPC):
75 "`commit` RPC. Depends on the `:candidate` capability, and the `:confirmed-commit`."
76
77 DEPENDS = [':candidate']
78
79 def request(self, confirmed=False, timeout=None, comment=None, synchronize=False, at_time=None, check=False):
80 """Commit the candidate configuration as the device's new current configuration. Depends on the `:candidate` capability.
81
82 A confirmed commit (i.e. if *confirmed* is `True`) is reverted if there is no followup commit within the *timeout* interval. If no timeout is specified the confirm timeout defaults to 600 seconds (10 minutes). A confirming commit may have the *confirmed* parameter but this is not required. Depends on the `:confirmed-commit` capability.
83
84 *confirmed* whether this is a confirmed commit. Mutually exclusive with at_time.
85
86 *timeout* specifies the confirm timeout in seconds
87
88 *comment* a string to comment the commit with. Review on the device using 'show system commit'
89
90 *synchronize* Whether we should synch this commit across both Routing Engines
91
92 *at_time* Mutually exclusive with confirmed. The time at which the commit should happen. Junos expects either of these two formats:
93 A time value of the form hh:mm[:ss] (hours, minutes, and, optionally, seconds)
94 A date and time value of the form yyyy-mm-dd hh:mm[:ss] (year, month, date, hours, minutes, and, optionally, seconds)
95
96 *check* Verify the syntactic correctness of the candidate configuration"""
97 # NOTE: non netconf standard, Junos specific commit-configuration element, see
98 # https://www.juniper.net/documentation/en_US/junos/topics/reference/tag-summary/junos-xml-protocol-commit-configuration.html
99 node = new_ele_ns("commit-configuration", "")
100 if confirmed and at_time is not None:
101 raise NCClientError("'Commit confirmed' and 'commit at' are mutually exclusive.")
102 if confirmed:
103 self._assert(":confirmed-commit")
104 sub_ele(node, "confirmed")
105 if timeout is not None:
106 # NOTE: For Junos, confirm-timeout has to be given in minutes:
107 # https://www.juniper.net/documentation/en_US/junos/topics/reference/tag-summary/junos-xml-protocol-commit-configuration.html
108 # but the netconf standard and ncclient library uses seconds:
109 # https://tools.ietf.org/html/rfc6241#section-8.4.5
110 # http://ncclient.readthedocs.io/en/latest/manager.html#ncclient.manager.Manager.commit
111 # so need to convert the value in seconds to minutes.
112 timeout_int = int(timeout) if isinstance(timeout, str) else timeout
113 sub_ele(node, "confirm-timeout").text = str(int(math.ceil(timeout_int/60.0)))
114 elif at_time is not None:
115 sub_ele(node, "at-time").text = at_time
116 if comment is not None:
117 sub_ele(node, "log").text = comment
118 if synchronize:
119 sub_ele(node, "synchronize")
120 if check:
121 sub_ele(node, "check")
122 return self._request(node)
123
124 class Rollback(RPC):
125 def request(self, rollback=0):
126 node = new_ele('load-configuration', {'rollback':str(rollback)})
127 return self._request(node)