Get planner from solution
support Strategy select different planner Implements: bp watcher-planner-selector Change-Id: I586e67f782e2965234826634ba3ff51681af4df8
This commit is contained in:
@@ -66,12 +66,10 @@ class AuditHandler(BaseAuditHandler):
|
|||||||
self._planner_loader = loader.DefaultPlannerLoader()
|
self._planner_loader = loader.DefaultPlannerLoader()
|
||||||
self.applier_client = rpcapi.ApplierAPI()
|
self.applier_client = rpcapi.ApplierAPI()
|
||||||
|
|
||||||
def get_planner(self, audit, request_context):
|
def get_planner(self, solution):
|
||||||
# because AuditHandler is a singletone we need to avoid race condition.
|
# because AuditHandler is a singletone we need to avoid race condition.
|
||||||
# thus we need to load planner every time
|
# thus we need to load planner every time
|
||||||
strategy = self.strategy_context.select_strategy(
|
planner_name = solution.strategy.planner
|
||||||
audit, request_context)
|
|
||||||
planner_name = strategy.planner
|
|
||||||
LOG.debug("Loading %s", planner_name)
|
LOG.debug("Loading %s", planner_name)
|
||||||
planner = self._planner_loader.load(name=planner_name)
|
planner = self._planner_loader.load(name=planner_name)
|
||||||
return planner
|
return planner
|
||||||
@@ -93,7 +91,7 @@ class AuditHandler(BaseAuditHandler):
|
|||||||
request_context, audit,
|
request_context, audit,
|
||||||
action=fields.NotificationAction.PLANNER,
|
action=fields.NotificationAction.PLANNER,
|
||||||
phase=fields.NotificationPhase.START)
|
phase=fields.NotificationPhase.START)
|
||||||
planner = self.get_planner(audit, request_context)
|
planner = self.get_planner(solution)
|
||||||
action_plan = planner.schedule(request_context, audit.id, solution)
|
action_plan = planner.schedule(request_context, audit.id, solution)
|
||||||
notifications.audit.send_action_notification(
|
notifications.audit.send_action_notification(
|
||||||
request_context, audit,
|
request_context, audit,
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ class BaseSolution(object):
|
|||||||
:type strategy: :py:class:`~.BaseStrategy` instance
|
:type strategy: :py:class:`~.BaseStrategy` instance
|
||||||
"""
|
"""
|
||||||
self.goal = goal
|
self.goal = goal
|
||||||
self.strategy = strategy
|
self._strategy = strategy
|
||||||
self.origin = None
|
self.origin = None
|
||||||
self.model = None
|
self.model = None
|
||||||
self.efficacy = efficacy.Efficacy(self.goal, self.strategy)
|
self.efficacy = efficacy.Efficacy(self.goal, self.strategy)
|
||||||
@@ -85,6 +85,10 @@ class BaseSolution(object):
|
|||||||
def efficacy_indicators(self):
|
def efficacy_indicators(self):
|
||||||
return self.efficacy.indicators
|
return self.efficacy.indicators
|
||||||
|
|
||||||
|
@property
|
||||||
|
def strategy(self):
|
||||||
|
return self._strategy
|
||||||
|
|
||||||
def compute_global_efficacy(self):
|
def compute_global_efficacy(self):
|
||||||
"""Compute the global efficacy given a map of efficacy indicators"""
|
"""Compute the global efficacy given a map of efficacy indicators"""
|
||||||
self.efficacy.compute_global_efficacy()
|
self.efficacy.compute_global_efficacy()
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
import mock
|
import mock
|
||||||
|
|
||||||
from watcher.decision_engine.solution import default
|
from watcher.decision_engine.solution import default
|
||||||
|
from watcher.decision_engine.strategy import strategies
|
||||||
from watcher.tests import base
|
from watcher.tests import base
|
||||||
|
|
||||||
|
|
||||||
@@ -24,7 +25,8 @@ class TestDefaultSolution(base.TestCase):
|
|||||||
|
|
||||||
def test_default_solution(self):
|
def test_default_solution(self):
|
||||||
solution = default.DefaultSolution(
|
solution = default.DefaultSolution(
|
||||||
goal=mock.Mock(), strategy=mock.Mock())
|
goal=mock.Mock(),
|
||||||
|
strategy=strategies.DummyStrategy(config=mock.Mock()))
|
||||||
parameters = {
|
parameters = {
|
||||||
"source_node": "server1",
|
"source_node": "server1",
|
||||||
"destination_node": "server2",
|
"destination_node": "server2",
|
||||||
@@ -43,10 +45,12 @@ class TestDefaultSolution(base.TestCase):
|
|||||||
solution.actions[0].get('action_type'))
|
solution.actions[0].get('action_type'))
|
||||||
self.assertEqual(expected_parameters,
|
self.assertEqual(expected_parameters,
|
||||||
solution.actions[0].get('input_parameters'))
|
solution.actions[0].get('input_parameters'))
|
||||||
|
self.assertEqual('weight', solution.strategy.planner)
|
||||||
|
|
||||||
def test_default_solution_with_no_input_parameters(self):
|
def test_default_solution_with_no_input_parameters(self):
|
||||||
solution = default.DefaultSolution(
|
solution = default.DefaultSolution(
|
||||||
goal=mock.Mock(), strategy=mock.Mock())
|
goal=mock.Mock(),
|
||||||
|
strategy=strategies.DummyStrategy(config=mock.Mock()))
|
||||||
solution.add_action(action_type="nop",
|
solution.add_action(action_type="nop",
|
||||||
resource_id="b199db0c-1408-4d52-b5a5-5ca14de0ff36")
|
resource_id="b199db0c-1408-4d52-b5a5-5ca14de0ff36")
|
||||||
self.assertEqual(1, len(solution.actions))
|
self.assertEqual(1, len(solution.actions))
|
||||||
@@ -58,3 +62,4 @@ class TestDefaultSolution(base.TestCase):
|
|||||||
solution.actions[0].get('action_type'))
|
solution.actions[0].get('action_type'))
|
||||||
self.assertEqual(expected_parameters,
|
self.assertEqual(expected_parameters,
|
||||||
solution.actions[0].get('input_parameters'))
|
solution.actions[0].get('input_parameters'))
|
||||||
|
self.assertEqual('weight', solution.strategy.planner)
|
||||||
|
|||||||
Reference in New Issue
Block a user