1 # Copyright (C) 2021 Wind River Systems, Inc.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # client talking to Stx standalone
18 from o2common.service.client.base_client import BaseClient
19 from typing import List
21 from o2ims.domain import stx_object as ocloudModel
22 from o2common.config import config
23 from o2ims.domain.resource_type import ResourceTypeEnum
25 # from dcmanagerclient.api import client
26 from cgtsclient.client import get_client
28 from o2common.helper import o2logging
29 logger = o2logging.get_logger(__name__)
32 class StxSaOcloudClient(BaseClient):
33 def __init__(self, driver=None):
35 self.driver = driver if driver else StxSaClientImp()
37 def _get(self, id) -> ocloudModel.StxGenericModel:
38 return self.driver.getInstanceInfo()
40 def _list(self, **filters):
41 return [self.driver.getInstanceInfo()]
44 class StxSaResourcePoolClient(BaseClient):
47 self.driver = StxSaClientImp()
49 def _get(self, id) -> ocloudModel.StxGenericModel:
50 return self.driver.getInstanceInfo()
52 def _list(self, **filters):
53 return [self.driver.getInstanceInfo()]
56 class StxSaDmsClient(BaseClient):
59 self.driver = StxSaClientImp()
61 def _get(self, name) -> ocloudModel.StxGenericModel:
62 return self.driver.getK8sDetail(name)
64 def _list(self, **filters):
65 return self.driver.getK8sList(**filters)
68 class StxPserverClient(BaseClient):
71 self.driver = StxSaClientImp()
73 def _get(self, id) -> ocloudModel.StxGenericModel:
74 return self.driver.getPserver(id)
76 def _list(self, **filters) -> List[ocloudModel.StxGenericModel]:
77 return self.driver.getPserverList(**filters)
80 class StxCpuClient(BaseClient):
83 # self._pserver_id = pserver_id
84 self.driver = StxSaClientImp()
86 def _get(self, id) -> ocloudModel.StxGenericModel:
87 return self.driver.getCpu(id)
89 def _list(self, **filters) -> List[ocloudModel.StxGenericModel]:
90 return self.driver.getCpuList(**filters)
93 class StxMemClient(BaseClient):
96 self.driver = StxSaClientImp()
98 def _get(self, id) -> ocloudModel.StxGenericModel:
99 return self.driver.getMem(id)
101 def _list(self, **filters) -> List[ocloudModel.StxGenericModel]:
102 return self.driver.getMemList(**filters)
105 class StxEthClient(BaseClient):
108 self.driver = StxSaClientImp()
110 def _get(self, id) -> ocloudModel.StxGenericModel:
111 return self.driver.getEthernet(id)
113 def _list(self, **filters) -> List[ocloudModel.StxGenericModel]:
114 return self.driver.getEthernetList(**filters)
117 class StxIfClient(BaseClient):
120 self.driver = StxSaClientImp()
122 def _get(self, id) -> ocloudModel.StxGenericModel:
123 return self.driver.getIf(id)
125 def _list(self, **filters) -> List[ocloudModel.StxGenericModel]:
126 return self.driver.getIfList(**filters)
129 class StxIfPortClient(BaseClient):
132 self.driver = StxSaClientImp()
134 def _get(self, id) -> ocloudModel.StxGenericModel:
135 return self.driver.getPort(id)
137 def _list(self, **filters) -> List[ocloudModel.StxGenericModel]:
138 return self.driver.getPortList(**filters)
141 # internal driver which implement client call to Stx Standalone instance
142 class StxSaClientImp(object):
143 def __init__(self, stx_client=None):
145 self.stxclient = stx_client if stx_client else self.getStxClient()
147 def getStxClient(self):
148 os_client_args = config.get_stx_access_info()
149 config_client = get_client(**os_client_args)
152 def getInstanceInfo(self) -> ocloudModel.StxGenericModel:
153 systems = self.stxclient.isystem.list()
154 logger.debug('systems:' + str(systems[0].to_dict()))
155 return ocloudModel.StxGenericModel(
156 ResourceTypeEnum.OCLOUD, systems[0]) if systems else None
158 def getPserverList(self, **filters) -> List[ocloudModel.StxGenericModel]:
159 # resourcepoolid = filters.get("resourcepoolid", None)
160 hosts = self.stxclient.ihost.list()
161 logger.debug('host 1:' + str(hosts[0].to_dict()))
162 return [ocloudModel.StxGenericModel(
163 ResourceTypeEnum.PSERVER, self._hostconverter(host))
164 for host in hosts if host and host.availability == 'available']
166 def getPserver(self, id) -> ocloudModel.StxGenericModel:
167 host = self.stxclient.ihost.get(id)
168 logger.debug('host:' + str(host.to_dict()))
169 return ocloudModel.StxGenericModel(
170 ResourceTypeEnum.PSERVER, self._hostconverter(host))
172 def getK8sList(self, **filters) -> List[ocloudModel.StxGenericModel]:
173 k8sclusters = self.stxclient.kube_cluster.list()
174 logger.debug('k8sresources[0]:' + str(k8sclusters[0].to_dict()))
175 return [ocloudModel.StxGenericModel(
176 ResourceTypeEnum.DMS,
177 self._k8sconverter(k8sres), self._k8shasher(k8sres))
178 for k8sres in k8sclusters if k8sres]
180 def getK8sDetail(self, name) -> ocloudModel.StxGenericModel:
182 k8sclusters = self.stxclient.kube_cluster.list()
183 # logger.debug("k8sresources[0]:" + str(k8sclusters[0].to_dict()))
184 k8scluster = k8sclusters.pop()
186 k8scluster = self.stxclient.kube_cluster.get(name)
190 logger.debug('k8sresource:' + str(k8scluster.to_dict()))
191 return ocloudModel.StxGenericModel(
192 ResourceTypeEnum.DMS,
193 self._k8sconverter(k8scluster), self._k8shasher(k8scluster))
195 def getCpuList(self, **filters) -> List[ocloudModel.StxGenericModel]:
196 hostid = filters.get('hostid', None)
197 assert (hostid is not None), 'missing hostid to query icpu list'
198 cpulist = self.stxclient.icpu.list(hostid)
199 return [ocloudModel.StxGenericModel(
200 ResourceTypeEnum.PSERVER_CPU,
201 self._cpuconverter(cpures)) for cpures in cpulist if cpures]
203 def getCpu(self, id) -> ocloudModel.StxGenericModel:
204 cpuinfo = self.stxclient.icpu.get(id)
205 return ocloudModel.StxGenericModel(
206 ResourceTypeEnum.PSERVER_CPU, self._cpuconverter(cpuinfo))
208 def getMemList(self, **filters) -> List[ocloudModel.StxGenericModel]:
209 hostid = filters.get('hostid', None)
210 assert (hostid is not None), 'missing hostid to query imem list'
211 memlist = self.stxclient.imemory.list(hostid)
212 return [ocloudModel.StxGenericModel(
213 ResourceTypeEnum.PSERVER_RAM,
214 self._memconverter(memories)) for memories in memlist if memories]
216 def getMem(self, id) -> ocloudModel.StxGenericModel:
217 meminfo = self.stxclient.imemory.get(id)
218 return ocloudModel.StxGenericModel(
219 ResourceTypeEnum.PSERVER_RAM, self._memconverter(meminfo))
221 def getEthernetList(self, **filters) -> List[ocloudModel.StxGenericModel]:
222 hostid = filters.get('hostid', None)
223 assert (hostid is not None), 'missing hostid to query port list'
224 ethlist = self.stxclient.ethernet_port.list(hostid)
225 return [ocloudModel.StxGenericModel(
226 ResourceTypeEnum.PSERVER_ETH,
227 self._ethconverter(eth)) for eth in ethlist if eth]
229 def getEthernet(self, id) -> ocloudModel.StxGenericModel:
230 ethinfo = self.stxclient.ethernet_port.get(id)
231 return ocloudModel.StxGenericModel(
232 ResourceTypeEnum.PSERVER_ETH, self._ethconverter(ethinfo))
234 def getIfList(self, **filters) -> List[ocloudModel.StxGenericModel]:
235 hostid = filters.get('hostid', None)
236 assert (hostid is not None), 'missing hostid to query iinterface list'
237 iflist = self.stxclient.iinterface.list(hostid)
238 return [ocloudModel.StxGenericModel(
239 ResourceTypeEnum.PSERVER_IF,
240 self._ifconverter(ifs)) for ifs in iflist if ifs]
242 def getIf(self, id) -> ocloudModel.StxGenericModel:
243 ifinfo = self.stxclient.iinterface.get(id)
244 return ocloudModel.StxGenericModel(
245 ResourceTypeEnum.PSERVER_IF, self._ifconverter(ifinfo))
247 def getPortList(self, **filters) -> List[ocloudModel.StxGenericModel]:
248 ifid = filters.get('interfaceid', None)
249 assert (ifid is not None), 'missing interface id to query port list'
250 portlist = self.stxclient.iinterface.list_ports(ifid)
251 return [ocloudModel.StxGenericModel(
252 ResourceTypeEnum.PSERVER_IF_PORT,
253 port) for port in portlist if port]
255 def getPort(self, id) -> ocloudModel.StxGenericModel:
256 portinfo = self.stxclient.port.get(id)
257 return ocloudModel.StxGenericModel(
258 ResourceTypeEnum.PSERVER_IF_PORT, portinfo)
260 def _getIsystems(self):
261 return self.stxclient.isystem.list()
263 def _getIsystem(self, id=None):
265 return self.stxclient.isystem.get(id)
267 isystems = self.stxclient.isystem.list()
268 if len(isystems) != 1 and not id:
269 raise Exception('No system uuid was provided and '
270 'more than one system exists in the account.')
274 def _hostconverter(host):
275 setattr(host, 'name', host.hostname)
279 def _cpuconverter(cpu):
280 setattr(cpu, 'name', cpu.ihost_uuid.split(
281 '-', 1)[0] + '-cpu-'+str(cpu.cpu))
285 def _memconverter(mem):
286 setattr(mem, 'name', mem.ihost_uuid.split('-', 1)[0] +
287 '-mem-node-'+str(mem.numa_node))
291 def _ethconverter(eth):
292 setattr(eth, 'name', eth.host_uuid.split('-', 1)[0] + '-'+eth.name)
293 setattr(eth, 'updated_at', None)
294 setattr(eth, 'created_at', None)
298 def _ifconverter(ifs):
299 setattr(ifs, 'name', ifs.ihost_uuid.split('-', 1)[0] + '-'+ifs.ifname)
300 setattr(ifs, 'updated_at', None)
301 setattr(ifs, 'created_at', None)
305 def _k8sconverter(cluster):
306 setattr(cluster, 'name', cluster.cluster_name)
307 setattr(cluster, 'uuid',
308 uuid.uuid3(uuid.NAMESPACE_URL, cluster.cluster_name))
309 setattr(cluster, 'updated_at', None)
310 setattr(cluster, 'created_at', None)
311 setattr(cluster, 'events', [])
312 logger.debug('k8s cluster name/uuid:' +
313 cluster.name + '/' + str(cluster.uuid))
317 def _k8shasher(cluster):
318 return str(hash((cluster.cluster_name,
319 cluster.cluster_api_endpoint, cluster.admin_user)))