blob: ccf1938fbd307055bc5a6d73d56a4dfe0ac1f508 [file] [log] [blame]
Nick Karanatsiosf9336002014-02-24 11:26:58 -08001#!/usr/bin/env ruby
2
Nick Karanatsios88948d32014-02-18 15:14:30 -08003require "rest-client"
4require "optparse"
5
6options = {
7 :rest_op => "add",
Nick Karanatsios87e8be72014-02-21 23:45:37 -08008 :intent_id => 1,
9 :intent_category => "high",
Nick Karanatsios88948d32014-02-18 15:14:30 -080010 :intent_type => "shortest_intent_type",
11 :max_switches => 4,
12 :intent_op => "add"
13}
14
15parser = OptionParser.new do |opts|
Nick Karanatsiosed645df2014-02-20 23:22:29 -080016 opts.banner = "Usage [get-options] [post-options]"
17
18 opts.separator ""
19 opts.separator "Get options:"
20
21 opts.on('-G', '--get_intents', 'get intents state') do
22 options[:get_intents] = true
Nick Karanatsios88948d32014-02-18 15:14:30 -080023 options[:rest_op] = "get"
24 end
Nick Karanatsiosed645df2014-02-20 23:22:29 -080025 opts.on('-g', '--get_intent intent_id', 'get intent state') do |intent_id|
26 options[:rest_op] = "get"
27 options[:get_intent] = intent_id
28 end
29
Nick Karanatsios1f4defb2014-02-23 19:34:47 -080030 opts.separator ""
31 opts.separator "Delete options"
32 opts.on('-d', '--purge', 'purge all intents') do
33 options[:rest_op] = "delete"
34 end
35
Nick Karanatsiosed645df2014-02-20 23:22:29 -080036 opts.separator ""
37 opts.separator "Post options:"
38
Nick Karanatsios88948d32014-02-18 15:14:30 -080039 opts.on('-t', '--max_intents max_intents', 'max. number of intents') do |max_intents|
40 options[:max_intents] = max_intents
41 end
Nick Karanatsios87e8be72014-02-21 23:45:37 -080042 opts.on('-e', '--intent_category intent_category', 'intent category high|low') do |intent_category|
43 options[:intent_category] = intent_category
44 end
Nick Karanatsios88948d32014-02-18 15:14:30 -080045 opts.on('-i', '--intent_id intent_id', 'global intent id') do |id|
46 options[:intent_id] = id.to_i
47 end
48 # optional argument
49 opts.on('-s', '--shortest', 'create a shortest path intent') do
50 options[:intent_type] = "shortest_intent_type"
51 end
52 # optional argument
53 opts.on('-c', '--constrained', 'create a constrained shortest path intent') do
54 options[:intent_type] = "constrained_shortest_intent_type"
55 end
56 # optional argument
57 opts.on('-r', '--random_intent', 'create minimum no. of random intents') do
58 options[:random_intent] = true
59 end
60 opts.on('-m', '--max_switches max_switches', 'max. number of switches') do |max_switches|
61 options[:max_switches] = max_switches.to_i
62 end
63 opts.on('-o', '--intent_op add|remove', 'an operation to post an intent') do |operation|
64 options[:intent_op] = operation
65 end
66 opts.on('-w', '--server server', 'server to post intents') do |server|
67 options[:server] = server
68 end
69 opts.on('-p','--port port', 'server port') do |port|
70 options[:port] = port
71 end
72 opts.on('b', '--bulk_limit bulk_limit', 'bulk request upto this limit') do |bulk_limit|
73 options[:bulk_limit] = bulk_limit
74 end
75 opts.on('-h', '--help', 'Display help') do
76 puts opts
77 exit
78 end
79end
80parser.parse!
81
Nick Karanatsios87e8be72014-02-21 23:45:37 -080082
Nick Karanatsios88948d32014-02-18 15:14:30 -080083class Intent
84 attr_reader :switches, :ports, :intent_id
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -080085 attr_reader :intent_type, :intent_op
Nick Karanatsios88948d32014-02-18 15:14:30 -080086 attr_reader :random_intent, :server, :port
87 attr_reader :bulk_limit
88
89 def initialize options
90 parse_options options
91 end
92
93 def post_intent
94 create_specific_intent
95 end
96
Nick Karanatsiosed645df2014-02-20 23:22:29 -080097 def get_intent options
98 if options[:get_intents] == true
Nick Karanatsios87e8be72014-02-21 23:45:37 -080099 request = RestClient.get "http://#{@server}:#{@port}/wm/onos/datagrid/get/intents/#{options[:intent_category]}/json"
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800100 else
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800101 url = "http://#{@server}:#{@port}/wm/onos/datagrid/get/intent/#{options[:intent_category]}/#{options[:get_intent]}/json"
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800102 request = RestClient.get url
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800103 end
Nick Karanatsios88948d32014-02-18 15:14:30 -0800104 puts request
105 end
106
Nick Karanatsios1f4defb2014-02-23 19:34:47 -0800107 def purge_intents
108 response = RestClient.delete "http://#{@server}:#{@port}/wm/onos/datagrid/delete/intents/json"
109 puts response
110 end
111
Nick Karanatsios88948d32014-02-18 15:14:30 -0800112 private
113
114 def create_specific_intent
115 if @random_intent == true
116 create_random_intent
117 else
118 create_many_intents
119 end
120 end
121
122 # create as many intents as the number of switches
123 def create_many_intents
124 intents = []
125 @switches.each do |sw|
126 rest = @switches - [sw]
127 intents = _create_intent sw, rest, intents
128puts intents.size
129 post_slice intents
130 end
131 post_slice intents, true
132 end
133
134 # pick a random src switch and create intents to all other switches
135 def create_random_intent
136 intents = []
137 sw = @switches.shuffle[0]
138 rest = @switches - [sw]
139 intents = _create_intent sw, rest, intents
140 post_slice intents, true
141 end
142
143 def post_slice intents, last=false
144 @bulk_limit = @bulk_limit.to_i
145 if intents.size >= @bulk_limit
146 post intents.slice!(0..(@bulk_limit - 1))
147 end
148 if last == true
149 loop do
150 new_bulk_limit = intents.size > @bulk_limit ? @bulk_limit : intents.size
151 post intents.slice!(0..(new_bulk_limit - 1))
152 break if new_bulk_limit < @bulk_limit
153 end
154 end
155 end
156
157 def post intents
158 json_data = intents.to_json
Nick Karanatsiosa1bad352014-02-22 14:16:34 -0800159 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 -0800160 puts response
161 end
162
163 def parse_options options
164 max_switches = options[:max_switches].to_i || 4
165 @switches = (1..max_switches).to_a
Nick Karanatsios882235b2014-02-21 15:49:53 -0800166 @ports = (1..max_switches).to_a
Nick Karanatsios88948d32014-02-18 15:14:30 -0800167 @intent_id = options[:intent_id]
168 @intent_id ||= 1
Nick Karanatsios88948d32014-02-18 15:14:30 -0800169 @intent_type = options[:intent_type]
170 @intent_op = options[:intent_op]
171 @intent_op ||= "add"
172 @random_intent = options[:random_intent]
173 @random_intent ||= false
174 @server = options[:server]
175 @server ||= "127.0.0.1"
176 @port = options[:port]
177 @port ||= 8080
178 @bulk_limit = options[:bulk_limit]
179 @bulk_limit ||= 10000
180 end
181
182
183 def _create_intent src_switch, iterable_switches, json_intents
184 network_id = 1
185 iterable_switches.each_index do |sw_i|
Nick Karanatsios88948d32014-02-18 15:14:30 -0800186 intent = {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800187 :intent_id => "#{@intent_id}",
Nick Karanatsios88948d32014-02-18 15:14:30 -0800188 :intent_type => @intent_type,
189 :intent_op => @intent_op,
190 :srcSwitch => src_switch.to_s,
Nick Karanatsios882235b2014-02-21 15:49:53 -0800191 :srcPort => @ports[-1],
Nick Karanatsios88948d32014-02-18 15:14:30 -0800192 :srcMac => "00:00:c0:a8:#{mac_format(src_switch)}",
193 :dstSwitch => iterable_switches[sw_i].to_s,
Nick Karanatsios882235b2014-02-21 15:49:53 -0800194 :dstPort => @ports[-1],
Nick Karanatsios88948d32014-02-18 15:14:30 -0800195 :dstMac => "00:00:c0:a8:#{mac_format(iterable_switches[sw_i].to_i)}"
196 }
197puts intent.inspect
198 @intent_id = @intent_id + 1
199 json_intents << intent
200puts
201 end
Nick Karanatsios88948d32014-02-18 15:14:30 -0800202 json_intents
203 end
204
205 def mac_format number
206 if number > 255
207 divisor = number / 256
208 remainder = number % 256
209 return sprintf("%02x:%02x",divisor ,remainder)
210 end
211 "00:%02x" % number
212 end
213end
214
215intent = Intent.new options
216if options[:rest_op] == "get"
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800217 intent.get_intent options
Nick Karanatsios1f4defb2014-02-23 19:34:47 -0800218elsif options[:rest_op] == "delete"
219 intent.purge_intents
Nick Karanatsios88948d32014-02-18 15:14:30 -0800220else
221 json_data = intent.post_intent
222end
223