Scott Baker | 031a043 | 2018-04-02 15:01:15 -0700 | [diff] [blame^] | 1 | |
| 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 | |
| 17 | from synchronizers.new_base.modelaccessor import VSGHWServiceInstance, model_accessor |
| 18 | from synchronizers.new_base.policy import Policy |
| 19 | from synchronizers.new_base.exceptions import * |
| 20 | |
| 21 | from xosconfig import Config |
| 22 | from multistructlog import create_logger |
| 23 | |
| 24 | log = create_logger(Config().get('logging')) |
| 25 | |
| 26 | class VSGHWServiceInstancePolicy(Policy): |
| 27 | model_name = "VSGHWServiceInstance" |
| 28 | |
| 29 | def handle_create(self, service_instance): |
| 30 | return self.handle_update(service_instance) |
| 31 | |
| 32 | def handle_update(self, service_instance): |
| 33 | log.info("Handle_update VSG-HW Service Instance", service_instance=service_instance) |
| 34 | if (service_instance.link_deleted_count>0) and (not service_instance.provided_links.exists()): |
| 35 | # if the last provided_link has just gone away, then self-destruct |
| 36 | self.logger.info("The last provided link has been deleted -- self-destructing.") |
| 37 | # TODO: We shouldn't have to call handle_delete ourselves. The model policy framework should handle this |
| 38 | # for us, but it isn't. I think that's happening is that serviceinstance.delete() isn't setting a new |
| 39 | # updated timestamp, since there's no way to pass `always_update_timestamp`, and therefore the |
| 40 | # policy framework doesn't know that the object has changed and needs new policies. For now, the |
| 41 | # workaround is to just call handle_delete ourselves. |
| 42 | self.handle_delete(service_instance) |
| 43 | # Note that if we deleted the Instance in handle_delete, then django may have cascade-deleted the service |
| 44 | # instance by now. Thus we have to guard our delete, to check that the service instance still exists. |
| 45 | if VSGHWServiceInstance.objects.filter(id=service_instance.id).exists(): |
| 46 | service_instance.delete() |
| 47 | else: |
| 48 | self.logger.info("Tenant %s is already deleted" % service_instance) |
| 49 | return |
| 50 | |
| 51 | def handle_delete(self, service_instance): |
| 52 | log.info("Handle_delete VSG-HW Service Instance", service_instance=service_instance) |
| 53 | |
| 54 | |