# coding=utf-8
import httplib2, time, json
INTERVAL = 10
class OdlUtil:
url = ''
def __init__(self, addr):
self.url = addr
def get_load(self,sw , port, container_name='default',username="admin", password="admin"):
http = httplib2.Http()
http.add_credentials(username, password)
headers = {'Accept': 'application/json'}
flow_name = 'flow_' + str(int(time.time()*1000))
#h1h8body1 = r'{"flow": [{"id": "1","match": {"ethernet-match":{"ethernet-type": {"type": "2048"}},"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.2/32"},"instructions": {"instruction": [{"order": "0","apply-actions": {"action": [{"output-action": {"output-node-connector": "4"},"order": "0"}]}}]},"priority": "1111","cookie": "1","table_id": "0"}]}'
headers = {'Content-type': 'application/json'}
response, content = http.request(uri='http://127.0.0.1:8181/restconf/operational/opendaylight-inventory:nodes/node/openflow:%d/node-connector/openflow:%d:%d'%(sw,sw,port), method='GET',headers=headers)
#print (content)
data = json.loads(content)
load = data["node-connector"][0]["opendaylight-port-statistics:flow-capable-node-connector-statistics"]["bytes"]["received"]
return load
class Spy_port:
def __init__(self):
self.last_cost = 0
self.cost = 0
self.has_links = []
class Util:
# init
def __init__(self):
# start
print("start\n")
# init odl
self.odl = OdlUtil('http://127.0.0.1:8181')
# init info
self.all_ports = {}
self.all_ports["port-1"] = Spy_port()
self.all_ports["port-1"].has_links = [5]
self.all_ports["port-2"] = Spy_port()
self.all_ports["port-2"].has_links = [4]
self.all_ports["port-3"] = Spy_port()
self.all_ports["port-3"].has_links = [6]
self.all_ports["port-4"] = Server_port()
def balance(self):
sw = 1
while True:
time.sleep(1)
for i in range(1, 5):
index = "port-" + str(i)
if self.all_ports[index].last_cost == 0:
self.all_ports[index].last_cost = self.odl.get_load(sw, i)
print "init port " + str(i)
continue
last = self.all_ports[index].last_cost
now = self.odl.get_load(sw, i)
cost = (now - last)
self.all_ports[index].cost = cost
#if now - last == 0:
#print "=="
#continue
print "port " + str(i) + ":" + str(cost)
self.all_ports[index].last_cost = now
print "\n\n"
util = Util()
util.balance()