示例#1
0
    def search_users(self, query):
        checked = 0
        users = None
        if query.has_key('name'):
            name = query['name']
            if self.users.has_key(name):
                users = [self.users[name]]
            elif name in self.restricted_names and len(query) == 1:
                return defer.succeed([name])
            else:
                users = []
            checked = checked + 1

        if query.has_key('name_glob'):
            regex_str = glob_to_regex(query['name_glob'])
            regex = re.compile(regex_str)
            if users == None:
                users = self.users.values()
            filter_list(users, lambda user: not regex.search(user.name))
            checked = checked + 1

        if checked != len(query):
            raise DirectoryException('Unsupported query parameters',
                                     DirectoryException.INVALID_QUERY)
        elif users == None:
            return defer.succeed(self.users.keys())
        return defer.succeed([user.name for user in users])
示例#2
0
    def search_groups(self, group_type, query_dict):
        group_dict = self.groups.get(group_type)
        if group_dict is None:
            return defer.fail(
                DirectoryException("Invalid or unsupported group type '%s'" %
                                   group_type))
        if len(set(query_dict.keys()) - set(['name', 'name_glob'])):
            raise DirectoryException("Unsupported query in search_groups")
        groups = None
        if query_dict.has_key('name'):
            name = query_dict['name']
            if group_dict.has_key(name):
                groups = [name]
            elif name in self.restricted_names and len(query_dict) == 1:
                return defer.succeed([name])
            else:
                groups = []

        if query_dict.has_key('name_glob'):
            regex_str = glob_to_regex(query_dict['name_glob'])
            regex = re.compile(regex_str)
            if groups == None:
                groups = group_dict.keys()
            filter_list(groups, lambda group: not regex.search(group))

        if groups == None:
            return defer.succeed(group_dict.keys())
        return defer.succeed(groups)
    def search_users(self, query):
        checked = 0
        users = None
        if query.has_key('name'):
            name = query['name']
            if self.users.has_key(name):
                users = [ self.users[name] ]
            elif name in self.restricted_names and len(query) == 1:
                return defer.succeed([name])
            else:
                users = []
            checked = checked + 1
        
        if query.has_key('name_glob'):
            regex_str = glob_to_regex(query['name_glob']) 
            regex = re.compile(regex_str)
            if users == None:
                users = self.users.values()
            filter_list(users, lambda user : not regex.search(user.name) ) 
            checked = checked + 1

        if checked != len(query):
            raise DirectoryException('Unsupported query parameters',
                    DirectoryException.INVALID_QUERY)
        elif users == None:
            return defer.succeed(self.users.keys())
        return defer.succeed([user.name for user in users])
示例#4
0
    def del_switch(self, switch_name):
        if not self.switches.has_key(switch_name):
            return defer.fail(
                DirectoryException('Cannot delete switch - does not exist.',
                                   DirectoryException.NONEXISTING_NAME))
        if switch_name in self.restricted_names:
            return defer.fail(
                DirectoryException('Cannot delete switch %s.' % switch_name,
                                   DirectoryException.OPERATION_NOT_PERMITTED))
        ret = self.switches[switch_name]
        del self.switches[switch_name]
        del self.switch_bindings[ret.dpid.as_host()]

        #delete the associated locations
        locations = self.locations.values()
        filter_list(locations, lambda location: location.dpid != ret.dpid)
        ret.locations = locations
        for loc in locations:
            key = loc.dpid.as_host() + (loc.port << 48)
            del self.locations[loc.name]
            del self.location_bindings[key]
            #delete location from groups - ignore deferred result as the
            #call is actually synchronous
            self._del_member_from_groups(ret, Directory.LOCATION_PRINCIPAL,
                                         mangle_name(self.name, loc))

        return self._del_member_from_groups(
            ret, Directory.SWITCH_PRINCIPAL,
            mangle_name(self.name, switch_name))
    def search_groups(self, group_type, query_dict):
        group_dict = self.groups.get(group_type)
        if group_dict is None:
            return defer.fail(DirectoryException(
                    "Invalid or unsupported group type '%s'" %group_type))
        if len(set(query_dict.keys()) - set(['name', 'name_glob'])):
            raise DirectoryException("Unsupported query in search_groups")
        groups = None
        if query_dict.has_key('name'):
            name = query_dict['name']
            if group_dict.has_key(name):
                groups = [name]
            elif name in self.restricted_names and len(query_dict) == 1:
                return defer.succeed([name])
            else:
                groups = []

        if query_dict.has_key('name_glob'):
            regex_str = glob_to_regex(query_dict['name_glob'])
            regex = re.compile(regex_str)
            if groups == None:
                groups = group_dict.keys()
            filter_list(groups, lambda group : not regex.search(group) )

        if groups == None:
            return defer.succeed(group_dict.keys())
        return defer.succeed(groups)
    def del_switch(self, switch_name):
        if not self.switches.has_key(switch_name):
            return defer.fail(DirectoryException(
                    'Cannot delete switch - does not exist.',
                    DirectoryException.NONEXISTING_NAME))
        if switch_name in self.restricted_names:
            return defer.fail(DirectoryException('Cannot delete switch %s.'
                    % switch_name, DirectoryException.OPERATION_NOT_PERMITTED))
        ret = self.switches[switch_name]
        del self.switches[switch_name]
        del self.switch_bindings[ret.dpid.as_host()]

        #delete the associated locations
        locations = self.locations.values()
        filter_list(locations, lambda location : location.dpid != ret.dpid)
        ret.locations = locations
        for loc in locations:
            key = loc.dpid.as_host() + (loc.port << 48)
            del self.locations[loc.name]
            del self.location_bindings[key]
            #delete location from groups - ignore deferred result as the
            #call is actually synchronous
            self._del_member_from_groups(ret, Directory.LOCATION_PRINCIPAL,
                    mangle_name(self.name, loc))

        return self._del_member_from_groups(ret, Directory.SWITCH_PRINCIPAL,
                mangle_name(self.name, switch_name))
示例#7
0
 def get_switch(self, switch_name, include_locations=False):
     if self.switches.has_key(switch_name):
         si = copy.deepcopy(self.switches[switch_name])
         if include_locations:
             locations = self.locations.values()
             filter_list(locations, lambda loc: loc.dpid != si.dpid)
             si.locations = locations
         return defer.succeed(si)
     elif switch_name in self.restricted_names:
         return defer.succeed(SwitchInfo(switch_name))
     return defer.succeed(None)
 def get_switch(self, switch_name, include_locations=False):
     if self.switches.has_key(switch_name):
         si = copy.deepcopy(self.switches[switch_name])
         if include_locations:
             locations = self.locations.values()
             filter_list(locations, lambda loc : loc.dpid != si.dpid)
             si.locations = locations
         return defer.succeed(si)
     elif switch_name in self.restricted_names:
         return defer.succeed(SwitchInfo(switch_name))
     return defer.succeed(None)
示例#9
0
    def search_switches(self, query):
        checked = 0
        switches = None
        if query.has_key('name'):
            name = query['name']
            if self.switches.has_key(name):
                switches = [self.switches[name]]
            # FIXME
            elif name in self.restricted_names and len(query) == 1:
                return defer.succeed([name])
            else:
                switches = []
            checked = checked + 1

        if query.has_key('dpid'):
            dpid = query['dpid']
            if switches == None:
                key = dpid.as_host()
                if self.switch_bindings.has_key(key):
                    switches = [self.switch_bindings[key]]
                else:
                    switches = []
            else:
                filter_list(switches, lambda switch: switch.dpid != dpid)
            checked = checked + 1

        if query.has_key('name_glob'):
            regex_str = glob_to_regex(query['name_glob'])
            regex = re.compile(regex_str)
            if switches == None:
                switches = self.switches.values()
            filter_list(switches, lambda sw: not regex.search(sw.name))
            checked = checked + 1

        if checked != len(query):
            raise DirectoryException('Unsupported query parameters',
                                     DirectoryException.INVALID_QUERY)
        elif switches == None:
            return defer.succeed(self.switches.keys())
        return defer.succeed([switch.name for switch in switches])
示例#10
0
    def search_switches(self, query):
        checked = 0
        switches = None
        if query.has_key('name'):
            name = query['name']
            if self.switches.has_key(name):
                switches = [ self.switches[name] ]
            # FIXME
            elif name in self.restricted_names and len(query) == 1:
                return defer.succeed([name])
            else:
                switches = []
            checked = checked + 1

        if query.has_key('dpid'):
            dpid = query['dpid']
            if switches == None:
                key = dpid.as_host()
                if self.switch_bindings.has_key(key):
                    switches = [ self.switch_bindings[key] ]
                else:
                    switches = []
            else:
                filter_list(switches, lambda switch : switch.dpid != dpid)
            checked = checked + 1
        
        if query.has_key('name_glob'):
            regex_str = glob_to_regex(query['name_glob'])
            regex = re.compile(regex_str)
            if switches == None:
                switches = self.switches.values()
            filter_list(switches, lambda sw : not regex.search(sw.name) )
            checked = checked + 1
            
        if checked != len(query):
            raise DirectoryException('Unsupported query parameters',
                    DirectoryException.INVALID_QUERY)
        elif switches == None:
            return defer.succeed(self.switches.keys())
        return defer.succeed([switch.name for switch in switches])
示例#11
0
    def search_locations(self, query):
        checked = 0
        locations = None
        if query.has_key('name'):
            name = query['name']
            if self.locations.has_key(name):
                locations = [self.locations[name]]
            elif name in self.restricted_names and len(query) == 1:
                return defer.succeed([name])
            else:
                locations = []
            checked = checked + 1

        if query.has_key('dpid'):
            dpid = query['dpid']
            if query.has_key('port'):
                key = dpid.as_host() + (query['port'] << 48)
                if self.location_bindings.has_key(key):
                    match = self.location_bindings[key]
                    if locations == None or match in locations:
                        locations = [match]
                    else:
                        locations = []
                else:
                    locations = []
                checked = checked + 1
            else:
                if locations == None:
                    locations = self.locations.values()
                filter_list(locations, lambda location: location.dpid != dpid)
            checked = checked + 1
        elif query.has_key('port'):
            port = query['port']
            if locations == None:
                locations = self.locations.values()
            filter_list(locations, lambda location: location.port != port)
            checked = checked + 1

        if query.has_key('name_glob'):
            regex_str = glob_to_regex(query['name_glob'])
            regex = re.compile(regex_str)
            if locations == None:
                locations = self.locations.values()
            filter_list(locations, lambda loc: not regex.search(loc.name))
            checked = checked + 1

        if checked != len(query):
            raise DirectoryException('Unsupported query parameters',
                                     DiretoryException.INVALID_QUERY)
        elif locations == None:
            return defer.succeed(self.locations.keys())
        return defer.succeed([location.name for location in locations])
示例#12
0
    def search_locations(self, query):
        checked = 0
        locations = None
        if query.has_key('name'):
            name = query['name']
            if self.locations.has_key(name):
                locations = [ self.locations[name] ]
            elif name in self.restricted_names and len(query) == 1:
                return defer.succeed([name])
            else:
                locations = []
            checked = checked + 1

        if query.has_key('dpid'):
            dpid = query['dpid']
            if query.has_key('port'):
                key = dpid.as_host() + (query['port'] << 48)
                if self.location_bindings.has_key(key):
                    match = self.location_bindings[key]
                    if locations == None or match in locations:
                        locations = [ match ]
                    else:
                        locations = []
                else:
                    locations = []
                checked = checked + 1
            else:
                if locations == None:
                    locations = self.locations.values()
                filter_list(locations, lambda location : location.dpid != dpid)
            checked = checked + 1
        elif query.has_key('port'):
            port = query['port']
            if locations == None:
                locations = self.locations.values()
            filter_list(locations, lambda location : location.port != port)
            checked = checked + 1
        
        if query.has_key('name_glob'):
            regex_str = glob_to_regex(query['name_glob']) 
            regex = re.compile(regex_str)
            if locations == None:
                locations = self.locations.values()
            filter_list(locations, lambda loc : not regex.search(loc.name) )
            checked = checked + 1
            
        if checked != len(query):
            raise DirectoryException('Unsupported query parameters',
                    DiretoryException.INVALID_QUERY)
        elif locations == None:
            return defer.succeed(self.locations.keys())
        return defer.succeed([location.name for location in locations])
示例#13
0
   def query_bs_cb(res, dirname):
      # the binding storage query was not over names, so we have
      # to filter them manually
      if "name" in flat_args:
        name = flat_args["name"]
        filter_list(res,lambda n: demangle_name(n)[1] != name)
      if "name_glob" in flat_args:
        regex_str = glob_to_regex(flat_args['name_glob'])
        regex = re.compile(regex_str)
        filter_list(res, lambda n : not regex.search(demangle_name(n)[1]) )

      if dirname is not None:
        filter_list(res, lambda n: demangle_name(n)[0] != dirname)
       
      bs_set.update(res)
示例#14
0
    def search_hosts(self, query):
        checked = 0
        hosts = None
        if query.has_key('name'):
            name = query['name']
            if self.hosts.has_key(name):
                hosts = [self.hosts[name]]
            elif name in self.restricted_names and len(query) == 1:
                return defer.succeed([name])
            else:
                hosts = []
            checked = checked + 1

        if query.has_key('dpid'):
            dpid = query['dpid']
            if query.has_key('port'):
                key = dpid.as_host() + (query['port'] << 48)
                if self.host_loc_bindings.has_key(key):
                    match = self.host_loc_bindings[key]
                    if hosts == None or match in hosts:
                        hosts = [match]
                    else:
                        hosts = []
                else:
                    hosts = []
                checked = checked + 1
            else:
                if hosts == None:
                    hosts = self.hosts.values()
                filter_list(
                    hosts,
                    self.host_netinfo_nomatch(lambda info: info.dpid == dpid))
            checked = checked + 1
        elif query.has_key('port'):
            port = query['port']
            if hosts == None:
                hosts = self.hosts.values()
            filter_list(
                hosts,
                self.host_netinfo_nomatch(lambda info: info.port == port))
            checked = checked + 1

        if query.has_key('dladdr'):
            dladdr = query['dladdr'].hb_long()
            if self.host_dl_bindings.has_key(dladdr):
                match = self.host_dl_bindings[dladdr]
                if hosts == None or match in hosts:
                    hosts = [match]
                else:
                    hosts = []
            else:
                hosts = []
            checked = checked + 1

        if query.has_key('nwaddr'):
            nwaddr = query['nwaddr']
            if self.host_nw_bindings.has_key(nwaddr):
                match = self.host_nw_bindings[nwaddr]
                if hosts == None or match in hosts:
                    hosts = [match]
                else:
                    hosts = []
            else:
                hosts = []
            checked = checked + 1

        if query.has_key('is_gateway'):
            is_gway = query['is_gateway']
            if hosts == None:
                hosts = self.hosts.values()
            filter_list(
                hosts,
                self.host_netinfo_nomatch(
                    lambda info: info.is_gateway == is_gway))
            checked = checked + 1

        if query.has_key('is_router'):
            is_rter = query['is_router']
            if hosts == None:
                hosts = self.hosts.values()
            filter_list(
                hosts,
                self.host_netinfo_nomatch(
                    lambda info: info.is_router == is_rter))
            checked = checked + 1

        if query.has_key('alias'):
            alias = query['alias']
            if hosts == None:
                hosts = self.hosts.values()
            filter_list(hosts, lambda host: alias not in host.aliases)
            checked = checked + 1

        if query.has_key('name_glob'):
            regex_str = glob_to_regex(query['name_glob'])
            regex = re.compile(regex_str)
            if hosts == None:
                hosts = self.hosts.values()
            filter_list(hosts, lambda host: not regex.search(host.name))
            checked = checked + 1

        if checked != len(query):
            raise DirectoryException('Unsupported query parameters',
                                     DirectoryException.INVALID_QUERY)
        elif hosts == None:
            return defer.succeed(self.hosts.keys())
        return defer.succeed([host.name for host in hosts])
示例#15
0
    def search_hosts(self, query):
        checked = 0
        hosts = None
        if query.has_key('name'):
            name = query['name']
            if self.hosts.has_key(name):
                hosts = [ self.hosts[name] ]
            elif name in self.restricted_names and len(query) == 1:
                return defer.succeed([name])
            else:
                hosts = []
            checked = checked + 1
        
        if query.has_key('dpid'):
            dpid = query['dpid']
            if query.has_key('port'):
                key = dpid.as_host() + (query['port'] << 48)
                if self.host_loc_bindings.has_key(key):
                    match = self.host_loc_bindings[key]
                    if hosts == None or match in hosts:
                        hosts = [ match ]
                    else:
                        hosts = []
                else:
                    hosts = []
                checked = checked + 1
            else:
                if hosts == None:
                    hosts = self.hosts.values()
                filter_list(hosts, self.host_netinfo_nomatch(lambda info : info.dpid == dpid))
            checked = checked + 1
        elif query.has_key('port'):
            port = query['port']
            if hosts == None:
                hosts = self.hosts.values()
            filter_list(hosts, self.host_netinfo_nomatch(lambda info : info.port == port))
            checked = checked + 1

        if query.has_key('dladdr'):
            dladdr = query['dladdr'].hb_long()
            if self.host_dl_bindings.has_key(dladdr):
                match = self.host_dl_bindings[dladdr]
                if hosts == None or match in hosts:
                    hosts = [ match ]
                else:
                    hosts = []
            else:
                hosts = []
            checked = checked + 1
            
        if query.has_key('nwaddr'):
            nwaddr = query['nwaddr']
            if self.host_nw_bindings.has_key(nwaddr):
                match = self.host_nw_bindings[nwaddr]
                if hosts == None or match in hosts:
                    hosts = [ match ]
                else:
                    hosts = []
            else:
                hosts = []
            checked = checked + 1

        if query.has_key('is_gateway'):
            is_gway = query['is_gateway']
            if hosts == None:
                hosts = self.hosts.values()
            filter_list(hosts, self.host_netinfo_nomatch(
                    lambda info : info.is_gateway == is_gway))
            checked = checked + 1

        if query.has_key('is_router'):
            is_rter = query['is_router']
            if hosts == None:
                hosts = self.hosts.values()
            filter_list(hosts, self.host_netinfo_nomatch(
                    lambda info : info.is_router == is_rter))
            checked = checked + 1

        if query.has_key('alias'):
            alias = query['alias']
            if hosts == None:
                hosts = self.hosts.values()
            filter_list(hosts, lambda host : alias not in host.aliases)
            checked = checked + 1
        
        if query.has_key('name_glob'):
            regex_str = glob_to_regex(query['name_glob'])
            regex = re.compile(regex_str)
            if hosts == None:
                hosts = self.hosts.values()
            filter_list(hosts, lambda host : not regex.search(host.name) )
            checked = checked + 1

        if checked != len(query):
            raise DirectoryException('Unsupported query parameters',
                    DirectoryException.INVALID_QUERY)
        elif hosts == None:
            return defer.succeed(self.hosts.keys())
        return defer.succeed([host.name for host in hosts])