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 |
| 54 | PRECEDENCE = 0 |
| 55 | PROTOCOL = hex(0) |
| 56 | PROTOCOL_MASK = hex(0) |
| 57 | |
| 58 | # Create the structure for SDF rule and parse the values in that. |
| 59 | for val in parser.sections(): |
| 60 | if val != 'GLOBAL': |
| 61 | # TBD: Need to handle exception |
| 62 | PCC_RULE_ID += 1 |
| 63 | if PCC_RULE_ID > 1: |
| 64 | PROTOCOL_MASK = '0xff' |
| 65 | |
| 66 | if parser.has_option(val, 'RULE_TYPE'): |
| 67 | RULE_TYPE = int(parser.get(val, \ |
| 68 | 'RULE_TYPE')) |
| 69 | |
| 70 | if parser.has_option(val, 'RATING_GROUP'): |
| 71 | RATING_GROUP = int(parser.get(val, \ |
| 72 | 'RATING_GROUP')) |
| 73 | |
| 74 | if parser.has_option(val, 'DIRECTION'): |
| 75 | DIRECTION = str(parser.get(val, \ |
| 76 | 'DIRECTION')) |
| 77 | |
| 78 | if parser.has_option(val, 'PRECEDENCE'): |
| 79 | PRECEDENCE = int(parser.get(val, \ |
| 80 | 'PRECEDENCE')) |
| 81 | |
| 82 | if parser.has_option(val, 'LOCAL_IP'): |
| 83 | LOCAL_IP = str(parser.get(val, \ |
| 84 | 'LOCAL_IP')) |
| 85 | |
| 86 | if parser.has_option(val, 'LOCAL_IP_MASK'): |
| 87 | LOCAL_IP_MASK = str(parser.get(val, \ |
| 88 | 'LOCAL_IP_MASK')) |
| 89 | |
| 90 | if parser.has_option(val, 'IPV4_REMOTE'): |
| 91 | IPV4_REMOTE = str(parser.get(val, \ |
| 92 | 'IPV4_REMOTE')) |
| 93 | |
| 94 | if parser.has_option(val, 'IPV4_REMOTE_MASK'): |
| 95 | IPV4_REMOTE_MASK = \ |
| 96 | IPAddress(struct.unpack('!L', \ |
| 97 | socket.inet_aton(\ |
| 98 | str(parser.get(val, \ |
| 99 | 'IPV4_REMOTE_MASK'))))\ |
| 100 | [0]).netmask_bits() |
| 101 | |
| 102 | if parser.has_option(val, 'PROTOCOL'): |
| 103 | PROTOCOL = hex(int(parser.get(val, \ |
| 104 | 'PROTOCOL'))) |
| 105 | |
| 106 | if parser.has_option(val, 'PROTOCOL_MASK'): |
| 107 | PROTOCOL_MASK = int(parser.get(val, \ |
| 108 | 'PROTOCOL_MASK')) |
| 109 | |
| 110 | if parser.has_option(val, 'LOCAL_LOW_LIMIT_PORT'): |
| 111 | LOCAL_LOW_LIMIT_PORT = int(parser.get(val, \ |
| 112 | 'LOCAL_LOW_LIMIT_PORT')) |
| 113 | |
| 114 | if parser.has_option(val, 'LOCAL_HIGH_LIMIT_PORT'): |
| 115 | LOCAL_HIGH_LIMIT_PORT = int(parser.get(val, \ |
| 116 | 'LOCAL_HIGH_LIMIT_PORT')) |
| 117 | |
| 118 | if parser.has_option(val, 'REMOTE_LOW_LIMIT_PORT'): |
| 119 | REMOTE_LOW_LIMIT_PORT = int(parser.get(val, \ |
| 120 | 'REMOTE_LOW_LIMIT_PORT')) |
| 121 | |
| 122 | if parser.has_option(val, 'REMOTE_HIGH_LIMIT_PORT'): |
| 123 | REMOTE_HIGH_LIMIT_PORT = int(parser.get(val, \ |
| 124 | 'REMOTE_HIGH_LIMIT_PORT')) |
| 125 | |
| 126 | if DIRECTION == 'bidirectional': |
| 127 | RULE_STRING = \ |
| 128 | str("%s/%s %s/%s %s : %s %s : %s %s/%s") % \ |
| 129 | (IPV4_REMOTE, IPV4_REMOTE_MASK, \ |
| 130 | LOCAL_IP, LOCAL_IP_MASK, \ |
| 131 | REMOTE_LOW_LIMIT_PORT, \ |
| 132 | REMOTE_HIGH_LIMIT_PORT, \ |
| 133 | LOCAL_LOW_LIMIT_PORT, \ |
| 134 | LOCAL_HIGH_LIMIT_PORT, \ |
| 135 | PROTOCOL, PROTOCOL_MASK) |
| 136 | |
| 137 | elif DIRECTION == 'uplink_only': |
| 138 | RULE_STRING = \ |
| 139 | str("%s/%s %s/%s %s : %s %s : %s %s/%s") % \ |
| 140 | (LOCAL_IP, LOCAL_IP_MASK, \ |
| 141 | IPV4_REMOTE, IPV4_REMOTE_MASK, \ |
| 142 | LOCAL_LOW_LIMIT_PORT, \ |
| 143 | LOCAL_HIGH_LIMIT_PORT, \ |
| 144 | REMOTE_LOW_LIMIT_PORT, \ |
| 145 | REMOTE_HIGH_LIMIT_PORT, \ |
| 146 | PROTOCOL, PROTOCOL_MASK) |
| 147 | |
| 148 | elif DIRECTION == 'downlink_only': |
| 149 | RULE_STRING = \ |
| 150 | str("%s/%s %s/%s %s : %s %s : %s 0x%s/0x%s") % \ |
| 151 | (IPV4_REMOTE, IPV4_REMOTE_MASK, \ |
| 152 | LOCAL_IP, LOCAL_IP_MASK, \ |
| 153 | REMOTE_LOW_LIMIT_PORT, \ |
| 154 | REMOTE_HIGH_LIMIT_PORT, \ |
| 155 | LOCAL_LOW_LIMIT_PORT, \ |
| 156 | LOCAL_HIGH_LIMIT_PORT, \ |
| 157 | PROTOCOL, PROTOCOL_MASK) |
| 158 | |
| 159 | # TBD: Need to handle exception |
| 160 | # Pack the structure and send over the zmq socket to DP |
| 161 | pub_socket.send("%s" % (struct.pack('!BBLLBI'+\ |
| 162 | str(len(RULE_STRING))+'s',topicId, MSG_TYPE,\ |
| 163 | PCC_RULE_ID, PRECEDENCE, RULE_TYPE, \ |
| 164 | len(RULE_STRING), RULE_STRING))) |
| 165 | time.sleep(1) |
| 166 | print "\nSDF Rule Values for %s :: \nPCC_RULE_ID :%s \ |
| 167 | \nPRECEDENCE :%s \nRULE_TYPE :%s \nRULE_STRING :%s"\ |
| 168 | % (val, PCC_RULE_ID, PRECEDENCE, RULE_TYPE, RULE_STRING) |
| 169 | print '\n ---># SDF Rule Successfully sent.. #<---\n' |
| 170 | parser.clear() |