Nick Karanatsios | 31ebc47 | 2014-02-24 19:15:37 -0800 | [diff] [blame] | 1 | #!/usr/bin/env ruby |
| 2 | |
| 3 | require "rest-client" |
| 4 | require "optparse" |
| 5 | |
| 6 | options = { |
| 7 | :rest_op => "add", |
| 8 | :intent_id => 1, |
| 9 | :intent_category => "high", |
| 10 | :intent_type => "shortest_intent_type", |
| 11 | :max_switches => 4, |
| 12 | :intent_op => "add" |
| 13 | } |
| 14 | |
| 15 | parser = OptionParser.new do |opts| |
| 16 | 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 |
| 23 | options[:rest_op] = "get" |
| 24 | end |
| 25 | 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 | |
| 30 | opts.separator "" |
| 31 | opts.separator "Purge options" |
| 32 | opts.on('-d', '--purge', 'purge all intents') do |
| 33 | options[:rest_op] = "delete" |
| 34 | end |
| 35 | |
| 36 | opts.separator "" |
| 37 | opts.separator "Post options:" |
| 38 | |
| 39 | opts.on('-t', '--max_intents max_intents', 'max. number of intents') do |max_intents| |
| 40 | options[:max_intents] = max_intents |
| 41 | end |
| 42 | opts.on('-e', '--intent_category intent_category', 'intent category high|low') do |intent_category| |
| 43 | options[:intent_category] = intent_category |
| 44 | end |
| 45 | 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 |
Toshio Koide | 93797dc | 2014-02-27 23:54:26 -0800 | [diff] [blame] | 75 | opts.on('f', '--freeze_intents', 'freeze dont reroute intents') do |
| 76 | options[:freeze] = "F" |
| 77 | end |
Nick Karanatsios | 31ebc47 | 2014-02-24 19:15:37 -0800 | [diff] [blame] | 78 | opts.on('-h', '--help', 'Display help') do |
| 79 | puts opts |
| 80 | exit |
| 81 | end |
| 82 | end |
| 83 | parser.parse! |
| 84 | |
| 85 | |
| 86 | class Intent |
| 87 | attr_reader :switches, :ports, :intent_id |
| 88 | attr_reader :intent_type, :intent_op |
| 89 | attr_reader :random_intent, :server, :port |
| 90 | attr_reader :bulk_limit, :max_switches |
| 91 | |
| 92 | def initialize options |
| 93 | parse_options options |
| 94 | end |
| 95 | |
| 96 | def post_intent |
| 97 | create_specific_intent |
| 98 | end |
| 99 | |
| 100 | def get_intent options |
| 101 | if options[:get_intents] == true |
| 102 | request = RestClient.get "http://#{@server}:#{@port}/wm/onos/datagrid/get/intents/#{options[:intent_category]}/json" |
| 103 | else |
| 104 | url = "http://#{@server}:#{@port}/wm/onos/datagrid/get/intent/#{options[:intent_category]}/#{options[:get_intent]}/json" |
| 105 | request = RestClient.get url |
| 106 | end |
| 107 | puts request |
| 108 | end |
| 109 | |
| 110 | def purge_intents |
| 111 | response = RestClient.delete "http://#{@server}:#{@port}/wm/onos/datagrid/delete/intents/json" |
| 112 | puts response |
| 113 | end |
| 114 | |
| 115 | private |
| 116 | |
| 117 | def create_specific_intent |
| 118 | if @random_intent == true |
| 119 | create_random_intent |
| 120 | else |
| 121 | create_many_intents |
| 122 | end |
| 123 | end |
| 124 | |
| 125 | # create as many intents as the number of switches |
| 126 | def create_many_intents |
| 127 | intents = [] |
| 128 | intents = _create_intent intents |
| 129 | post intents |
| 130 | end |
| 131 | |
| 132 | |
| 133 | # pick a random src switch and create intents to all other switches |
| 134 | def create_random_intent |
| 135 | intents = [] |
| 136 | sw = @switches.shuffle[0] |
| 137 | rest = @switches - [sw] |
| 138 | intents = _create_intent sw, rest, intents |
| 139 | post_slice intents, true |
| 140 | end |
| 141 | |
| 142 | def post_slice intents, last=false |
| 143 | @bulk_limit = @bulk_limit.to_i |
| 144 | if intents.size >= @bulk_limit |
| 145 | post intents.slice!(0..(@bulk_limit - 1)) |
| 146 | end |
| 147 | if last == true |
| 148 | loop do |
| 149 | new_bulk_limit = intents.size > @bulk_limit ? @bulk_limit : intents.size |
| 150 | post intents.slice!(0..(new_bulk_limit - 1)) |
| 151 | break if new_bulk_limit < @bulk_limit |
| 152 | end |
| 153 | end |
| 154 | end |
| 155 | |
| 156 | def post intents |
| 157 | json_data = intents.to_json |
| 158 | response = RestClient.post "http://#{@server}:#{@port}/wm/onos/datagrid/add/intents/json", json_data, :content_type => :json, :accept => :json |
| 159 | puts response |
| 160 | end |
| 161 | |
| 162 | def parse_options options |
| 163 | @max_switches = options[:max_switches].to_i || 4 |
| 164 | @switches = (1..max_switches).to_a |
| 165 | @ports = (1..max_switches).to_a |
| 166 | @intent_id = options[:intent_id] |
| 167 | @intent_id ||= 1 |
| 168 | @intent_type = options[:intent_type] |
| 169 | @intent_op = options[:intent_op] |
| 170 | @intent_op ||= "add" |
| 171 | @random_intent = options[:random_intent] |
| 172 | @random_intent ||= false |
| 173 | @server = options[:server] |
| 174 | @server ||= "127.0.0.1" |
| 175 | @port = options[:port] |
| 176 | @port ||= 8080 |
| 177 | @bulk_limit = options[:bulk_limit] |
| 178 | @bulk_limit ||= 10000 |
Toshio Koide | 93797dc | 2014-02-27 23:54:26 -0800 | [diff] [blame] | 179 | @freeze = options[:freeze] |
| 180 | @freeze ||= "" |
Nick Karanatsios | 31ebc47 | 2014-02-24 19:15:37 -0800 | [diff] [blame] | 181 | end |
| 182 | |
| 183 | |
| 184 | def _create_intent json_intents |
| 185 | (0...@max_switches).each do |idx| |
| 186 | intent = { |
Toshio Koide | 93797dc | 2014-02-27 23:54:26 -0800 | [diff] [blame] | 187 | :intent_id => "#{@freeze}#{@intent_id}", |
Nick Karanatsios | 31ebc47 | 2014-02-24 19:15:37 -0800 | [diff] [blame] | 188 | :intent_type => @intent_type, |
| 189 | :intent_op => @intent_op, |
| 190 | :srcSwitch => "258".to_s, |
| 191 | :srcPort => 65534, |
| 192 | :srcMac => "00:00:c0:#{mac_format(idx)}", |
| 193 | :dstSwitch => "514".to_s, |
| 194 | :dstPort => 65534, |
| 195 | :dstMac => "00:00:c1:#{mac_format(idx)}" |
| 196 | } |
| 197 | puts intent.inspect |
| 198 | @intent_id = @intent_id + 1 |
| 199 | json_intents << intent |
| 200 | puts |
| 201 | end |
| 202 | json_intents |
| 203 | end |
| 204 | |
| 205 | def mac_format number |
| 206 | if number > 255 |
| 207 | divisor1 = number / 256 |
| 208 | remainder1 = number % 256 |
| 209 | divisor2 = divisor1 / 256 |
| 210 | remainder2 = divisor1 % 256 |
| 211 | return sprintf("%02x:%02x:%02x",divisor2 ,remainder2, remainder1) |
| 212 | end |
| 213 | "00:00:%02x" % number |
| 214 | end |
| 215 | end |
| 216 | |
| 217 | intent = Intent.new options |
| 218 | if options[:rest_op] == "get" |
| 219 | intent.get_intent options |
| 220 | elsif options[:rest_op] == "delete" |
| 221 | intent.purge_intents |
| 222 | else |
| 223 | json_data = intent.post_intent |
| 224 | end |
| 225 | |