1 # Copyright (c) 2014 Rackspace Hosting
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 from ryu.controller import event as ryu_event
17 from ryu.controller import handler
20 class EventRowBase(ryu_event.EventBase):
21 def __init__(self, system_id, table, row, event_type):
22 super(EventRowBase, self).__init__()
23 self.system_id = system_id
26 self.event_type = event_type
29 return '%s<system_id=%s table=%s, uuid=%s>' % (self.__class__.__name__,
35 class EventRowDelete(EventRowBase):
36 def __init__(self, system_id, table, row):
37 super(EventRowDelete, self).__init__(system_id, table, row, 'Deleted')
40 class EventRowInsert(EventRowBase):
41 def __init__(self, system_id, table, row):
42 super(EventRowInsert, self).__init__(system_id, table, row, 'Inserted')
45 class EventRowUpdate(ryu_event.EventBase):
46 def __init__(self, system_id, table, old, new):
47 super(EventRowUpdate, self).__init__()
48 self.system_id = system_id
52 self.event_type = 'Updated'
55 return '%s<system_id=%s table=%s, uuid=%s>' % (self.__class__.__name__,
61 class EventModifyRequest(ryu_event.EventRequestBase):
62 """ Dispatch a modify function to OVSDB
64 `func` must be a callable that accepts an insert fucntion and the
65 IDL.tables object. It can then modify the tables as needed. For inserts,
66 specify a UUID for each insert, and return a tuple of the temporary
67 UUID's. The execution of `func` will be wrapped in a single transaction
68 and the reply will include a dict of temporary UUID to real UUID mappings.
72 new_port_uuid = uuid.uuid4()
74 def modify(tables, insert):
75 bridges = tables['Bridge'].rows
78 if b.name == 'my-bridge':
84 port = insert('Port', new_port_uuid)
86 bridge.ports = bridge.ports + [port]
88 return (new_port_uuid, )
90 request = EventModifyRequest(system_id, modify)
91 reply = send_request(request)
93 port_uuid = reply.insert_uuids[new_port_uuid]
96 def __init__(self, system_id, func):
97 super(EventModifyRequest, self).__init__()
99 self.system_id = system_id
103 return '%s<system_id=%s>' % (self.__class__.__name__, self.system_id)
106 class EventModifyReply(ryu_event.EventReplyBase):
107 def __init__(self, system_id, status, insert_uuids, err_msg):
108 self.system_id = system_id
110 self.insert_uuids = insert_uuids
111 self.err_msg = err_msg
114 return ('%s<system_id=%s, status=%s, insert_uuids=%s, error_msg=%s>'
115 % (self.__class__.__name__,
122 class EventNewOVSDBConnection(ryu_event.EventBase):
123 def __init__(self, client):
124 super(EventNewOVSDBConnection, self).__init__()
128 return '%s<system_id=%s>' % (self.__class__.__name__,
129 self.client.system_id)
133 return self.client.system_id
136 class EventReadRequest(ryu_event.EventRequestBase):
137 def __init__(self, system_id, func):
138 self.system_id = system_id
143 class EventReadReply(ryu_event.EventReplyBase):
144 def __init__(self, system_id, result, err_msg=''):
145 self.system_id = system_id
147 self.err_msg = err_msg
150 class EventRowInsertedBase(EventRowInsert):
151 def __init__(self, ev):
152 super(EventRowInsertedBase, self).__init__(ev.system_id,
157 class EventRowDeletedBase(EventRowDelete):
158 def __init__(self, ev):
159 super(EventRowDeletedBase, self).__init__(ev.system_id,
164 class EventRowUpdatedBase(EventRowUpdate):
165 def __init__(self, ev):
166 super(EventRowUpdatedBase, self).__init__(ev.system_id,
172 class EventPortInserted(EventRowInsertedBase):
176 class EventPortDeleted(EventRowDeletedBase):
180 class EventPortUpdated(EventRowUpdatedBase):
184 class EventInterfaceInserted(EventRowInsertedBase):
188 class EventInterfaceDeleted(EventRowDeletedBase):
192 class EventInterfaceUpdated(EventRowUpdatedBase):
196 handler.register_service('ryu.services.protocols.ovsdb.manager')