From ac461281684432c04c37b312d5d61aefd1532143 Mon Sep 17 00:00:00 2001 From: awlx Date: Tue, 29 Nov 2016 09:56:23 +0100 Subject: [PATCH] Initial commit --- napalm_fortios/fortios.py | 45 +++++++++++++++++++ .../normal/expected_result.json | 1 + .../normal/show_firewall_policy.txt | 23 ++++++++++ 3 files changed, 69 insertions(+) create mode 100644 test/unit/mocked_data/test_get_firewall_policies/normal/expected_result.json create mode 100644 test/unit/mocked_data/test_get_firewall_policies/normal/show_firewall_policy.txt diff --git a/napalm_fortios/fortios.py b/napalm_fortios/fortios.py index 365e6f5..5663533 100644 --- a/napalm_fortios/fortios.py +++ b/napalm_fortios/fortios.py @@ -261,6 +261,51 @@ class FortiOSDriver(NetworkDriver): if search in l: return l + def get_firewall_policies(self): + cmd = self.execute_command_with_vdom('show firewall policy') + policy = dict() + policy_id = None + default_policy = dict() + position = 1 + + for line in cmd: + policy_data = line.strip() + if policy_data.find("edit") == 0: + policy_id = policy_data.split()[1] + policy[policy_id] = dict() + if policy_id is not None: + if len(policy_data.split()) > 2: + policy_setting = policy_data.split()[1] + policy[policy_id][policy_setting] = policy_data.split()[2].replace("\"", "") + + for key in policy: + + enabled = 'status' in policy[key] + + logtraffic = policy[key]['logtraffic'] if 'logtraffic' in policy[key] else False + + action = 'permit' if 'action' in policy[key] else 'reject' + + policy_item = dict() + default_policy[key] = list() + policy_item['position'] = position + policy_item['packet_hits'] = -1 + policy_item['byte_hits'] = -1 + policy_item['id'] = unicode(key) + policy_item['enabled'] = enabled + policy_item['schedule'] = unicode(policy[key]['schedule']) + policy_item['log'] = unicode(logtraffic) + policy_item['l3_src'] = unicode(policy[key]['srcaddr']) + policy_item['l3_dst'] = unicode(policy[key]['dstaddr']) + policy_item['service'] = unicode(policy[key]['service']) + policy_item['src_zone'] = unicode(policy[key]['srcintf']) + policy_item['dst_zone'] = unicode(policy[key]['dstintf']) + policy_item['action'] = unicode(action) + default_policy[key].append(policy_item) + + position = position + 1 + return default_policy + def get_bgp_neighbors(self): families = ['ipv4', 'ipv6'] diff --git a/test/unit/mocked_data/test_get_firewall_policies/normal/expected_result.json b/test/unit/mocked_data/test_get_firewall_policies/normal/expected_result.json new file mode 100644 index 0000000..07f8e47 --- /dev/null +++ b/test/unit/mocked_data/test_get_firewall_policies/normal/expected_result.json @@ -0,0 +1 @@ +{'1': [{'src_zone': u'any', 'log': u'False', 'service': u'ALL', 'schedule': u'always', 'l3_src': u'all', 'enabled': False, 'packet_hits': -1, 'l3_dst': u'all', 'dst_zone': u'any', 'action': u'permit', 'position': 1, 'byte_hits': -1, 'id': u'1'}], '2': [{'src_zone': u'port3', 'log': u'all', 'service': u'ALL', 'schedule': u'always', 'l3_src': u'all', 'enabled': False, 'packet_hits': -1, 'l3_dst': u'all', 'dst_zone': u'port2', 'action': u'reject', 'position': 2, 'byte_hits': -1, 'id': u'2'}]} diff --git a/test/unit/mocked_data/test_get_firewall_policies/normal/show_firewall_policy.txt b/test/unit/mocked_data/test_get_firewall_policies/normal/show_firewall_policy.txt new file mode 100644 index 0000000..996a5d7 --- /dev/null +++ b/test/unit/mocked_data/test_get_firewall_policies/normal/show_firewall_policy.txt @@ -0,0 +1,23 @@ +config firewall policy + edit 1 + set uuid 9d747de8-dad4-51e5-7a7e-d078aa77140a + set srcintf "any" + set dstintf "any" + set srcaddr "all" + set dstaddr "all" + set action accept + set schedule "always" + set service "ALL" + next + edit 2 + set name "bla" + set uuid de597b64-aca8-51e6-5d00-b7874e6b72b8 + set srcintf "port3" + set dstintf "port2" + set srcaddr "all" + set dstaddr "all" + set schedule "always" + set service "ALL" + set logtraffic all + next +end