Add cloudtaf framework
[ta/cloudtaf.git] / libraries / cluster / hosts.py
1 # Copyright 2019 Nokia
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import abc
16 from collections import namedtuple
17 import six
18
19
20 STORAGE = 'storage'
21
22
23 class Profiles(object):
24     _master = None
25     _worker = None
26
27     @classmethod
28     def set_profiles(cls, master, worker):
29         cls._master = master
30         cls._worker = worker
31
32     @property
33     def master(self):
34         return self._master
35
36     @property
37     def worker(self):
38         return self._worker
39
40     @property
41     def storage(self):
42         return STORAGE
43
44     @property
45     def profiles_mask(self):
46         return set([self.master, self.worker, self.storage])
47
48
49 class MgmtTarget(namedtuple('MgmtTarget', ['host', 'user', 'password'])):
50     """Container for the cloudtaf2 management VIP target attributes.
51
52     Arguments:
53         host: IP address or FQDN of the host management VIP
54         user: Username, e.g. cloudadmin for login
55         password: Login password
56
57     Example:
58
59        Library    cluster.cluster.MgmtTarget
60        ...    host=1.2.3.4
61        ...    user=cloudadmin
62        ...    password=good_password
63     """
64
65     def asdict(self):
66         return self._asdict()
67
68
69 @six.add_metaclass(abc.ABCMeta)
70 class HostBase(object):
71     """Container base for Host attributes.
72
73     Attributes:
74         name: name of the host
75         service_profiles: list of service profiles like ['master', 'worker']
76         shelldicts: list of dictionaries to RemoteSession.set_runner_target
77     """
78     def __init__(self, host_config):
79         self._host_config = host_config
80
81     @property
82     def is_dpdk(self):
83         """Return True if dpdk is used in provider network interfaces.
84         In more detail, is True if and only if one of the network profiles has
85         at least one interface with type *ovs-dpdk*.
86         """
87         return self._host_config.is_dpdk
88
89     @property
90     def name(self):
91         return self._host_config.name
92
93     @property
94     def service_profiles(self):
95         return self._host_config.service_profiles
96
97     @property
98     def network_domain(self):
99         return self._host_config.network_domain
100
101     @abc.abstractproperty
102     def shelldicts(self):
103         """Return *shelldicts* for :class:`crl.remotesession.remotesession`.
104         """
105
106     @property
107     def _host_dict(self):
108         return {'host': self._infra['ip'],
109                 'user': self._host_config.mgmt_target.user,
110                 'password': self._host_config.mgmt_target.password}
111
112     @property
113     def _infra(self):
114         return self._host_config.networking['infra_{}'.format(self._infra_type)]
115
116     @abc.abstractproperty
117     def _infra_type(self):
118         """Return any infra type e.g. 'internal', 'external' etc.
119         """
120
121
122 class Master(HostBase):
123     @property
124     def shelldicts(self):
125         return [self._host_dict]
126
127     @property
128     def _infra_type(self):
129         return 'external'
130
131     @property
132     def external_ip(self):
133         return self._infra['ip']
134
135
136 class NonMaster(HostBase):
137     @property
138     def shelldicts(self):
139         return [self._host_config.mgmt_target.asdict(), self._host_dict]
140
141     @property
142     def _infra_type(self):
143         return 'internal'
144
145
146 class HostConfig(namedtuple('HostConfig', ['name',
147                                            'network_domain',
148                                            'service_profiles',
149                                            'networking',
150                                            'mgmt_target',
151                                            'is_dpdk'])):
152     pass