9 from datetime import datetime
12 """ gives 1 chance out of n (default: 2) to return True """
14 return bool(random.randint(0, n-1))
15 def now_dmaap_timestamp():
16 return str(datetime.now().timestamp()).replace(".","")[:13]
17 def now_notification_time():
18 return datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f+00:00")
20 CONTROL_LOOP_NAMES = [
21 'ClosedLoop-vUSP-SIG-d925ed73-8231-4d02-9545-db4e101f88f8',
22 'ClosedLoop-vUSP-SIG-37b1c91e-fd6b-4abd-af15-771902d5fdb1',
23 'ClosedLoop-vUSP-SIG-c2597657-7113-4efb-a1f9-397a7a24c3e1',
24 'ClosedLoop-vUSP-SIG-a11318ba-4c61-46b8-a9da-bd40bec11d45',
25 'ClosedLoop-vUSP-SIG-5321c558-2254-4efb-ac24-99dd54edc94f',
29 'event_abated' :'event_abated.json',
30 'event_onset' :'event_onset.json',
31 'notification_active' :'notification_active.json',
32 'notification_final_failed' :'notification_final_failed.json',
33 'notification_final_open' :'notification_final_open.json',
34 'notification_final_success' :'notification_final_success.json',
35 'notification_operation_failure' :'notification_operation_failure.json',
36 'notification_operation' :'notification_operation.json',
37 'notification_operation_success' :'notification_operation_success.json',
38 'notification_rejected_disabled' :'notification_rejected_disabled.json',
39 'notification_rejected_missing' :'notification_rejected_missing.json',
43 with open(TEMPLATES[key]) as f:
45 TEMPLATES[key] = json.loads(content)
48 class DMaaPMessage(dict):
50 dmaap_host_url = "http://dmaap.host.url:9200/"
55 def from_template(cls, tmpl, **kwargs):
57 obj.update(copy.deepcopy(TEMPLATES[tmpl]))
58 for keys,value in kwargs.items():
60 keys = keys.split(".")
62 for i in range(len(keys) - 1):
63 current_node = current_node[keys[i]]
65 current_node[key] = value
68 def publish(self, topic):
69 url = "%s/events/%s" % (self.dmaap_host_url, topic)
71 if self.dmaap_username and self.dmaap_password:
72 auth = (self.dmaap_username, self.dmaap_password)
73 response = requests.post(url, data=json.dumps(self), auth=auth)
74 return response.status_code
76 class Event(DMaaPMessage):
78 topic = "DCAE-CL-EVENT"
82 return Event.from_template('event_abated', **kwargs)
86 return Event.from_template('event_onset', **kwargs)
89 return super().publish(self.topic)
92 class Notification(DMaaPMessage):
94 topic = "POLICY-CL-MGT"
97 def from_template(cls, tmpl, **kwargs):
98 kwargs['notificationTime'] = now_notification_time()
99 return super().from_template(tmpl, **kwargs)
102 def active(**kwargs):
103 return Notification.from_template('notification_active', **kwargs)
107 class FinalNotification(Notification):
109 def success(**kwargs):
110 return FinalNotification.from_template('notification_final_success', **kwargs)
112 def failed(**kwargs):
113 return FinalNotification.from_template('notification_final_failed', **kwargs)
116 return FinalNotification.from_template('notification_final_open', **kwargs)
117 return FinalNotification
120 def operation(**kwargs):
121 class OperationNotification(Notification):
123 def success(**kwargs):
124 return OperationNotification.from_template('notification_operation_success', **kwargs)
126 def failure(**kwargs):
127 return OperationNotification.from_template('notification_operation_failure', **kwargs)
128 return OperationNotification.from_template('notification_operation', **kwargs)
131 def rejected(**kwargs):
132 class RejectedNotification(Notification):
134 def disabled(**kwargs):
135 return RejectedNotification.from_template('notification_rejected_disabled', **kwargs)
137 def missing_fields(**kwargs):
138 return RejectedNotification.from_template('notification_rejected_missing', **kwargs)
140 return RejectedNotification
143 return super().publish(self.topic)
147 class CLStatus(object):
149 def __init__(self, dmaap_url=None,
150 missing=None, disabled=None, op_failure=None):
151 self._stopped = False
156 self._missing = maybe(missing)
157 self._disabled = maybe(disabled)
158 self._op_failure = maybe(op_failure)
160 requestID=str(uuid.uuid4()),
161 closedLoopControlName=CONTROL_LOOP_NAMES[random.randint(0, len(CONTROL_LOOP_NAMES) - 1)]
169 raise StopIteration()
170 config = self._config
171 config.update(dict(closedLoopAlarmStart=now_dmaap_timestamp()))
172 yield Event.onset(**config)
175 yield Notification.rejected().missing_fields(**config)
176 raise StopIteration()
179 yield Notification.rejected().disabled(**config)
180 raise StopIteration()
182 yield Notification.active(**config)
183 yield Notification.operation(**config)
185 config['closedLoopAlarmEnd'] = now_dmaap_timestamp()
187 yield Notification.operation().failure(**config)
189 yield Notification.final().failed(**config)
191 yield Notification.operation().success(**config)
192 yield Event.abated(**config)
194 yield Notification.final().success(**config)
195 raise StopIteration()
197 DMaaPMessage.dmaap_host_url = "http://uebsb93kcdc.it.att.com:3904"
198 Event.topic = "APPC-TEST-AMDOCS1-1607-E2E"
199 Notification.topic = "APPC-TEST-AMDOCS1-1607-IST"
200 # Request.topic = "APPC-TEST-AMDOCS1-1607-RY303T"
202 test_datas = [CLStatus(missing=False, disabled=False, op_failure=False) for i in range(45)] \
203 + [CLStatus(missing=True, disabled=False, op_failure=False) for i in range(5)] \
204 + [CLStatus(missing=False, disabled=True, op_failure=False) for i in range(6)] \
205 + [CLStatus(missing=False, disabled=False, op_failure=True) for i in range(7)]
206 random.shuffle(test_datas)
207 random.shuffle(test_datas)
209 for current_i, status in enumerate(test_datas):
210 time.sleep(random.randint(0,6))
213 status_code = s.publish()
214 if status_code != 200:
215 print("Error when publishing : status_code={}".format(status_code))
217 time.sleep(random.randint(0,3))
218 print("%03d,missing:%5s,disabled:%5s,op_failure:%5s - %s" % (current_i, status._missing, status._disabled, status._op_failure, status._config))