6de5ae82bc3a92abc0a60df90a7ba80d03981298
[project/luci.git] / applications / luci-app-mwan3 / luasrc / model / cbi / mwan / policy.lua
1 -- ------ extra functions ------ --
2
3 function policyCheck() -- check to see if any policy names exceed the maximum of 15 characters
4         uci.cursor():foreach("mwan3", "policy",
5                 function (section)
6                         if string.len(section[".name"]) > 15 then
7                                 nameTooLong = 1
8                                 err_name_list = err_name_list .. section[".name"] .. " "
9                         end
10                 end
11         )
12 end
13
14 function policyWarn() -- display status and warning messages at the top of the page
15         if nameTooLong == 1 then
16                 return "<font color=\"ff0000\"><strong>" .. translate("WARNING: Some policies have names exceeding the maximum of 15 characters!") .. "</strong></font>"
17         else
18                 return ""
19         end
20 end
21
22 -- ------ policy configuration ------ --
23
24 ds = require "luci.dispatcher"
25 sys = require "luci.sys"
26
27 nameTooLong = 0
28 err_name_list = " "
29 policyCheck()
30
31
32 m5 = Map("mwan3", translate("MWAN - Policies"),
33         policyWarn())
34
35
36 mwan_policy = m5:section(TypedSection, "policy", nil,
37         translate("Policies are profiles grouping one or more members controlling how MWAN distributes traffic<br />" ..
38         "Member interfaces with lower metrics are used first. Interfaces with the same metric load-balance<br />" ..
39         "Load-balanced member interfaces distribute more traffic out those with higher weights<br />" ..
40         "Names may contain characters A-Z, a-z, 0-9, _ and no spaces. Names must be 15 characters or less<br />" ..
41         "Policies may not share the same name as configured interfaces, members or rules"))
42         mwan_policy.addremove = true
43         mwan_policy.dynamic = false
44         mwan_policy.sectionhead = translate("Policy")
45         mwan_policy.sortable = true
46         mwan_policy.template = "cbi/tblsection"
47         mwan_policy.extedit = ds.build_url("admin", "network", "mwan", "policy", "%s")
48         function mwan_policy.create(self, section)
49                 TypedSection.create(self, section)
50                 m5.uci:save("mwan3")
51                 luci.http.redirect(ds.build_url("admin", "network", "mwan", "policy", section))
52         end
53
54
55 use_member = mwan_policy:option(DummyValue, "use_member", translate("Members assigned"))
56         use_member.rawhtml = true
57         function use_member.cfgvalue(self, s)
58                 local memberConfig, memberList = self.map:get(s, "use_member"), ""
59                 if memberConfig then
60                         for k,v in pairs(memberConfig) do
61                                 memberList = memberList .. v .. "<br />"
62                         end
63                         return memberList
64                 else
65                         return "&#8212;"
66                 end
67         end
68
69 last_resort = mwan_policy:option(DummyValue, "last_resort", translate("Last resort"))
70         last_resort.rawhtml = true
71         function last_resort.cfgvalue(self, s)
72                 local action = self.map:get(s, "last_resort")
73                 if action == "blackhole" then
74                         return translate("blackhole (drop)")
75                 elseif action == "default" then
76                         return translate("default (use main routing table)")
77                 else
78                         return translate("unreachable (reject)")
79                 end
80         end
81
82 errors = mwan_policy:option(DummyValue, "errors", translate("Errors"))
83         errors.rawhtml = true
84         function errors.cfgvalue(self, s)
85                 if not string.find(err_name_list, " " .. s .. " ") then
86                         return ""
87                 else
88                         return "<span title=\"Name exceeds 15 characters\"><img src=\"/luci-static/resources/cbi/reset.gif\" alt=\"error\"></img></span>"
89                 end
90         end
91
92
93 return m5