blob: 715916ee659f3c256142040f541afc7547d27f3e [file] [log] [blame]
JianHao4a9550f2017-10-19 11:05:14 +08001# 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
15import os
16import sys
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080017import time
JianHao4a9550f2017-10-19 11:05:14 +080018from django.db.models import Q, F
19from synchronizers.new_base.modelaccessor import *
20from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
21
22parentdir = os.path.join(os.path.dirname(__file__), "..")
23sys.path.insert(0, parentdir)
24
Sapan Bhatia28b75602017-11-28 14:18:21 -050025class ServiceGraphException(Exception):
26 pass
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050027
JianHao4a9550f2017-10-19 11:05:14 +080028class SyncVSPGWCTenant(SyncInstanceUsingAnsible):
JianHao4a9550f2017-10-19 11:05:14 +080029 observes = VSPGWCTenant
JianHao4a9550f2017-10-19 11:05:14 +080030 template_name = "vspgwctenant_playbook.yaml"
JianHao4a9550f2017-10-19 11:05:14 +080031 service_key_name = "/opt/xos/configurations/mcord/mcord_private_key"
32
33 def __init__(self, *args, **kwargs):
34 super(SyncVSPGWCTenant, self).__init__(*args, **kwargs)
35
Woojoong Kimf000eda2017-10-20 15:00:43 -070036 def get_extra_attributes(self, o):
JianHao4a9550f2017-10-19 11:05:14 +080037
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050038 scenario = self.get_scenario(o)
JianHao4a9550f2017-10-19 11:05:14 +080039
Woojoong Kim103fba02018-01-18 20:01:06 -080040 if scenario == 'cord_4_1_scenario':
41 return self.get_values_for_CORD_4_1(o)
42 elif scenario == 'cord_5_0_scenario':
43 return self.get_values_for_CORD_5_0(o)
Woojoong Kimf000eda2017-10-20 15:00:43 -070044 else:
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050045 return self.get_extra_attributes_for_manual(o)
Woojoong Kimf000eda2017-10-20 15:00:43 -070046
47 # fields for manual case
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050048 def get_extra_attributes_for_manual(self, o):
Woojoong Kimf000eda2017-10-20 15:00:43 -070049 fields = {}
Woojoong Kim103fba02018-01-18 20:01:06 -080050 fields['scenario'] = "manual"
51 fields['cord_version'] = "manual"
Woojoong Kimf000eda2017-10-20 15:00:43 -070052 # for interface.cfg file
53 fields['zmq_sub_ip'] = "manual"
54 fields['zmq_pub_ip'] = "manual"
55 fields['dp_comm_ip'] = "manual"
56 fields['cp_comm_ip'] = "manual"
57 fields['fpc_ip'] = "manual"
58 fields['cp_nb_server_ip'] = "manual"
59
60 # for cp_config.cfg file
61 fields['s11_sgw_ip'] = "manual"
62 fields['s11_mme_ip'] = "manual"
63 fields['s1u_sgw_ip'] = "manual"
64
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080065 # for rules setup in ONOS
66 fields['sgi_as_ip'] = "manual"
67 fields['sgi_spgwu_ip'] = "manual"
68
Woojoong Kimf000eda2017-10-20 15:00:43 -070069 return fields
70
Woojoong Kim103fba02018-01-18 20:01:06 -080071 def get_values_for_CORD_4_1(self, o):
Woojoong Kim1a1e3702017-10-27 13:26:34 -070072 fields = {}
Woojoong Kim103fba02018-01-18 20:01:06 -080073 fields['cord_version'] = "4.1"
74 fields['scenario'] = "cord_4_1_scenario"
Woojoong Kim1a1e3702017-10-27 13:26:34 -070075 # for interface.cfg file
76 fields['zmq_sub_ip'] = "127.0.0.1"
77 fields['zmq_pub_ip'] = "127.0.0.1"
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050078 fields['dp_comm_ip'] = self.get_ip_address_from_peer_service_instance(
Sapan Bhatia97536292017-11-27 21:35:34 -050079 'spgw_network', "VSPGWUTenant", o, 'dp_comm_ip')
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050080 fields['cp_comm_ip'] = self.get_ip_address_from_peer_service_instance_instance(
Sapan Bhatia97536292017-11-27 21:35:34 -050081 'spgw_network', o, o, 'cp_comm_ip')
Woojoong Kim1a1e3702017-10-27 13:26:34 -070082 fields['fpc_ip'] = "127.0.0.1"
83 fields['cp_nb_server_ip'] = "127.0.0.1"
84
85 # for cp_config.cfg file
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050086 fields['s11_sgw_ip'] = self.get_ip_address_from_peer_service_instance_instance(
Sapan Bhatia97536292017-11-27 21:35:34 -050087 's11_network', o, o, 's11_sgw_ip')
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050088 fields['s1u_sgw_ip'] = self.get_ip_address_from_peer_service_instance(
Sapan Bhatia97536292017-11-27 21:35:34 -050089 's1u_network', "VSPGWUTenant", o, 's1u_sgw_ip')
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050090 fields['s11_mme_ip'] = self.get_ip_address_from_peer_service_instance(
Sapan Bhatia97536292017-11-27 21:35:34 -050091 's11_network', "VENBServiceInstance", o, 's11_mme_ip')
Woojoong Kim1a1e3702017-10-27 13:26:34 -070092
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080093 # for rules setup in ONOS
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050094 fields['sgi_as_ip'] = self.get_ip_address_from_peer_service_instance(
Sapan Bhatia97536292017-11-27 21:35:34 -050095 'sgi_network', "VENBServiceInstance", o, 'sgi_as_ip')
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -050096 fields['sgi_spgwu_ip'] = self.get_ip_address_from_peer_service_instance(
Sapan Bhatia97536292017-11-27 21:35:34 -050097 'sgi_network', "VSPGWUTenant", o, 'sgi_spgwu_ip')
Woojoong Kim4f09cbf2017-11-19 21:25:37 -080098
Woojoong Kim1a1e3702017-10-27 13:26:34 -070099 return fields
100
Woojoong Kim103fba02018-01-18 20:01:06 -0800101 def get_values_for_CORD_5_0(self, o):
Woojoong Kim313d35c2017-12-18 17:15:46 -0800102 fields = {}
Woojoong Kim103fba02018-01-18 20:01:06 -0800103 fields['cord_version'] = "5.0"
104 fields['scenario'] = "cord_5_0_scenario"
Woojoong Kim313d35c2017-12-18 17:15:46 -0800105
106 # for interface.cfg file
107 fields['zmq_sub_ip'] = "127.0.0.1"
108 fields['zmq_pub_ip'] = "127.0.0.1"
109 fields['dp_comm_ip'] = self.get_ip_address_from_peer_service_instance(
110 'spgw_network', "VSPGWUTenant", o, 'dp_comm_ip')
111 fields['cp_comm_ip'] = self.get_ip_address_from_peer_service_instance_instance(
112 'spgw_network', o, o, 'cp_comm_ip')
113 fields['fpc_ip'] = "127.0.0.1"
114 fields['cp_nb_server_ip'] = "127.0.0.1"
115
116 # for cp_config.cfg file
117 fields['s11_sgw_ip'] = self.get_ip_address_from_peer_service_instance_instance(
118 's11_network', o, o, 's11_sgw_ip')
119 fields['s1u_sgw_ip'] = self.get_ip_address_from_peer_service_instance(
Woojoong Kim103fba02018-01-18 20:01:06 -0800120 's1u_network', "VSPGWUTenant", o, 's1u_sgw_ip')
Woojoong Kim313d35c2017-12-18 17:15:46 -0800121 fields['s11_mme_ip'] = self.get_ip_address_from_peer_service_instance(
122 's11_network', "VMMETenant", o, 's11_mme_ip')
123
124 # for rules setup in ONOS
Woojoong Kim103fba02018-01-18 20:01:06 -0800125 internetemulator_flag = self.has_instance("InternetEmulatorServiceInstance", o)
126 if (internetemulator_flag):
127 fields['sgi_as_ip'] = self.get_ip_address_from_peer_service_instance(
128 'sgi_network', "InternetEmulatorServiceInstance", o, 'sgi_as_ip')
129 else:
130 fields['sgi_as_ip'] = o.appserver_ip_addr
Woojoong Kim313d35c2017-12-18 17:15:46 -0800131 fields['sgi_spgwu_ip'] = self.get_ip_address_from_peer_service_instance(
132 'sgi_network', "VSPGWUTenant", o, 'sgi_spgwu_ip')
133
134 return fields
135
136
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500137 def get_scenario(self, o):
138 venb_flag = self.has_instance("VENBServiceInstance", o)
139 vmme_flag = self.has_instance("VMMETenant", o)
140 sdncontroller_flag = self.has_instance(
141 "SDNControllerServiceInstance", o)
142 vspgwu_flag = self.has_instance("VSPGWUTenant", o)
143 internetemulator_flag = self.has_instance(
Woojoong Kim103fba02018-01-18 20:01:06 -0800144 "InternetEmulatorServiceInstance", o)
145 vhss_flag = self.has_instance("VHSSTenant", o)
146 hssdb_flag = self.has_instance("HSSDBServiceInstance", o)
Woojoong Kim4f09cbf2017-11-19 21:25:37 -0800147
Woojoong Kim103fba02018-01-18 20:01:06 -0800148 if (o.blueprint == "build") or (o.blueprint == "MCORD 4.1"):
149 if not venb_flag:
150 self.defer_sync(o, "Waiting for eNB image to become available")
151 if not vspgwu_flag:
152 self.defer_sync(o, "Waiting for SPGWU image to become available")
153 return 'cord_4_1_scenario'
Woojoong Kimf000eda2017-10-20 15:00:43 -0700154
Woojoong Kim103fba02018-01-18 20:01:06 -0800155 if (o.blueprint == "mcord_5") or (o.blueprint == "MCORD 5"):
156 if not hssdb_flag:
157 self.defer_sync(o, "Waiting for HSS_DB image to become available")
158 if not vhss_flag:
159 self.defer_sync(o, "Waiting for vHSS image to become available")
160 if not vmme_flag:
161 self.defer_sync(o, "Waiting for vMME image to become available")
162 if not vspgwu_flag:
163 self.defer_sync(o, "Waiting for vSPGWU image to become available")
164 return 'cord_5_0_scenario'
Woojoong Kim313d35c2017-12-18 17:15:46 -0800165
Woojoong Kim1a1e3702017-10-27 13:26:34 -0700166 return 'manual'
167
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500168 def get_ip_address_from_peer_service_instance(self, network_name, sitype, o, parameter=None):
169 peer_si = self.get_peer_serviceinstance_of_type(sitype, o)
Sapan Bhatia97536292017-11-27 21:35:34 -0500170 return self.get_ip_address_from_peer_service_instance_instance(network_name, peer_si, o, parameter)
Woojoong Kimf000eda2017-10-20 15:00:43 -0700171
Sapan Bhatia97536292017-11-27 21:35:34 -0500172 def get_ip_address_from_peer_service_instance_instance(self, network_name, peer_si, o, parameter=None):
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500173 try:
174 net_id = self.get_network_id(network_name)
Sapan Bhatia97536292017-11-27 21:35:34 -0500175 ins_id = peer_si.leaf_model.instance_id
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500176 ip_address = Port.objects.get(
177 network_id=net_id, instance_id=ins_id).ip
178 except Exception:
179 self.log.error("Failed to fetch parameter",
180 parameter=parameter,
181 network_name=network_name)
Sapan Bhatia97536292017-11-27 21:35:34 -0500182 self.defer_sync(o, "Waiting for parameters to become available")
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500183
184 return ip_address
185
186 def get_peer_serviceinstance_of_type(self, sitype, o):
187 prov_link_set = ServiceInstanceLink.objects.filter(
188 subscriber_service_instance_id=o.id)
189
190 try:
191 peer_service = next(
Sapan Bhatia97536292017-11-27 21:35:34 -0500192 p.provider_service_instance for p in prov_link_set if p.provider_service_instance.leaf_model_name == sitype)
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500193 except StopIteration:
194 sub_link_set = ServiceInstanceLink.objects.filter(
195 provider_service_instance_id=o.id)
196 try:
197 peer_service = next(
Sapan Bhatia97536292017-11-27 21:35:34 -0500198 s.subscriber_service_instance for s in sub_link_set if s.subscriber_service_instance.leaf_model_name == sitype)
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500199 except StopIteration:
200 self.log.error(
201 'Could not find service type in service graph', service_type=sitype, object=o)
Sapan Bhatia28b75602017-11-28 14:18:21 -0500202 raise ServiceGraphException(
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500203 "Synchronization failed due to incomplete service graph")
Sapan Bhatia97536292017-11-27 21:35:34 -0500204
205 return peer_service
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500206
Woojoong Kimf000eda2017-10-20 15:00:43 -0700207 # To get each network id
208 def get_network_id(self, network_name):
209 return Network.objects.get(name=network_name).id
210
211 # To get service_instance (assumption: there is a single instance for each service)
212 def get_instance_id(self, serviceinstance):
213 instances = serviceinstance.objects.all()
214 instance_id = instances[0].instance_id
215 return instance_id
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500216
217 def has_instance(self, sitype, o):
Sapan Bhatia28b75602017-11-28 14:18:21 -0500218 try:
219 i = self.get_peer_serviceinstance_of_type(sitype, o)
220 except ServiceGraphException:
Sapan Bhatiaa1fbd382017-11-22 13:18:11 -0500221 self.log.info("Missing in ServiceInstance graph",
222 serviceinstance=sitype)
223 return False
224
Woojoong Kim38d84882017-11-28 15:38:39 -0800225 return i.leaf_model.instance_id