def create_obj_type_dict(obj_type, field, key=None, value=None): """ Return a dictionary from a table search, where the key is one of the fields. This doesn't manage multiple field matches. Typically, the field selected is a foreign key for the obj_type. For ('host-network-address', 'host'), this creates a dict indexed by the mac address, returning the row in the table associated with the mac (since the primary key for 'host' is a mac address). For ('tag-mapping', 'host'), this creates a dict indexed by the mac, returning the matching row in the table. note: This gets the whole table """ if not mi.obj_type_has_field(obj_type, field): return {} if not mi.obj_type_has_model(obj_type): data = {} if key and value: data[key] = value rows = rest_to_model.get_model_from_url(obj_type, data) elif not type(key) is dict: try: rows = sdnsh.get_table_from_store(obj_type, key, value) except Exception, e: errors = sdnsh.rest_error_to_dict(e) print sdnsh.rest_error_dict_to_message(errors) rows = []
def create_obj_type_dict(obj_type, field, key = None, value = None): """ Return a dictionary from a table search, where the key is one of the fields. This doesn't manage multiple field matches. Typically, the field selected is a foreign key for the obj_type. For ('host-network-address', 'host'), this creates a dict indexed by the mac address, returning the row in the table associated with the mac (since the primary key for 'host' is a mac address). For ('tag-mapping', 'host'), this creates a dict indexed by the mac, returning the matching row in the table. note: This gets the whole table """ if not mi.obj_type_has_field(obj_type, field): return {} if not mi.obj_type_has_model(obj_type): data = {} if key and value: data[key] = value rows = rest_to_model.get_model_from_url(obj_type, data) elif not type(key) is dict: try: rows = sdnsh.get_table_from_store(obj_type, key, value) except Exception, e: errors = sdnsh.rest_error_to_dict(e) print sdnsh.rest_error_dict_to_message(errors) rows = []
def check_missing_interface(switch, interface): # # 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 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] try: row = rest_to_model.get_model_from_url('switches', {'dpid' : switch }) except: if sdnsh.description or sdnsh.debug_backtrace: traceback.print_exc() row = [] if len(row) == 0 or row[0].get('ip-address', '') == '': sdnsh.warning('switch %s currently not active, ' 'interface %s may not exist' % (switch, interface)) return try: ports = rest_to_model.get_model_from_url('interfaces', {'dpid' : switch }) except: # can't validate current list of interface names return if_names = [x['portName'].lower() for x in ports] if not interface.lower() in if_names: # pre-servce case, try to identify unique ranges ranges = interface_ranges([x['portName'] for x in ports]) sdnsh.warning( 'active switch has no interface "%s", ' 'known: %s' % (interface, ', '.join(ranges)) + '\nUse \"exit; no interface %s\" to remove' % interface)
def check_missing_interface(switch, interface): # # 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 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] try: row = rest_to_model.get_model_from_url('switches', {'dpid': switch}) except: if sdnsh.description or sdnsh.debug_backtrace: traceback.print_exc() row = [] if len(row) == 0 or row[0].get('ip-address', '') == '': sdnsh.warning('switch %s currently not active, ' 'interface %s may not exist' % (switch, interface)) return try: ports = rest_to_model.get_model_from_url('interfaces', {'dpid': switch}) except: # can't validate current list of interface names return if_names = [x['portName'].lower() for x in ports] if not interface.lower() in if_names: # pre-servce case, try to identify unique ranges ranges = interface_ranges([x['portName'] for x in ports]) sdnsh.warning('active switch has no interface "%s", ' 'known: %s' % (interface, ', '.join(ranges)) + '\nUse \"exit; no interface %s\" to remove' % interface)
def update_switch_port_name_cache(): """ Update the cliModeInfo prettyprinting portNames table """ # return update_show_alias('port') errors = None switch_port_to_name_dict = {} try: ports = rest_to_model.get_model_from_url('interfaces', {}) except Exception, e: errors = sdnsh.rest_error_to_dict(e)
def validate_host(typedef, value): """ """ if _is_mac_address(value): return value pk = mi.pk('host-config') key = convert_alias_to_object_key('host-config', value) key_dict = { pk : key } mi.split_compound_into_dict('host-config', pk, key_dict, is_prefix = True) if 'mac' in key_dict and _is_mac_address(key_dict['mac']): try: _exists = rest_to_model.get_model_from_url('host', key_dict) return value except: raise error.ArgumentValidationError( 'host "%s": doesn\'t exist' % value) command._raise_argument_validation_exception(typedef, value, 'not host alias nor mac address')
def validate_host(typedef, value): """ """ if _is_mac_address(value): return value pk = mi.pk('host-config') key = convert_alias_to_object_key('host-config', value) key_dict = {pk: key} mi.split_compound_into_dict('host-config', pk, key_dict, is_prefix=True) if 'mac' in key_dict and _is_mac_address(key_dict['mac']): try: _exists = rest_to_model.get_model_from_url('host', key_dict) return value except: raise error.ArgumentValidationError('host "%s": doesn\'t exist' % value) command._raise_argument_validation_exception( typedef, value, 'not host alias nor mac address')
def convert_interface_to_port(value, data, field, other = None, scoped = None): # look for the switch name in data if scoped: dpid = data.get(scoped) elif 'dpid' in data: dpid = data['dpid'] else: dpid = data.get('switch', '') # possibly other choices # if its not a specific switch, no conversion is possible # should the value be passed through? if dpid == '': data[field] = value return ports = rest_to_model.get_model_from_url('interfaces', {'dpid' : dpid}) for port in ports: if port['portName'] == value: data[field] = port['portNumber'] # should this be a string? break else: raise error.ArgumentValidationError("Can't find port %s on switch %s" % (value, dpid))
def convert_interface_to_port(value, data, field, other=None, scoped=None): # look for the switch name in data if scoped: dpid = data.get(scoped) elif 'dpid' in data: dpid = data['dpid'] else: dpid = data.get('switch', '') # possibly other choices # if its not a specific switch, no conversion is possible # should the value be passed through? if dpid == '': data[field] = value return ports = rest_to_model.get_model_from_url('interfaces', {'dpid': dpid}) for port in ports: if port['portName'] == value: data[field] = port['portNumber'] # should this be a string? break else: raise error.ArgumentValidationError("Can't find port %s on switch %s" % (value, dpid))
case = mi.get_obj_type_field_case_sensitive(obj_type, key) id_value = utif.convert_case(case, text) if mi.obj_type_has_model(obj_type): # from the database try: entries = sdnsh.get_table_from_store(obj_type, key, id_value) errors = None except Exception, e: errors = sdnsh.rest_error_to_dict(e) if errors: print sdnsh.rest_error_dict_to_message(errors) return key_entries else: if id_value == '': entries = rest_to_model.get_model_from_url(obj_type, {}) else: entries = rest_to_model.get_model_from_url(obj_type, { key + "__startswith" : id_value }) if key and entries: # Expand any key values which are lists (hosts, for example) items = [x[key] for x in entries if x.get(key)] entries = [] for item in items: if type(item) == list: entries += item else: entries.append(item) key_entries = [sdnsh.quote_item(obj_type, x) for x in entries if x.startswith(id_value)] #
case = mi.get_obj_type_field_case_sensitive(obj_type, key) id_value = utif.convert_case(case, text) if mi.obj_type_has_model(obj_type): # from the database try: entries = sdnsh.get_table_from_store(obj_type, key, id_value) errors = None except Exception, e: errors = sdnsh.rest_error_to_dict(e) if errors: print sdnsh.rest_error_dict_to_message(errors) return key_entries else: if id_value == '': entries = rest_to_model.get_model_from_url(obj_type, {}) else: entries = rest_to_model.get_model_from_url( obj_type, {key + "__startswith": id_value}) if key and entries: # Expand any key values which are lists (hosts, for example) items = [x[key] for x in entries if x.get(key)] entries = [] for item in items: if type(item) == list: entries += item else: entries.append(item) key_entries = [ sdnsh.quote_item(obj_type, x) for x in entries