luci-app-mwan3: refactoring warning compilation on the rule config pages
[project/luci.git] / applications / luci-app-mwan3 / luasrc / model / cbi / mwan / rule.lua
1 dsp = require "luci.dispatcher"
2 sys = require "luci.sys"
3 ut = require "luci.util"
4
5 function ruleCheck()
6         local rule_error = {}
7         uci.cursor():foreach("mwan3", "rule",
8                 function (section)
9                         rule_error[section[".name"]] = false
10                         local uci = uci.cursor(nil, "/var/state")
11                         local sourcePort = uci:get("mwan3", section[".name"], "src_port")
12                         local destPort = uci:get("mwan3", section[".name"], "dest_port")
13                         if sourcePort ~= nil or destPort ~= nil then
14                                 local protocol = uci:get("mwan3", section[".name"], "proto")
15                                 if protocol == nil or protocol == "all" then
16                                         rule_error[section[".name"]] = true
17                                 end
18                         end
19                 end
20         )
21         return rule_error
22 end
23
24 function ruleWarn(rule_error)
25         local warnings = ""
26         for i, k in pairs(rule_error) do
27                 if rule_error[i] == true then
28                         warnings = warnings .. string.format("<strong>%s</strong></br>",
29                                 translatef("WARNING: Rule %s have a port configured with no or improper protocol specified!", i)
30                                 )
31                 end
32         end
33
34         return warnings
35 end
36
37 m5 = Map("mwan3", translate("MWAN - Rules"),
38         ruleWarn(ruleCheck())
39         )
40
41 mwan_rule = m5:section(TypedSection, "rule", nil,
42         translate("Rules specify which traffic will use a particular MWAN policy<br />" ..
43         "Rules are based on IP address, port or protocol<br />" ..
44         "Rules are matched from top to bottom<br />" ..
45         "Rules below a matching rule are ignored<br />" ..
46         "Traffic not matching any rule is routed using the main routing table<br />" ..
47         "Traffic destined for known (other than default) networks is handled by the main routing table<br />" ..
48         "Traffic matching a rule, but all WAN interfaces for that policy are down will be blackholed<br />" ..
49         "Names may contain characters A-Z, a-z, 0-9, _ and no spaces<br />" ..
50         "Rules may not share the same name as configured interfaces, members or policies"))
51         mwan_rule.addremove = true
52         mwan_rule.anonymous = false
53         mwan_rule.dynamic = false
54         mwan_rule.sectionhead = translate("Rule")
55         mwan_rule.sortable = true
56         mwan_rule.template = "cbi/tblsection"
57         mwan_rule.extedit = dsp.build_url("admin", "network", "mwan", "rule", "%s")
58         function mwan_rule.create(self, section)
59                 TypedSection.create(self, section)
60                 m5.uci:save("mwan3")
61                 luci.http.redirect(dsp.build_url("admin", "network", "mwan", "rule", section))
62         end
63
64
65 src_ip = mwan_rule:option(DummyValue, "src_ip", translate("Source address"))
66         src_ip.rawhtml = true
67         function src_ip.cfgvalue(self, s)
68                 return self.map:get(s, "src_ip") or "&#8212;"
69         end
70
71 src_port = mwan_rule:option(DummyValue, "src_port", translate("Source port"))
72         src_port.rawhtml = true
73         function src_port.cfgvalue(self, s)
74                 return self.map:get(s, "src_port") or "&#8212;"
75         end
76
77 dest_ip = mwan_rule:option(DummyValue, "dest_ip", translate("Destination address"))
78         dest_ip.rawhtml = true
79         function dest_ip.cfgvalue(self, s)
80                 return self.map:get(s, "dest_ip") or "&#8212;"
81         end
82
83 dest_port = mwan_rule:option(DummyValue, "dest_port", translate("Destination port"))
84         dest_port.rawhtml = true
85         function dest_port.cfgvalue(self, s)
86                 return self.map:get(s, "dest_port") or "&#8212;"
87         end
88
89 proto = mwan_rule:option(DummyValue, "proto", translate("Protocol"))
90         proto.rawhtml = true
91         function proto.cfgvalue(self, s)
92                 return self.map:get(s, "proto") or "all"
93         end
94
95 use_policy = mwan_rule:option(DummyValue, "use_policy", translate("Policy assigned"))
96         use_policy.rawhtml = true
97         function use_policy.cfgvalue(self, s)
98                 return self.map:get(s, "use_policy") or "&#8212;"
99         end
100
101 return m5