blob: 36e00db417657a34a12680f83311ac10405e840f [file] [log] [blame]
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -07001import os
2import sys
3import requests
4import json
5from django.db.models import Q, F
6from services.vrouter.models import *
7from synchronizers.base.syncstep import SyncStep
8from xos.logger import Logger, logging
9
10# from core.models import Service
11from requests.auth import HTTPBasicAuth
12
13parentdir = os.path.join(os.path.dirname(__file__), "..")
14sys.path.insert(0, parentdir)
15
16logger = Logger(level=logging.INFO)
17
18
19class SyncVRouterDevice(SyncStep):
20 provides = [VRouterDevice]
21
22 observes = VRouterDevice
23
24 requested_interval = 0
25
26 def __init__(self, *args, **kwargs):
27 super(SyncVRouterDevice, self).__init__(*args, **kwargs)
28
29 def get_onos_fabric_addr(self, app):
30 vrouter_service = VRouterService.objects.get(id=app.vrouter_service_id)
31
32 return "http://%s:%s/onos/v1/network/configuration/" % (vrouter_service.rest_hostname, vrouter_service.rest_port)
33
34 def get_onos_fabric_auth(self, app):
35 vrouter_service = VRouterService.objects.get(id=app.vrouter_service_id)
36
37 return HTTPBasicAuth(vrouter_service.rest_user, vrouter_service.rest_pass)
38
39 def sync_record(self, device):
40
41 logger.info("Sync'ing Edited vRouterDevice: %s" % device.name)
42
43 onos_addr = self.get_onos_fabric_addr(device)
44
45 data = {}
46 data["driver"] = device.driver
47
48 url = onos_addr + "devices/" + device.openflow_id + "/" + device.config_key + "/"
49
50 print "POST %s for device %s" % (url, device.name)
51
52 auth = self.get_onos_fabric_auth(device)
53 r = requests.post(url, data=json.dumps(data), auth=auth)
54 if (r.status_code != 200):
55 print r
56 raise Exception("Received error from vrouter device update (%d)" % r.status_code)
57
58 def delete_record(self, device):
59
60 logger.info("Sync'ing Deleted vRouterDevice: %s" % device.name)
61
62 onos_addr = self.get_onos_fabric_addr()
63
64 url = onos_addr + "devices/" + device.openflow_id + "/"
65
66 print "DELETE %s for device %s" % (url, device.name)
67
68 auth = self.get_onos_fabric_auth(device)
69 r = requests.delete(url, auth=auth)
70 if (r.status_code != 204):
71 print r
72 raise Exception("Received error from vrouter device deletion (%d)" % r.status_code)