Chip Boling | f5af85d | 2019-02-12 15:36:17 -0600 | [diff] [blame] | 1 | # Copyright 2017-present 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 |
| 13 | # limitations under the License. |
| 14 | |
| 15 | import json |
| 16 | import struct |
| 17 | import binascii |
| 18 | from adapters.adtran_common.net.adtran_zmq import AdtranZmqClient |
| 19 | |
| 20 | DEFAULT_PON_AGENT_TCP_PORT = 5656 |
| 21 | |
| 22 | |
| 23 | class PonClient(AdtranZmqClient): |
| 24 | """ |
| 25 | Adtran ZeroMQ Client for PON Agent service |
| 26 | """ |
| 27 | def __init__(self, ip_address, rx_callback, port): |
| 28 | super(PonClient, self).__init__(ip_address, rx_callback, port) |
| 29 | |
| 30 | def encode_omci_packet(self, msg, pon_index, onu_id): |
| 31 | """ |
| 32 | Create an OMCI Tx Packet for the specified ONU |
| 33 | |
| 34 | :param msg: (str) OMCI message to send |
| 35 | :param pon_index: (unsigned int) PON Port index |
| 36 | :param onu_id: (unsigned int) ONU ID |
| 37 | |
| 38 | :return: (bytes) octet string to send |
| 39 | """ |
| 40 | return json.dumps({"operation": "NOTIFY", |
| 41 | "url": "adtran-olt-pon-control/omci-message", |
| 42 | "pon-id": pon_index, |
| 43 | "onu-id": onu_id, |
| 44 | "message-contents": msg.decode("hex").encode("base64") |
| 45 | }) |
| 46 | |
| 47 | def decode_packet(self, packet): |
| 48 | """ |
| 49 | Decode the PON-Agent packet provided by the ZMQ client |
| 50 | |
| 51 | :param packet: (bytes) Packet |
| 52 | :return: (long, long, bytes, boolean) PON Index, ONU ID, Frame Contents (OMCI or Ethernet),\ |
| 53 | and a flag indicating if it is OMCI |
| 54 | """ |
| 55 | msg = json.loads(packet) |
| 56 | pon_id = msg['pon-id'] |
| 57 | onu_id = msg['onu-id'] |
| 58 | msg_data = msg['message-contents'].decode("base64") |
| 59 | is_omci = msg['operation'] == "NOTIFY" and 'omci-message' in msg['url'] |
| 60 | |
| 61 | return pon_id, onu_id, msg_data, is_omci |