mirror of
https://github.com/LibreQoE/LibreQoS.git
synced 2024-11-22 08:16:25 -06:00
Update integrationSplynx.py
This commit is contained in:
parent
352bafce96
commit
35381756a0
@ -14,20 +14,26 @@ if find_ipv6_using_mikrotik() == True:
|
|||||||
from integrationCommon import NetworkGraph, NetworkNode, NodeType
|
from integrationCommon import NetworkGraph, NetworkNode, NodeType
|
||||||
|
|
||||||
def buildHeaders():
|
def buildHeaders():
|
||||||
|
"""
|
||||||
|
Build authorization headers for Splynx API requests using API key and secret.
|
||||||
|
"""
|
||||||
credentials = splynx_api_key() + ':' + splynx_api_secret()
|
credentials = splynx_api_key() + ':' + splynx_api_secret()
|
||||||
credentials = base64.b64encode(credentials.encode()).decode()
|
credentials = base64.b64encode(credentials.encode()).decode()
|
||||||
return {'Authorization': "Basic %s" % credentials}
|
return {'Authorization': "Basic %s" % credentials}
|
||||||
|
|
||||||
def spylnxRequest(target, headers):
|
def spylnxRequest(target, headers):
|
||||||
# Sends a REST GET request to Spylnx and returns the
|
"""
|
||||||
# result in JSON
|
Send a GET request to the Splynx API and return the JSON response.
|
||||||
|
"""
|
||||||
url = splynx_api_url() + "/api/2.0/" + target
|
url = splynx_api_url() + "/api/2.0/" + target
|
||||||
r = requests.get(url, headers=headers, timeout=120)
|
r = requests.get(url, headers=headers, timeout=120)
|
||||||
return r.json()
|
return r.json()
|
||||||
|
|
||||||
def getTariffs(headers):
|
def getTariffs(headers):
|
||||||
|
"""
|
||||||
|
Retrieve tariff data from Splynx API and calculate download/upload speeds for each tariff.
|
||||||
|
"""
|
||||||
data = spylnxRequest("admin/tariffs/internet", headers)
|
data = spylnxRequest("admin/tariffs/internet", headers)
|
||||||
tariff = []
|
|
||||||
downloadForTariffID = {}
|
downloadForTariffID = {}
|
||||||
uploadForTariffID = {}
|
uploadForTariffID = {}
|
||||||
for tariff in data:
|
for tariff in data:
|
||||||
@ -36,10 +42,12 @@ def getTariffs(headers):
|
|||||||
speed_upload = round((int(tariff['speed_upload']) / 1000))
|
speed_upload = round((int(tariff['speed_upload']) / 1000))
|
||||||
downloadForTariffID[tariffID] = speed_download
|
downloadForTariffID[tariffID] = speed_download
|
||||||
uploadForTariffID[tariffID] = speed_upload
|
uploadForTariffID[tariffID] = speed_upload
|
||||||
return (tariff, downloadForTariffID, uploadForTariffID)
|
return (data, downloadForTariffID, uploadForTariffID)
|
||||||
|
|
||||||
def buildSiteBandwidths():
|
def buildSiteBandwidths():
|
||||||
# Builds a dictionary of site bandwidths from the integrationSplynxBandwidths.csv file.
|
"""
|
||||||
|
Build a dictionary of site bandwidths by reading data from a CSV file.
|
||||||
|
"""
|
||||||
siteBandwidth = {}
|
siteBandwidth = {}
|
||||||
if os.path.isfile("integrationSplynxBandwidths.csv"):
|
if os.path.isfile("integrationSplynxBandwidths.csv"):
|
||||||
with open('integrationSplynxBandwidths.csv') as csv_file:
|
with open('integrationSplynxBandwidths.csv') as csv_file:
|
||||||
@ -53,19 +61,21 @@ def buildSiteBandwidths():
|
|||||||
return siteBandwidth
|
return siteBandwidth
|
||||||
|
|
||||||
def getCustomers(headers):
|
def getCustomers(headers):
|
||||||
data = spylnxRequest("admin/customers/customer", headers)
|
"""
|
||||||
#addressForCustomerID = {}
|
Retrieve all customer data from Splynx API.
|
||||||
#customerIDs = []
|
"""
|
||||||
#for customer in data:
|
return spylnxRequest("admin/customers/customer", headers)
|
||||||
# customerIDs.append(customer['id'])
|
|
||||||
# addressForCustomerID[customer['id']] = customer['street_1']
|
|
||||||
return data
|
|
||||||
|
|
||||||
def getCustomersOnline(headers):
|
def getCustomersOnline(headers):
|
||||||
data = spylnxRequest("admin/customers/customers-online", headers)
|
"""
|
||||||
return data
|
Retrieve data of currently online customers from Splynx API.
|
||||||
|
"""
|
||||||
|
return spylnxRequest("admin/customers/customers-online", headers)
|
||||||
|
|
||||||
def getRouters(headers):
|
def getRouters(headers):
|
||||||
|
"""
|
||||||
|
Retrieve router data from Splynx API and build dictionaries for router IPs and names.
|
||||||
|
"""
|
||||||
data = spylnxRequest("admin/networking/routers", headers)
|
data = spylnxRequest("admin/networking/routers", headers)
|
||||||
routerIdList = []
|
routerIdList = []
|
||||||
ipForRouter = {}
|
ipForRouter = {}
|
||||||
@ -80,6 +90,9 @@ def getRouters(headers):
|
|||||||
return (ipForRouter, nameForRouterID, routerIdList)
|
return (ipForRouter, nameForRouterID, routerIdList)
|
||||||
|
|
||||||
def getSectors(headers):
|
def getSectors(headers):
|
||||||
|
"""
|
||||||
|
Retrieve sector data from Splynx API and build a dictionary mapping routers to their sectors.
|
||||||
|
"""
|
||||||
data = spylnxRequest("admin/networking/routers-sectors", headers)
|
data = spylnxRequest("admin/networking/routers-sectors", headers)
|
||||||
sectorForRouter = {}
|
sectorForRouter = {}
|
||||||
for sector in data:
|
for sector in data:
|
||||||
@ -97,18 +110,24 @@ def getSectors(headers):
|
|||||||
return sectorForRouter
|
return sectorForRouter
|
||||||
|
|
||||||
def combineAddress(json):
|
def combineAddress(json):
|
||||||
# Combines address fields into a single string
|
"""
|
||||||
# The API docs seem to indicate that there isn't a "state" field?
|
Combine address fields into a single string. If address fields are empty, use ID and name.
|
||||||
|
"""
|
||||||
if json["street_1"] == "" and json["city"] == "" and json["zip_code"] == "":
|
if json["street_1"] == "" and json["city"] == "" and json["zip_code"] == "":
|
||||||
return str(json["id"]) + "/" + json["name"]
|
return str(json["id"]) + "/" + json["name"]
|
||||||
else:
|
else:
|
||||||
return json["street_1"] + " " + json["city"] + " " + json["zip_code"]
|
return json["street_1"] + " " + json["city"] + " " + json["zip_code"]
|
||||||
|
|
||||||
def getAllServices(headers):
|
def getAllServices(headers):
|
||||||
services = spylnxRequest("admin/customers/customer/0/internet-services?main_attributes%5Bstatus%5D=active", headers)
|
"""
|
||||||
return services
|
Retrieve all active internet services from Splynx API.
|
||||||
|
"""
|
||||||
|
return spylnxRequest("admin/customers/customer/0/internet-services?main_attributes%5Bstatus%5D=active", headers)
|
||||||
|
|
||||||
def getAllIPs(headers):
|
def getAllIPs(headers):
|
||||||
|
"""
|
||||||
|
Retrieve all used IPv4 and IPv6 addresses from Splynx API and map them to customer IDs.
|
||||||
|
"""
|
||||||
ipv4ByCustomerID = {}
|
ipv4ByCustomerID = {}
|
||||||
ipv6ByCustomerID = {}
|
ipv6ByCustomerID = {}
|
||||||
allIPv4 = spylnxRequest("admin/networking/ipv4-ip?main_attributes%5Bis_used%5D=1", headers)
|
allIPv4 = spylnxRequest("admin/networking/ipv4-ip?main_attributes%5Bis_used%5D=1", headers)
|
||||||
@ -129,6 +148,9 @@ def getAllIPs(headers):
|
|||||||
return (ipv4ByCustomerID, ipv6ByCustomerID)
|
return (ipv4ByCustomerID, ipv6ByCustomerID)
|
||||||
|
|
||||||
def createShaper():
|
def createShaper():
|
||||||
|
"""
|
||||||
|
Main function to fetch data from Splynx, build the network graph, and shape devices.
|
||||||
|
"""
|
||||||
net = NetworkGraph()
|
net = NetworkGraph()
|
||||||
|
|
||||||
print("Fetching data from Spylnx")
|
print("Fetching data from Spylnx")
|
||||||
@ -142,10 +164,11 @@ def createShaper():
|
|||||||
ipv4ByCustomerID, ipv6ByCustomerID = getAllIPs(headers)
|
ipv4ByCustomerID, ipv6ByCustomerID = getAllIPs(headers)
|
||||||
siteBandwidth = buildSiteBandwidths()
|
siteBandwidth = buildSiteBandwidths()
|
||||||
|
|
||||||
#Go through all online customers, create Nodes
|
|
||||||
allParentNodes = []
|
allParentNodes = []
|
||||||
custIDtoParentNode = {}
|
custIDtoParentNode = {}
|
||||||
parentNodeIDCounter = 30000
|
parentNodeIDCounter = 30000
|
||||||
|
|
||||||
|
# Create nodes for sites and assign bandwidth
|
||||||
for customer in customersOnline:
|
for customer in customersOnline:
|
||||||
download = 1000
|
download = 1000
|
||||||
upload = 1000
|
upload = 1000
|
||||||
@ -153,7 +176,6 @@ def createShaper():
|
|||||||
|
|
||||||
if nodeName not in allParentNodes:
|
if nodeName not in allParentNodes:
|
||||||
if nodeName in siteBandwidth:
|
if nodeName in siteBandwidth:
|
||||||
# Use the CSV bandwidth values
|
|
||||||
download = siteBandwidth[nodeName]["download"]
|
download = siteBandwidth[nodeName]["download"]
|
||||||
upload = siteBandwidth[nodeName]["upload"]
|
upload = siteBandwidth[nodeName]["upload"]
|
||||||
|
|
||||||
@ -168,19 +190,16 @@ def createShaper():
|
|||||||
|
|
||||||
parentNodeIDCounter += 1
|
parentNodeIDCounter += 1
|
||||||
|
|
||||||
#customerIDtoParentNodeID = {}
|
|
||||||
allServicesDict = {}
|
allServicesDict = {}
|
||||||
for serviceItem in allServices:
|
for serviceItem in allServices:
|
||||||
if (serviceItem['status'] == 'active'):
|
if serviceItem['status'] == 'active':
|
||||||
if serviceItem["customer_id"] not in allServicesDict:
|
if serviceItem["customer_id"] not in allServicesDict:
|
||||||
allServicesDict[serviceItem["customer_id"]] = []
|
allServicesDict[serviceItem["customer_id"]] = []
|
||||||
temp = allServicesDict[serviceItem["customer_id"]]
|
temp = allServicesDict[serviceItem["customer_id"]]
|
||||||
temp.append(serviceItem)
|
temp.append(serviceItem)
|
||||||
allServicesDict[serviceItem["customer_id"]] = temp
|
allServicesDict[serviceItem["customer_id"]] = temp
|
||||||
|
|
||||||
#It's not very clear how a service is meant to handle multiple
|
# Create nodes for customers and their devices
|
||||||
#devices on a shared tariff. Creating each service as a combined
|
|
||||||
#entity including the customer, to be on the safe side.
|
|
||||||
for customerJson in customers:
|
for customerJson in customers:
|
||||||
if customerJson['status'] == 'active':
|
if customerJson['status'] == 'active':
|
||||||
if customerJson['id'] in allServicesDict:
|
if customerJson['id'] in allServicesDict:
|
||||||
@ -201,37 +220,12 @@ def createShaper():
|
|||||||
address=combineAddress(customerJson),
|
address=combineAddress(customerJson),
|
||||||
customerName=customerJson["name"],
|
customerName=customerJson["name"],
|
||||||
download=downloadForTariffID[tariff_id],
|
download=downloadForTariffID[tariff_id],
|
||||||
upload=uploadForTariffID[tariff_id],
|
upload=uploadForTariffID[tariff_id]
|
||||||
)
|
)
|
||||||
net.addRawNode(customer)
|
net.addRawNode(customer)
|
||||||
|
|
||||||
ipv4 = []
|
ipv4 = ipv4ByCustomerID.get(customerJson["id"], [])
|
||||||
ipv6 = []
|
ipv6 = ipv6ByCustomerID.get(customerJson["id"], [])
|
||||||
routerID = service['router_id']
|
|
||||||
|
|
||||||
# If not "Taking IPv4" (Router will assign IP), then use router's set IP
|
|
||||||
taking_ipv4 = int(service['taking_ipv4'])
|
|
||||||
if taking_ipv4 == 0:
|
|
||||||
if routerID in ipForRouter:
|
|
||||||
ipv4 = [ipForRouter[routerID]]
|
|
||||||
|
|
||||||
elif taking_ipv4 == 1:
|
|
||||||
ipv4 = [service['ipv4']]
|
|
||||||
if len(ipv4) == 0:
|
|
||||||
#Only do this if single service for a customer
|
|
||||||
if len(servicesForCustomer) == 1:
|
|
||||||
if customerJson['id'] in ipv4ByCustomerID:
|
|
||||||
ipv4 = ipv4ByCustomerID[customerJson['id']]
|
|
||||||
|
|
||||||
# If not "Taking IPv6" (Router will assign IP), then use router's set IP
|
|
||||||
if isinstance(service['taking_ipv6'], str):
|
|
||||||
taking_ipv6 = int(service['taking_ipv6'])
|
|
||||||
else:
|
|
||||||
taking_ipv6 = service['taking_ipv6']
|
|
||||||
if taking_ipv6 == 0:
|
|
||||||
ipv6 = []
|
|
||||||
elif taking_ipv6 == 1:
|
|
||||||
ipv6 = [service['ipv6']]
|
|
||||||
|
|
||||||
device = NetworkNode(
|
device = NetworkNode(
|
||||||
id=combinedId + "_d" + str(service["id"]),
|
id=combinedId + "_d" + str(service["id"]),
|
||||||
@ -253,7 +247,9 @@ def createShaper():
|
|||||||
net.createShapedDevices()
|
net.createShapedDevices()
|
||||||
|
|
||||||
def importFromSplynx():
|
def importFromSplynx():
|
||||||
#createNetworkJSON()
|
"""
|
||||||
|
Entry point for the script to initiate the Splynx data import and shaper creation process.
|
||||||
|
"""
|
||||||
createShaper()
|
createShaper()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
Reference in New Issue
Block a user