luci-app-mwan3: cleanup policy cbi
[project/luci.git] / applications / luci-app-mwan3 / luasrc / model / cbi / mwan / policy.lua
1 -- Copyright 2014 Aedan Renner <chipdankly@gmail.com>
2 -- Copyright 2018 Florian Eckert <fe@dev.tdt.de>
3 -- Licensed to the public under the GNU General Public License v2.
4
5 local dsp = require "luci.dispatcher"
6 local uci = require "uci"
7
8 local m, s, o
9
10 function policyCheck()
11 local policy_error = {}
12
13 uci.cursor():foreach("mwan3", "policy",
14 function (section)
15 policy_error[section[".name"]] = false
16 if string.len(section[".name"]) > 15 then
17 policy_error[section[".name"]] = true
18 end
19 end
20 )
21
22 return policy_error
23 end
24
25 function policyError(policy_error)
26 local warnings = ""
27 for i, k in pairs(policy_error) do
28 if policy_error[i] == true then
29 warnings = warnings .. string.format("<strong>%s</strong><br />",
30 translatef("WARNING: Policy %s has exceeding the maximum name of 15 characters", i)
31 )
32 end
33 end
34
35 return warnings
36 end
37
38 m = Map("mwan3", translate("MWAN - Policies"),
39 policyError(policyCheck()))
40
41 s = m:section(TypedSection, "policy", nil,
42 translate("Policies are profiles grouping one or more members controlling how MWAN distributes traffic<br />" ..
43 "Member interfaces with lower metrics are used first<br />" ..
44 "Member interfaces with the same metric will be load-balanced<br />" ..
45 "Load-balanced member interfaces distribute more traffic out those with higher weights<br />" ..
46 "Names may contain characters A-Z, a-z, 0-9, _ and no spaces<br />" ..
47 "Names must be 15 characters or less<br />" ..
48 "Policies may not share the same name as configured interfaces, members or rules"))
49 s.addremove = true
50 s.dynamic = false
51 s.sectionhead = translate("Policy")
52 s.sortable = true
53 s.template = "cbi/tblsection"
54 s.extedit = dsp.build_url("admin", "network", "mwan", "policy", "%s")
55 function s.create(self, section)
56 if #section > 15 then
57 self.invalid_cts = true
58 else
59 TypedSection.create(self, section)
60 m.uci:save("mwan3")
61 luci.http.redirect(dsp.build_url("admin", "network", "mwan", "policy", section))
62 end
63 end
64
65 o = s:option(DummyValue, "use_member", translate("Members assigned"))
66 o.rawhtml = true
67 function o.cfgvalue(self, s)
68 local memberConfig, memberList = self.map:get(s, "use_member"), ""
69 if memberConfig then
70 for k,v in pairs(memberConfig) do
71 memberList = memberList .. v .. "<br />"
72 end
73 return memberList
74 else
75 return "&#8212;"
76 end
77 end
78
79 o = s:option(DummyValue, "last_resort", translate("Last resort"))
80 o.rawhtml = true
81 function o.cfgvalue(self, s)
82 local action = self.map:get(s, "last_resort")
83 if action == "blackhole" then
84 return translate("blackhole (drop)")
85 elseif action == "default" then
86 return translate("default (use main routing table)")
87 else
88 return translate("unreachable (reject)")
89 end
90 end
91
92 return m