Class: OpenFlow::Controller::Controller

Inherits:
Object
  • Object
show all
Includes:
Protocol
Defined in:
lib/openflow-controller/controller.rb

Constant Summary collapse

DEFAULT_IP_ADDRESS =
'0.0.0.0'
DEFAULT_TCP_PORT =
6633

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(debug = false) ⇒ Controller

Returns a new instance of Controller.



36
37
38
39
40
41
42
43
44
45
# File 'lib/openflow-controller/controller.rb', line 36

def initialize(debug = false)
  @switches = {}
  @messages = {}
  @logger = Logger.new($stdout).tap do |logger|
    logger.formatter = proc do |severity, datetime, _progname, msg|
      "#{datetime} (#{severity}) -- #{msg}\n"
    end
    logger.level = debug ? Logger::DEBUG : Logger::INFO
  end
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



34
35
36
# File 'lib/openflow-controller/controller.rb', line 34

def logger
  @logger
end

Class Method Details

.create(*args) ⇒ Object



21
22
23
# File 'lib/openflow-controller/controller.rb', line 21

def self.create(*args)
  (@controller_class || self).new(*args)
end

.inherited(subclass) ⇒ Object



17
18
19
# File 'lib/openflow-controller/controller.rb', line 17

def self.inherited(subclass)
  @controller_class = subclass
end

.timer_event(handler, options) ⇒ Object



25
26
27
28
# File 'lib/openflow-controller/controller.rb', line 25

def self.timer_event(handler, options)
  @timer_handlers ||= {}
  @timer_handlers[handler] = options.fetch(:interval)
end

.timer_handlersObject



30
31
32
# File 'lib/openflow-controller/controller.rb', line 30

def self.timer_handlers
  @timer_handlers || {}
end

Instance Method Details

#broadcast(msg) ⇒ Object



76
77
78
# File 'lib/openflow-controller/controller.rb', line 76

def broadcast(msg)
  datapath_ids.each { |did| send_message did, msg }
end

#datapath_idsObject



60
61
62
# File 'lib/openflow-controller/controller.rb', line 60

def datapath_ids
  @switches.keys.map(&:to_i)
end

#echo_request(datapath_id, msg) ⇒ Object



100
101
102
# File 'lib/openflow-controller/controller.rb', line 100

def echo_request(datapath_id, msg)
  send_message datapath_id, EchoReply.new(xid: msg.xid)
end

#error(_datapath_id, _msg) ⇒ Object



99
# File 'lib/openflow-controller/controller.rb', line 99

def error(_datapath_id, _msg) end

#eval(input) ⇒ Object



47
48
49
# File 'lib/openflow-controller/controller.rb', line 47

def eval(input)
  binding.eval(input)
end

#flow_removed(_datapath_id, _msg) ⇒ Object



107
# File 'lib/openflow-controller/controller.rb', line 107

def flow_removed(_datapath_id, _msg) end

#last_messageObject



92
93
94
# File 'lib/openflow-controller/controller.rb', line 92

def last_message
  messages.last
end

#last_message_for(datapath_id) ⇒ Object



84
85
86
# File 'lib/openflow-controller/controller.rb', line 84

def last_message_for(datapath_id)
  messages_for(datapath_id).last
end

#message_received(_datapath_id, _msg) ⇒ Object



98
# File 'lib/openflow-controller/controller.rb', line 98

def message_received(_datapath_id, _msg) end

#messagesObject



88
89
90
# File 'lib/openflow-controller/controller.rb', line 88

def messages
  messages_for datapath_ids.first
end

#messages_for(datapath_id) ⇒ Object



80
81
82
# File 'lib/openflow-controller/controller.rb', line 80

def messages_for(datapath_id)
  @messages.fetch(datapath_id.to_s)
end

#packet_in(_datapath_id, _msg) ⇒ Object



103
# File 'lib/openflow-controller/controller.rb', line 103

def packet_in(_datapath_id, _msg) end

#port_add(_datapath_id, _msg) ⇒ Object



104
# File 'lib/openflow-controller/controller.rb', line 104

def port_add(_datapath_id, _msg) end

#port_delete(_datapath_id, _msg) ⇒ Object



105
# File 'lib/openflow-controller/controller.rb', line 105

def port_delete(_datapath_id, _msg) end

#port_modify(_datapath_id, _msg) ⇒ Object



106
# File 'lib/openflow-controller/controller.rb', line 106

def port_modify(_datapath_id, _msg) end

#run(ip = DEFAULT_IP_ADDRESS, port = DEFAULT_TCP_PORT, *args) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/openflow-controller/controller.rb', line 51

def run(ip = DEFAULT_IP_ADDRESS, port = DEFAULT_TCP_PORT, *args)
  maybe_send_handler :start, *args
  socket = TCPServer.open(ip, port)
  socket.setsockopt(:SOCKET, :REUSEADDR, true)
  logger.info "Controller running on #{ip}:#{port}."
  start_timers
  loop { start_switch_thread socket.accept }
end

#send_message(datapath_id, msg = nil) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/openflow-controller/controller.rb', line 68

def send_message(datapath_id, msg = nil)
  if msg.nil?
    msg         = datapath_id
    datapath_id = datapath_ids.first
  end
  @switches.fetch(datapath_id.to_s).send(msg)
end

#start(*_args) ⇒ Object



96
# File 'lib/openflow-controller/controller.rb', line 96

def start(*_args) end

#switch_ready(_datapath_id) ⇒ Object



97
# File 'lib/openflow-controller/controller.rb', line 97

def switch_ready(_datapath_id) end

#switchesObject



64
65
66
# File 'lib/openflow-controller/controller.rb', line 64

def switches
  @switches.values
end