blob: c47191cbd023e8bb3d06558003c0603b6dd7ed16 [file] [log] [blame]
Martin Cosynsf88ed6e2020-12-02 10:30:10 +01001# Copyright 2020 ADTRAN, Inc.
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
13from grpc_robot.grpc_robot import _package_version_get
14
15from dmi import hw_metrics_mgmt_service_pb2, hw_events_mgmt_service_pb2
16from ..tools.protobuf_to_dict import protobuf_to_dict
17
18
19class DmiTools(object):
20 """
21 Tools for the device-management-interface, e.g decoding / conversions.
22 """
23
24 try:
25 ROBOT_LIBRARY_VERSION = _package_version_get('grpc_robot')
26 except NameError:
27 ROBOT_LIBRARY_VERSION = 'unknown'
28
29 @staticmethod
30 def hw_metrics_mgmt_decode_metric(bytestring, return_enum_integer='false', return_defaults='false'):
31 """
32 Converts bytes to a Metric as defined in _message Metric_ from hw_metrics_mgmt_service.proto
33
34 *Parameters*:
35 - bytestring: <bytes>; Byte string, e.g. as it comes from Kafka messages.
36 - return_enum_integer: <bool> or <string>; Whether or not to return the enum values as integer values rather than their labels. Default: ${FALSE} or false.
37 - return_defaults: <bool> or <string>; Whether or not to return the default values. Default: _${FALSE}_ or _false_.
38
39 *Return*: A dictionary with same structure as the _metric_ key from the return dictionary of keyword _Hw Metrics Mgmt Service Get Metric_.
40
41 *Example*:
42 | Import Library | grpc_robot.DmiTools | WITH NAME | dmi_tools |
43 | ${kafka_records} | kafka.Records Get |
44 | FOR | ${kafka_record} | IN | @{kafka_records} |
45 | | ${metric} | dmi_tools.Hw Metrics Mgmt Decode Metric | ${kafka_record}[message] |
46 | | Log | ${metric} |
47 | END |
48 """
49 return_enum_integer = bool(str(return_enum_integer).lower() == 'true')
50 metric = hw_metrics_mgmt_service_pb2.Metric.FromString(bytestring)
51 return protobuf_to_dict(metric, use_enum_labels=not return_enum_integer, including_default_value_fields=return_defaults)
52
53 @staticmethod
54 def hw_events_mgmt_decode_event(bytestring, return_enum_integer='false', return_defaults='false'):
55 """
56 Converts bytes to a Event as defined in _message Event_ from hw_events_mgmt_service.proto
57
58 *Parameters*:
59 - bytestring: <bytes>; Byte string, e.g. as it comes from Kafka messages.
60 - return_enum_integer: <bool> or <string>; Whether or not to return the enum values as integer values rather than their labels. Default: ${FALSE} or false.
61 - return_defaults: <bool> or <string>; Whether or not to return the default values. Default: _${FALSE}_ or _false_.
62
63 *Return*: A dictionary with same structure as the _event_ key from the return dictionary of keyword _Hw Event Mgmt Service List Events_.
64
65 *Example*:
66 | Import Library | grpc_robot.DmiTools | WITH NAME | dmi_tools |
67 | ${kafka_records} | kafka.Records Get |
68 | FOR | ${kafka_record} | IN | @{kafka_records} |
69 | | ${metric} | dmi_tools.Hw Events Mgmt Decode Event | ${kafka_record}[message] |
70 | | Log | ${metric} |
71 | END |
72 """
73 return_enum_integer = bool(str(return_enum_integer).lower() == 'true')
74 event = hw_events_mgmt_service_pb2.Event.FromString(bytestring)
75 return protobuf_to_dict(event, use_enum_labels=not return_enum_integer, including_default_value_fields=return_defaults)