Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import requests |
| 4 | import json |
Scott Baker | 1c1c44e | 2017-03-14 10:19:19 -0700 | [diff] [blame^] | 5 | from synchronizers.new_base.syncstep import SyncStep |
| 6 | from synchronizers.new_base.modelaccessor import * |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 7 | from xos.logger import Logger, logging |
| 8 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 9 | from requests.auth import HTTPBasicAuth |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 10 | logger = Logger(level=logging.INFO) |
| 11 | |
| 12 | |
| 13 | class 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) |