2012-02-03 12:21:14 +09:00
|
|
|
# Copyright (C) 2011, 2012 Nippon Telegraph and Telephone Corporation.
|
2012-01-31 16:45:05 +09:00
|
|
|
# Copyright (C) 2011, 2012 Isaku Yamahata <yamahata at valinux co jp>
|
2011-12-09 15:56:05 +09:00
|
|
|
#
|
2012-04-06 08:38:45 +09:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
2011-12-09 15:56:05 +09:00
|
|
|
#
|
2012-04-06 08:38:45 +09:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2011-12-09 15:56:05 +09:00
|
|
|
#
|
2012-04-06 08:38:45 +09:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
|
|
# implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2011-12-09 15:56:05 +09:00
|
|
|
|
|
|
|
import copy
|
|
|
|
import inspect
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from ryu.controller import dispatcher
|
2012-02-02 14:48:35 +09:00
|
|
|
from ryu.controller import ofp_event
|
2011-12-09 15:56:05 +09:00
|
|
|
|
|
|
|
LOG = logging.getLogger('ryu.controller.handler')
|
|
|
|
|
2012-01-31 16:45:01 +09:00
|
|
|
QUEUE_NAME_OFP_MSG = 'ofp_msg'
|
2012-01-31 16:45:05 +09:00
|
|
|
DISPATCHER_NAME_OFP_HANDSHAKE = 'ofp_handshake'
|
2012-02-02 14:48:34 +09:00
|
|
|
HANDSHAKE_DISPATCHER = dispatcher.EventDispatcher(
|
2012-01-31 16:45:05 +09:00
|
|
|
DISPATCHER_NAME_OFP_HANDSHAKE)
|
|
|
|
DISPATCHER_NAME_OFP_CONFIG = 'ofp_config'
|
2012-02-02 14:48:34 +09:00
|
|
|
CONFIG_DISPATCHER = dispatcher.EventDispatcher(DISPATCHER_NAME_OFP_CONFIG)
|
2012-01-31 16:45:05 +09:00
|
|
|
DISPATCHER_NAME_OFP_MAIN = 'ofp_main'
|
2012-02-02 14:48:34 +09:00
|
|
|
MAIN_DISPATCHER = dispatcher.EventDispatcher(DISPATCHER_NAME_OFP_MAIN)
|
2012-01-31 16:45:06 +09:00
|
|
|
DISPATCHER_NAME_OFP_DEAD = 'ofp_dead'
|
|
|
|
DEAD_DISPATCHER = dispatcher.EventDispatcher(DISPATCHER_NAME_OFP_DEAD)
|
2011-12-09 15:56:05 +09:00
|
|
|
|
|
|
|
|
2012-09-04 12:19:36 +09:00
|
|
|
def set_ev_cls(ev_cls, dispatchers):
|
2011-12-09 15:56:05 +09:00
|
|
|
def _set_ev_cls_dec(handler):
|
|
|
|
handler.ev_cls = ev_cls
|
2012-09-04 12:19:36 +09:00
|
|
|
handler.dispatchers = dispatchers
|
2011-12-09 15:56:05 +09:00
|
|
|
return handler
|
|
|
|
return _set_ev_cls_dec
|
|
|
|
|
|
|
|
|
|
|
|
def _is_ev_handler(meth):
|
2012-12-17 18:07:04 +09:00
|
|
|
return hasattr(meth, 'ev_cls')
|
2011-12-09 15:56:05 +09:00
|
|
|
|
|
|
|
|
|
|
|
def _listify(may_list):
|
|
|
|
if may_list is None:
|
|
|
|
may_list = []
|
|
|
|
if not isinstance(may_list, list):
|
|
|
|
may_list = [may_list]
|
|
|
|
return may_list
|
|
|
|
|
|
|
|
|
2012-09-04 12:19:36 +09:00
|
|
|
def register_instance(i):
|
2012-02-02 14:48:34 +09:00
|
|
|
for _k, m in inspect.getmembers(i, inspect.ismethod):
|
|
|
|
# LOG.debug('instance %s k %s m %s', i, _k, m)
|
2011-12-09 15:56:05 +09:00
|
|
|
if not _is_ev_handler(m):
|
|
|
|
continue
|
|
|
|
|
2012-09-04 12:19:36 +09:00
|
|
|
_dispatchers = _listify(getattr(m, 'dispatchers', None))
|
2011-12-09 15:56:05 +09:00
|
|
|
# LOG.debug("_dispatchers %s", _dispatchers)
|
|
|
|
for d in _dispatchers:
|
|
|
|
# LOG.debug('register dispatcher %s ev %s k %s m %s',
|
2012-02-02 14:48:34 +09:00
|
|
|
# d.name, m.ev_cls, _k, m)
|
2011-12-09 15:56:05 +09:00
|
|
|
d.register_handler(m.ev_cls, m)
|