blob: b1d8dfd46012cae4833ab48b89eed5f3c1eadba1 [file] [log] [blame]
Nick Karanatsios1a4a2002014-02-14 04:35:39 -08001require "rest-client"
2require "optparse"
3
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -08004options = { :intent_id => 123, :intent_type => "shortest_intent_type", :max_switches => 4 }
Nick Karanatsios1a4a2002014-02-14 04:35:39 -08005
6parser = 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 Karanatsios5fcf93e2014-02-15 14:13:55 -080012 options[:application_id] = appl_id.to_i
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080013 end
14 opts.on('-i', '--intent_id intent_id', 'global intent id') do |id|
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080015 options[:intent_id] = id.to_i
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080016 end
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080017 opts.on('-s', '--shortest', 'create a shortest path intent') do
18 options[:intent_type] = "shortest_intent_type"
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080019 end
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080020 opts.on('-c', '--constrained', 'create a constrained shortest path intent') do
21 options[:intent_type] = "constrained_shortest_intent_type"
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080022 end
23 opts.on('-m', '--max_switches max_switches', 'max. number of switches') do |max_switches|
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080024 options[:max_switches] = max_switches.to_i
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080025 end
26 opts.on('-h', '--help', 'Display help') do
27 puts opts
28 exit
29 end
30end
31parser.parse!
32
33puts options.inspect
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080034server = options[:server]
35server ||= "127.0.0.1"
36port = options[:port]
37port ||= 8080
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080038
39def rand_mac
40 mac = `openssl rand -hex 6`
41 mac.scan(/(..)/).join(":")
42end
43
44def rand_switch
45 switch = `openssl rand -hex 5`.chomp
46end
47
48class Intent
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080049 attr_reader :switches
50 attr_reader :ports
51 attr_reader :intent_id
52 attr_reader :application_id
53 attr_reader :intent_type
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080054
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 Karanatsios5fcf93e2014-02-15 14:13:55 -080065 json_intents
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080066 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 Karanatsios5fcf93e2014-02-15 14:13:55 -080072 @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 Karanatsios1a4a2002014-02-14 04:35:39 -080077 end
78
79
80 def _create_intent src_switch, iterable_switches, json_intents
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080081 network_id = 1
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080082 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 Karanatsios5fcf93e2014-02-15 14:13:55 -080088 :intent_id => "#{@application_id}:#{@intent_id}",
89 :intent_type => @intent_type,
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080090 :srcSwitch => src_switch.to_s,
91 :srcPort => @ports[sw_i],
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080092 :srcMac => "00:00:c0:a8:#{mac_format(src_switch)}",
93 :dstSwitch => iterable_switches[sw_i].to_s,
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080094 :dstPort => dst_port,
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080095 :dstMac => "00:00:c0:a8:#{mac_format(iterable_switches[sw_i].to_i)}"
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080096 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080097puts intent
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080098 @intent_id = @intent_id + 1
99 json_intents << intent
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800100puts
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800101 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 Karanatsios5fcf93e2014-02-15 14:13:55 -0800108
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 Karanatsios1a4a2002014-02-14 04:35:39 -0800117end
118
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800119intent = Intent.new options
120json_data = intent.create_intent
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800121response = RestClient.post "http://#{server}:#{port}/wm/onos/datagrid/add/intent/json", json_data.to_json, :content_type => :json, :accept => :json
122puts response.inspect