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