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