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