Koray Kokten | 8592a23 | 2018-08-27 07:41:14 +0000 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2017 the original author or authors. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | |
| 17 | |
| 18 | from unittest import main, TestCase |
| 19 | import structlog |
| 20 | from loxi.of13.message import meter_mod |
| 21 | from ofagent.converter import to_grpc |
| 22 | from ofagent.loxi.of13 import const |
| 23 | from random import randint |
| 24 | from protobuf_to_dict import protobuf_to_dict |
| 25 | from ofagent.loxi.of13.message import meter_mod |
| 26 | |
| 27 | log = structlog.get_logger() |
| 28 | |
| 29 | |
| 30 | class test_of_agent_vcore_connection(TestCase): |
| 31 | |
| 32 | def test_meter_mod_loxi_to_grpc_converter(self): |
| 33 | from ofagent.loxi.of13 import meter_band |
| 34 | |
| 35 | assertion_check_keys = dict() |
| 36 | meter_bands = list() |
| 37 | |
| 38 | command_list = [const.OFPMC_ADD, const.OFPMC_MODIFY, const.OFPMC_DELETE] |
| 39 | flag_list = [const.OFPMF_KBPS, const.OFPMF_PKTPS, const.OFPMF_BURST, const.OFPMF_STATS] |
| 40 | meter_band_constructs = [meter_band.dscp_remark, meter_band.drop, meter_band.experimenter] |
| 41 | |
| 42 | assertion_check_keys['meter_band_entry_cnt'] = randint(0, 20) |
| 43 | assertion_check_keys['xid_instance'] = randint(0, 0xFFFFFFFF) |
| 44 | assertion_check_keys['command_instance'] = command_list[randint(0, len(command_list) - 1)] |
| 45 | assertion_check_keys['flag_instance'] = flag_list[randint(0, len(flag_list) - 1)] |
| 46 | |
| 47 | for i in range(0, assertion_check_keys['meter_band_entry_cnt']): |
| 48 | meter_band = meter_band_constructs[randint(0, len(meter_band_constructs)-1)]() |
| 49 | assertion_check_keys['meter_band_type_' + str(i)] = meter_band.type |
| 50 | meter_bands.append(meter_band) |
| 51 | |
| 52 | of_meter_mod_req = meter_mod(xid=assertion_check_keys['xid_instance'], |
| 53 | command=assertion_check_keys['command_instance'], |
| 54 | flags=assertion_check_keys['flag_instance'], |
| 55 | meters=meter_bands) |
| 56 | |
| 57 | req = to_grpc(of_meter_mod_req) |
| 58 | request = protobuf_to_dict(req) |
| 59 | |
| 60 | if request.has_key('flags'): |
| 61 | self.assertEqual(request['flags'], assertion_check_keys['flag_instance']) |
| 62 | |
| 63 | if request.has_key('command'): |
| 64 | self.assertEqual(request['command'], assertion_check_keys['command_instance']) |
| 65 | |
| 66 | if request.has_key('bands'): |
| 67 | self.assertEqual(assertion_check_keys['meter_band_entry_cnt'], len(request['bands'])) |
| 68 | |
| 69 | name_suffix = 0 |
| 70 | for i in request['bands']: |
| 71 | self.assertEqual(i['type'], assertion_check_keys['meter_band_type_' + str(name_suffix)]) |
| 72 | name_suffix = name_suffix+1 |
| 73 | |
| 74 | |
| 75 | if __name__ == '__main__': |
| 76 | main() |