Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 1 | require "rest-client" |
| 2 | require "optparse" |
| 3 | |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame^] | 4 | options = { :intent_id => 123, :intent_type => "shortest_intent_type", :max_switches => 4 } |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 5 | |
| 6 | parser = OptionParser.new do |opts| |
| 7 | opts.banner = "Usage add-intent [options]" |
| 8 | opts.on('-t', '--max_intents max_intents', 'max. number of intents') do |max_intents| |
| 9 | options[:max_intents] = max_intents |
| 10 | end |
| 11 | opts.on('-a', '--application application_id', 'set application id') do |appl_id| |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame^] | 12 | options[:application_id] = appl_id.to_i |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 13 | end |
| 14 | opts.on('-i', '--intent_id intent_id', 'global intent id') do |id| |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame^] | 15 | options[:intent_id] = id.to_i |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 16 | end |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame^] | 17 | opts.on('-s', '--shortest', 'create a shortest path intent') do |
| 18 | options[:intent_type] = "shortest_intent_type" |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 19 | end |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame^] | 20 | opts.on('-c', '--constrained', 'create a constrained shortest path intent') do |
| 21 | options[:intent_type] = "constrained_shortest_intent_type" |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 22 | end |
| 23 | opts.on('-m', '--max_switches max_switches', 'max. number of switches') do |max_switches| |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame^] | 24 | options[:max_switches] = max_switches.to_i |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 25 | end |
| 26 | opts.on('-h', '--help', 'Display help') do |
| 27 | puts opts |
| 28 | exit |
| 29 | end |
| 30 | end |
| 31 | parser.parse! |
| 32 | |
| 33 | puts options.inspect |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame^] | 34 | server = options[:server] |
| 35 | server ||= "127.0.0.1" |
| 36 | port = options[:port] |
| 37 | port ||= 8080 |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 38 | |
| 39 | def rand_mac |
| 40 | mac = `openssl rand -hex 6` |
| 41 | mac.scan(/(..)/).join(":") |
| 42 | end |
| 43 | |
| 44 | def rand_switch |
| 45 | switch = `openssl rand -hex 5`.chomp |
| 46 | end |
| 47 | |
| 48 | class Intent |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame^] | 49 | attr_reader :switches |
| 50 | attr_reader :ports |
| 51 | attr_reader :intent_id |
| 52 | attr_reader :application_id |
| 53 | attr_reader :intent_type |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 54 | |
| 55 | def initialize options |
| 56 | parse_options options |
| 57 | end |
| 58 | |
| 59 | def create_intent |
| 60 | json_intents = [] |
| 61 | @switches.each do |sw| |
| 62 | rest = switches - [sw] |
| 63 | json_intents = _create_intent sw, rest, json_intents |
| 64 | end |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame^] | 65 | json_intents |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 66 | end |
| 67 | |
| 68 | def parse_options options |
| 69 | max_switches = options[:max_switches].to_i || 4 |
| 70 | @switches = (1..max_switches).to_a |
| 71 | @ports = (1..(max_switches - 1)).to_a |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame^] | 72 | @intent_id = options[:intent_id] |
| 73 | @intent_id ||= 1 |
| 74 | @application_id = options[:application_id] |
| 75 | @application_id ||= 1 |
| 76 | @intent_type = options[:intent_type] |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 77 | end |
| 78 | |
| 79 | |
| 80 | def _create_intent src_switch, iterable_switches, json_intents |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame^] | 81 | network_id = 1 |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 82 | iterable_switches.each_index do |sw_i| |
| 83 | dst_switch = iterable_switches[sw_i] |
| 84 | sw_set = @switches - [dst_switch] |
| 85 | dst_port = sw_set.index(src_switch) |
| 86 | dst_port = dst_port + 1 |
| 87 | intent = { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame^] | 88 | :intent_id => "#{@application_id}:#{@intent_id}", |
| 89 | :intent_type => @intent_type, |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 90 | :srcSwitch => src_switch.to_s, |
| 91 | :srcPort => @ports[sw_i], |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame^] | 92 | :srcMac => "00:00:c0:a8:#{mac_format(src_switch)}", |
| 93 | :dstSwitch => iterable_switches[sw_i].to_s, |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 94 | :dstPort => dst_port, |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame^] | 95 | :dstMac => "00:00:c0:a8:#{mac_format(iterable_switches[sw_i].to_i)}" |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 96 | } |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame^] | 97 | puts intent |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 98 | @intent_id = @intent_id + 1 |
| 99 | json_intents << intent |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame^] | 100 | puts |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 101 | end |
| 102 | #sha256 = Digest::SHA256.new |
| 103 | #sha256.update intent_hash.to_s |
| 104 | #puts sha256.hexdigest |
| 105 | #puts "intent hash = #{intent_hash}" |
| 106 | json_intents |
| 107 | end |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame^] | 108 | |
| 109 | def mac_format number |
| 110 | if number > 255 |
| 111 | divisor = number / 256 |
| 112 | remainder = number % 256 |
| 113 | return sprintf("%02x:%02x",divisor ,remainder) |
| 114 | end |
| 115 | "00:%02x" % number |
| 116 | end |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 117 | end |
| 118 | |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 119 | intent = Intent.new options |
| 120 | json_data = intent.create_intent |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame^] | 121 | response = RestClient.post "http://#{server}:#{port}/wm/onos/datagrid/add/intent/json", json_data.to_json, :content_type => :json, :accept => :json |
| 122 | puts response.inspect |