async_update_list = (context['PROCESSINSTANCEID'], context['TASKID'], context['EXECNUMBER']) #Create order object IpamOrderObject = Order(ipam_device_id) IpamOrderObject.command_synchronize(300) #Retrieve info about tenants Orchestration.update_asynchronous_task_details(*async_update_list, 'Retrieving information about customer... ') objects_list = IpamOrderObject.command_objects_instances(ms_ipam_tenant) #Check if customer exists in IPAM system does_tenant_exists = False counter = 0 while not does_tenant_exists and counter < len(objects_list): tenant_object = IpamOrderObject.command_objects_instances_by_id(ms_ipam_tenant, objects_list[counter])[ms_ipam_tenant][objects_list[counter]] if tenant_object['object_id'] == customer_name: does_tenant_exists = True customer_id = tenant_object['id'] counter += 1 #If the customer does not exist in IPAM system, finish the task as fail if not does_tenant_exists: fail_comment = f'Customer {customer_name} has not been found' print(fail_string) #If we here, cusotmer exists. Extract customer sites Orchestration.update_asynchronous_task_details(*async_update_list, 'Retrieving information about customer... OK') Orchestration.update_asynchronous_task_details(*async_update_list, 'Retrieving customer details... ')
fail_string = f'{{"wo_status": "FAIL", "wo_comment": "{fail_comment}"}}' success_string = f'{{"wo_status": "ENDED", "wo_comment": "{success_comment}"}}' #Create Orchestration object to update GUI dynamically Orchestration = Orchestration(context['UBIQUBEID']) async_update_list = (context['PROCESSINSTANCEID'], context['TASKID'], context['EXECNUMBER']) #Create IPAM order object IpamOrderObject = Order(ipam_device_id) IpamOrderObject.command_synchronize(300) #Identify ASN for the site and save to context Orchestration.update_asynchronous_task_details(*async_update_list, 'Retrieve AS number for the site... ') site_object = IpamOrderObject.command_objects_instances_by_id(ms_ipam_site, site)[ms_ipam_site][site] context['site_asn'] = site_object['asn'] Orchestration.update_asynchronous_task_details(*async_update_list, 'Retrieve AS number for the site... OK') time.sleep(3) #Identify IPv4 subnets for the site and save to context Orchestration.update_asynchronous_task_details(*async_update_list, 'Find out a IPv4 block for the site... ') context['site_prefixes_list'] = list() objects_list = IpamOrderObject.command_objects_instances(ms_ipam_prefix) for object in objects_list: prefix_object = IpamOrderObject.command_objects_instances_by_id(ms_ipam_prefix, object)[ms_ipam_prefix][object]
IpamOrderObject.command_synchronize(300) Orchestration.update_asynchronous_task_details( *async_update_list, 'Retrieve information about CE device on the site... ') objects_list = IpamOrderObject.command_objects_instances(ms_ipam_device) #Retrieve information about CE device what is located on the site counter = 0 ce_device_id = None ce_device_name = None #Find CE device name first while ce_device_name is None or counter < len(objects_list): device_object = IpamOrderObject.command_objects_instances_by_id( ms_ipam_device, objects_list[counter])[ms_ipam_device][objects_list[counter]] if device_object['role'] == 'CE' and device_object['site'] == site: ce_device_name = device_object['object_id'] counter += 1 #Find device ID value second MsaLookup = lookup.Lookup() MsaLookup.look_list_device_ids() devices = json.loads(MsaLookup.content) counter = 0 while ce_device_id is None or counter < len(devices): if devices[counter]['name'] == ce_device_name: ce_device_id = devices[counter]['id']
Orchestration.update_asynchronous_task_details( *async_update_list, 'IPAM integration is used. Retrive VLAN info from IPAM... ') context['ipam_device'] = re.match('^\D+?(\d+?)$', context['ipam_device']).group(1) #Retrive information about VLANs IpamOrderObject = Order(context['ipam_device']) IpamOrderObject.command_synchronize(300) objects_list = IpamOrderObject.command_objects_instances(ms_ipam_vlan) vlan_string = str() counter = 0 context['vlan_list'] = dict() for vlan_name in objects_list: vlan_object = IpamOrderObject.command_objects_instances_by_id( ms_ipam_vlan, vlan_name)[ms_ipam_vlan][vlan_name] if 'site' in vlan_object: if vlan_object['site'] == context['site']: context['vlan_list'][counter] = { 'id': vlan_object['vid'], 'name': vlan_object['object_id'], "__index__": counter } vlan_string += '{} '.format(vlan_object['object_id']) counter += 1 Orchestration.update_asynchronous_task_details( *async_update_list, 'IPAM integration is used. Retrive VLAN info from IPAM... OK. VLANs: {}' .format(vlan_string)) time.sleep(3)
with open(ms_advertised_prefix_file_path) as temp_ms_file: temp_ms_file_content = temp_ms_file.read() CeOrderObject.command_synchronize(300) time.sleep(10) objects_list = CeOrderObject.command_objects_instances(ms_router_advert_by_bgp) is_site_prefix_announced = False counter = 0 while is_site_prefix_announced is False and counter < len(objects_list): prefix_object = CeOrderObject.command_objects_instances_by_id(ms_router_advert_by_bgp, objects_list[counter])[ms_router_advert_by_bgp][objects_list[counter]] if prefix_object['object_id'] == ce_internal_prefix: is_site_prefix_announced = True is_site_prefix_announced_to_all_peers.append(is_site_prefix_announced) counter += 1 sed_command = 'sed -i \'s@{original_string}@{rewrite_string}@\' {ms_file}'.format(original_string = connection['neighbour']['ip_address'].split('/')[0], rewrite_string = '1.1.1.1', ms_file = ms_advertised_prefix_file_path) os.system(sed_command) Orchestration.update_asynchronous_task_details(*async_update_list, 'Verify that internal prefix announced to PE device... OK') if len(is_site_prefix_announced_to_all_peers) > 0 and all(is_site_prefix_announced_to_all_peers): context.pop('ce_connections') context.pop('ce_device_details')
# MATH ALGORITHM # ######################################################################################################## info_message('Processing metrics...') cost_summary = {} for device in device_id_list: d_name = Device(device_id=device).name info_message(f'Processing metrics from: {d_name}...') cost_summary[device] = float() order = Order(device) order.command_objects_instances('logs') for ms_item in json.loads(order.content): if check_pod_name(ms_item): order.command_objects_instances_by_id('logs', ms_item) cost = json.loads(order.content)['logs'][ms_item]['min'] cost_summary[device] += float(cost) target_device_id = min(cost_summary, key=cost_summary.get) target_device_name = Device(device_id=target_device_id).name info_message(f'Best offer from: {target_device_name}.', 5) context['target_device_id'] = target_device_id context['target_device_name'] = target_device_name ######################################################################################################## # DEPLOY USER APP # ########################################################################################################
Orchestration = Orchestration(context['UBIQUBEID']) async_update_list = (context['PROCESSINSTANCEID'], context['TASKID'], context['EXECNUMBER']) #Create order object IpamOrderObject = Order(ipam_device_id) #Find site ID objects_list = IpamOrderObject.command_objects_instances(ms_ipam_site) #Looking for CE device in STAGED state on the site Orchestration.update_asynchronous_task_details(*async_update_list, 'Looking for CE device on site {} in "staged" state... '.format(site)) #Collect cusromer sites object_id for future use for site in objects_list: site_object = IpamOrderObject.command_objects_instances_by_id(ms_ipam_site, site)[ms_ipam_site][site] if site_object['object_id'] == site: context['site_id'] = site_object['id'] #Find all devices on the site site_device_list = list() for device in context['customer_details']['devices_list']: device_object = IpamOrderObject.command_objects_instances_by_id(ms_ipam_device, device)[ms_ipam_device][device] try: if device_object['site'] == context['site']: site_device_list.append(device) except: pass #Find staged devices on the site
Orchestration.update_asynchronous_task_details(*async_update_list, 'Provisioning of CE device... [{}] OK'.format(''.join(pretty_formatted_bar))) time.sleep(3) #Attach configuration profile Orchestration.update_asynchronous_task_details(*async_update_list, 'Attaching configuration deployment settings profile... ') CeDeviceObject.profile_attach(ce_local_context['msa_specific']['deployment_settings_ref']) #Create CE device order object CeOrderObject = Order(ce_device_id) response = CeOrderObject.command_synchronize(300) device_object = IpamOrderObject.command_objects_instances_by_id(ms_ipam_device, ce_device_name)[ms_ipam_device][ce_device_name.replace('.', '_')] Orchestration.update_asynchronous_task_details(*async_update_list, 'Attaching configuration deployment settings profile... OK') Orchestration.update_asynchronous_task_details(*async_update_list, 'Marking device {} as "ACTIVE" in IPAM system...'.format(ce_device_name)) #Mark the device as Active in IPAM ms_dict = {ms_ipam_device: {ce_device_name: {'object_id': ce_device_name, 'status': 'active', 'site': device_object['site'], 'role': device_object['role'], 'model': device_object['model'] } } } IpamOrderObject.command_execute('UPDATE', ms_dict)
*async_update_list, 'Configuring CE device... BGP peer for {}... OK'.format( connection['neighbour']['device'])) CeOrderObject.command_synchronize(300) #Check if customer VRF exists on PE Orchestration.update_asynchronous_task_details( *async_update_list, 'Configuring PE device... Configuring VRF... ') does_customer_vrf_exist = False counter = 0 objects_list = pe_order_list[-1].command_objects_instances(ms_router_vrf) if customer_vrf not in objects_list: #Create custromer VRF on PE #Grab RD from IPAM vrf_object = IpamOrderObject.command_objects_instances_by_id( ms_ipam_vrf, customer_vrf)[ms_ipam_vrf][customer_vrf] ms_dict = { ms_router_vrf: { customer_vrf: { 'object_id': customer_vrf, 'interfaces': { '0': { 'iface_name': connection['neighbour']['interface'] } }, 'rd': vrf_object['rd'], 'route_target_export': vrf_object['rd'], 'route_target_import': vrf_object['rd'] } }
return ''.join(random.choice('0123456789') for i in range(8)) cost_summary = {} ######################################################################################################## # MATH ALGORITHM # ######################################################################################################## for device in device_id_list.values(): cost_summary[device] = float() order = Order(device) order.command_objects_instances('k8s_log') for ms_item in json.loads(order.content.decode()): order.command_objects_instances_by_id('k8s_log', ms_item) cost = json.loads(order.content.decode())['k8s_log'][ms_item]['min'] cost_summary[device] += float(cost) target_device_id = min(cost_summary, key=cost_summary.get) target_device_name = Device(device_id=target_device_id).name context['target_device_id'] = target_device_id context['target_device_name'] = target_device_name ######################################################################################################## # DEPLOY USER APP # ######################################################################################################## ''' Generate data in format. ms_vars_dict = {"namespace": , "pod_name": ,
StationOrderObject = Order(context['station_device_id']) response = StationOrderObject.command_synchronize(300) Orchestration.update_asynchronous_task_details( *async_update_list, 'Import current configuration... OK') time.sleep(3) #Check VLAN IDs Orchestration.update_asynchronous_task_details(*async_update_list, 'Verifying VLANs... ') objects_list = StationOrderObject.command_objects_instances(ms_vlan) do_vlans_exist = list() for number, vlan in context['vlan_list'].items(): Orchestration.update_asynchronous_task_details( *async_update_list, 'Verifying VLANs... ID {}... '.format(vlan['id'])) for vlan_id in objects_list: vlan_object = StationOrderObject.command_objects_instances_by_id( ms_vlan, vlan_id)[ms_vlan][vlan_id] if vlan['id'] == vlan_object['object_id']: Orchestration.update_asynchronous_task_details( *async_update_list, 'Verifying VLANs... ID {}... OK'.format(vlan['id'])) time.sleep(3) do_vlans_exist.append(True) break else: Orchestration.update_asynchronous_task_details( *async_update_list, 'Verifying VLANs... ID {}... FAILED'.format(vlan['id'])) time.sleep(3) do_vlans_exist.append(False) #Check trunk port status
IpamOrderObject.command_synchronize(300) objects_list = IpamOrderObject.command_objects_instances(ms_ipam_device) #Retrieve information about CE device what is located on the site counter = 0 ce_device_id = None ce_device_name = None #Find CE device name Orchestration.update_asynchronous_task_details( *async_update_list, 'Retrieve information about CE device on the site... ') while ce_device_name is None or counter < len(objects_list): util.log_to_process_file( process_id, 'DEBUG: {}'.format( IpamOrderObject.command_objects_instances_by_id( ms_ipam_device, objects_list[counter]))) device_object = IpamOrderObject.command_objects_instances_by_id( ms_ipam_device, objects_list[counter])[ms_ipam_device][objects_list[counter]] if device_object['role'] == 'CE' and device_object['site'] == site: ce_device_name = device_object['object_id'] counter += 1 #Find device ID value MsaLookup = lookup.Lookup() MsaLookup.look_list_device_ids() devices = json.loads(MsaLookup.content) counter = 0 while ce_device_id is None or counter < len(devices):
#Remove site IP addresses for ip_address in context['site_address_list']: Orchestration.update_asynchronous_task_details(*async_update_list, 'Remove site {} address {} from IPAM... '.format(site, ip_address['object_id'])) ms_dict = {ms_ipam_address: {ip_address['object_id'].replace('.', '_'): {'object_id': ip_address['object_id']} } } IpamOrderObject.command_execute('DELETE', ms_dict) Orchestration.update_asynchronous_task_details(*async_update_list, 'Remove site {} address {} from IPAM... OK'.format(site, ip_address['object_id'])) time.sleep(3) IpamOrderObject.command_synchronize(300) Orchestration.update_asynchronous_task_details(*async_update_list, 'Remove ASN for the site from IPAM... ') site_object = IpamOrderObject.command_objects_instances_by_id(ms_ipam_site, site)[ms_ipam_site][site] ms_dict = {ms_ipam_site: {site_object['object_id']: {'object_id': site_object['object_id'], 'id': site_object['id'], 'slug': site_object['slug'], 'tenant': site_object['tenant'], 'status': site_object['status'] } } } IpamOrderObject.command_execute('UPDATE', ms_dict) time.sleep(3) Orchestration.update_asynchronous_task_details(*async_update_list, 'Remove ASN for the site from IPAM... ') IpamOrderObject.command_synchronize(300)