From 0c191a2da948ca994ed39f1244cba61467bff216 Mon Sep 17 00:00:00 2001 From: licanwei Date: Tue, 17 Sep 2019 19:36:07 -0700 Subject: [PATCH] Get planner from solution support Strategy select different planner Implements: bp watcher-planner-selector Change-Id: I586e67f782e2965234826634ba3ff51681af4df8 --- watcher/decision_engine/audit/base.py | 8 +++----- watcher/decision_engine/solution/base.py | 6 +++++- .../decision_engine/solution/test_default_solution.py | 9 +++++++-- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/watcher/decision_engine/audit/base.py b/watcher/decision_engine/audit/base.py index e43e77489..07d4bf0e1 100644 --- a/watcher/decision_engine/audit/base.py +++ b/watcher/decision_engine/audit/base.py @@ -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, diff --git a/watcher/decision_engine/solution/base.py b/watcher/decision_engine/solution/base.py index 3aa895ce3..ba56a0716 100644 --- a/watcher/decision_engine/solution/base.py +++ b/watcher/decision_engine/solution/base.py @@ -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() diff --git a/watcher/tests/decision_engine/solution/test_default_solution.py b/watcher/tests/decision_engine/solution/test_default_solution.py index c0fc8393b..5b1492989 100644 --- a/watcher/tests/decision_engine/solution/test_default_solution.py +++ b/watcher/tests/decision_engine/solution/test_default_solution.py @@ -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)