Update flake8 rules for nfv

This commit updates the flake8 rules for the nfv folder, deleting the
copyright notices for empty __init__.py files (H104), updating multiline
docstring comments (H404 and H405) and reducing lines too long (E501).
Ruff was used to help format comments and remove blank lines between
conditionals and functions, i.e. D201, D202, D205, E302, E304.

Test plan:
1. PASS: Build an ISO.
2. PASS: Deploy a DC system with AIO-SX and AIO-DX subclouds.
3. PASS: Successfully run sw-upgrade for the system controller and
   subclouds.
4. PASS: Successfully run kube-upgrade for the subclouds
5. PASS: Successfully run kube-rootca update for the subclouds.

Story: 2011777
Task: 54256

Change-Id: I4851e425eb6103cf7149ecf5957be635c70adf36
Signed-off-by: Raphael <Raphael.Lima@windriver.com>
This commit is contained in:
Raphael committed 2026-05-05 15:04:04 -03:00
1 parent 9f340ba938
commit 44f728b64a
435 files changed
+9869 -14428

No files matched your search

-1
View File
@@ -69,7 +69,6 @@
name: nfv-tox-black
parent: nfv-tox-base
description: Run black for nfv
voting: false
vars:
python_version: 3.9
tox_envlist: black
+12 -13
View File
@@ -16,11 +16,11 @@ import os
import sys
extensions = [
'openstackdocstheme',
'os_api_ref',
"openstackdocstheme",
"os_api_ref",
]
html_theme = 'starlingxdocs'
html_theme = "starlingxdocs"
html_theme_options = {
"sidebar_dropdown": "api_ref",
@@ -30,21 +30,21 @@ html_theme_options = {
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
sys.path.insert(0, os.path.abspath('../../'))
sys.path.insert(0, os.path.abspath('../'))
sys.path.insert(0, os.path.abspath('./'))
sys.path.insert(0, os.path.abspath("../../"))
sys.path.insert(0, os.path.abspath("../"))
sys.path.insert(0, os.path.abspath("./"))
# The suffix of source filenames.
source_suffix = '.rst'
source_suffix = ".rst"
# The master toctree document.
master_doc = 'index'
master_doc = "index"
# General information about the project.
project = u'StarlingX NFV'
project = "StarlingX NFV"
# openstackdocstheme options
openstackdocs_repo_name = 'starlingx/nfv'
openstackdocs_repo_name = "starlingx/nfv"
openstackdocs_use_storyboard = True
openstackdocs_auto_name = False
@@ -57,12 +57,11 @@ add_module_names = False
show_authors = False
# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'native'
pygments_style = "native"
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass
# [howto/manual]).
latex_documents = [
('index', 'stx-nfv.tex', u'stx-nfv API Documentation',
u'StarlingX', 'manual'),
("index", "stx-nfv.tex", "stx-nfv API Documentation", "StarlingX", "manual"),
]
+26 -24
View File
@@ -23,9 +23,9 @@
# -- Project information -----------------------------------------------------
project = u'StarlingX NFV'
copyright = u'2018, StarlingX'
author = u'StarlingX'
project = "StarlingX NFV"
copyright = "2018, StarlingX"
author = "StarlingX"
# -- General configuration ---------------------------------------------------
@@ -37,23 +37,23 @@ author = u'StarlingX'
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'openstackdocstheme',
"openstackdocstheme",
]
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
templates_path = ["_templates"]
# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'
source_suffix = ".rst"
# The master toctree document.
master_doc = 'index'
master_doc = "index"
# openstackdocstheme options
openstackdocs_repo_name = 'starlingx/nfv'
openstackdocs_repo_name = "starlingx/nfv"
openstackdocs_use_storyboard = True
openstackdocs_auto_name = False
@@ -62,7 +62,7 @@ openstackdocs_auto_name = False
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = 'en'
language = "en"
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
@@ -70,7 +70,7 @@ language = 'en'
exclude_patterns = []
# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'native'
pygments_style = "native"
# -- Options for HTML output -------------------------------------------------
@@ -78,7 +78,7 @@ pygments_style = 'native'
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'starlingxdocs'
html_theme = "starlingxdocs"
# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
@@ -105,7 +105,7 @@ html_theme = 'starlingxdocs'
# -- Options for HTMLHelp output ---------------------------------------------
# Output file base name for HTML help builder.
htmlhelp_basename = 'stx-nfvdoc'
htmlhelp_basename = "stx-nfvdoc"
# -- Options for LaTeX output ------------------------------------------------
@@ -114,15 +114,15 @@ latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#
# 'papersize': 'letterpaper',
#
# The font size ('10pt', '11pt' or '12pt').
#
# 'pointsize': '10pt',
#
# Additional stuff for the LaTeX preamble.
#
# 'preamble': '',
#
# Latex figure (float) alignment
#
# 'figure_align': 'htbp',
@@ -132,8 +132,7 @@ latex_elements = {
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(master_doc, 'stx-nfv.tex', u'stx-nfv Documentation',
u'StarlingX', 'manual'),
(master_doc, "stx-nfv.tex", "stx-nfv Documentation", "StarlingX", "manual"),
]
@@ -141,10 +140,7 @@ latex_documents = [
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, 'stx-nfv', u'stx-nfv Documentation',
[author], 1)
]
man_pages = [(master_doc, "stx-nfv", "stx-nfv Documentation", [author], 1)]
# -- Options for Texinfo output ----------------------------------------------
@@ -153,7 +149,13 @@ man_pages = [
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'stx-nfv', u'stx-nfv Documentation',
author, 'stx-nfv', 'StarlingX NFVI Orchestration.',
'Miscellaneous'),
(
master_doc,
"stx-nfv",
"stx-nfv Documentation",
author,
"stx-nfv",
"StarlingX NFVI Orchestration.",
"Miscellaneous",
),
]
-4
View File
@@ -1,4 +0,0 @@
# Copyright (c) 2016 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
@@ -1,4 +0,0 @@
# Copyright (c) 2016 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
+9 -15
View File
@@ -9,9 +9,7 @@ import re
class Token(object):
"""
Token
"""
"""Token."""
def __init__(self, token_data, token_id):
self._expired = False
@@ -32,22 +30,19 @@ class Token(object):
return True
def get_id(self):
"""
Get the identifier of the token.
"""
"""Get the identifier of the token."""
return self._token_id
def get_tenant_id(self):
"""
Get the tenant identifier of the token.
"""
"""Get the tenant identifier of the token."""
return self._data["token"]["project"]["id"]
@staticmethod
def _url_strip_version(url):
"""
Strip the version information from the url
"""
"""Strip the version information from the url."""
# Get rid of the trailing '/' if present and remove the version
# information from the URL.
url = url.rstrip("/")
@@ -64,9 +59,8 @@ class Token(object):
return url
def get_service_url(self, region_name, service_name, service_type, interface):
"""
Search the catalog of a service in a region for the url
"""
"""Search the catalog of a service in a region for the url."""
for catalog in self._data["token"]["catalog"]:
if catalog["type"] == service_type:
if catalog["name"] == service_name:
@@ -15,17 +15,13 @@ from nfv_client.openstack.objects import Token
class OpenStackServices(object):
"""
OpenStack Services Constants
"""
"""OpenStack Services Constants."""
VIM = "vim"
class OpenStackServiceTypes(object):
"""
OpenStack Service Types Constants
"""
"""OpenStack Service Types Constants."""
NFV = "nfv"
@@ -38,9 +34,7 @@ CAFILE = os.environ.get("REQUESTS_CA_BUNDLE")
def get_token(
auth_uri, project_name, project_domain_name, username, password, user_domain_name
):
"""
Ask OpenStack for a token
"""
"""Ask OpenStack for a token."""
ssl_context = None
try:
@@ -10,7 +10,6 @@ import requests
from nfv_client.auth_types import AUTH_TYPES
from nfv_client import sw_update
CAFILE = os.environ.get("REQUESTS_CA_BUNDLE")
STRATEGY_EXISTS = "strategy already exists."
UNKNOWN_FAILURE = "Check /var/log/nfv-vim-api.log for details."
@@ -34,8 +33,8 @@ def request(
timeout_in_secs=40,
auth_type="keystone",
):
"""
Make a rest-api request
"""Make a rest-api request.
Note: Using a default timeout of 40 seconds. The VIM's internal handling
of these requests times out after 30 seconds - we want that to happen
first (if possible).
@@ -23,7 +23,7 @@ class StrategyStep(object):
reason = None
def __repr__(self):
return "%s" % str(self.__dict__)
return "%s" % str(self.__dict__) # noqa: H501
class StrategyStage(object):
@@ -40,7 +40,7 @@ class StrategyStage(object):
reason = None
def __repr__(self):
return "%s" % str(self.__dict__)
return "%s" % str(self.__dict__) # noqa: H501
class StrategyPhase(object):
@@ -59,7 +59,7 @@ class StrategyPhase(object):
response = None
def __repr__(self):
return "%s" % str(self.__dict__)
return "%s" % str(self.__dict__) # noqa: H501
class Strategy(object):
@@ -81,13 +81,12 @@ class Strategy(object):
abort_phase = None
def __repr__(self):
return "%s" % str(self.__dict__)
return "%s" % str(self.__dict__) # noqa: H501
def _get_strategy_step_object_from_response(response):
"""
Convert the Rest-API response into a strategy step object
"""
"""Convert the Rest-API response into a strategy step object."""
step = StrategyStep()
step.step_id = response["step-id"]
step.step_name = response["step-name"]
@@ -103,9 +102,8 @@ def _get_strategy_step_object_from_response(response):
def _get_strategy_stage_object_from_response(response):
"""
Convert the Rest-API response into a strategy stage object
"""
"""Convert the Rest-API response into a strategy stage object."""
stage = StrategyStage()
stage.stage_id = response["stage-id"]
stage.stage_name = response["stage-name"]
@@ -126,9 +124,8 @@ def _get_strategy_stage_object_from_response(response):
def _get_strategy_phase_object_from_response(response):
"""
Convert the Rest-API response into a strategy phase object
"""
"""Convert the Rest-API response into a strategy phase object."""
phase = StrategyPhase()
phase.phase_name = response["phase-name"]
phase.total_stages = response["total-stages"]
@@ -151,9 +148,8 @@ def _get_strategy_phase_object_from_response(response):
def _get_strategy_object_from_response(response):
"""
Convert the Rest-API response into a strategy object
"""
"""Convert the Rest-API response into a strategy object."""
strategy_data = response.get("strategy", None)
if strategy_data is None:
return None
@@ -189,9 +185,8 @@ def _get_strategy_object_from_response(response):
def _get_current_strategy_from_response(response):
"""
Returns Strategy Type and State
"""
"""Returns Strategy Type and State."""
current_strategy = {}
strategy_data = response.get("strategy", None)
if strategy_data is None:
@@ -213,9 +208,8 @@ def get_strategies(
tenant=None,
auth_type=AUTH_TYPES.KEYSTONE,
):
"""
Software Update - Get Strategies
"""
"""Software Update - Get Strategies."""
api_cmd = url + "/api/orchestration/%s/strategy" % strategy_name
api_cmd_headers = dict()
@@ -245,9 +239,8 @@ def get_strategy(
tenant=None,
auth_type=AUTH_TYPES.KEYSTONE,
):
"""
Software Update - Get Strategy
"""
"""Software Update - Get Strategy."""
api_cmd = url + "/api/orchestration/%s/strategy/%s" % (strategy_name, strategy_uuid)
api_cmd_headers = dict()
@@ -282,11 +275,10 @@ def create_strategy(
user_domain_name=None,
tenant=None,
auth_type=AUTH_TYPES.KEYSTONE,
**kwargs
**kwargs,
):
"""
Software Update - Create Strategy
"""
"""Software Update - Create Strategy."""
api_cmd = url + "/api/orchestration/%s/strategy" % strategy_name
api_cmd_headers = dict()
@@ -359,9 +351,8 @@ def delete_strategy(
tenant=None,
auth_type=AUTH_TYPES.KEYSTONE,
):
"""
Software Update - Delete Strategy
"""
"""Software Update - Delete Strategy."""
api_cmd = url + "/api/orchestration/%s/strategy" % strategy_name
api_cmd_headers = dict()
@@ -403,9 +394,8 @@ def apply_strategy(
tenant=None,
auth_type=AUTH_TYPES.KEYSTONE,
):
"""
Software Update - Apply Strategy
"""
"""Software Update - Apply Strategy."""
api_cmd = url + ("/api/orchestration/%s/strategy/actions" % strategy_name)
api_cmd_headers = dict()
@@ -448,9 +438,8 @@ def abort_strategy(
tenant=None,
auth_type=AUTH_TYPES.KEYSTONE,
):
"""
Software Update - Abort Strategy
"""
"""Software Update - Abort Strategy."""
api_cmd = url + ("/api/orchestration/%s/strategy/actions" % strategy_name)
api_cmd_headers = dict()
@@ -488,9 +477,8 @@ def get_current_strategy(
tenant=None,
auth_type=AUTH_TYPES.KEYSTONE,
):
"""
Get The Current Active Strategy Type And State
"""
"""Get The Current Active Strategy Type And State."""
api_cmd = url + "/api/orchestration/current-strategy/strategy"
api_cmd_headers = dict()
+16 -27
View File
@@ -12,7 +12,6 @@ import urllib.request
from nfv_client import sw_update
REGISTERED_STRATEGIES = {}
@@ -22,8 +21,7 @@ def _process_exit(error):
def register_strategy(cmd_area, strategy_name):
"""
Registers a parser command with an update strategy name
"""Registers a parser command with an update strategy name.
:param cmd_area: the parser command to register
:param strategy_name: the strategy to associate with this parser
@@ -32,8 +30,7 @@ def register_strategy(cmd_area, strategy_name):
def get_strategy_name(cmd_area):
"""
Determines the strategy name for a parser command
"""Determines the strategy name for a parser command.
:param cmd_area: the parser command to lookup
:returns: the strategy name associated with the parser
@@ -46,8 +43,7 @@ def get_strategy_name(cmd_area):
def get_extra_create_args(cmd_area, args):
"""
Return the extra create arguments supported by a strategy type
"""Return the extra create arguments supported by a strategy type.
:param cmd_area: the strategy that supports additional create arguments
:param args: the parsed arguments to extract the additional fields from
@@ -88,8 +84,7 @@ def get_extra_create_args(cmd_area, args):
def add_list_arg(some_cmd, some_arg, some_list):
"""
Adds an argument to a command accepting a list of valid values.
"""Adds an argument to a command accepting a list of valid values.
:param some_cmd: a command parser object that is adding a new argument
:param some_arg: a string indicating the new argument. ex: --foo
@@ -104,8 +99,7 @@ def add_list_arg(some_cmd, some_arg, some_list):
def setup_abort_cmd(parser):
"""
Sets up an 'abort' command for a strategy command parser.
"""Sets up an 'abort' command for a strategy command parser.
ex: sw-manager sw-deploy-strategy abort <some args>
@@ -121,8 +115,7 @@ def setup_abort_cmd(parser):
def setup_apply_cmd(parser):
"""
Sets up an 'apply' command for a strategy command parser.
"""Sets up an 'apply' command for a strategy command parser.
ex: sw-manager sw-deploy-strategy apply <some args>
@@ -147,8 +140,7 @@ def setup_create_cmd(
min_parallel=1,
max_parallel=5,
):
"""
Sets up a 'create' command for a strategy command parser.
"""Sets up a 'create' command for a strategy command parser.
ex: sw-manager sw-deploy-strategy create <some args>
@@ -182,8 +174,7 @@ def setup_create_cmd(
def setup_delete_cmd(parser):
"""
Sets up a 'delete' command for a strategy command parser.
"""Sets up a 'delete' command for a strategy command parser.
ex: sw-manager sw-deploy-strategy delete <some args>
@@ -196,8 +187,7 @@ def setup_delete_cmd(parser):
def setup_show_cmd(parser):
"""
Sets up a 'show' command for a strategy command parser.
"""Sets up a 'show' command for a strategy command parser.
ex: sw-manager sw-deploy-strategy show <some args>
@@ -220,7 +210,7 @@ def setup_show_cmd(parser):
def setup_fw_update_parser(commands):
"""Firmware Update Strategy Commands"""
"""Firmware Update Strategy Commands."""
cmd_area = sw_update.CMD_NAME_FW_UPDATE
register_strategy(cmd_area, sw_update.STRATEGY_NAME_FW_UPDATE)
@@ -265,7 +255,7 @@ def setup_fw_update_parser(commands):
def setup_kube_rootca_update_parser(commands):
"""Kubernetes RootCA Update Strategy Commands"""
"""Kubernetes RootCA Update Strategy Commands."""
cmd_area = sw_update.CMD_NAME_KUBE_ROOTCA_UPDATE
register_strategy(cmd_area, sw_update.STRATEGY_NAME_KUBE_ROOTCA_UPDATE)
@@ -326,7 +316,7 @@ def setup_kube_rootca_update_parser(commands):
def setup_kube_upgrade_parser(commands):
"""Kubernetes Upgrade Strategy Commands"""
"""Kubernetes Upgrade Strategy Commands."""
cmd_area = sw_update.CMD_NAME_KUBE_UPGRADE
register_strategy(cmd_area, sw_update.STRATEGY_NAME_KUBE_UPGRADE)
@@ -384,7 +374,7 @@ def setup_kube_upgrade_parser(commands):
def setup_system_config_update_parser(commands):
"""System config update Strategy Commands"""
"""System config update Strategy Commands."""
cmd_area = sw_update.CMD_NAME_SYSTEM_CONFIG_UPDATE
register_strategy(cmd_area, sw_update.STRATEGY_NAME_SYSTEM_CONFIG_UPDATE)
@@ -436,7 +426,7 @@ def setup_system_config_update_parser(commands):
def setup_sw_deploy_parser(commands):
"""Software Deploy Strategy Commands"""
"""Software Deploy Strategy Commands."""
cmd_area = sw_update.CMD_NAME_SW_DEPLOY
# TODO(jkraitbe): Backend for sw-deploy will continue as old sw-upgrade for now
@@ -587,9 +577,8 @@ def confirm_if_required(args, command, cli_conf_enabled):
def process_main(argv=sys.argv[1:]): # pylint: disable=dangerous-default-value
"""
Client - Main
"""
"""Client - Main."""
try:
parser = argparse.ArgumentParser()
parser.add_argument("--debug", action="store_true")
@@ -66,9 +66,8 @@ def _print(indent_by, field, value, remains=""):
def _display_strategy_step(strategy_step, active=False):
"""
Software Update - Display Strategy Step Information
"""
"""Software Update - Display Strategy Step Information."""
# If active flag is passed
# skip steps that are not started:'initial' or completed cleanly: 'success'
# this leaves failed and in-progress states
@@ -94,9 +93,7 @@ def _display_strategy_step(strategy_step, active=False):
def _get_current_stage_and_step(strategy):
"""
Get the current stage and step
"""
"""Get the current stage and step."""
current_phase = None
current_stage, current_step = None, None
@@ -125,9 +122,8 @@ def _get_current_stage_and_step(strategy):
def _display_strategy_stage(strategy_stage, details=False, active=False):
"""
Software Update - Display Strategy Stage Information
"""
"""Software Update - Display Strategy Stage Information."""
# If active flag is passed, only display a stage that is in progress
if active:
if not strategy_stage.inprogress:
@@ -154,9 +150,8 @@ def _display_strategy_stage(strategy_stage, details=False, active=False):
def _display_strategy_phase(strategy_phase, details=False, active=False):
"""
Software Update - Display Strategy Phase Information
"""
"""Software Update - Display Strategy Phase Information."""
# If active flag is passed, only display a phase that is in progress
if active:
if not strategy_phase.inprogress:
@@ -183,9 +178,8 @@ def _display_strategy_phase(strategy_phase, details=False, active=False):
def _display_strategy(strategy, details=False, active=False, error_details=False):
"""
Software Update - Display Strategy Information
"""
"""Software Update - Display Strategy Information."""
# TODO(jkraitbe): Backend for sw-deploy will continue as old sw-upgrade for now
if strategy.name == STRATEGY_NAME_SW_UPGRADE:
print("Strategy Software Deploy Strategy:")
@@ -280,7 +274,7 @@ def _get_auth_token_and_url(
os_interface,
stx_auth_type,
):
"""Get authentication context for OIDC or Keystone"""
"""Get authentication context for OIDC or Keystone."""
if stx_auth_type == AUTH_TYPES.OIDC:
# OIDC authentication path
@@ -354,14 +348,13 @@ def create_strategy(
stx_auth_type,
**kwargs,
):
"""
Software Update - Create Strategy
"""
"""Software Update - Create Strategy."""
if alarm_restrictions == ALARM_RESTRICTIONS_PERMISSIVE:
response = input(
"Using --alarm-restrictions permissive is unsafe.\n"
"Ensure you have contacted support and understand the possible complications.\n"
"Ensure you have contacted support and understand the possible "
"complications.\n"
"Enter 'yes' to continue or anything else to abort: "
)
if response != "yes":
@@ -415,9 +408,8 @@ def delete_strategy(
stx_auth_type,
force=False,
):
"""
Software Update - Delete Strategy
"""
"""Software Update - Delete Strategy."""
token_id, url = _get_auth_token_and_url(
os_auth_uri,
os_project_name,
@@ -460,9 +452,8 @@ def apply_strategy(
stx_auth_type,
stage_id=None,
):
"""
Software Update - Apply Strategy
"""
"""Software Update - Apply Strategy."""
token_id, url = _get_auth_token_and_url(
os_auth_uri,
os_project_name,
@@ -528,9 +519,8 @@ def abort_strategy(
stx_auth_type,
stage_id=None,
):
"""
Software Update - Abort Strategy
"""
"""Software Update - Abort Strategy."""
token_id, url = _get_auth_token_and_url(
os_auth_uri,
os_project_name,
@@ -577,9 +567,8 @@ def show_strategy(
active=False,
error_details=False,
):
"""
Software Update - Show Strategy
"""
"""Software Update - Show Strategy."""
token_id, url = _get_auth_token_and_url(
os_auth_uri,
os_project_name,
-5
View File
@@ -1,5 +0,0 @@
#
# Copyright (c) 2015-2016 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
@@ -12,9 +12,7 @@ DLOG = debug.debug_get_logger("nfv_common.alarm.alarm_handlers")
class AlarmHandlers(stevedore.enabled.EnabledExtensionManager, metaclass=Singleton):
"""
Alarm Handlers
"""
"""Alarm Handlers."""
_version = "1.0.0"
_signature = "e33d7cf6-f270-4256-893e-16266ee4dd2e"
@@ -43,9 +41,8 @@ class AlarmHandlers(stevedore.enabled.EnabledExtensionManager, metaclass=Singlet
@staticmethod
def valid_handler(handler):
"""
Verify signature of the handler is valid
"""
"""Verify signature of the handler is valid."""
if AlarmHandlers._signature == handler.obj.signature:
return True
else:
@@ -57,36 +54,31 @@ class AlarmHandlers(stevedore.enabled.EnabledExtensionManager, metaclass=Singlet
return False
def raise_alarm(self, alarm_uuid, alarm_data):
"""
Raise an alarm using the handlers
"""
"""Raise an alarm using the handlers."""
for handler_type, handler in list(self._handlers.items()):
handler.obj.raise_alarm(alarm_uuid, alarm_data)
def clear_alarm(self, alarm_uuid):
"""
Clear an alarm using the handlers
"""
"""Clear an alarm using the handlers."""
for handler_type, handler in list(self._handlers.items()):
handler.obj.clear_alarm(alarm_uuid)
def audit_alarms(self):
"""
Audit alarms using the handlers
"""
"""Audit alarms using the handlers."""
for handler_type, handler in list(self._handlers.items()):
handler.obj.audit_alarms()
def initialize(self, config_file):
"""
Initialize handlers
"""
"""Initialize handlers."""
for handler_id, handler in list(self._handlers.items()):
handler.obj.initialize(config_file)
def finalize(self):
"""
Finalize handlers
"""
"""Finalize handlers."""
for handler_id, handler in list(self._handlers.items()):
handler.obj.finalize()
@@ -9,45 +9,40 @@ DLOG = debug.debug_get_logger("nfv_common.alarm.alarm_module")
def alarm_raise(alarm_uuid, alarm_data):
"""
Raise an alarm
"""
"""Raise an alarm."""
from nfv_common.alarm._alarm_thread import AlarmThread
AlarmThread().alarm_raise(alarm_uuid, alarm_data)
def alarm_clear(alarm_uuid):
"""
Clear an alarm
"""
"""Clear an alarm."""
from nfv_common.alarm._alarm_thread import AlarmThread
AlarmThread().alarm_clear(alarm_uuid)
def alarm_subsystem_sane():
"""
Returns true if the alarm subsystem is healthy
"""
"""Returns true if the alarm subsystem is healthy."""
from nfv_common.alarm._alarm_thread import AlarmThread
return 600 >= AlarmThread().stall_elapsed_secs
def alarm_initialize(config):
"""
Initialize the alarm subsystem
"""
"""Initialize the alarm subsystem."""
from nfv_common.alarm._alarm_thread import AlarmThread
AlarmThread(config).start()
def alarm_finalize():
"""
Finalize the alarm subsystem
"""
"""Finalize the alarm subsystem."""
from nfv_common.alarm._alarm_thread import AlarmThread
AlarmThread().stop(max_wait_in_seconds=5)
@@ -18,9 +18,7 @@ DLOG = debug.debug_get_logger("nfv_common.alarm.alarm_thread")
class AlarmWorker(thread.ThreadWorker, metaclass=Singleton):
"""
Alarm Worker
"""
"""Alarm Worker."""
def __init__(self, name, config):
super(AlarmWorker, self).__init__(name)
@@ -30,17 +28,15 @@ class AlarmWorker(thread.ThreadWorker, metaclass=Singleton):
@coroutine
def _alarm_audit(self):
"""
Called periodically to audit alarms
"""
"""Called periodically to audit alarms."""
while True:
(yield)
self._handlers.audit_alarms()
def initialize(self):
"""
Initialize the Alarm Worker
"""
"""Initialize the Alarm Worker."""
self._handlers = AlarmHandlers(
self._config["namespace"], self._config["handlers"]
)
@@ -54,9 +50,8 @@ class AlarmWorker(thread.ThreadWorker, metaclass=Singleton):
)
def finalize(self):
"""
Finalize the Alarm Worker
"""
"""Finalize the Alarm Worker."""
if self._handlers is not None:
self._handlers.finalize()
@@ -64,9 +59,8 @@ class AlarmWorker(thread.ThreadWorker, metaclass=Singleton):
timers.timers_delete_timer(self._alarm_audit_timer_id)
def do_work(self, action, work):
"""
Do work given to the Alarm Worker
"""
"""Do work given to the Alarm Worker."""
if AlarmThread.ACTION_RAISE_ALARM == action:
DLOG.verbose("Raise alarm with uuid=%s" % work["alarm-uuid"])
self._handlers.raise_alarm(work["alarm-uuid"], work["alarm-data"])
@@ -80,9 +74,7 @@ class AlarmWorker(thread.ThreadWorker, metaclass=Singleton):
class AlarmThread(thread.Thread, metaclass=Singleton):
"""
Alarm Thread
"""
"""Alarm Thread."""
ACTION_RAISE_ALARM = "thread-raise-alarm"
ACTION_CLEAR_ALARM = "thread-clear-alarm"
@@ -92,9 +84,8 @@ class AlarmThread(thread.Thread, metaclass=Singleton):
super(AlarmThread, self).__init__("Alarm", self._worker)
def alarm_raise(self, alarm_uuid, alarm_data):
"""
Send raise alarm to the Alarm Thread Worker
"""
"""Send raise alarm to the Alarm Thread Worker."""
work = dict()
work["alarm-uuid"] = alarm_uuid
work["alarm-data"] = alarm_data
@@ -102,9 +93,8 @@ class AlarmThread(thread.Thread, metaclass=Singleton):
self.send_work(AlarmThread.ACTION_RAISE_ALARM, work)
def alarm_clear(self, alarm_uuid):
"""
Send clear alarm to the Alarm Thread Worker
"""
"""Send clear alarm to the Alarm Thread Worker."""
work = dict()
work["alarm-uuid"] = alarm_uuid
work["alarm-data"] = None
@@ -1,5 +0,0 @@
#
# Copyright (c) 2015-2016 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
@@ -7,55 +7,62 @@ import abc
class AlarmHandler(object, metaclass=abc.ABCMeta):
"""
Abstract Alarm Handler Class Definition
"""
"""Abstract Alarm Handler Class Definition."""
@property
@abc.abstractmethod
def name(self):
"""The name of handler"""
"""The name of handler."""
pass
@property
@abc.abstractmethod
def version(self):
"""The versions of the handler"""
"""The versions of the handler."""
pass
@property
@abc.abstractmethod
def provider(self):
"""Who created the handler"""
"""Who created the handler."""
pass
@property
@abc.abstractmethod
def signature(self):
"""Signature of the handler"""
"""Signature of the handler."""
pass
@abc.abstractmethod
def raise_alarm(self, alarm_uuid, alarm_data):
"""Raise an alarm via the handler"""
"""Raise an alarm via the handler."""
pass
@abc.abstractmethod
def clear_alarm(self, alarm_uuid):
"""Clear an alarm via the handler"""
"""Clear an alarm via the handler."""
pass
@abc.abstractmethod
def audit_alarms(self):
"""Audit alarms via the handler"""
"""Audit alarms via the handler."""
pass
@abc.abstractmethod
def initialize(self, config_file):
"""Initialize the handler"""
"""Initialize the handler."""
pass
@abc.abstractmethod
def finalize(self):
"""Finalize the handler"""
"""Finalize the handler."""
pass
@@ -7,9 +7,7 @@ from datetime import datetime
class AlarmStateData(object):
"""
Alarm State Data
"""
"""Alarm State Data."""
def __init__(self, state):
self.state = state
@@ -19,9 +17,7 @@ class AlarmStateData(object):
class AlarmThresholdData(object):
"""
Alarm Threshold Data
"""
"""Alarm Threshold Data."""
def __init__(self, threshold_value, observed_value):
self.threshold_value = threshold_value
@@ -32,9 +28,7 @@ class AlarmThresholdData(object):
class AlarmData(object):
"""
Alarm Data
"""
"""Alarm Data."""
def __init__(
self,
@@ -10,9 +10,7 @@ from nfv_common.helpers import Singleton
class _AlarmType(Constants, metaclass=Singleton):
"""
Alarm Type Constants
"""
"""Alarm Type Constants."""
UNKNOWN = Constant("unknown")
MULTI_NODE_RECOVERY_MODE = Constant("multi-node-recovery-mode")
@@ -67,18 +65,14 @@ class _AlarmType(Constants, metaclass=Singleton):
class _AlarmContext(Constants, metaclass=Singleton):
"""
Alarm Context Constants
"""
"""Alarm Context Constants."""
ADMIN = Constant("admin")
TENANT = Constant("tenant")
class _AlarmEventType(Constants, metaclass=Singleton):
"""
Alarm Event Type Constants
"""
"""Alarm Event Type Constants."""
UNKNOWN = Constant("unknown")
COMMUNICATIONS_ALARM = Constant("communications-alarm")
@@ -95,9 +89,7 @@ class _AlarmEventType(Constants, metaclass=Singleton):
class _AlarmProbableCause(Constants, metaclass=Singleton):
"""
Alarm Probable Cause Constants
"""
"""Alarm Probable Cause Constants."""
UNKNOWN = Constant("unknown")
INDETERMINATE = Constant("indeterminate")
@@ -108,9 +100,7 @@ class _AlarmProbableCause(Constants, metaclass=Singleton):
class _AlarmSeverity(Constants, metaclass=Singleton):
"""
Alarm Severity Constants
"""
"""Alarm Severity Constants."""
UNKNOWN = Constant("unknown")
CLEARED = Constant("cleared")
@@ -122,9 +112,7 @@ class _AlarmSeverity(Constants, metaclass=Singleton):
class _AlarmTrendIndication(Constants, metaclass=Singleton):
"""
Alarm Trend Indication Constants
"""
"""Alarm Trend Indication Constants."""
UNKNOWN = Constant("unknown")
LESS_SEVERE = Constant("less-severe")
@@ -12,9 +12,7 @@ DLOG = debug.debug_get_logger("nfv_common.catalog.catalog_backend")
class CatalogBackend(stevedore.named.NamedExtensionManager, metaclass=Singleton):
"""
Catalog Backend
"""
"""Catalog Backend."""
_version = "1.0.0"
_signature = "7926ef8d-b04c-4f5b-8627-f40f59fd8d11"
@@ -40,7 +38,8 @@ class CatalogBackend(stevedore.named.NamedExtensionManager, metaclass=Singleton)
@staticmethod
def valid_plugin(plugin):
"""Verify signature of plugin is valid"""
"""Verify signature of plugin is valid."""
if CatalogBackend._signature == plugin.obj.signature:
return True
else:
@@ -52,7 +51,8 @@ class CatalogBackend(stevedore.named.NamedExtensionManager, metaclass=Singleton)
return False
def read_vnf_descriptor(self, vnfd_id, vnf_vendor, vnf_version):
"""Read a particular vnf descriptor"""
"""Read a particular vnf descriptor."""
vnfd_record = None
if self.plugin is not None:
vnfd_record = self.plugin.obj.read_vnf_descriptor(
@@ -61,11 +61,13 @@ class CatalogBackend(stevedore.named.NamedExtensionManager, metaclass=Singleton)
return vnfd_record
def initialize(self):
"""Initialize plugin"""
"""Initialize plugin."""
if self.plugin is not None:
self.plugin.obj.initialize(self._version)
def finalize(self):
"""Finalize plugin"""
"""Finalize plugin."""
if self.plugin is not None:
self.plugin.obj.finalize()
@@ -9,19 +9,22 @@ _catalog_backend = None
def read_vnf_descriptor(vnfd_id, vnf_vendor, vnf_version):
"""Read a vnf descriptor"""
"""Read a vnf descriptor."""
if _catalog_backend is not None:
return _catalog_backend.read_vnf_descriptor(vnfd_id, vnf_vendor, vnf_version)
return None
def catalog_initialize(plugin_namespace, plugin_name):
"""Catalog Initialize"""
"""Catalog Initialize."""
global _catalog_backend
_catalog_backend = CatalogBackend(plugin_namespace, plugin_name)
def catalog_finalize():
"""Catalog Finalize"""
"""Catalog Finalize."""
global _catalog_backend
_catalog_backend = None
@@ -9,9 +9,7 @@ from nfv_common.helpers import Singleton
class ConnectionType(object, metaclass=Singleton):
"""
Connection Type Constants
"""
"""Connection Type Constants."""
UNKNOWN = Constant("unknown")
VIRTUAL_PORT = Constant("virtual-port")
@@ -21,9 +19,7 @@ class ConnectionType(object, metaclass=Singleton):
class ConnectivityType(object, metaclass=Singleton):
"""
Connectivity Type Constants
"""
"""Connectivity Type Constants."""
UNKNOWN = Constant("unknown")
E_LINE = Constant("E-Line")
@@ -20,24 +20,24 @@ from nfv_common.catalog.model._defs import CONNECTIVITY_TYPE
class VNFD(object):
"""
Virtual Network Function Descriptor
id: unique name for this vnfd
vendor: who created this vnfd
descriptor_version: the version of the vnf descriptor
version: the version of the vnf software
vdu: one or more VDU() objects
virtual_link: zero or more VirtualLink() objects
connection_point: one or more connection points described by
ConnectionPointVNFD() objects
dependency: describes dependencies between vdus
monitoring_parameter: monitoring parameters which can be tracked for
this vnf
deployment_flavor: one or more DeploymentFlavor objects
auto_scale_policy: describes the policy in terms of criteria and action
manifest_file: a file that lists all files in the vnf package
manifest_file_security: a file that contains a digest of each file that
it lists as part of the vnf package
"""Virtual Network Function Descriptor.
id: unique name for this vnfd
vendor: who created this vnfd
descriptor_version: the version of the vnf descriptor
version: the version of the vnf software
vdu: one or more VDU() objects
virtual_link: zero or more VirtualLink() objects
connection_point: one or more connection points described by
ConnectionPointVNFD() objects
dependency: describes dependencies between vdus
monitoring_parameter: monitoring parameters which can be tracked for
this vnf
deployment_flavor: one or more DeploymentFlavor objects
auto_scale_policy: describes the policy in terms of criteria and action
manifest_file: a file that lists all files in the vnf package
manifest_file_security: a file that contains a digest of each file that
it lists as part of the vnf package.
"""
def __init__(self, id):
@@ -57,12 +57,12 @@ class VNFD(object):
class ConnectionPointVNFD(object):
"""
Connection Point of a Virtual Network Function Descriptor
id: connection point identifier
virtual_link_reference: references zero or more virtual links by their
identifiers
type: type of connection
"""Connection Point of a Virtual Network Function Descriptor.
id: connection point identifier
virtual_link_reference: references zero or more virtual links by their
identifiers
type: type of connection.
"""
def __init__(self, id):
@@ -72,16 +72,16 @@ class ConnectionPointVNFD(object):
class VirtualLink(object):
"""
Virtual Link
id: unique identifier of this internal virtual link
connectivity_type: the type of connectivity
connection_points_references: 2 or more connection point identifiers
root_requirement: describes the throughput of the link
leaf_requirement: describes the throughput of the leaf connections
qos: describes the qos options to be supported on the virtual link
test_access: describes the test access facilities to be supported on
the virtual link
"""Virtual Link.
id: unique identifier of this internal virtual link
connectivity_type: the type of connectivity
connection_points_references: 2 or more connection point identifiers
root_requirement: describes the throughput of the link
leaf_requirement: describes the throughput of the leaf connections
qos: describes the qos options to be supported on the virtual link
test_access: describes the test access facilities to be supported on
the virtual link.
"""
def __init__(self, id):
@@ -95,13 +95,13 @@ class VirtualLink(object):
class DeploymentFlavor(object):
"""
Deployment Flavor
id: vnf flavor identifier
flavor_key: monitoring parameter and it's value against which this
flavor is being described
constraint: zero or more deployment flavor constraints
constituent_vdu: one or more ConstituentVDU() objects
"""Deployment Flavor.
id: vnf flavor identifier
flavor_key: monitoring parameter and it's value against which this
flavor is being described
constraint: zero or more deployment flavor constraints
constituent_vdu: one or more ConstituentVDU() objects.
"""
def __init__(self, id):
@@ -112,12 +112,12 @@ class DeploymentFlavor(object):
class ConstituentVDU(object):
"""
Constituent Virtual Deployment Unit
vdu_reference: identifier of a vdu
number_of_instances: number of vdu instances required
constituent_vnfc: one or more vnfc identifiers that should be used for
this deployment
"""Constituent Virtual Deployment Unit.
vdu_reference: identifier of a vdu
number_of_instances: number of vdu instances required
constituent_vnfc: one or more vnfc identifiers that should be used for
this deployment.
"""
def __init__(self, vdu_reference):
@@ -127,25 +127,25 @@ class ConstituentVDU(object):
class VDU(object):
"""
Virtual Deployment Unit
id: unique identifier for this vdu within the scope of the vnfd
vm_image: a reference to the vm image, does not need to be specified in
the case of null containers
computation_requirement: description of the required computation
resource characteristics
virtual_memory_resource_element: the virtual memory needed for this vdu
virtual_network_bandwidth_resource: the network bandwidth needed for
this vdu
lifecycle_event: describes vnfc functional scripts/workflows for
specific lifecycle events
constraint: vdu specific constraints
high_availability: specifies the redundancy model
scale_in_out: describes the minimum and maximum number of instances
that can be created when scaling
vnfc: describes one or more VNFC() objects created using this vdu
monitoring_parameter: zero or more monitoring parameters which can be
tracked for a vnfc based on this vdu
"""Virtual Deployment Unit.
id: unique identifier for this vdu within the scope of the vnfd
vm_image: a reference to the vm image, does not need to be specified in
the case of null containers
computation_requirement: description of the required computation
resource characteristics
virtual_memory_resource_element: the virtual memory needed for this vdu
virtual_network_bandwidth_resource: the network bandwidth needed for
this vdu
lifecycle_event: describes vnfc functional scripts/workflows for
specific lifecycle events
constraint: vdu specific constraints
high_availability: specifies the redundancy model
scale_in_out: describes the minimum and maximum number of instances
that can be created when scaling
vnfc: describes one or more VNFC() objects created using this vdu
monitoring_parameter: zero or more monitoring parameters which can be
tracked for a vnfc based on this vdu.
"""
def __init__(self, id):
@@ -163,10 +163,10 @@ class VDU(object):
class VNFC(object):
"""
Virtual Network Function Component
id: unique vnfc identification within the namespace of a specific vnf
connection_point: one or more network connections
"""Virtual Network Function Component.
id: unique vnfc identification within the namespace of a specific vnf
connection_point: one or more network connections.
"""
def __init__(self, id):
@@ -175,11 +175,11 @@ class VNFC(object):
class ConnectionPointVNFC(object):
"""
Connection Point of a Virtual Network Function Component
id: connection point identifier
virtual_link_reference: references zero or more internal virtual links
type: type of network connection
"""Connection Point of a Virtual Network Function Component.
id: connection point identifier
virtual_link_reference: references zero or more internal virtual links
type: type of network connection.
"""
def __init__(self, id):
@@ -1,5 +0,0 @@
#
# Copyright (c) 2015-2016 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
@@ -1,5 +0,0 @@
#
# Copyright (c) 2015-2016 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
@@ -9,45 +9,50 @@ from abc import abstractmethod
class CatalogPlugin(object, metaclass=ABCMeta):
"""
Abstract Catalog Plugin Class Definition
"""
"""Abstract Catalog Plugin Class Definition."""
@property
@abstractmethod
def name(self):
"""The name of plugin"""
"""The name of plugin."""
pass
@property
@abstractmethod
def version(self):
"""The versions of the plugin"""
"""The versions of the plugin."""
pass
@property
@abstractmethod
def provider(self):
"""Vendor created the plugin"""
"""Vendor created the plugin."""
pass
@property
@abstractmethod
def signature(self):
"""Signature of the plugin"""
"""Signature of the plugin."""
pass
@abstractmethod
def read_vnf_descriptor(self, vnfd_id, vnf_vendor, vnf_version):
"""Read a particular vnf descriptor"""
"""Read a particular vnf descriptor."""
pass
@abstractmethod
def initialize(self, version):
"""Initialize the plugin"""
"""Initialize the plugin."""
pass
@abstractmethod
def finalize(self):
"""Finalize the plugin"""
"""Finalize the plugin."""
pass
+5 -9
View File
@@ -15,9 +15,7 @@ CONF = dict()
class Config(configparser.ConfigParser):
"""
Override ConfigParser class to add dictionary functionality.
"""
"""Override ConfigParser class to add dictionary functionality."""
def as_dict(self):
d = dict(self._sections)
@@ -28,17 +26,15 @@ class Config(configparser.ConfigParser):
def section_exists(section):
"""
Returns true if configuration section exists
"""
"""Returns true if configuration section exists."""
section = CONF.get(section, None)
return section is not None
def load(config_file):
"""
Load the configuration file into a global CONF variable.
"""
"""Load the configuration file into a global CONF variable."""
global CONF
config = Config()
@@ -39,9 +39,7 @@ from nfv_common.helpers import Singleton
class DebugConfig(object, metaclass=Singleton):
"""
Debug Configuration
"""
"""Debug Configuration."""
debug_level_mapping = {
"debug.level.none": DEBUG_LEVEL.NONE,
@@ -55,32 +53,28 @@ class DebugConfig(object, metaclass=Singleton):
}
def __init__(self, filename):
"""
Create debug configuration
"""
"""Create debug configuration."""
self._filename = filename
self._config = None
def load(self):
"""
Load debug configuration
"""
"""Load debug configuration."""
if self._config is None:
self._config = configparser.ConfigParser()
self._config.read(self._filename)
@property
def filename(self):
"""
Returns the debug configuration file name
"""
"""Returns the debug configuration file name."""
return self._filename
@property
def debug_level_overall(self):
"""
Returns the overall debug level
"""
"""Returns the overall debug level."""
debug_level = DEBUG_LEVEL.NONE
if self._config is not None:
@@ -101,9 +95,8 @@ class DebugConfig(object, metaclass=Singleton):
@property
def trace_level_overall(self):
"""
Returns the overall trace level
"""
"""Returns the overall trace level."""
trace_level = DEBUG_LEVEL.NONE
if self._config is not None:
@@ -124,9 +117,8 @@ class DebugConfig(object, metaclass=Singleton):
@property
def debug_loggers(self):
"""
Returns a list of debug loggers
"""
"""Returns a list of debug loggers."""
debug_list = []
if self._config is not None:
@@ -9,9 +9,7 @@ from nfv_common.helpers import Singleton
class _DebugLevel(object, metaclass=Singleton):
"""
Debug Level Constants
"""
"""Debug Level Constants."""
NONE = Constant(8000)
CRITICAL = Constant(7000)
+49 -75
View File
@@ -19,9 +19,7 @@ _debug_loggers = {}
class DebugLogFormatter(logging.Formatter):
"""
Debug Log Formatter
"""
"""Debug Log Formatter."""
def formatTime(self, record, date_format=None):
dt = datetime.datetime.fromtimestamp(record.created)
@@ -33,9 +31,7 @@ class DebugLogFormatter(logging.Formatter):
class DebugLogHandler(logging.Handler):
"""
Debug Log Handler
"""
"""Debug Log Handler."""
def __init__(self):
super(DebugLogHandler, self).__init__()
@@ -59,29 +55,28 @@ class DebugLogHandler(logging.Handler):
self.setFormatter(formatter)
def set_process_name(self, process_name):
"""
Set the process name
"""
"""Set the process name."""
self.process_name = process_name
def set_thread_name(self, thread_name):
"""
Set the thread name
"""
"""Set the thread name."""
self.thread_name = thread_name
@staticmethod
def send(log_record):
"""
Send log record to debug logging thread
"""
"""Send log record to debug logging thread."""
DebugLoggingThread().send_log_record(log_record)
def _format_record(self, log_record):
"""Format the record.
It is formatted in a way that it can be pickled and sent to the debug logging
thread.
"""
Format the record so that it can be pickled and sent
to the debug logging thread
"""
if log_record.args:
# Not all arguments can be pickled.
log_record.msg %= log_record.args
@@ -105,9 +100,8 @@ class DebugLogHandler(logging.Handler):
return log_record
def emit(self, record):
"""
Send record to debug logging thread
"""
"""Send record to debug logging thread."""
try:
log_record = self._format_record(record)
DebugLoggingThread().send_log_record(log_record)
@@ -120,9 +114,7 @@ class DebugLogHandler(logging.Handler):
class DebugLogger(object):
"""
Debug Logger
"""
"""Debug Logger."""
log_level_mapping = {
DEBUG_LEVEL.NONE: logging.NOTSET,
@@ -138,9 +130,8 @@ class DebugLogger(object):
def __init__(
self, name, debug_level=DEBUG_LEVEL.NONE, process_name=None, thread_name=None
):
"""
Create debug logger
"""
"""Create debug logger."""
self.name = name
self.process_name = process_name
self.thread_name = thread_name
@@ -153,110 +144,96 @@ class DebugLogger(object):
self.logger.addHandler(DebugLogHandler())
def set_level(self, debug_level):
"""
Set the debug level for the logger
"""
"""Set the debug level for the logger."""
log_level = self.log_level_mapping.get(debug_level, logging.NOTSET)
self.debug_level = debug_level
self.logger.setLevel(log_level)
def set_process_name(self, process_name):
"""
Set the process name
"""
"""Set the process name."""
for handler in self.logger.handlers:
handler.set_process_name(process_name)
def set_thread_name(self, thread_name):
"""
Set the thread name
"""
"""Set the thread name."""
for handler in self.logger.handlers:
handler.set_thread_name(thread_name)
@staticmethod
def get_caller():
"""
Get the calling function and line number
"""
"""Get the calling function and line number."""
caller = inspect.currentframe().f_back.f_back
_, filename = os.path.split(caller.f_code.co_filename)
return "%42s.%-4s " % (filename, caller.f_lineno)
def verbose(self, msg, *args, **kwargs):
"""
Debug log with severity of VERBOSE
"""
"""Debug log with severity of VERBOSE."""
if DEBUG_LEVEL.VERBOSE >= Debug().debug_level:
if DEBUG_LEVEL.VERBOSE >= self.debug_level:
caller = self.get_caller()
self.logger.debug(caller + msg, *args, **kwargs)
def debug(self, msg, *args, **kwargs):
"""
Debug log with severity of DEBUG
"""
"""Debug log with severity of DEBUG."""
if DEBUG_LEVEL.DEBUG >= Debug().debug_level:
if DEBUG_LEVEL.DEBUG >= self.debug_level:
caller = self.get_caller()
self.logger.debug(caller + msg, *args, **kwargs)
def info(self, msg, *args, **kwargs):
"""
Debug log with severity of INFO
"""
"""Debug log with severity of INFO."""
if DEBUG_LEVEL.INFO >= Debug().debug_level:
if DEBUG_LEVEL.INFO >= self.debug_level:
caller = self.get_caller()
self.logger.info(caller + msg, *args, **kwargs)
def notice(self, msg, *args, **kwargs):
"""
Debug log with severity of NOTICE
"""
"""Debug log with severity of NOTICE."""
if DEBUG_LEVEL.NOTICE >= Debug().debug_level:
if DEBUG_LEVEL.NOTICE >= self.debug_level:
caller = self.get_caller()
self.logger.info(caller + msg, *args, **kwargs)
def warn(self, msg, *args, **kwargs):
"""
Debug log with severity of WARNING
"""
"""Debug log with severity of WARNING."""
if DEBUG_LEVEL.WARN >= Debug().debug_level:
if DEBUG_LEVEL.WARN >= self.debug_level:
caller = self.get_caller()
self.logger.warning(caller + msg, *args, **kwargs)
def error(self, msg, *args, **kwargs):
"""
Debug log with severity of ERROR
"""
"""Debug log with severity of ERROR."""
if DEBUG_LEVEL.ERROR >= Debug().debug_level:
if DEBUG_LEVEL.ERROR >= self.debug_level:
caller = self.get_caller()
self.logger.error(caller + msg, *args, **kwargs)
def critical(self, msg, *args, **kwargs):
"""
Debug log with severity of CRITICAL
"""
"""Debug log with severity of CRITICAL."""
if DEBUG_LEVEL.CRITICAL >= Debug().debug_level:
if DEBUG_LEVEL.CRITICAL >= self.debug_level:
caller = self.get_caller()
self.logger.critical(caller + msg, *args, **kwargs)
def exception(self, msg, *args):
"""
Debug exception log
"""
"""Debug exception log."""
self.logger.exception(msg, *args)
def debug_trace(trace_level):
"""
Decorator function used to trace entering and exiting functions
"""
"""Decorator function used to trace entering and exiting functions."""
def trace_wrap(func):
def trace_wrapper(*args, **kwargs):
@@ -307,9 +284,8 @@ def debug_trace(trace_level):
def debug_dump_loggers(directory):
"""
Dump all available loggers
"""
"""Dump all available loggers."""
if os.path.exists(directory):
program_name = os.path.basename(sys.argv[0])
@@ -319,9 +295,8 @@ def debug_dump_loggers(directory):
def debug_get_logger(name, debug_level=None, process_name=None, thread_name=None):
"""
Create a logger if it does not already exist
"""
"""Create a logger if it does not already exist."""
global _debug_loggers
logger = _debug_loggers.get(name, None)
@@ -346,9 +321,8 @@ def debug_get_logger(name, debug_level=None, process_name=None, thread_name=None
def debug_set_loggers_level(debug_level=None):
"""
Set the level of all loggers
"""
"""Set the level of all loggers."""
global _debug_loggers
for name in _debug_loggers:
@@ -13,9 +13,7 @@ from nfv_common.debug._debug_thread import DebugLoggingThread
class Debug(object, metaclass=Singleton):
"""
Debug
"""
"""Debug."""
def __init__(self):
self._config_change_callbacks = list()
@@ -30,22 +28,19 @@ class Debug(object, metaclass=Singleton):
self._config_change_callbacks = list()
def register_config_change_callback(self, callback):
"""
Register a configuration change callback
"""
"""Register a configuration change callback."""
if callback not in self._config_change_callbacks:
self._config_change_callbacks.append(callback)
def deregister_config_change_callback(self, callback):
"""
Deregister a configuration change callback
"""
"""Deregister a configuration change callback."""
self._config_change_callbacks.remove(callback)
def load(self, process_name=None, thread_name=None):
"""
Load debug configuration settings
"""
"""Load debug configuration settings."""
from nfv_common.debug._debug_log import debug_get_logger
from nfv_common.debug._debug_log import debug_set_loggers_level
@@ -58,9 +53,8 @@ class Debug(object, metaclass=Singleton):
debug_get_logger(name, debug_level, process_name, thread_name)
def reload(self):
"""
Reload debug configuration settings
"""
"""Reload debug configuration settings."""
from nfv_common.debug._debug_log import debug_get_logger
from nfv_common.debug._debug_log import debug_set_loggers_level
@@ -77,16 +71,14 @@ class Debug(object, metaclass=Singleton):
@property
def config(self):
"""
Returns the debug configuration
"""
"""Returns the debug configuration."""
return self._config
@config.setter
def config(self, config):
"""
Set the debug configuration file
"""
"""Set the debug configuration file."""
if self._config is None:
self._config = config
self._debug_config = DebugConfig(config["config_file"])
@@ -94,72 +86,62 @@ class Debug(object, metaclass=Singleton):
@property
def debug_level(self):
"""
Returns the debug level currently set
"""
"""Returns the debug level currently set."""
return self._debug_level
@property
def trace_level(self):
"""
Returns the debug trace level currently set
"""
"""Returns the debug trace level currently set."""
return self._debug_trace_level
@property
def trace_depth(self):
"""
Returns the debug trace depth currently set
"""
"""Returns the debug trace depth currently set."""
return self._debug_trace_depth
@trace_depth.setter
def trace_depth(self, trace_depth):
"""
Set the current trace depth
"""
"""Set the current trace depth."""
self._debug_trace_depth = trace_depth
@property
def output(self):
"""
Returns where the debug output should go
"""
"""Returns where the debug output should go."""
return self._debug_output
def debug_register_config_change_callback(callback):
"""
Register debug configuration change callback
"""
"""Register debug configuration change callback."""
return Debug().register_config_change_callback(callback)
def debug_deregister_config_change_callback(callback):
"""
Deregister debug configuration change callback
"""
"""Deregister debug configuration change callback."""
return Debug().deregister_config_change_callback(callback)
def debug_get_config():
"""
Get debug configuration
"""
"""Get debug configuration."""
return Debug().config
def debug_reload_config():
"""
Reload debug configuration
"""
"""Reload debug configuration."""
Debug().reload()
def debug_initialize(config, process_name=None, thread_name=None):
"""
Initializes the debug subsystem
"""
"""Initializes the debug subsystem."""
Debug().reinitialize()
if config is not None:
@@ -169,7 +151,6 @@ def debug_initialize(config, process_name=None, thread_name=None):
def debug_finalize():
"""
Finalizes the debug subsystem
"""
"""Finalizes the debug subsystem."""
return
@@ -14,14 +14,11 @@ from nfv_common.helpers import Singleton
class DebugLoggingThreadFormatter(logging.Formatter):
"""
Debug Log Formatter
"""
"""Debug Log Formatter."""
def format(self, record):
"""
Override the formatter if the record has already been formatted
"""
"""Override the formatter if the record has already been formatted."""
if hasattr(record, "formatted_log"):
return record.formatted_log
else:
@@ -29,9 +26,7 @@ class DebugLoggingThreadFormatter(logging.Formatter):
class DebugLoggingThread(object, metaclass=Singleton):
"""
Debug Logging Thread
"""
"""Debug Logging Thread."""
def __init__(self):
self._handlers = list()
@@ -41,21 +36,18 @@ class DebugLoggingThread(object, metaclass=Singleton):
self._thread.start()
def send_log_record(self, log_record):
"""
Send a log record to debug logging thread
"""
"""Send a log record to debug logging thread."""
self._log_queue.put_nowait(["log-record", log_record])
def send_log_config(self, config):
"""
Send log configuration to debug logging thread
"""
"""Send log configuration to debug logging thread."""
self._log_queue.put_nowait(["log-config", config])
def _receive_logs(self):
"""
Receive log records sent to the debug logging thread
"""
"""Receive log records sent to the debug logging thread."""
formatter = DebugLoggingThreadFormatter()
while True:
@@ -12,9 +12,7 @@ DLOG = debug.debug_get_logger("nfv_common.event_log.event_log_handlers")
class EventLogHandlers(stevedore.enabled.EnabledExtensionManager, metaclass=Singleton):
"""
Event Log Handlers
"""
"""Event Log Handlers."""
_version = "1.0.0"
_signature = "e33d7cf6-f270-4256-893e-16266ee4dd2e"
@@ -43,9 +41,8 @@ class EventLogHandlers(stevedore.enabled.EnabledExtensionManager, metaclass=Sing
@staticmethod
def valid_handler(handler):
"""
Verify signature of the handler is valid
"""
"""Verify signature of the handler is valid."""
if EventLogHandlers._signature == handler.obj.signature:
return True
else:
@@ -57,22 +54,19 @@ class EventLogHandlers(stevedore.enabled.EnabledExtensionManager, metaclass=Sing
return False
def log(self, log_data):
"""
Log a particular event using the handlers
"""
"""Log a particular event using the handlers."""
for handler_type, handler in list(self._handlers.items()):
handler.obj.log(log_data)
def initialize(self, config_file):
"""
Initialize handlers
"""
"""Initialize handlers."""
for handler_id, handler in list(self._handlers.items()):
handler.obj.initialize(config_file)
def finalize(self):
"""
Finalize handlers
"""
"""Finalize handlers."""
for handler_id, handler in list(self._handlers.items()):
handler.obj.finalize()
@@ -6,36 +6,32 @@
def event_log(log_data):
"""
Log a particular event
"""
"""Log a particular event."""
from nfv_common.event_log._event_log_thread import EventLogThread
EventLogThread().log(log_data)
def event_log_subsystem_sane():
"""
Returns true if the event log subsystem is healthy
"""
"""Returns true if the event log subsystem is healthy."""
from nfv_common.event_log._event_log_thread import EventLogThread
return 600 >= EventLogThread().stall_elapsed_secs
def event_log_initialize(config):
"""
Initialize the event log subsystem
"""
"""Initialize the event log subsystem."""
from nfv_common.event_log._event_log_thread import EventLogThread
EventLogThread(config).start()
def event_log_finalize():
"""
Finalize the event log subsystem
"""
"""Finalize the event log subsystem."""
from nfv_common.event_log._event_log_thread import EventLogThread
EventLogThread().stop(max_wait_in_seconds=5)
@@ -15,9 +15,7 @@ DLOG = debug.debug_get_logger("nfv_common.event_log.event_log_thread")
class EventLogWorker(thread.ThreadWorker, metaclass=Singleton):
"""
Event Log Worker
"""
"""Event Log Worker."""
def __init__(self, name, config):
super(EventLogWorker, self).__init__(name)
@@ -25,33 +23,28 @@ class EventLogWorker(thread.ThreadWorker, metaclass=Singleton):
self._handlers = None
def initialize(self):
"""
Initialize the Event Log Worker
"""
"""Initialize the Event Log Worker."""
self._handlers = EventLogHandlers(
self._config["namespace"], self._config["handlers"]
)
self._handlers.initialize(self._config["config_file"])
def finalize(self):
"""
Finalize the Event Log Worker
"""
"""Finalize the Event Log Worker."""
if self._handlers is not None:
self._handlers.finalize()
def do_work(self, action, work):
"""
Do work given to the Event Log Worker
"""
"""Do work given to the Event Log Worker."""
if EventLogThread.ACTION_LOG_EVENT == action:
self._handlers.log(work["log-data"])
class EventLogThread(thread.Thread, metaclass=Singleton):
"""
Event Log Thread
"""
"""Event Log Thread."""
ACTION_LOG_EVENT = "thread-log-event"
@@ -60,9 +53,8 @@ class EventLogThread(thread.Thread, metaclass=Singleton):
super(EventLogThread, self).__init__("Event-Log", self._worker)
def log(self, log_data):
"""
Send log data to the Event Log Thread
"""
"""Send log data to the Event Log Thread."""
work = dict()
work["log-data"] = log_data
self.send_work(EventLogThread.ACTION_LOG_EVENT, work)
@@ -1,5 +0,0 @@
#
# Copyright (c) 2015-2016 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
@@ -7,45 +7,50 @@ import abc
class EventLogHandler(object, metaclass=abc.ABCMeta):
"""
Abstract Event Log Handler Class Definition
"""
"""Abstract Event Log Handler Class Definition."""
@property
@abc.abstractmethod
def name(self):
"""The name of handler"""
"""The name of handler."""
pass
@property
@abc.abstractmethod
def version(self):
"""The versions of the handler"""
"""The versions of the handler."""
pass
@property
@abc.abstractmethod
def provider(self):
"""Who created the handler"""
"""Who created the handler."""
pass
@property
@abc.abstractmethod
def signature(self):
"""Signature of the handler"""
"""Signature of the handler."""
pass
@abc.abstractmethod
def log(self, log_data):
"""Log an event via the handler"""
"""Log an event via the handler."""
pass
@abc.abstractmethod
def initialize(self, config_file):
"""Initialize the handler"""
"""Initialize the handler."""
pass
@abc.abstractmethod
def finalize(self):
"""Finalize the handler"""
"""Finalize the handler."""
pass
@@ -7,18 +7,14 @@ from datetime import datetime
class EventLogStateData(object):
"""
Event Log State Data
"""
"""Event Log State Data."""
def __init__(self, state):
self.state = state
class EventLogThresholdData(object):
"""
Event Log Threshold Data
"""
"""Event Log Threshold Data."""
def __init__(self, threshold_value, observed_value):
self.threshold_value = threshold_value
@@ -26,9 +22,7 @@ class EventLogThresholdData(object):
class EventLogData(object):
"""
Event Log Data
"""
"""Event Log Data."""
_id = 1
@@ -62,7 +56,6 @@ class EventLogData(object):
EventLogData._id += 1
def as_dict(self):
"""
Render this object as a dictionary of its fields
"""
"""Render this object as a dictionary of its fields."""
return dict.copy(self.__dict__)
@@ -10,9 +10,7 @@ from nfv_common.helpers import Singleton
class _EventId(Constants, metaclass=Singleton):
"""
Event Type Constants
"""
"""Event Type Constants."""
UNKNOWN = Constant("unknown")
MULTI_NODE_RECOVERY_MODE_ENTER = Constant("multi-node-recovery-enter")
@@ -279,9 +277,7 @@ class _EventId(Constants, metaclass=Singleton):
class _EventType(Constants, metaclass=Singleton):
"""
Event Type Constants
"""
"""Event Type Constants."""
UNKNOWN = Constant("unknown")
STATE_EVENT = Constant("state-event")
@@ -291,18 +287,14 @@ class _EventType(Constants, metaclass=Singleton):
class _EventContext(Constants, metaclass=Singleton):
"""
Event Context Constants
"""
"""Event Context Constants."""
ADMIN = Constant("admin")
TENANT = Constant("tenant")
class _EventImportance(Constants, metaclass=Singleton):
"""
Event Importance Constants
"""
"""Event Importance Constants."""
UNKNOWN = Constant("unknown")
HIGH = Constant("high")
@@ -311,9 +303,7 @@ class _EventImportance(Constants, metaclass=Singleton):
class _EventInitiatedBy(Constants, metaclass=Singleton):
"""
Initiated-By Constants
"""
"""Initiated-By Constants."""
UNKNOWN = Constant("unknown")
TENANT = Constant("tenant")
+3 -3
View File
@@ -6,9 +6,9 @@
class PickleableException(Exception):
"""
Pickleable Exception
Used to mark custom exception classes that can be pickled.
"""Pickleable Exception.
Used to mark custom exception classes that can be pickled.
"""
pass
+31 -52
View File
@@ -18,9 +18,7 @@ def _analysis_instances_success(
action_types=None,
callback=None,
):
"""
Analyze records and determine if instance success
"""
"""Analyze records and determine if instance success."""
def default_callback(idx, record):
return True
@@ -195,9 +193,8 @@ def _analysis_instances_success(
def analysis_instance_start_success(
instance_uuid, instance_name, records, action=False, guest_hb=False
):
"""
Analyze records and determine if instance is started
"""
"""Analyze records and determine if instance is started."""
always = True
possible_records = [
@@ -221,9 +218,8 @@ def analysis_instance_start_success(
def analysis_instance_stop_success(
instance_uuid, instance_name, records, action=False, guest_hb=False
):
"""
Analyze records and determine if instance is stopped
"""
"""Analyze records and determine if instance is stopped."""
always = True
possible_records = [
@@ -254,9 +250,8 @@ def analysis_instance_stop_success(
def analysis_instance_pause_success(
instance_uuid, instance_name, records, action=False, guest_hb=False
):
"""
Analyze records and determine if instance is paused
"""
"""Analyze records and determine if instance is paused."""
always = True
possible_records = [
@@ -287,9 +282,8 @@ def analysis_instance_pause_success(
def analysis_instance_unpause_success(
instance_uuid, instance_name, records, action=False, guest_hb=False
):
"""
Analyze records and determine if instance is unpaused
"""
"""Analyze records and determine if instance is unpaused."""
always = True
possible_records = [
@@ -322,9 +316,8 @@ def analysis_instance_unpause_success(
def analysis_instance_suspend_success(
instance_uuid, instance_name, records, action=False, guest_hb=False
):
"""
Analyze records and determine if instance is suspended
"""
"""Analyze records and determine if instance is suspended."""
always = True
possible_records = [
@@ -359,9 +352,8 @@ def analysis_instance_suspend_success(
def analysis_instance_resume_success(
instance_uuid, instance_name, records, action=False, guest_hb=False
):
"""
Analyze records and determine if instance is resumed
"""
"""Analyze records and determine if instance is resumed."""
always = True
possible_records = [
@@ -390,9 +382,7 @@ def analysis_instance_resume_success(
def analysis_instance_reboot_success(
instance_uuid, instance_name, records, action=False, guest_hb=False
):
"""
Analyze records and determine if instance rebooted
"""
"""Analyze records and determine if instance rebooted."""
def callback(idx, record):
record_data = record["data"]
@@ -437,9 +427,8 @@ def analysis_instance_reboot_success(
def analysis_instance_rebuild_success(
instance_uuid, instance_name, records, action=False, guest_hb=False
):
"""
Analyze records and determine if instance was rebuilt
"""
"""Analyze records and determine if instance was rebuilt."""
always = True
possible_records = [
@@ -463,9 +452,8 @@ def analysis_instance_rebuild_success(
def analysis_instance_live_migrate_success(
instance_uuid, instance_name, records, action=False, guest_hb=False
):
"""
Analyze records and determine if instance live-migrated
"""
"""Analyze records and determine if instance live-migrated."""
always = True
possible_records = [
@@ -510,9 +498,8 @@ def analysis_instance_live_migrate_success(
def analysis_instance_cold_migrate_success(
instance_uuid, instance_name, records, action=False, guest_hb=False
):
"""
Analyze records and determine if instance cold-migrated
"""
"""Analyze records and determine if instance cold-migrated."""
always = True
guest_hb_only = not action and guest_hb
@@ -559,9 +546,8 @@ def analysis_instance_cold_migrate_success(
def analysis_instance_cold_migrate_confirm_success(
instance_uuid, instance_name, records, action=False, guest_hb=False
):
"""
Analyze records and determine if instance cold-migrate confirmed
"""
"""Analyze records and determine if instance cold-migrate confirmed."""
possible_records = [
(action, NFV_VIM.INSTANCE_NFVI_ACTION_START),
(action, NFV_VIM.INSTANCE_COLD_MIGRATE_CONFIRM_STATE),
@@ -596,9 +582,8 @@ def analysis_instance_cold_migrate_confirm_success(
def analysis_instance_cold_migrate_revert_success(
instance_uuid, instance_name, records, action=False, guest_hb=False
):
"""
Analyze records and determine if instance cold-migrate reverted
"""
"""Analyze records and determine if instance cold-migrate reverted."""
possible_records = [
(action, NFV_VIM.INSTANCE_NFVI_ACTION_START),
(action, NFV_VIM.INSTANCE_COLD_MIGRATE_REVERT_STATE),
@@ -633,9 +618,8 @@ def analysis_instance_cold_migrate_revert_success(
def analysis_instance_resize_success(
instance_uuid, instance_name, records, action=False, guest_hb=False
):
"""
Analyze records and determine if instance resized
"""
"""Analyze records and determine if instance resized."""
possible_records = [
(action, NFV_VIM.INSTANCE_NFVI_ACTION_START),
(action, NFV_VIM.INSTANCE_RESIZE_STATE),
@@ -667,9 +651,8 @@ def analysis_instance_resize_success(
def analysis_instance_resize_confirm_success(
instance_uuid, instance_name, records, action=False, guest_hb=False
):
"""
Analyze records and determine if instance resize confirmed
"""
"""Analyze records and determine if instance resize confirmed."""
possible_records = [
(action, NFV_VIM.INSTANCE_NFVI_ACTION_START),
(action, NFV_VIM.INSTANCE_RESIZE_CONFIRM_STATE),
@@ -699,9 +682,8 @@ def analysis_instance_resize_confirm_success(
def analysis_instance_resize_revert_success(
instance_uuid, instance_name, records, action=False, guest_hb=False
):
"""
Analyze records and determine if instance resize reverted
"""
"""Analyze records and determine if instance resize reverted."""
possible_records = [
(action, NFV_VIM.INSTANCE_NFVI_ACTION_START),
(action, NFV_VIM.INSTANCE_RESIZE_REVERT_STATE),
@@ -729,9 +711,7 @@ def analysis_instance_resize_revert_success(
def analysis_stdout(records):
"""
Analyze records and display results to stdout
"""
"""Analyze records and display results to stdout."""
def timestamp_str(timestamp_data):
return timestamp_data.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
@@ -756,7 +736,6 @@ def analysis_stdout(records):
NFV_VIM.HOST_DELETING_STATE,
NFV_VIM.HOST_DELETED_STATE,
]:
if data["host_name"] in hosts:
prev_record = hosts[data["host_name"]]
elapsed_time = record["timestamp"] - prev_record["timestamp"]
+1 -3
View File
@@ -6,9 +6,7 @@
class _NfvVimRecordType(object):
"""
NFV-VIM Record Type Constants
"""
"""NFV-VIM Record Type Constants."""
HOST_NFVI_STATE_CHANGE = "nfv_vim_host_nfvi_state_change"
HOST_CONFIGURE_STATE = "nfv_vim_host_configure_state"
@@ -10,9 +10,8 @@ DLOG = debug.debug_get_logger("forensic-evidence")
def evidence_from_files(files, start_date, end_date, progress=None):
"""
Gather evidence
"""
"""Gather evidence."""
records = list()
file_ctrl = dict()
@@ -1,4 +1,4 @@
# Copyright (c) 2015-2016 Wind River Systems, Inc.
# Copyright (c) 2015-2016, 2026 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
@@ -6,14 +6,12 @@ from nfv_common.forensic import _parsers
def forensic_initialize():
"""
Initialize forensic module
"""
"""Initialize forensic module."""
_parsers.parser_initialize()
def forensic_finalize():
"""
Finalize forensic module
"""
"""Finalize forensic module."""
_parsers.parser_finalize()
@@ -18,9 +18,7 @@ from pyparsing import Word
class NfvVimParser(object):
"""
NFV-VIM Parser
"""
"""NFV-VIM Parser."""
def __init__(self, config_data):
self._config_data = config_data
@@ -122,9 +120,8 @@ class NfvVimParser(object):
def parser_initialize():
"""
Initialize module
"""
"""Initialize module."""
path = os.path.abspath(__file__)
config_file = os.path.dirname(path) + "/config/nfv-vim.yaml"
if os.path.isfile(config_file):
@@ -134,7 +131,6 @@ def parser_initialize():
def parser_finalize():
"""
Finalize module
"""
"""Finalize module."""
pass
@@ -8,23 +8,20 @@ _parsers = dict()
def parser_get(parser_name):
"""
Returns the associated parser
"""
"""Returns the associated parser."""
return _parsers.get(parser_name, None)
def parser_initialize():
"""
Initialize module
"""
"""Initialize module."""
global _parsers
_parsers["nfv-vim"] = _nfv_vim_parser.parser_initialize()
def parser_finalize():
"""
Finalize module
"""
"""Finalize module."""
_nfv_vim_parser.parser_finalize()
+10 -19
View File
@@ -11,7 +11,8 @@ import socket
def syscall_retry_on_interrupt(func, *args):
"""Attempt system call again if interrupted by EINTR"""
"""Attempt system call again if interrupted by EINTR."""
for _ in range(0, 5):
try:
return func(*args)
@@ -37,9 +38,7 @@ def process_uptime_in_secs():
class Object(object):
"""
Class Object Type Definition
"""
"""Class Object Type Definition."""
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
@@ -49,9 +48,7 @@ class Object(object):
class Result(object):
"""
Generic Result Object Type Definition
"""
"""Generic Result Object Type Definition."""
def __init__(self, result_data, ancillary_data=None):
self.result_data = result_data
@@ -73,9 +70,7 @@ class Constants(object):
class Constant(object):
"""
Constant Type Definition
"""
"""Constant Type Definition."""
def __init__(self, value):
self.value = value
@@ -91,9 +86,7 @@ class Constant(object):
class Singleton(type):
"""
Singleton Type Definition
"""
"""Singleton Type Definition."""
_instances = {}
@@ -104,9 +97,7 @@ class Singleton(type):
def coroutine(func):
"""
Co-Routine decorator that wraps a function and starts the co-routine
"""
"""Co-Routine decorator that wraps a function and starts the co-routine."""
def start(*args, **kwargs):
target = func(*args, **kwargs)
@@ -118,14 +109,14 @@ def coroutine(func):
def get_local_host_name():
"""
Returns the name of the local host
"""
"""Returns the name of the local host."""
return socket.gethostname()
def get_system_ca_file():
"""Return path to system default CA file."""
# Standard CA file locations for Debian/Ubuntu, RedHat/Fedora,
# Suse, FreeBSD/OpenBSD
ca_path = [
+19 -30
View File
@@ -13,9 +13,7 @@ DLOG = debug.debug_get_logger("nfv_common.histogram")
class Histogram(object):
"""
Histogram Object
"""
"""Histogram Object."""
def __init__(self, name, num_buckets, units):
self._name = name
@@ -32,15 +30,13 @@ class Histogram(object):
@property
def name(self):
"""
Returns the name of the histogram
"""
"""Returns the name of the histogram."""
return self._name
def add_data(self, sample):
"""
Convert data given to the nearest power of two.
"""
"""Convert data given to the nearest power of two."""
sample_as_int = int(sample)
if 0 == sample_as_int:
bucket_idx = sample_as_int.bit_length()
@@ -61,9 +57,8 @@ class Histogram(object):
self._buckets[bucket_idx] += 1
def reset_data(self):
"""
Clear out the collected samples.
"""
"""Clear out the collected samples."""
self._reset_date = datetime.datetime.now()
self._sample_total = 0
self._num_samples = 0
@@ -75,9 +70,8 @@ class Histogram(object):
@staticmethod
def _scale_sample(scale_min, scale_max, sample_min, sample_max, sample):
"""
Normalize sample to be compared with other samples
"""
"""Normalize sample to be compared with other samples."""
if scale_min == scale_max:
return sample
@@ -90,9 +84,8 @@ class Histogram(object):
) + sample_max
def display_data(self, pretty_format=True):
"""
Output the histogram to a log.
"""
"""Output the histogram to a log."""
date_str = ""
values_str = ""
@@ -153,18 +146,16 @@ _histograms = dict()
def _find_histogram(name):
"""
Lookup a histogram with a particular name
"""
"""Lookup a histogram with a particular name."""
if name in _histograms:
return _histograms[name]
return None
def add_histogram_data(name, sample, units):
"""
Add a sample to a histogram
"""
"""Add a sample to a histogram."""
global _histograms
histogram = _find_histogram(name)
@@ -176,9 +167,8 @@ def add_histogram_data(name, sample, units):
def reset_histogram_data(name=None):
"""
Reset histogram data
"""
"""Reset histogram data."""
if name is None:
for histogram in list(_histograms.values()):
histogram.reset_data()
@@ -189,9 +179,8 @@ def reset_histogram_data(name=None):
def display_histogram_data(name=None, pretty_format=True):
"""
Display histogram data captured
"""
"""Display histogram data captured."""
if name is None:
for histogram in list(_histograms.values()):
histogram.display_data(pretty_format)
+10 -15
View File
@@ -18,9 +18,8 @@ except ImportError:
def profile_memory_references(obj_type, obj_name):
"""
Graph memory references
"""
"""Graph memory references."""
if memory_profiling is not None:
objs = objgraph.by_type(obj_type)
objgraph.show_backrefs(
@@ -32,17 +31,15 @@ def profile_memory_references(obj_type, obj_name):
def profile_memory_set_reference():
"""
Set the memory usage reference
"""
"""Set the memory usage reference."""
if memory_profiling is not None:
memory_profiling.setref()
def profile_memory_dump():
"""
Dumps the current memory usage
"""
"""Dumps the current memory usage."""
if memory_profiling is not None:
DLOG.info("%s" % "-" * 120)
DLOG.info("Memory Profile: %s" % memory_profiling.heap())
@@ -51,16 +48,14 @@ def profile_memory_dump():
def profiler_initialize():
"""
Profiler - Initialize
"""
"""Profiler - Initialize."""
if memory_profiling is not None:
DLOG.info("Memory Profiling Enabled")
memory_profiling.setref()
def profiler_finalize():
"""
Profiler - Finalize
"""
"""Profiler - Finalize."""
pass
@@ -17,9 +17,8 @@ MESSAGE_ONE = b"1"
def schedule_function_call(func, *args, **kwargs):
"""
Schedule a function call to be performed at a later time
"""
"""Schedule a function call to be performed at a later time."""
global _send_socket, _pending_function_calls
function_data = (func, args, kwargs)
@@ -43,9 +42,8 @@ def _schedule_dispatch():
def schedule_initialize():
"""
Initialize the schedule module
"""
"""Initialize the schedule module."""
global _send_socket, _receive_socket, _pending_function_calls
_send_socket, _receive_socket = socket.socketpair()
@@ -57,9 +55,8 @@ def schedule_initialize():
def schedule_finalize():
"""
Finalize the schedule module
"""
"""Finalize the schedule module."""
global _send_socket, _receive_socket, _pending_function_calls
if _send_socket is not None:
@@ -17,9 +17,9 @@ _error_callbacks = dict()
def selobj_add_read_obj(selobj, callback, *callback_args, **callback_kwargs):
"""
Add read selection object, callback is a co-routine that is
sent the selection object that has become readable
"""Add read selection object, callback is a co-routine that is
sent the selection object that has become readable.
"""
global _read_callbacks
@@ -28,9 +28,8 @@ def selobj_add_read_obj(selobj, callback, *callback_args, **callback_kwargs):
def selobj_del_read_obj(selobj):
"""
Delete read selection object
"""
"""Delete read selection object."""
global _read_callbacks
if selobj in list(_read_callbacks):
@@ -38,9 +37,9 @@ def selobj_del_read_obj(selobj):
def selobj_add_write_obj(selobj, callback, *callback_args, **callback_kwargs):
"""
Add write selection object, callback is a co-routine that is
sent the selection object that has become writeable
"""Add write selection object, callback is a co-routine that is
sent the selection object that has become writeable.
"""
global _write_callbacks
@@ -49,9 +48,8 @@ def selobj_add_write_obj(selobj, callback, *callback_args, **callback_kwargs):
def selobj_del_write_obj(selobj):
"""
Delete write selection object
"""
"""Delete write selection object."""
global _write_callbacks
if selobj in list(_write_callbacks):
@@ -59,9 +57,9 @@ def selobj_del_write_obj(selobj):
def selobj_add_error_callback(selobj, callback, *callback_args, **callback_kwargs):
"""
Add selection object error callback which is a co-routine that is
called when the selection object is in error
"""Add selection object error callback which is a co-routine that is
called when the selection object is in error.
"""
global _error_callbacks
@@ -70,9 +68,8 @@ def selobj_add_error_callback(selobj, callback, *callback_args, **callback_kwarg
def selobj_del_error_callback(selobj):
"""
Delete selection object error callback
"""
"""Delete selection object error callback."""
global _error_callbacks
if selobj in list(_error_callbacks):
@@ -80,9 +77,9 @@ def selobj_del_error_callback(selobj):
def selobj_dispatch(timeout_in_ms):
"""
Dispatch selection objects that have become readable or writeable
within the given time period
"""Dispatch selection objects that have become readable or writeable
within the given time period.
"""
from nfv_common import histogram
from nfv_common import timers
@@ -152,9 +149,8 @@ def selobj_dispatch(timeout_in_ms):
def selobj_initialize():
"""
Initialize the selection object module
"""
"""Initialize the selection object module."""
global _read_callbacks, _write_callbacks
del _read_callbacks
@@ -165,9 +161,8 @@ def selobj_initialize():
def selobj_finalize():
"""
Finalize the selection object module
"""
"""Finalize the selection object module."""
global _read_callbacks, _write_callbacks
del _read_callbacks
@@ -9,49 +9,40 @@ DLOG = debug.debug_get_logger("nfv_common.state_machine.state")
class State(object):
"""
State Object
"""
"""State Object."""
def __init__(self, name):
"""
Create State
"""
"""Create State."""
self._name = name
@property
def name(self):
"""
Returns the name of the state
"""
"""Returns the name of the state."""
return self._name
def __str__(self):
"""
Returns the name of the state
"""
"""Returns the name of the state."""
return self._name
def enter(self, context, *context_args, **context_kwargs):
"""
Called by the State Machine to enter this state
"""
"""Called by the State Machine to enter this state."""
raise NotImplementedError
def exit(self, context):
"""
Called by the State Machine to exit this state
"""
"""Called by the State Machine to exit this state."""
raise NotImplementedError
def transition(self, context, event, event_data, to_state):
"""
Called by the State Machine to transition from this state
"""
"""Called by the State Machine to transition from this state."""
raise NotImplementedError
def handle_event(self, context, event, event_data=None):
"""
Called by the State Machine to handle an event
"""
"""Called by the State Machine to handle an event."""
raise NotImplementedError
@@ -9,40 +9,33 @@ DLOG = debug.debug_get_logger("nfv_common.state_machine.state_exception")
class StateException(Exception):
"""
State Exception
"""
"""State Exception."""
def __init__(self, message, reason):
"""
Create a State exception
"""
"""Create a State exception."""
super(StateException, self).__init__(message, reason)
self._reason = reason # a message string or another exception
self._message = message
def __str__(self):
"""
Return a string representing the exception
"""
"""Return a string representing the exception."""
return "[State Exception:reason=%s]" % self._reason
def __repr__(self):
"""
Provide a representation of the exception
"""
"""Provide a representation of the exception."""
return str(self)
@property
def message(self):
"""
Returns the message for the exception
"""
"""Returns the message for the exception."""
return self._message
@property
def reason(self):
"""
Returns the reason for the exception
"""
"""Returns the reason for the exception."""
return self._reason
@@ -13,14 +13,11 @@ DLOG = debug.debug_get_logger("nfv_common.state_machine.state_machine")
class StateMachine(object):
"""
State Machine Object
"""
"""State Machine Object."""
def __init__(self, context, context_args, context_kwargs, initial_state, states):
"""
Create State Machine
"""
"""Create State Machine."""
if context_args is None:
context_args = tuple()
@@ -39,30 +36,26 @@ class StateMachine(object):
@property
def _context(self):
"""
Returns the context
"""
"""Returns the context."""
context = self._context_reference()
return context
@property
def current_state(self):
"""
Returns the current state
"""
"""Returns the current state."""
return self._current_state
def register_state_change_callback(self, callback):
"""
Register state change callback
"""
"""Register state change callback."""
if callback not in self._state_change_callbacks:
self._state_change_callbacks.append(callback)
def handle_event(self, event, event_data=None):
"""
Handle event
"""
"""Handle event."""
if self._transitioning:
self._event_backlog.append((self._event_backlog_state, event, event_data))
return
@@ -29,9 +29,7 @@ def result_log(result, message):
class StateTask(object):
"""
State Task
"""
"""State Task."""
def __init__(self, name, task_work_list):
self._name = name
@@ -53,15 +51,13 @@ class StateTask(object):
@property
def name(self):
"""
Returns the name of the task
"""
"""Returns the name of the task."""
return self._name
def _cleanup(self):
"""
State Task Cleanup
"""
"""State Task Cleanup."""
if self._timer_id is not None:
timers.timers_delete_timer(self._timer_id)
self._timer_id = None
@@ -71,9 +67,8 @@ class StateTask(object):
self._task_work_timer_id = None
def _abort(self):
"""
State Task Internal Abort
"""
"""State Task Internal Abort."""
DLOG.debug("Task (%s) abort." % self._name)
if self._task_inprogress and 0 < len(self._task_work_list):
@@ -88,17 +83,15 @@ class StateTask(object):
self._cleanup()
def abort(self):
"""
State Task Abort
"""
"""State Task Abort."""
self._task_result = STATE_TASK_RESULT.ABORTED
self._task_result_reason = "aborted"
self._abort()
def start(self):
"""
State Task Start
"""
"""State Task Start."""
self._cleanup()
self._current_task_work = 0
self._task_inprogress = True
@@ -110,9 +103,8 @@ class StateTask(object):
self._run()
def refresh_timeouts(self):
"""
State Task Refresh Timeouts
"""
"""State Task Refresh Timeouts."""
if self._timer_id is None:
# No need to refresh task timer, task not started
return
@@ -154,9 +146,8 @@ class StateTask(object):
)
def _run(self):
"""
State Task Run
"""
"""State Task Run."""
if not self._task_inprogress:
DLOG.debug("Task (%s) not inprogress." % self._name)
return
@@ -210,34 +201,29 @@ class StateTask(object):
self._complete(self._task_result, self._task_result_reason)
def inprogress(self):
"""
Returns if the task is inprogress or not
"""
"""Returns if the task is inprogress or not."""
return self._task_inprogress
def is_failed(self):
"""
Return true if this task is failed
"""
"""Return true if this task is failed."""
return STATE_TASK_RESULT.FAILED == self._task_result
def timed_out(self):
"""
Return true if this task has timed out
"""
"""Return true if this task has timed out."""
return STATE_TASK_RESULT.TIMED_OUT == self._task_result
def aborted(self):
"""
Return true if this task was aborted
"""
"""Return true if this task was aborted."""
return STATE_TASK_RESULT.ABORTED == self._task_result
@coroutine
def _timeout(self):
"""
State Task Timeout
"""
"""State Task Timeout."""
(yield)
DLOG.debug(
"Task (%s) timed out, timeout_in_secs=%s."
@@ -249,9 +235,8 @@ class StateTask(object):
self._complete(self._task_result, self._task_result_reason)
def task_work_complete(self, task_work_result, task_work_result_reason=None):
"""
State Task Work Complete
"""
"""State Task Work Complete."""
task_work = self._task_work_list[self._current_task_work]
result_log(
task_work_result,
@@ -298,9 +283,8 @@ class StateTask(object):
@coroutine
def _task_work_timeout(self):
"""
State Task Work Timeout
"""
"""State Task Work Timeout."""
(yield)
if len(self._task_work_list) <= self._current_task_work:
DLOG.error(
@@ -325,21 +309,18 @@ class StateTask(object):
self.task_work_complete(task_work_result, task_work_result_reason)
def _complete(self, result, reason):
"""
State Task Internal Complete
"""
"""State Task Internal Complete."""
self.complete(result, reason)
def complete(self, result, reason):
"""
State Task Complete (expected to be overridden by child class)
"""
"""State Task Complete (expected to be overridden by child class)."""
DLOG.debug("Task (%s) complete." % self._name)
def handle_event(self, event, event_data=None):
"""
State Task Handle Event (expected to be overridden by child class)
"""
"""State Task Handle Event (expected to be overridden by child class)."""
DLOG.debug("Task (%s) handle event (%s)." % (self._name, event))
handled = False
@@ -11,9 +11,7 @@ from nfv_common.state_machine._state_task_work_result import STATE_TASK_WORK_RES
class _StateTaskResult(object, metaclass=Singleton):
"""
State Task Result - Constants
"""
"""State Task Result - Constants."""
SUCCESS = Constant("success")
FAILED = Constant("failed")
@@ -25,15 +23,13 @@ class _StateTaskResult(object, metaclass=Singleton):
def state_task_result_update(
task_result, task_result_reason, task_work_result, task_work_result_reason
):
"""
Update State Task Result given a state task work result
"""
"""Update State Task Result given a state task work result."""
if STATE_TASK_WORK_RESULT.WAIT == task_result:
# Nothing to update
return task_result, task_result_reason
if STATE_TASK_RESULT.SUCCESS == task_result:
if STATE_TASK_WORK_RESULT.FAILED == task_work_result:
return STATE_TASK_RESULT.FAILED, task_work_result_reason
@@ -47,7 +43,6 @@ def state_task_result_update(
return STATE_TASK_RESULT.TIMED_OUT, task_work_result_reason
elif STATE_TASK_RESULT.DEGRADED == task_result:
if STATE_TASK_WORK_RESULT.FAILED == task_work_result:
return STATE_TASK_RESULT.FAILED, task_work_result_reason
@@ -13,9 +13,7 @@ DLOG = debug.debug_get_logger("nfv_common.state_machine.state_task_work")
class StateTaskWork(object):
"""
State Task Work
"""
"""State Task Work."""
def __init__(self, name, task, force_pass=False, timeout_in_secs=1, max_retries=1):
self._name = name
@@ -26,30 +24,26 @@ class StateTaskWork(object):
@property
def name(self):
"""
Returns the name of the task work
"""
"""Returns the name of the task work."""
return self._name
@property
def force_pass(self):
"""
Returns the true if force_pass has been set, otherwise false
"""
"""Returns the true if force_pass has been set, otherwise false."""
return self._force_pass
@property
def max_retries(self):
"""
Returns the maximum retry attempts for task work to be completed
"""
"""Returns the maximum retry attempts for task work to be completed."""
return self._max_retries
@property
def timeout_in_secs(self):
"""
Returns the maximum amount of time to wait for completion
"""
"""Returns the maximum amount of time to wait for completion."""
return self._timeout_in_secs
@property
@@ -58,9 +52,8 @@ class StateTaskWork(object):
return task
def extend_timeout(self, timeout_in_secs):
"""
Allow the task work timeout to be extended
"""
"""Allow the task work timeout to be extended."""
DLOG.verbose(
"Extending state task work timeout for %s to %s."
% (self._name, timeout_in_secs)
@@ -69,16 +62,14 @@ class StateTaskWork(object):
self.task.refresh_timeouts()
def run(self):
"""
State Task Work Run (expected to be overridden by child class)
"""
"""State Task Work Run (expected to be overridden by child class)."""
DLOG.verbose("Default state task work run for %s." % self._name)
return STATE_TASK_WORK_RESULT.SUCCESS, ""
def complete(self, result, reason):
"""
State Task Work Completed (can be overridden by child class)
"""
"""State Task Work Completed (can be overridden by child class)."""
DLOG.verbose(
"Default state task work complete for %s, result=%s, "
"reason=%s." % (self._name, result, reason)
@@ -86,15 +77,13 @@ class StateTaskWork(object):
return result, reason
def abort(self):
"""
State Task Work Abort (can be overridden by child class)
"""
"""State Task Work Abort (can be overridden by child class)."""
DLOG.verbose("Default state task work abort for %s." % self._name)
def timeout(self):
"""
State Task Work Timeout (can be overridden by child class)
"""
"""State Task Work Timeout (can be overridden by child class)."""
DLOG.verbose(
"Default state task work timeout for %s, timeout=%s secs."
% (self._name, self._timeout_in_secs)
@@ -102,8 +91,7 @@ class StateTaskWork(object):
return STATE_TASK_WORK_RESULT.TIMED_OUT, ""
def handle_event(self, event, event_data=None):
"""
State Task Work Handle Event (expected to be overridden by child class)
"""
"""State Task Work Handle Event (expected to be overridden by child class)."""
DLOG.verbose("Default state task work handle event for %s." % self._name)
return False
@@ -9,9 +9,7 @@ from nfv_common.helpers import Singleton
class _StateTaskWorkResult(object, metaclass=Singleton):
"""
State Task Work Result - Constants
"""
"""State Task Work Result - Constants."""
WAIT = Constant("wait")
SUCCESS = Constant("success")
+77 -117
View File
@@ -17,9 +17,7 @@ DLOG = debug.debug_get_logger("nfv_common.strategy")
class Strategy(object):
"""
Strategy
"""
"""Strategy."""
def __init__(
self,
@@ -69,144 +67,124 @@ class Strategy(object):
@property
def uuid(self):
"""
Returns the uuid of the strategy
"""
"""Returns the uuid of the strategy."""
return self._uuid
@property
def name(self):
"""
Returns the name of the strategy
"""
"""Returns the name of the strategy."""
return self._name
@property
def state(self):
"""
Returns the state of the strategy
"""
"""Returns the state of the strategy."""
return self._state
@property
def current_phase(self):
"""
Returns the current phase being executed for the strategy
"""
"""Returns the current phase being executed for the strategy."""
return self._current_phase
@property
def build_phase(self):
"""
Returns the build phase of the strategy
"""
"""Returns the build phase of the strategy."""
return self._phase[STRATEGY_PHASE.BUILD]
@property
def apply_phase(self):
"""
Returns the apply phase of the strategy
"""
"""Returns the apply phase of the strategy."""
return self._phase[STRATEGY_PHASE.APPLY]
@property
def abort_phase(self):
"""
Returns the abort phase of the strategy
"""
"""Returns the abort phase of the strategy."""
return self._phase[STRATEGY_PHASE.ABORT]
def is_building(self):
"""
Returns true if the strategy is building
"""
"""Returns true if the strategy is building."""
return STRATEGY_STATE.BUILDING == self._state
def is_build_failed(self):
"""
Returns true if the strategy build failed
"""
"""Returns true if the strategy build failed."""
return (
STRATEGY_STATE.BUILD_FAILED == self._state or self.build_phase.is_failed()
)
def is_build_timed_out(self):
"""
Returns true if the strategy build timed out
"""
"""Returns true if the strategy build timed out."""
return (
STRATEGY_STATE.BUILD_TIMEOUT == self._state
or self.build_phase.is_timed_out()
)
def is_ready_to_apply(self):
"""
Returns true if the strategy is ready to apply
"""
"""Returns true if the strategy is ready to apply."""
return STRATEGY_STATE.READY_TO_APPLY == self._state
def is_applying(self):
"""
Returns true if the strategy is applying
"""
"""Returns true if the strategy is applying."""
return STRATEGY_STATE.APPLYING == self._state
def is_apply_failed(self):
"""
Returns true if the strategy apply failed
"""
"""Returns true if the strategy apply failed."""
return (
STRATEGY_STATE.APPLY_FAILED == self._state or self.apply_phase.is_failed()
)
def is_apply_timed_out(self):
"""
Returns true if the strategy apply timed out
"""
"""Returns true if the strategy apply timed out."""
return (
STRATEGY_STATE.APPLY_TIMEOUT == self._state
or self.apply_phase.is_timed_out()
)
def is_applied(self):
"""
Returns true if the strategy is applied
"""
"""Returns true if the strategy is applied."""
return STRATEGY_STATE.APPLIED == self._state
def is_aborting(self):
"""
Returns true if the strategy is aborting
"""
"""Returns true if the strategy is aborting."""
return STRATEGY_STATE.ABORTING == self._state
def is_abort_failed(self):
"""
Returns true if the strategy abort failed
"""
"""Returns true if the strategy abort failed."""
return (
STRATEGY_STATE.ABORT_FAILED == self._state or self.abort_phase.is_failed()
)
def is_abort_timed_out(self):
"""
Returns true if the strategy abort timed out
"""
"""Returns true if the strategy abort timed out."""
return (
STRATEGY_STATE.ABORT_TIMEOUT == self._state
or self.abort_phase.is_timed_out()
)
def is_aborted(self):
"""
Returns true if the strategy is aborted
"""
"""Returns true if the strategy is aborted."""
return STRATEGY_STATE.ABORTED == self._state
def _build(self):
"""
Strategy Build
"""
"""Strategy Build."""
if STRATEGY_PHASE.INITIAL == self._current_phase:
if STRATEGY_STATE.INITIAL == self._state:
self._state = STRATEGY_STATE.BUILDING
@@ -214,9 +192,8 @@ class Strategy(object):
self.build_phase.apply()
def _apply(self, stage_id=None):
"""
Strategy Apply
"""
"""Strategy Apply."""
success = True
reason = ""
@@ -305,9 +282,8 @@ class Strategy(object):
return success, reason
def _abort(self, stage_id=None):
"""
Strategy Abort
"""
"""Strategy Abort."""
if STRATEGY_PHASE.APPLY == self._current_phase:
if stage_id is not None:
if (
@@ -348,9 +324,8 @@ class Strategy(object):
return True, ""
def _handle_event(self, event, event_data=None):
"""
Strategy Handle Event
"""
"""Strategy Handle Event."""
handled = False
if STRATEGY_STATE.BUILDING == self._state:
@@ -371,21 +346,18 @@ class Strategy(object):
return handled
def phase_save(self):
"""
Strategy Phase Save
"""
"""Strategy Phase Save."""
self.save()
def phase_extend_timeout(self, phase):
"""
Strategy Phase Extend Timeout
"""
"""Strategy Phase Extend Timeout."""
phase.refresh_timeouts()
def phase_complete(self, phase, phase_result, phase_result_reason=None):
"""
Strategy Phase Complete
"""
"""Strategy Phase Complete."""
self.save()
result, result_reason = strategy_result_update(
@@ -459,73 +431,63 @@ class Strategy(object):
self.save()
def refresh_timeouts(self):
"""
Strategy Refresh Timeouts
"""
"""Strategy Refresh Timeouts."""
self.build_phase.refresh_timeouts()
self.apply_phase.refresh_timeouts()
self.abort_phase.refresh_timeouts()
def save(self):
"""
Strategy Save (can be overridden by child class)
"""
"""Strategy Save (can be overridden by child class)."""
pass
def build(self):
"""
Strategy Build (can be overridden by child class)
"""
"""Strategy Build (can be overridden by child class)."""
self._build()
self.save()
def build_complete(self, result, result_reason):
"""
Strategy Build Complete (can be overridden by child class)
"""
"""Strategy Build Complete (can be overridden by child class)."""
self.save()
return result, result_reason
def apply(self, stage_id):
"""
Strategy Apply (can be overridden by child class)
"""
"""Strategy Apply (can be overridden by child class)."""
success, reason = self._apply(stage_id)
self.save()
return success, reason
def apply_complete(self, result, result_reason):
"""
Strategy Apply Complete (can be overridden by child class)
"""
"""Strategy Apply Complete (can be overridden by child class)."""
self.save()
return result, result_reason
def abort(self, stage_id):
"""
Strategy Abort (can be overridden by child class)
"""
"""Strategy Abort (can be overridden by child class)."""
success, reason = self._abort(stage_id)
self.save()
return success, reason
def abort_complete(self, result, result_reason):
"""
Strategy Abort Complete (can be overridden by child class)
"""
"""Strategy Abort Complete (can be overridden by child class)."""
self.save()
return result, result_reason
def handle_event(self, event, event_data=None):
"""
Strategy Handle Event (can be overridden by child class)
"""
"""Strategy Handle Event (can be overridden by child class)."""
return self._handle_event(event, event_data)
def from_dict(self, data, build_phase=None, apply_phase=None, abort_phase=None):
"""
Initializes a strategy object using the given dictionary
"""
"""Initializes a strategy object using the given dictionary."""
Strategy.__init__(
self,
data["uuid"],
@@ -539,9 +501,8 @@ class Strategy(object):
return self
def as_dict(self):
"""
Represent the strategy as a dictionary
"""
"""Represent the strategy as a dictionary."""
data = dict()
data["uuid"] = self.uuid
data["name"] = self.name
@@ -567,7 +528,6 @@ class Strategy(object):
return data
def as_json(self):
"""
Represent the strategy as json
"""
"""Represent the strategy as json."""
return json.dumps(self.as_dict())
@@ -10,9 +10,7 @@ from nfv_common.helpers import Singleton
class StrategyApplyTypes(Constants, metaclass=Singleton):
"""
Strategy - Apply Type Constants
"""
"""Strategy - Apply Type Constants."""
SERIAL = Constant("serial")
PARALLEL = Constant("parallel")
@@ -20,9 +18,7 @@ class StrategyApplyTypes(Constants, metaclass=Singleton):
class StrategyAlarmRestrictionTypes(Constants, metaclass=Singleton):
"""
Strategy - Alarm Restriction Type Constants
"""
"""Strategy - Alarm Restriction Type Constants."""
STRICT = Constant("strict")
RELAXED = Constant("relaxed")
@@ -30,9 +26,7 @@ class StrategyAlarmRestrictionTypes(Constants, metaclass=Singleton):
class StrategyPhases(Constants, metaclass=Singleton):
"""
Strategy - Phase Constants
"""
"""Strategy - Phase Constants."""
INITIAL = Constant("initial")
BUILD = Constant("build")
@@ -41,9 +35,7 @@ class StrategyPhases(Constants, metaclass=Singleton):
class StrategyStates(Constants, metaclass=Singleton):
"""
Strategy - State Constants
"""
"""Strategy - State Constants."""
INITIAL = Constant("initial")
BUILDING = Constant("building")
@@ -23,9 +23,7 @@ DLOG = debug.debug_get_logger("nfv_common.strategy.phase")
class StrategyPhase(object):
"""
Strategy Phase
"""
"""Strategy Phase."""
def __init__(self, name):
self._name = name
@@ -48,137 +46,118 @@ class StrategyPhase(object):
@property
def name(self):
"""
Returns the name of the strategy phase
"""
"""Returns the name of the strategy phase."""
return self._name
@property
def strategy(self):
"""
Returns the strategy this phase is a member of
"""
"""Returns the strategy this phase is a member of."""
if self._strategy_reference is not None:
return self._strategy_reference()
return None
@strategy.setter
def strategy(self, strategy_value):
"""
Set the strategy that this phase is a member of
"""
"""Set the strategy that this phase is a member of."""
self._strategy_reference = weakref.ref(strategy_value)
@property
def current_stage(self):
"""
Return the current stage
"""
"""Return the current stage."""
return self._current_stage
@property
def stop_at_stage(self):
"""
Return the stage to stop at
"""
"""Return the stage to stop at."""
return self._stop_at_stage
@property
def total_stages(self):
"""
Returns the number of stages
"""
"""Returns the number of stages."""
return len(self._stages)
@property
def stages(self):
"""
Returns the stages for this phase
"""
"""Returns the stages for this phase."""
return self._stages
@property
def timeout_in_secs(self):
"""
Returns the maximum amount of time to wait for completion
"""
"""Returns the maximum amount of time to wait for completion."""
return self._timeout_in_secs
@property
def result(self):
"""
Returns the result of the strategy phase
"""
"""Returns the result of the strategy phase."""
return self._result
@result.setter
def result(self, result):
"""
Updates the result of the strategy phase
"""
"""Updates the result of the strategy phase."""
self._result = result
@property
def result_reason(self):
"""
Returns the reason for the result of the strategy phase
"""
"""Returns the reason for the result of the strategy phase."""
return self._result_reason
@result_reason.setter
def result_reason(self, reason):
"""
Updates the reason for the result of the strategy phase
"""
"""Updates the reason for the result of the strategy phase."""
self._result_reason = reason
@property
def result_response(self):
"""
Returns the response for the result of the strategy phase
"""
"""Returns the response for the result of the strategy phase."""
return self._result_response
@result_response.setter
def result_response(self, response):
"""
Updates the response for the result of the strategy phase
"""
"""Updates the response for the result of the strategy phase."""
self._result_response = response
@property
def start_date_time(self):
"""
Returns the start date-time of the strategy phase
"""
"""Returns the start date-time of the strategy phase."""
return self._start_date_time
@start_date_time.setter
def start_date_time(self, date_time_str):
"""
Updates the start date-time of the strategy phase
"""
"""Updates the start date-time of the strategy phase."""
self._start_date_time = date_time_str
@property
def end_date_time(self):
"""
Returns the end date-time of the strategy phase
"""
"""Returns the end date-time of the strategy phase."""
return self._end_date_time
@end_date_time.setter
def end_date_time(self, date_time_str):
"""
Updates the end date-time of the strategy phase
"""
"""Updates the end date-time of the strategy phase."""
self._end_date_time = date_time_str
@property
def completion_percentage(self):
"""
Returns the percentage completed
"""
"""Returns the percentage completed."""
completed_steps = 0
total_steps = 0
@@ -201,65 +180,56 @@ class StrategyPhase(object):
return int((completed_steps * 100) // total_steps)
def is_inprogress(self):
"""
Returns true if the phase is inprogress
"""
"""Returns true if the phase is inprogress."""
if self._inprogress:
stage = self._stages[self._current_stage]
return stage.is_inprogress()
return False
def is_degraded(self):
"""
Returns true if the phase is degraded
"""
"""Returns true if the phase is degraded."""
return STRATEGY_PHASE_RESULT.DEGRADED == self._result
def is_failed(self):
"""
Returns true if the phase has failed
"""
"""Returns true if the phase has failed."""
return STRATEGY_PHASE_RESULT.FAILED == self._result
def is_timed_out(self):
"""
Returns true if the phase has timed out
"""
"""Returns true if the phase has timed out."""
return STRATEGY_PHASE_RESULT.TIMED_OUT == self._result
def is_aborted(self):
"""
Returns true if the phase is aborted
"""
"""Returns true if the phase is aborted."""
return STRATEGY_PHASE_RESULT.ABORTED == self._result
def is_success(self):
"""
Returns true if the phase completed successfully
"""
"""Returns true if the phase completed successfully."""
return STRATEGY_PHASE_RESULT.SUCCESS == self._result
def add_stage(self, stage):
"""
Add a stage to this strategy phase
"""
"""Add a stage to this strategy phase."""
stage.id = len(self._stages)
stage.phase = self
self._stages.append(stage)
def _save(self):
"""
Phase Save
"""
"""Phase Save."""
if self.strategy is not None:
self.strategy.phase_save() # pylint: disable=no-member
else:
DLOG.info("Strategy reference is invalid for phase (%s)." % self._name)
def _cleanup(self):
"""
Phase Cleanup
"""
"""Phase Cleanup."""
DLOG.info("Phase (%s) cleanup called" % self._name)
if self._timer_id is not None:
@@ -271,9 +241,8 @@ class StrategyPhase(object):
self._stage_timer_id = None
def _abort(self):
"""
Phase Abort
"""
"""Phase Abort."""
abort_list = list()
if STRATEGY_PHASE_RESULT.INITIAL == self._result:
@@ -299,9 +268,8 @@ class StrategyPhase(object):
@coroutine
def _timeout(self):
"""
Phase Timeout
"""
"""Phase Timeout."""
(yield)
DLOG.info(
"Phase (%s) timed out, timeout_in_secs=%s."
@@ -319,9 +287,8 @@ class StrategyPhase(object):
self._complete(self._result, self._result_reason)
def result_complete_response(self, response):
"""
Sets complete result response
"""
"""Sets complete result response."""
# response message is converted to readable display format
response_format = response.copy()
if "error-message" in response_format:
@@ -332,9 +299,8 @@ class StrategyPhase(object):
self._result_response = response_result
def _complete(self, result, reason):
"""
Phase Internal Complete
"""
"""Phase Internal Complete."""
self._inprogress = False
self._cleanup()
self._save()
@@ -342,9 +308,8 @@ class StrategyPhase(object):
return self.complete(result, reason)
def _apply(self):
"""
Phase Apply
"""
"""Phase Apply."""
if not self._inprogress:
if 0 == self._current_stage:
self._cleanup()
@@ -438,9 +403,8 @@ class StrategyPhase(object):
return self._result, self._result_reason
def stage_complete(self, stage_result, stage_result_reason=None):
"""
Strategy Stage Complete
"""
"""Strategy Stage Complete."""
stage = self._stages[self._current_stage]
DLOG.debug(
"Phase (%s) stage (%s) complete, result=%s, reason=%s."
@@ -469,9 +433,8 @@ class StrategyPhase(object):
@coroutine
def _stage_timeout(self):
"""
Strategy Stage Timeout
"""
"""Strategy Stage Timeout."""
(yield)
if len(self._stages) <= self._current_stage:
DLOG.error(
@@ -508,24 +471,21 @@ class StrategyPhase(object):
self.stage_complete(stage_result, stage_result_reason)
def stage_extend_timeout(self):
"""
Strategy Stage Extend Timeout
"""
"""Strategy Stage Extend Timeout."""
if self.strategy is not None:
self.strategy.phase_extend_timeout(self) # pylint: disable=no-member
else:
self.refresh_timeouts()
def stage_save(self):
"""
Strategy Stage Save
"""
"""Strategy Stage Save."""
self._save()
def refresh_timeouts(self):
"""
Phase Refresh Timeouts
"""
"""Phase Refresh Timeouts."""
if not self._inprogress:
# No need to refresh phase timer, phase not started
return
@@ -583,9 +543,8 @@ class StrategyPhase(object):
stage.refresh_timeouts()
def abort(self):
"""
Phase Abort (can be overridden by child class)
"""
"""Phase Abort (can be overridden by child class)."""
abort_list = self._abort()
abort_phase = StrategyPhase(STRATEGY_PHASE.ABORT)
for abort_stage in abort_list:
@@ -593,9 +552,8 @@ class StrategyPhase(object):
return abort_phase
def apply(self, stop_at_stage=None):
"""
Phase Apply (can be overridden by child class)
"""
"""Phase Apply (can be overridden by child class)."""
if stop_at_stage is None:
self._stop_at_stage = len(self._stages)
elif 0 <= stop_at_stage <= len(self._stages):
@@ -603,9 +561,8 @@ class StrategyPhase(object):
return self._apply()
def complete(self, result, reason):
"""
Phase Complete (can be overridden by child class)
"""
"""Phase Complete (can be overridden by child class)."""
DLOG.debug("Strategy Phase (%s) complete." % self._name)
if not self.is_success() and self.result_response:
DLOG.error(
@@ -620,9 +577,8 @@ class StrategyPhase(object):
return self._result, self._result_reason
def handle_event(self, event, event_data=None):
"""
Phase Handle Event (can be overridden by child class)
"""
"""Phase Handle Event (can be overridden by child class)."""
DLOG.debug("Strategy Phase (%s) handle event (%s)." % (self._name, event))
handled = False
@@ -636,9 +592,8 @@ class StrategyPhase(object):
return handled
def from_dict(self, data, stages=None):
"""
Initializes a strategy phase object using the given dictionary
"""
"""Initializes a strategy phase object using the given dictionary."""
StrategyPhase.__init__(self, data["name"])
self._inprogress = data["inprogress"]
self._current_stage = data["current_stage"]
@@ -663,9 +618,8 @@ class StrategyPhase(object):
return self
def as_dict(self):
"""
Represent the strategy phase as a dictionary
"""
"""Represent the strategy phase as a dictionary."""
data = dict()
data["name"] = self.name
data["timeout"] = self._timeout_in_secs
@@ -9,9 +9,7 @@ from nfv_common.helpers import Singleton
class StrategyResult(object, metaclass=Singleton):
"""
Strategy Result - Constants
"""
"""Strategy Result - Constants."""
INITIAL = Constant("initial")
INPROGRESS = Constant("inprogress")
@@ -25,9 +23,7 @@ class StrategyResult(object, metaclass=Singleton):
class StrategyPhaseResult(object, metaclass=Singleton):
"""
Strategy Phase Result - Constants
"""
"""Strategy Phase Result - Constants."""
INITIAL = Constant("initial")
INPROGRESS = Constant("inprogress")
@@ -41,9 +37,7 @@ class StrategyPhaseResult(object, metaclass=Singleton):
class StrategyStageResult(object, metaclass=Singleton):
"""
Strategy Stage Result - Constants
"""
"""Strategy Stage Result - Constants."""
INITIAL = Constant("initial")
INPROGRESS = Constant("inprogress")
@@ -57,9 +51,7 @@ class StrategyStageResult(object, metaclass=Singleton):
class StrategyStepResult(object, metaclass=Singleton):
"""
Strategy Step Result - Constants
"""
"""Strategy Step Result - Constants."""
INITIAL = Constant("initial")
INPROGRESS = Constant("inprogress")
@@ -75,15 +67,13 @@ class StrategyStepResult(object, metaclass=Singleton):
def strategy_result_update(
strategy_result, strategy_result_reason, phase_result, phase_result_reason
):
"""
Update Strategy Stage Result given a strategy phase result
"""
"""Update Strategy Stage Result given a strategy phase result."""
if STRATEGY_RESULT.WAIT == strategy_result:
# Nothing to update
return strategy_result, strategy_result_reason
if STRATEGY_RESULT.INITIAL == strategy_result:
if STRATEGY_PHASE_RESULT.ABORTING == phase_result:
return STRATEGY_RESULT.ABORTING, phase_result_reason
@@ -106,7 +96,6 @@ def strategy_result_update(
return STRATEGY_RESULT.INPROGRESS, phase_result_reason
elif STRATEGY_RESULT.INPROGRESS == strategy_result:
if STRATEGY_PHASE_RESULT.ABORTING == phase_result:
return STRATEGY_RESULT.ABORTING, phase_result_reason
@@ -126,7 +115,6 @@ def strategy_result_update(
return STRATEGY_RESULT.SUCCESS, phase_result_reason
elif STRATEGY_RESULT.SUCCESS == strategy_result:
if STRATEGY_PHASE_RESULT.ABORTING == phase_result:
return STRATEGY_RESULT.ABORTING, phase_result_reason
@@ -143,7 +131,6 @@ def strategy_result_update(
return STRATEGY_RESULT.DEGRADED, phase_result_reason
elif STRATEGY_STAGE_RESULT.DEGRADED == strategy_result:
if STRATEGY_PHASE_RESULT.ABORTING == phase_result:
return STRATEGY_RESULT.ABORTING, phase_result_reason
@@ -162,15 +149,13 @@ def strategy_result_update(
def strategy_phase_result_update(
phase_result, phase_result_reason, stage_result, stage_result_reason
):
"""
Update Strategy Phase Result given a strategy stage result
"""
"""Update Strategy Phase Result given a strategy stage result."""
if STRATEGY_PHASE_RESULT.WAIT == phase_result:
# Nothing to update
return phase_result, phase_result_reason
if STRATEGY_PHASE_RESULT.INITIAL == phase_result:
if STRATEGY_STAGE_RESULT.ABORTING == stage_result:
return STRATEGY_PHASE_RESULT.ABORTING, stage_result_reason
@@ -193,7 +178,6 @@ def strategy_phase_result_update(
return STRATEGY_PHASE_RESULT.INPROGRESS, stage_result_reason
elif STRATEGY_PHASE_RESULT.INPROGRESS == phase_result:
if STRATEGY_STAGE_RESULT.ABORTING == stage_result:
return STRATEGY_PHASE_RESULT.ABORTING, stage_result_reason
@@ -213,7 +197,6 @@ def strategy_phase_result_update(
return STRATEGY_PHASE_RESULT.SUCCESS, stage_result_reason
elif STRATEGY_PHASE_RESULT.SUCCESS == phase_result:
if STRATEGY_STAGE_RESULT.ABORTING == stage_result:
return STRATEGY_PHASE_RESULT.ABORTING, stage_result_reason
@@ -230,7 +213,6 @@ def strategy_phase_result_update(
return STRATEGY_PHASE_RESULT.DEGRADED, stage_result_reason
elif STRATEGY_PHASE_RESULT.DEGRADED == phase_result:
if STRATEGY_STAGE_RESULT.ABORTING == stage_result:
return STRATEGY_PHASE_RESULT.ABORTING, stage_result_reason
@@ -249,15 +231,13 @@ def strategy_phase_result_update(
def strategy_stage_result_update(
stage_result, stage_result_reason, step_result, step_result_reason
):
"""
Update Strategy Stage Result given a strategy step result
"""
"""Update Strategy Stage Result given a strategy step result."""
if STRATEGY_STAGE_RESULT.WAIT == stage_result:
# Nothing to update
return stage_result, stage_result_reason
if STRATEGY_STAGE_RESULT.INITIAL == stage_result:
if STRATEGY_STEP_RESULT.ABORTING == step_result:
return STRATEGY_STAGE_RESULT.ABORTING, step_result_reason
@@ -280,7 +260,6 @@ def strategy_stage_result_update(
return STRATEGY_STAGE_RESULT.INPROGRESS, step_result_reason
elif STRATEGY_STAGE_RESULT.INPROGRESS == stage_result:
if STRATEGY_STEP_RESULT.ABORTING == step_result:
return STRATEGY_STAGE_RESULT.ABORTING, step_result_reason
@@ -300,7 +279,6 @@ def strategy_stage_result_update(
return STRATEGY_STAGE_RESULT.SUCCESS, step_result_reason
elif STRATEGY_STAGE_RESULT.SUCCESS == stage_result:
if STRATEGY_STEP_RESULT.ABORTING == step_result:
return STRATEGY_STAGE_RESULT.ABORTING, step_result_reason
@@ -317,7 +295,6 @@ def strategy_stage_result_update(
return STRATEGY_STAGE_RESULT.DEGRADED, step_result_reason
elif STRATEGY_STAGE_RESULT.DEGRADED == stage_result:
if STRATEGY_STEP_RESULT.ABORTING == step_result:
return STRATEGY_STAGE_RESULT.ABORTING, step_result_reason
@@ -19,9 +19,7 @@ DLOG = debug.debug_get_logger("nfv_common.strategy.stage")
class StrategyStage(object):
"""
Strategy Stage
"""
"""Strategy Stage."""
def __init__(self, name):
self._id = 0
@@ -43,155 +41,133 @@ class StrategyStage(object):
@property
def name(self):
"""
Returns the name of the stage
"""
"""Returns the name of the stage."""
return self._name
@property
def id(self):
"""
Returns the id of the stage
"""
"""Returns the id of the stage."""
return self._id
@id.setter
def id(self, value):
"""
Sets the id of the step
"""
"""Sets the id of the step."""
self._id = value
@property
def current_step(self):
"""
Returns the current step of the stage
"""
"""Returns the current step of the stage."""
return self._current_step
@property
def timeout_in_secs(self):
"""
Returns the maximum amount of time to wait for completion
"""
"""Returns the maximum amount of time to wait for completion."""
return self._timeout_in_secs
@property
def result(self):
"""
Returns the result of the stage
"""
"""Returns the result of the stage."""
return self._result
@result.setter
def result(self, result):
"""
Updates the result of the stage
"""
"""Updates the result of the stage."""
self._result = result
@property
def result_reason(self):
"""
Returns the reason for the result of the stage
"""
"""Returns the reason for the result of the stage."""
return self._result_reason
@result_reason.setter
def result_reason(self, reason):
"""
Updates the reason for the result of the stage
"""
"""Updates the reason for the result of the stage."""
self._result_reason = reason
@property
def start_date_time(self):
"""
Returns the start date-time of the stage
"""
"""Returns the start date-time of the stage."""
return self._start_date_time
@start_date_time.setter
def start_date_time(self, date_time_str):
"""
Updates the start date-time of the stage
"""
"""Updates the start date-time of the stage."""
self._start_date_time = date_time_str
@property
def end_date_time(self):
"""
Returns the end date-time of the stage
"""
"""Returns the end date-time of the stage."""
return self._end_date_time
@end_date_time.setter
def end_date_time(self, date_time_str):
"""
Updates the end date-time of the stage
"""
"""Updates the end date-time of the stage."""
self._end_date_time = date_time_str
@property
def strategy(self):
"""
Returns the strategy this stage is a member of
"""
"""Returns the strategy this stage is a member of."""
if self.phase is not None:
return self.phase.strategy
return None
@property
def phase(self):
"""
Returns the phase this stage is a member of
"""
"""Returns the phase this stage is a member of."""
if self._phase_reference is not None:
return self._phase_reference()
return None
@phase.setter
def phase(self, phase_value):
"""
Set the phase that this stage is a member of
"""
"""Set the phase that this stage is a member of."""
self._phase_reference = weakref.ref(phase_value)
@property
def steps(self):
"""
Returns the steps for this stage
"""
"""Returns the steps for this stage."""
return self._steps
def is_inprogress(self):
"""
Returns if the stage is inprogress or not
"""
"""Returns if the stage is inprogress or not."""
return self._inprogress
def is_failed(self):
"""
Return true if this stage has failed
"""
"""Return true if this stage has failed."""
return STRATEGY_STAGE_RESULT.FAILED == self._result
def timed_out(self):
"""
Return true if this stage has timed out
"""
"""Return true if this stage has timed out."""
return STRATEGY_STAGE_RESULT.TIMED_OUT == self._result
def aborted(self):
"""
Return true if this stage was aborted
"""
"""Return true if this stage was aborted."""
return STRATEGY_STAGE_RESULT.ABORTED == self._result
def add_step(self, step):
"""
Add a step to this stage
"""
"""Add a step to this stage."""
step.id = len(self._steps)
step.stage = self
self._steps.append(step)
@@ -204,9 +180,8 @@ class StrategyStage(object):
self._timeout_in_secs += 1
def _save(self):
"""
Stage Save
"""
"""Stage Save."""
import inspect
import os
@@ -238,9 +213,8 @@ class StrategyStage(object):
)
def _cleanup(self):
"""
Stage Cleanup
"""
"""Stage Cleanup."""
DLOG.info("Stage (%s) cleanup called" % self._name)
if self._timer_id is not None:
@@ -252,9 +226,8 @@ class StrategyStage(object):
self._step_timer_id = None
def _abort(self):
"""
Stage Abort
"""
"""Stage Abort."""
abort_list = list()
if STRATEGY_STAGE_RESULT.INITIAL == self._result:
@@ -280,9 +253,8 @@ class StrategyStage(object):
@coroutine
def _timeout(self):
"""
Stage Timeout
"""
"""Stage Timeout."""
(yield)
DLOG.info(
"Stage (%s) timed out, timeout_in_secs=%s."
@@ -300,9 +272,8 @@ class StrategyStage(object):
self._complete(self._result, self._result_reason)
def _complete(self, result, reason):
"""
Stage Internal Complete
"""
"""Stage Internal Complete."""
self._inprogress = False
self._cleanup()
self._save()
@@ -310,9 +281,8 @@ class StrategyStage(object):
return self.complete(result, reason)
def _apply(self):
"""
Stage Apply
"""
"""Stage Apply."""
if not self._inprogress:
if 0 == self._current_step:
self._cleanup()
@@ -393,9 +363,8 @@ class StrategyStage(object):
return self._complete(self._result, self._result_reason)
def step_complete(self, step_result, step_result_reason=None):
"""
Stage Step Complete
"""
"""Stage Step Complete."""
step = self._steps[self._current_step]
DLOG.debug(
@@ -436,9 +405,8 @@ class StrategyStage(object):
@coroutine
def _step_timeout(self):
"""
Stage Step Timeout
"""
"""Stage Step Timeout."""
(yield)
if len(self._steps) <= self._current_step:
DLOG.error(
@@ -477,18 +445,16 @@ class StrategyStage(object):
self.step_complete(step.result, step.result_reason)
def step_extend_timeout(self):
"""
Stage Step Extend Timeout
"""
"""Stage Step Extend Timeout."""
if self.phase is not None:
self.phase.stage_extend_timeout()
else:
self.refresh_timeouts()
def refresh_timeouts(self):
"""
Stage Refresh Timeouts
"""
"""Stage Refresh Timeouts."""
if not self.is_inprogress():
# No need to refresh stage timer, stage not started
return
@@ -538,9 +504,8 @@ class StrategyStage(object):
)
def abort(self):
"""
Stage Abort (can be overridden by child class)
"""
"""Stage Abort (can be overridden by child class)."""
abort_list = self._abort()
if abort_list:
abort_stage = StrategyStage(self.name)
@@ -550,15 +515,13 @@ class StrategyStage(object):
return []
def apply(self):
"""
Stage Apply (can be overridden by child class)
"""
"""Stage Apply (can be overridden by child class)."""
return self._apply()
def complete(self, result, result_reason):
"""
Stage Complete (can be overridden by child class)
"""
"""Stage Complete (can be overridden by child class)."""
DLOG.debug("Strategy Stage (%s) complete." % self._name)
if self.phase is not None:
self.phase.stage_complete(result, result_reason)
@@ -569,9 +532,8 @@ class StrategyStage(object):
return self._result, self._result_reason
def handle_event(self, event, event_data=None):
"""
Stage Handle Event (can be overridden by child class)
"""
"""Stage Handle Event (can be overridden by child class)."""
DLOG.debug("Stage (%s) handle event (%s)." % (self._name, event))
handled = False
@@ -582,9 +544,8 @@ class StrategyStage(object):
return handled
def from_dict(self, data, steps=None):
"""
Initializes a strategy stage object using the given dictionary
"""
"""Initializes a strategy stage object using the given dictionary."""
StrategyStage.__init__(self, data["name"])
self._inprogress = data["inprogress"]
self._current_step = data["current_step"]
@@ -617,9 +578,8 @@ class StrategyStage(object):
return self
def as_dict(self):
"""
Represent the strategy stage as a dictionary
"""
"""Represent the strategy stage as a dictionary."""
data = dict()
data["id"] = self._id
data["name"] = self._name
@@ -13,9 +13,7 @@ DLOG = debug.debug_get_logger("nfv_common.strategy.step")
class StrategyStep(object):
"""
Strategy Step
"""
"""Strategy Step."""
def __init__(self, name, force_pass=False, timeout_in_secs=0, max_retries=1):
self._id = 0
@@ -31,140 +29,121 @@ class StrategyStep(object):
@property
def name(self):
"""
Returns the name of the step
"""
"""Returns the name of the step."""
return self._name
@property
def id(self):
"""
Returns the id of the step
"""
"""Returns the id of the step."""
return self._id
@id.setter
def id(self, value):
"""
Sets the id of the step
"""
"""Sets the id of the step."""
self._id = value
@property
def force_pass(self):
"""
Returns the true if force_pass has been set, otherwise false
"""
"""Returns the true if force_pass has been set, otherwise false."""
return self._force_pass
@property
def max_retries(self):
"""
Returns the maximum retry attempts for step to be completed
"""
"""Returns the maximum retry attempts for step to be completed."""
return self._max_retries
@property
def timeout_in_secs(self):
"""
Returns the maximum amount of time to wait for completion
"""
"""Returns the maximum amount of time to wait for completion."""
return self._timeout_in_secs
@property
def result(self):
"""
Returns the result of the step
"""
"""Returns the result of the step."""
return self._result
@result.setter
def result(self, result):
"""
Updates the result of the step
"""
"""Updates the result of the step."""
self._result = result
@property
def result_reason(self):
"""
Returns the reason for the result of the step
"""
"""Returns the reason for the result of the step."""
return self._result_reason
@result_reason.setter
def result_reason(self, reason):
"""
Updates the reason for the result of the step
"""
"""Updates the reason for the result of the step."""
self._result_reason = reason
@property
def start_date_time(self):
"""
Returns the start date-time of the step
"""
"""Returns the start date-time of the step."""
return self._start_date_time
@start_date_time.setter
def start_date_time(self, date_time_str):
"""
Updates the start date-time of the step
"""
"""Updates the start date-time of the step."""
self._start_date_time = date_time_str
@property
def end_date_time(self):
"""
Returns the end date-time of the step
"""
"""Returns the end date-time of the step."""
return self._end_date_time
@end_date_time.setter
def end_date_time(self, date_time_str):
"""
Updates the end date-time of the step
"""
"""Updates the end date-time of the step."""
self._end_date_time = date_time_str
@property
def strategy(self):
"""
Returns the strategy this step is a member of
"""
"""Returns the strategy this step is a member of."""
if self.phase is not None:
return self.phase.strategy
return None
@property
def phase(self):
"""
Returns the phase this step is a member of
"""
"""Returns the phase this step is a member of."""
if self.stage is not None:
return self.stage.phase
return None
@property
def stage(self):
"""
Returns the stage this step is a member of
"""
"""Returns the stage this step is a member of."""
if self._stage_reference is not None:
return self._stage_reference()
return None
@stage.setter
def stage(self, stage_value):
"""
Set the stage that this step is a member of
"""
"""Set the stage that this step is a member of."""
self._stage_reference = weakref.ref(stage_value)
def extend_timeout(self, timeout_in_secs):
"""
Allow the step timeout to be extended
"""
"""Allow the step timeout to be extended."""
DLOG.verbose(
"Extending strategy step timeout for %s to %s."
% (self._name, timeout_in_secs)
@@ -174,23 +153,20 @@ class StrategyStep(object):
self.stage.step_extend_timeout()
def abort(self):
"""
Strategy Step Abort (can be overridden by child class)
"""
"""Strategy Step Abort (can be overridden by child class)."""
DLOG.info("Default strategy step abort for %s." % self._name)
return []
def apply(self):
"""
Strategy Step Apply (expected to be overridden by child class)
"""
"""Strategy Step Apply (expected to be overridden by child class)."""
DLOG.verbose("Default strategy step apply for %s." % self._name)
return STRATEGY_STEP_RESULT.SUCCESS, ""
def complete(self, result, result_reason):
"""
Strategy Step Completed (can be overridden by child class)
"""
"""Strategy Step Completed (can be overridden by child class)."""
DLOG.verbose(
"Default strategy step complete for %s, result=%s, "
"reason=%s." % (self._name, result, result_reason)
@@ -198,9 +174,8 @@ class StrategyStep(object):
return result, result_reason
def timeout(self):
"""
Strategy Step Timeout (can be overridden by child class)
"""
"""Strategy Step Timeout (can be overridden by child class)."""
DLOG.verbose(
"Default strategy step timeout for %s, timeout=%s secs."
% (self._name, self._timeout_in_secs)
@@ -209,16 +184,14 @@ class StrategyStep(object):
return STRATEGY_STEP_RESULT.TIMED_OUT, timedout_step
def handle_event(self, event, event_data=None):
"""
Strategy Step Handle Event (expected to be overridden by child class)
"""
"""Strategy Step Handle Event (expected to be overridden by child class)."""
DLOG.verbose("Default strategy step handle event for %s." % self._name)
return False
def from_dict(self, data):
"""
Returns a strategy step object initialized using the given dictionary
"""
"""Returns a strategy step object initialized using the given dictionary."""
StrategyStep.__init__(
self, data["name"], data["force_pass"], data["timeout"], data["max_retries"]
)
@@ -229,9 +202,8 @@ class StrategyStep(object):
return self
def as_dict(self):
"""
Represent the strategy step as a dictionary
"""
"""Represent the strategy step as a dictionary."""
data = dict()
data["id"] = self._id
data["name"] = self._name
+38 -54
View File
@@ -16,9 +16,7 @@ DLOG = debug.debug_get_logger("nfv_common.tasks.task")
class TaskPriority(Constants, metaclass=Singleton):
"""
Task Priority Constants
"""
"""Task Priority Constants."""
HIGH = 0
MED = 1
@@ -30,9 +28,7 @@ TASK_PRIORITY = TaskPriority()
class Task(object):
"""
Task
"""
"""Task."""
_READY = Constant("Ready")
_RUNNING = Constant("Running")
@@ -44,9 +40,9 @@ class Task(object):
_id = 1
def __init__(self, scheduler, priority, target):
"""
Create a task, scheduler is used to schedule this task and the task
work, the target is a co-routine that is sent task results
"""Create a task, scheduler is used to schedule this task and the task
work, the target is a co-routine that is sent task results.
"""
self._id = Task._id
self._priority = priority
@@ -60,42 +56,37 @@ class Task(object):
@property
def id(self):
"""
Returns the unique identifier of the task
"""
"""Returns the unique identifier of the task."""
return self._id
@property
def name(self):
"""
Returns the name of the task
"""
"""Returns the name of the task."""
return self._name
@property
def priority(self):
"""
Returns the priority of the task
"""
"""Returns the priority of the task."""
return self._priority
def add_timer(self, name, interval_secs):
"""
Add a timer that will fire in so many milliseconds
"""
"""Add a timer that will fire in so many milliseconds."""
timer_id = self._scheduler.add_task_timer(name, interval_secs, self)
return timer_id
def cancel_timer(self, timer_id):
"""
Cancel timer
"""
"""Cancel timer."""
self._scheduler.cancel_task_timer(timer_id, self)
def timer_fired(self, timer_id):
"""
Handle timer firing; the timer identifier is sent to
the task's co-routine target
"""Handle timer firing; the timer identifier is sent to
the task's co-routine target.
"""
task_result = TaskResult(
complete=True,
@@ -107,34 +98,30 @@ class Task(object):
self._scheduler.schedule_task(self)
def add_io_read_wait(self, select_obj):
"""
Add a read selection object to wait on
"""
"""Add a read selection object to wait on."""
self._scheduler.add_task_io_read_wait(select_obj, self)
def cancel_io_read_wait(self, select_obj):
"""
Cancel a read selection object being waited on
"""
"""Cancel a read selection object being waited on."""
self._scheduler.cancel_task_io_read_wait(select_obj, self)
def add_io_write_wait(self, select_obj):
"""
Add a write selection object to wait on
"""
"""Add a write selection object to wait on."""
self._scheduler.add_task_io_write_wait(select_obj, self)
def cancel_io_write_wait(self, select_obj):
"""
Cancel a write selection object being waited on
"""
"""Cancel a write selection object being waited on."""
self._scheduler.cancel_task_io_write_wait(select_obj, self)
def io_wait_complete(self, select_obj):
"""
Called when a selection object being waited on has become
"""Called when a selection object being waited on has become
readable or writeable; the selection object is sent to
the tasks co-routine target
the tasks co-routine target.
"""
task_result = TaskResult(
complete=True,
@@ -146,17 +133,16 @@ class Task(object):
self._scheduler.schedule_task(self)
def add_task_work(self, task_work):
"""
Add work to be done by the task
"""
"""Add work to be done by the task."""
task_work.task_id = self._id
self._work_list[task_work.id] = [Task._READY, task_work]
return task_work
def task_work_complete(self, task_work):
"""
Task work has been completed, send result to the task target
(results are sent in order the task work was scheduled)
"""Task work has been completed, send result to the task target
(results are sent in order the task work was scheduled).
"""
DLOG.verbose("TaskWork complete, name=%s." % task_work.name)
@@ -204,9 +190,8 @@ class Task(object):
self._target.send(task_result)
def task_work_timeout(self, task_work):
"""
Work being done by the task has timed out
"""
"""Work being done by the task has timed out."""
DLOG.error(
"Task(%s) work (%s) timed out, id=%s."
% (self._name, task_work.name, self._id)
@@ -219,9 +204,8 @@ class Task(object):
self._scheduler.schedule_task(self)
def run(self):
"""
Run the task
"""
"""Run the task."""
DLOG.debug("Task(%s) run, id=%s." % (self._name, self._id))
if not self._started:
self._target.send(None)
+25 -36
View File
@@ -13,29 +13,25 @@ TASK_TIMEOUT = 20
class TaskFuture(object):
"""
Task Future
"""
"""Task Future."""
def __init__(self, scheduler):
"""
Create a task future
"""
"""Create a task future."""
self._scheduler = scheduler
self._result = None
self._timeouts = None
def set_timeouts(self, timeouts):
"""
Set the timeout values to be used when work is to be done
Parameter timeouts is a dictionary of target and the timeout in seconds
"""Set the timeout values to be used when work is to be done
Parameter timeouts is a dictionary of target and the timeout in seconds.
"""
self._timeouts = timeouts
def work(self, target, *args, **kwargs):
"""
Schedule work in the future
"""
"""Schedule work in the future."""
timeout_in_secs = None
if self._timeouts is not None:
# Look for a target specific timeout
@@ -87,10 +83,10 @@ class TaskFuture(object):
raise LookupError("Running task no longer running")
def timer(self, name, interval_secs):
"""
Schedule a timer to be fired after so many milliseconds,
"""Schedule a timer to be fired after so many milliseconds,
callback is a co-routine that is sent the timer identifier
that has fired
that has fired.
"""
if self._scheduler.running_task is not None:
timer_id = self._scheduler.running_task.add_timer(name, interval_secs)
@@ -99,45 +95,40 @@ class TaskFuture(object):
raise LookupError("Running task no longer running")
def cancel_timer(self, timer_id):
"""
Cancel a scheduled timer
"""
"""Cancel a scheduled timer."""
if self._scheduler.running_task is not None:
self._scheduler.running_task.cancel_timer(timer_id)
else:
raise LookupError("Running task no longer running")
def io_read_wait(self, select_obj):
"""
Wait on a read selection object
"""
"""Wait on a read selection object."""
if self._scheduler.running_task is not None:
self._scheduler.running_task.add_io_read_wait(select_obj)
else:
raise LookupError("Running task no longer running")
def io_read_wait_cancel(self, select_obj):
"""
Cancel a wait on a read selection object
"""
"""Cancel a wait on a read selection object."""
if self._scheduler.running_task is not None:
self._scheduler.running_task.cancel_io_read_wait(select_obj)
else:
raise LookupError("Running task no longer running")
def io_write_wait(self, select_obj):
"""
Wait on a write selection object
"""
"""Wait on a write selection object."""
if self._scheduler.running_task is not None:
self._scheduler.running_task.add_io_write_wait(select_obj)
else:
raise LookupError("Running task no longer running")
def io_write_wait_cancel(self, select_obj):
"""
Cancel a wait on a write selection object
"""
"""Cancel a wait on a write selection object."""
if self._scheduler.running_task is not None:
self._scheduler.running_task.cancel_io_write_wait(select_obj)
else:
@@ -145,14 +136,12 @@ class TaskFuture(object):
@property
def result(self):
"""
Returns the result of a future
"""
"""Returns the result of a future."""
return self._result
@result.setter
def result(self, result):
"""
Set the result of a future
"""
"""Set the result of a future."""
self._result = result
+15 -24
View File
@@ -9,9 +9,7 @@ DLOG = debug.debug_get_logger("nfv_common.tasks.task_result")
class TaskResult(object):
"""
Task Result
"""
"""Task Result."""
def __init__(
self,
@@ -22,9 +20,8 @@ class TaskResult(object):
result_data=None,
ancillary_result_data=None,
):
"""
Create a task result
"""
"""Create a task result."""
self._complete = complete
self._aborted = aborted
self._timer_result = timer_result
@@ -34,38 +31,32 @@ class TaskResult(object):
@property
def ancillary_data(self):
"""
Returns the ancillary result data
"""
"""Returns the ancillary result data."""
return self._ancillary_result_data
@property
def data(self):
"""
Returns the result data
"""
"""Returns the result data."""
return self._result_data
def is_complete(self):
"""
Indicates if the task result has been completed
"""
"""Indicates if the task result has been completed."""
return self._complete
def is_aborted(self):
"""
Indicates if the task result has been aborted
"""
"""Indicates if the task result has been aborted."""
return self._aborted
def is_timer(self):
"""
Indicates if the task result data is a timer identifier
"""
"""Indicates if the task result data is a timer identifier."""
return self._timer_result
def is_selobj(self):
"""
Indicates if the task result data is a selection object
"""
"""Indicates if the task result data is a selection object."""
return self._selobj_result
@@ -21,14 +21,11 @@ DLOG = debug.debug_get_logger("nfv_common.tasks.task_scheduler")
class TaskScheduler(object):
"""
Task Scheduler
"""
"""Task Scheduler."""
def __init__(self, name, task_worker_pool):
"""
Create a task scheduler
"""
"""Create a task scheduler."""
self._name = name
self._task_worker_pool = task_worker_pool
self._workers_selobj = dict()
@@ -51,22 +48,19 @@ class TaskScheduler(object):
@property
def name(self):
"""
Returns the name of the scheduler
"""
"""Returns the name of the scheduler."""
return self._name
@property
def running_task(self):
"""
Returns the running task
"""
"""Returns the running task."""
return self._running_task
def add_task(self, priority, target, *args, **kwargs):
"""
Add a task to the task scheduler
"""
"""Add a task to the task scheduler."""
if inspect.isgeneratorfunction(target):
future = TaskFuture(self)
task = Task(self, priority, target(future, *args, **kwargs))
@@ -80,9 +74,8 @@ class TaskScheduler(object):
return result
def delete_task(self, task):
"""
Delete a task from the task scheduler
"""
"""Delete a task from the task scheduler."""
DLOG.debug(
"Pool %s: Delete Task, name=%s." % (self._task_worker_pool.name, task.name)
)
@@ -109,9 +102,8 @@ class TaskScheduler(object):
del self._tasks[task.id]
def add_task_timer(self, name, interval_secs, task):
"""
Add timer for a task
"""
"""Add timer for a task."""
timer_id = timers.timers_create_timer(
name, interval_secs, interval_secs, self.task_timer_timeout
)
@@ -127,9 +119,8 @@ class TaskScheduler(object):
@coroutine
def task_timer_timeout(self):
"""
Called when a task timer has fired
"""
"""Called when a task timer has fired."""
while True:
timer_id = yield
task = self._task_timers.get(timer_id, None)
@@ -142,16 +133,14 @@ class TaskScheduler(object):
break
def add_task_io_read_wait(self, select_obj, task):
"""
Add a task read selection object to wait on
"""
"""Add a task read selection object to wait on."""
selobj.selobj_add_read_obj(select_obj, self.task_io_wait_complete)
self._task_read_selobjs[select_obj] = task
def cancel_task_io_read_wait(self, select_obj, task):
"""
Cancel a task read selection object being waited on
"""
"""Cancel a task read selection object being waited on."""
select_obj_owner = self._task_read_selobjs.get(select_obj, None)
if select_obj_owner is not None:
if select_obj_owner.id == task.id:
@@ -159,16 +148,14 @@ class TaskScheduler(object):
del self._task_read_selobjs[select_obj]
def add_io_write_wait(self, select_obj, task):
"""
Add a task write selection object to wait on
"""
"""Add a task write selection object to wait on."""
selobj.selobj_add_write_obj(select_obj, self.task_io_wait_complete)
self._task_write_selobjs[select_obj] = task
def cancel_io_write_wait(self, select_obj, task):
"""
Cancel a task write selection object being waited on
"""
"""Cancel a task write selection object being waited on."""
select_obj_owner = self._task_write_selobjs.get(select_obj, None)
if select_obj_owner is not None:
if select_obj_owner.id == task.id:
@@ -177,9 +164,9 @@ class TaskScheduler(object):
@coroutine
def task_io_wait_complete(self):
"""
Called when a task selection object being waited on has become
readable or writeable
"""Called when a task selection object being waited on has become
readable or writeable.
"""
while True:
select_obj = yield
@@ -195,9 +182,8 @@ class TaskScheduler(object):
break
def _schedule_next_task(self):
"""
Schedule next task
"""
"""Schedule next task."""
task_id = None
if self._ready_dequeues[TASK_PRIORITY.HIGH] >= 60:
@@ -236,9 +222,8 @@ class TaskScheduler(object):
self._tasks_scheduled = True
def _schedule_task(self, task, reschedule=False):
"""
Schedule or Reschedule a task
"""
"""Schedule or Reschedule a task."""
DLOG.verbose(
"Pool %s: Scheduling Task, name=%s."
% (self._task_worker_pool.name, task.name)
@@ -264,21 +249,18 @@ class TaskScheduler(object):
self._schedule_next_task()
def reschedule_task(self, task):
"""
Reschedule a task
"""
"""Reschedule a task."""
self._schedule_task(task, reschedule=True)
def schedule_task(self, task):
"""
Schedule a task
"""
"""Schedule a task."""
self._schedule_task(task)
def schedule_task_work(self, task_work=None):
"""
Schedule task work to one of the task workers if available
"""
"""Schedule task work to one of the task workers if available."""
if task_work is not None:
self._wait_queue.appendleft(task_work)
@@ -317,9 +299,8 @@ class TaskScheduler(object):
@coroutine
def task_work_complete(self):
"""
A task worker has completed it's assigned work
"""
"""A task worker has completed it's assigned work."""
while True:
select_obj = yield
worker = self._workers_selobj.get(select_obj, None)
@@ -349,9 +330,8 @@ class TaskScheduler(object):
@coroutine
def task_work_timeout(self):
"""
Work being done by the task has timed out
"""
"""Work being done by the task has timed out."""
timer_id = yield
worker = self._workers_timer.get(timer_id, None)
if worker is not None:
@@ -375,9 +355,8 @@ class TaskScheduler(object):
@coroutine
def run_tasks(self):
"""
Run tasks that are ready to run
"""
"""Run tasks that are ready to run."""
while True:
select_obj = yield
if select_obj == self._run_queue.selobj:
+22 -33
View File
@@ -13,16 +13,13 @@ DLOG = debug.debug_get_logger("nfv_common.tasks.task_work")
class TaskWork(object):
"""
Task Work
"""
"""Task Work."""
_id = 0
def __init__(self, timeout, target, *args, **kwargs):
"""
Create task work
"""
"""Create task work."""
self._id = TaskWork._id
self._name = target.__name__
self._task_id = None
@@ -42,65 +39,57 @@ class TaskWork(object):
@property
def name(self):
"""
Returns the name of the task work
"""
"""Returns the name of the task work."""
return self._name
@property
def id(self):
"""
Returns the unique identifier of the task work
"""
"""Returns the unique identifier of the task work."""
return self._id
@property
def task_id(self):
"""
Returns the task identifier that owns this task work
"""
"""Returns the task identifier that owns this task work."""
return self._task_id
@task_id.setter
def task_id(self, task_id):
"""
Set the task identifier that owns this task work
"""
"""Set the task identifier that owns this task work."""
self._task_id = task_id
@property
def create_timestamp_ms(self):
"""
Returns the creation timestamp in milliseconds
"""
"""Returns the creation timestamp in milliseconds."""
return self._create_timestamp_ms
@property
def timeout_in_secs(self):
"""
Returns the maximum timeout in seconds that the task work should
take to run
"""Returns the maximum timeout in seconds that the task work should
take to run.
"""
return self._timeout_in_secs
@property
def ancillary_result_data(self):
"""
Returns the ancillary result data for the task work
"""
"""Returns the ancillary result data for the task work."""
return self._ancillary_result_data
@property
def result(self):
"""
Returns the result of the task work
"""
"""Returns the result of the task work."""
return self._result
def run(self):
"""
Runs the task work
"""
"""Runs the task work."""
DLOG.debug("TaskWork run, id=%s, name=%s." % (self._id, self._name))
try:
result = self._target(*self._args, **self._kwargs)
+18 -30
View File
@@ -12,29 +12,24 @@ DLOG = debug.debug_get_logger("nfv_common.tasks.task_worker")
class TaskWorker(thread.ThreadWorker):
"""
Task Worker
"""
"""Task Worker."""
def __init__(self, name):
super(TaskWorker, self).__init__(name)
def initialize(self):
"""
Initialize the Task Worker
"""
"""Initialize the Task Worker."""
return
def finalize(self):
"""
Finalize the Task Worker
"""
"""Finalize the Task Worker."""
return
def do_work(self, action, work):
"""
Do work given to the Task Worker
"""
"""Do work given to the Task Worker."""
if TaskWorkerThread.ACTION_DO_WORK == action:
if work is not None:
work.run()
@@ -42,17 +37,14 @@ class TaskWorker(thread.ThreadWorker):
class TaskWorkerThread(thread.Thread):
"""
Task Worker Thread
"""
"""Task Worker Thread."""
ACTION_DO_WORK = "thread-do-work"
_id = 1
def __init__(self, name):
"""
Create a task worker
"""
"""Create a task worker."""
self._id = TaskWorkerThread._id
self._name = name
self._worker = TaskWorker(self._name)
@@ -61,28 +53,24 @@ class TaskWorkerThread(thread.Thread):
@property
def id(self):
"""
Returns a unique identifier for this task worker
"""
"""Returns a unique identifier for this task worker."""
return self._id
@property
def name(self):
"""
Returns the name for this task worker
"""
"""Returns the name for this task worker."""
return self._name
def submit_task_work(self, task_work):
"""
Submit task work for this task worker to execute
"""
"""Submit task work for this task worker to execute."""
self.send_work(TaskWorkerThread.ACTION_DO_WORK, task_work)
def get_task_work_result(self):
"""
Returns the result of task work completed
"""
"""Returns the result of task work completed."""
result = self._worker.get_result()
if hasattr(result.ancillary_result_data, "execution_time"):
@@ -13,14 +13,11 @@ DLOG = debug.debug_get_logger("nfv_common.tasks.task_worker_pool")
class TaskWorkerPool(object):
"""
Task Worker Pool
"""
"""Task Worker Pool."""
def __init__(self, pool_name, num_workers=1):
"""
Create Task Worker Pool
"""
"""Create Task Worker Pool."""
self._pool_name = pool_name
self._workers_avail = collections.OrderedDict()
self._workers = list()
@@ -35,23 +32,20 @@ class TaskWorkerPool(object):
@property
def name(self):
"""
Returns the pool name
"""
"""Returns the pool name."""
return self._pool_name
def available_workers(self):
"""
Returns true if there are workers available to do work
"""
"""Returns true if there are workers available to do work."""
if self._workers_avail:
return True
return False
def claim_worker(self):
"""
Claims a worker, returns a worker if available or None otherwise
"""
"""Claims a worker, returns a worker if available or None otherwise."""
if self._workers_avail:
_, worker = self._workers_avail.popitem()
DLOG.verbose("Claim worker %s" % worker.name)
@@ -59,17 +53,15 @@ class TaskWorkerPool(object):
return None
def release_worker(self, worker, timeout=False):
"""
Release a worker back into the pool
"""
"""Release a worker back into the pool."""
if worker is not None:
DLOG.verbose("Release worker %s" % worker.name)
self._workers_avail[worker.id] = worker
def timeout_worker(self, worker):
"""
Timeout a worker
"""
"""Timeout a worker."""
if worker is not None:
DLOG.info("Timeout worker %s" % worker.name)
worker.stop(max_wait_in_seconds=1)
@@ -81,8 +73,7 @@ class TaskWorkerPool(object):
del worker
def shutdown(self):
"""
Shutdown the pool of workers
"""
"""Shutdown the pool of workers."""
for worker in self._workers:
worker.stop(max_wait_in_seconds=1)
@@ -125,7 +125,6 @@ class UnitTest(object):
if __name__ == "__main__":
debug.debug_initialize(config.CONF["debug"])
selobj.selobj_initialize()
timers.timers_initialize(500, 1000, 1000)
@@ -18,16 +18,13 @@ DLOG = debug.debug_get_logger("nfv_common.tcp")
class TCPConnection(object):
"""
TCP Connection
"""
"""TCP Connection."""
AUTH_VECTOR_MAX_SIZE = 64
def __init__(self, ip, port, sock=None, blocking=True, owner=None, auth_key=None):
"""
Create a TCP connection
"""
"""Create a TCP connection."""
self._owner = owner
self._auth_key = auth_key
self._ip = ip
@@ -61,42 +58,36 @@ class TCPConnection(object):
@property
def ip(self):
"""
Returns the ip of the connection
"""
"""Returns the ip of the connection."""
return self._ip
@property
def port(self):
"""
Returns the port of the connection
"""
"""Returns the port of the connection."""
return self._port
@property
def sock(self):
"""
Returns the socket object of the connection
"""
"""Returns the socket object of the connection."""
return self._socket
@property
def selobj(self):
"""
Returns the selection object associated with the connection
"""
"""Returns the selection object associated with the connection."""
return self._socket.fileno()
def is_shutdown(self):
"""
Returns true if the connection has shutdown
"""
"""Returns true if the connection has shutdown."""
return self._socket is None