blob: 91554f547bc8f21b8c5218e5fa6dc83ab2c703bb [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
28 opts.separator ""
29 opts.separator "Post options:"
30
Nick Karanatsios88948d32014-02-18 15:14:30 -080031 opts.on('-t', '--max_intents max_intents', 'max. number of intents') do |max_intents|
32 options[:max_intents] = max_intents
33 end
Nick Karanatsios87e8be72014-02-21 23:45:37 -080034 opts.on('-e', '--intent_category intent_category', 'intent category high|low') do |intent_category|
35 options[:intent_category] = intent_category
36 end
Nick Karanatsios88948d32014-02-18 15:14:30 -080037 opts.on('-i', '--intent_id intent_id', 'global intent id') do |id|
38 options[:intent_id] = id.to_i
39 end
40 # optional argument
41 opts.on('-s', '--shortest', 'create a shortest path intent') do
42 options[:intent_type] = "shortest_intent_type"
43 end
44 # optional argument
45 opts.on('-c', '--constrained', 'create a constrained shortest path intent') do
46 options[:intent_type] = "constrained_shortest_intent_type"
47 end
48 # optional argument
49 opts.on('-r', '--random_intent', 'create minimum no. of random intents') do
50 options[:random_intent] = true
51 end
52 opts.on('-m', '--max_switches max_switches', 'max. number of switches') do |max_switches|
53 options[:max_switches] = max_switches.to_i
54 end
55 opts.on('-o', '--intent_op add|remove', 'an operation to post an intent') do |operation|
56 options[:intent_op] = operation
57 end
58 opts.on('-w', '--server server', 'server to post intents') do |server|
59 options[:server] = server
60 end
61 opts.on('-p','--port port', 'server port') do |port|
62 options[:port] = port
63 end
64 opts.on('b', '--bulk_limit bulk_limit', 'bulk request upto this limit') do |bulk_limit|
65 options[:bulk_limit] = bulk_limit
66 end
67 opts.on('-h', '--help', 'Display help') do
68 puts opts
69 exit
70 end
71end
72parser.parse!
73
Nick Karanatsios87e8be72014-02-21 23:45:37 -080074
Nick Karanatsios88948d32014-02-18 15:14:30 -080075class Intent
76 attr_reader :switches, :ports, :intent_id
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -080077 attr_reader :intent_type, :intent_op
Nick Karanatsios88948d32014-02-18 15:14:30 -080078 attr_reader :random_intent, :server, :port
79 attr_reader :bulk_limit
80
81 def initialize options
82 parse_options options
83 end
84
85 def post_intent
86 create_specific_intent
87 end
88
Nick Karanatsiosed645df2014-02-20 23:22:29 -080089 def get_intent options
90 if options[:get_intents] == true
Nick Karanatsios87e8be72014-02-21 23:45:37 -080091 request = RestClient.get "http://#{@server}:#{@port}/wm/onos/datagrid/get/intents/#{options[:intent_category]}/json"
Nick Karanatsiosed645df2014-02-20 23:22:29 -080092 else
Nick Karanatsios87e8be72014-02-21 23:45:37 -080093 url = "http://#{@server}:#{@port}/wm/onos/datagrid/get/intent/#{options[:intent_category]}/#{options[:get_intent]}/json"
Nick Karanatsiosed645df2014-02-20 23:22:29 -080094 request = RestClient.get url
Nick Karanatsiosed645df2014-02-20 23:22:29 -080095 end
Nick Karanatsios88948d32014-02-18 15:14:30 -080096 puts request
97 end
98
99 private
100
101 def create_specific_intent
102 if @random_intent == true
103 create_random_intent
104 else
105 create_many_intents
106 end
107 end
108
109 # create as many intents as the number of switches
110 def create_many_intents
111 intents = []
112 @switches.each do |sw|
113 rest = @switches - [sw]
114 intents = _create_intent sw, rest, intents
115puts intents.size
116 post_slice intents
117 end
118 post_slice intents, true
119 end
120
121 # pick a random src switch and create intents to all other switches
122 def create_random_intent
123 intents = []
124 sw = @switches.shuffle[0]
125 rest = @switches - [sw]
126 intents = _create_intent sw, rest, intents
127 post_slice intents, true
128 end
129
130 def post_slice intents, last=false
131 @bulk_limit = @bulk_limit.to_i
132 if intents.size >= @bulk_limit
133 post intents.slice!(0..(@bulk_limit - 1))
134 end
135 if last == true
136 loop do
137 new_bulk_limit = intents.size > @bulk_limit ? @bulk_limit : intents.size
138 post intents.slice!(0..(new_bulk_limit - 1))
139 break if new_bulk_limit < @bulk_limit
140 end
141 end
142 end
143
144 def post intents
145 json_data = intents.to_json
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800146 response = RestClient.post "http://#{@server}:#{@port}/wm/onos/datagrid/#{intent_op}/intents/json", json_data, :content_type => :json, :accept => :json
Nick Karanatsios88948d32014-02-18 15:14:30 -0800147 puts response
148 end
149
150 def parse_options options
151 max_switches = options[:max_switches].to_i || 4
152 @switches = (1..max_switches).to_a
Nick Karanatsios882235b2014-02-21 15:49:53 -0800153 @ports = (1..max_switches).to_a
Nick Karanatsios88948d32014-02-18 15:14:30 -0800154 @intent_id = options[:intent_id]
155 @intent_id ||= 1
Nick Karanatsios88948d32014-02-18 15:14:30 -0800156 @intent_type = options[:intent_type]
157 @intent_op = options[:intent_op]
158 @intent_op ||= "add"
159 @random_intent = options[:random_intent]
160 @random_intent ||= false
161 @server = options[:server]
162 @server ||= "127.0.0.1"
163 @port = options[:port]
164 @port ||= 8080
165 @bulk_limit = options[:bulk_limit]
166 @bulk_limit ||= 10000
167 end
168
169
170 def _create_intent src_switch, iterable_switches, json_intents
171 network_id = 1
172 iterable_switches.each_index do |sw_i|
Nick Karanatsios88948d32014-02-18 15:14:30 -0800173 intent = {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800174 :intent_id => "#{@intent_id}",
Nick Karanatsios88948d32014-02-18 15:14:30 -0800175 :intent_type => @intent_type,
176 :intent_op => @intent_op,
177 :srcSwitch => src_switch.to_s,
Nick Karanatsios882235b2014-02-21 15:49:53 -0800178 :srcPort => @ports[-1],
Nick Karanatsios88948d32014-02-18 15:14:30 -0800179 :srcMac => "00:00:c0:a8:#{mac_format(src_switch)}",
180 :dstSwitch => iterable_switches[sw_i].to_s,
Nick Karanatsios882235b2014-02-21 15:49:53 -0800181 :dstPort => @ports[-1],
Nick Karanatsios88948d32014-02-18 15:14:30 -0800182 :dstMac => "00:00:c0:a8:#{mac_format(iterable_switches[sw_i].to_i)}"
183 }
184puts intent.inspect
185 @intent_id = @intent_id + 1
186 json_intents << intent
187puts
188 end
Nick Karanatsios88948d32014-02-18 15:14:30 -0800189 json_intents
190 end
191
192 def mac_format number
193 if number > 255
194 divisor = number / 256
195 remainder = number % 256
196 return sprintf("%02x:%02x",divisor ,remainder)
197 end
198 "00:%02x" % number
199 end
200end
201
202intent = Intent.new options
203if options[:rest_op] == "get"
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800204 intent.get_intent options
Nick Karanatsios88948d32014-02-18 15:14:30 -0800205else
206 json_data = intent.post_intent
207end
208