modules/admin-full: rewrite vlan setup for new swconfig style config format
[project/luci.git] / modules / admin-full / luasrc / model / cbi / admin_network / vlan.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2010 Jo-Philipp Wich <xm@subsignal.org>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15 m = Map("network", translate("Switch"), translate("The network ports on your router can be combined to several <abbr title=\"Virtual Local Area Network\">VLAN</abbr>s in which computers can communicate directly with each other. <abbr title=\"Virtual Local Area Network\">VLAN</abbr>s are often used to separate different network segments. Often there is by default one Uplink port for a connection to the next greater network like the internet and other ports for a local network."))
16
17 m.uci:foreach("network", "switch",
18         function(x)
19
20                 -- Switch properties
21                 s = m:section(NamedSection, x['.name'], "switch", "Switch: %s" % x['.name'])
22                 s.addremove = false
23
24                 s:option(Flag, "enable", "Enable this switch")
25                         .cfgvalue = function(self, section) return Flag.cfgvalue(self, section) or self.enabled end
26
27                 s:option(Flag, "enable_vlan", "Enable VLAN functionality")
28                         .cfgvalue = function(self, section) return Flag.cfgvalue(self, section) or self.enabled end
29
30                 s:option(Flag, "reset", "Reset switch during setup")
31                         .cfgvalue = function(self, section) return Flag.cfgvalue(self, section) or self.enabled end
32
33
34                 -- VLAN table
35                 s = m:section(TypedSection, "switch_vlan", "VLANs: %s" % x['.name'])
36                 s.template = "cbi/tblsection"
37                 s.rowcolors = true
38                 s.addremove = true
39
40                 s.sectiontitle = function(self, section)
41                         return "VLAN #%d" % (m.uci:get("network", section, "vlan") or 0)
42                 end
43
44                 s.filter = function(self, section)
45                         return m.uci:get("network", section, "device") == x['.name']
46                                 or m.uci:get("network", section, "device") == nil -- needed for just created vlan sections
47                 end
48
49                 s.cfgsections = function(self)
50                         local osections = TypedSection.cfgsections(self)
51                         local sections = { }
52                         local section   
53
54                         for _, section in luci.util.spairs(
55                                 osections,
56                                 function(a, b)
57                                         return (tonumber(m.uci:get("network", osections[a], "vlan")) or 0)
58                                                 <  (tonumber(m.uci:get("network", osections[b], "vlan")) or 0)
59                                 end
60                         ) do
61                                 sections[#sections+1] = section
62                         end
63
64                         return sections
65                 end
66
67                 s.create = function(self, section)
68                         local n = tonumber(section)
69                         if n ~= nil and n >= 0 then
70                                 local sn = "%s_%d" %{ x['.name'], n }
71                                 local rv = TypedSection.create(self, sn)
72                                 m.uci:set("network", sn, "device", x['.name'])
73                                 m.uci:set("network", sn, "vlan", n)
74                                 return rv
75                         end
76                         return nil
77                 end
78
79
80                 p0 = s:option(Flag, "0", "Port 0")
81                 p1 = s:option(Flag, "1", "Port 1")
82                 p2 = s:option(Flag, "2", "Port 2")
83                 p3 = s:option(Flag, "3", "Port 3")
84                 p4 = s:option(Flag, "4", "Port 4")
85                 p5 = s:option(Flag, "5", "CPU"   )
86
87
88                 p0.cfgvalue = function(self, section)
89                         local pts = (m.uci:get("network", section, "ports") or "")
90                         return (pts:match("%f[%w]" .. self.option .. "%f[%W]") and self.enabled or self.disabled)
91                 end
92
93                 p1.cfgvalue = p0.cfgvalue
94                 p2.cfgvalue = p0.cfgvalue
95                 p3.cfgvalue = p0.cfgvalue
96                 p4.cfgvalue = p0.cfgvalue
97                 p5.cfgvalue = p0.cfgvalue
98
99
100                 p0.parse = function(self, section)
101                         local pts = { }
102                         if p0:formvalue(section) then pts[#pts+1] = 0 end
103                         if p1:formvalue(section) then pts[#pts+1] = 1 end
104                         if p2:formvalue(section) then pts[#pts+1] = 2 end
105                         if p3:formvalue(section) then pts[#pts+1] = 3 end
106                         if p4:formvalue(section) then pts[#pts+1] = 4 end
107                         if p5:formvalue(section) then pts[#pts+1] = 5 end
108                         m.uci:set("network", section, "ports", table.concat(pts, " "))
109                 end
110
111                 p1.parse = function() end
112                 p2.parse = p1.parse
113                 p3.parse = p1.parse
114                 p4.parse = p1.parse
115                 p5.parse = p1.parse
116         end
117 )
118
119 return m