blob: 179ec91d2f2fe3ba6906c9ccb536edf76bff8fb8 [file] [log] [blame]
Matteo Scandolod02b73b2017-08-08 13:05:26 -07001
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 Scandoloa4e6e9a2016-08-23 12:04:45 -070017import os
18import sys
19import requests
20import json
Scott Baker1c1c44e2017-03-14 10:19:19 -070021from synchronizers.new_base.syncstep import SyncStep
22from synchronizers.new_base.modelaccessor import *
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070023from xos.logger import Logger, logging
24
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070025from requests.auth import HTTPBasicAuth
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070026logger = Logger(level=logging.INFO)
27
28
29class SyncVRouterDevice(SyncStep):
30 provides = [VRouterDevice]
31
32 observes = VRouterDevice
33
34 requested_interval = 0
35
36 def __init__(self, *args, **kwargs):
37 super(SyncVRouterDevice, 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, device):
50
51 logger.info("Sync'ing Edited vRouterDevice: %s" % device.name)
52
53 onos_addr = self.get_onos_fabric_addr(device)
54
55 data = {}
56 data["driver"] = device.driver
57
58 url = onos_addr + "devices/" + device.openflow_id + "/" + device.config_key + "/"
59
60 print "POST %s for device %s" % (url, device.name)
61
62 auth = self.get_onos_fabric_auth(device)
63 r = requests.post(url, data=json.dumps(data), auth=auth)
64 if (r.status_code != 200):
65 print r
66 raise Exception("Received error from vrouter device update (%d)" % r.status_code)
67
68 def delete_record(self, device):
69
70 logger.info("Sync'ing Deleted vRouterDevice: %s" % device.name)
71
72 onos_addr = self.get_onos_fabric_addr()
73
74 url = onos_addr + "devices/" + device.openflow_id + "/"
75
76 print "DELETE %s for device %s" % (url, device.name)
77
78 auth = self.get_onos_fabric_auth(device)
79 r = requests.delete(url, auth=auth)
80 if (r.status_code != 204):
81 print r
82 raise Exception("Received error from vrouter device deletion (%d)" % r.status_code)