Merge "Get planner from solution"

This commit is contained in:
Zuul 2019-09-19 02:21:23 +00:00 committed by Gerrit Code Review
commit 594a0d9b01
3 changed files with 15 additions and 8 deletions

View File

@ -66,12 +66,10 @@ class AuditHandler(BaseAuditHandler):
self._planner_loader = loader.DefaultPlannerLoader()
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.
# thus we need to load planner every time
strategy = self.strategy_context.select_strategy(
audit, request_context)
planner_name = strategy.planner
planner_name = solution.strategy.planner
LOG.debug("Loading %s", planner_name)
planner = self._planner_loader.load(name=planner_name)
return planner
@ -93,7 +91,7 @@ class AuditHandler(BaseAuditHandler):
request_context, audit,
action=fields.NotificationAction.PLANNER,
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)
notifications.audit.send_action_notification(
request_context, audit,

View File

@ -72,7 +72,7 @@ class BaseSolution(object):
:type strategy: :py:class:`~.BaseStrategy` instance
"""
self.goal = goal
self.strategy = strategy
self._strategy = strategy
self.origin = None
self.model = None
self.efficacy = efficacy.Efficacy(self.goal, self.strategy)
@ -85,6 +85,10 @@ class BaseSolution(object):
def efficacy_indicators(self):
return self.efficacy.indicators
@property
def strategy(self):
return self._strategy
def compute_global_efficacy(self):
"""Compute the global efficacy given a map of efficacy indicators"""
self.efficacy.compute_global_efficacy()

View File

@ -17,6 +17,7 @@
import mock
from watcher.decision_engine.solution import default
from watcher.decision_engine.strategy import strategies
from watcher.tests import base
@ -24,7 +25,8 @@ class TestDefaultSolution(base.TestCase):
def test_default_solution(self):
solution = default.DefaultSolution(
goal=mock.Mock(), strategy=mock.Mock())
goal=mock.Mock(),
strategy=strategies.DummyStrategy(config=mock.Mock()))
parameters = {
"source_node": "server1",
"destination_node": "server2",
@ -43,10 +45,12 @@ class TestDefaultSolution(base.TestCase):
solution.actions[0].get('action_type'))
self.assertEqual(expected_parameters,
solution.actions[0].get('input_parameters'))
self.assertEqual('weight', solution.strategy.planner)
def test_default_solution_with_no_input_parameters(self):
solution = default.DefaultSolution(
goal=mock.Mock(), strategy=mock.Mock())
goal=mock.Mock(),
strategy=strategies.DummyStrategy(config=mock.Mock()))
solution.add_action(action_type="nop",
resource_id="b199db0c-1408-4d52-b5a5-5ca14de0ff36")
self.assertEqual(1, len(solution.actions))
@ -58,3 +62,4 @@ class TestDefaultSolution(base.TestCase):
solution.actions[0].get('action_type'))
self.assertEqual(expected_parameters,
solution.actions[0].get('input_parameters'))
self.assertEqual('weight', solution.strategy.planner)