Merge pull request #54 from eloicaso/master
[feed/routing.git] / bird-openwrt / bird4-openwrt / src / model / gen_proto.lua
1 --[[
2 Copyright (C) 2014 - Eloi Carbó Solé (GSoC2014)
3 BGP/Bird integration with OpenWRT and QMP
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 --]]
18
19 require("luci.sys")
20 local http = require "luci.http"
21 local uci = require "luci.model.uci"
22 local uciout = uci.cursor()
23
24 m=Map("bird4", "Bird4 general protocol's configuration.")
25
26 -- Optional parameters lists
27 local protoptions = {
28 {["name"]="table", ["help"]="Auxiliar table for routing", ["depends"]={"static","kernel"}},
29 {["name"]="import", ["help"]="Set if the protocol must import routes", ["depends"]={"kernel"}},
30 {["name"]="export", ["help"]="Set if the protocol must export routes", ["depends"]={"kernel"}},
31 {["name"]="scan_time", ["help"]="Time between scans", ["depends"]={"kernel","device"}},
32 {["name"]="kernel_table", ["help"]="Set which table must be used as auxiliar kernel table", ["depends"]={"kernel"}},
33 {["name"]="learn", ["help"]="Learn routes", ["depends"]={"kernel"}},
34 {["name"]="persist", ["help"]="Store routes. After a restart, routes will be still configured", ["depends"]={"kernel"}}
35 }
36
37 local routeroptions = {
38 {["name"]="prefix",["help"]="",["depends"]={"router","special","iface","multipath","recursive"}},
39 {["name"]="via",["help"]="",["depends"]={"router","multipath"}},
40 {["name"]="attribute",["help"]="",["depends"]={"special"}},
41 {["name"]="iface",["help"]="",["depends"]={"iface"}},
42 {["name"]="ip",["help"]="",["depends"]={"recursive"}}
43 }
44
45 --
46 -- KERNEL PROTOCOL
47 --
48
49 sect_kernel_protos = m:section(TypedSection, "kernel", "Kernel options", "Configuration of the kernel protocols. First Instance MUST be Primary table (no table or kernel_table fields).")
50 sect_kernel_protos.addremove = true
51 sect_kernel_protos.anonymous = false
52
53 -- Default kernel parameters
54
55 disabled = sect_kernel_protos:option(Flag, "disabled", "Disabled", "If this option is true, the protocol will not be configured.")
56 disabled.default=0
57
58 -- Optional parameters
59 for _,o in ipairs(protoptions) do
60 if o.name ~= nil then
61 for _, d in ipairs(o.depends) do
62 if d == "kernel" then
63 if o.name == "learn" or o.name == "persist" then
64 value = sect_kernel_protos:option(Flag, o.name, translate(o.name), translate(o.help))
65 elseif o.name == "table" then
66 value = sect_kernel_protos:option(ListValue, o.name, translate(o.name), translate(o.help))
67 uciout:foreach("bird4", "table",
68 function (s)
69 value:value(s.name)
70 end)
71 value:value("")
72 else
73 value = sect_kernel_protos:option(Value, o.name, translate(o.name), translate(o.help))
74 end
75 value.optional = true
76 value.rmempty = true
77 end
78 end
79
80 end
81 end
82
83 --
84 -- DEVICE PROTOCOL
85 --
86
87 sect_device_protos = m:section(TypedSection, "device", "Device options", "Configuration of the device protocols.")
88 sect_device_protos.addremove = true
89 sect_device_protos.anonymous = false
90
91 -- Default kernel parameters
92
93 disabled = sect_device_protos:option(Flag, "disabled", "Disabled", "If this option is true, the protocol will not be configured.")
94 disabled.default=0
95
96 -- Optional parameters
97 for _,o in ipairs(protoptions) do
98 if o.name ~= nil then
99 for _, d in ipairs(o.depends) do
100 if d == "device" then
101 value = sect_device_protos:option(Value, o.name, translate(o.name), translate(o.help))
102 value.optional = true
103 value.rmempty = true
104 end
105 end
106 end
107 end
108
109 --
110 -- STATIC PROTOCOL
111 --
112
113 sect_static_protos = m:section(TypedSection, "static", "Static options", "Configuration of the static protocols.")
114 sect_static_protos.addremove = true
115 sect_static_protos.anonymous = false
116
117 -- Default kernel parameters
118
119 disabled = sect_static_protos:option(Flag, "disabled", "Disabled", "If this option is true, the protocol will not be configured.")
120 disabled.default=0
121
122 -- Optional parameters
123 for _,o in ipairs(protoptions) do
124 if o.name ~= nil then
125 for _, d in ipairs(o.depends) do
126 if d == "static" then
127 if o.name == "table" then
128 value = sect_static_protos:option(ListValue, o.name, translate(o.name), translate(o.help))
129 uciout:foreach("bird4", "table",
130 function (s)
131 value:value(s.name)
132 end)
133 value:value("")
134 else
135 value = sect_static_protos:option(Value, o.name, translate(o.name), translate(o.help))
136 end
137 value.optional = true
138 value.rmempty = true
139 end
140 end
141 end
142 end
143
144 --
145 -- ROUTES FOR STATIC PROTOCOL
146 --
147
148
149 sect_routes = m:section(TypedSection, "route", "Routes configuration", "Configuration of the routes used in static protocols.")
150 sect_routes.addremove = true
151 sect_routes.anonymous = true
152
153 instance = sect_routes:option(ListValue, "instance", "Route instance", "")
154 i = 0
155
156 uciout:foreach("bird4", "static",
157 function (s)
158 instance:value(s[".name"])
159 end)
160
161 prefix = sect_routes:option(Value, "prefix", "Route prefix", "")
162
163 type = sect_routes:option(ListValue, "type", "Type of route", "")
164 type:value("router")
165 type:value("special")
166 type:value("iface")
167 type:value("recursive")
168 type:value("multipath")
169
170 valueVia = sect_routes:option(Value, "via", "Via", "")
171 valueVia.optional = false
172 valueVia:depends("type", "router")
173 valueVia.datatype = "ip4addr"
174
175 listVia = sect_routes:option(DynamicList, "l_via", "Via", "")
176 listVia:depends("type", "multipath")
177 listVia.optional=false
178 listVia.datatype = "ip4addr"
179
180 attribute = sect_routes:option(Value, "attribute", "Attribute", "Types are: unreachable, prohibit and blackhole")
181 attribute:depends("type", "special")
182
183 iface = sect_routes:option(ListValue, "iface", "Interface", "")
184 iface:depends("type", "iface")
185
186 uciout:foreach("wireless", "wifi-iface",
187 function(section)
188 iface:value(section[".name"])
189 end)
190
191 ip = sect_routes:option(Value, "ip", "IP address", "")
192 ip:depends("type", "ip")
193 ip.datatype = [[ or"ip4addr", "ip6addr" ]]
194
195 function m.on_commit(self,map)
196 luci.sys.call('/etc/init.d/bird4 stop; /etc/init.d/bird4 start')
197 end
198
199 return m
200