def accept_user_fs_request(fs_request): ''' accepts a user flowspace request and adds it to user flowpscae. If a previous user flowspace is subset of what is being accepted, replace it with new flowpsace only to avoid duplicate. @param fs_request: flowspace request to be accepted @type fs_request: list of RequestedUserFlowSpace @return: [fv_args,match_list] to be xmlrpc-called to fv @note: all fs_requests should belong to one user ''' if len(fs_request) == 0: return [[], []] #check which of the previous user flowspaces are a subset of this flowspace ufs = UserFlowSpace.objects.filter(user=fs_request[0].user) for fs in ufs: if (singlefs_is_subset_of(fs, fs_request)): fs.delete() for single_fs in fs_request: user = single_fs.user ufs = UserFlowSpace(user=single_fs.user, approver=single_fs.admin) copy_fs(single_fs, ufs) ufs.save() single_fs.delete() [fv_args, match_list] = update_user_opts(user) return [fv_args, match_list]
def accept_user_fs_request(fs_request): ''' accepts a user flowspace request and adds it to user flowpscae. If a previous user flowspace is subset of what is being accepted, replace it with new flowpsace only to avoid duplicate. @param fs_request: flowspace request to be accepted @type fs_request: list of RequestedUserFlowSpace @return: [fv_args,match_list] to be xmlrpc-called to fv @note: all fs_requests should belong to one user ''' if len(fs_request) == 0: return [[],[]] #check which of the previous user flowspaces are a subset of this flowspace ufs = UserFlowSpace.objects.filter(user=fs_request[0].user) for fs in ufs: if (singlefs_is_subset_of(fs,fs_request)): fs.delete() for single_fs in fs_request: user = single_fs.user ufs = UserFlowSpace(user=single_fs.user, approver=single_fs.admin) copy_fs(single_fs,ufs) ufs.save() single_fs.delete() [fv_args,match_list] = update_user_opts(user) return [fv_args,match_list]
def update_admin_opts(admin): ''' When an adminflowspace is being changed, call this fucntion to opt out all the rules that are no longer are under full control of this admin. @param adam: The admin whose flowspace is changed and need an update on UserOpts ''' adm_fs = AdminFlowSpace.objects.filter(user=admin) opted_fses = OptsFlowSpace.objects.filter(opt__user=admin) to_opt_out = [] for fs in opted_fses: if not singlefs_is_subset_of(fs, adm_fs): to_opt_out.append(fs) fv_args = opt_fses_outof_exp(to_opt_out) # now delete all the UserOpts that doesn't have any OptsFlowSpace: useropts = UserOpts.objects.filter(user=admin) for useropt in useropts: optfs_set = useropt.optsflowspace_set.all() if len(optfs_set) == 0: useropt.delete() return fv_args
def update_admin_opts(admin): ''' When an adminflowspace is being changed, call this fucntion to opt out all the rules that are no longer are under full control of this admin. @param adam: The admin whose flowspace is changed and need an update on UserOpts ''' adm_fs = AdminFlowSpace.objects.filter(user=admin) opted_fses = OptsFlowSpace.objects.filter(opt__user=admin) to_opt_out = [] for fs in opted_fses: if not singlefs_is_subset_of(fs,adm_fs): to_opt_out.append(fs) fv_args = opt_fses_outof_exp(to_opt_out) # now delete all the UserOpts that doesn't have any OptsFlowSpace: useropts = UserOpts.objects.filter(user=admin) for useropt in useropts: optfs_set = useropt.optsflowspace_set.all() if len(optfs_set) == 0: useropt.delete() return fv_args
def test_singlefs_is_subset_of(self): f1 = FlowSpace(ip_src_s=10, ip_src_e=10) f2 = FlowSpace(ip_dst_s=10, ip_dst_e=10) f3 = FlowSpace(ip_src_s=10, ip_src_e=10, mac_src_s=20, mac_src_e=30) self.assertTrue(singlefs_is_subset_of(f1, [f1])) self.assertFalse(singlefs_is_subset_of(f1, [f2])) self.assertTrue(singlefs_is_subset_of(f1, [f1, f2])) self.assertTrue(singlefs_is_subset_of(f3, [f1])) self.assertFalse(singlefs_is_subset_of(f1, [f3])) f4 = FlowSpace( mac_dst_s=90, mac_dst_e=100, ) f5 = FlowSpace( mac_dst_s=100, mac_dst_e=100, ) self.assertTrue(singlefs_is_subset_of(f5, [f4])) f6 = FlowSpace( ip_src_s=30, ip_src_e=40, mac_src_s=50, mac_src_e=60, ip_dst_s=70, ip_dst_e=70, mac_dst_s=90, mac_dst_e=100, ) f7 = FlowSpace( ip_src_s=35, ip_src_e=40, mac_src_s=50, mac_src_e=60, ip_dst_s=70, ip_dst_e=70, mac_dst_s=100, mac_dst_e=100, ) f8 = FlowSpace( ip_src_s=35, ip_src_e=40, mac_src_s=50, mac_src_e=60, ip_dst_s=70, ip_dst_e=70, mac_dst_s=95, mac_dst_e=105, ) self.assertTrue(singlefs_is_subset_of(f7, [f6])) self.assertFalse(singlefs_is_subset_of(f8, [f7])) f9 = FlowSpace( ip_src_s=1, ip_src_e=1, mac_src_s=2, mac_src_e=2, ip_dst_s=3, ip_dst_e=3, mac_dst_s=4, mac_dst_e=4, ) f10 = FlowSpace( ip_src_s=1, ip_src_e=2, mac_src_s=1, mac_src_e=2, ip_dst_s=3, ip_dst_e=3, mac_dst_s=3, mac_dst_e=5, ) f11 = FlowSpace( ip_src_s=2, ip_src_e=2, mac_src_s=2, mac_src_e=2, ip_dst_s=3, ip_dst_e=3, mac_dst_s=3, mac_dst_e=5, ) self.assertTrue(singlefs_is_subset_of(f9, [f9])) self.assertTrue(singlefs_is_subset_of(f9, [f10])) self.assertFalse(singlefs_is_subset_of(f9, [f11])) f12 = FlowSpace( ip_src_s=1, ip_src_e=4, mac_src_s=2, mac_src_e=5, ip_dst_s=3, ip_dst_e=6, mac_dst_s=4, mac_dst_e=7, ) f13 = FlowSpace( ip_src_s=1, ip_src_e=2, mac_src_s=2, mac_src_e=3, ip_dst_s=3, ip_dst_e=4, mac_dst_s=4, mac_dst_e=5, ) f14 = FlowSpace( ip_src_s=3, ip_src_e=4, mac_src_s=4, mac_src_e=5, ip_dst_s=5, ip_dst_e=6, mac_dst_s=6, mac_dst_e=7, ) f15 = FlowSpace(ip_src_s=1, ip_src_e=4) f16 = FlowSpace(ip_src_s=1, ip_src_e=2) f17 = FlowSpace(ip_src_s=3, ip_src_e=4) self.assertFalse(singlefs_is_subset_of(f12, [f13, f14])) self.assertTrue(singlefs_is_subset_of(f15, [f16, f17]))
def test_singlefs_is_subset_of(self): f1 = FlowSpace(ip_src_s=10, ip_src_e=10) f2 = FlowSpace(ip_dst_s=10, ip_dst_e=10) f3 = FlowSpace(ip_src_s=10, ip_src_e=10, mac_src_s = 20, mac_src_e=30) self.assertTrue(singlefs_is_subset_of(f1,[f1])) self.assertFalse(singlefs_is_subset_of(f1,[f2])) self.assertTrue(singlefs_is_subset_of(f1,[f1,f2])) self.assertTrue(singlefs_is_subset_of(f3,[f1])) self.assertFalse(singlefs_is_subset_of(f1,[f3])) f4 = FlowSpace(mac_dst_s = 90, mac_dst_e=100,) f5 = FlowSpace(mac_dst_s = 100, mac_dst_e=100,) self.assertTrue(singlefs_is_subset_of(f5,[f4])) f6 = FlowSpace(ip_src_s=30, ip_src_e=40, mac_src_s = 50, mac_src_e=60, ip_dst_s=70, ip_dst_e=70, mac_dst_s = 90, mac_dst_e=100,) f7 = FlowSpace(ip_src_s=35, ip_src_e=40, mac_src_s = 50, mac_src_e=60, ip_dst_s=70, ip_dst_e=70, mac_dst_s = 100, mac_dst_e=100,) f8 = FlowSpace(ip_src_s=35, ip_src_e=40, mac_src_s = 50, mac_src_e=60, ip_dst_s=70, ip_dst_e=70, mac_dst_s = 95, mac_dst_e=105,) self.assertTrue(singlefs_is_subset_of(f7,[f6])) self.assertFalse(singlefs_is_subset_of(f8,[f7])) f9 = FlowSpace(ip_src_s=1, ip_src_e=1, mac_src_s = 2, mac_src_e=2, ip_dst_s=3, ip_dst_e=3, mac_dst_s = 4, mac_dst_e=4,) f10 = FlowSpace(ip_src_s=1, ip_src_e=2, mac_src_s = 1, mac_src_e=2, ip_dst_s=3, ip_dst_e=3, mac_dst_s = 3, mac_dst_e=5,) f11 = FlowSpace(ip_src_s=2, ip_src_e=2, mac_src_s = 2, mac_src_e=2, ip_dst_s=3, ip_dst_e=3, mac_dst_s = 3, mac_dst_e=5,) self.assertTrue(singlefs_is_subset_of(f9,[f9])) self.assertTrue(singlefs_is_subset_of(f9,[f10])) self.assertFalse(singlefs_is_subset_of(f9,[f11])) f12 = FlowSpace(ip_src_s=1, ip_src_e=4, mac_src_s = 2, mac_src_e=5, ip_dst_s=3, ip_dst_e=6, mac_dst_s = 4, mac_dst_e=7,) f13 = FlowSpace(ip_src_s=1, ip_src_e=2, mac_src_s = 2, mac_src_e=3, ip_dst_s=3, ip_dst_e=4, mac_dst_s = 4, mac_dst_e=5,) f14 = FlowSpace(ip_src_s=3, ip_src_e=4, mac_src_s = 4, mac_src_e=5, ip_dst_s=5, ip_dst_e=6, mac_dst_s = 6, mac_dst_e=7,) f15 = FlowSpace(ip_src_s=1, ip_src_e=4) f16 = FlowSpace(ip_src_s=1, ip_src_e=2) f17 = FlowSpace(ip_src_s=3, ip_src_e=4) self.assertFalse(singlefs_is_subset_of(f12,[f13,f14])) self.assertTrue(singlefs_is_subset_of(f15,[f16,f17]))