Developing lightweight computation at the DSG edge

bmx6.lua 6.89 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

p4u's avatar
p4u committed
67
68
	--- chat
	table.insert(place,"Chat")
69
	entry(place,call("action_chat"),"Chat")
p4u's avatar
p4u committed
70
71
	table.remove(place)

p4u's avatar
p4u committed
72
	--- Graph
73
	table.insert(place,"Graph")
p4u's avatar
p4u committed
74
75
76
77
	entry(place, template("bmx6/graph"), "Graph")
	table.remove(place)

	--- Topology (hidden)
78
	table.insert(place,"topology")
p4u's avatar
p4u committed
79
80
	entry(place, call("action_topology"), nil)
	table.remove(place)
81

82
83
84
85
	--- configuration (CBI)
	table.insert(place,"Configuration")
	entry(place, cbi("bmx6/main"), "Configuration").dependent=false

86
	table.insert(place,"Advanced")
87
88
	entry(place, cbi("bmx6/advanced"), "Advanced")
	table.remove(place)
89
90

	table.insert(place,"Interfaces")
91
92
93
	entry(place, cbi("bmx6/interfaces"), "Interfaces")
	table.remove(place)

94
	table.insert(place,"Plugins")
95
96
97
	entry(place, cbi("bmx6/plugins"), "Plugins")
	table.remove(place)

98
	table.insert(place,"HNA")
99
100
	entry(place, cbi("bmx6/hna"), "HNA")
	table.remove(place)
101
102

	table.remove(place)
103

hitz's avatar
hitz committed
104
end
105

hitz's avatar
hitz committed
106
function action_status()
p4u's avatar
p4u committed
107
108
109
110
		local status = bmx6json.get("status").status or nil
		local interfaces = bmx6json.get("interfaces").interfaces or nil

		if status == nil or interfaces == nil then
111
			luci.template.render("bmx6/error", {txt="Cannot fetch data from bmx6 json"})
p4u's avatar
p4u committed
112
113
114
		else
        	luci.template.render("bmx6/status", {status=status,interfaces=interfaces})
		end
hitz's avatar
hitz committed
115
end
116

hitz's avatar
hitz committed
117
function action_neighbours()
118
		local orig_list = bmx6json.get("originators").originators or nil
p4u's avatar
p4u committed
119

120
		if orig_list == nil then
p4u's avatar
p4u committed
121
122
123
124
			luci.template.render("bmx6/error", {txt="Cannot fetch data from bmx6 json"})
			return nil
		end

125
126
127
128
		local originators = {}
		local desc = nil
		local orig = nil
		local name = ""
p4u's avatar
p4u committed
129
		local ipv4 = ""
130

131
132
133
		for _,o in ipairs(orig_list) do
			orig = bmx6json.get("originators/"..o.name) or {}
			desc = bmx6json.get("descriptions/"..o.name) or {}
134

135
136
137
138
139
			if string.find(o.name,'.') then
				name = luci.util.split(o.name,'.')[1]
			else
				name = o.name
			end
140
141

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

145
146
					if h ~= nil and  string.find(h.address,"::ffff:") then
						ipv4=string.gsub(h.address,"::ffff:","")
147
						break
148
					end
149
				end
150
			end
151
152

			if ipv4 == "" then
153
				ipv4="0.0.0.0"
p4u's avatar
p4u committed
154
			end
155

p4u's avatar
p4u committed
156
			table.insert(originators,{name=name,ipv4=ipv4,orig=orig,desc=desc})
p4u's avatar
p4u committed
157
158
		end

159
        luci.template.render("bmx6/neighbours", {originators=originators})
hitz's avatar
hitz committed
160
end
p4u's avatar
p4u committed
161
162
163

function action_neighbours_j()
	local http = require "luci.http"
p4u's avatar
p4u committed
164
	local link_non_js = "/cgi-bin/luci" .. http.getenv("PATH_INFO") .. '/neighbours_nojs'
p4u's avatar
p4u committed
165
166
167
168
169
170

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

function action_links(host)
	local links = bmx6json.get("links", host)
p4u's avatar
p4u committed
171
	local devlinks = {}
p4u's avatar
p4u committed
172
173
	local _,l

p4u's avatar
p4u committed
174
175
	if links ~= nil then
		links = links.links
p4u's avatar
p4u committed
176
177
178
179
		for _,l in ipairs(links) do
			devlinks[l.viaDev] = {}
		end
		for _,l in ipairs(links) do
p4u's avatar
p4u committed
180
			l.globalId = luci.util.split(l.globalId,'.')[1]
181
			table.insert(devlinks[l.viaDev],l)
p4u's avatar
p4u committed
182
		end
p4u's avatar
p4u committed
183
	end
p4u's avatar
p4u committed
184

185
	luci.template.render("bmx6/links", {links=devlinks})
hitz's avatar
hitz committed
186
187
end

p4u's avatar
p4u committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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("^[^%.]+"))
205

p4u's avatar
p4u committed
206
207
208
209
210
211
212
213
			local first2 = true

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

p4u's avatar
p4u committed
215
216
217
218
				luci.http.write('{ "globalId": "%s", "rxRate": %s, "txRate": %s }'
					%{ l.globalId:match("^[^%.]+"), l.rxRate, l.txRate })

			end
219

p4u's avatar
p4u committed
220
221
222
223
224
225
226
227
			luci.http.write(']}')
		end

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


p4u's avatar
p4u committed
228
function action_chat()
p4u's avatar
p4u committed
229
230
231
	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
232
	local sms_list = bmx6json.get("rcvdSms")
233
234
	local sender = ""
	local sms_file = ""
p4u's avatar
p4u committed
235
236
237
238
239
	local chat = {}
	local to_send = nil
	local sent = ""
	local fd = nil

p4u's avatar
p4u committed
240
241
242
243
244
	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

245
246
247
248
249
250
251
252
253
254
255
	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
256
		end
257
258
259

		fd = io.open(sms_path,"r")
		chat[sender] = fd:read()
260
		fd:close()
261
	  end
p4u's avatar
p4u committed
262
263
	end

264
	to_send = luci.http.formvalue("toSend")
p4u's avatar
p4u committed
265
266
267
268
269
270
271
272
273
274
275
276
	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