Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 1 | require "rest-client" |
| 2 | require "optparse" |
| 3 | |
| 4 | options = { |
| 5 | :rest_op => "add", |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 6 | :intent_id => 1, |
| 7 | :intent_category => "high", |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 8 | :intent_type => "shortest_intent_type", |
| 9 | :max_switches => 4, |
| 10 | :intent_op => "add" |
| 11 | } |
| 12 | |
| 13 | parser = OptionParser.new do |opts| |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 14 | 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 Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 21 | options[:rest_op] = "get" |
| 22 | end |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 23 | 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 Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 31 | opts.on('-t', '--max_intents max_intents', 'max. number of intents') do |max_intents| |
| 32 | options[:max_intents] = max_intents |
| 33 | end |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 34 | opts.on('-e', '--intent_category intent_category', 'intent category high|low') do |intent_category| |
| 35 | options[:intent_category] = intent_category |
| 36 | end |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 37 | 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 |
| 71 | end |
| 72 | parser.parse! |
| 73 | |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 74 | |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 75 | class Intent |
| 76 | attr_reader :switches, :ports, :intent_id |
Nick Karanatsios | da9f36f | 2014-02-21 01:27:02 -0800 | [diff] [blame] | 77 | attr_reader :intent_type, :intent_op |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 78 | 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 Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 89 | def get_intent options |
| 90 | if options[:get_intents] == true |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 91 | request = RestClient.get "http://#{@server}:#{@port}/wm/onos/datagrid/get/intents/#{options[:intent_category]}/json" |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 92 | else |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 93 | url = "http://#{@server}:#{@port}/wm/onos/datagrid/get/intent/#{options[:intent_category]}/#{options[:get_intent]}/json" |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 94 | request = RestClient.get url |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 95 | end |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 96 | 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 |
| 115 | puts 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 Karanatsios | a1bad35 | 2014-02-22 14:16:34 -0800 | [diff] [blame] | 146 | response = RestClient.post "http://#{@server}:#{@port}/wm/onos/datagrid/add/intents/json", json_data, :content_type => :json, :accept => :json |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 147 | 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 Karanatsios | 882235b | 2014-02-21 15:49:53 -0800 | [diff] [blame] | 153 | @ports = (1..max_switches).to_a |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 154 | @intent_id = options[:intent_id] |
| 155 | @intent_id ||= 1 |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 156 | @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 Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 173 | intent = { |
Nick Karanatsios | da9f36f | 2014-02-21 01:27:02 -0800 | [diff] [blame] | 174 | :intent_id => "#{@intent_id}", |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 175 | :intent_type => @intent_type, |
| 176 | :intent_op => @intent_op, |
| 177 | :srcSwitch => src_switch.to_s, |
Nick Karanatsios | 882235b | 2014-02-21 15:49:53 -0800 | [diff] [blame] | 178 | :srcPort => @ports[-1], |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 179 | :srcMac => "00:00:c0:a8:#{mac_format(src_switch)}", |
| 180 | :dstSwitch => iterable_switches[sw_i].to_s, |
Nick Karanatsios | 882235b | 2014-02-21 15:49:53 -0800 | [diff] [blame] | 181 | :dstPort => @ports[-1], |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 182 | :dstMac => "00:00:c0:a8:#{mac_format(iterable_switches[sw_i].to_i)}" |
| 183 | } |
| 184 | puts intent.inspect |
| 185 | @intent_id = @intent_id + 1 |
| 186 | json_intents << intent |
| 187 | puts |
| 188 | end |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 189 | 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 |
| 200 | end |
| 201 | |
| 202 | intent = Intent.new options |
| 203 | if options[:rest_op] == "get" |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 204 | intent.get_intent options |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 205 | else |
| 206 | json_data = intent.post_intent |
| 207 | end |
| 208 | |