blob: ac18c5a28988fecdb11fcfa8f6c842b5cb190fd0 [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 voltha_protos import events_pb2
17from voltha_protos import tech_profile_pb2
18from grpc_robot.tools.protobuf_to_dict import protobuf_to_dict
19
20
21class VolthaTools(object):
22 """
23 Tools for the voltha, e.g decoding / conversions.
24 """
25
26 try:
27 ROBOT_LIBRARY_VERSION = _package_version_get('grpc_robot')
28 except NameError:
29 ROBOT_LIBRARY_VERSION = 'unknown'
30
31 @staticmethod
32 def _convert_string_to_bytes(string):
33 """Converts a string to a bytes object."""
34 try:
35 return bytes.fromhex(string.replace('\\x', ' '))
36 except:
37 try:
38 b = bytearray()
39 b.extend(map(ord, string))
40 return bytes(b)
41 except (TypeError, AttributeError, SystemError):
42 return string
43
44 def events_decode_event(self, bytestring, return_enum_integer='false', return_defaults='false', human_readable_timestamps='true'):
45 """
46 Converts bytes to an Event as defined in _message Event_ from events.proto
47
48 *Parameters*:
49 - bytestring: <bytes>; Byte string, e.g. as it comes from Kafka messages.
50 - return_enum_integer: <string> or <bool>; Whether or not to return the enum values as integer values rather than their labels. Default: _false_.
51 - return_defaults: <string> or <bool>; Whether or not to return the default values. Default: _false_.
52 - human_readable_timestamps: <string> or <bool>; Whether or not to convert the timestamps to human-readable format. Default: _true_.
53
54 *Return*: A dictionary with _event_ structure.
55
56 *Example*:
57 | Import Library | grpc_robot.VolthaTools | WITH NAME | voltha_tools |
58 | ${kafka_records} | kafka.Records Get |
59 | FOR | ${kafka_record} | IN | @{kafka_records} |
60 | | ${event} | voltha_tools.Events Decode Event | ${kafka_record}[message] |
61 | | Log | ${event} |
62 | END |
63 """
64 return_enum_integer = str(return_enum_integer).lower() == 'true'
65 result = events_pb2.Event.FromString(self._convert_string_to_bytes(bytestring))
66 return protobuf_to_dict(result,
67 use_enum_labels=not return_enum_integer,
68 including_default_value_fields=str(return_defaults).lower() == 'true',
69 human_readable_timestamps=str(human_readable_timestamps).lower() == 'true')
70
71 def tech_profile_decode_resource_instance(self, bytestring, return_enum_integer='false', return_defaults='false', human_readable_timestamps='true'):
72 """
73 Converts bytes to an resource instance as defined in _message ResourceInstance_ from tech_profile.proto
74
75 *Parameters*:
76 - bytestring: <bytes>; Byte string, e.g. as it comes from Kafka messages.
77 - return_enum_integer: <string> or <bool>; Whether or not to return the enum values as integer values rather than their labels. Default: _false_.
78 - return_defaults: <string> or <bool>; Whether or not to return the default values. Default: _false_.
79 - human_readable_timestamps: <string> or <bool>; Whether or not to convert the timestamps to human-readable format. Default: _true_.
80
81 *Return*: A dictionary with _event_ structure.
82
83 *Example*:
84 | Import Library | grpc_robot.VolthaTools | WITH NAME | voltha_tools |
85 | ${kafka_records} | kafka.Records Get |
86 | FOR | ${kafka_record} | IN | @{kafka_records} |
87 | | ${event} | voltha_tools. Tech Profile Decode Resource Instance | ${kafka_record}[message] |
88 | | Log | ${event} |
89 | END |
90 """
91 return_enum_integer = str(return_enum_integer).lower() == 'true'
92 result = tech_profile_pb2.ResourceInstance.FromString(self._convert_string_to_bytes(bytestring))
93 return protobuf_to_dict(result,
94 use_enum_labels=not return_enum_integer,
95 including_default_value_fields=str(return_defaults).lower() == 'true',
96 human_readable_timestamps=str(human_readable_timestamps).lower() == 'true')
97
98
99if __name__ == '__main__':
100 messages = [
101 b'\nD\n#Voltha.openolt..1626255789301080436\x10\x02 \x02*\x030.12\x06\x08\xad\xe3\xba\x87\x06:\x0c\x08\xad\xe3\xba\x87\x06\x10\xd9\xc9\xc8\x8f\x01"\xc2\x02\x11\x00\x00@k\xac;\xd8A\x1a\xb6\x02\n\x93\x01\n\x08PONStats\x11\x00\x00@k\xac;\xd8A*$65950aaf-b40f-4697-b5c3-8deb50fedd5d2-\n\x05oltid\x12$65950aaf-b40f-4697-b5c3-8deb50fedd5d2\x15\n\ndevicetype\x12\x07openolt2\x12\n\tportlabel\x12\x05pon-0\x12\x10\n\tTxPackets\x15\x00\x00\x8bC\x12\x15\n\x0eTxMcastPackets\x15\x00\x00\xa6B\x12\x15\n\x0eTxBcastPackets\x15\x00\x00\xa6B\x12\x0e\n\x07RxBytes\x15\x00\x00\x8bF\x12\x10\n\tRxPackets\x15\x00\x00\x8bC\x12\x15\n\x0eRxMcastPackets\x15\x00\x00\xa6B\x12\x15\n\x0eRxBcastPackets\x15\x00\x00\xa6B\x12\x0e\n\x07TxBytes\x15\x00\x00\x8bF',
102 b'\nD\n#Voltha.openolt..1613491472935896440\x10\x02 \x02*\x030.12\x06\x08\xad\xe3\xba\x87\x06:\x0c\x08\xad\xe3\xba\x87\x06\x10\xd9\xc9\xc8\x8f\x01"\xc2\x02\x11\x00\x00@k\xac;\xd8A\x1a\xb6\x02\n\x93\x01\n\x08PONStats\x11\x00\x00@k\xac;\xd8A*$65950aaf-b40f-4697-b5c3-8deb50fedd5d2-\n\x05oltid\x12$65950aaf-b40f-4697-b5c3-8deb50fedd5d2\x15\n\ndevicetype\x12\x07openolt2\x12\n\tportlabel\x12\x05pon-0\x12\x10\n\tTxPackets\x15\x00\x00\x8bC\x12\x15\n\x0eTxMcastPackets\x15\x00\x00\xa6B\x12\x15\n\x0eTxBcastPackets\x15\x00\x00\xa6B\x12\x0e\n\x07RxBytes\x15\x00\x00\x8bF\x12\x10\n\tRxPackets\x15\x00\x00\x8bC\x12\x15\n\x0eRxMcastPackets\x15\x00\x00\xa6B\x12\x15\n\x0eRxBcastPackets\x15\x00\x00\xa6B\x12\x0e\n\x07TxBytes\x15\x00\x00\x8bF'
103 ]
104 for message in messages:
105 print(VolthaTools().events_decode_event(message))
106
107 messages = [
108 b"\x08\x40\x12\x07\x58\x47\x53\x2d\x50\x4f\x4e\x1a\x42\x6f\x6c\x74\x2d\x7b\x39\x35\x36\x31\x37\x31\x32\x62\x2d\x35\x33\x32\x33\x2d\x34\x64\x64\x63\x2d\x38\x36\x62\x34\x2d\x64\x35\x31\x62\x62\x34\x61\x65\x30\x37\x33\x39\x7d\x2f\x70\x6f\x6e\x2d\x7b\x31\x7d\x2f\x6f\x6e\x75\x2d\x7b\x32\x7d\x2f\x75\x6e\x69\x2d\x7b\x30\x7d\x20\x81\x08\x2a\x10\x88\x08\x89\x08\x8a\x08\x8b\x08\x8c\x08\x8d\x08\x8e\x08\x8f\x08",
109 b"\x08\x40\x12\x07\x58\x47\x53\x2d\x50\x4f\x4e\x1a\x42\x6f\x6c\x74\x2d\x7b\x35\x66\x35\x39\x61\x32\x32\x63\x2d\x37\x63\x37\x65\x2d\x34\x65\x30\x63\x2d\x39\x38\x30\x65\x2d\x37\x34\x66\x31\x35\x33\x62\x33\x32\x33\x38\x31\x7d\x2f\x70\x6f\x6e\x2d\x7b\x30\x7d\x2f\x6f\x6e\x75\x2d\x7b\x31\x7d\x2f\x75\x6e\x69\x2d\x7b\x30\x7d\x20\x81\x08\x2a\x10\x88\x08\x89\x08\x8a\x08\x8b\x08\x8c\x08\x8d\x08\x8e\x08\x8f\x08",
110 b"\x08\x40\x12\x07\x58\x47\x53\x2d\x50\x4f\x4e\x1a\x42\x6f\x6c\x74\x2d\x7b\x35\x66\x35\x39\x61\x32\x32\x63\x2d\x37\x63\x37\x65\x2d\x34\x65\x30\x63\x2d\x39\x38\x30\x65\x2d\x37\x34\x66\x31\x35\x33\x62\x33\x32\x33\x38\x31\x7d\x2f\x70\x6f\x6e\x2d\x7b\x30\x7d\x2f\x6f\x6e\x75\x2d\x7b\x32\x7d\x2f\x75\x6e\x69\x2d\x7b\x30\x7d\x20\x80\x08\x2a\x10\x80\x08\x81\x08\x82\x08\x83\x08\x84\x08\x85\x08\x86\x08\x87\x08",
111 b"\x08\x40\x12\x07\x58\x47\x53\x2d\x50\x4f\x4e\x1a\x42\x6f\x6c\x74\x2d\x7b\x35\x66\x35\x39\x61\x32\x32\x63\x2d\x37\x63\x37\x65\x2d\x34\x65\x30\x63\x2d\x39\x38\x30\x65\x2d\x37\x34\x66\x31\x35\x33\x62\x33\x32\x33\x38\x31\x7d\x2f\x70\x6f\x6e\x2d\x7b\x31\x7d\x2f\x6f\x6e\x75\x2d\x7b\x31\x7d\x2f\x75\x6e\x69\x2d\x7b\x30\x7d\x20\x81\x08\x2a\x10\x88\x08\x89\x08\x8a\x08\x8b\x08\x8c\x08\x8d\x08\x8e\x08\x8f\x08",
112 b"\x08\x40\x12\x07\x58\x47\x53\x2d\x50\x4f\x4e\x1a\x42\x6f\x6c\x74\x2d\x7b\x35\x66\x35\x39\x61\x32\x32\x63\x2d\x37\x63\x37\x65\x2d\x34\x65\x30\x63\x2d\x39\x38\x30\x65\x2d\x37\x34\x66\x31\x35\x33\x62\x33\x32\x33\x38\x31\x7d\x2f\x70\x6f\x6e\x2d\x7b\x31\x7d\x2f\x6f\x6e\x75\x2d\x7b\x32\x7d\x2f\x75\x6e\x69\x2d\x7b\x30\x7d\x20\x80\x08\x2a\x10\x80\x08\x81\x08\x82\x08\x83\x08\x84\x08\x85\x08\x86\x08\x87\x08",
113 b"\x08\x40\x12\x07\x58\x47\x53\x2d\x50\x4f\x4e\x1a\x42\x6f\x6c\x74\x2d\x7b\x38\x34\x62\x35\x64\x35\x61\x39\x2d\x33\x34\x64\x66\x2d\x34\x61\x33\x37\x2d\x62\x66\x37\x64\x2d\x63\x37\x37\x61\x34\x65\x33\x34\x33\x61\x37\x64\x7d\x2f\x70\x6f\x6e\x2d\x7b\x30\x7d\x2f\x6f\x6e\x75\x2d\x7b\x31\x7d\x2f\x75\x6e\x69\x2d\x7b\x30\x7d\x20\x81\x08\x2a\x10\x88\x08\x89\x08\x8a\x08\x8b\x08\x8c\x08\x8d\x08\x8e\x08\x8f\x08",
114 b"\x08\x40\x12\x07\x58\x47\x53\x2d\x50\x4f\x4e\x1a\x42\x6f\x6c\x74\x2d\x7b\x38\x34\x62\x35\x64\x35\x61\x39\x2d\x33\x34\x64\x66\x2d\x34\x61\x33\x37\x2d\x62\x66\x37\x64\x2d\x63\x37\x37\x61\x34\x65\x33\x34\x33\x61\x37\x64\x7d\x2f\x70\x6f\x6e\x2d\x7b\x30\x7d\x2f\x6f\x6e\x75\x2d\x7b\x32\x7d\x2f\x75\x6e\x69\x2d\x7b\x30\x7d\x20\x80\x08\x2a\x10\x80\x08\x81\x08\x82\x08\x83\x08\x84\x08\x85\x08\x86\x08\x87\x08",
115 b"\x08\x40\x12\x07\x58\x47\x53\x2d\x50\x4f\x4e\x1a\x42\x6f\x6c\x74\x2d\x7b\x38\x34\x62\x35\x64\x35\x61\x39\x2d\x33\x34\x64\x66\x2d\x34\x61\x33\x37\x2d\x62\x66\x37\x64\x2d\x63\x37\x37\x61\x34\x65\x33\x34\x33\x61\x37\x64\x7d\x2f\x70\x6f\x6e\x2d\x7b\x31\x7d\x2f\x6f\x6e\x75\x2d\x7b\x31\x7d\x2f\x75\x6e\x69\x2d\x7b\x30\x7d\x20\x80\x08\x2a\x10\x80\x08\x81\x08\x82\x08\x83\x08\x84\x08\x85\x08\x86\x08\x87\x08",
116 b"\x08\x40\x12\x07\x58\x47\x53\x2d\x50\x4f\x4e\x1a\x42\x6f\x6c\x74\x2d\x7b\x38\x34\x62\x35\x64\x35\x61\x39\x2d\x33\x34\x64\x66\x2d\x34\x61\x33\x37\x2d\x62\x66\x37\x64\x2d\x63\x37\x37\x61\x34\x65\x33\x34\x33\x61\x37\x64\x7d\x2f\x70\x6f\x6e\x2d\x7b\x31\x7d\x2f\x6f\x6e\x75\x2d\x7b\x32\x7d\x2f\x75\x6e\x69\x2d\x7b\x30\x7d\x20\x81\x08\x2a\x10\x88\x08\x89\x08\x8a\x08\x8b\x08\x8c\x08\x8d\x08\x8e\x08\x8f\x08",
117 b"\x08\x40\x12\x07\x58\x47\x53\x2d\x50\x4f\x4e\x1a\x42\x6f\x6c\x74\x2d\x7b\x61\x61\x34\x36\x63\x62\x63\x61\x2d\x39\x31\x64\x37\x2d\x34\x36\x64\x65\x2d\x61\x61\x30\x65\x2d\x61\x32\x65\x33\x64\x32\x36\x61\x61\x66\x36\x32\x7d\x2f\x70\x6f\x6e\x2d\x7b\x30\x7d\x2f\x6f\x6e\x75\x2d\x7b\x31\x7d\x2f\x75\x6e\x69\x2d\x7b\x30\x7d\x20\x80\x08\x2a\x10\x80\x08\x81\x08\x82\x08\x83\x08\x84\x08\x85\x08\x86\x08\x87\x08",
118 "\x08\x40\x12\x07\x58\x47\x53\x2d\x50\x4f\x4e\x1a\x42\x6f\x6c\x74\x2d\x7b\x61\x61\x34\x36\x63\x62\x63\x61\x2d\x39\x31\x64\x37\x2d\x34\x36\x64\x65\x2d\x61\x61\x30\x65\x2d\x61\x32\x65\x33\x64\x32\x36\x61\x61\x66\x36\x32\x7d\x2f\x70\x6f\x6e\x2d\x7b\x30\x7d\x2f\x6f\x6e\x75\x2d\x7b\x31\x7d\x2f\x75\x6e\x69\x2d\x7b\x30\x7d\x20\x80\x08\x2a\x10\x80\x08\x81\x08\x82\x08\x83\x08\x84\x08\x85\x08\x86\x08\x87\x08",
119 "\\x08\\x40\\x12\\x07\\x58\\x47\\x53\\x2d\\x50\\x4f\\x4e\\x1a\\x42\\x6f\\x6c\\x74\\x2d\\x7b\\x61\\x61\\x34\\x36\\x63\\x62\\x63\\x61\\x2d\\x39\\x31\\x64\\x37\\x2d\\x34\\x36\\x64\\x65\\x2d\\x61\\x61\\x30\\x65\\x2d\\x61\\x32\\x65\\x33\\x64\\x32\\x36\\x61\\x61\\x66\\x36\\x32\\x7d\\x2f\\x70\\x6f\\x6e\\x2d\\x7b\\x30\\x7d\\x2f\\x6f\\x6e\\x75\\x2d\\x7b\\x31\\x7d\\x2f\\x75\\x6e\\x69\\x2d\\x7b\\x30\\x7d\\x20\\x80\\x08\\x2a\\x10\\x80\\x08\\x81\\x08\\x82\\x08\\x83\\x08\\x84\\x08\\x85\\x08\\x86\\x08\\x87\\x08"
120 ]
121 # for message in messages:
122 # print(VolthaTools().tech_profile_decode_resource_instance(message))