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", |
| 6 | :intent_id => 123, |
| 7 | :intent_type => "shortest_intent_type", |
| 8 | :max_switches => 4, |
| 9 | :intent_op => "add" |
| 10 | } |
| 11 | |
| 12 | parser = 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 |
| 57 | end |
| 58 | parser.parse! |
| 59 | |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 60 | |
| 61 | class Intent |
| 62 | attr_reader :switches, :ports, :intent_id |
| 63 | attr_reader :application_id, :intent_type, :intent_op |
| 64 | attr_reader :random_intent, :server, :port |
| 65 | attr_reader :bulk_limit |
| 66 | |
| 67 | def initialize options |
| 68 | parse_options options |
| 69 | end |
| 70 | |
| 71 | def post_intent |
| 72 | create_specific_intent |
| 73 | end |
| 74 | |
| 75 | def get_intent |
| 76 | request = RestClient.get "http://#{@server}:#{@port}/wm/onos/datagrid/get/intents/json" |
| 77 | puts request |
| 78 | end |
| 79 | |
| 80 | private |
| 81 | |
| 82 | def create_specific_intent |
| 83 | if @random_intent == true |
| 84 | create_random_intent |
| 85 | else |
| 86 | create_many_intents |
| 87 | end |
| 88 | end |
| 89 | |
| 90 | # create as many intents as the number of switches |
| 91 | def create_many_intents |
| 92 | intents = [] |
| 93 | @switches.each do |sw| |
| 94 | rest = @switches - [sw] |
| 95 | intents = _create_intent sw, rest, intents |
| 96 | puts intents.size |
| 97 | post_slice intents |
| 98 | end |
| 99 | post_slice intents, true |
| 100 | end |
| 101 | |
| 102 | # pick a random src switch and create intents to all other switches |
| 103 | def create_random_intent |
| 104 | intents = [] |
| 105 | sw = @switches.shuffle[0] |
| 106 | rest = @switches - [sw] |
| 107 | intents = _create_intent sw, rest, intents |
| 108 | post_slice intents, true |
| 109 | end |
| 110 | |
| 111 | def post_slice intents, last=false |
| 112 | @bulk_limit = @bulk_limit.to_i |
| 113 | if intents.size >= @bulk_limit |
| 114 | post intents.slice!(0..(@bulk_limit - 1)) |
| 115 | end |
| 116 | if last == true |
| 117 | loop do |
| 118 | new_bulk_limit = intents.size > @bulk_limit ? @bulk_limit : intents.size |
| 119 | post intents.slice!(0..(new_bulk_limit - 1)) |
| 120 | break if new_bulk_limit < @bulk_limit |
| 121 | end |
| 122 | end |
| 123 | end |
| 124 | |
| 125 | def post intents |
| 126 | json_data = intents.to_json |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame^] | 127 | response = RestClient.post "http://#{@server}:#{@port}/wm/onos/datagrid/#{intent_op}/intents/json", json_data, :content_type => :json, :accept => :json |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 128 | puts response |
| 129 | end |
| 130 | |
| 131 | def parse_options options |
| 132 | max_switches = options[:max_switches].to_i || 4 |
| 133 | @switches = (1..max_switches).to_a |
| 134 | @ports = (1..(max_switches - 1)).to_a |
| 135 | @intent_id = options[:intent_id] |
| 136 | @intent_id ||= 1 |
| 137 | @application_id = options[:application_id] |
| 138 | @application_id ||= 1 |
| 139 | @intent_type = options[:intent_type] |
| 140 | @intent_op = options[:intent_op] |
| 141 | @intent_op ||= "add" |
| 142 | @random_intent = options[:random_intent] |
| 143 | @random_intent ||= false |
| 144 | @server = options[:server] |
| 145 | @server ||= "127.0.0.1" |
| 146 | @port = options[:port] |
| 147 | @port ||= 8080 |
| 148 | @bulk_limit = options[:bulk_limit] |
| 149 | @bulk_limit ||= 10000 |
| 150 | end |
| 151 | |
| 152 | |
| 153 | def _create_intent src_switch, iterable_switches, json_intents |
| 154 | network_id = 1 |
| 155 | iterable_switches.each_index do |sw_i| |
| 156 | dst_switch = iterable_switches[sw_i] |
| 157 | sw_set = @switches - [dst_switch] |
| 158 | dst_port = sw_set.index(src_switch) |
| 159 | dst_port = dst_port + 1 |
| 160 | intent = { |
| 161 | :intent_id => "#{@application_id}:#{@intent_id}", |
| 162 | :intent_type => @intent_type, |
| 163 | :intent_op => @intent_op, |
| 164 | :srcSwitch => src_switch.to_s, |
| 165 | :srcPort => @ports[sw_i], |
| 166 | :srcMac => "00:00:c0:a8:#{mac_format(src_switch)}", |
| 167 | :dstSwitch => iterable_switches[sw_i].to_s, |
| 168 | :dstPort => dst_port, |
| 169 | :dstMac => "00:00:c0:a8:#{mac_format(iterable_switches[sw_i].to_i)}" |
| 170 | } |
| 171 | puts intent.inspect |
| 172 | @intent_id = @intent_id + 1 |
| 173 | json_intents << intent |
| 174 | puts |
| 175 | end |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 176 | json_intents |
| 177 | end |
| 178 | |
| 179 | def mac_format number |
| 180 | if number > 255 |
| 181 | divisor = number / 256 |
| 182 | remainder = number % 256 |
| 183 | return sprintf("%02x:%02x",divisor ,remainder) |
| 184 | end |
| 185 | "00:%02x" % number |
| 186 | end |
| 187 | end |
| 188 | |
| 189 | intent = Intent.new options |
| 190 | if options[:rest_op] == "get" |
| 191 | intent.get_intent |
| 192 | else |
| 193 | json_data = intent.post_intent |
| 194 | end |
| 195 | |