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 |
| 21 | import urllib |
Scott Baker | 1c1c44e | 2017-03-14 10:19:19 -0700 | [diff] [blame] | 22 | from synchronizers.new_base.syncstep import SyncStep |
| 23 | from synchronizers.new_base.modelaccessor import * |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 24 | from xos.logger import Logger, logging |
| 25 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 26 | from requests.auth import HTTPBasicAuth |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 27 | logger = Logger(level=logging.INFO) |
| 28 | |
| 29 | |
| 30 | class SyncVRouterPort(SyncStep): |
| 31 | provides = [VRouterPort] |
| 32 | |
| 33 | observes = VRouterPort |
| 34 | |
| 35 | requested_interval = 0 |
| 36 | |
| 37 | def __init__(self, *args, **kwargs): |
| 38 | super(SyncVRouterPort, self).__init__(*args, **kwargs) |
| 39 | |
| 40 | def get_onos_fabric_addr(self, app): |
| 41 | vrouter_service = VRouterService.objects.get(id=app.vrouter_service_id) |
| 42 | |
| 43 | return "http://%s:%s/onos/v1/network/configuration/" % (vrouter_service.rest_hostname, vrouter_service.rest_port) |
| 44 | |
| 45 | def get_onos_fabric_auth(self, app): |
| 46 | vrouter_service = VRouterService.objects.get(id=app.vrouter_service_id) |
| 47 | |
| 48 | return HTTPBasicAuth(vrouter_service.rest_user, vrouter_service.rest_pass) |
| 49 | |
| 50 | def sync_record(self, port): |
| 51 | |
| 52 | logger.info("Sync'ing Edited vRouterPort: %s" % port.name) |
| 53 | |
| 54 | # NOTE port is now related to service, |
| 55 | # probably it makes more sense to relate them to a device (and device is related to service) |
| 56 | onos_addr = self.get_onos_fabric_addr(port) |
| 57 | |
| 58 | # NOTE |
| 59 | # from a port read all interfaces |
| 60 | # from interfaces read all ips |
| 61 | |
| 62 | ifaces = [] |
| 63 | for interface in port.interfaces.all(): |
| 64 | iface = { |
| 65 | 'name': interface.name, |
| 66 | 'mac': interface.mac, |
| 67 | 'vlan': interface.vlan, |
| 68 | 'ips': [] |
| 69 | } |
| 70 | |
| 71 | for ip in interface.ips.all(): |
| 72 | iface["ips"].append(ip.ip) |
| 73 | |
| 74 | ifaces.append(iface) |
| 75 | |
| 76 | data = {} |
| 77 | data[port.openflow_id] = { |
| 78 | 'interfaces': ifaces |
| 79 | } |
| 80 | |
| 81 | url = onos_addr + "ports/" |
| 82 | |
| 83 | print "POST %s for port %s" % (url, port.name) |
| 84 | |
| 85 | auth = self.get_onos_fabric_auth(port) |
| 86 | r = requests.post(url, data=json.dumps(data), auth=auth) |
| 87 | if (r.status_code != 200): |
| 88 | print r |
| 89 | raise Exception("Received error from vrouter port update (%d)" % r.status_code) |
| 90 | |
| 91 | def delete_record(self, port): |
| 92 | |
| 93 | logger.info("Sync'ing Deleted vRouterPort: %s" % port.name) |
| 94 | |
| 95 | onos_addr = self.get_onos_fabric_addr() |
| 96 | |
| 97 | url = onos_addr + "ports/" + urllib.quote(port.openflow_id, safe='') + "/" |
| 98 | |
| 99 | print "DELETE %s for port %s" % (url, port.name) |
| 100 | |
| 101 | auth = self.get_onos_fabric_auth(port) |
| 102 | r = requests.delete(url, auth=auth) |
| 103 | if (r.status_code != 204): |
| 104 | print r |
| 105 | raise Exception("Received error from vrouter port deletion (%d)" % r.status_code) |