summaryrefslogtreecommitdiff
path: root/ironic/drivers/modules/oneview/common.py
blob: 122c847f28c32ceeaeed8ea1054ca04969629ecc (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# Copyright (2015-2017) Hewlett Packard Enterprise Development LP
# Copyright (2015-2017) Universidade Federal de Campina Grande
#
#    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.

from oslo_log import log as logging
from oslo_serialization import jsonutils
from oslo_utils import importutils
from six.moves.urllib import parse

from ironic.common import exception
from ironic.common.i18n import _
from ironic.conf import CONF
from ironic.drivers import utils

LOG = logging.getLogger(__name__)

hponeview_client = importutils.try_import('hpOneView.oneview_client')
redfish = importutils.try_import('redfish')
client_exception = importutils.try_import('hpOneView.exceptions')

REQUIRED_ON_DRIVER_INFO = {
    'server_hardware_uri': _("Server Hardware URI. Required in driver_info."),
}

REQUIRED_ON_PROPERTIES = {
    'server_hardware_type_uri': _(
        "Server Hardware Type URI. Required in properties/capabilities."
    ),
    'server_profile_template_uri': _(
        "Server Profile Template URI to clone from. "
        "Required in properties/capabilities."
    ),
}

OPTIONAL_ON_PROPERTIES = {
    'enclosure_group_uri': _(
        "Enclosure Group URI. Optional in properties/capabilities."),
}

ILOREST_BASE_PORT = "443"

COMMON_PROPERTIES = {}
COMMON_PROPERTIES.update(REQUIRED_ON_DRIVER_INFO)
COMMON_PROPERTIES.update(REQUIRED_ON_PROPERTIES)
COMMON_PROPERTIES.update(OPTIONAL_ON_PROPERTIES)

ISCSI_PXE_ONEVIEW = 'iscsi_pxe_oneview'
AGENT_PXE_ONEVIEW = 'agent_pxe_oneview'

# NOTE(xavierr): We don't want to translate NODE_IN_USE_BY_ONEVIEW and
# SERVER_HARDWARE_ALLOCATION_ERROR to avoid inconsistency in the nodes
# caused by updates on translation in upgrades of ironic.
NODE_IN_USE_BY_ONEVIEW = 'node in use by OneView'
SERVER_HARDWARE_ALLOCATION_ERROR = 'server hardware allocation error'


def prepare_manager_url(manager_url):
    # NOTE(mrtenio) python-oneviewclient uses https or http in the manager_url
    # while python-hpOneView does not. This will not be necessary when
    # python-hpOneView client is the only OneView library.
    if manager_url:
        url_parse = parse.urlparse(manager_url)
        manager_url = url_parse.netloc
    return manager_url


def get_hponeview_client():
    """Generate an instance of the HPE OneView client.

    Generates an instance of the HPE OneView client using the hpOneView lib.

    :returns: an instance of the OneViewClient
    :raises: InvalidParameterValue if mandatory information is missing on the
             node or on invalid input.
    """
    manager_url = prepare_manager_url(CONF.oneview.manager_url)
    config = {
        "ip": manager_url,
        "credentials": {
            "userName": CONF.oneview.username,
            "password": CONF.oneview.password
        }
    }
    return hponeview_client.OneViewClient(config)


def get_ilorest_client(oneview_client, server_hardware):
    """Generate an instance of the iLORest library client.

    :param oneview_client: an instance of a python-hpOneView
    :param: server_hardware: a server hardware uuid or uri
    :returns: an instance of the iLORest client
    :raises: InvalidParameterValue if mandatory information is missing on the
             node or on invalid input.
    """
    remote_console = oneview_client.server_hardware.get_remote_console_url(
        server_hardware
    )
    host_ip, ilo_token = _get_ilo_access(remote_console)
    base_url = "https://%s:%s" % (host_ip, ILOREST_BASE_PORT)
    return redfish.rest_client(base_url=base_url, sessionkey=ilo_token)


def _get_ilo_access(remote_console):
    """Get the needed information to access ilo.

    Get the host_ip and a token of an iLO remote console instance which can be
    used to perform operations on that controller.

    The Remote Console url has the following format:
    hplocons://addr=1.2.3.4&sessionkey=a79659e3b3b7c8209c901ac3509a6719

    :param: remote_console: OneView Remote Console object with a
            remoteConsoleUrl
    :returns: A tuple with the Host IP and Token to access ilo, for
              example: ('1.2.3.4', 'a79659e3b3b7c8209c901ac3509a6719')
    """
    url = remote_console.get('remoteConsoleUrl')
    url_parse = parse.urlparse(url)
    host_ip = parse.parse_qs(url_parse.netloc).get('addr')[0]
    token = parse.parse_qs(url_parse.netloc).get('sessionkey')[0]
    return host_ip, token


def verify_node_info(node):
    """Verify if fields and namespaces of a node are valid.

    Verifies if the 'driver_info' field and the 'properties/capabilities'
    namespace exist and are not empty.

    :param: node: node object to be verified
    :raises: InvalidParameterValue if required node capabilities and/or
             driver_info are malformed or missing
    :raises: MissingParameterValue if required node capabilities and/or
             driver_info are missing
    """
    capabilities_dict = utils.capabilities_to_dict(
        node.properties.get('capabilities', '')
    )
    driver_info = node.driver_info

    _verify_node_info('properties/capabilities', capabilities_dict,
                      REQUIRED_ON_PROPERTIES)

    _verify_node_info('driver_info', driver_info,
                      REQUIRED_ON_DRIVER_INFO)


def get_oneview_info(node):
    """Get OneView information from the node.

    :param: node: node object to get information from
    :returns: a dictionary containing:
        :server_hardware_uri: the uri of the server hardware in OneView
        :server_hardware_type_uri: the uri of the server hardware type in
            OneView
        :enclosure_group_uri: the uri of the enclosure group in OneView
        :server_profile_template_uri: the uri of the server profile template in
            OneView
    :raises OneViewInvalidNodeParameter: if node capabilities are malformed
    """
    try:
        capabilities_dict = utils.capabilities_to_dict(
            node.properties.get('capabilities', '')
        )
    except exception.InvalidParameterValue as e:
        raise exception.OneViewInvalidNodeParameter(node_uuid=node.uuid,
                                                    error=e)

    driver_info = node.driver_info

    oneview_info = {
        'server_hardware_uri':
            driver_info.get('server_hardware_uri'),
        'server_hardware_type_uri':
            capabilities_dict.get('server_hardware_type_uri'),
        'enclosure_group_uri':
            capabilities_dict.get('enclosure_group_uri'),
        'server_profile_template_uri':
            capabilities_dict.get('server_profile_template_uri'),
        'applied_server_profile_uri':
            driver_info.get('applied_server_profile_uri'),
    }

    return oneview_info


def validate_oneview_resources_compatibility(oneview_client, task):
    """Validate if the node configuration is consistent with OneView.

    This method calls hpOneView functions to validate if the node
    configuration is consistent with the OneView resources it represents,
    including serverHardwareUri, serverHardwareTypeUri, serverGroupUri
    serverProfileTemplateUri, enclosureGroupUri and node ports. If any
    validation fails, the driver will raise an appropriate OneViewError.

    :param oneview_client: an instance of the OneView client
    :param: task: a TaskManager instance containing the node to act on.
    :raises: OneViewError if any validation fails.
    """
    ports = task.ports
    oneview_info = get_oneview_info(task.node)

    _validate_node_server_profile_template(oneview_client, oneview_info)
    _validate_node_server_hardware_type(oneview_client, oneview_info)
    _validate_node_enclosure_group(oneview_client, oneview_info)
    _validate_server_profile_template_mac_type(oneview_client, oneview_info)
    _validate_node_port_mac_server_hardware(
        oneview_client, oneview_info, ports)


def _verify_node_info(node_namespace, node_info_dict, info_required):
    """Verify if info_required is present in node_namespace."""
    missing_keys = set(info_required) - set(node_info_dict)

    if missing_keys:
        raise exception.MissingParameterValue(
            _("Missing the keys for the following OneView data in node's "
              "%(namespace)s: %(missing_keys)s.") %
            {'namespace': node_namespace,
             'missing_keys': ', '.join(missing_keys)
             }
        )

    # False and 0 can still be considered as valid values
    missing_values_keys = [k for k in info_required
                           if node_info_dict[k] in ('', None)]
    if missing_values_keys:
        missing_keys = ["%s:%s" % (node_namespace, k)
                        for k in missing_values_keys]
        raise exception.MissingParameterValue(
            _("Missing parameter value for: '%s'") % "', '".join(missing_keys)
        )


def node_has_server_profile(func):
    """Checks if the node's Server Hardware has a Server Profile associated.

    Decorator to execute before the function execution to check if the Server
    Profile is applied to the Server Hardware.

    :param func: a given decorated function.
    """
    def inner(self, *args, **kwargs):
        task = args[0]
        has_server_profile(task, self.client)
        return func(self, *args, **kwargs)
    return inner


def has_server_profile(task, client):
    """Checks if the node's Server Hardware has a Server Profile associated.

    Function to check if the Server Profile is applied to the Server Hardware.

    :param client: an instance of the OneView client
    :param task: a TaskManager instance containing the node to act on.
    """
    try:
        profile = task.node.driver_info.get('applied_server_profile_uri')
        client.server_profiles.get(profile)
    except client_exception.HPOneViewException as exc:
        LOG.error(
            "Failed to get server profile from OneView appliance for"
            " node %(node)s. Error: %(message)s",
            {"node": task.node.uuid, "message": exc}
        )
        raise exception.OneViewError(error=exc)


def _get_server_hardware_mac_from_ilo(oneview_client, server_hardware):
    """Get the MAC of Server Hardware's iLO controller.

    :param: oneview_client: an instance of the HPE OneView client
    :param: server_hardware: a server hardware uuid or uri
    :return: MAC of Server Hardware's iLO controller.
    :raises: InvalidParameterValue if required iLO credentials are missing.
    :raises: OneViewError if can't get mac from a server hardware via iLO or
             if fails to get JSON object with the default path.
    """
    try:
        client = get_ilorest_client(oneview_client, server_hardware)
        ilo_path = "/rest/v1/systems/1"
        hardware = jsonutils.loads(client.get(ilo_path).text)
        hardware_mac = hardware['HostCorrelation']['HostMACAddress'][0]
    except redfish.JsonDecodingError as exc:
        LOG.error("Failed in JSON object getting path: %s", ilo_path)
        raise exception.OneViewError(error=exc)
    except (ValueError, TypeError, IndexError) as exc:
        LOG.error(
            "Failed to get mac from server hardware %(server_hardware)s "
            "via iLO. Error: %(message)s", {
                "server_hardware": server_hardware.get("uri"),
                "message": exc
            }
        )
        raise exception.OneViewError(error=exc)

    return hardware_mac


def _get_server_hardware_mac(server_hardware):
    """Get the MAC address of the first PXE bootable port of an Ethernet port.

    :param: server_hardware: OneView Server Hardware object
    :return: MAC of the first Ethernet and function 'a' port of the
             Server Hardware object
    :raises: OneViewError if there is no Ethernet port on the Server Hardware
             or if there is no portMap on the Server Hardware requested
    """
    sh_physical_port = None

    if server_hardware.get('portMap'):
        for device in server_hardware.get(
                'portMap', {}).get('deviceSlots', ()):
            for physical_port in device.get('physicalPorts', ()):
                if physical_port.get('type') == 'Ethernet':
                    sh_physical_port = physical_port
                    break
        if sh_physical_port:
            for virtual_port in sh_physical_port.get('virtualPorts', ()):
                # NOTE(nicodemos): Ironic oneview drivers needs to use a
                # port that type is Ethernet and function identifier 'a' for
                # this FlexNIC to be able to make a deploy using PXE.
                if virtual_port.get('portFunction') == 'a':
                    return virtual_port.get('mac', ()).lower()
        raise exception.OneViewError(
            _("There is no Ethernet port on the Server Hardware: %s") %
            server_hardware.get('uri'))
    else:
        raise exception.OneViewError(
            _("The Server Hardware: %s doesn't have a list of adapters/slots, "
              "their ports and attributes. This information is available only "
              "for blade servers. Is this a rack server?") %
            server_hardware.get('uri'))


def _validate_node_server_profile_template(oneview_client, oneview_info):
    """Validate if the Server Profile Template is consistent.

    :param: oneview_client: an instance of the HPE OneView client
    :param: oneview_info: the OneView related info in an Ironic node
    :raises: OneViewError if the node's Server Profile Template is not
             consistent
    """
    server_profile_template = oneview_client.server_profile_templates.get(
        oneview_info['server_profile_template_uri'])
    server_hardware = oneview_client.server_hardware.get(
        oneview_info['server_hardware_uri'])

    _validate_server_profile_template_server_hardware_type(
        server_profile_template, server_hardware)
    _validate_spt_enclosure_group(server_profile_template, server_hardware)
    _validate_server_profile_template_manage_boot(server_profile_template)


def _validate_server_profile_template_server_hardware_type(
        server_profile_template, server_hardware):
    """Validate if the Server Hardware Types are the same.

    Validate if the Server Profile Template and the Server Hardware have the
    same Server Hardware Type

    :param: server_profile_template: OneView Server Profile Template object
    :param: server_hardware: OneView Server Hardware object
    :raises: OneViewError if the Server Profile Template and the Server
             Hardware does not have the same Server Hardware Type
    """
    spt_server_hardware_type_uri = (
        server_profile_template.get('serverHardwareTypeUri')
    )
    sh_server_hardware_type_uri = server_hardware.get('serverHardwareTypeUri')

    if spt_server_hardware_type_uri != sh_server_hardware_type_uri:
        message = _(
            "Server profile template %(spt_uri)s serverHardwareTypeUri is "
            "inconsistent with server hardware %(server_hardware_uri)s "
            "serverHardwareTypeUri.") % {
                'spt_uri': server_profile_template.get('uri'),
                'server_hardware_uri': server_hardware.get('uri')}
        raise exception.OneViewError(message)


def _validate_spt_enclosure_group(server_profile_template, server_hardware):
    """Validate Server Profile Template's Enclosure Group and Server Hardware's.

    :param: server_profile_template: OneView Server Profile Template object
    :param: server_hardware: OneView Server Hardware object
    :raises: OneViewError if the Server Profile Template's Enclosure Group does
             not match the Server Hardware's
    """
    spt_enclosure_group_uri = server_profile_template.get('enclosureGroupUri')
    sh_enclosure_group_uri = server_hardware.get('serverGroupUri')

    if spt_enclosure_group_uri != sh_enclosure_group_uri:
        message = _("Server profile template %(spt_uri)s enclosureGroupUri is "
                    "inconsistent with server hardware %(sh_uri)s "
                    "serverGroupUri.") % {
                        'spt_uri': server_profile_template.get('uri'),
                        'sh_uri': server_hardware.get('uri')}
        raise exception.OneViewError(message)


def _validate_server_profile_template_manage_boot(server_profile_template):
    """Validate if the Server Profile Template allows to manage the boot order.

    :param: server_profile_template: OneView Server Profile Template object
    :raises: OneViewError if the Server Profile Template does not allows to
             manage the boot order.
    """
    manage_boot = server_profile_template.get('boot', {}).get('manageBoot')

    if not manage_boot:
        message = _("Server Profile Template: %s, does not allow to manage "
                    "boot order.") % server_profile_template.get('uri')
        raise exception.OneViewError(message)


def _validate_node_server_hardware_type(oneview_client, oneview_info):
    """Validate if the node's Server Hardware Type matches Server Hardware's.

    :param: oneview_client: the HPE OneView Client
    :param: oneview_info: the OneView related info in an Ironic node
    :raises: OneViewError if the node's Server Hardware Type group doesn't
             match the Server Hardware's
    """
    node_server_hardware_type_uri = oneview_info['server_hardware_type_uri']
    server_hardware = oneview_client.server_hardware.get(
        oneview_info['server_hardware_uri'])
    server_hardware_sht_uri = server_hardware.get('serverHardwareTypeUri')

    if server_hardware_sht_uri != node_server_hardware_type_uri:
        message = _("Node server_hardware_type_uri is inconsistent "
                    "with OneView's server hardware %(server_hardware_uri)s "
                    "serverHardwareTypeUri.") % {
                        'server_hardware_uri': server_hardware.get('uri')}
        raise exception.OneViewError(message)


def _validate_node_enclosure_group(oneview_client, oneview_info):
    """Validate if the node's Enclosure Group matches the Server Hardware's.

    :param: oneview_client: an instance of the HPE OneView client
    :param: oneview_info: the OneView related info in an Ironic node
    :raises: OneViewError if the node's enclosure group doesn't match the
             Server Hardware's
    """
    server_hardware = oneview_client.server_hardware.get(
        oneview_info['server_hardware_uri'])
    sh_enclosure_group_uri = server_hardware.get('serverGroupUri')
    node_enclosure_group_uri = oneview_info['enclosure_group_uri']

    if node_enclosure_group_uri and (
            sh_enclosure_group_uri != node_enclosure_group_uri):
        message = _(
            "Node enclosure_group_uri '%(node_enclosure_group_uri)s' "
            "is inconsistent with OneView's server hardware "
            "serverGroupUri '%(sh_enclosure_group_uri)s' of "
            "ServerHardware %(server_hardware)s") % {
                'node_enclosure_group_uri': node_enclosure_group_uri,
                'sh_enclosure_group_uri': sh_enclosure_group_uri,
                'server_hardware': server_hardware.get('uri')}
        raise exception.OneViewError(message)


def _validate_node_port_mac_server_hardware(oneview_client,
                                            oneview_info, ports):
    """Validate if a port matches the node's Server Hardware's MAC.

    :param: oneview_client: an instance of the HPE OneView client
    :param: oneview_info: the OneView related info in an Ironic node
    :param: ports: a list of Ironic node's ports
    :raises: OneViewError if there is no port with MAC address matching one
    in OneView

    """
    server_hardware = oneview_client.server_hardware.get(
        oneview_info['server_hardware_uri'])

    if not ports:
        return

    # NOTE(nicodemos) If hponeview client's unable to get the MAC of the Server
    # Hardware and raises an exception, the driver will try to get it from
    # the iLOrest client.
    try:
        mac = _get_server_hardware_mac(server_hardware)
    except exception.OneViewError:
        mac = _get_server_hardware_mac_from_ilo(
            oneview_client, server_hardware)

    incompatible_macs = []
    for port in ports:
        if port.address.lower() == mac.lower():
            return
        incompatible_macs.append(port.address)

    message = _("The ports of the node are not compatible with its "
                "server hardware %(server_hardware_uri)s. There are no Ironic "
                "port MAC's: %(port_macs)s, that matches with the "
                "server hardware's MAC: %(server_hardware_mac)s") % {
                    'server_hardware_uri': server_hardware.get('uri'),
                    'port_macs': ', '.join(incompatible_macs),
                    'server_hardware_mac': mac}
    raise exception.OneViewError(message)


def _validate_server_profile_template_mac_type(oneview_client, oneview_info):
    """Validate if the node's Server Profile Template's MAC type is physical.

    :param: oneview_client: an instance of the HPE OneView client
    :param: oneview_info: the OneView related info in an Ironic node
    :raises: OneViewError if the node's Server Profile Template's MAC type is
             not physical
    """
    server_profile_template = oneview_client.server_profile_templates.get(
        oneview_info['server_profile_template_uri']
    )
    if server_profile_template.get('macType') != 'Physical':
        message = _("The server profile template %s is not set to use "
                    "physical MAC.") % server_profile_template.get('uri')
        raise exception.OneViewError(message)