JianHao | 4a9550f | 2017-10-19 11:05:14 +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 SyncVSPGWCTenant(SyncInstanceUsingAnsible): |
| 25 | provides = [VSPGWCTenant] |
| 26 | |
| 27 | observes = VSPGWCTenant |
| 28 | |
| 29 | requested_interval = 0 |
| 30 | |
| 31 | template_name = "vspgwctenant_playbook.yaml" |
| 32 | |
| 33 | service_key_name = "/opt/xos/configurations/mcord/mcord_private_key" |
| 34 | |
| 35 | def __init__(self, *args, **kwargs): |
| 36 | super(SyncVSPGWCTenant, self).__init__(*args, **kwargs) |
| 37 | |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame^] | 38 | def get_extra_attributes(self, o): |
| 39 | if self.get_scenario() == 'manual': |
| 40 | return self.get_extra_attributes_for_manual() |
JianHao | 4a9550f | 2017-10-19 11:05:14 +0800 | [diff] [blame] | 41 | |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame^] | 42 | fields = {} |
| 43 | fields['scenario'] = self.get_scenario() |
| 44 | # for interface.cfg file |
| 45 | fields['zmq_sub_ip'] = self.get_ip_address('sbi_network', SDNControllerServiceInstance, 'zmq_sub_ip') |
| 46 | fields['zmq_pub_ip'] = self.get_ip_address('sbi_network', SDNControllerServiceInstance, 'zmq_pub_ip') |
| 47 | fields['dp_comm_ip'] = self.get_ip_address('sbi_network', VSPGWUTenant, 'dp_comm_ip') |
| 48 | fields['cp_comm_ip'] = self.get_ip_address('nbi_network', VSPGWCTenant, 'cp_comm_ip') |
| 49 | fields['fpc_ip'] = self.get_ip_address('nbi_network', SDNControllerServiceInstance, 'fpc_ip') |
| 50 | fields['cp_nb_server_ip'] = self.get_ip_address('nbi_network', VSPGWCTenant, 'cp_nb_server_ip') |
JianHao | 4a9550f | 2017-10-19 11:05:14 +0800 | [diff] [blame] | 51 | |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame^] | 52 | # for cp_config.cfg file |
| 53 | fields['s11_sgw_ip'] = self.get_ip_address('s11_network', VSPGWCTenant, 's11_sgw_ip') |
| 54 | fields['s1u_sgw_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_sgw_ip') |
JianHao | 4a9550f | 2017-10-19 11:05:14 +0800 | [diff] [blame] | 55 | |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame^] | 56 | # the parameter 's11_mme_ip' depends on scenarios |
| 57 | if self.get_scenario() == 'ng4t': |
| 58 | fields['s11_mme_ip'] = self.get_ip_address('s11_network', VMMETenant, 's11_mme_ip') |
| 59 | elif self.get_scenario() == 'spirent': |
| 60 | fields['s11_mme_ip'] = self.get_ip_address('s11_network', VENBServiceInstance, 's11_mme_ip') |
| 61 | else: |
| 62 | fields['s11_mme_ip'] = "scenario_error" |
JianHao | 4a9550f | 2017-10-19 11:05:14 +0800 | [diff] [blame] | 63 | |
Woojoong Kim | f000eda | 2017-10-20 15:00:43 -0700 | [diff] [blame^] | 64 | return fields |
| 65 | |
| 66 | # fields for manual case |
| 67 | def get_extra_attributes_for_manual(self): |
| 68 | fields = {} |
| 69 | fields['scenario'] = self.get_scenario() |
| 70 | # for interface.cfg file |
| 71 | fields['zmq_sub_ip'] = "manual" |
| 72 | fields['zmq_pub_ip'] = "manual" |
| 73 | fields['dp_comm_ip'] = "manual" |
| 74 | fields['cp_comm_ip'] = "manual" |
| 75 | fields['fpc_ip'] = "manual" |
| 76 | fields['cp_nb_server_ip'] = "manual" |
| 77 | |
| 78 | # for cp_config.cfg file |
| 79 | fields['s11_sgw_ip'] = "manual" |
| 80 | fields['s11_mme_ip'] = "manual" |
| 81 | fields['s1u_sgw_ip'] = "manual" |
| 82 | |
| 83 | return fields |
| 84 | |
| 85 | def has_venb(self): |
| 86 | # try get vMME instance |
| 87 | try: |
| 88 | instance_id = self.get_instance_id(VENBServiceInstance) |
| 89 | except Exception: |
| 90 | print 'cannot get VENBServiceInstance' |
| 91 | return False |
| 92 | |
| 93 | return True |
| 94 | |
| 95 | def has_vmme(self): |
| 96 | # try get vMME instance |
| 97 | try: |
| 98 | instance_id = self.get_instance_id(VMMETenant) |
| 99 | except Exception: |
| 100 | print 'cannot get VMMETenant' |
| 101 | return False |
| 102 | |
| 103 | return True |
| 104 | |
| 105 | |
| 106 | # Which scenario does it use among Spirent or NG4T? |
| 107 | def get_scenario(self): |
| 108 | # try get vENB instance: one of both Spirent and NG4T |
| 109 | venb_flag = self.has_venb() |
| 110 | vmme_flag = self.has_vmme() |
| 111 | |
| 112 | if vmme_flag: |
| 113 | return 'ng4t' |
| 114 | else: |
| 115 | if venb_flag: |
| 116 | return 'spirent' |
| 117 | else: |
| 118 | return 'manual' |
| 119 | |
| 120 | def get_ip_address(self, network_name, service_instance, parameter): |
| 121 | |
| 122 | if self.get_scenario() == 'manual': |
| 123 | return "manual" |
| 124 | |
| 125 | try: |
| 126 | net_id = self.get_network_id(network_name) |
| 127 | ins_id = self.get_instance_id(service_instance) |
| 128 | ip_address = Port.objects.get(network_id=net_id, instance_id=ins_id).ip |
| 129 | |
| 130 | except Exception: |
| 131 | ip_address = "error" |
| 132 | print "get failed -- %s" % (parameter) |
| 133 | |
| 134 | return ip_address |
| 135 | |
| 136 | # To get each network id |
| 137 | def get_network_id(self, network_name): |
| 138 | return Network.objects.get(name=network_name).id |
| 139 | |
| 140 | # To get service_instance (assumption: there is a single instance for each service) |
| 141 | def get_instance_id(self, serviceinstance): |
| 142 | instances = serviceinstance.objects.all() |
| 143 | instance_id = instances[0].instance_id |
| 144 | return instance_id |