Merge pull request #1250 from dibdot/luci-app-travelmate
[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 Policy Configuration"),
33         policyWarn())
34         m5:append(Template("mwan/config_css"))
35
36
37 mwan_policy = m5:section(TypedSection, "policy", translate("Policies"),
38         translate("Policies are profiles grouping one or more members controlling how MWAN distributes traffic<br />" ..
39         "Member interfaces with lower metrics are used first. Interfaces with the same metric load-balance<br />" ..
40         "Load-balanced member interfaces distribute more traffic out those with higher weights<br />" ..
41         "Names may contain characters A-Z, a-z, 0-9, _ and no spaces. Names must be 15 characters or less<br />" ..
42         "Policies may not share the same name as configured interfaces, members or rules"))
43         mwan_policy.addremove = true
44         mwan_policy.dynamic = false
45         mwan_policy.sectionhead = translate("Policy")
46         mwan_policy.sortable = true
47         mwan_policy.template = "cbi/tblsection"
48         mwan_policy.extedit = ds.build_url("admin", "network", "mwan", "configuration", "policy", "%s")
49         function mwan_policy.create(self, section)
50                 TypedSection.create(self, section)
51                 m5.uci:save("mwan3")
52                 luci.http.redirect(ds.build_url("admin", "network", "mwan", "configuration", "policy", section))
53         end
54
55
56 use_member = mwan_policy:option(DummyValue, "use_member", translate("Members assigned"))
57         use_member.rawhtml = true
58         function use_member.cfgvalue(self, s)
59                 local memberConfig, memberList = self.map:get(s, "use_member"), ""
60                 if memberConfig then
61                         for k,v in pairs(memberConfig) do
62                                 memberList = memberList .. v .. "<br />"
63                         end
64                         return memberList
65                 else
66                         return "&#8212;"
67                 end
68         end
69
70 last_resort = mwan_policy:option(DummyValue, "last_resort", translate("Last resort"))
71         last_resort.rawhtml = true
72         function last_resort.cfgvalue(self, s)
73                 local action = self.map:get(s, "last_resort")
74                 if action == "blackhole" then
75                         return translate("blackhole (drop)")
76                 elseif action == "default" then
77                         return translate("default (use main routing table)")
78                 else
79                         return translate("unreachable (reject)")
80                 end
81         end
82
83 errors = mwan_policy:option(DummyValue, "errors", translate("Errors"))
84         errors.rawhtml = true
85         function errors.cfgvalue(self, s)
86                 if not string.find(err_name_list, " " .. s .. " ") then
87                         return ""
88                 else
89                         return "<span title=\"Name exceeds 15 characters\"><img src=\"/luci-static/resources/cbi/reset.gif\" alt=\"error\"></img></span>"
90                 end
91         end
92
93
94 return m5