Scott Baker | a6c687c | 2018-07-16 15:08:49 -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 | |
Scott Baker | 8256547 | 2018-08-20 11:40:03 -0700 | [diff] [blame^] | 17 | from synchronizers.new_base.modelaccessor import FabricCrossconnectServiceInstance, ServiceInstance, model_accessor |
Scott Baker | a6c687c | 2018-07-16 15:08:49 -0700 | [diff] [blame] | 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 FabricCrossconnectServiceInstancePolicy(Policy): |
| 27 | model_name = "FabricCrossconnectServiceInstance" |
| 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 Fabric Crossconnect Service Instance", service_instance=service_instance) |
| 34 | |
| 35 | if (service_instance.link_deleted_count > 0) and (not service_instance.provided_links.exists()): |
| 36 | # If this instance has no links pointing to it, delete |
| 37 | self.handle_delete(service_instance) |
| 38 | if FabricCrossconnectServiceInstance.objects.filter(id=service_instance.id).exists(): |
| 39 | service_instance.delete() |
| 40 | return |
| 41 | |
Scott Baker | 8256547 | 2018-08-20 11:40:03 -0700 | [diff] [blame^] | 42 | # If there is a westbound link, then make sure the SerivceInstance is consistent with the |
| 43 | # westbound fields. |
| 44 | |
| 45 | if service_instance.provided_links.exists(): |
| 46 | updated_fields = [] |
| 47 | |
| 48 | si = ServiceInstance.objects.get(id=service_instance.id) |
| 49 | s_tag = si.get_westbound_service_instance_properties("s_tag") |
| 50 | switch_datapath_id = si.get_westbound_service_instance_properties("switch_datapath_id") |
| 51 | source_port = si.get_westbound_service_instance_properties("switch_port") |
| 52 | |
| 53 | if (s_tag is None): |
| 54 | raise Exception("Westbound ServiceInstance s-tag is None on fcsi %s" % service_instance.id) |
| 55 | |
| 56 | if (not switch_datapath_id): |
| 57 | raise Exception("Westbound ServiceInstance switch_datapath_id is unset on fcsi %s" % service_instance.id) |
| 58 | |
| 59 | if (source_port is None): |
| 60 | raise Exception("Westbound ServiceInstance switch_port is None on fcsi %s" % service_instance.id) |
| 61 | |
| 62 | s_tag = int(s_tag) |
| 63 | source_port = int(source_port) |
| 64 | |
| 65 | if (s_tag != service_instance.s_tag): |
| 66 | if service_instance.s_tag is not None: |
| 67 | raise Exception("Westbound ServiceInstance changing s-tag is not currently permitted") |
| 68 | service_instance.s_tag = s_tag |
| 69 | updated_fields.append("s_tag") |
| 70 | if (switch_datapath_id != service_instance.switch_datapath_id): |
| 71 | if service_instance.switch_datapath_id: |
| 72 | raise Exception("Westbound ServiceInstance changing switch_datapath_id is not currently permitted") |
| 73 | service_instance.switch_datapath_id = switch_datapath_id |
| 74 | updated_fields.append("switch_datapath_id") |
| 75 | if (source_port != service_instance.source_port): |
| 76 | if service_instance.source_port is not None: |
| 77 | raise Exception("Westbound ServiceInstance changing source_port is not currently permitted") |
| 78 | service_instance.source_port = source_port |
| 79 | updated_fields.append("source_port") |
| 80 | |
| 81 | if updated_fields: |
| 82 | service_instance.save(update_fields = updated_fields) |
| 83 | |
Scott Baker | a6c687c | 2018-07-16 15:08:49 -0700 | [diff] [blame] | 84 | def handle_delete(self, service_instance): |
| 85 | log.info("Handle_delete Fabric-Crossconnect Service Instance", service_instance=service_instance) |
| 86 | |
| 87 | |