def warn_missing_interface(value, data, field, is_no, obj_value, many=False): if not is_no: bigdb = bigsh.bigdb if debug.description() or debug.cli(): print 'warn_missing_interface:', value, data, field, is_no, \ obj_value, many # need switch, if_name pk_data = {} bigdb.add_mode_stack_paths(pk_data) switch = pk_data.get('switch') if switch == None: switch = pk_data.get('dpid') if switch == None: switch = data.get('switch') if switch == None: switch = data.get('dpid') if bigdb.enabled and switch == None: for (n, v) in data.items() + pk_data.items(): if n.find('/') >= 0: parts = n.split('/') for suffix in ['switch', 'switch-dpid', 'switch-id']: if parts[-1] == suffix: switch = v if switch == None: if debug.description(): print 'warn_missing_interface:', data raise error.ArgumentValidationError( "Can't identify switch for validation") force = True if data.get('force', '') != '' else False # check to see if the interface is a list or a range, ifs = value.split(',') range_re = re.compile(r'([A-Za-z0-9-/\.:]*?)(\d+)-(\d+)$') if many: interfaces = [] remedy = '' for if_name in ifs: # check for trailing "-<integer>" which is intended to # identify a range, if so split that into multiple entites m = range_re.match(if_name) if m: print 'TAIL MATCH', m.group(1), m.group(2), m.group(3) for suffix in range(int(m.group(2)), int(m.group(3)) + 1): interfaces.append('%s%s' % (m.group(1), suffix)) else: interfaces.append(if_name) else: remedy = '\nUse \"exit; no interface %s\" to remove' % value interfaces = [value] check_missing_interface(switch, interfaces, remedy) data[field] = value
def warn_missing_interface(value, data, field, is_no, obj_value, many = False): if not is_no: bigdb = bigsh.bigdb if debug.description() or debug.cli(): print 'warn_missing_interface:', value, data, field, is_no, \ obj_value, many # need switch, if_name pk_data = {} bigdb.add_mode_stack_paths(pk_data) switch = pk_data.get('switch') if switch == None: switch = pk_data.get('dpid') if switch == None: switch = data.get('switch') if switch == None: switch = data.get('dpid') if bigdb.enabled and switch == None: for (n,v) in data.items() + pk_data.items(): if n.find('/') >= 0: parts = n.split('/') for suffix in ['switch', 'switch-dpid', 'switch-id']: if parts[-1] == suffix: switch = v if switch == None: if debug.description(): print 'warn_missing_interface:', data raise error.ArgumentValidationError("Can't identify switch for validation") force = True if data.get('force', '') != '' else False # check to see if the interface is a list or a range, ifs = value.split(',') range_re = re.compile(r'([A-Za-z0-9-/\.:]*?)(\d+)-(\d+)$') if many: interfaces = [] remedy = '' for if_name in ifs: # check for trailing "-<integer>" which is intended to # identify a range, if so split that into multiple entites m = range_re.match(if_name) if m: print 'TAIL MATCH', m.group(1), m.group(2), m.group(3) for suffix in range(int(m.group(2)), int(m.group(3))+1): interfaces.append('%s%s' % (m.group(1), suffix)) else: interfaces.append(if_name) else: remedy = '\nUse \"exit; no interface %s\" to remove' % value interfaces = [value] check_missing_interface(switch, interfaces, remedy) data[field] = value
def alias_to_value_handler(value, data, field, path=None, other_path=None): """ Compute the alias value for the named field for the path. Place the resulting converted field into the data dictionary. Since this is a data-handler, the data dict must be updated even if this isn't an alias, otherwise the field value is lost. """ global bigsh if debug.description(): print 'alias_to_value_handler: ', value, path, other_path, data, field if path == None and other_path == None: raise error.CommandInternalError( "alias_to_value_handler path or other-path requred") # ought to assert bigsh.bigsb.enabled() items = {} alias_path = other_path if other_path else path alias_path = alias_path.split('|')[0] # other_path may have trailing keys for (key, alias) in bigsh.bigdb.alias_key_value(alias_path, field, data): if alias == value: data[field] = key break else: data[field] = value
def convert_tag_to_parts(value, data, namespace_key, name_key, value_key): """ Split a tag of the form [ns].name=value into the three component parts """ if debug.description(): print "convert_tag_to_parts: %s %s %s %s %s" % ( value, data, namespace_key, name_key, value_key) tag_and_value = value.split('=') if len(tag_and_value) != 2: raise error.ArgumentValidationError( "tag <[tag-namespace.]name>=<value>") tag_parts = tag_and_value[0].split('.') if len(tag_parts) == 1: tag_namespace = "default" tag_name = tag_parts[0] elif len(tag_parts) >= 2: tag_namespace = '.'.join(tag_parts[:-1]) tag_name = tag_parts[-1] # should the names have some specific validation? data[namespace_key] = tag_namespace data[name_key] = tag_name data[value_key] = tag_and_value[1]
def alias_to_value_handler(value, data, field, path = None, other_path = None): """ Compute the alias value for the named field for the path. Place the resulting converted field into the data dictionary. Since this is a data-handler, the data dict must be updated even if this isn't an alias, otherwise the field value is lost. """ global bigsh if debug.description(): print 'alias_to_value_handler: ', value, path, other_path, data, field if path == None and other_path == None: raise error.CommandInternalError("alias_to_value_handler path or other-path requred") # ought to assert bigsh.bigsb.enabled() items = {} alias_path = other_path if other_path else path alias_path = alias_path.split('|')[0] # other_path may have trailing keys for (key, alias) in bigsh.bigdb.alias_key_value(alias_path, field, data): if alias == value: data[field] = key break else: data[field] = value
def convert_tag_to_parts(value, data, namespace_key, name_key, value_key): """ Split a tag of the form [ns].name=value into the three component parts """ if debug.description(): print "convert_tag_to_parts: %s %s %s %s %s" % ( value, data, namespace_key, name_key, value_key) tag_and_value = value.split('=') if len(tag_and_value) != 2: raise error.ArgumentValidationError("tag <[tag-namespace.]name>=<value>") tag_parts = tag_and_value[0].split('.') if len(tag_parts) == 1: tag_namespace = "default" tag_name = tag_parts[0] elif len(tag_parts) >= 2: tag_namespace = '.'.join(tag_parts[:-1]) tag_name = tag_parts[-1] # should the names have some specific validation? data[namespace_key] = tag_namespace data[name_key] = tag_name data[value_key] = tag_and_value[1]
def complete_object_field(field, data, completions, path = None, other_path = None, mode = None, parent_field = None, parent_id = None, prefix = None, no_command = None, scoped = None): """ Populate 'completions' with the values of the primary key for the particular path """ if debug.description(): print "complete_object_field: ", path, other_path, mode, field, data, scoped if path == None: top = bigsh.mode_stack[-1] path = top.get('path') if path == None: raise error.CommandDescriptionError("path required") filter = {} if scoped: filter = dict(data) # completion version of data bigsh.bigdb.add_mode_stack_paths(filter) if no_command: bigsh.bigdb.completions(completions, path, field, prefix, filter) else: # Include complete-config-field as a second completion, # it will remove items which are already configured from the # other choices which currently exist. deletions = {} bigsh.bigdb.completions(deletions, path, field, prefix, filter) if debug.description(): print 'complete_object_field: removing ', deletions for delete in deletions: if delete in completions: del completions[delete]
def check_missing_interface(switch, interfaces, remedy): # # The switch value could be a compound key reference to a # switch, if there's a '|' in the switch valud, try to guess # which entry is the switch if type(interfaces) == str or type(interfaces) == unicode: interfaces = [interfaces] parts = switch.split('|') if len(parts) > 1: for part in parts: if utif.COMMAND_DPID_RE.match(part): switch = part break else: switch = part[0] bigdb = bigsh.bigdb try: (schema, result) = \ bigdb.schema_and_result('core/switch', {'dpid' : switch }) final_result = result.expect_single_result() if final_result == None: raise error.ArgumentValidationError("switch not connected") except: # no switch if debug.description() or debug.cli(): traceback.print_exc() bigsh.warning('switch %s currently not active, ' 'interface %s may not exist' % (switch, ', '.join(interfaces))) return if not 'interface' in final_result: bigsh.warning('switch %s currently not active, ' 'interface %s may not exist' % (switch, ', '.join(interfaces))) return known_interfaces = [x['name'] for x in final_result['interface']] if_names = [x.lower() for x in known_interfaces] for interface in interfaces: if not interface.lower() in if_names: # pre-servce case, try to identify unique ranges ranges = interface_ranges(known_interfaces) bigsh.warning('active switch has no interface "%s", ' 'known: %s' % (interface, ', '.join(ranges)) + remedy) return
def check_missing_interface(switch, interfaces, remedy): # # The switch value could be a compound key reference to a # switch, if there's a '|' in the switch valud, try to guess # which entry is the switch if type(interfaces) == str or type(interfaces) == unicode: interfaces = [interfaces] parts = switch.split('|') if len(parts) > 1: for part in parts: if utif.COMMAND_DPID_RE.match(part): switch = part break else: switch = part[0] bigdb = bigsh.bigdb try: (schema, result) = \ bigdb.schema_and_result('core/switch', {'dpid' : switch }) final_result = result.expect_single_result() if final_result == None: raise error.ArgumentValidationError("switch not connected") except: # no switch if debug.description() or debug.cli(): traceback.print_exc() bigsh.warning('switch %s currently not active, ' 'interface %s may not exist' % (switch, ', '.join(interfaces))) return if not 'interface' in final_result: bigsh.warning('switch %s currently not active, ' 'interface %s may not exist' % (switch, ', '.join(interfaces))) return known_interfaces = [x['name'] for x in final_result['interface']] if_names = [x.lower() for x in known_interfaces] for interface in interfaces: if not interface.lower() in if_names: # pre-servce case, try to identify unique ranges ranges = interface_ranges(known_interfaces) bigsh.warning( 'active switch has no interface "%s", ' 'known: %s' % (interface, ', '.join(ranges)) + remedy) return
def feature_enabled(feature): """ Return True when a particular feature is enabled. @param feature name of the feature ('bvs', 'bigtap', 'ha', etc) """ if feature in feature_registry: (enabled, initializer, args) = feature_registry[feature] # initialized? if initializer and not feature in feature_registry_initialized: feature_registry_initialized[feature] = True (initializer)(featrue, **args) return (enabled)(feature, **args) if debug.cli() or debug.description(): print 'feature_enabled: missing configuration for:', feature return False
def complete_alias_choice(field, data, prefix, completions, no_command, path = None, other_path = None, scoped = None): """ Complete selections from an external object (unlreated to this object stack's details), only returning unique keys, either aliases for the path, or primary keys. This ought to be improved, objects_starting_with() in the bigcli.py, is primarily intended to be use within bigcli.py """ if debug.description(): print "complete_alias_choice:", path, other_path, field, data, prefix, scoped if path == None: raise error.CommandDescriptionError("path requrired") filter = data if scoped else {} # XXX scoped <- need data from submode stack. bigdb_path = other_path if other_path else path bigsh.bigdb.completions(completions, bigdb_path, field, prefix, filter)
def complete_from_another(field, data, completions, no_command, path = None, other_path = None, prefix = None, parent_field = None, parent_id = None, scoped = None, mode_switch = None, explicit=None): """ Completion function used when another path is used to populate values for the current path the 'other-path' field identifies the path to use to collect choices from, it can consist of two parts other|field. When field isn't described here, it comes from the description parameter, however, the 'field' value there may be in use to describe the value of the associated action. """ if debug.description(): print "complete_from_another:", path, other_path, field, data, parent_field, parent_id # complete_from_another is intended to include other fields, which # shouldn't apply for a no command. if no_command: return bigdb = bigsh.bigdb filter = data if scoped else {} # XXX scoped <- need data from submode stack. if other_path.find('|') >= 0: parts = other_path.split('|') other_path = parts[0] field = parts[1] bigdb.completions(completions, other_path, field, prefix, filter, mode_switch = mode_switch)