1 # ============LICENSE_START=======================================================
2 # Copyright (C) 2021 Orange
3 # ================================================================================
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # ============LICENSE_END=========================================================
21 from onapsdk.so.so_db_adapter import SoDbAdapter, IdentityService
23 from config import Config
24 from k8s_client import K8sClient
25 from onapsdk.exceptions import ResourceNotFound, APIError
26 from onapsdk.aai.business import Customer
27 from onapsdk.aai.cloud_infrastructure import Complex, CloudRegion
28 from onapsdk.msb.k8s import ConnectivityInfo
30 logger = logging.getLogger("")
31 logger.setLevel(logging.DEBUG)
32 fh = logging.StreamHandler()
33 fh_formatter = logging.Formatter('%(asctime)s %(levelname)s %(lineno)d:%(filename)s(%(process)d) - %(message)s')
34 fh.setFormatter(fh_formatter)
37 MYPATH = os.path.dirname(os.path.realpath(__file__))
40 def create_complex(region_id):
41 #### Create complex if not exists ####
42 logger.info("******** Complex *******")
43 complex_id = Config.CLOUD_REGIONS[region_id]["complex_id"]
45 region_complex = next(Complex.get_all(physical_location_id=complex_id))
46 logger.info("Complex exists")
47 except ResourceNotFound:
48 logger.info("Complex does not exist")
49 region_complex = Complex.create(physical_location_id=complex_id,
51 physical_location_type="office",
52 street1="DummyStreet 1",
55 country="DummyCountry",
57 logger.info("Complex created")
62 def create_cloud_region(region_id):
63 #### Create cloud region if not exists ####
64 logger.info("******** Cloud Region *******")
65 cloud_owner = Config.CLOUD_REGIONS[region_id]["cloud_owner"]
66 cloud_type = Config.CLOUD_REGIONS[region_id]["cloud_type"]
67 complex_id = Config.CLOUD_REGIONS[region_id]["complex_id"]
68 cloud_region_version = "1.0" if cloud_type == "k8s" else "v2.5"
70 region = next(CloudRegion.get_all(cloud_owner=cloud_owner, cloud_region_id=region_id))
71 logger.info("Cloud region exists")
72 except ResourceNotFound:
73 logger.info("Cloud region does not exist")
74 region = CloudRegion.create(cloud_owner=cloud_owner,
75 cloud_region_id=region_id,
76 cloud_type=cloud_type,
77 owner_defined_type="t1",
78 cloud_region_version=cloud_region_version,
79 complex_name=complex_id,
80 cloud_zone="CloudZone",
81 sriov_automation="false",
82 orchestration_disabled=False,
84 logger.info("Cloud region created")
89 def link_region_to_complex(cloud_region, complx):
90 logger.info("******** Cloud region <-> Complex *******")
91 cloud_region.link_to_complex(complex_object=complx)
94 def add_availability_zone(cloud_region):
95 logger.info("******** Availability zone *******")
96 region_id = cloud_region.cloud_region_id
97 availability_zone_name = Config.CLOUD_REGIONS[region_id]["availability_zone"]
98 cloud_type = Config.CLOUD_REGIONS[region_id]["cloud_type"]
100 cloud_region.get_availability_zone_by_name(availability_zone_name)
101 logger.info("Availability zone exists")
102 except ResourceNotFound:
103 logger.info("Availability zone does not exist")
104 cloud_region.add_availability_zone(availability_zone_name=availability_zone_name,
105 availability_zone_hypervisor_type=cloud_type)
106 logger.info("Availability zone added to region")
109 def add_tenant(cloud_region):
110 logger.info("******** Tenant *******")
111 region_id = cloud_region.cloud_region_id
112 tenant_id = Config.CLOUD_REGIONS[region_id]["tenant"]["id"]
113 tenant_name = Config.CLOUD_REGIONS[region_id]["tenant"]["name"]
116 cloud_region.get_tenant(tenant_id)
117 logger.info("Tenant exists")
118 except ResourceNotFound:
119 logger.info("Tenant does not exist")
120 cloud_region.add_tenant(tenant_id=tenant_id,
121 tenant_name=tenant_name)
122 logger.info("Tenant added to region")
125 def create_customer():
126 #### Create customer if not exists ####
127 logger.info("******** Customer *******")
129 Customer.get_by_global_customer_id(Config.GLOBAL_CUSTOMER_ID)
130 logger.info("Customer exists")
131 except ResourceNotFound:
132 logger.info("Customer does not exist")
133 Customer.create(Config.GLOBAL_CUSTOMER_ID, Config.GLOBAL_CUSTOMER_ID, "INFRA")
134 logger.info("Customer created")
137 def update_connectivity_info(region_id):
138 #### Update or create connectivity info ####
139 logger.info("******** Connectivity Info *******")
140 cluster_kubeconfig_path = Config.CLOUD_REGIONS[region_id]["cluster_kubeconfig_file"]
141 cloud_owner = Config.CLOUD_REGIONS[region_id]["cloud_owner"]
142 with open(os.path.join(MYPATH, cluster_kubeconfig_path), 'rb') as kubeconfig_file:
143 kubeconfig = kubeconfig_file.read()
145 connectivity_info = ConnectivityInfo.get_connectivity_info_by_region_id(cloud_region_id=region_id)
146 logger.info("Connectivity Info exists ")
147 logger.info("Delete Connectivity Info ")
148 connectivity_info.delete()
149 ConnectivityInfo.create(cloud_region_id=region_id,
150 cloud_owner=cloud_owner,
151 kubeconfig=kubeconfig)
152 logger.info("Connectivity Info created ")
153 except (APIError, ResourceNotFound):
154 logger.info("Connectivity Info does not exists ")
155 ConnectivityInfo.create(cloud_region_id=region_id,
156 cloud_owner=cloud_owner,
157 kubeconfig=kubeconfig)
158 logger.info("Connectivity Info created ")
161 def add_custom_resource_definitions(region_id):
162 #### Add Custom Resource Definitions ####
163 logger.info("******** Custom Resource Definitions *******")
164 cluster_kubeconfig_path = Config.CLOUD_REGIONS[region_id]["cluster_kubeconfig_file"]
165 k8s_client = K8sClient(kubeconfig_path=cluster_kubeconfig_path)
166 for crd in Config.CLOUD_REGIONS[region_id]["customer_resource_definitions"]:
167 k8s_client.create_custom_object(crd)
170 def add_region_to_so_db(region):
171 #### Add region to SO db ####
172 logger.info("******** SO Database *******")
173 if is_k8s_region(region.cloud_region_id):
174 identity_service = IdentityService(identity_id="Keystone_K8s",
175 url="http://test:5000/v3",
176 mso_id="onapsdk_user",
177 mso_pass="mso_pass_onapsdk",
178 project_domain_name="NULL",
179 user_domain_name="NULL",
180 identity_server_type="KEYSTONE")
182 SoDbAdapter.add_cloud_site(cloud_region_id=region.cloud_region_id,
183 complex_id=region.complex_name,
184 identity_service=identity_service,
185 orchestrator="multicloud")
187 identity_url = Config.CLOUD_REGIONS[region.cloud_region_id]["identity_url"]
188 mso_id = Config.CLOUD_REGIONS[region.cloud_region_id]["mso_id"]
189 mso_pass = Config.CLOUD_REGIONS[region.cloud_region_id]["mso_pass"]
190 identity_server_type = Config.CLOUD_REGIONS[region.cloud_region_id]["identity_server_type"]
191 identity_service = IdentityService(identity_id=region.cloud_region_id + "_KEYSTONE",
195 project_domain_name="Default",
196 user_domain_name="Default",
197 identity_server_type=identity_server_type)
199 SoDbAdapter.add_cloud_site(cloud_region_id=region.cloud_region_id,
200 complex_id=region.complex_name,
201 identity_service=identity_service,
205 def is_k8s_region(region_id):
207 if Config.CLOUD_REGIONS[region_id]["cloud_type"] == "k8s":
212 ########################################################################################################################
216 for cloud_region_id in Config.CLOUD_REGIONS:
218 complx = create_complex(cloud_region_id)
219 cloud_region = create_cloud_region(cloud_region_id)
220 link_region_to_complex(cloud_region, complx)
221 add_availability_zone(cloud_region)
222 add_tenant(cloud_region)
224 if is_k8s_region(cloud_region_id):
225 update_connectivity_info(cloud_region_id)
226 add_custom_resource_definitions(cloud_region_id)
228 add_region_to_so_db(cloud_region)
231 if __name__ == "__main__":