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 : adc_rules.py |
| 12 | # |
| 13 | # Comments : |
| 14 | # Read ADC Rules config file from './config/' and get parameters |
| 15 | # values as per the ADC Rule Table. |
| 16 | # As per ADC Rule table, formed a structure and parse values in struct, |
| 17 | # and finally pack a struct and send over the zmq(PUB/SUB) socket to DP. |
| 18 | # |
| 19 | # Reference : message_sdn_dp.docx |
| 20 | # Section : Table No.11 ADC Rule |
| 21 | ############################################################################## |
| 22 | import sys |
| 23 | import os |
| 24 | import time |
| 25 | import struct |
| 26 | import socket |
| 27 | |
| 28 | from configparser import ConfigParser |
| 29 | |
| 30 | parser = ConfigParser() |
| 31 | |
| 32 | # Covert the string into num and stored into usigned 32 bit integer. |
| 33 | def name_to_num(name): |
| 34 | num = 0 |
| 35 | for ch in name[::-1]: |
| 36 | num = (num << 4) | (ord(ch) - ord('a')) |
| 37 | return num & 0xffffffff |
| 38 | |
| 39 | def parse_adc_values(pub_socket,topicId): |
| 40 | parser.read('./config/adc_rules.cfg') |
| 41 | print "\n ---> Reading Values from ADC rules file <---" |
| 42 | print "\n ---> Sending ADC rules <---" |
| 43 | MSG_TYPE = 17 |
| 44 | |
| 45 | # Create a structure for ADC rule and parse values in that. |
| 46 | for val in parser.sections(): |
| 47 | if val != 'GLOBAL': |
| 48 | # TBD: Need to handle exception |
| 49 | ADC_TYPE = int(parser.get(val, 'ADC_TYPE')) |
| 50 | if ADC_TYPE == 0: |
| 51 | DOMAIN = str(parser.get(val, 'DOMAIN')) |
| 52 | if ADC_TYPE == 1: |
| 53 | IP = struct.unpack('!L', \ |
| 54 | socket.inet_aton(str(parser.get(val, \ |
| 55 | 'IP')))) |
| 56 | if ADC_TYPE == 2: |
| 57 | IP = struct.unpack('!L', \ |
| 58 | socket.inet_aton(str(parser.get(val, \ |
| 59 | 'IP')))) |
| 60 | PREFIX = int(parser.get(val, 'PREFIX')) |
| 61 | |
| 62 | GATE_STATUS = int(parser.get(val, 'GATE_STATUS')) |
| 63 | RATING_GROUP = name_to_num(str(parser.get(val, \ |
| 64 | 'RATING_GROUP'))) |
| 65 | SERVICE_ID = name_to_num(str(parser.get(val, \ |
| 66 | 'SERVICE_ID'))) |
| 67 | PRECEDENCE = int(parser.get(val, 'PRECEDENCE')) |
| 68 | MTR_PROFILE_INDEX = int(parser.get(val, \ |
| 69 | 'MTR_PROFILE_INDEX')) |
| 70 | SPONSOR = str(parser.get(val, 'SPONSOR')) |
| 71 | |
| 72 | #TBD:: Need to handle exception |
| 73 | # Pack the structure and send over the zmq socket to DP |
| 74 | time.sleep(1) |
| 75 | if ADC_TYPE == 0: |
| 76 | pub_socket.send("%s" % (struct.pack('!BBBB'+ \ |
| 77 | str(len(DOMAIN))+'sBLLLHB'+\ |
| 78 | str(len(SPONSOR))+'s', \ |
| 79 | topicId, MSG_TYPE, ADC_TYPE, \ |
| 80 | len(DOMAIN), DOMAIN, \ |
| 81 | GATE_STATUS, RATING_GROUP, \ |
| 82 | SERVICE_ID, PRECEDENCE, \ |
| 83 | MTR_PROFILE_INDEX, \ |
| 84 | len(SPONSOR), SPONSOR))) |
| 85 | time.sleep(1) |
| 86 | |
| 87 | print "\nADC Rule Values for ::\ |
| 88 | \nADC_TYPE :%s \nDOMAIN :%s\ |
| 89 | \nGATE_STATUS :%s\ |
| 90 | \nrating group :%s \ |
| 91 | \nSERVICE_ID :%s \ |
| 92 | \nPRECEDENCE :%s \ |
| 93 | \nMTR_PROFILE_INDEX :%s\ |
| 94 | \nSPONSOR_LEN :%s\ |
| 95 | \nSPONSOR :%s" % \ |
| 96 | (ADC_TYPE, DOMAIN, GATE_STATUS,\ |
| 97 | RATING_GROUP, SERVICE_ID, \ |
| 98 | PRECEDENCE, MTR_PROFILE_INDEX, \ |
| 99 | len(SPONSOR), SPONSOR) |
| 100 | |
| 101 | if ADC_TYPE == 1: |
| 102 | pub_socket.send("%s" % (struct.pack('!BBBLBLLLHB'+\ |
| 103 | str(len(SPONSOR))+'s',topicId, \ |
| 104 | MSG_TYPE, ADC_TYPE, IP[0], \ |
| 105 | GATE_STATUS, RATING_GROUP, \ |
| 106 | SERVICE_ID, PRECEDENCE, \ |
| 107 | MTR_PROFILE_INDEX, \ |
| 108 | len(SPONSOR), SPONSOR))) |
| 109 | |
| 110 | print "\nADC Rule Values for ::\ |
| 111 | \nADC_TYPE :%s \nIP :%s \ |
| 112 | \nGATE_STATUS :%s\ |
| 113 | \nRATING_GROUP :%s\ |
| 114 | \nSERVICE_ID :%s\ |
| 115 | \nPRECEDENCE :%s\ |
| 116 | \nMTR_PROFILE_INDEX :%s\ |
| 117 | \nSPONSOR_LEN :%s \ |
| 118 | \nSPONSOR :%s" % \ |
| 119 | (ADC_TYPE, IP[0], GATE_STATUS,\ |
| 120 | RATING_GROUP, SERVICE_ID, \ |
| 121 | PRECEDENCE, MTR_PROFILE_INDEX, \ |
| 122 | len(SPONSOR), SPONSOR) |
| 123 | |
| 124 | if ADC_TYPE == 2: |
| 125 | pub_socket.send("%s" % (struct.pack('!BBBLHBL\ |
| 126 | LLHB'+str(len(SPONSOR))+'s', \ |
| 127 | topicId, MSG_TYPE, ADC_TYPE, \ |
| 128 | IP[0], PREFIX, GATE_STATUS, \ |
| 129 | RATING_GROUP, SERVICE_ID, \ |
| 130 | PRECEDENCE, MTR_PROFILE_INDEX, \ |
| 131 | len(SPONSOR), SPONSOR))) |
| 132 | |
| 133 | print "\nADC Rule Values for ::\ |
| 134 | \nADC_TYPE :%s \nIP :%s \ |
| 135 | \nPREFIX :%s\nGATE_STATUS :%s \ |
| 136 | \nRATING_GROUP:%s \ |
| 137 | \nSERVICE_ID :%s\ |
| 138 | \nPRECEDENCE :%s \ |
| 139 | \nMTR_PROFILE_INDEX :%s\ |
| 140 | \nSPONSOR_LEN :%s\ |
| 141 | \nSPONSOR :%s"\ |
| 142 | % (ADC_TYPE, IP[0], \ |
| 143 | PREFIX, GATE_STATUS, \ |
| 144 | RATING_GROUP, SERVICE_ID, \ |
| 145 | PRECEDENCE, MTR_PROFILE_INDEX, \ |
| 146 | len(SPONSOR), SPONSOR) |
| 147 | |
| 148 | |
| 149 | time.sleep(1) |
| 150 | print '\n ---># ADC Rule Successfully sent...#<---\n' |
| 151 | parser.clear() |
| 152 | |