blob: e73b8e54064ce3105fde2fe98d6db7c66bc8f16c [file] [log] [blame]
Nick Karanatsios88948d32014-02-18 15:14:30 -08001require "rest-client"
2require "optparse"
3
4options = {
5 :rest_op => "add",
Nick Karanatsios87e8be72014-02-21 23:45:37 -08006 :intent_id => 1,
7 :intent_category => "high",
Nick Karanatsios88948d32014-02-18 15:14:30 -08008 :intent_type => "shortest_intent_type",
9 :max_switches => 4,
10 :intent_op => "add"
11}
12
13parser = OptionParser.new do |opts|
Nick Karanatsiosed645df2014-02-20 23:22:29 -080014 opts.banner = "Usage [get-options] [post-options]"
15
16 opts.separator ""
17 opts.separator "Get options:"
18
19 opts.on('-G', '--get_intents', 'get intents state') do
20 options[:get_intents] = true
Nick Karanatsios88948d32014-02-18 15:14:30 -080021 options[:rest_op] = "get"
22 end
Nick Karanatsiosed645df2014-02-20 23:22:29 -080023 opts.on('-g', '--get_intent intent_id', 'get intent state') do |intent_id|
24 options[:rest_op] = "get"
25 options[:get_intent] = intent_id
26 end
27
Nick Karanatsios1f4defb2014-02-23 19:34:47 -080028 opts.separator ""
29 opts.separator "Delete options"
30 opts.on('-d', '--purge', 'purge all intents') do
31 options[:rest_op] = "delete"
32 end
33
Nick Karanatsiosed645df2014-02-20 23:22:29 -080034 opts.separator ""
35 opts.separator "Post options:"
36
Nick Karanatsios88948d32014-02-18 15:14:30 -080037 opts.on('-t', '--max_intents max_intents', 'max. number of intents') do |max_intents|
38 options[:max_intents] = max_intents
39 end
Nick Karanatsios87e8be72014-02-21 23:45:37 -080040 opts.on('-e', '--intent_category intent_category', 'intent category high|low') do |intent_category|
41 options[:intent_category] = intent_category
42 end
Nick Karanatsios88948d32014-02-18 15:14:30 -080043 opts.on('-i', '--intent_id intent_id', 'global intent id') do |id|
44 options[:intent_id] = id.to_i
45 end
46 # optional argument
47 opts.on('-s', '--shortest', 'create a shortest path intent') do
48 options[:intent_type] = "shortest_intent_type"
49 end
50 # optional argument
51 opts.on('-c', '--constrained', 'create a constrained shortest path intent') do
52 options[:intent_type] = "constrained_shortest_intent_type"
53 end
54 # optional argument
55 opts.on('-r', '--random_intent', 'create minimum no. of random intents') do
56 options[:random_intent] = true
57 end
58 opts.on('-m', '--max_switches max_switches', 'max. number of switches') do |max_switches|
59 options[:max_switches] = max_switches.to_i
60 end
61 opts.on('-o', '--intent_op add|remove', 'an operation to post an intent') do |operation|
62 options[:intent_op] = operation
63 end
64 opts.on('-w', '--server server', 'server to post intents') do |server|
65 options[:server] = server
66 end
67 opts.on('-p','--port port', 'server port') do |port|
68 options[:port] = port
69 end
70 opts.on('b', '--bulk_limit bulk_limit', 'bulk request upto this limit') do |bulk_limit|
71 options[:bulk_limit] = bulk_limit
72 end
73 opts.on('-h', '--help', 'Display help') do
74 puts opts
75 exit
76 end
77end
78parser.parse!
79
Nick Karanatsios87e8be72014-02-21 23:45:37 -080080
Nick Karanatsios88948d32014-02-18 15:14:30 -080081class Intent
82 attr_reader :switches, :ports, :intent_id
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -080083 attr_reader :intent_type, :intent_op
Nick Karanatsios88948d32014-02-18 15:14:30 -080084 attr_reader :random_intent, :server, :port
85 attr_reader :bulk_limit
86
87 def initialize options
88 parse_options options
89 end
90
91 def post_intent
92 create_specific_intent
93 end
94
Nick Karanatsiosed645df2014-02-20 23:22:29 -080095 def get_intent options
96 if options[:get_intents] == true
Nick Karanatsios87e8be72014-02-21 23:45:37 -080097 request = RestClient.get "http://#{@server}:#{@port}/wm/onos/datagrid/get/intents/#{options[:intent_category]}/json"
Nick Karanatsiosed645df2014-02-20 23:22:29 -080098 else
Nick Karanatsios87e8be72014-02-21 23:45:37 -080099 url = "http://#{@server}:#{@port}/wm/onos/datagrid/get/intent/#{options[:intent_category]}/#{options[:get_intent]}/json"
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800100 request = RestClient.get url
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800101 end
Nick Karanatsios88948d32014-02-18 15:14:30 -0800102 puts request
103 end
104
Nick Karanatsios1f4defb2014-02-23 19:34:47 -0800105 def purge_intents
106 response = RestClient.delete "http://#{@server}:#{@port}/wm/onos/datagrid/delete/intents/json"
107 puts response
108 end
109
Nick Karanatsios88948d32014-02-18 15:14:30 -0800110 private
111
112 def create_specific_intent
113 if @random_intent == true
114 create_random_intent
115 else
116 create_many_intents
117 end
118 end
119
120 # create as many intents as the number of switches
121 def create_many_intents
122 intents = []
123 @switches.each do |sw|
124 rest = @switches - [sw]
125 intents = _create_intent sw, rest, intents
126puts intents.size
127 post_slice intents
128 end
129 post_slice intents, true
130 end
131
132 # pick a random src switch and create intents to all other switches
133 def create_random_intent
134 intents = []
135 sw = @switches.shuffle[0]
136 rest = @switches - [sw]
137 intents = _create_intent sw, rest, intents
138 post_slice intents, true
139 end
140
141 def post_slice intents, last=false
142 @bulk_limit = @bulk_limit.to_i
143 if intents.size >= @bulk_limit
144 post intents.slice!(0..(@bulk_limit - 1))
145 end
146 if last == true
147 loop do
148 new_bulk_limit = intents.size > @bulk_limit ? @bulk_limit : intents.size
149 post intents.slice!(0..(new_bulk_limit - 1))
150 break if new_bulk_limit < @bulk_limit
151 end
152 end
153 end
154
155 def post intents
156 json_data = intents.to_json
Nick Karanatsiosa1bad352014-02-22 14:16:34 -0800157 response = RestClient.post "http://#{@server}:#{@port}/wm/onos/datagrid/add/intents/json", json_data, :content_type => :json, :accept => :json
Nick Karanatsios88948d32014-02-18 15:14:30 -0800158 puts response
159 end
160
161 def parse_options options
162 max_switches = options[:max_switches].to_i || 4
163 @switches = (1..max_switches).to_a
Nick Karanatsios882235b2014-02-21 15:49:53 -0800164 @ports = (1..max_switches).to_a
Nick Karanatsios88948d32014-02-18 15:14:30 -0800165 @intent_id = options[:intent_id]
166 @intent_id ||= 1
Nick Karanatsios88948d32014-02-18 15:14:30 -0800167 @intent_type = options[:intent_type]
168 @intent_op = options[:intent_op]
169 @intent_op ||= "add"
170 @random_intent = options[:random_intent]
171 @random_intent ||= false
172 @server = options[:server]
173 @server ||= "127.0.0.1"
174 @port = options[:port]
175 @port ||= 8080
176 @bulk_limit = options[:bulk_limit]
177 @bulk_limit ||= 10000
178 end
179
180
181 def _create_intent src_switch, iterable_switches, json_intents
182 network_id = 1
183 iterable_switches.each_index do |sw_i|
Nick Karanatsios88948d32014-02-18 15:14:30 -0800184 intent = {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800185 :intent_id => "#{@intent_id}",
Nick Karanatsios88948d32014-02-18 15:14:30 -0800186 :intent_type => @intent_type,
187 :intent_op => @intent_op,
188 :srcSwitch => src_switch.to_s,
Nick Karanatsios882235b2014-02-21 15:49:53 -0800189 :srcPort => @ports[-1],
Nick Karanatsios88948d32014-02-18 15:14:30 -0800190 :srcMac => "00:00:c0:a8:#{mac_format(src_switch)}",
191 :dstSwitch => iterable_switches[sw_i].to_s,
Nick Karanatsios882235b2014-02-21 15:49:53 -0800192 :dstPort => @ports[-1],
Nick Karanatsios88948d32014-02-18 15:14:30 -0800193 :dstMac => "00:00:c0:a8:#{mac_format(iterable_switches[sw_i].to_i)}"
194 }
195puts intent.inspect
196 @intent_id = @intent_id + 1
197 json_intents << intent
198puts
199 end
Nick Karanatsios88948d32014-02-18 15:14:30 -0800200 json_intents
201 end
202
203 def mac_format number
204 if number > 255
205 divisor = number / 256
206 remainder = number % 256
207 return sprintf("%02x:%02x",divisor ,remainder)
208 end
209 "00:%02x" % number
210 end
211end
212
213intent = Intent.new options
214if options[:rest_op] == "get"
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800215 intent.get_intent options
Nick Karanatsios1f4defb2014-02-23 19:34:47 -0800216elsif options[:rest_op] == "delete"
217 intent.purge_intents
Nick Karanatsios88948d32014-02-18 15:14:30 -0800218else
219 json_data = intent.post_intent
220end
221