blob: 1627aeaa9ab3fda26df40ea03948b239d59a17b2 [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|
13 opts.banner = "Usage add-get-intent [options]"
14 opts.on('-g', '--get_intents', 'get intents state') do
15 options[:rest_op] = "get"
16 end
17 opts.on('-t', '--max_intents max_intents', 'max. number of intents') do |max_intents|
18 options[:max_intents] = max_intents
19 end
20 opts.on('-l', '--application application_id', 'set application id') do |appl_id|
21 options[:application_id] = appl_id.to_i
22 end
23 opts.on('-i', '--intent_id intent_id', 'global intent id') do |id|
24 options[:intent_id] = id.to_i
25 end
26 # optional argument
27 opts.on('-s', '--shortest', 'create a shortest path intent') do
28 options[:intent_type] = "shortest_intent_type"
29 end
30 # optional argument
31 opts.on('-c', '--constrained', 'create a constrained shortest path intent') do
32 options[:intent_type] = "constrained_shortest_intent_type"
33 end
34 # optional argument
35 opts.on('-r', '--random_intent', 'create minimum no. of random intents') do
36 options[:random_intent] = true
37 end
38 opts.on('-m', '--max_switches max_switches', 'max. number of switches') do |max_switches|
39 options[:max_switches] = max_switches.to_i
40 end
41 opts.on('-o', '--intent_op add|remove', 'an operation to post an intent') do |operation|
42 options[:intent_op] = operation
43 end
44 opts.on('-w', '--server server', 'server to post intents') do |server|
45 options[:server] = server
46 end
47 opts.on('-p','--port port', 'server port') do |port|
48 options[:port] = port
49 end
50 opts.on('b', '--bulk_limit bulk_limit', 'bulk request upto this limit') do |bulk_limit|
51 options[:bulk_limit] = bulk_limit
52 end
53 opts.on('-h', '--help', 'Display help') do
54 puts opts
55 exit
56 end
57end
58parser.parse!
59
60def rand_mac
61 mac = `openssl rand -hex 6`
62 mac.scan(/(..)/).join(":")
63end
64
65def rand_switch
66 switch = `openssl rand -hex 5`.chomp
67end
68
69class Intent
70 attr_reader :switches, :ports, :intent_id
71 attr_reader :application_id, :intent_type, :intent_op
72 attr_reader :random_intent, :server, :port
73 attr_reader :bulk_limit
74
75 def initialize options
76 parse_options options
77 end
78
79 def post_intent
80 create_specific_intent
81 end
82
83 def get_intent
84 request = RestClient.get "http://#{@server}:#{@port}/wm/onos/datagrid/get/intents/json"
85 puts request
86 end
87
88 private
89
90 def create_specific_intent
91 if @random_intent == true
92 create_random_intent
93 else
94 create_many_intents
95 end
96 end
97
98 # create as many intents as the number of switches
99 def create_many_intents
100 intents = []
101 @switches.each do |sw|
102 rest = @switches - [sw]
103 intents = _create_intent sw, rest, intents
104puts intents.size
105 post_slice intents
106 end
107 post_slice intents, true
108 end
109
110 # pick a random src switch and create intents to all other switches
111 def create_random_intent
112 intents = []
113 sw = @switches.shuffle[0]
114 rest = @switches - [sw]
115 intents = _create_intent sw, rest, intents
116 post_slice intents, true
117 end
118
119 def post_slice intents, last=false
120 @bulk_limit = @bulk_limit.to_i
121 if intents.size >= @bulk_limit
122 post intents.slice!(0..(@bulk_limit - 1))
123 end
124 if last == true
125 loop do
126 new_bulk_limit = intents.size > @bulk_limit ? @bulk_limit : intents.size
127 post intents.slice!(0..(new_bulk_limit - 1))
128 break if new_bulk_limit < @bulk_limit
129 end
130 end
131 end
132
133 def post intents
134 json_data = intents.to_json
135 response = RestClient.post "http://#{@server}:#{@port}/wm/onos/datagrid/#{intent_op}/intent/json", json_data, :content_type => :json, :accept => :json
136 puts response
137 end
138
139 def parse_options options
140 max_switches = options[:max_switches].to_i || 4
141 @switches = (1..max_switches).to_a
142 @ports = (1..(max_switches - 1)).to_a
143 @intent_id = options[:intent_id]
144 @intent_id ||= 1
145 @application_id = options[:application_id]
146 @application_id ||= 1
147 @intent_type = options[:intent_type]
148 @intent_op = options[:intent_op]
149 @intent_op ||= "add"
150 @random_intent = options[:random_intent]
151 @random_intent ||= false
152 @server = options[:server]
153 @server ||= "127.0.0.1"
154 @port = options[:port]
155 @port ||= 8080
156 @bulk_limit = options[:bulk_limit]
157 @bulk_limit ||= 10000
158 end
159
160
161 def _create_intent src_switch, iterable_switches, json_intents
162 network_id = 1
163 iterable_switches.each_index do |sw_i|
164 dst_switch = iterable_switches[sw_i]
165 sw_set = @switches - [dst_switch]
166 dst_port = sw_set.index(src_switch)
167 dst_port = dst_port + 1
168 intent = {
169 :intent_id => "#{@application_id}:#{@intent_id}",
170 :intent_type => @intent_type,
171 :intent_op => @intent_op,
172 :srcSwitch => src_switch.to_s,
173 :srcPort => @ports[sw_i],
174 :srcMac => "00:00:c0:a8:#{mac_format(src_switch)}",
175 :dstSwitch => iterable_switches[sw_i].to_s,
176 :dstPort => dst_port,
177 :dstMac => "00:00:c0:a8:#{mac_format(iterable_switches[sw_i].to_i)}"
178 }
179puts intent.inspect
180 @intent_id = @intent_id + 1
181 json_intents << intent
182puts
183 end
184 #sha256 = Digest::SHA256.new
185 #sha256.update intent_hash.to_s
186 #puts sha256.hexdigest
187 #puts "intent hash = #{intent_hash}"
188 json_intents
189 end
190
191 def mac_format number
192 if number > 255
193 divisor = number / 256
194 remainder = number % 256
195 return sprintf("%02x:%02x",divisor ,remainder)
196 end
197 "00:%02x" % number
198 end
199end
200
201intent = Intent.new options
202if options[:rest_op] == "get"
203 intent.get_intent
204else
205 json_data = intent.post_intent
206end
207