c2f47d6b2120ca6770e4942db2f00f4f5683bc99
[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, mwan_policy, use_member, last_resort
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 mwan_policy = 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 mwan_policy.addremove = true
50 mwan_policy.dynamic = false
51 mwan_policy.sectionhead = translate("Policy")
52 mwan_policy.sortable = true
53 mwan_policy.template = "cbi/tblsection"
54 mwan_policy.extedit = dsp.build_url("admin", "network", "mwan", "policy", "%s")
55 function mwan_policy.create(self, section)
56 TypedSection.create(self, section)
57 m.uci:save("mwan3")
58 luci.http.redirect(dsp.build_url("admin", "network", "mwan", "policy", section))
59 end
60
61 use_member = mwan_policy:option(DummyValue, "use_member", translate("Members assigned"))
62 use_member.rawhtml = true
63 function use_member.cfgvalue(self, s)
64 local memberConfig, memberList = self.map:get(s, "use_member"), ""
65 if memberConfig then
66 for k,v in pairs(memberConfig) do
67 memberList = memberList .. v .. "<br />"
68 end
69 return memberList
70 else
71 return "&#8212;"
72 end
73 end
74
75 last_resort = mwan_policy:option(DummyValue, "last_resort", translate("Last resort"))
76 last_resort.rawhtml = true
77 function last_resort.cfgvalue(self, s)
78 local action = self.map:get(s, "last_resort")
79 if action == "blackhole" then
80 return translate("blackhole (drop)")
81 elseif action == "default" then
82 return translate("default (use main routing table)")
83 else
84 return translate("unreachable (reject)")
85 end
86 end
87
88 return m