Matteo Scandolo | d02b73b | 2017-08-08 13:05:26 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 17 | import os |
| 18 | import sys |
| 19 | import requests |
| 20 | import json |
Scott Baker | 1c1c44e | 2017-03-14 10:19:19 -0700 | [diff] [blame] | 21 | from synchronizers.new_base.syncstep import SyncStep |
| 22 | from synchronizers.new_base.modelaccessor import * |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 23 | from xos.logger import Logger, logging |
| 24 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 25 | from requests.auth import HTTPBasicAuth |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 26 | logger = Logger(level=logging.INFO) |
| 27 | |
| 28 | |
| 29 | class SyncVRouterApp(SyncStep): |
| 30 | provides = [VRouterApp] |
| 31 | |
| 32 | observes = VRouterApp |
| 33 | |
| 34 | requested_interval = 0 |
| 35 | |
| 36 | def __init__(self, *args, **kwargs): |
| 37 | super(SyncVRouterApp, self).__init__(*args, **kwargs) |
| 38 | |
| 39 | def get_onos_fabric_addr(self, app): |
| 40 | vrouter_service = VRouterService.objects.get(id=app.vrouter_service_id) |
| 41 | |
| 42 | return "http://%s:%s/onos/v1/network/configuration/" % (vrouter_service.rest_hostname, vrouter_service.rest_port) |
| 43 | |
| 44 | def get_onos_fabric_auth(self, app): |
| 45 | vrouter_service = VRouterService.objects.get(id=app.vrouter_service_id) |
| 46 | |
| 47 | return HTTPBasicAuth(vrouter_service.rest_user, vrouter_service.rest_pass) |
| 48 | |
| 49 | def sync_record(self, app): |
| 50 | |
| 51 | logger.info("Sync'ing Edited vRouterApps: %s" % app.name) |
| 52 | |
| 53 | onos_addr = self.get_onos_fabric_addr(app) |
| 54 | |
| 55 | data = {} |
| 56 | data["controlPlaneConnectPoint"] = app.control_plane_connect_point |
| 57 | data["ospfEnabled"] = app.ospf_enabled |
| 58 | data["interfaces"] = app.interfaces |
| 59 | |
| 60 | url = onos_addr + "apps/" + app.name + "/router/" |
| 61 | |
| 62 | print "POST %s for app %s" % (url, app.name) |
| 63 | |
| 64 | # XXX fixme - hardcoded auth |
| 65 | auth = self.get_onos_fabric_auth(app) |
| 66 | r = requests.post(url, data=json.dumps(data), auth=auth) |
| 67 | if (r.status_code != 200): |
| 68 | print r |
| 69 | raise Exception("Received error from vrouter app update (%d)" % r.status_code) |
| 70 | |
| 71 | def delete_record(self, app): |
| 72 | |
| 73 | logger.info("Sync'ing Deleted vRouterApps: %s" % app.name) |
| 74 | |
| 75 | onos_addr = self.get_onos_fabric_addr() |
| 76 | |
| 77 | url = onos_addr + "apps/" + app.name + "/" |
| 78 | |
| 79 | print "DELETE %s for app %s" % (url, app.name) |
| 80 | |
| 81 | auth = self.get_onos_fabric_auth(app) |
| 82 | r = requests.delete(url, auth=auth) |
| 83 | if (r.status_code != 204): |
| 84 | print r |
| 85 | raise Exception("Received error from vrouter app deletion (%d)" % r.status_code) |