JianHao | 90e65dc | 2017-10-31 18:01:07 +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 SyncVENBServiceInstance(SyncInstanceUsingAnsible): |
JianHao | 90e65dc | 2017-10-31 18:01:07 +0800 | [diff] [blame] | 25 | observes = VENBServiceInstance |
JianHao | 90e65dc | 2017-10-31 18:01:07 +0800 | [diff] [blame] | 26 | template_name = "venbserviceinstance_playbook.yaml" |
JianHao | 90e65dc | 2017-10-31 18:01:07 +0800 | [diff] [blame] | 27 | service_key_name = "/opt/xos/configurations/mcord/mcord_private_key" |
| 28 | |
| 29 | def __init__(self, *args, **kwargs): |
| 30 | super(SyncVENBServiceInstance, self).__init__(*args, **kwargs) |
| 31 | |
Andy Bavier | d1bfbd8 | 2017-11-14 17:26:18 -0700 | [diff] [blame] | 32 | def get_service(self, o): |
| 33 | if not o.owner: |
| 34 | return None |
| 35 | |
| 36 | service = VENBService.objects.filter(id=o.owner.id) |
| 37 | |
| 38 | if not service: |
| 39 | return None |
| 40 | |
| 41 | return service[0] |
| 42 | |
JianHao | 90e65dc | 2017-10-31 18:01:07 +0800 | [diff] [blame] | 43 | def get_extra_attributes(self, o): |
| 44 | |
| 45 | fields = {} |
Sapan Bhatia | e89039d | 2017-11-22 10:59:19 -0500 | [diff] [blame] | 46 | fields['flat_ip'] = self.get_ip_address('flat_network', o, 'get_venb_flat_ip') |
Andy Bavier | d1bfbd8 | 2017-11-14 17:26:18 -0700 | [diff] [blame] | 47 | service = self.get_service(o) |
| 48 | fields['login_user'] = service.login_user |
| 49 | fields['login_password'] = service.login_password |
Andy Bavier | a42532a | 2017-11-15 14:45:13 -0700 | [diff] [blame] | 50 | fields['tas_ip'] = service.tas_ip |
| 51 | fields['ntp_ip'] = service.ntp_ip |
JianHao | 90e65dc | 2017-10-31 18:01:07 +0800 | [diff] [blame] | 52 | return fields |
| 53 | |
| 54 | def get_ip_address(self, network_name, service_instance, parameter): |
JianHao | 90e65dc | 2017-10-31 18:01:07 +0800 | [diff] [blame] | 55 | try: |
| 56 | net_id = self.get_network_id(network_name) |
Sapan Bhatia | e89039d | 2017-11-22 10:59:19 -0500 | [diff] [blame] | 57 | ins_id = service_instance.instance_id |
JianHao | 90e65dc | 2017-10-31 18:01:07 +0800 | [diff] [blame] | 58 | ip_address = Port.objects.get(network_id=net_id, instance_id=ins_id).ip |
| 59 | |
| 60 | except Exception: |
| 61 | ip_address = "error" |
Sapan Bhatia | 10d0d3e | 2017-11-22 09:38:15 -0500 | [diff] [blame] | 62 | self.log.error("Could not fetch parameter", parameter = parameter, network_name = network_name) |
Sapan Bhatia | e325a13 | 2017-11-22 10:13:43 -0500 | [diff] [blame] | 63 | self.defer_sync("Waiting for parmaters to become available") |
JianHao | 90e65dc | 2017-10-31 18:01:07 +0800 | [diff] [blame] | 64 | |
| 65 | return ip_address |
| 66 | |
| 67 | # To get each network id |
| 68 | def get_network_id(self, network_name): |
| 69 | return Network.objects.get(name=network_name).id |