blob: ad7e34e8d0f8110409dbaf56ef3fb1286a8f7e7e [file] [log] [blame]
Nick Karanatsios31ebc472014-02-24 19:15:37 -08001#!/usr/bin/env ruby
2
Yuta HIGUCHIaf2bdce2014-04-24 09:55:21 -07003begin
Nick Karanatsios31ebc472014-02-24 19:15:37 -08004require "rest-client"
Yuta HIGUCHIaf2bdce2014-04-24 09:55:21 -07005rescue LoadError => e
6 puts "This scripts requires rubygems rest-client."
7 puts "Please install them using gem command. You may need to add sudo."
8 puts " $ gem install rest-client"
9 puts
10 exit -1
11end
Nick Karanatsios31ebc472014-02-24 19:15:37 -080012require "optparse"
13
14options = {
15 :rest_op => "add",
16 :intent_id => 1,
17 :intent_category => "high",
18 :intent_type => "shortest_intent_type",
19 :max_switches => 4,
20 :intent_op => "add"
21}
22
23parser = OptionParser.new do |opts|
24 opts.banner = "Usage [get-options] [post-options]"
25
26 opts.separator ""
27 opts.separator "Get options:"
28
29 opts.on('-G', '--get_intents', 'get intents state') do
30 options[:get_intents] = true
31 options[:rest_op] = "get"
32 end
33 opts.on('-g', '--get_intent intent_id', 'get intent state') do |intent_id|
34 options[:rest_op] = "get"
35 options[:get_intent] = intent_id
36 end
37
38 opts.separator ""
39 opts.separator "Purge options"
40 opts.on('-d', '--purge', 'purge all intents') do
41 options[:rest_op] = "delete"
42 end
43
44 opts.separator ""
45 opts.separator "Post options:"
46
47 opts.on('-t', '--max_intents max_intents', 'max. number of intents') do |max_intents|
48 options[:max_intents] = max_intents
49 end
50 opts.on('-e', '--intent_category intent_category', 'intent category high|low') do |intent_category|
51 options[:intent_category] = intent_category
52 end
53 opts.on('-i', '--intent_id intent_id', 'global intent id') do |id|
54 options[:intent_id] = id.to_i
55 end
56 # optional argument
57 opts.on('-s', '--shortest', 'create a shortest path intent') do
58 options[:intent_type] = "shortest_intent_type"
59 end
60 # optional argument
61 opts.on('-c', '--constrained', 'create a constrained shortest path intent') do
62 options[:intent_type] = "constrained_shortest_intent_type"
63 end
64 # optional argument
65 opts.on('-r', '--random_intent', 'create minimum no. of random intents') do
66 options[:random_intent] = true
67 end
68 opts.on('-m', '--max_switches max_switches', 'max. number of switches') do |max_switches|
69 options[:max_switches] = max_switches.to_i
70 end
71 opts.on('-o', '--intent_op add|remove', 'an operation to post an intent') do |operation|
72 options[:intent_op] = operation
73 end
74 opts.on('-w', '--server server', 'server to post intents') do |server|
75 options[:server] = server
76 end
77 opts.on('-p','--port port', 'server port') do |port|
78 options[:port] = port
79 end
80 opts.on('b', '--bulk_limit bulk_limit', 'bulk request upto this limit') do |bulk_limit|
81 options[:bulk_limit] = bulk_limit
82 end
Toshio Koide93797dc2014-02-27 23:54:26 -080083 opts.on('f', '--freeze_intents', 'freeze dont reroute intents') do
84 options[:freeze] = "F"
85 end
Nick Karanatsios31ebc472014-02-24 19:15:37 -080086 opts.on('-h', '--help', 'Display help') do
87 puts opts
88 exit
89 end
90end
91parser.parse!
92
93
94class Intent
95 attr_reader :switches, :ports, :intent_id
96 attr_reader :intent_type, :intent_op
97 attr_reader :random_intent, :server, :port
98 attr_reader :bulk_limit, :max_switches
99
100 def initialize options
101 parse_options options
102 end
103
104 def post_intent
105 create_specific_intent
106 end
107
108 def get_intent options
109 if options[:get_intents] == true
110 request = RestClient.get "http://#{@server}:#{@port}/wm/onos/datagrid/get/intents/#{options[:intent_category]}/json"
111 else
112 url = "http://#{@server}:#{@port}/wm/onos/datagrid/get/intent/#{options[:intent_category]}/#{options[:get_intent]}/json"
113 request = RestClient.get url
114 end
115 puts request
116 end
117
118 def purge_intents
119 response = RestClient.delete "http://#{@server}:#{@port}/wm/onos/datagrid/delete/intents/json"
120 puts response
121 end
122
123 private
124
125 def create_specific_intent
126 if @random_intent == true
127 create_random_intent
128 else
129 create_many_intents
130 end
131 end
132
133 # create as many intents as the number of switches
134 def create_many_intents
135 intents = []
136 intents = _create_intent intents
137 post intents
138 end
139
140
141 # pick a random src switch and create intents to all other switches
142 def create_random_intent
143 intents = []
144 sw = @switches.shuffle[0]
145 rest = @switches - [sw]
146 intents = _create_intent sw, rest, intents
147 post_slice intents, true
148 end
149
150 def post_slice intents, last=false
151 @bulk_limit = @bulk_limit.to_i
152 if intents.size >= @bulk_limit
153 post intents.slice!(0..(@bulk_limit - 1))
154 end
155 if last == true
156 loop do
157 new_bulk_limit = intents.size > @bulk_limit ? @bulk_limit : intents.size
158 post intents.slice!(0..(new_bulk_limit - 1))
159 break if new_bulk_limit < @bulk_limit
160 end
161 end
162 end
163
164 def post intents
165 json_data = intents.to_json
166 response = RestClient.post "http://#{@server}:#{@port}/wm/onos/datagrid/add/intents/json", json_data, :content_type => :json, :accept => :json
167 puts response
168 end
169
170 def parse_options options
171 @max_switches = options[:max_switches].to_i || 4
172 @switches = (1..max_switches).to_a
173 @ports = (1..max_switches).to_a
174 @intent_id = options[:intent_id]
175 @intent_id ||= 1
176 @intent_type = options[:intent_type]
177 @intent_op = options[:intent_op]
178 @intent_op ||= "add"
179 @random_intent = options[:random_intent]
180 @random_intent ||= false
181 @server = options[:server]
182 @server ||= "127.0.0.1"
183 @port = options[:port]
184 @port ||= 8080
185 @bulk_limit = options[:bulk_limit]
186 @bulk_limit ||= 10000
Toshio Koide93797dc2014-02-27 23:54:26 -0800187 @freeze = options[:freeze]
188 @freeze ||= ""
Nick Karanatsios31ebc472014-02-24 19:15:37 -0800189 end
190
191
192 def _create_intent json_intents
193 (0...@max_switches).each do |idx|
194 intent = {
Toshio Koide93797dc2014-02-27 23:54:26 -0800195 :intent_id => "#{@freeze}#{@intent_id}",
Nick Karanatsios31ebc472014-02-24 19:15:37 -0800196 :intent_type => @intent_type,
197 :intent_op => @intent_op,
Pavlin Radoslavov2fa80f42014-04-18 13:10:38 -0700198 :srcSwitch => "00:00:00:00:00:00:02:02".to_s,
199 :srcPort => 1,
Nick Karanatsios31ebc472014-02-24 19:15:37 -0800200 :srcMac => "00:00:c0:#{mac_format(idx)}",
Pavlin Radoslavov2fa80f42014-04-18 13:10:38 -0700201 :dstSwitch => "00:00:00:00:00:00:04:02".to_s,
202 :dstPort => 1,
Nick Karanatsios31ebc472014-02-24 19:15:37 -0800203 :dstMac => "00:00:c1:#{mac_format(idx)}"
204 }
205puts intent.inspect
206 @intent_id = @intent_id + 1
207 json_intents << intent
208puts
209 end
210 json_intents
211 end
212
213 def mac_format number
214 if number > 255
215 divisor1 = number / 256
216 remainder1 = number % 256
217 divisor2 = divisor1 / 256
218 remainder2 = divisor1 % 256
219 return sprintf("%02x:%02x:%02x",divisor2 ,remainder2, remainder1)
220 end
221 "00:00:%02x" % number
222 end
223end
224
225intent = Intent.new options
226if options[:rest_op] == "get"
227 intent.get_intent options
228elsif options[:rest_op] == "delete"
229 intent.purge_intents
230else
231 json_data = intent.post_intent
232end
233