-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdc3lib_stub.rb
191 lines (177 loc) · 7.24 KB
/
dc3lib_stub.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
# coding: utf-8
require 'json'
require 'rest-client'
$interfaces_rt1 = {
"output" =>
{
"interface" =>
[{
"name" => "eth0",
"description" => "router 1/0",
"mac-address" => "11:22:33:44:55:66",
"physical" => true,
"ipv4-address" => ["192.168.10.1/24"],
"admin-status" => true
},{
"name" => "eth1",
"description" => "router 1/1",
"mac-address" => "11:22:33:44:55:66",
"physical" => true,
"ipv4-address" => ["192.168.12.1/24"],
"admin-status" => true
}
]
}
}
$interfaces_rt2 = {
"output" =>
{
"interface" =>
[{
"name" => "eth0",
"description" => "router 1/0",
"mac-address" => "11:22:33:44:55:66",
"physical" => true,
"ipv4-address" => ["192.168.10.2/24"],
"admin-status" => true
},{
"name" => "eth1",
"description" => "router 1/1",
"mac-address" => "11:22:33:44:55:66",
"physical" => true,
"ipv4-address" => ["192.168.11.1/24"],
"admin-status" => true
}
]
}
}
$interfaces_rt3 = {
"output" =>
{
"interface" =>
[{
"name" => "eth0",
"description" => "router 1/0",
"mac-address" => "11:22:33:44:55:66",
"physical" => true,
"ipv4-address" => ["192.168.11.2/24"],
"admin-status" => true
},{
"name" => "eth1",
"description" => "router 1/1",
"mac-address" => "11:22:33:44:55:66",
"physical" => true,
"ipv4-address" => ["192.168.12.2/24"],
"admin-status" => true
}
]
}
}
class DC3Lib
def initialize
end
def get_interfaces(ipaddr)
# name
# description
# mac-address
# physical
# ipv4-address (addr/prefix)
# ipv6-address (addr/prefix)
# admin-status
# oper-status
# vlan-id
# parent-device
p "RESTCONF: get_interfaces to #{ipaddr}"
case ipaddr
when "192.168.0.1" then
return $interfaces_rt1.to_json
when "192.168.0.2" then
return $interfaces_rt2.to_json
when "192.168.0.3" then
return $interfaces_rt3.to_json
end
end
def failure_action_port_down(ipaddr = "127.0.0.1", iface = "")
p "RESTCONF: port down of #{ipaddr} iface #{iface}"
return ""
end
def failure_action_all_loss(ipaddr = "127.0.0.1", iface = "", direction="both")
ret = RestClient.post("http://#{ipaddr}/restconf/operations/destcloud3:failure-action",
{ "input" => { "interface" => iface,
"action" => {"all-lose" => [],
"direction" => direction}}}.to_json,
:content_type => :json)
if ret.code != 200 then
return nil
end
result = JSON.parse (ret.to_s)
uuid = result['output']['uuid']
return uuid
end
def failure_action_packet_loss(ipaddr = "127.0.0.1", iface = "", ratio = 0, direction = "both")
ret = RestClient.post("http://#{ipaddr}/restconf/operations/destcloud3:failure-action",
{ "input" => { "interface" => iface,
"action" => {"packet-loss" => ratio,
"direction" => direction}}}.to_json,
:content_type => :json)
if ret.code != 200 then
return nil
end
result = JSON.parse (ret.to_s)
uuid = result['output']['uuid']
return uuid
end
def failure_action_route_change(ipaddr = "127.0.0.1", prefix ="", nexthop="")
ret = RestClient.post("http://#{ipaddr}/restconf/operations/destcloud3:failure-action",
{ "input" => {"action" => {"route-change" =>
{"prefix" => prefix,
"nexthop" => nexthop}}}}.to_json,
:content_type => :json)
if ret.code != 200 then
return nil
end
result = JSON.parse (ret.to_s)
uuid = result['output']['uuid']
return uuid
end
def failure_action_shaping(ipaddr = "127.0.0.1", iface = "")
ret = RestClient.post("http://#{ipaddr}/restconf/operations/destcloud3:failure-action",
{ "input" => { "interface" => iface,
"action" => {"port-down" => []}}}.to_json,
:content_type => :json)
if ret.code != 200 then
return nil
end
result = JSON.parse (ret.to_s)
uuid = result['output']['uuid']
return uuid
end
def failure_action_delay(ipaddr = "127.0.0.1", iface = "")
ret = RestClient.post("http://#{ipaddr}/restconf/operations/destcloud3:failure-action",
{ "input" => { "interface" => iface,
"action" => {"port-down" => []}}}.to_json,
:content_type => :json)
if ret.code != 200 then
return nil
end
result = JSON.parse (ret.to_s)
uuid = result['output']['uuid']
return uuid
end
def failure_action(ipaddr = "127.0.0.1", iface = "")
return uuid
end
def recover(ipaddr = "127.0.0.1", uuid)
ret = RestClient.post("http://#{ipaddr}/restconf/operations/destcloud3:recovery",
{ "input" => { "uuid" => uuid} }.to_json,
:content_type => :json)
return ret
end
def set_interface_state(ipaddr = "127.0.0.1", iface = "", state = "up")
ret = RestClient.post("http://#{ipaddr}/restconf/operations/destcloud3:set-interface-state",
{ "input" => { "interface" => iface,
"state" => state} }.to_json,
:content_type => :json)
return ret
end
end