Dimitrios Mavrommatis | 0774a59 | 2017-12-21 14:57:52 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | #coding: utf8 |
| 3 | #Copyright © 2016 - 2017 Copyright (c) Sprint, Inc. and others. All rights |
| 4 | #reserved. |
| 5 | # |
| 6 | #This program and the accompanying materials are made available under the |
| 7 | #terms of the Eclipse Public License v1.0 which accompanies this distribution, |
| 8 | #and is available at http://www.eclipse.org/legal/epl-v10.html |
| 9 | # |
| 10 | ############################################################################ |
| 11 | # File : sdf_rules.py |
| 12 | # |
| 13 | # Comments : |
| 14 | # Read SDF Rules config file from './config/' and get parameters |
| 15 | # values as per the SDF Rule Table. |
| 16 | # As per SDF Rule table, formed a structure and parse values in structure, |
| 17 | # and finally pack a structure and send over the zmq socket to DP. |
| 18 | # |
| 19 | # Reference : message_sdn_dp.docx |
| 20 | # Section : Table No.14 SDF Rule |
| 21 | ############################################################################ |
| 22 | |
| 23 | import sys |
| 24 | import os |
| 25 | import time |
| 26 | import struct |
| 27 | import socket |
| 28 | |
| 29 | from netaddr import IPAddress |
| 30 | from configparser import ConfigParser |
| 31 | |
| 32 | parser = ConfigParser() |
| 33 | |
| 34 | def parse_sdf_values(pub_socket,topicId): |
| 35 | # TBD: Need to handle exception |
| 36 | parser.read('./config/sdf_rules.cfg') |
| 37 | print "\n ---> Reading Values from SDF rules file <--- " |
| 38 | print "\n ---> Sending SDF Rules <---" |
| 39 | |
| 40 | MSG_TYPE = 20 |
| 41 | # Initilize the parameters |
| 42 | # Rule type set 0 if Rule String and 1 Five tuple |
| 43 | RULE_TYPE = 0 |
| 44 | PCC_RULE_ID = 0 |
| 45 | DIRECTION = 'bidirectional' |
| 46 | LOCAL_IP = '0.0.0.0' |
| 47 | LOCAL_IP_MASK = 0 |
| 48 | IPV4_REMOTE = '0.0.0.0' |
| 49 | IPV4_REMOTE_MASK = 0 |
| 50 | LOCAL_LOW_LIMIT_PORT = 0 |
| 51 | LOCAL_HIGH_LIMIT_PORT = 65535 |
| 52 | REMOTE_LOW_LIMIT_PORT = 0 |
| 53 | REMOTE_HIGH_LIMIT_PORT = 65535 |
Dimitrios Mavrommatis | 0774a59 | 2017-12-21 14:57:52 -0800 | [diff] [blame] | 54 | PROTOCOL = hex(0) |
| 55 | PROTOCOL_MASK = hex(0) |
| 56 | |
| 57 | # Create the structure for SDF rule and parse the values in that. |
| 58 | for val in parser.sections(): |
| 59 | if val != 'GLOBAL': |
| 60 | # TBD: Need to handle exception |
| 61 | PCC_RULE_ID += 1 |
| 62 | if PCC_RULE_ID > 1: |
| 63 | PROTOCOL_MASK = '0xff' |
| 64 | |
| 65 | if parser.has_option(val, 'RULE_TYPE'): |
| 66 | RULE_TYPE = int(parser.get(val, \ |
| 67 | 'RULE_TYPE')) |
| 68 | |
Dimitrios Mavrommatis | 0774a59 | 2017-12-21 14:57:52 -0800 | [diff] [blame] | 69 | if parser.has_option(val, 'DIRECTION'): |
| 70 | DIRECTION = str(parser.get(val, \ |
| 71 | 'DIRECTION')) |
| 72 | |
Dimitrios Mavrommatis | 0774a59 | 2017-12-21 14:57:52 -0800 | [diff] [blame] | 73 | if parser.has_option(val, 'LOCAL_IP'): |
| 74 | LOCAL_IP = str(parser.get(val, \ |
| 75 | 'LOCAL_IP')) |
| 76 | |
| 77 | if parser.has_option(val, 'LOCAL_IP_MASK'): |
| 78 | LOCAL_IP_MASK = str(parser.get(val, \ |
| 79 | 'LOCAL_IP_MASK')) |
| 80 | |
| 81 | if parser.has_option(val, 'IPV4_REMOTE'): |
| 82 | IPV4_REMOTE = str(parser.get(val, \ |
| 83 | 'IPV4_REMOTE')) |
| 84 | |
| 85 | if parser.has_option(val, 'IPV4_REMOTE_MASK'): |
| 86 | IPV4_REMOTE_MASK = \ |
| 87 | IPAddress(struct.unpack('!L', \ |
| 88 | socket.inet_aton(\ |
| 89 | str(parser.get(val, \ |
| 90 | 'IPV4_REMOTE_MASK'))))\ |
| 91 | [0]).netmask_bits() |
| 92 | |
| 93 | if parser.has_option(val, 'PROTOCOL'): |
| 94 | PROTOCOL = hex(int(parser.get(val, \ |
| 95 | 'PROTOCOL'))) |
| 96 | |
| 97 | if parser.has_option(val, 'PROTOCOL_MASK'): |
| 98 | PROTOCOL_MASK = int(parser.get(val, \ |
| 99 | 'PROTOCOL_MASK')) |
| 100 | |
| 101 | if parser.has_option(val, 'LOCAL_LOW_LIMIT_PORT'): |
| 102 | LOCAL_LOW_LIMIT_PORT = int(parser.get(val, \ |
| 103 | 'LOCAL_LOW_LIMIT_PORT')) |
| 104 | |
| 105 | if parser.has_option(val, 'LOCAL_HIGH_LIMIT_PORT'): |
| 106 | LOCAL_HIGH_LIMIT_PORT = int(parser.get(val, \ |
| 107 | 'LOCAL_HIGH_LIMIT_PORT')) |
| 108 | |
| 109 | if parser.has_option(val, 'REMOTE_LOW_LIMIT_PORT'): |
| 110 | REMOTE_LOW_LIMIT_PORT = int(parser.get(val, \ |
| 111 | 'REMOTE_LOW_LIMIT_PORT')) |
| 112 | |
| 113 | if parser.has_option(val, 'REMOTE_HIGH_LIMIT_PORT'): |
| 114 | REMOTE_HIGH_LIMIT_PORT = int(parser.get(val, \ |
| 115 | 'REMOTE_HIGH_LIMIT_PORT')) |
| 116 | |
| 117 | if DIRECTION == 'bidirectional': |
| 118 | RULE_STRING = \ |
| 119 | str("%s/%s %s/%s %s : %s %s : %s %s/%s") % \ |
| 120 | (IPV4_REMOTE, IPV4_REMOTE_MASK, \ |
| 121 | LOCAL_IP, LOCAL_IP_MASK, \ |
| 122 | REMOTE_LOW_LIMIT_PORT, \ |
| 123 | REMOTE_HIGH_LIMIT_PORT, \ |
| 124 | LOCAL_LOW_LIMIT_PORT, \ |
| 125 | LOCAL_HIGH_LIMIT_PORT, \ |
| 126 | PROTOCOL, PROTOCOL_MASK) |
| 127 | |
| 128 | elif DIRECTION == 'uplink_only': |
| 129 | RULE_STRING = \ |
| 130 | str("%s/%s %s/%s %s : %s %s : %s %s/%s") % \ |
| 131 | (LOCAL_IP, LOCAL_IP_MASK, \ |
| 132 | IPV4_REMOTE, IPV4_REMOTE_MASK, \ |
| 133 | LOCAL_LOW_LIMIT_PORT, \ |
| 134 | LOCAL_HIGH_LIMIT_PORT, \ |
| 135 | REMOTE_LOW_LIMIT_PORT, \ |
| 136 | REMOTE_HIGH_LIMIT_PORT, \ |
| 137 | PROTOCOL, PROTOCOL_MASK) |
| 138 | |
| 139 | elif DIRECTION == 'downlink_only': |
| 140 | RULE_STRING = \ |
| 141 | str("%s/%s %s/%s %s : %s %s : %s 0x%s/0x%s") % \ |
| 142 | (IPV4_REMOTE, IPV4_REMOTE_MASK, \ |
| 143 | LOCAL_IP, LOCAL_IP_MASK, \ |
| 144 | REMOTE_LOW_LIMIT_PORT, \ |
| 145 | REMOTE_HIGH_LIMIT_PORT, \ |
| 146 | LOCAL_LOW_LIMIT_PORT, \ |
| 147 | LOCAL_HIGH_LIMIT_PORT, \ |
| 148 | PROTOCOL, PROTOCOL_MASK) |
| 149 | |
| 150 | # TBD: Need to handle exception |
| 151 | # Pack the structure and send over the zmq socket to DP |
slowr | 8400139 | 2018-01-11 08:02:57 -0500 | [diff] [blame] | 152 | pub_socket.send("%s" % (struct.pack('!BBBI'+\ |
Dimitrios Mavrommatis | 0774a59 | 2017-12-21 14:57:52 -0800 | [diff] [blame] | 153 | str(len(RULE_STRING))+'s',topicId, MSG_TYPE,\ |
slowr | 8400139 | 2018-01-11 08:02:57 -0500 | [diff] [blame] | 154 | RULE_TYPE, \ |
Dimitrios Mavrommatis | 0774a59 | 2017-12-21 14:57:52 -0800 | [diff] [blame] | 155 | len(RULE_STRING), RULE_STRING))) |
slowr | 8400139 | 2018-01-11 08:02:57 -0500 | [diff] [blame] | 156 | |
| 157 | print "\nSDF Rule Values for %s :: \nRULE_ID :%s \ |
| 158 | \nRULE_TYPE :%s \nRULE_STRING :%s"\ |
| 159 | % (val, PCC_RULE_ID, RULE_TYPE, RULE_STRING) |
Dimitrios Mavrommatis | 0774a59 | 2017-12-21 14:57:52 -0800 | [diff] [blame] | 160 | print '\n ---># SDF Rule Successfully sent.. #<---\n' |
| 161 | parser.clear() |
slowr | 8400139 | 2018-01-11 08:02:57 -0500 | [diff] [blame] | 162 | |