Skip to content

Commit

Permalink
Extend schema and work on converter
Browse files Browse the repository at this point in the history
  • Loading branch information
okraits committed Aug 10, 2018
1 parent 124a300 commit 57c7613
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 16 deletions.
45 changes: 29 additions & 16 deletions netjsonconfig/backends/openwrt/converters/firewall.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,50 @@
from collections import OrderedDict

from ..schema import schema
# from ..schema import schema
from .base import OpenWrtConverter


class Firewall(OpenWrtConverter):
netjson_key = 'firewall'
intermediate_key = 'firewall'
_uci_types = ['defaults', 'forwarding', 'zone']
_schema = schema['properties']['firewall']
# _schema = schema['properties']['firewall']

def to_intermediate_loop(self, block, result, index=None):
if block:
provider_list = self.__intermediate_providers(block.pop('providers', {}))
block.update({
'.type': 'defaults',
'.name': block.pop('id', None),
})
result.setdefault('firewall', [])
result['firewall'] = [self.sorted_dict(block)] + provider_list
print("### block:", block)
forwardings = self.__intermediate_forwardings(block.pop('forwardings', {}))
zones = self.__intermediate_zones(block.pop('zones', {}))
block.update({
'.type': 'defaults'
})
result.setdefault('firewall', [])
result['firewall'] = [self.sorted_dict(block)] + forwardings + zones
return result

def __intermediate_providers(self, providers):
def __intermediate_forwardings(self, forwardings):
"""
converts NetJSON provider to
converts NetJSON forwarding to
UCI intermediate data structure
"""
result = []
for provider in providers:
uci_name = self._get_uci_name(provider['lookup_host'])
for forwarding in forwardings:
resultdict = OrderedDict([('.type', 'forwarding')])
for key, value in forwardings.items():
resultdict[key] = value
result.append(resultdict)
return result

def __intermediate_zones(self, zones):
"""
converts NetJSON zone to
UCI intermediate data structure
"""
result = []
for zone in zones:
uci_name = self._get_uci_name(zone['name'])
resultdict = OrderedDict((('.name', uci_name),
('.type', 'service')))
for key, value in provider.items():
('.type', 'zone')))
for key, value in zone.items():
resultdict[key] = value
result.append(resultdict)
return result
Expand Down
82 changes: 82 additions & 0 deletions netjsonconfig/backends/openwrt/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,88 @@
}
}
}
},
"zones": {
"type": "array",
"title": "Zones",
"propertyOrder": 6,
"items": {
"type": "object",
"title": "Zones",
"additionalProperties": True,
"required": [
"name"
],
"properties": {
"name": {
"type": "string",
"title": "name",
"description": "unique zone name",
"maxLength": 11,
"propertyOrder": 1
},
"network": {
"type": "array",
"title": "Network",
"description": "list of interfaces attached to this zone",
"uniqueItems": True,
"propertyOrder": 2,
"items": {
"title": "Network",
"type": "string",
"maxLength": 15,
"pattern": "^[a-zA-z0-9_\\.\\-]*$"
}
},
"masq": {
"type": "boolean",
"title": "masq",
"description": "specifies wether outgoing zone traffic should be "
"masqueraded",
"default": False,
"format": "checkbox",
"propertyOrder": 3
},
"mtu_fix": {
"type": "boolean",
"title": "mtu_fix",
"description": "enable MSS clamping for outgoing zone traffic",
"default": False,
"format": "checkbox",
"propertyOrder": 4,
},
"input": {
"allOf": [
{"$ref": "#/definitions/zone_policy"},
{
"title": "input",
"description": "default policy for incoming zone traffic",
"propertyOrder": 5,
}
]
},
"output": {
"allOf": [
{"$ref": "#/definitions/zone_policy"},
{
"title": "output",
"description": "default policy for outgoing zone traffic",
"propertyOrder": 6,
}
]
},
"forward": {
"allOf": [
{"$ref": "#/definitions/zone_policy"},
{
"title": "forward",
"description": "default policy for forwarded zone traffic",
"propertyOrder": 7,
}
]
}
}
}
}
}
}
Expand Down

0 comments on commit 57c7613

Please sign in to comment.