JianHao | adbe03c | 2017-10-06 00:52:44 +0800 | [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 |
| 18 | from synchronizers.new_base.modelaccessor import * |
| 19 | from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 20 | |
| 21 | parentdir = os.path.join(os.path.dirname(__file__), "..") |
| 22 | sys.path.insert(0, parentdir) |
| 23 | |
| 24 | class SyncVSGWCTenant(SyncInstanceUsingAnsible): |
| 25 | provides = [VSGWCTenant] |
| 26 | |
| 27 | observes = VSGWCTenant |
| 28 | |
| 29 | requested_interval = 0 |
| 30 | |
| 31 | template_name = "vsgwctenant_playbook.yaml" |
| 32 | |
| 33 | service_key_name = "/opt/xos/configurations/mcord/mcord_private_key" |
| 34 | |
| 35 | def __init__(self, *args, **kwargs): |
| 36 | super(SyncVSGWCTenant, self).__init__(*args, **kwargs) |
| 37 | |
| 38 | # def fetch_pending(self, deleted): |
| 39 | # if (not deleted): |
| 40 | # objs = VSGWCTenant.get_tenant_objects().filter( |
| 41 | # Q(enacted__lt=F('updated')) | Q(enacted=None), Q(lazy_blocked=False)) |
| 42 | # else: |
| 43 | # # If this is a deletion we get all of the deleted tenants.. |
| 44 | # objs = VSGWCTenant.get_deleted_tenant_objects() |
| 45 | # |
| 46 | # return objs |
| 47 | |
| 48 | # Gets the attributes that are used by the Ansible template but are not |
| 49 | # part of the set of default attribtues. |
| 50 | def get_extra_attributes(self, o): |
| 51 | fields = {} |
| 52 | shared_net_id = Network.objects.get(name='shared_network').id |
| 53 | |
| 54 | try: |
| 55 | fields['sgwc_shared_ip'] = Port.objects.get(network_id=shared_net_id, instance_id=o.instance_id).ip |
| 56 | except Exception: |
| 57 | print '{} does not have an instance'.format(o.name) |
| 58 | |
| 59 | try: |
| 60 | mme = TenantWithContainer.objects.get(provider_service_id=Service.objects.get(name='vmme').id, subscriber_tenant_id=o.subscriber_tenant_id) |
| 61 | fields['mme_shared_ip'] = Port.objects.get(network_id=shared_net_id, instance_id=mme.instance_id).ip |
| 62 | except Exception: |
| 63 | print '{} does not have a VMME instance'.format(o.subscriber_tenant.name) |
| 64 | |
| 65 | try: |
| 66 | sgwu = TenantWithContainer.objects.get(provider_service_id=Service.objects.get(name='vsgwu').id, subscriber_tenant_id=o.subscriber_tenant_id) |
| 67 | fields['sgwu_shared_ip'] = Port.objects.get(network_id=shared_net_id, instance_id=sgwu.instance_id).ip |
| 68 | except Exception: |
| 69 | print '{} does not have a VSGWU instance'.format(o.subscriber_tenant.name) |
| 70 | |
| 71 | return fields |