rdudyala | b086cf3 | 2016-08-11 00:07:45 -0400 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | import socket |
| 3 | from oslo_utils import units |
| 4 | from oslo_utils import netutils |
| 5 | import kafka |
| 6 | import kafka_broker |
| 7 | import fnmatch |
| 8 | import logging |
| 9 | import copy |
| 10 | |
| 11 | sub_info=[] |
| 12 | class subinfo: |
| 13 | def __init__(self,scheme,app_id,app_ip,app_port,subscription_info,sub_info_filter,target): |
| 14 | logging.debug("* Updating subscription_info ") |
| 15 | self.scheme = scheme |
| 16 | self.app_id = app_id |
| 17 | self.ipaddress = app_ip |
| 18 | self.portno = app_port |
| 19 | self.subscription_info = subscription_info |
| 20 | self.sub_info_filter = sub_info_filter |
| 21 | self.target = target |
| 22 | |
| 23 | if scheme == "kafka": |
| 24 | ''' Creating kafka publisher to send message over kafka ''' |
| 25 | parse_target = netutils.urlsplit(target) |
| 26 | self.kafka_publisher = kafka_broker.KafkaBrokerPublisher(parse_target) |
| 27 | elif scheme == "udp": |
| 28 | ''' Creating UDP socket to send message over UDP ''' |
| 29 | self.udp = socket.socket(socket.AF_INET, # Internet |
| 30 | socket.SOCK_DGRAM) # UDP |
| 31 | self.udp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 32 | |
| 33 | def update_subinfo(self): |
| 34 | logging.info("* inside %s",self.update_subinfo.__name__) |
| 35 | if not sub_info: |
| 36 | logging.debug("* -----------List is EMpty -------------") |
| 37 | sub_info.append(self) |
| 38 | logging.debug("* Subscription is sucessful") |
| 39 | return "Subscription is sucessful \n" |
| 40 | for obj in sub_info: |
| 41 | if obj.app_id == self.app_id : |
| 42 | # obj.subscription_info=self.subscription_info |
| 43 | sub_info.remove(obj) |
| 44 | sub_info.append(self) |
| 45 | logging.warning("* entry already exists so overwriting this subscription \n") |
| 46 | return "entry already exists so overwriting this subscription \n" |
| 47 | sub_info.append(self) |
| 48 | return "Subscription is sucessful \n" |
| 49 | |
| 50 | @staticmethod |
| 51 | def delete_subinfo(app_id): |
| 52 | logging.info("* inside %s",subinfo.delete_subinfo.__name__) |
| 53 | Flag = False |
| 54 | for obj in sub_info: |
| 55 | if obj.app_id == app_id : |
| 56 | sub_info.remove(obj) |
| 57 | Flag = True |
| 58 | logging.debug("* Un-Subscription is sucessful") |
| 59 | return "Un-Subscription is sucessful \n" |
| 60 | if not Flag : |
| 61 | err_str = "No subscription exists with app id: " + app_id + "\n" |
| 62 | logging.error("* No subscription exists with app id:%s ",app_id) |
| 63 | raise Exception (err_str) |
| 64 | |
| 65 | @staticmethod |
| 66 | def print_subinfo(): |
| 67 | logging.info("* inside %s",subinfo.print_subinfo.__name__) |
| 68 | for obj in sub_info: |
| 69 | logging.debug("* ------------------------------------------------") |
| 70 | logging.debug("* scheme:%s",obj.scheme) |
| 71 | logging.debug("* app_id:%s",obj.app_id) |
| 72 | logging.debug("* portno:%s",obj.portno ) |
| 73 | logging.debug("* ipaddress:%s",obj.ipaddress) |
| 74 | logging.debug("* subscription_info:%s",obj.subscription_info) |
| 75 | logging.debug("* sub_info_filter:%s",obj.sub_info_filter) |
| 76 | logging.debug("* target:%s",obj.target) |
| 77 | logging.debug("* ------------------------------------------------") |
| 78 | @staticmethod |
| 79 | def get_subinfo(app_id): |
| 80 | logging.info("* inside %s",subinfo.get_subinfo.__name__) |
| 81 | Flag = False |
| 82 | for obj in sub_info: |
| 83 | if obj.app_id == app_id : |
| 84 | return obj.subscription_info,obj.target |
| 85 | return (None,None) |
| 86 | |
| 87 | |
| 88 | @staticmethod |
| 89 | def get_sub_list(notif_subscription_info): |
| 90 | logging.info("* inside %s",subinfo.get_sublist.__name__) |
| 91 | sub_list=[] |
| 92 | for obj in sub_info: |
| 93 | if obj.subscription_info == notif_subscription_info: |
| 94 | sub_list.append(obj) |
| 95 | return sub_list |