Developing lightweight computation at the DSG edge

bmx6.lua 7.09 KB
Newer Older
1
--[[
2
3
    Copyright (C) 2011 Pau Escrich <pau@dabax.net>
    Contributors Jo-Philipp Wich <xm@subsignal.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

    The full GNU General Public License is included in this distribution in
    the file called "COPYING".
--]]

23
24
local bmx6json = require("luci.model.bmx6json")

hitz's avatar
hitz committed
25
module("luci.controller.bmx6", package.seeall)
26

hitz's avatar
hitz committed
27
function index()
28
	local place = {}
29
30
31
32
33
34
	local ucim = require "luci.model.uci"
	local uci = ucim.cursor()
	-- checking if ignore is on
	if uci:get("luci-bmx6","luci","ignore") == "1" then
		return nil
	end
35

36
37
	-- getting value from uci database
	local uci_place = uci:get("luci-bmx6","luci","place")
38

39
	-- default values
40
41
42
	if uci_place == nil then
		place = {"bmx6"}
	else
hitz's avatar
hitz committed
43
44
		local util = require "luci.util"
		place = util.split(uci_place," ")
hitz's avatar
hitz committed
45
	end
46
47
48
	---------------------------
	-- Starting with the pages
	---------------------------
49

p4u's avatar
p4u committed
50
	--- neighbours/descriptions (default)
51
52
53
	entry(place,call("action_neighbours_j"),place[#place])

	table.insert(place,"neighbours_nojs")
p4u's avatar
p4u committed
54
55
	entry(place, call("action_neighbours"), nil)
	table.remove(place)
56

57
	--- status (this is default one)
p4u's avatar
p4u committed
58
	table.insert(place,"Status")
59
	entry(place,call("action_status"),"Status")
hitz's avatar
hitz committed
60
	table.remove(place)
p4u's avatar
p4u committed
61

p4u's avatar
p4u committed
62
63
	--- links
	table.insert(place,"Links")
p4u's avatar
p4u committed
64
	entry(place,call("action_links"),"Links").leaf = true
hitz's avatar
hitz committed
65
	table.remove(place)
66

67
68
69
70
71
	-- Gateways
	table.insert(place,"Gateways")
	entry(place,call("action_gateways_j"),"Gateways").leaf = true
	table.remove(place)

p4u's avatar
p4u committed
72
73
	--- chat
	table.insert(place,"Chat")
74
	entry(place,call("action_chat"),"Chat")
p4u's avatar
p4u committed
75
76
	table.remove(place)

p4u's avatar
p4u committed
77
	--- Graph
78
	table.insert(place,"Graph")
p4u's avatar
p4u committed
79
80
81
82
	entry(place, template("bmx6/graph"), "Graph")
	table.remove(place)

	--- Topology (hidden)
83
	table.insert(place,"topology")
p4u's avatar
p4u committed
84
85
	entry(place, call("action_topology"), nil)
	table.remove(place)
86

87
88
89
90
	--- configuration (CBI)
	table.insert(place,"Configuration")
	entry(place, cbi("bmx6/main"), "Configuration").dependent=false

91
	table.insert(place,"Advanced")
92
93
	entry(place, cbi("bmx6/advanced"), "Advanced")
	table.remove(place)
94
95

	table.insert(place,"Interfaces")
96
97
98
	entry(place, cbi("bmx6/interfaces"), "Interfaces")
	table.remove(place)

99
	table.insert(place,"Plugins")
100
101
102
	entry(place, cbi("bmx6/plugins"), "Plugins")
	table.remove(place)

103
	table.insert(place,"HNA")
104
105
	entry(place, cbi("bmx6/hna"), "HNA")
	table.remove(place)
106
107

	table.remove(place)
108

hitz's avatar
hitz committed
109
end
110

hitz's avatar
hitz committed
111
function action_status()
p4u's avatar
p4u committed
112
113
114
115
		local status = bmx6json.get("status").status or nil
		local interfaces = bmx6json.get("interfaces").interfaces or nil

		if status == nil or interfaces == nil then
116
			luci.template.render("bmx6/error", {txt="Cannot fetch data from bmx6 json"})
p4u's avatar
p4u committed
117
118
119
		else
        	luci.template.render("bmx6/status", {status=status,interfaces=interfaces})
		end
hitz's avatar
hitz committed
120
end
121

hitz's avatar
hitz committed
122
function action_neighbours()
123
		local orig_list = bmx6json.get("originators").originators or nil
p4u's avatar
p4u committed
124

125
		if orig_list == nil then
p4u's avatar
p4u committed
126
127
128
129
			luci.template.render("bmx6/error", {txt="Cannot fetch data from bmx6 json"})
			return nil
		end

130
131
132
133
		local originators = {}
		local desc = nil
		local orig = nil
		local name = ""
p4u's avatar
p4u committed
134
		local ipv4 = ""
135

136
137
138
		for _,o in ipairs(orig_list) do
			orig = bmx6json.get("originators/"..o.name) or {}
			desc = bmx6json.get("descriptions/"..o.name) or {}
139

140
141
142
143
144
			if string.find(o.name,'.') then
				name = luci.util.split(o.name,'.')[1]
			else
				name = o.name
			end
145
146

			--Not sure about that, but trying to find main ipv4 from HNA6 published by each node
147
148
			if desc.DESC_ADV ~= nil then
				for _,h in ipairs(desc.DESC_ADV.extensions[2].HNA6_EXTENSION) do
149

150
151
					if h ~= nil and  string.find(h.address,"::ffff:") then
						ipv4=string.gsub(h.address,"::ffff:","")
152
						break
153
					end
154
				end
155
			end
156
157

			if ipv4 == "" then
158
				ipv4="0.0.0.0"
p4u's avatar
p4u committed
159
			end
160

p4u's avatar
p4u committed
161
			table.insert(originators,{name=name,ipv4=ipv4,orig=orig,desc=desc})
p4u's avatar
p4u committed
162
163
		end

164
        luci.template.render("bmx6/neighbours", {originators=originators})
hitz's avatar
hitz committed
165
end
p4u's avatar
p4u committed
166
167
168

function action_neighbours_j()
	local http = require "luci.http"
p4u's avatar
p4u committed
169
	local link_non_js = "/cgi-bin/luci" .. http.getenv("PATH_INFO") .. '/neighbours_nojs'
p4u's avatar
p4u committed
170
171
172
173

	luci.template.render("bmx6/neighbours_j", {link_non_js=link_non_js})
end

174
175
176
177
178
function action_gateways_j()
	luci.template.render("bmx6/gateways_j", {})
end


p4u's avatar
p4u committed
179
180
function action_links(host)
	local links = bmx6json.get("links", host)
p4u's avatar
p4u committed
181
	local devlinks = {}
p4u's avatar
p4u committed
182
183
	local _,l

p4u's avatar
p4u committed
184
185
	if links ~= nil then
		links = links.links
p4u's avatar
p4u committed
186
187
188
189
		for _,l in ipairs(links) do
			devlinks[l.viaDev] = {}
		end
		for _,l in ipairs(links) do
p4u's avatar
p4u committed
190
			l.globalId = luci.util.split(l.globalId,'.')[1]
191
			table.insert(devlinks[l.viaDev],l)
p4u's avatar
p4u committed
192
		end
p4u's avatar
p4u committed
193
	end
p4u's avatar
p4u committed
194

195
	luci.template.render("bmx6/links", {links=devlinks})
hitz's avatar
hitz committed
196
197
end

p4u's avatar
p4u committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
function action_topology()
	local originators = bmx6json.get("originators/all")
	local o,i,l,i2
	local first = true
	luci.http.prepare_content("application/json")
	luci.http.write('[ ')

	for i,o in ipairs(originators) do
		local links = bmx6json.get("links",o.primaryIp)
		if links then
			if first then
				first = false
			else
				luci.http.write(', ')
			end

			luci.http.write('{ "globalId": "%s", "links": [' %o.globalId:match("^[^%.]+"))
215

p4u's avatar
p4u committed
216
217
218
219
220
221
222
223
			local first2 = true

			for i2,l in ipairs(links.links) do
				if first2 then
					first2 = false
				else
					luci.http.write(', ')
				end
224

p4u's avatar
p4u committed
225
226
227
228
				luci.http.write('{ "globalId": "%s", "rxRate": %s, "txRate": %s }'
					%{ l.globalId:match("^[^%.]+"), l.rxRate, l.txRate })

			end
229

p4u's avatar
p4u committed
230
231
232
233
234
235
236
237
			luci.http.write(']}')
		end

	end
	luci.http.write(' ]')
end


p4u's avatar
p4u committed
238
function action_chat()
p4u's avatar
p4u committed
239
240
241
	local sms_dir = "/var/run/bmx6/sms"
	local rcvd_dir = sms_dir .. "/rcvdSms"
	local send_file = sms_dir .. "/sendSms/chat"
p4u's avatar
p4u committed
242
	local sms_list = bmx6json.get("rcvdSms")
243
244
	local sender = ""
	local sms_file = ""
p4u's avatar
p4u committed
245
246
247
248
249
	local chat = {}
	local to_send = nil
	local sent = ""
	local fd = nil

p4u's avatar
p4u committed
250
251
252
253
254
	if luci.sys.call("test -d " .. sms_dir) ~= 0 then
		luci.template.render("bmx6/error", {txt="sms plugin disabled or some problem with directory " .. sms_dir})
		return nil
	end

255
256
257
258
259
260
261
262
263
264
265
	sms_list = luci.util.split(luci.util.exec("ls "..rcvd_dir.."/*:chat"))

	for _,sms_path in ipairs(sms_list) do
	  if #sms_path > #rcvd_dir then
		sms_file = luci.util.split(sms_path,'/')
		sms_file = sms_file[#sms_file]
		sender = luci.util.split(sms_file,':')[1]

		-- Trying to clean the name
		if string.find(sender,".") ~= nil then
			sender = luci.util.split(sender,".")[1]
p4u's avatar
p4u committed
266
		end
267
268
269

		fd = io.open(sms_path,"r")
		chat[sender] = fd:read()
270
		fd:close()
271
	  end
p4u's avatar
p4u committed
272
273
	end

274
	to_send = luci.http.formvalue("toSend")
p4u's avatar
p4u committed
275
276
277
278
279
280
281
282
283
284
285
286
	if to_send ~= nil and #to_send > 1  then
		fd = io.open(send_file,"w")
		fd:write(to_send)
		fd:close()
		sent = to_send
	else
		sent = luci.util.exec("cat "..send_file)
	end

	luci.template.render("bmx6/chat", {chat=chat,sent=sent})
end