2222from oslo_log import log
2323
2424from neutron .common .ovn import constants as ovn_const
25+ from neutron .common import utils as n_utils
2526from neutron .db import db_base_plugin_common
2627from neutron .db import ovn_revision_numbers_db as db_rev
2728from neutron .objects import ports as port_obj
2829from neutron .services .trunk .drivers import base as trunk_base
30+ from neutron .services .trunk import exceptions as trunk_exc
2931
3032
3133SUPPORTED_INTERFACES = (
@@ -162,6 +164,10 @@ def _unset_binding_profile(self, context, subport, ovn_txn):
162164 LOG .debug ("Done unsetting parent for subport %s" , subport .port_id )
163165 return db_port
164166
167+ @staticmethod
168+ def _is_port_bound (port ):
169+ return n_utils .is_port_bound (port , log_message = False )
170+
165171 def trunk_updated (self , trunk ):
166172 # Check if parent port is handled by OVN.
167173 if not self .plugin_driver .nb_ovn .lookup ('Logical_Switch_Port' ,
@@ -208,6 +214,16 @@ def trunk_event(self, resource, event, trunk_plugin, payload):
208214 self .trunk_updated (payload .states [0 ])
209215 elif event == events .AFTER_DELETE :
210216 self .trunk_deleted (payload .states [0 ])
217+ elif event == events .PRECOMMIT_CREATE :
218+ trunk = payload .desired_state
219+ parent_port = trunk .db_obj .port
220+ if self ._is_port_bound (parent_port ):
221+ raise trunk_exc .ParentPortInUse (port_id = parent_port .id )
222+ elif event == events .PRECOMMIT_DELETE :
223+ trunk = payload .states [0 ]
224+ parent_port = payload .states [1 ]
225+ if self ._is_port_bound (parent_port ):
226+ raise trunk_exc .TrunkInUse (trunk_id = trunk .id )
211227
212228 def subport_event (self , resource , event , trunk_plugin , payload ):
213229 if event == events .AFTER_CREATE :
@@ -233,7 +249,8 @@ def register(self, resource, event, trigger, payload=None):
233249 resource , event , trigger , payload = payload )
234250 self ._handler = OVNTrunkHandler (self .plugin_driver )
235251 for _event in (events .AFTER_CREATE , events .AFTER_UPDATE ,
236- events .AFTER_DELETE ):
252+ events .AFTER_DELETE , events .PRECOMMIT_CREATE ,
253+ events .PRECOMMIT_DELETE ):
237254 registry .subscribe (self ._handler .trunk_event ,
238255 resources .TRUNK ,
239256 _event )
0 commit comments