ccsession.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. # Copyright (C) 2009 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. #
  16. # Client-side functionality for configuration and commands
  17. #
  18. # It keeps a cc-channel session with the configuration manager daemon,
  19. # and handles configuration updates and direct commands
  20. # modeled after ccsession.h/cc 'protocol' changes here need to be
  21. # made there as well
  22. """Classes and functions for handling configuration and commands
  23. This module provides the ModuleCCSession and UIModuleCCSession
  24. classes, as well as a set of utility functions to create and parse
  25. messages related to commands and configuration
  26. Modules should use the ModuleCCSession class to connect to the
  27. configuration manager, and receive updates and commands from
  28. other modules.
  29. Configuration user interfaces should use the UIModuleCCSession
  30. to connect to b10-cmdctl, and receive and send configuration and
  31. commands through that to the configuration manager.
  32. """
  33. from isc.cc import Session
  34. from isc.config.config_data import ConfigData, MultiConfigData, BIND10_CONFIG_DATA_VERSION
  35. import isc.config.module_spec
  36. import isc
  37. from isc.util.file import path_search
  38. import bind10_config
  39. from isc.log import log_config_update
  40. import json
  41. from isc.log_messages.config_messages import *
  42. logger = isc.log.Logger("config")
  43. class ModuleCCSessionError(Exception): pass
  44. def parse_answer(msg):
  45. """Returns a tuple (rcode, value), where value depends on the
  46. command that was called. If rcode != 0, value is a string
  47. containing an error message"""
  48. if type(msg) != dict:
  49. raise ModuleCCSessionError("Answer message is not a dict: " + str(msg))
  50. if 'result' not in msg:
  51. raise ModuleCCSessionError("answer message does not contain 'result' element")
  52. elif type(msg['result']) != list:
  53. raise ModuleCCSessionError("wrong result type in answer message")
  54. elif len(msg['result']) < 1:
  55. raise ModuleCCSessionError("empty result list in answer message")
  56. elif type(msg['result'][0]) != int:
  57. raise ModuleCCSessionError("wrong rcode type in answer message")
  58. else:
  59. if len(msg['result']) > 1:
  60. if (msg['result'][0] != 0 and type(msg['result'][1]) != str):
  61. raise ModuleCCSessionError("rcode in answer message is non-zero, value is not a string")
  62. return msg['result'][0], msg['result'][1]
  63. else:
  64. return msg['result'][0], None
  65. def create_answer(rcode, arg = None):
  66. """Creates an answer packet for config&commands. rcode must be an
  67. integer. If rcode == 0, arg is an optional value that depends
  68. on what the command or option was. If rcode != 0, arg must be
  69. a string containing an error message"""
  70. if type(rcode) != int:
  71. raise ModuleCCSessionError("rcode in create_answer() must be an integer")
  72. if rcode != 0 and type(arg) != str:
  73. raise ModuleCCSessionError("arg in create_answer for rcode != 0 must be a string describing the error")
  74. if arg != None:
  75. return { 'result': [ rcode, arg ] }
  76. else:
  77. return { 'result': [ rcode ] }
  78. # 'fixed' commands
  79. """Fixed names for command and configuration messages"""
  80. COMMAND_CONFIG_UPDATE = "config_update"
  81. COMMAND_MODULE_SPECIFICATION_UPDATE = "module_specification_update"
  82. COMMAND_GET_COMMANDS_SPEC = "get_commands_spec"
  83. COMMAND_GET_STATISTICS_SPEC = "get_statistics_spec"
  84. COMMAND_GET_CONFIG = "get_config"
  85. COMMAND_SET_CONFIG = "set_config"
  86. COMMAND_GET_MODULE_SPEC = "get_module_spec"
  87. COMMAND_MODULE_SPEC = "module_spec"
  88. COMMAND_SHUTDOWN = "shutdown"
  89. COMMAND_MODULE_STOPPING = "stopping"
  90. def parse_command(msg):
  91. """Parses what may be a command message. If it looks like one,
  92. the function returns (command, value) where command is a
  93. string. If it is not, this function returns None, None"""
  94. if type(msg) == dict and len(msg.items()) == 1:
  95. cmd, value = msg.popitem()
  96. if cmd == "command" and type(value) == list:
  97. if len(value) == 1 and type(value[0]) == str:
  98. return value[0], None
  99. elif len(value) > 1 and type(value[0]) == str:
  100. return value[0], value[1]
  101. return None, None
  102. def create_command(command_name, params = None):
  103. """Creates a module command message with the given command name (as
  104. specified in the module's specification, and an optional params
  105. object"""
  106. # TODO: validate_command with spec
  107. if type(command_name) != str:
  108. raise ModuleCCSessionError("command in create_command() not a string")
  109. cmd = [ command_name ]
  110. if params:
  111. cmd.append(params)
  112. msg = { 'command': cmd }
  113. return msg
  114. def default_logconfig_handler(new_config, config_data):
  115. errors = []
  116. if config_data.get_module_spec().validate_config(False, new_config, errors):
  117. isc.log.log_config_update(json.dumps(new_config),
  118. json.dumps(config_data.get_module_spec().get_full_spec()))
  119. else:
  120. logger.error(CONFIG_LOG_CONFIG_ERRORS, errors)
  121. class ModuleCCSession(ConfigData):
  122. """This class maintains a connection to the command channel, as
  123. well as configuration options for modules. The module provides
  124. a specification file that contains the module name, configuration
  125. options, and commands. It also gives the ModuleCCSession two callback
  126. functions, one to call when there is a direct command to the
  127. module, and one to update the configuration run-time. These
  128. callbacks are called when 'check_command' is called on the
  129. ModuleCCSession"""
  130. def __init__(self, spec_file_name, config_handler, command_handler,
  131. cc_session=None, handle_logging_config=True,
  132. socket_file = None):
  133. """Initialize a ModuleCCSession. This does *NOT* send the
  134. specification and request the configuration yet. Use start()
  135. for that once the ModuleCCSession has been initialized.
  136. specfile_name is the path to the specification file.
  137. config_handler and command_handler are callback functions,
  138. see set_config_handler and set_command_handler for more
  139. information on their signatures.
  140. cc_session can be used to pass in an existing CCSession,
  141. if it is None, one will be set up. This is mainly intended
  142. for testing purposes.
  143. handle_logging_config: if True, the module session will
  144. automatically handle logging configuration for the module;
  145. it will read the system-wide Logging configuration and call
  146. the logger manager to apply it. It will also inform the
  147. logger manager when the logging configuration gets updated.
  148. The module does not need to do anything except intializing
  149. its loggers, and provide log messages. Defaults to true.
  150. socket_file: If cc_session was none, this optional argument
  151. specifies which socket file to use to connect to msgq. It
  152. will be overridden by the environment variable
  153. MSGQ_SOCKET_FILE. If none, and no environment variable is
  154. set, it will use the system default.
  155. """
  156. module_spec = isc.config.module_spec_from_file(spec_file_name)
  157. ConfigData.__init__(self, module_spec)
  158. self._module_name = module_spec.get_module_name()
  159. self.set_config_handler(config_handler)
  160. self.set_command_handler(command_handler)
  161. if not cc_session:
  162. self._session = Session(socket_file)
  163. else:
  164. self._session = cc_session
  165. self._session.group_subscribe(self._module_name, "*")
  166. self._remote_module_configs = {}
  167. self._remote_module_callbacks = {}
  168. if handle_logging_config:
  169. self.add_remote_config(path_search('logging.spec', bind10_config.PLUGIN_PATHS),
  170. default_logconfig_handler)
  171. def __del__(self):
  172. # If the CC Session obejct has been closed, it returns
  173. # immediately.
  174. if self._session._closed: return
  175. self._session.group_unsubscribe(self._module_name, "*")
  176. for module_name in self._remote_module_configs:
  177. self._session.group_unsubscribe(module_name)
  178. def start(self):
  179. """Send the specification for this module to the configuration
  180. manager, and request the current non-default configuration.
  181. The config_handler will be called with that configuration"""
  182. self.__send_spec()
  183. self.__request_config()
  184. def send_stopping(self):
  185. """Sends a 'stopping' message to the configuration manager. This
  186. message is just an FYI, and no response is expected. Any errors
  187. when sending this message (for instance if the msgq session has
  188. previously been closed) are logged, but ignored."""
  189. # create_command could raise an exception as well, but except for
  190. # out of memory related errors, these should all be programming
  191. # failures and are not caught
  192. msg = create_command(COMMAND_MODULE_STOPPING,
  193. self.get_module_spec().get_full_spec())
  194. try:
  195. self._session.group_sendmsg(msg, "ConfigManager")
  196. except Exception as se:
  197. # If the session was previously closed, obvously trying to send
  198. # a message fails. (TODO: check if session is open so we can
  199. # error on real problems?)
  200. logger.error(CONFIG_SESSION_STOPPING_FAILED, se)
  201. def get_socket(self):
  202. """Returns the socket from the command channel session. This
  203. should *only* be used for select() loops to see if there
  204. is anything on the channel. If that loop is not completely
  205. time-critical, it is strongly recommended to only use
  206. check_command(), and not look at the socket at all."""
  207. return self._session._socket
  208. def close(self):
  209. """Close the session to the command channel"""
  210. self._session.close()
  211. def check_command(self, nonblock=True):
  212. """Check whether there is a command or configuration update on
  213. the channel. This function does a read on the cc session, and
  214. returns nothing.
  215. It calls check_command_without_recvmsg()
  216. to parse the received message.
  217. If nonblock is True, it just checks if there's a command
  218. and does nothing if there isn't. If nonblock is False, it
  219. waits until it arrives. It temporarily sets timeout to infinity,
  220. because commands may not come in arbitrary long time."""
  221. timeout_orig = self._session.get_timeout()
  222. self._session.set_timeout(0)
  223. try:
  224. msg, env = self._session.group_recvmsg(nonblock)
  225. finally:
  226. self._session.set_timeout(timeout_orig)
  227. self.check_command_without_recvmsg(msg, env)
  228. def check_command_without_recvmsg(self, msg, env):
  229. """Parse the given message to see if there is a command or a
  230. configuration update. Calls the corresponding handler
  231. functions if present. Responds on the channel if the
  232. handler returns a message."""
  233. # should we default to an answer? success-by-default? unhandled error?
  234. if msg is not None and not 'result' in msg:
  235. answer = None
  236. try:
  237. module_name = env['group']
  238. cmd, arg = isc.config.ccsession.parse_command(msg)
  239. if cmd == COMMAND_CONFIG_UPDATE:
  240. new_config = arg
  241. # If the target channel was not this module
  242. # it might be in the remote_module_configs
  243. if module_name != self._module_name:
  244. if module_name in self._remote_module_configs:
  245. # no checking for validity, that's up to the
  246. # module itself.
  247. newc = self._remote_module_configs[module_name].get_local_config()
  248. isc.cc.data.merge(newc, new_config)
  249. self._remote_module_configs[module_name].set_local_config(newc)
  250. if self._remote_module_callbacks[module_name] != None:
  251. self._remote_module_callbacks[module_name](new_config,
  252. self._remote_module_configs[module_name])
  253. # For other modules, we're not supposed to answer
  254. return
  255. # ok, so apparently this update is for us.
  256. errors = []
  257. if not self._config_handler:
  258. answer = create_answer(2, self._module_name + " has no config handler")
  259. elif not self.get_module_spec().validate_config(False, new_config, errors):
  260. answer = create_answer(1, ", ".join(errors))
  261. else:
  262. isc.cc.data.remove_identical(new_config, self.get_local_config())
  263. answer = self._config_handler(new_config)
  264. rcode, val = parse_answer(answer)
  265. if rcode == 0:
  266. newc = self.get_local_config()
  267. isc.cc.data.merge(newc, new_config)
  268. self.set_local_config(newc)
  269. else:
  270. # ignore commands for 'remote' modules
  271. if module_name == self._module_name:
  272. if self._command_handler:
  273. answer = self._command_handler(cmd, arg)
  274. else:
  275. answer = create_answer(2, self._module_name + " has no command handler")
  276. except Exception as exc:
  277. answer = create_answer(1, str(exc))
  278. if answer:
  279. self._session.group_reply(env, answer)
  280. def set_config_handler(self, config_handler):
  281. """Set the config handler for this module. The handler is a
  282. function that takes the full configuration and handles it.
  283. It should return an answer created with create_answer()"""
  284. self._config_handler = config_handler
  285. # should we run this right now since we've changed the handler?
  286. def set_command_handler(self, command_handler):
  287. """Set the command handler for this module. The handler is a
  288. function that takes a command as defined in the .spec file
  289. and return an answer created with create_answer()"""
  290. self._command_handler = command_handler
  291. def _add_remote_config_internal(self, module_spec,
  292. config_update_callback=None):
  293. """The guts of add_remote_config and add_remote_config_by_name"""
  294. module_cfg = ConfigData(module_spec)
  295. module_name = module_spec.get_module_name()
  296. self._session.group_subscribe(module_name)
  297. # Get the current config for that module now
  298. seq = self._session.group_sendmsg(create_command(COMMAND_GET_CONFIG, { "module_name": module_name }), "ConfigManager")
  299. try:
  300. answer, _ = self._session.group_recvmsg(False, seq)
  301. except isc.cc.SessionTimeout:
  302. raise ModuleCCSessionError("No answer from ConfigManager when "
  303. "asking about Remote module " +
  304. module_name)
  305. call_callback = False
  306. if answer:
  307. rcode, value = parse_answer(answer)
  308. if rcode == 0:
  309. if value != None:
  310. if module_spec.validate_config(False, value):
  311. module_cfg.set_local_config(value)
  312. call_callback = True
  313. else:
  314. raise ModuleCCSessionError("Bad config data for " +
  315. module_name + ": " +
  316. str(value))
  317. else:
  318. raise ModuleCCSessionError("Failure requesting remote " +
  319. "configuration data for " +
  320. module_name)
  321. # all done, add it
  322. self._remote_module_configs[module_name] = module_cfg
  323. self._remote_module_callbacks[module_name] = config_update_callback
  324. if call_callback and config_update_callback is not None:
  325. config_update_callback(value, module_cfg)
  326. def add_remote_config_by_name(self, module_name,
  327. config_update_callback=None):
  328. """
  329. This does the same as add_remote_config, but you provide the module name
  330. instead of the name of the spec file.
  331. """
  332. seq = self._session.group_sendmsg(create_command(COMMAND_GET_MODULE_SPEC,
  333. { "module_name":
  334. module_name }),
  335. "ConfigManager")
  336. try:
  337. answer, env = self._session.group_recvmsg(False, seq)
  338. except isc.cc.SessionTimeout:
  339. raise ModuleCCSessionError("No answer from ConfigManager when " +
  340. "asking about for spec of Remote " +
  341. "module " + module_name)
  342. if answer:
  343. rcode, value = parse_answer(answer)
  344. if rcode == 0:
  345. module_spec = isc.config.module_spec.ModuleSpec(value)
  346. if module_spec.get_module_name() != module_name:
  347. raise ModuleCCSessionError("Module name mismatch: " +
  348. module_name + " and " +
  349. module_spec.get_module_name())
  350. self._add_remote_config_internal(module_spec,
  351. config_update_callback)
  352. else:
  353. raise ModuleCCSessionError("Error code " + str(rcode) +
  354. "when asking for module spec of " +
  355. module_name)
  356. else:
  357. raise ModuleCCSessionError("No answer when asking for module " +
  358. "spec of " + module_name)
  359. # Just to be consistent with the add_remote_config
  360. return module_name
  361. def add_remote_config(self, spec_file_name, config_update_callback=None):
  362. """Gives access to the configuration of a different module.
  363. These remote module options can at this moment only be
  364. accessed through get_remote_config_value(). This function
  365. also subscribes to the channel of the remote module name
  366. to receive the relevant updates. It is not possible to
  367. specify your own handler for this right now, but you can
  368. specify a callback that is called after the change happened.
  369. start() must have been called on this CCSession
  370. prior to the call to this method.
  371. Returns the name of the module."""
  372. module_spec = isc.config.module_spec_from_file(spec_file_name)
  373. self._add_remote_config_internal(module_spec, config_update_callback)
  374. return module_spec.get_module_name()
  375. def remove_remote_config(self, module_name):
  376. """Removes the remote configuration access for this module"""
  377. if module_name in self._remote_module_configs:
  378. self._session.group_unsubscribe(module_name)
  379. del self._remote_module_configs[module_name]
  380. del self._remote_module_callbacks[module_name]
  381. def get_remote_config_value(self, module_name, identifier):
  382. """Returns the current setting for the given identifier at the
  383. given module. If the module has not been added with
  384. add_remote_config, a ModuleCCSessionError is raised"""
  385. if module_name in self._remote_module_configs:
  386. return self._remote_module_configs[module_name].get_value(identifier)
  387. else:
  388. raise ModuleCCSessionError("Remote module " + module_name +
  389. " not found")
  390. def __send_spec(self):
  391. """Sends the data specification to the configuration manager"""
  392. msg = create_command(COMMAND_MODULE_SPEC, self.get_module_spec().get_full_spec())
  393. seq = self._session.group_sendmsg(msg, "ConfigManager")
  394. try:
  395. answer, env = self._session.group_recvmsg(False, seq)
  396. except isc.cc.SessionTimeout:
  397. # TODO: log an error?
  398. pass
  399. def __request_config(self):
  400. """Asks the configuration manager for the current configuration, and call the config handler if set.
  401. Raises a ModuleCCSessionError if there is no answer from the configuration manager"""
  402. seq = self._session.group_sendmsg(create_command(COMMAND_GET_CONFIG, { "module_name": self._module_name }), "ConfigManager")
  403. try:
  404. answer, env = self._session.group_recvmsg(False, seq)
  405. if answer:
  406. rcode, value = parse_answer(answer)
  407. if rcode == 0:
  408. errors = []
  409. if value != None:
  410. if self.get_module_spec().validate_config(False,
  411. value,
  412. errors):
  413. self.set_local_config(value)
  414. if self._config_handler:
  415. self._config_handler(value)
  416. else:
  417. raise ModuleCCSessionError(
  418. "Wrong data in configuration: " +
  419. " ".join(errors))
  420. else:
  421. logger.error(CONFIG_GET_FAILED, value)
  422. else:
  423. raise ModuleCCSessionError("No answer from configuration manager")
  424. except isc.cc.SessionTimeout:
  425. raise ModuleCCSessionError("CC Session timeout waiting for configuration manager")
  426. class UIModuleCCSession(MultiConfigData):
  427. """This class is used in a configuration user interface. It contains
  428. specific functions for getting, displaying, and sending
  429. configuration settings through the b10-cmdctl module."""
  430. def __init__(self, conn):
  431. """Initialize a UIModuleCCSession. The conn object that is
  432. passed must have send_GET and send_POST functions"""
  433. MultiConfigData.__init__(self)
  434. self._conn = conn
  435. self.update_specs_and_config()
  436. def request_specifications(self):
  437. """Clears the current list of specifications, and requests a new
  438. list from b10-cmdctl. As other actions may have caused modules
  439. to be stopped, or new modules to be added, this is expected to
  440. be run after each interaction (at this moment). It is usually
  441. also combined with request_current_config(). For that reason,
  442. we provide update_specs_and_config() which calls both."""
  443. specs = self._conn.send_GET('/module_spec')
  444. self.clear_specifications()
  445. for module in specs.keys():
  446. self.set_specification(isc.config.ModuleSpec(specs[module]))
  447. def request_current_config(self):
  448. """Requests the current configuration from the configuration
  449. manager through b10-cmdctl, and stores those as CURRENT. This
  450. does not modify any local changes, it just updates to the current
  451. state of the server itself."""
  452. config = self._conn.send_GET('/config_data')
  453. if 'version' not in config or config['version'] != BIND10_CONFIG_DATA_VERSION:
  454. raise ModuleCCSessionError("Bad config version")
  455. self._set_current_config(config)
  456. def update_specs_and_config(self):
  457. """Convenience function to both clear and update the known list of
  458. module specifications, and update the current configuration on
  459. the server side. There are a few cases where the caller might only
  460. want to run one of these tasks, but often they are both needed."""
  461. self.request_specifications()
  462. self.request_current_config()
  463. def _add_value_to_list(self, identifier, value, module_spec):
  464. cur_list, status = self.get_value(identifier)
  465. if not cur_list:
  466. cur_list = []
  467. if value is None and "list_item_spec" in module_spec:
  468. if "item_default" in module_spec["list_item_spec"]:
  469. value = module_spec["list_item_spec"]["item_default"]
  470. if value is None:
  471. raise isc.cc.data.DataNotFoundError(
  472. "No value given and no default for " + str(identifier))
  473. if value not in cur_list:
  474. cur_list.append(value)
  475. self.set_value(identifier, cur_list)
  476. else:
  477. raise isc.cc.data.DataAlreadyPresentError(str(value) +
  478. " already in "
  479. + str(identifier))
  480. def _add_value_to_named_set(self, identifier, value, item_value):
  481. if type(value) != str:
  482. raise isc.cc.data.DataTypeError("Name for named_set " +
  483. identifier +
  484. " must be a string")
  485. # fail on both None and empty string
  486. if not value:
  487. raise isc.cc.data.DataNotFoundError(
  488. "Need a name to add a new item to named_set " +
  489. str(identifier))
  490. else:
  491. cur_map, status = self.get_value(identifier)
  492. if not cur_map:
  493. cur_map = {}
  494. if value not in cur_map:
  495. cur_map[value] = item_value
  496. self.set_value(identifier, cur_map)
  497. else:
  498. raise isc.cc.data.DataAlreadyPresentError(value +
  499. " already in " +
  500. identifier)
  501. def add_value(self, identifier, value_str = None, set_value_str = None):
  502. """Add a value to a configuration list. Raises a DataTypeError
  503. if the value does not conform to the list_item_spec field
  504. of the module config data specification. If value_str is
  505. not given, we add the default as specified by the .spec
  506. file. Raises a DataNotFoundError if the given identifier
  507. is not specified in the specification as a map or list.
  508. Raises a DataAlreadyPresentError if the specified element
  509. already exists."""
  510. module_spec = self.find_spec_part(identifier)
  511. if module_spec is None:
  512. raise isc.cc.data.DataNotFoundError("Unknown item " + str(identifier))
  513. # for type any, we determine the 'type' by what value is set
  514. # (which would be either list or dict)
  515. cur_value, _ = self.get_value(identifier)
  516. type_any = module_spec['item_type'] == 'any'
  517. # the specified element must be a list or a named_set
  518. if 'list_item_spec' in module_spec or\
  519. (type_any and type(cur_value) == list):
  520. value = None
  521. # in lists, we might get the value with spaces, making it
  522. # the third argument. In that case we interpret both as
  523. # one big string meant as the value
  524. if value_str is not None:
  525. if set_value_str is not None:
  526. value_str += set_value_str
  527. value = isc.cc.data.parse_value_str(value_str)
  528. self._add_value_to_list(identifier, value, module_spec)
  529. elif 'named_set_item_spec' in module_spec or\
  530. (type_any and type(cur_value) == dict):
  531. item_name = None
  532. item_value = None
  533. if value_str is not None:
  534. item_name = value_str
  535. if set_value_str is not None:
  536. item_value = isc.cc.data.parse_value_str(set_value_str)
  537. else:
  538. if 'item_default' in module_spec['named_set_item_spec']:
  539. item_value = module_spec['named_set_item_spec']['item_default']
  540. self._add_value_to_named_set(identifier, item_name,
  541. item_value)
  542. else:
  543. raise isc.cc.data.DataNotFoundError(str(identifier) + " is not a list or a named set")
  544. def _remove_value_from_list(self, identifier, value):
  545. if value is None:
  546. # we are directly removing a list index
  547. id, list_indices = isc.cc.data.split_identifier_list_indices(identifier)
  548. if list_indices is None:
  549. raise isc.cc.data.DataTypeError("identifier in remove_value() does not contain a list index, and no value to remove")
  550. else:
  551. self.set_value(identifier, None)
  552. else:
  553. cur_list, status = self.get_value(identifier)
  554. if not cur_list:
  555. cur_list = []
  556. elif value in cur_list:
  557. cur_list.remove(value)
  558. self.set_value(identifier, cur_list)
  559. def _remove_value_from_named_set(self, identifier, value):
  560. if value is None:
  561. raise isc.cc.data.DataNotFoundError("Need a name to remove an item from named_set " + str(identifier))
  562. elif type(value) != str:
  563. raise isc.cc.data.DataTypeError("Name for named_set " + identifier + " must be a string")
  564. else:
  565. cur_map, status = self.get_value(identifier)
  566. if not cur_map:
  567. cur_map = {}
  568. if value in cur_map:
  569. del cur_map[value]
  570. self.set_value(identifier, cur_map)
  571. else:
  572. raise isc.cc.data.DataNotFoundError(value + " not found in named_set " + str(identifier))
  573. def remove_value(self, identifier, value_str):
  574. """Remove a value from a configuration list or named set.
  575. The value string must be a string representation of the full
  576. item. Raises a DataTypeError if the value at the identifier
  577. is not a list, or if the given value_str does not match the
  578. list_item_spec """
  579. module_spec = self.find_spec_part(identifier)
  580. if module_spec is None:
  581. raise isc.cc.data.DataNotFoundError("Unknown item " + str(identifier))
  582. value = None
  583. if value_str is not None:
  584. value = isc.cc.data.parse_value_str(value_str)
  585. # for type any, we determine the 'type' by what value is set
  586. # (which would be either list or dict)
  587. cur_value, _ = self.get_value(identifier)
  588. type_any = module_spec['item_type'] == 'any'
  589. # there's two forms of 'remove from list'; the remove-value-from-list
  590. # form, and the 'remove-by-index' form. We can recognize the second
  591. # case by value is None
  592. if 'list_item_spec' in module_spec or\
  593. (type_any and type(cur_value) == list) or\
  594. value is None:
  595. if not type_any and value is not None:
  596. isc.config.config_data.check_type(module_spec['list_item_spec'], value)
  597. self._remove_value_from_list(identifier, value)
  598. elif 'named_set_item_spec' in module_spec or\
  599. (type_any and type(cur_value) == dict):
  600. self._remove_value_from_named_set(identifier, value_str)
  601. else:
  602. raise isc.cc.data.DataNotFoundError(str(identifier) + " is not a list or a named_set")
  603. def commit(self):
  604. """Commit all local changes, send them through b10-cmdctl to
  605. the configuration manager"""
  606. if self.get_local_changes():
  607. response = self._conn.send_POST('/ConfigManager/set_config',
  608. [ self.get_local_changes() ])
  609. answer = isc.cc.data.parse_value_str(response.read().decode())
  610. # answer is either an empty dict (on success), or one
  611. # containing errors
  612. if answer == {}:
  613. self.clear_local_changes()
  614. elif "error" in answer:
  615. raise ModuleCCSessionError("Error: " + str(answer["error"]) + "\n" + "Configuration not committed")
  616. else:
  617. raise ModuleCCSessionError("Unknown format of answer in commit(): " + str(answer))