blob: de37814bf08b62a6e09a56261fdc63f9bf32235e [file] [log] [blame]
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -07001import os
2import sys
3import requests
4import json
5import urllib
Scott Baker1c1c44e2017-03-14 10:19:19 -07006from synchronizers.new_base.syncstep import SyncStep
7from synchronizers.new_base.modelaccessor import *
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -07008from xos.logger import Logger, logging
9
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070010from requests.auth import HTTPBasicAuth
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070011logger = Logger(level=logging.INFO)
12
13
14class SyncVRouterPort(SyncStep):
15 provides = [VRouterPort]
16
17 observes = VRouterPort
18
19 requested_interval = 0
20
21 def __init__(self, *args, **kwargs):
22 super(SyncVRouterPort, self).__init__(*args, **kwargs)
23
24 def get_onos_fabric_addr(self, app):
25 vrouter_service = VRouterService.objects.get(id=app.vrouter_service_id)
26
27 return "http://%s:%s/onos/v1/network/configuration/" % (vrouter_service.rest_hostname, vrouter_service.rest_port)
28
29 def get_onos_fabric_auth(self, app):
30 vrouter_service = VRouterService.objects.get(id=app.vrouter_service_id)
31
32 return HTTPBasicAuth(vrouter_service.rest_user, vrouter_service.rest_pass)
33
34 def sync_record(self, port):
35
36 logger.info("Sync'ing Edited vRouterPort: %s" % port.name)
37
38 # NOTE port is now related to service,
39 # probably it makes more sense to relate them to a device (and device is related to service)
40 onos_addr = self.get_onos_fabric_addr(port)
41
42 # NOTE
43 # from a port read all interfaces
44 # from interfaces read all ips
45
46 ifaces = []
47 for interface in port.interfaces.all():
48 iface = {
49 'name': interface.name,
50 'mac': interface.mac,
51 'vlan': interface.vlan,
52 'ips': []
53 }
54
55 for ip in interface.ips.all():
56 iface["ips"].append(ip.ip)
57
58 ifaces.append(iface)
59
60 data = {}
61 data[port.openflow_id] = {
62 'interfaces': ifaces
63 }
64
65 url = onos_addr + "ports/"
66
67 print "POST %s for port %s" % (url, port.name)
68
69 auth = self.get_onos_fabric_auth(port)
70 r = requests.post(url, data=json.dumps(data), auth=auth)
71 if (r.status_code != 200):
72 print r
73 raise Exception("Received error from vrouter port update (%d)" % r.status_code)
74
75 def delete_record(self, port):
76
77 logger.info("Sync'ing Deleted vRouterPort: %s" % port.name)
78
79 onos_addr = self.get_onos_fabric_addr()
80
81 url = onos_addr + "ports/" + urllib.quote(port.openflow_id, safe='') + "/"
82
83 print "DELETE %s for port %s" % (url, port.name)
84
85 auth = self.get_onos_fabric_auth(port)
86 r = requests.delete(url, auth=auth)
87 if (r.status_code != 204):
88 print r
89 raise Exception("Received error from vrouter port deletion (%d)" % r.status_code)