blob: fe8eb1c88db812b5027ed98580384700a3a2dbfc [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
21import urllib
Scott Baker1c1c44e2017-03-14 10:19:19 -070022from synchronizers.new_base.syncstep import SyncStep
23from synchronizers.new_base.modelaccessor import *
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070024from xos.logger import Logger, logging
25
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070026from requests.auth import HTTPBasicAuth
Matteo Scandoloa4e6e9a2016-08-23 12:04:45 -070027logger = Logger(level=logging.INFO)
28
29
30class 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)