Skip to content
Snippets Groups Projects
Commit 3bef4907 authored by Chukwuemeka Michael Muonagor's avatar Chukwuemeka Michael Muonagor
Browse files

Update whatif_loop.py

parent 99211c3f
No related branches found
No related tags found
No related merge requests found
...@@ -6,6 +6,7 @@ import string ...@@ -6,6 +6,7 @@ import string
import pandas as pd import pandas as pd
import delete_intents_elasticsearch import delete_intents_elasticsearch
import config import config
import get_intents_script
port = config.port port = config.port
...@@ -13,10 +14,61 @@ elastic_host = config.elastic_host ...@@ -13,10 +14,61 @@ elastic_host = config.elastic_host
elastic_port = config.elastic_port elastic_port = config.elastic_port
parameters = config.parameters parameters = config.parameters
workflow_url = config.workflow_url workflow_url = config.workflow_url
stored_qos_intents_url = config.stored_qos_intents_url
elasticsearch_url = config.elasticsearch_url elasticsearch_url = config.elasticsearch_url
es = Elasticsearch(elasticsearch_url) es = Elasticsearch(elasticsearch_url)
def whatif_send_fun(policy_dict, whatif_send_url): def whatif_send_fun(policy_dict, whatif_send_url):
sent_whatif = []
# empty dict containing the elements of the what-if question which are the matched policy attributes
for i in range(len(policy_dict['host'])):
whatif_question = {}
id_digits = 9
whatif_id = ''.join(random.choices(string.ascii_uppercase +
string.digits, k=id_digits))
print('intent type is prevention, sending what-if question')
whatif_question['command'] = 'send_what_if'
whatif_question['intent_type'] = policy_dict['intent_type']
whatif_question['threat'] = policy_dict['threat']
#whatif_question['host'] = list(policy_dict['host'][i].split(" "))
whatif_question['host'] = policy_dict['host'][i]
whatif_question['action'] = policy_dict['action']
whatif_question['duration'] = str(policy_dict['duration'])
whatif_question['id'] = whatif_id
whatif_question['kpi_measured'] = policy_dict['kpi_measured']
whatif_question['prevention_host'] = policy_dict['prevention_host'][i]
#whatif_question['prevention_host'] = list(policy_dict['prevention_host'][i].split(" "))
#whatif_question['interface'] = policy_dict['interface'][i]
#whatif_question['host_references'] = policy_dict['host_references'][i]
intent_index = es.exists(index="awaiting_intents", id=1)
l = len(sent_whatif)
if l != 0 and whatif_question != sent_whatif[l - 1]:
sent_whatif.append(whatif_question)
if intent_index == True:
resp1 = es.search(index="awaiting_intents", size=100, query={"match_all": {}})
total = resp1['hits']['total']['value']
id = total + 1
es.index(index="awaiting_intents", id=id, document=whatif_question)
else:
es.index(index="awaiting_intents", id=1, document=whatif_question)
# send what-if question to the what_if_send_url
send_workflows.send_workflow_fun(whatif_send_url, whatif_question)
elif l == 0:
sent_whatif.append(whatif_question)
if intent_index == True:
resp1 = es.search(index="awaiting_intents", size=100, query={"match_all": {}})
total = resp1['hits']['total']['value']
id = total + 1
es.index(index="awaiting_intents", id=id, document=whatif_question)
else:
es.index(index="awaiting_intents", id=1, document=whatif_question)
# send what-if question to the what_if_send_url
send_workflows.send_workflow_fun(whatif_send_url, whatif_question)
time.sleep(1)
#return whatif_question
'''def whatif_send_fun(policy_dict, whatif_send_url):
sent_whatif = [] sent_whatif = []
# empty dict containing the elements of the what-if question which are the matched policy attributes # empty dict containing the elements of the what-if question which are the matched policy attributes
whatif_question = {} whatif_question = {}
...@@ -31,6 +83,11 @@ def whatif_send_fun(policy_dict, whatif_send_url): ...@@ -31,6 +83,11 @@ def whatif_send_fun(policy_dict, whatif_send_url):
whatif_question['action'] = policy_dict['action'] whatif_question['action'] = policy_dict['action']
whatif_question['duration'] = str(policy_dict['duration']) whatif_question['duration'] = str(policy_dict['duration'])
whatif_question['id'] = whatif_id whatif_question['id'] = whatif_id
whatif_question['kpi_measured'] = policy_dict['kpi_measured']
whatif_question['prevention_host'] = policy_dict['prevention_host']
whatif_question['interface'] = policy_dict['interface']
whatif_question['host_references'] = policy_dict['host_references']
l = len(sent_whatif) l = len(sent_whatif)
if l != 0 and whatif_question != sent_whatif[l-1]: if l != 0 and whatif_question != sent_whatif[l-1]:
sent_whatif.append(whatif_question) sent_whatif.append(whatif_question)
...@@ -40,7 +97,7 @@ def whatif_send_fun(policy_dict, whatif_send_url): ...@@ -40,7 +97,7 @@ def whatif_send_fun(policy_dict, whatif_send_url):
sent_whatif.append(whatif_question) sent_whatif.append(whatif_question)
# send what-if question to the what_if_send_url # send what-if question to the what_if_send_url
send_workflows.send_workflow_fun(whatif_send_url, whatif_question) send_workflows.send_workflow_fun(whatif_send_url, whatif_question)
return whatif_question return whatif_question'''
#the function runs through the awaiting intents elasticsearch index every 60 secs #the function runs through the awaiting intents elasticsearch index every 60 secs
#and sends what-if questions to the SAN for the hosts in the awaiting intents index #and sends what-if questions to the SAN for the hosts in the awaiting intents index
...@@ -64,10 +121,56 @@ def whatif_loop_fun(es, whatif_send_url): ...@@ -64,10 +121,56 @@ def whatif_loop_fun(es, whatif_send_url):
#when a what-if answer is received from the SAN #when a what-if answer is received from the SAN
#the IBI proceeds with the intent if the response from the SAN is acceptable #the IBI proceeds with the intent if the response from the SAN is acceptable
def whatif_receive_fun(whatif_receive): def whatif_receive_fun(whatif_receive):
global what_if_response
stored_qos_intents_arr = get_intents_script.get_intent_fun(stored_qos_intents_url)
bandwidth_unit_dict = {'bps': 1, 'kbps': 10^3, 'mbps': 10^6, 'gbps': 10^9}
latency_unit_dict = {'s': 1, 'ms': 10^(-3), 'μs': 10^(-6)}
reliability_unit_dict = {'1': 1, '%': 100}
reject_whatif = 0
for i in range(len(stored_qos_intents_arr)):
for j in range(len(whatif_receive.host)):
if (whatif_receive.host[j] == stored_qos_intents_arr[i]['host'] and
stored_qos_intents_arr[i]['name'] == whatif_receive.kpi_measured):
#and \ stored_qos_intents_arr[i]['intent_type'] == 'qos_ntp'):
print('conflict with qos intent')
#real_kpi_val = whatif_receive['value'] * whatif_receive['']
if (whatif_receive.kpi_measured == 'bandwidth'):
dict_key_list = list(bandwidth_unit_dict.keys())
for key in dict_key_list:
if (key == whatif_receive.unit):
whatif_kpi_val = float(whatif_receive.value) * bandwidth_unit_dict[key]
qos_kpi_val = stored_qos_intents_arr[i]['value'] * bandwidth_unit_dict[key]
if qos_kpi_val > whatif_kpi_val:
#what_if_response = 'ok'
reject_whatif += 1
elif (whatif_receive.kpi_measured == 'latency'):
dict_key_list = list(latency_unit_dict.keys())
for key in dict_key_list:
if (key == whatif_receive.unit):
whatif_kpi_val = float(whatif_receive.value) * latency_unit_dict[key]
qos_kpi_val = stored_qos_intents_arr[i]['value'] * latency_unit_dict[key]
if qos_kpi_val < whatif_kpi_val:
#what_if_response = 'ok'
reject_whatif += 1
elif (whatif_receive.kpi_measured == 'reliability'):
dict_key_list = list(reliability_unit_dict.keys())
for key in dict_key_list:
if (key == whatif_receive.unit):
whatif_kpi_val = float(whatif_receive.value) * reliability_unit_dict[key]
qos_kpi_val = stored_qos_intents_arr[i]['value'] * reliability_unit_dict[key]
if qos_kpi_val > whatif_kpi_val:
#what_if_response = 'ok'
reject_whatif += 1
if reject_whatif == 0:
what_if_response = 'ok'
else:
what_if_response = 'reject'
import policy_configurator import policy_configurator
stored_intents_url = config.stored_intents_url stored_intents_url = config.stored_intents_url
whatif_answer = {} whatif_answer = {}
if whatif_receive.what_if_response == 'ok': if what_if_response == 'ok':
intent_index = es.exists(index="awaiting_intents", id=1) intent_index = es.exists(index="awaiting_intents", id=1)
if intent_index == True: if intent_index == True:
resp1 = es.search(index="awaiting_intents", size=100, query={"match_all": {}}) resp1 = es.search(index="awaiting_intents", size=100, query={"match_all": {}})
...@@ -84,11 +187,11 @@ def whatif_receive_fun(whatif_receive): ...@@ -84,11 +187,11 @@ def whatif_receive_fun(whatif_receive):
whatif_answer['command'] = 'add' whatif_answer['command'] = 'add'
whatif_answer['intent_type'] = whatif_question['intent_type'] whatif_answer['intent_type'] = whatif_question['intent_type']
whatif_answer['threat'] = whatif_question['threat'] whatif_answer['threat'] = whatif_question['threat']
whatif_answer['host'] = whatif_question['host'] whatif_answer['host'] = list(whatif_question['host'].split(" "))
whatif_answer['action'] = whatif_question['action'] whatif_answer['action'] = whatif_question['action']
whatif_answer['duration'] = whatif_question['duration'] whatif_answer['duration'] = whatif_question['duration']
whatif_answer['id'] = whatif_receive.id whatif_answer['id'] = whatif_receive.id
whatif_answer['what_if_response'] = whatif_receive.what_if_response whatif_answer['what_if_response'] = what_if_response
df_policy = pd.read_csv(config.policy_store_directory) df_policy = pd.read_csv(config.policy_store_directory)
for ind in df_policy.index: for ind in df_policy.index:
if df_policy['action'][ind] == whatif_answer['action']: if df_policy['action'][ind] == whatif_answer['action']:
...@@ -101,7 +204,7 @@ def whatif_receive_fun(whatif_receive): ...@@ -101,7 +204,7 @@ def whatif_receive_fun(whatif_receive):
for ind in range(len(resp['hits']['hits'])): for ind in range(len(resp['hits']['hits'])):
hit1 = resp['hits']['hits'][ind]['_source'] hit1 = resp['hits']['hits'][ind]['_source']
if hit1['intent_type'] == whatif_answer['intent_type'] and hit1['threat'] == whatif_answer['threat'] and \ if hit1['intent_type'] == whatif_answer['intent_type'] and hit1['threat'] == whatif_answer['threat'] and \
str(hit1['host']) == str(whatif_answer['host']) and hit1['action'] == whatif_answer['action'] and \ str(hit1['host']) == str(whatif_answer['host'][0]) and hit1['action'] == whatif_answer['action'] and \
str(hit1['duration']) == str(whatif_answer['duration']) and hit1['id'] == whatif_answer['id']: str(hit1['duration']) == str(whatif_answer['duration']) and hit1['id'] == whatif_answer['id']:
delete_intents_elasticsearch.delete_intents_elasticsearch_fun(elasticsearch_url, resp['hits']['hits'][ind]['_id'], delete_intents_elasticsearch.delete_intents_elasticsearch_fun(elasticsearch_url, resp['hits']['hits'][ind]['_id'],
"awaiting_intents") "awaiting_intents")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment