Clean imports in code

In some part in the code we import objects.
In the Openstack style guidelines they recommend to import only modules.

http://docs.openstack.org/developer/hacking/#imports

Change-Id: I25bb6d0986a7d598d8c694e9b4dbd638a26abcbc
This commit is contained in:
Nguyen Hung Phuong 2016-08-18 10:55:50 +07:00
parent 25fe9211b4
commit ee8ed9d984
6 changed files with 98 additions and 94 deletions

View File

@ -1,74 +1,74 @@
# Copyright (c) 2015 OpenStack Foundation.
# All Rights Reserved.
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
from oslo_log import log
from dragonflow._i18n import _LI
from dragonflow.controller.common import constants as const
from dragonflow.controller.common import utils as cookie
from dragonflow.controller.df_base_app import DFlowApp
from dragonflow.controller.ofswitch import OpenFlowSwitchMixin
LOG = log.getLogger("dragonflow.controller.aging")
class Aging(DFlowApp, OpenFlowSwitchMixin):
def __init__(self, *args, **kwargs):
DFlowApp.__init__(self, *args, **kwargs)
OpenFlowSwitchMixin.__init__(self, args[0])
self.aging_mask = const.GLOBAL_AGING_COOKIE_MASK
self.do_aging = True
"""
check_aging_needed()
-> get_canary_flow()
-> canary flow exist, get canary cookie
-> no canary flow, i.e., first boot or ovs restart, no need to do
aging
-> renew_aging_cookie()
-> add canary flows with new cookie
after all apps flushed flows with new cookie, ovs_sync_finished() will
be called
"""
def ovs_sync_started(self):
LOG.info(_LI("start aging"))
canary_flow = self.get_canary_flow()
if canary_flow is None:
self.do_aging = False
cookie.set_aging_cookie(const.GLOBAL_INIT_AGING_COOKIE)
LOG.info(_LI("no canary table, don't do aging"))
else:
self._renew_aging_cookie(canary_flow.cookie)
self.add_canary_flow(cookie.get_aging_cookie())
"""
now all apps had flushed flows with new cookie
delete flows with old cookie
"""
def ovs_sync_finished(self):
if self.do_aging is True:
self._start_aging()
LOG.info(_LI("do aging"))
def _start_aging(self):
old_cookie = cookie.get_xor_cookie(cookie.get_aging_cookie())
self.cleanup_flows(old_cookie, self.aging_mask)
def _renew_aging_cookie(self, cur_c):
LOG.info(_LI("renew cookie, current cookie is %x"), cur_c)
new_c = cookie.get_xor_cookie(cur_c)
cookie.set_aging_cookie(new_c & self.aging_mask)
return cookie.get_aging_cookie()
# Copyright (c) 2015 OpenStack Foundation.
# All Rights Reserved.
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
from oslo_log import log
from dragonflow._i18n import _LI
from dragonflow.controller.common import constants as const
from dragonflow.controller.common import utils as cookie
from dragonflow.controller import df_base_app
from dragonflow.controller import ofswitch
LOG = log.getLogger("dragonflow.controller.aging")
class Aging(df_base_app.DFlowApp, ofswitch.OpenFlowSwitchMixin):
def __init__(self, *args, **kwargs):
df_base_app.DFlowApp.__init__(self, *args, **kwargs)
ofswitch.OpenFlowSwitchMixin.__init__(self, args[0])
self.aging_mask = const.GLOBAL_AGING_COOKIE_MASK
self.do_aging = True
"""
check_aging_needed()
-> get_canary_flow()
-> canary flow exist, get canary cookie
-> no canary flow, i.e., first boot or ovs restart, no need to do
aging
-> renew_aging_cookie()
-> add canary flows with new cookie
after all apps flushed flows with new cookie, ovs_sync_finished() will
be called
"""
def ovs_sync_started(self):
LOG.info(_LI("start aging"))
canary_flow = self.get_canary_flow()
if canary_flow is None:
self.do_aging = False
cookie.set_aging_cookie(const.GLOBAL_INIT_AGING_COOKIE)
LOG.info(_LI("no canary table, don't do aging"))
else:
self._renew_aging_cookie(canary_flow.cookie)
self.add_canary_flow(cookie.get_aging_cookie())
"""
now all apps had flushed flows with new cookie
delete flows with old cookie
"""
def ovs_sync_finished(self):
if self.do_aging is True:
self._start_aging()
LOG.info(_LI("do aging"))
def _start_aging(self):
old_cookie = cookie.get_xor_cookie(cookie.get_aging_cookie())
self.cleanup_flows(old_cookie, self.aging_mask)
def _renew_aging_cookie(self, cur_c):
LOG.info(_LI("renew cookie, current cookie is %x"), cur_c)
new_c = cookie.get_xor_cookie(cur_c)
cookie.set_aging_cookie(new_c & self.aging_mask)
return cookie.get_aging_cookie()

View File

@ -20,12 +20,12 @@ from ryu.lib.packet import packet
from ryu.ofproto import ether
from dragonflow.controller.common.utils import set_aging_cookie_bits
from dragonflow.controller.df_db_notifier import DBNotifyInterface
from dragonflow.controller import df_db_notifier
LOG = logging.getLogger(__name__)
class DFlowApp(DBNotifyInterface):
class DFlowApp(df_db_notifier.DBNotifyInterface):
def __init__(self, api, db_store=None, vswitch_api=None, nb_api=None):
self.api = api
self.db_store = db_store

View File

@ -19,9 +19,9 @@ from oslo_config import cfg
from ryu.lib.mac import haddr_to_bin
from dragonflow._i18n import _
from dragonflow.controller.common.arp_responder import ArpResponder
from dragonflow.controller.common import arp_responder
from dragonflow.controller.common import constants as const
from dragonflow.controller.df_base_app import DFlowApp
from dragonflow.controller import df_base_app
DF_L2_APP_OPTS = [
cfg.BoolOpt(
@ -38,7 +38,7 @@ DF_L2_APP_OPTS = [
OF_IN_PORT = 0xfff8
class L2App(DFlowApp):
class L2App(df_base_app.DFlowApp):
def __init__(self, *args, **kwargs):
super(L2App, self).__init__(*args, **kwargs)
@ -93,7 +93,8 @@ class L2App(DFlowApp):
return
network_id = lport.get_external_value('local_network_id')
mac = lport.get_mac()
ArpResponder(self.get_datapath(), network_id, ip, mac).add()
arp_responder.ArpResponder(self.get_datapath(),
network_id, ip, mac).add()
def _remove_arp_responder(self, lport):
if not self.is_install_arp_responder:
@ -104,7 +105,8 @@ class L2App(DFlowApp):
if netaddr.IPAddress(ip).version != 4:
return
network_id = lport.get_external_value('local_network_id')
ArpResponder(self.get_datapath(), network_id, ip).remove()
arp_responder.ArpResponder(self.get_datapath(),
network_id, ip).remove()
def _add_dst_classifier_flow_for_port(self, network_id, mac, tunnel_key):
parser = self.get_datapath().ofproto_parser

View File

@ -20,9 +20,9 @@ from oslo_log import log
from ryu.lib.mac import haddr_to_bin
from dragonflow._i18n import _, _LI
from dragonflow.controller.common.arp_responder import ArpResponder
from dragonflow.controller.common import arp_responder
from dragonflow.controller.common import constants as const
from dragonflow.controller.df_base_app import DFlowApp
from dragonflow.controller import df_base_app
DF_L2_APP_OPTS = [
cfg.BoolOpt(
@ -41,7 +41,7 @@ OF_IN_PORT = 0xfff8
LOG = log.getLogger(__name__)
class L2App(DFlowApp):
class L2App(df_base_app.DFlowApp):
def __init__(self, *args, **kwargs):
super(L2App, self).__init__(*args, **kwargs)
self.local_networks = {}
@ -93,7 +93,8 @@ class L2App(DFlowApp):
return
network_id = lport.get_external_value('local_network_id')
mac = lport.get_mac()
ArpResponder(self.get_datapath(), network_id, ip, mac).add()
arp_responder.ArpResponder(self.get_datapath(),
network_id, ip, mac).add()
def _remove_arp_responder(self, lport):
if not self.is_install_arp_responder:
@ -102,7 +103,8 @@ class L2App(DFlowApp):
if netaddr.IPAddress(ip).version != 4:
return
network_id = lport.get_external_value('local_network_id')
ArpResponder(self.get_datapath(), network_id, ip).remove()
arp_responder.ArpResponder(self.get_datapath(),
network_id, ip).remove()
def remove_local_port(self, lport):

View File

@ -19,7 +19,7 @@ from ryu.ofproto import ether
from dragonflow._i18n import _LI, _LE
from dragonflow.controller.common import constants as const
from dragonflow.controller.df_base_app import DFlowApp
from dragonflow.controller import df_base_app
config.setup_logging()
@ -31,7 +31,7 @@ COOKIE_FULLMASK = 0xffffffffffffffff
SG_PRIORITY_OFFSET = 2
class SGApp(DFlowApp):
class SGApp(df_base_app.DFlowApp):
def __init__(self, *args, **kwargs):
super(SGApp, self).__init__(*args, **kwargs)

View File

@ -13,7 +13,7 @@
from oslo_log import log
from dragonflow._i18n import _LI, _LE, _LW
from dragonflow.db.api_nb import OvsPort
from dragonflow.db import api_nb
LOG = log.getLogger(__name__)
@ -21,10 +21,10 @@ LOG = log.getLogger(__name__)
class Topology(object):
def __init__(self, controller, enable_selective_topology_distribution):
self.ovs_port_type = (OvsPort.TYPE_VM,
OvsPort.TYPE_TUNNEL,
OvsPort.TYPE_PATCH,
OvsPort.TYPE_BRIDGE)
self.ovs_port_type = (api_nb.OvsPort.TYPE_VM,
api_nb.OvsPort.TYPE_TUNNEL,
api_nb.OvsPort.TYPE_PATCH,
api_nb.OvsPort.TYPE_BRIDGE)
# Stores topics(tenants) subscribed by lports in the current local
# controller. I,e, {tenant1:{lport1, lport2}, tenant2:{lport3}}
@ -124,7 +124,7 @@ class Topology(object):
if (ofport is None) or (ofport < 0) or (port_type is None):
return False
if (port_type == OvsPort.TYPE_VM) \
if (port_type == api_nb.OvsPort.TYPE_VM) \
and (ovs_port.get_iface_id() is None):
return False