aboutsummaryrefslogtreecommitdiffstats
path: root/core/pod_controller.py
blob: 8bc91ec452d10519ec5165d4c03255c9331b08bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Copyright 2020 Spirent Communications
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" pod Controller interface
"""

import logging
import pexpect
#from conf import settings
from pods.pod.pod import IPod

class PodController():
    """POD controller class

    Used to set-up and control PODs for specified scenario

    Attributes:
        _pod_class: A class object representing the POD.
        _deployment: A string describing the scenario to set-up in the
            constructor.
        _pods: A list of pods controlled by the controller.
    """

    def __init__(self, deployment, pod_class):
        """Sets up the POD infrastructure based on deployment scenario

        :param pod_class: The POD class to be used.
        """
        # reset POD ID counter for each testcase
        IPod.reset_pod_counter()
        pod_number = 0
        # setup controller with requested number of pods
        self._logger = logging.getLogger(__name__)
        self._pod_class = pod_class
        self._deployment = deployment.lower()
        self._pods = []
        if self._deployment == 'p2p':
            pod_number = 1

        if pod_number:
            self._pods = [pod_class() for _ in range(pod_number)]

            self._logger.debug('Initializing the pod')

    def get_pods(self):
        """Returns a list of pods controlled by this controller.
        """
        self._logger.debug('get the pods')
        return self._pods

    def get_pods_number(self):
        """Returns a number of pods controlled by this controller.
        """
        self._logger.debug('get_pods_number %s pod[s]', str(len(self._pods)))
        return len(self._pods)

    def start(self):
        """Boots all pods set-up by __init__.

        This is a blocking function.
        """
        self._logger.debug('start the pod')
        try:
            for pod in self._pods:
                pod.create()
        except pexpect.TIMEOUT:
            self.stop()
            raise

    def stop(self):
        """Stops all pods set-up by __init__.

        This is a blocking function.
        """
        self._logger.debug('stopping the pod')
        for pod in self._pods:
            pod.terminate()

    def __enter__(self):
        self.start()

    def __exit__(self, type_, value, traceback):
        self.stop()