def buildKZorpMessage(self, dispatcher_name): """ <method internal="yes"> </method> """ import kzorp.messages as kzorp dim_name_to_attr_type = { 'reqid': kzorp.KZNL_ATTR_N_DIMENSION_REQID, 'iface': kzorp.KZNL_ATTR_N_DIMENSION_IFACE, 'ifgroup': kzorp.KZNL_ATTR_N_DIMENSION_IFGROUP, 'proto': kzorp.KZNL_ATTR_N_DIMENSION_PROTO, 'proto_type': kzorp.KZNL_ATTR_N_DIMENSION_PROTO_TYPE, 'proto_subtype': kzorp.KZNL_ATTR_N_DIMENSION_PROTO_SUBTYPE, 'src_port': kzorp.KZNL_ATTR_N_DIMENSION_SRC_PORT, 'dst_port': kzorp.KZNL_ATTR_N_DIMENSION_DST_PORT, 'src_subnet': kzorp.KZNL_ATTR_N_DIMENSION_SRC_IP, 'src_subnet6': kzorp.KZNL_ATTR_N_DIMENSION_SRC_IP6, 'src_zone': kzorp.KZNL_ATTR_N_DIMENSION_SRC_ZONE, 'dst_subnet': kzorp.KZNL_ATTR_N_DIMENSION_DST_IP, 'dst_subnet6': kzorp.KZNL_ATTR_N_DIMENSION_DST_IP6, 'dst_iface': kzorp.KZNL_ATTR_N_DIMENSION_DST_IFACE, 'dst_ifgroup': kzorp.KZNL_ATTR_N_DIMENSION_DST_IFGROUP, 'dst_zone': kzorp.KZNL_ATTR_N_DIMENSION_DST_ZONE, } messages = [] # determine maximum dimension length kzorp_dimensions = {} for (key, value) in self._dimensions.items(): kzorp_dimensions[dim_name_to_attr_type[key]] = value kzorp_dimension_sizes = dict( map(lambda (key, value): (key, len(value)), kzorp_dimensions.items())) max_dimension_length = max(kzorp_dimension_sizes.values() ) if len(kzorp_dimension_sizes) > 0 else 0 messages.append( kzorp.KZorpAddRuleMessage(dispatcher_name, self.getId(), self._service.name, kzorp_dimension_sizes)) for i in xrange(max_dimension_length): data = {} for dimension, values in kzorp_dimensions.items(): if len(values) > i: data[dimension] = values[i] messages.append( kzorp.KZorpAddRuleEntryMessage(dispatcher_name, self.getId(), data)) return messages
def setup_service_dispatcher(self, services, dispatchers, add_zone = True, add_service = True): self._dumped_diszpancsers = [] self.start_transaction() if add_zone: self._addzones() if add_service: for service in services: if type(service) == types.DictType: service = service['name'] self.send_message(messages.KZorpAddProxyServiceMessage(service)) for dispatcher in dispatchers: message_add_dispatcher = messages.KZorpAddDispatcherMessage(dispatcher['name'], dispatcher['num_rules'] ) self.send_message(message_add_dispatcher, error_handler=lambda res: os.strerror(res)+" "+str(message_add_dispatcher)) for rule in dispatcher['rules']: _max = 0 for name, value in rule['entry_nums'].items(): if _max < value: _max = value message_add_rule = messages.KZorpAddRuleMessage(dispatcher['name'], rule['rule_id'], rule['service'], rule['entry_nums'] ) self.send_message(message_add_rule) for i in range(_max): data = {} for dim_type in messages.N_DIMENSION_ATTRS: if dim_type in rule['entry_nums'] and rule['entry_nums'][dim_type] > i: if dim_type in [messages.KZNL_ATTR_N_DIMENSION_SRC_IP, messages.KZNL_ATTR_N_DIMENSION_DST_IP]: subnet = rule['entry_values'][dim_type][i] data[dim_type] = (testutil.addr_packed(subnet), testutil.netmask_packed(subnet)) elif dim_type in [messages.KZNL_ATTR_N_DIMENSION_SRC_IP6, messages.KZNL_ATTR_N_DIMENSION_DST_IP6]: subnet = rule['entry_values'][dim_type][i] data[dim_type] = (testutil.addr_packed6(subnet), testutil.netmask_packed6(subnet)) else: data[dim_type] = rule['entry_values'][dim_type][i] #print "rule=%s\ndispatcher=%s\ndata=%s\n"%(rule,dispatcher['name'],data) message_add_rule_entry = messages.KZorpAddRuleEntryMessage(dispatcher['name'], rule['rule_id'], data) self.send_message(message_add_rule_entry) self.end_transaction()
def test_n_dimension_errors(self): error_dup_dispatchers = [{ 'name': 'n_dimension_error', 'num_rules': 0, }, { 'name': 'n_dimension_error2', 'num_rules': 2, 'rules': [{ 'rule_id': 1, 'service': 'A_A', 'entry_nums': { messages.KZNL_ATTR_N_DIMENSION_IFACE: 2 }, 'errno': 0 }] }] error_num_rules_dispatchers = [ { 'name': 'n_dimension_error3', 'num_rules': 1, 'rules': [{ 'rule_id': 2, 'service': 'A_A', 'entry_nums': { messages.KZNL_ATTR_N_DIMENSION_IFACE: 2 }, 'errno': 0 }, { 'rule_id': 3, 'service': 'A_A', 'entry_nums': { messages.KZNL_ATTR_N_DIMENSION_IFACE: 2 }, 'errno': -errno.EINVAL }] }, { 'name': 'n_dimension_error4', 'num_rules': 1, 'rules': [{ 'rule_id': 3, 'service': 'A_A', 'entry_nums': { messages.KZNL_ATTR_N_DIMENSION_IFACE: 2 }, #FIXME: this shouldbe: -errno.EEXIST 'errno': 0 }] } ] error_num_rule_entries = [{ 'name': 'n_dimension_error5', 'num_rules': 8, 'rules': [{ 'rule_id': 4, 'service': 'A_A', 'entry_nums': { messages.KZNL_ATTR_N_DIMENSION_IFACE: 1 }, 'entry_values': { messages.KZNL_ATTR_N_DIMENSION_IFACE: ['eth4', 'eth2'] }, 'rule_entry_errnos': [0, -errno.ENOMEM] }, { 'rule_id': 5, 'service': 'A_A', 'entry_nums': { messages.KZNL_ATTR_N_DIMENSION_PROTO: 1 }, 'entry_values': { messages.KZNL_ATTR_N_DIMENSION_PROTO: [socket.IPPROTO_TCP, socket.IPPROTO_UDP] }, 'rule_entry_errnos': [0, -errno.ENOMEM] }, { 'rule_id': 6, 'service': 'A_A', 'entry_nums': { messages.KZNL_ATTR_N_DIMENSION_SRC_PORT: 1 }, 'entry_values': { messages.KZNL_ATTR_N_DIMENSION_SRC_PORT: [(1, 1), (2, 2)] }, 'rule_entry_errnos': [0, -errno.ENOMEM] }, { 'rule_id': 7, 'service': 'A_A', 'entry_nums': { messages.KZNL_ATTR_N_DIMENSION_DST_PORT: 1 }, 'entry_values': { messages.KZNL_ATTR_N_DIMENSION_DST_PORT: [(3, 3), (4, 5)] }, 'rule_entry_errnos': [0, -errno.ENOMEM] }, { 'rule_id': 8, 'service': 'A_A', 'entry_nums': { messages.KZNL_ATTR_N_DIMENSION_SRC_IP: 1 }, 'entry_values': { messages.KZNL_ATTR_N_DIMENSION_SRC_IP: ['1.2.3.4', '2.3.4.5'] }, 'rule_entry_errnos': [0, -errno.ENOMEM] }, { 'rule_id': 9, 'service': 'A_A', 'entry_nums': { messages.KZNL_ATTR_N_DIMENSION_SRC_ZONE: 1 }, 'entry_values': { messages.KZNL_ATTR_N_DIMENSION_SRC_ZONE: ['ZZZ', 'ZZ'] }, 'rule_entry_errnos': [0, -errno.ENOMEM] }, { 'rule_id': 10, 'service': 'A_A', 'entry_nums': { messages.KZNL_ATTR_N_DIMENSION_DST_IP: 1 }, 'entry_values': { messages.KZNL_ATTR_N_DIMENSION_DST_IP: ['3.4.5.6', '4.5.6.7'] }, 'rule_entry_errnos': [0, -errno.ENOMEM] }, { 'rule_id': 11, 'service': 'A_A', 'entry_nums': { messages.KZNL_ATTR_N_DIMENSION_DST_ZONE: 1 }, 'entry_values': { messages.KZNL_ATTR_N_DIMENSION_DST_ZONE: ['AAA', 'AA'] }, 'rule_entry_errnos': [0, -errno.ENOMEM] }] }] error_zones_exist = [{ 'name': 'n_dimension_error6', 'num_rules': 2, 'rules': [{ 'rule_id': 12, 'service': 'A_A', 'entry_nums': { messages.KZNL_ATTR_N_DIMENSION_SRC_ZONE: 1 }, 'entry_values': { messages.KZNL_ATTR_N_DIMENSION_SRC_ZONE: 'BBB' }, 'rule_entry_errnos': [-errno.ENOENT] }, { 'rule_id': 13, 'service': 'A_A', 'entry_nums': { messages.KZNL_ATTR_N_DIMENSION_DST_ZONE: 1 }, 'entry_values': { messages.KZNL_ATTR_N_DIMENSION_DST_ZONE: 'CCC' }, 'rule_entry_errnos': [-errno.ENOENT] }] }] #Check add_dispatcher without starting a transaction dispatcher = error_dup_dispatchers[0] message_add_dispatcher = messages.KZorpAddDispatcherMessage( dispatcher['name'], dispatcher['num_rules']) res = self.send_message(message_add_dispatcher, assert_on_error=False) self.assertEqual(res, -errno.ENOENT) #check duplicated add_dispatcher self.start_transaction() message_add_dispatcher = messages.KZorpAddDispatcherMessage( dispatcher['name'], dispatcher['num_rules']) res = self.send_message(message_add_dispatcher, assert_on_error=False) self.assertEqual(res, 0) res = self.send_message(message_add_dispatcher, assert_on_error=False) self.assertEqual(res, -errno.EEXIST) self.end_transaction() #check if num_rules > number of rule_entries dispathcer = error_dup_dispatchers[1] self.start_transaction() message_add_dispatcher = messages.KZorpAddDispatcherMessage( dispatcher['name'], dispatcher['num_rules']) res = self.send_message(message_add_dispatcher, assert_on_error=False) self.assertEqual(res, 0) self.end_transaction() #check if num_rules < number of rule entries, check adding existing rule_id self.start_transaction() for i in range(len(error_num_rules_dispatchers)): dispatcher = error_num_rules_dispatchers[i] message_add_dispatcher = messages.KZorpAddDispatcherMessage( dispatcher['name'], dispatcher['num_rules']) res = self.send_message(message_add_dispatcher, assert_on_error=False) for rule in dispatcher['rules']: message_add_rule = messages.KZorpAddRuleMessage( dispatcher['name'], rule['rule_id'], rule['service'], rule['entry_nums']) res = self.send_message(message_add_rule, assert_on_error=False) if 'errno' in rule: self.assertEqual(res, rule['errno']) self.end_transaction() #check if entry_nums < number of entry_values self.start_transaction() for i in range(len(error_num_rule_entries)): dispatcher = error_num_rule_entries[i] message_add_dispatcher = messages.KZorpAddDispatcherMessage( dispatcher['name'], dispatcher['num_rules']) res = self.send_message(message_add_dispatcher, assert_on_error=False) for rule in dispatcher['rules']: _max = 2 message_add_rule = messages.KZorpAddRuleMessage( dispatcher['name'], rule['rule_id'], rule['service'], rule['entry_nums']) res = self.send_message(message_add_rule, assert_on_error=False) if 'errno' in rule: self.assertEqual(res, rule['errno']) for i in range(_max): data = {} for dim_type in messages.N_DIMENSION_ATTRS: if dim_type in rule['entry_nums']: if dim_type in [ messages.KZNL_ATTR_N_DIMENSION_SRC_IP, messages.KZNL_ATTR_N_DIMENSION_DST_IP, messages.KZNL_ATTR_N_DIMENSION_SRC_IP6, messages.KZNL_ATTR_N_DIMENSION_DST_IP6 ]: data[dim_type] = ( testutil.addr_packed( rule['entry_values'][dim_type][i]), testutil.netmask_packed( rule['entry_values'][dim_type][i])) else: data[dim_type] = rule['entry_values'][ dim_type][i] message_add_rule_entry = messages.KZorpAddRuleEntryMessage( dispatcher['name'], rule['rule_id'], data) res = self.send_message(message_add_rule_entry, assert_on_error=False) self.assertEqual(res, rule['rule_entry_errnos'][i]) self.end_transaction() self.start_transaction() #check zones exist for i in range(len(error_zones_exist)): dispatcher = error_zones_exist[i] message_add_dispatcher = messages.KZorpAddDispatcherMessage( dispatcher['name'], dispatcher['num_rules']) res = self.send_message(message_add_dispatcher, assert_on_error=False) for rule in dispatcher['rules']: _max = 1 message_add_rule = messages.KZorpAddRuleMessage( dispatcher['name'], rule['rule_id'], rule['service'], rule['entry_nums']) res = self.send_message(message_add_rule, assert_on_error=False) if 'errno' in rule: self.assertEqual(res, rule['errno']) for i in range(_max): data = {} for dim_type in messages.N_DIMENSION_ATTRS: if dim_type in rule['entry_nums']: if dim_type == messages.KZNL_ATTR_N_DIMENSION_SRC_IP or dim_type == messages.KZNL_ATTR_N_DIMENSION_DST_IP: data[dim_type] = (struct.pack( 'I', rule['entry_values'][dim_type][i].ip), struct.pack( 'I', rule['entry_values'] [dim_type][i].mask)) else: data[dim_type] = rule['entry_values'][ dim_type][i] message_add_rule_entry = messages.KZorpAddRuleEntryMessage( dispatcher['name'], rule['rule_id'], data) res = self.send_message(message_add_rule_entry, assert_on_error=False) self.assertEqual(res, rule['rule_entry_errnos'][i]) self.end_transaction()