blob: 5bdb1685583124b7b48441ccca5e7d0d195f4fec [file] [log] [blame]
Martin Cosyns0efdc872021-09-27 16:24:30 +00001# Copyright 2020-present Open Networking Foundation
2# Original copyright 2020-present ADTRAN, Inc.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14from grpc_robot.grpc_robot import _package_version_get
15
16from dmi import hw_metrics_mgmt_service_pb2, hw_events_mgmt_service_pb2
17from ..tools.protobuf_to_dict import protobuf_to_dict
18
19
20class DmiTools(object):
21 """
22 Tools for the device-management-interface, e.g decoding / conversions.
23 """
24
25 try:
26 ROBOT_LIBRARY_VERSION = _package_version_get('grpc_robot')
27 except NameError:
28 ROBOT_LIBRARY_VERSION = 'unknown'
29
30 @staticmethod
31 def hw_metrics_mgmt_decode_metric(bytestring, return_enum_integer='false', return_defaults='false', human_readable_timestamps='true'):
32 """
33 Converts bytes to a Metric as defined in _message Metric_ from hw_metrics_mgmt_service.proto
34
35 *Parameters*:
36 - bytestring: <bytes>; Byte string, e.g. as it comes from Kafka messages.
37 - return_enum_integer: <string> or <bool>; Whether or not to return the enum values as integer values rather than their labels. Default: _false_.
38 - return_defaults: <string> or <bool>; Whether or not to return the default values. Default: _false_.
39 - human_readable_timestamps: <string> or <bool>; Whether or not to convert the timestamps to human-readable format. Default: _true_.
40
41 *Return*: A dictionary with same structure as the _metric_ key from the return dictionary of keyword _Hw Metrics Mgmt Service Get Metric_.
42
43 *Example*:
44 | Import Library | grpc_robot.DmiTools | WITH NAME | dmi_tools |
45 | ${kafka_records} | kafka.Records Get |
46 | FOR | ${kafka_record} | IN | @{kafka_records} |
47 | | ${metric} | dmi_tools.Hw Metrics Mgmt Decode Metric | ${kafka_record}[message] |
48 | | Log | ${metric} |
49 | END |
50 """
51 return_enum_integer = str(return_enum_integer).lower() == 'true'
52 metric = hw_metrics_mgmt_service_pb2.Metric.FromString(bytestring)
53 return protobuf_to_dict(metric,
54 use_enum_labels=not return_enum_integer,
55 including_default_value_fields=str(return_defaults).lower() == 'true',
56 human_readable_timestamps=str(human_readable_timestamps).lower() == 'true')
57
58 @staticmethod
59 def hw_events_mgmt_decode_event(bytestring, return_enum_integer='false', return_defaults='false', human_readable_timestamps='true'):
60 """
61 Converts bytes to a Event as defined in _message Event_ from hw_events_mgmt_service.proto
62
63 *Parameters*:
64 - bytestring: <bytes>; Byte string, e.g. as it comes from Kafka messages.
65 - return_enum_integer: <string> or <bool>; Whether or not to return the enum values as integer values rather than their labels. Default: _false_.
66 - return_defaults: <string> or <bool>; Whether or not to return the default values. Default: _false_.
67 - human_readable_timestamps: <string> or <bool>; Whether or not to convert the timestamps to human-readable format. Default: _true_.
68
69 *Return*: A dictionary with same structure as the _event_ key from the return dictionary of keyword _Hw Event Mgmt Service List Events_.
70
71 *Example*:
72 | Import Library | grpc_robot.DmiTools | WITH NAME | dmi_tools |
73 | ${kafka_records} | kafka.Records Get |
74 | FOR | ${kafka_record} | IN | @{kafka_records} |
75 | | ${event} | dmi_tools.Hw Events Mgmt Decode Event | ${kafka_record}[message] |
76 | | Log | ${event} |
77 | END |
78 """
79 return_enum_integer = str(return_enum_integer).lower() == 'true'
80 event = hw_events_mgmt_service_pb2.Event.FromString(bytestring)
81 return protobuf_to_dict(event,
82 use_enum_labels=not return_enum_integer,
83 including_default_value_fields=str(return_defaults).lower() == 'true',
84 human_readable_timestamps=str(human_readable_timestamps).lower() == 'true')