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