blob: 891bfef6127736ee3f90e8f6f39f9860ec5b54d1 [file] [log] [blame]
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -07001import os
2import sys
3import requests
4import json
Scott Baker1c1c44e2017-03-14 10:19:19 -07005from synchronizers.new_base.syncstep import SyncStep
6from synchronizers.new_base.modelaccessor import *
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -07007from xos.logger import Logger, logging
8
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -07009from requests.auth import HTTPBasicAuth
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070010logger = Logger(level=logging.INFO)
11
12
13class SyncVRouterApp(SyncStep):
14 provides = [VRouterApp]
15
16 observes = VRouterApp
17
18 requested_interval = 0
19
20 def __init__(self, *args, **kwargs):
21 super(SyncVRouterApp, self).__init__(*args, **kwargs)
22
23 def get_onos_fabric_addr(self, app):
24 vrouter_service = VRouterService.objects.get(id=app.vrouter_service_id)
25
26 return "http://%s:%s/onos/v1/network/configuration/" % (vrouter_service.rest_hostname, vrouter_service.rest_port)
27
28 def get_onos_fabric_auth(self, app):
29 vrouter_service = VRouterService.objects.get(id=app.vrouter_service_id)
30
31 return HTTPBasicAuth(vrouter_service.rest_user, vrouter_service.rest_pass)
32
33 def sync_record(self, app):
34
35 logger.info("Sync'ing Edited vRouterApps: %s" % app.name)
36
37 onos_addr = self.get_onos_fabric_addr(app)
38
39 data = {}
40 data["controlPlaneConnectPoint"] = app.control_plane_connect_point
41 data["ospfEnabled"] = app.ospf_enabled
42 data["interfaces"] = app.interfaces
43
44 url = onos_addr + "apps/" + app.name + "/router/"
45
46 print "POST %s for app %s" % (url, app.name)
47
48 # XXX fixme - hardcoded auth
49 auth = self.get_onos_fabric_auth(app)
50 r = requests.post(url, data=json.dumps(data), auth=auth)
51 if (r.status_code != 200):
52 print r
53 raise Exception("Received error from vrouter app update (%d)" % r.status_code)
54
55 def delete_record(self, app):
56
57 logger.info("Sync'ing Deleted vRouterApps: %s" % app.name)
58
59 onos_addr = self.get_onos_fabric_addr()
60
61 url = onos_addr + "apps/" + app.name + "/"
62
63 print "DELETE %s for app %s" % (url, app.name)
64
65 auth = self.get_onos_fabric_auth(app)
66 r = requests.delete(url, auth=auth)
67 if (r.status_code != 204):
68 print r
69 raise Exception("Received error from vrouter app deletion (%d)" % r.status_code)