Woojoong Kim | e30f606 | 2017-10-05 19:38:56 -0500 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 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 |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 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 implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | import os |
| 16 | import sys |
| 17 | from django.db.models import Q, F |
Woojoong Kim | e30f606 | 2017-10-05 19:38:56 -0500 | [diff] [blame] | 18 | from synchronizers.new_base.modelaccessor import * |
Woojoong Kim | 033b2ce | 2017-10-10 10:09:06 -0700 | [diff] [blame] | 19 | from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
Woojoong Kim | e30f606 | 2017-10-05 19:38:56 -0500 | [diff] [blame] | 20 | |
| 21 | parentdir = os.path.join(os.path.dirname(__file__), "..") |
| 22 | sys.path.insert(0, parentdir) |
| 23 | |
| 24 | class SyncVHSSTenant(SyncInstanceUsingAnsible): |
Woojoong Kim | 033b2ce | 2017-10-10 10:09:06 -0700 | [diff] [blame] | 25 | |
Woojoong Kim | e30f606 | 2017-10-05 19:38:56 -0500 | [diff] [blame] | 26 | provides = [VHSSTenant] |
| 27 | |
| 28 | observes = VHSSTenant |
| 29 | |
| 30 | requested_interval = 0 |
| 31 | |
| 32 | template_name = "vhsstenant_playbook.yaml" |
| 33 | |
| 34 | service_key_name = "/opt/xos/configurations/mcord/mcord_private_key" |
| 35 | |
Woojoong Kim | e30f606 | 2017-10-05 19:38:56 -0500 | [diff] [blame] | 36 | def __init__(self, *args, **kwargs): |
| 37 | super(SyncVHSSTenant, self).__init__(*args, **kwargs) |
| 38 | |
Andy Bavier | 03905bc | 2018-01-17 15:49:00 -0700 | [diff] [blame^] | 39 | def get_extra_attributes(self, o): |
| 40 | fields = {} |
Woojoong Kim | e30f606 | 2017-10-05 19:38:56 -0500 | [diff] [blame] | 41 | |
Andy Bavier | 03905bc | 2018-01-17 15:49:00 -0700 | [diff] [blame^] | 42 | fields['db_ip'] = self.get_ip_address_from_peer_service_instance('db_network', 'HSSDBServiceInstance', o, 'db_ip') |
| 43 | |
| 44 | return fields |
| 45 | |
| 46 | def get_my_ip_address(self, o, network_name, parameter=None): |
| 47 | return self.get_ip_address_from_peer_service_instance_instance(network_name, o, o, parameter) |
| 48 | |
| 49 | def get_ip_address_from_peer_service_instance(self, network_name, sitype, o, parameter=None): |
| 50 | peer_si = self.get_peer_serviceinstance_of_type(sitype, o) |
| 51 | return self.get_ip_address_from_peer_service_instance_instance(network_name, peer_si, o, parameter) |
| 52 | |
| 53 | def get_ip_address_from_peer_service_instance_instance(self, network_name, peer_si, o, parameter=None): |
| 54 | try: |
| 55 | net_id = self.get_network_id(network_name) |
| 56 | ins_id = peer_si.leaf_model.instance_id |
| 57 | ip_address = Port.objects.get( |
| 58 | network_id=net_id, instance_id=ins_id).ip |
| 59 | except Exception: |
| 60 | self.log.error("Failed to fetch parameter", |
| 61 | parameter=parameter, |
| 62 | network_name=network_name) |
| 63 | self.defer_sync(o, "Waiting for parameters to become available") |
| 64 | |
| 65 | return ip_address |
| 66 | |
| 67 | def get_peer_serviceinstance_of_type(self, sitype, o): |
| 68 | prov_link_set = ServiceInstanceLink.objects.filter( |
| 69 | subscriber_service_instance_id=o.id) |
| 70 | |
| 71 | try: |
| 72 | peer_service = next( |
| 73 | p.provider_service_instance for p in prov_link_set if p.provider_service_instance.leaf_model_name == sitype) |
| 74 | except StopIteration: |
| 75 | sub_link_set = ServiceInstanceLink.objects.filter( |
| 76 | provider_service_instance_id=o.id) |
| 77 | try: |
| 78 | peer_service = next( |
| 79 | s.subscriber_service_instance for s in sub_link_set if s.subscriber_service_instance.leaf_model_name == sitype) |
| 80 | except StopIteration: |
| 81 | self.log.error( |
| 82 | 'Could not find service type in service graph', service_type=sitype, object=o) |
| 83 | raise ServiceGraphException( |
| 84 | "Synchronization failed due to incomplete service graph") |
| 85 | |
| 86 | return peer_service |
| 87 | |
| 88 | # To get each network id |
| 89 | def get_network_id(self, network_name): |
| 90 | return Network.objects.get(name=network_name).id |