blob: 59dea82d2ea34078092a0df71e025adabcb2ceea [file] [log] [blame]
Nick Karanatsios88948d32014-02-18 15:14:30 -08001require "rest-client"
2require "optparse"
3
4options = {
5 :rest_op => "add",
6 :intent_id => 123,
7 :intent_type => "shortest_intent_type",
8 :max_switches => 4,
9 :intent_op => "add"
10}
11
12parser = OptionParser.new do |opts|
Nick Karanatsiosed645df2014-02-20 23:22:29 -080013 opts.banner = "Usage [get-options] [post-options]"
14
15 opts.separator ""
16 opts.separator "Get options:"
17
18 opts.on('-G', '--get_intents', 'get intents state') do
19 options[:get_intents] = true
Nick Karanatsios88948d32014-02-18 15:14:30 -080020 options[:rest_op] = "get"
21 end
Nick Karanatsiosed645df2014-02-20 23:22:29 -080022 opts.on('-g', '--get_intent intent_id', 'get intent state') do |intent_id|
23 options[:rest_op] = "get"
24 options[:get_intent] = intent_id
25 end
26
27 opts.separator ""
28 opts.separator "Post options:"
29
Nick Karanatsios88948d32014-02-18 15:14:30 -080030 opts.on('-t', '--max_intents max_intents', 'max. number of intents') do |max_intents|
31 options[:max_intents] = max_intents
32 end
Nick Karanatsios88948d32014-02-18 15:14:30 -080033 opts.on('-i', '--intent_id intent_id', 'global intent id') do |id|
34 options[:intent_id] = id.to_i
35 end
36 # optional argument
37 opts.on('-s', '--shortest', 'create a shortest path intent') do
38 options[:intent_type] = "shortest_intent_type"
39 end
40 # optional argument
41 opts.on('-c', '--constrained', 'create a constrained shortest path intent') do
42 options[:intent_type] = "constrained_shortest_intent_type"
43 end
44 # optional argument
45 opts.on('-r', '--random_intent', 'create minimum no. of random intents') do
46 options[:random_intent] = true
47 end
48 opts.on('-m', '--max_switches max_switches', 'max. number of switches') do |max_switches|
49 options[:max_switches] = max_switches.to_i
50 end
51 opts.on('-o', '--intent_op add|remove', 'an operation to post an intent') do |operation|
52 options[:intent_op] = operation
53 end
54 opts.on('-w', '--server server', 'server to post intents') do |server|
55 options[:server] = server
56 end
57 opts.on('-p','--port port', 'server port') do |port|
58 options[:port] = port
59 end
60 opts.on('b', '--bulk_limit bulk_limit', 'bulk request upto this limit') do |bulk_limit|
61 options[:bulk_limit] = bulk_limit
62 end
63 opts.on('-h', '--help', 'Display help') do
64 puts opts
65 exit
66 end
67end
68parser.parse!
69
Nick Karanatsios88948d32014-02-18 15:14:30 -080070class Intent
71 attr_reader :switches, :ports, :intent_id
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -080072 attr_reader :intent_type, :intent_op
Nick Karanatsios88948d32014-02-18 15:14:30 -080073 attr_reader :random_intent, :server, :port
74 attr_reader :bulk_limit
75
76 def initialize options
77 parse_options options
78 end
79
80 def post_intent
81 create_specific_intent
82 end
83
Nick Karanatsiosed645df2014-02-20 23:22:29 -080084 def get_intent options
85 if options[:get_intents] == true
86 request = RestClient.get "http://#{@server}:#{@port}/wm/onos/datagrid/get/intents/json"
87 else
88 url = "http://#{@server}:#{@port}/wm/onos/datagrid/get/intent/#{options[:get_intent]}/json"
89 request = RestClient.get url
90 #request = RestClient.get "http://#{@server}:#{@port}/wm/onos/datagrid/get/intent/json",{:intent_id => options[:get_intent]}
91 end
Nick Karanatsios88948d32014-02-18 15:14:30 -080092 puts request
93 end
94
95 private
96
97 def create_specific_intent
98 if @random_intent == true
99 create_random_intent
100 else
101 create_many_intents
102 end
103 end
104
105 # create as many intents as the number of switches
106 def create_many_intents
107 intents = []
108 @switches.each do |sw|
109 rest = @switches - [sw]
110 intents = _create_intent sw, rest, intents
111puts intents.size
112 post_slice intents
113 end
114 post_slice intents, true
115 end
116
117 # pick a random src switch and create intents to all other switches
118 def create_random_intent
119 intents = []
120 sw = @switches.shuffle[0]
121 rest = @switches - [sw]
122 intents = _create_intent sw, rest, intents
123 post_slice intents, true
124 end
125
126 def post_slice intents, last=false
127 @bulk_limit = @bulk_limit.to_i
128 if intents.size >= @bulk_limit
129 post intents.slice!(0..(@bulk_limit - 1))
130 end
131 if last == true
132 loop do
133 new_bulk_limit = intents.size > @bulk_limit ? @bulk_limit : intents.size
134 post intents.slice!(0..(new_bulk_limit - 1))
135 break if new_bulk_limit < @bulk_limit
136 end
137 end
138 end
139
140 def post intents
141 json_data = intents.to_json
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800142 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 -0800143 puts response
144 end
145
146 def parse_options options
147 max_switches = options[:max_switches].to_i || 4
148 @switches = (1..max_switches).to_a
Nick Karanatsios882235b2014-02-21 15:49:53 -0800149 @ports = (1..max_switches).to_a
Nick Karanatsios88948d32014-02-18 15:14:30 -0800150 @intent_id = options[:intent_id]
151 @intent_id ||= 1
Nick Karanatsios88948d32014-02-18 15:14:30 -0800152 @intent_type = options[:intent_type]
153 @intent_op = options[:intent_op]
154 @intent_op ||= "add"
155 @random_intent = options[:random_intent]
156 @random_intent ||= false
157 @server = options[:server]
158 @server ||= "127.0.0.1"
159 @port = options[:port]
160 @port ||= 8080
161 @bulk_limit = options[:bulk_limit]
162 @bulk_limit ||= 10000
163 end
164
165
166 def _create_intent src_switch, iterable_switches, json_intents
167 network_id = 1
168 iterable_switches.each_index do |sw_i|
Nick Karanatsios88948d32014-02-18 15:14:30 -0800169 intent = {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800170 :intent_id => "#{@intent_id}",
Nick Karanatsios88948d32014-02-18 15:14:30 -0800171 :intent_type => @intent_type,
172 :intent_op => @intent_op,
173 :srcSwitch => src_switch.to_s,
Nick Karanatsios882235b2014-02-21 15:49:53 -0800174 :srcPort => @ports[-1],
Nick Karanatsios88948d32014-02-18 15:14:30 -0800175 :srcMac => "00:00:c0:a8:#{mac_format(src_switch)}",
176 :dstSwitch => iterable_switches[sw_i].to_s,
Nick Karanatsios882235b2014-02-21 15:49:53 -0800177 :dstPort => @ports[-1],
Nick Karanatsios88948d32014-02-18 15:14:30 -0800178 :dstMac => "00:00:c0:a8:#{mac_format(iterable_switches[sw_i].to_i)}"
179 }
180puts intent.inspect
181 @intent_id = @intent_id + 1
182 json_intents << intent
183puts
184 end
Nick Karanatsios88948d32014-02-18 15:14:30 -0800185 json_intents
186 end
187
188 def mac_format number
189 if number > 255
190 divisor = number / 256
191 remainder = number % 256
192 return sprintf("%02x:%02x",divisor ,remainder)
193 end
194 "00:%02x" % number
195 end
196end
197
198intent = Intent.new options
199if options[:rest_op] == "get"
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800200 intent.get_intent options
Nick Karanatsios88948d32014-02-18 15:14:30 -0800201else
202 json_data = intent.post_intent
203end
204