luci-mod-network: remove unused `iface_down` endpoint
[oweals/luci.git] / protocols / luci-proto-relay / luasrc / model / network / proto_relay.lua
1 -- Copyright 2011 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 local netmod = luci.model.network
5 local device = luci.util.class(netmod.interface)
6
7 netmod:register_pattern_virtual("^relay%-%w")
8
9 local proto = netmod:register_protocol("relay")
10
11 function proto.get_i18n(self)
12         return luci.i18n.translate("Relay bridge")
13 end
14
15 function proto.ifname(self)
16         return "relay-" .. self.sid
17 end
18
19 function proto.opkg_package(self)
20         return "relayd"
21 end
22
23 function proto.is_installed(self)
24         return nixio.fs.access("/etc/init.d/relayd")
25 end
26
27 function proto.is_floating(self)
28         return true
29 end
30
31 function proto.is_virtual(self)
32         return true
33 end
34
35 function proto.is_up(self)
36         local iface = self:get_interface()
37         return iface and iface:is_up() or false
38 end
39
40 function proto.get_interface(self)
41         return device(self.sid, self)
42 end
43
44 function proto.get_interfaces(self)
45         if not self.ifaces then
46                 local ifs = { }
47                 local _, net, dev
48
49                 for net in luci.util.imatch(self:_get("network")) do
50                         net = netmod:get_network(net)
51                         if net then
52                                 dev = net:get_interface()
53                                 if dev then
54                                         ifs[dev:name()] = dev
55                                 end
56                         end
57                 end
58
59                 for dev in luci.util.imatch(self:_get("ifname")) do
60                         dev = netmod:get_interface(dev)
61                         if dev then
62                                 ifs[dev:name()] = dev
63                         end
64                 end
65
66                 self.ifaces = { }
67
68                 for _, dev in luci.util.kspairs(ifs) do
69                         self.ifaces[#self.ifaces+1] = dev
70                 end
71         end
72
73         return self.ifaces
74 end
75
76 function proto.uptime(self)
77         local net
78         local upt = 0
79         for net in luci.util.imatch(self:_get("network")) do
80                 net = netmod:get_network(net)
81                 if net then
82                         upt = math.max(upt, net:uptime())
83                 end
84         end
85         return upt
86 end
87
88 function proto.errors(self)
89         return nil
90 end
91
92
93 function device.__init__(self, ifname, network)
94         self.ifname  = ifname
95         self.network = network
96 end
97
98 function device.type(self)
99         return "tunnel"
100 end
101
102 function device.is_up(self)
103         if self.network then
104                 local _, dev
105                 for _, dev in ipairs(self.network:get_interfaces()) do
106                         if not dev:is_up() then
107                                 return false
108                         end
109                 end
110                 return true
111         end
112         return false
113 end
114
115 function device._stat(self, what)
116         local v = 0
117         if self.network then
118                 local _, dev
119                 for _, dev in ipairs(self.network:get_interfaces()) do
120                         v = v + dev[what](dev)
121                 end
122         end
123         return v
124 end
125
126 function device.rx_bytes(self) return self:_stat("rx_bytes") end
127 function device.tx_bytes(self) return self:_stat("tx_bytes") end
128 function device.rx_packets(self) return self:_stat("rx_packets") end
129 function device.tx_packets(self) return self:_stat("tx_packets") end
130
131 function device.mac(self)
132         if self.network then
133                 local _, dev
134                 for _, dev in ipairs(self.network:get_interfaces()) do
135                         return dev:mac()
136                 end
137         end
138 end
139
140 function device.ipaddrs(self)
141         local addrs = { }
142         if self.network then
143                 addrs[1] = luci.ip.IPv4(self.network:_get("ipaddr"))
144         end
145         return addrs
146 end
147
148 function device.ip6addrs(self)
149         return { }
150 end
151
152 function device.shortname(self)
153         return "%s %q" % { luci.i18n.translate("Relay"), self.ifname }
154 end
155
156 function device.get_type_i18n(self)
157         return luci.i18n.translate("Relay Bridge")
158 end