+import os
+import re
+import logging
+
+
+from ganeti import errors
+from ganeti import utils
+from ganeti import constants
+
+
+def _IsCpuMaskWellFormed(cpu_mask):
+ """Verifies if the given single CPU mask is valid
+
+ The single CPU mask should be in the form "a,b,c,d", where each
+ letter is a positive number or range.
+
+ """
+ try:
+ cpu_list = utils.ParseCpuMask(cpu_mask)
+ except errors.ParseError, _:
+ return False
+ return isinstance(cpu_list, list) and len(cpu_list) > 0
+
+
+def _IsMultiCpuMaskWellFormed(cpu_mask):
+ """Verifies if the given multiple CPU mask is valid
+
+ A valid multiple CPU mask is in the form "a:b:c:d", where each
+ letter is a single CPU mask.
+
+ """
+ try:
+ utils.ParseMultiCpuMask(cpu_mask)
+ except errors.ParseError, _:
+ return False
+
+ return True
+
+
+# Read the BaseHypervisor.PARAMETERS docstring for the syntax of the
+# _CHECK values
+
+# must be afile
+_FILE_CHECK = (utils.IsNormAbsPath, "must be an absolute normalized path",
+ os.path.isfile, "not found or not a file")
+
+# must be a directory
+_DIR_CHECK = (utils.IsNormAbsPath, "must be an absolute normalized path",
+ os.path.isdir, "not found or not a directory")
+
+# CPU mask must be well-formed
+# TODO: implement node level check for the CPU mask
+_CPU_MASK_CHECK = (_IsCpuMaskWellFormed,
+ "CPU mask definition is not well-formed",
+ None, None)
+
+# Multiple CPU mask must be well-formed
+_MULTI_CPU_MASK_CHECK = (_IsMultiCpuMaskWellFormed,
+ "Multiple CPU mask definition is not well-formed",
+ None, None)
+
+# Check for validity of port number
+_NET_PORT_CHECK = (lambda x: 0 < x < 65535, "invalid port number",
+ None, None)
+
+# nice wrappers for users
+REQ_FILE_CHECK = (True, ) + _FILE_CHECK
+OPT_FILE_CHECK = (False, ) + _FILE_CHECK
+REQ_DIR_CHECK = (True, ) + _DIR_CHECK
+OPT_DIR_CHECK = (False, ) + _DIR_CHECK
+REQ_NET_PORT_CHECK = (True, ) + _NET_PORT_CHECK
+OPT_NET_PORT_CHECK = (False, ) + _NET_PORT_CHECK
+REQ_CPU_MASK_CHECK = (True, ) + _CPU_MASK_CHECK
+OPT_CPU_MASK_CHECK = (False, ) + _CPU_MASK_CHECK
+REQ_MULTI_CPU_MASK_CHECK = (True, ) + _MULTI_CPU_MASK_CHECK
+OPT_MULTI_CPU_MASK_CHECK = (False, ) + _MULTI_CPU_MASK_CHECK
+
+# no checks at all
+NO_CHECK = (False, None, None, None, None)
+
+# required, but no other checks
+REQUIRED_CHECK = (True, None, None, None, None)
+
+# migration type
+MIGRATION_MODE_CHECK = (True, lambda x: x in constants.HT_MIGRATION_MODES,
+ "invalid migration mode", None, None)
+
+
+def ParamInSet(required, my_set):
+ """Builds parameter checker for set membership.
+
+ @type required: boolean
+ @param required: whether this is a required parameter
+ @type my_set: tuple, list or set
+ @param my_set: allowed values set
+
+ """
+ fn = lambda x: x in my_set
+ err = ("The value must be one of: %s" % utils.CommaJoin(my_set))
+ return (required, fn, err, None, None)
+
+