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 random |
| 17 | from adapters.adtran_common.net.adtran_zmq import AdtranZmqClient |
| 18 | from enum import IntEnum |
| 19 | |
| 20 | DEFAULT_PIO_TCP_PORT = 5555 |
| 21 | #DEFAULT_PIO_TCP_PORT = 5657 |
| 22 | |
| 23 | |
| 24 | class PioClient(AdtranZmqClient): |
| 25 | """ |
| 26 | Adtran ZeroMQ Client for packet in/out service |
| 27 | """ |
| 28 | def __init__(self, ip_address, rx_callback, port): |
| 29 | super(PioClient, self).__init__(ip_address, rx_callback, port) |
| 30 | self._seq_number = random.randint(1, 2**32) |
| 31 | |
| 32 | class UrlType(IntEnum): |
| 33 | PACKET_IN = 0 # Packet In |
| 34 | PACKET_OUT = 1 # Packet Out |
| 35 | EVCMAPS_REQUEST = 2 # EVC-MAPs request |
| 36 | EVCMAPS_RESPONSE = 3 # EVC-MAPs response |
| 37 | UNKNOWN = 4 # UNKNOWN URL |
| 38 | |
| 39 | def get_url_type(self, packet): |
| 40 | url_type = PioClient.UrlType.UNKNOWN |
| 41 | message = json.loads(packet) |
| 42 | if 'url' in message: |
| 43 | if message['url'] == 'adtran-olt-of-control/packet-in': |
| 44 | url_type = PioClient.UrlType.PACKET_IN |
| 45 | elif message['url'] == 'adtran-olt-of-control/packet-out': |
| 46 | url_type = PioClient.UrlType.PACKET_OUT |
| 47 | elif message['url'] == 'adtran-olt-of-control/evc-map-response': |
| 48 | url_type = PioClient.UrlType.EVCMAPS_RESPONSE |
| 49 | elif message['url'] == 'adtran-olt-of-control/evc-map-request': |
| 50 | url_type = PioClient.UrlType.EVCMAPS_REQUEST |
| 51 | return url_type |
| 52 | |
| 53 | def decode_packet(self, packet): |
| 54 | from scapy.layers.l2 import Ether |
| 55 | try: |
| 56 | message = json.loads(packet) |
| 57 | self.log.debug('message', message=message) |
| 58 | |
| 59 | for field in ['url', 'evc-map-name', 'total-len', 'port-number', 'message-contents']: |
| 60 | assert field in message, "Missing field '{}' in received packet".format(field) |
| 61 | |
| 62 | decoded = message['message-contents'].decode('base64') |
| 63 | |
| 64 | assert len(decoded.encode('hex'))/2 == message['total-len'], \ |
| 65 | 'Decoded length ({}) != Message Encoded length ({})'.\ |
| 66 | format(len(decoded.encode('hex')), message['total-len']) |
| 67 | |
| 68 | return int(message['port-number']), message['evc-map-name'], Ether(decoded) |
| 69 | |
| 70 | except Exception as e: |
| 71 | self.log.exception('decode', e=e) |
| 72 | raise |
| 73 | |
| 74 | @property |
| 75 | def sequence_number(self): |
| 76 | if self._seq_number >= 2**32: |
| 77 | self._seq_number = 0 |
| 78 | else: |
| 79 | self._seq_number += 1 |
| 80 | |
| 81 | return self._seq_number |
| 82 | |
| 83 | def encode_packet(self, egress_port, packet, map_name='TODO', exception_type=''): |
| 84 | """ |
| 85 | Encode a message for transmission as a Packet Out |
| 86 | :param egress_port: (int) egress physical port number |
| 87 | :param packet: (str) actual message |
| 88 | :param map_name: (str) EVC-MAP Name |
| 89 | :param exception_type: (str) Type of exception |
| 90 | """ |
| 91 | return json.dumps({ |
| 92 | 'url': 'adtran-olt-of-control/packet-out', |
| 93 | 'buffer-id': self.sequence_number, |
| 94 | 'total-len': len(packet), |
| 95 | 'evc-map-name': map_name, |
| 96 | 'exception-type': exception_type, |
| 97 | 'port-number': egress_port, |
| 98 | 'message-contents': packet.encode('base64') |
| 99 | }) |
| 100 | |
| 101 | def query_request_packet(self): |
| 102 | """ |
| 103 | Create query-request to get all installed exceptions |
| 104 | :return: Request string |
| 105 | """ |
| 106 | return json.dumps({ |
| 107 | 'url': 'adtran-olt-of-control/evc-map-request' |
| 108 | }) |
| 109 | |
| 110 | def decode_query_response_packet(self, packet, map_name=None): |
| 111 | """ |
| 112 | Create query-request to get all installed exceptions |
| 113 | :param map_name: (str) EVC-MAP Name (None=all) |
| 114 | :param packet: returned query response packet |
| 115 | :return: list of evcmaps and associated exceptions |
| 116 | """ |
| 117 | from scapy.layers.l2 import Ether |
| 118 | message = json.loads(packet) |
| 119 | self.log.debug('message', message=message) |
| 120 | |
| 121 | if 'url' in message and message['url'] == 'adtran-olt-of-control/evc-map-response': |
| 122 | maps=message['evc-map-list'] |
| 123 | if maps is not None: |
| 124 | self.log.debug('evc-maps-query-response', maps=maps) |
| 125 | return maps |
| 126 | return [] |