-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddMethods.rb
551 lines (502 loc) · 16.1 KB
/
addMethods.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# Copyright (c) 2012 NITLab, University of Thessaly, CERTH, Greece
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# == modular architecture
#
# NITOS API v2
#
module AddMethods
##################################################################
# ADD methods
###################################################################
# Available ADD methods:
# reserveNodes, reserveChannels, addUser, addUserToSlice,
# addUserKey, addSlice, addNode, addChannel
#####################################################################
# Returns the node's id
#####################################################################
def reserveNodes(nodesInfo)
already_reserved = Array.new
final_reserved = Array.new
slice_name = Array.new
nodes = Array.new
slice_id = 0
time1 = 0
time2 = 0
nodesInfo.each_pair { | name, value |
if(name == "slice_id")
slice_id = value
elsif(name == "start_time")
time1 = value.to_i
elsif(name == "end_time")
time2 = value.to_i
elsif(name == "nodes")
nodes = value
else
puts "wrong parameter: #{name}"
return "wrong parameter: #{name}"
end
}
puts slice_id
puts nodes
puts time1
puts time2
time1 = Time.at(time1).to_datetime
time2 = Time.at(time2).to_datetime
# check if the time slot is correct
if (time1.min != 0 && time1.min != 30 && time1.sec !=0)||(time2.min != 0 && time2.min != 30 && time2.sec !=0)
puts "False time value. Minutes and seconds must be 00 or 30"
return -1
end
if (time2.hour-time1.hour > 4)
puts "False time period. The period should be 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4 hours"
return -1
end
if (time2.hour - time1.hour == 4)
if(time2.min != 0 && time2.min != 0)
puts "False time period. The period should be 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4 hours"
return -1
end
end
start_time = time1.strftime("%Y-%m-%d %H:%M:%S")
finish_time = time2.strftime("%Y-%m-%d %H:%M:%S")
puts "Connecting to database..."
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:#{$db}:#{$db_ip}","#{$user}", "#{$pass}")
my_query = dbh.prepare("SELECT node_id FROM reservation WHERE begin_time <= '#{start_time}' AND end_time > '#{start_time}'")
my_query.execute()
i = 0
my_query.fetch do | n |
nodes.each do | id |
if n[0].to_f == id.to_f
already_reserved[i] = id
i += 1
end
end
end
#final_reserved = nodesInfo.values[1] - already_reserved
final_reserved = nodes - already_reserved
# find the slice_name
my_query = dbh.prepare("SELECT slice_name FROM slices WHERE id = '#{slice_id}'")
result = my_query.execute()
my_query.fetch do | name |
slice_name = name
end
#puts slice_name
node_y = 0
now = Time.now
puts "Active time slot...#{now}"
# execute enable_node for reservations in the active time slot
#final_reserved.each do | id |
# if (start_time < now) && (now < finish_time)
# my_query = dbh.prepare("SELECT y FROM node_list WHERE id = '#{id}'")
# result = my_query.execute()
# my_query.fetch do | y |
# node_y = y
# end
#puts node_y
# cmd = `enable_node #{slice_name} #{node_y}`
# end
# end
# insert values into the database
i = 0
final_reserved.each do | id |
puts id
id = id.to_i
puts id
dbh.do("INSERT INTO reservation (username,begin_time,end_time,node_id) VALUES ('#{slice_name[0]}','#{start_time}','#{finish_time}','#{id}')")
final_reserved[i] = id
i += 1
end
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
return final_reserved
end
#####################################################################
# Returns the channel's id
#####################################################################
def reserveChannels(channelInfo)
already_reserved = Array.new
final_reserved = Array.new
slice_name = Array.new
channels = Array.new
slice_id = 0
time1 = 0
time2 = 0
channelInfo.each_pair { | name, value |
if(name == "slice_id")
slice_id = value
elsif(name == "start_time")
time1 = value.to_i
elsif(name == "end_time")
time2 = value.to_i
elsif(name == "channels")
channels = value
else
puts "wrong parameter: #{name}"
return "wrong parameter: #{name}"
end
}
puts slice_id
puts channels
puts time1
puts time2
time1 = Time.at(time1).to_datetime
time2 = Time.at(time2).to_datetime
# check if the time slot is correct
if (time1.min != 0 && time1.min != 30 && time1.sec !=0)||(time2.min != 0 && time2.min != 30 && time2.sec !=0)
puts "Faulse time value. Minutes and seconds must be 00 or 30"
return -1
end
if (time2.hour-time1.hour > 4)
puts "Faulse time period. The period should be 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4 hours"
return -1
end
if (time2.hour - time1.hour == 4)
if(time2.min != 0 && time2.min != 0)
puts "Faulse time period. The period should be 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4 hours"
return -1
end
end
start_time = time1.strftime("%Y-%m-%d %H:%M:%S")
finish_time = time2.strftime("%Y-%m-%d %H:%M:%S")
puts start_time
puts finish_time
puts "Connecting to database..."
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:#{$db}:#{$db_ip}","#{$user}", "#{$pass}")
my_query = dbh.prepare("SELECT spectrum_id FROM spec_reserve WHERE begin_time <= '#{start_time}' AND end_time > '#{start_time}'")
my_query.execute()
i = 0
my_query.fetch do | n |
channels.each do | id |
if n[0].to_f == id.to_f
already_reserved[i] = id
i += 1
end
end
end
final_reserved = channels - already_reserved
my_query = dbh.prepare("SELECT slice_name FROM slices WHERE id = '#{slice_id}'")
result = my_query.execute()
my_query.fetch do | name |
slice_name = name
end
#puts slice_name
# insert values into the database
i = 0
final_reserved.each do | id |
dbh.do("INSERT INTO spec_reserve (username,begin_time,end_time,spectrum_id) VALUES ('#{slice_name[0]}','#{start_time}','#{finish_time}','#{id}')")
final_reserved[i] = id
i += 1
end
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
return final_reserved
end
##################################################################
# Adds a new user into users table
# Returns the new id of the user
##################################################################
def addUser(userInfo)
new_id = 0
username = 0
email = 0
userInfo.each_pair { | name, value |
if(name == "username")
username = value
elsif(name == "email")
email = value
else
puts "wrong parameter: #{name}"
return "wrong parameter: #{name}"
end
}
puts "Connecting to database..."
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:#{$db}:#{$db_ip}","#{$user}", "#{$pass}")
dbh.do("INSERT INTO nitlab.b9tj1_users (username) VALUES ('#{username}')")
id = dbh.prepare("SELECT id FROM nitlab.b9tj1_users WHERE username = '#{username}'")
id.execute()
id.fetch do | n |
new_id = n[0]
end
my_query = dbh.prepare("UPDATE nitlab.b9tj1_users SET email = '#{email}' WHERE id = '#{new_id}'")
my_query.execute()
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
puts new_id
return new_id
end
##################################################################
# Adds a new user to an existing slice
# Returns 0 for success or -1 for failure
##################################################################
def addUserToSlice(userToSliceInfo)
num = 0
new_id = 0
user_id = 0
slice_id = 0
userToSliceInfo.each_pair { | name, value |
if(name == "user_id")
user_id = value
elsif(name == "slice_id")
slice_id = value
else
puts "wrong parameter: #{name}"
return "wrong parameter: #{name}"
end
}
puts "Connecting to database..."
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:#{$db}:#{$db_ip}","#{$user}", "#{$pass}")
dbh.do("INSERT INTO users_slices (user_id) VALUES ('#{user_id}')")
id = dbh.prepare("SELECT id FROM users_slices WHERE user_id = '#{user_id}'")
id.execute()
id.fetch do | n |
new_id = n[0]
end
num = dbh.do("UPDATE users_slices SET slice_id = '#{slice_id}' WHERE id = '#{new_id}'")
if (num == 0)
return -1
end
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
puts "Association between user and slice done..."
return 0
end
###################################################################
# Adds a new user's key to rsa_keys table
# Returns 0 for success or -1 for failure
##################################################################
def addUserKey(userKeyInfo)
num = 0
user_id = 0
new_id = 0
key = 0
slice_id = 0
userKeyInfo.each_pair { | name, value |
if(name == "user_id")
user_id = value
elsif(name == "key")
key = value
elsif(name == "slice_id")
slice_id = value
else
puts "wrong parameter: #{name}"
return "wrong parameter: #{name}"
end
}
puts "Connecting to database..."
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:#{$db}:#{$db_ip}","#{$user}", "#{$pass}")
dbh.do("INSERT INTO rsa_keys (user_id) VALUES ('#{user_id}')")
id = dbh.prepare("SELECT id FROM rsa_keys WHERE user_id = '#{user_id}'")
id.execute()
id.fetch do | n |
new_id = n[0]
end
### WE NEED TO ASSOCIATE THE KEY WITH THE RIGHT SLICE. NOW WE CONSIDER THAT THE USER IS ASSOCIATED WITH ONE SLICE ###
# sliceId = dbh.prepare("SELECT slice_id FROM users_slices WHERE user_id = '#{user_id}'")
# sliceId.execute()
# sliceId.fetch do |sid|
# puts sid.class
# puts key.class
# puts new_id
# num = dbh.do("UPDATE rsa_keys SET slice_id = '#{sid[0]}', rsa_keys.key = '#{key}' WHERE id = '#{new_id}'")
num = dbh.do("UPDATE rsa_keys SET slice_id = '#{slice_id}', rsa_keys.key = '#{key}' WHERE id = '#{new_id}'")
# end
if (num == 0)
return -1
end
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
return 0
end
#################################################################
# Adds a new slice into slices table
# Returns the new id of the slice
##################################################################
def addSlice(sliceInfo)
new_id = nil
slice_name = nil
sliceInfo.each_pair { |name, value |
if(name == "slice_name")
slice_name = value
else
puts "wrong parameter: #{name}"
return "wrong parameter: #{name}"
end
}
#puts "Connecting to database..."
begin
# connect to the MySQL server
#dbh = DBI.connect("DBI:Mysql:#{$db}:#{$db_ip}","#{$user}", "#{$pass}")
# dbh.do("INSERT INTO slices (slice_name) VALUES ('#{slice_name}')")
# id = dbh.prepare("SELECT id FROM slices WHERE slice_name = '#{slice_name}'")
# id.execute()
# id.fetch do | n |
# new_id = n[0]
# end
# create OMF pubsub node into the xmpp server
# value = `create_slice_sfa #{slice_name} #{new_id}`
value = `create_slice #{slice_name} nitlab.inf.uth.gr`
puts value
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
puts "New Slice created... #{slice_name}"
return new_id
end
#################################################################
# Adds a new node into node_list table
# Returns the new id of the node
##################################################################
def addNode(nodeInfo)
new_id = nil
hostname = 0
node_type = 0
floor = 0
view = 0
wall = 0
position = Struct.new(:X,:Y,:Z)
nodeInfo.each_pair { | name, value |
if(name == "hostname")
hostname = value
elsif(name == "type")
node_type = value
elsif(name == "floor")
floor = value
elsif(name == "view")
view = value
elsif(name == "wall")
wall = value
elsif(name == "position")
position = value
else
puts "wrong parameter: #{name}"
return "wrong parameter: #{name}"
end
}
puts "Connecting to database..."
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:#{$db}:#{$db_ip}","#{$user}", "#{$pass}")
dbh.do("INSERT INTO node_list (name,type,floor,view,wall,X,Y,Z) VALUES ('#{hostname}','#{node_type}','#{floor}','#{view}','#{wall}','#{position.values[0]}','#{position.values[1]}','#{position.values[2]}')")
id = dbh.prepare("SELECT id FROM node_list WHERE name = '#{hostname}'")
id.execute()
id.fetch do | n |
new_id = n[0]
end
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
puts "New node added..."
return new_id
end
#################################################################
# Adds a new channel into spectrum table
# Returns the new id of the channel
##################################################################
def addChannel(channelInfo)
new_id = nil
channel = 0
frequency = 0
modulation = 0
channelInfo.each_pair { | name, value |
if(name == "channel")
channel = value
elsif(name == "frequency")
frequency = value
elsif(name == "modulation")
modulation = value
else
puts "wrong parameter: #{name}"
return "wrong parameter: #{name}"
end
}
puts "Connecting to database..."
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:#{$db}:#{$db_ip}","#{$user}", "#{$pass}")
dbh.do("INSERT INTO spectrum (channel) VALUES ('#{channel}')")
id = dbh.prepare("SELECT id FROM spectrum WHERE channel = '#{channel}'")
id.execute()
id.fetch do | n |
new_id = n[0]
end
num = dbh.do("UPDATE spectrum SET frequency = '#{frequency}', modulation = '#{modulation}' WHERE id = '#{new_id}'")
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
puts "New channel added..."
return new_id
end
end