def get_mgmt_ips_frm_lo_ips(self, job_ctx, lo0_ips, device_name): FilterLog.instance("GetMgmtIPFilter", device_name) _task_log("Starting to get the mgmt_ips") mgmt_ips = [] try: _task_log("Getting the vnc handle") self.vnc_lib = JobVncApi.vnc_init(job_ctx) for lo0_ip in lo0_ips: if lo0_ip: mgmt_ip, dev_name = self._get_mgmt_ip(lo0_ip) if mgmt_ip: mgmt_ips.append({mgmt_ip : (dev_name, lo0_ip)}) _task_done() return { 'status': 'success', 'get_mgmt_ip_log': FilterLog.instance().dump(), 'get_mgmt_ips': mgmt_ips } except Exception as ex: _task_error_log(str(ex)) _task_error_log(traceback.format_exc()) return {'status': 'failure', 'error_msg': str(ex), 'get_mgmt_ips': mgmt_ips, 'get_mgmt_ip_log': FilterLog.instance().dump()}
def validate_critical_roles_for_hitless(self, job_ctx, image_upgrade_list): try: FilterLog.instance("HitlessUpgradeFilter") self.job_input = FilterModule._validate_job_ctx(job_ctx) self.fabric_uuid = self.job_input['fabric_uuid'] self.vncapi = JobVncApi.vnc_init(job_ctx) self.job_ctx = job_ctx self.ja = JobAnnotations(self.vncapi) self.advanced_parameters = self._get_advanced_params() self._cache_job_input() self.image_upgrade_list = image_upgrade_list self.device_uuid_list = [] for image_entry in self.image_upgrade_list: device_list = image_entry.get('device_list') image_uuid = image_entry.get('image_uuid') image_obj = self.vncapi.device_image_read(id=image_uuid) for device_uuid in device_list: device_obj = self.vncapi.physical_router_read( id=device_uuid) if image_obj.device_image_os_version !=\ device_obj.physical_router_os_version: self.device_uuid_list.append(device_uuid) results = self._validate_critical_roles_for_hitless_upgrade() return results except Exception as ex: errmsg = "Unexpected error validating: %s\n%s" % ( str(ex), traceback.format_exc()) _task_error_log(errmsg) return { 'status': 'failure', 'error_msg': errmsg, }
def _cli_config_obj_update(self, job_ctx, new_cli_uuid, existing_cli_dict, file_list): cli_diff_info_list = [] vnc_lib = JobVncApi.vnc_init(job_ctx) # Read cli obj if newly created else take the existing dict if new_cli_uuid is not None: cli_dict_to_update = vnc_lib.cli_config_read(id=new_cli_uuid) else: cli_dict_to_update = existing_cli_dict # Update object for every item in the filelist for file_item in file_list: filename = file_item.split("/")[-1] file_properties = filename.split("_") timestamp = file_properties[-2] date = file_properties[-3] username = file_properties[-4] # Open file to read contents file_handler = open(file_item, "r") config_changes = file_handler.read() if config_changes == "[]": continue cli_commit_dict = { "username": username, "time": date + " " + timestamp, "config_changes": config_changes } file_handler.close() cli_diff_info_list.append(cli_commit_dict) final_update_dict = {"commit_diff_info": cli_diff_info_list} cli_dict_to_update.set_commit_diff_list(final_update_dict) vnc_lib.cli_config_update(cli_dict_to_update) return cli_dict_to_update
def get_device_info( self, job_ctx, device_uuid, ): try: FilterLog.instance("HitlessUpgradeFilter") self.job_input = FilterModule._validate_job_ctx(job_ctx) self.fabric_uuid = self.job_input['fabric_uuid'] self.vncapi = JobVncApi.vnc_init(job_ctx) self.job_ctx = job_ctx self.ja = JobAnnotations(self.vncapi) self.advanced_parameters = self._get_advanced_params() self._cache_job_input() self.device_uuid = device_uuid device_info = self._get_device_info() return device_info except Exception as ex: errmsg = "Unexpected error getting device info: %s\n%s" % ( str(ex), traceback.format_exc()) _task_error_log(errmsg) return { 'status': 'failure', 'error_msg': errmsg, }
def _create_interfaces(self, job_ctx, interfaces_payload, prouter_name): vnc_lib = JobVncApi.vnc_init(job_ctx) physical_interfaces_list = interfaces_payload.get( 'physical_interfaces_list') logical_interfaces_list = interfaces_payload.get( 'logical_interfaces_list') vnc_physical_interfaces_list, vnc_logical_interfaces_list = \ self.get_create_interfaces_payload( prouter_name, physical_interfaces_list, logical_interfaces_list) phy_intfs_success_names, phy_intf_failed_info =\ self._create_physical_interfaces( vnc_lib, vnc_physical_interfaces_list) log_intfs_success_names, log_intf_failed_info =\ self._create_logical_interfaces( vnc_lib, vnc_logical_interfaces_list) return { "phy_intfs_success_names": list(set(phy_intfs_success_names)), "log_intfs_success_names": list(set(log_intfs_success_names)), "phy_intf_failed_info": phy_intf_failed_info, "log_intf_failed_info": log_intf_failed_info }
def get_hitless_upgrade_plan(self, job_ctx, image_upgrade_list, device_json): try: FilterLog.instance("HitlessUpgradeFilter") self.job_input = FilterModule._validate_job_ctx(job_ctx) self.fabric_uuid = self.job_input['fabric_uuid'] self.vncapi = JobVncApi.vnc_init(job_ctx) self.job_ctx = job_ctx self.ja = JobAnnotations(self.vncapi) self.advanced_parameters = self._get_advanced_params() self._cache_job_input() self.batch_limit = self.advanced_parameters.get( 'bulk_device_upgrade_count') self.image_upgrade_list = image_upgrade_list self.device_json = device_json upgrade_plan = self._get_hitless_upgrade_plan() return upgrade_plan except Exception as ex: errmsg = "Unexpected error: %s\n%s" % (str(ex), traceback.format_exc()) _task_error_log(errmsg) return { 'status': 'failure', 'error_msg': errmsg, }
def get_mgmt_ips_frm_lo_ips(self, job_ctx, lo0_ips, device_name): FilterLog.instance("GetMgmtIPFilter", device_name) _task_log("Starting to get the mgmt_ips") mgmt_ips = [] try: _task_log("Getting the vnc handle") self.vnc_lib = JobVncApi.vnc_init(job_ctx) for lo0_ip in lo0_ips: if lo0_ip: mgmt_ip, dev_name = self._get_mgmt_ip(lo0_ip) if mgmt_ip: mgmt_ips.append({mgmt_ip: (dev_name, lo0_ip)}) _task_done() return { 'status': 'success', 'get_mgmt_ip_log': FilterLog.instance().dump(), 'get_mgmt_ips': mgmt_ips } except Exception as ex: _task_error_log(str(ex)) _task_error_log(traceback.format_exc()) return { 'status': 'failure', 'error_msg': str(ex), 'get_mgmt_ips': mgmt_ips, 'get_mgmt_ip_log': FilterLog.instance().dump() }
def _create_neighbor_links(self, job_ctx, lldp_neighbors_payload, prouter_fqname, prouter_vendor): if not lldp_neighbors_payload.get('neighbor_info_list'): _task_log("No neighbors found") _task_done() return { 'lldp_neighbors_success_names': [], 'lldp_neighbors_failed_info': [] } vnc_lib = JobVncApi.vnc_init(job_ctx) vnc_topology_disc_payload = self.get_vnc_payload( vnc_lib, prouter_fqname, prouter_vendor, lldp_neighbors_payload['neighbor_info_list']) topology_disc_payload = self._do_further_parsing( vnc_lib, vnc_topology_disc_payload) _task_done("Parsed payload completely") _task_log("Creating links between neighboring physical interfaces") topology_discovery_resp = self._create_physical_interface_refs( vnc_lib, topology_disc_payload) return topology_discovery_resp
def rma_get_device_password(self, job_ctx, rma_devices_list): try: self.vncapi = JobVncApi.vnc_init(job_ctx) device_uuid = rma_devices_list[0]['device_uuid'] device_obj = self.vncapi.physical_router_read(id=device_uuid) return self._get_password(device_obj) except Exception as ex: errmsg = "Unexpected error: %s\n%s" % (str(ex), traceback.format_exc()) _task_error_log(errmsg) return { 'status': 'failure', 'error_msg': errmsg, }
def find_leaf_devices_filter(self, job_ctx): """ Validate input and call method to find leaf devices in provided fabric. :param job_ctx: Dictionary example: { 'job_transaction_descr': 'Discover OS Computes', 'fabric_uuid': '123412341234-123412341234', 'contrail_command_host': '10.10.10.10:9091', 'cc_username': '******', 'cc_password': "******" } :return: Dictionary if success, returns { 'status': 'success', 'leaf_devices': [ { 'username': u'admin', 'host': '10.10.10.4', 'password': '******' } ] } if failure, returns { 'status': 'failure', 'error_msg': <string: error message> } """ try: FilterModule._validate_job_ctx(job_ctx) job_input = job_ctx.get('input') vnc_api = JobVncApi.vnc_init(job_ctx) fabric_uuid = job_input['fabric_uuid'] leaf_devices = self.find_leaf_devices(fabric_uuid, vnc_api) except Exception as e: errmsg = "Unexpected error: %s\n%s" % (str(e), traceback.format_exc()) return { STATUS: FAILURE, ERRMSG: errmsg, } return { STATUS: SUCCESS, LEAF_DEVICES: leaf_devices, }
def rma_devices_to_ztp(self, job_ctx, rma_devices_list): try: FilterLog.instance("RmaDevicesFilter") self.job_input = FilterModule._validate_job_ctx(job_ctx) self.vncapi = JobVncApi.vnc_init(job_ctx) self.job_ctx = job_ctx return self._rma_devices_to_ztp(rma_devices_list) except Exception as ex: errmsg = "Unexpected error: %s\n%s" % (str(ex), traceback.format_exc()) _task_error_log(errmsg) return { 'status': 'failure', 'error_msg': errmsg, }
def _init_vnc_lib(self): # Instantiate the VNC library # Retry for sometime, till API server is up errmsg = None for i in range(0, 10): try: self.vnc_lib = JobVncApi.vnc_init(self.job_ctx) break except Exception as ex: time.sleep(10) errmsg = "Failed to connect to API server due to error: %s"\ % str(ex) if self.vnc_lib is None: raise RuntimeError(errmsg)
def _validate_job_ctx(job_ctx): vnc_api = JobVncApi.vnc_init(job_ctx) job_template_fqname = job_ctx.get('job_template_fqname') if not job_template_fqname: raise ValueError('Invalid job_ctx: missing job_template_fqname') job_input = job_ctx.get('input') if not job_input: raise ValueError('Invalid job_ctx: missing job_input') # retrieve job input schema from job template to validate the job input node_profile_template = vnc_api.job_template_read( fq_name=job_template_fqname) input_schema = node_profile_template.get_job_template_input_schema() jsonschema.validate(job_input, input_schema) return job_input
def _validate_job_ctx(job_ctx): vnc_api = JobVncApi.vnc_init(job_ctx) job_template_fqname = job_ctx.get('job_template_fqname') if not job_template_fqname: raise ValueError('Invalid job_ctx: missing job_template_fqname') job_input = job_ctx.get('input') if not job_input: raise ValueError('Invalid job_ctx: missing job_input') # retrieve job input schema from job template to validate the job input server_discovery_template = vnc_api.job_template_read( fq_name=job_template_fqname ) input_schema = server_discovery_template.get_job_template_input_schema() jsonschema.validate(job_input, input_schema) return job_input
def cli_diff_list_update(self, job_ctx, cli_fq_name, total_commit_diff_list, final_commit_processed_list): try: vnc_lib = JobVncApi.vnc_init(job_ctx) pr_cli_obj_raw = vnc_lib.cli_config_read(fq_name=cli_fq_name) for item in final_commit_processed_list: total_commit_diff_list.remove(item) updated_dict = {"commit_diff_info": total_commit_diff_list} pr_cli_obj_raw.set_commit_diff_list(updated_dict) vnc_lib.cli_config_update(pr_cli_obj_raw) if len(total_commit_diff_list) == 0: return True except Exception as ex: errmsg = "Unexpected error: %s\n%s" % (str(ex), traceback.format_exc()) _task_error_log(errmsg)
def _create_hardware_inventory(self, job_ctx, hardware_inventory_payload, prouter_name, pr_product_name, prouter_vendor): object_type = "hardware_inventory" hardware_iven_obj = prouter_vendor + "__" + pr_product_name json_data =\ hardware_inventory_payload['hardware_inventory_inventory_info'] data_str = json.dumps(json_data) hardware_inventory_payload_final = { "parent_type": "physical-router", "fq_name": [ "default-global-system-config", prouter_name, hardware_iven_obj], "hardware_inventory_inventory_info": data_str } hardware_obj_key = "hardware_inventory_inventory_info" vnc_lib = JobVncApi.vnc_init(job_ctx) # try to update the existing hardware existing object else create new one. try: try: cls = JobVncApi.get_vnc_cls(object_type) inventory_obj = cls.from_dict( **hardware_inventory_payload_final) existing_obj = vnc_lib.hardware_inventory_read( fq_name=hardware_inventory_payload_final.get('fq_name')) existing_obj_dict = vnc_lib.obj_to_dict(existing_obj) to_be_upd_value =\ hardware_inventory_payload_final[hardware_obj_key] existing_value = existing_obj_dict.get(hardware_obj_key) if to_be_upd_value != existing_value: vnc_lib.hardware_inventory_update( inventory_obj) except NoIdError: vnc_lib.hardware_inventory_create(inventory_obj) except Exception as exc: raise Exception("Inventory object creation failed" " with exception: %s", str(exc))
def create_cli_obj(self, job_ctx, device_mgmt_ip, device_name): directory_path = PLAYBOOK_BASE + "/manual_config/" + device_mgmt_ip file_list = glob.glob(directory_path + "/*.diff") if len(file_list) == 0: return "No new cli commits found. Proceeding..." else: object_type = 'cli_config' device_cli_obj_name = device_name + "_" + "cli_config" cli_config_payload = { "parent_type": "physical-router", "fq_name": [ "default-global-system-config", device_name, device_cli_obj_name ] } # try to update an existing object else create and then update with # diff info try: vnc_lib = JobVncApi.vnc_init(job_ctx) pr_fq_name = ["default-global-system-config", device_name] pr_obj = vnc_lib.physical_router_read(fq_name=pr_fq_name) pr_obj.set_physical_router_cli_commit_state("out_of_sync") vnc_lib.physical_router_update(pr_obj) try: cls = JobVncApi.get_vnc_cls(object_type) cli_obj_payload = cls.from_dict(**cli_config_payload) existing_cli_obj = vnc_lib.cli_config_read( fq_name=cli_config_payload.get('fq_name')) updated_cli_obj = self._cli_config_obj_update( job_ctx, None, existing_cli_obj, file_list) except NoIdError: new_cli_uuid = vnc_lib.cli_config_create(cli_obj_payload) updated_cli_obj = self._cli_config_obj_update( job_ctx, new_cli_uuid, None, file_list) except Exception as exc: raise Exception( "Database operation has failed with " "exception: %s", str(exc)) return updated_cli_obj
def get_device_info(self, job_ctx, device_uuid): try: FilterLog.instance("HitlessUpgradeFilter") self.job_input = FilterModule._validate_job_ctx(job_ctx) self.fabric_uuid = self.job_input['fabric_uuid'] self.vncapi = JobVncApi.vnc_init(job_ctx) self.job_ctx = job_ctx self.ja = JobAnnotations(self.vncapi) self.advanced_parameters = self._get_advanced_params() self._cache_job_input() self.device_uuid = device_uuid device_info = self._get_device_info() return device_info except Exception as ex: errmsg = "Unexpected error getting device info: %s\n%s" % ( str(ex), traceback.format_exc() ) _task_error_log(errmsg) return { 'status': 'failure', 'error_msg': errmsg, }
def _create_interfaces_and_update_dataplane_ip(self, job_ctx, interfaces_payload, prouter_name): vnc_lib = JobVncApi.vnc_init(job_ctx) physical_interfaces_list = interfaces_payload.get( 'physical_interfaces_list') logical_interfaces_list = interfaces_payload.get( 'logical_interfaces_list') dataplane_ip = interfaces_payload.get('dataplane_ip', "") vnc_physical_interfaces_list, vnc_logical_interfaces_list = \ self.get_create_interfaces_payload( prouter_name, physical_interfaces_list, logical_interfaces_list) phy_intfs_success_names, phy_intf_failed_info =\ self._create_physical_interfaces( vnc_lib, vnc_physical_interfaces_list) log_intfs_success_names, log_intf_failed_info =\ self._create_logical_interfaces( vnc_lib, vnc_logical_interfaces_list) dataplane_ip, dataplane_ip_upd_resp, warning_info =\ self._update_dataplane_ip( vnc_lib, dataplane_ip, prouter_name) return { "phy_intfs_success_names": list(set(phy_intfs_success_names)), "log_intfs_success_names": list(set(log_intfs_success_names)), "phy_intf_failed_info": phy_intf_failed_info, "log_intf_failed_info": log_intf_failed_info, "dataplane_ip": dataplane_ip, "dataplane_ip_upd_resp": dataplane_ip_upd_resp, "warning_info": warning_info }
def get_hitless_upgrade_plan(self, job_ctx, image_upgrade_list): try: FilterLog.instance("HitlessUpgradeFilter") self.job_input = FilterModule._validate_job_ctx(job_ctx) self.fabric_uuid = self.job_input['fabric_uuid'] self.vncapi = JobVncApi.vnc_init(job_ctx) self.job_ctx = job_ctx self.ja = JobAnnotations(self.vncapi) self.advanced_parameters = self._get_advanced_params() self._cache_job_input() self.batch_limit = self.advanced_parameters.get( 'bulk_device_upgrade_count') self.image_upgrade_list = image_upgrade_list upgrade_plan = self._get_hitless_upgrade_plan() return upgrade_plan except Exception as ex: errmsg = "Unexpected error: %s\n%s" % ( str(ex), traceback.format_exc() ) _task_error_log(errmsg) return { 'status': 'failure', 'error_msg': errmsg, }
def update_physical_router(self, job_ctx, prouter_name, obj_dict_payload, prouter_vendor): """ :param job_ctx: Dictionary example: { "auth_token": "EB9ABC546F98", "job_input": { "fabric_fq_name": [ "default-global-system-config", "fab01" ], "device_auth": [{ "username": "******", "password": "******" }] } } :param prouter_name: String example: "5c3-qfx8" :param obj_dict_payload: Dictionary example: { "physical_router_dataplane_ip": "10.0.0.2", "physical_router_loopback_ip": "10.0.0.2", "fq_name": ["default-global-system-config","5c3-qfx8"], "physical_router_os_version": "srx-20.65", "additional_properties": { 'Some Key that needs to be tapped': 'Some random annotations spec. to this vendor' } } :param prouter_vendor: String example: "juniper" :return: Dictionary if success, returns { 'status': 'success', 'upd_pr_log': <String: upd_pr_log>, 'physical_router_upd_resp': <Dictionary: physical_router_upd_resp> } if failure, returns { 'status': 'failure', 'error_msg': <String: exception message>, 'upd_pr_log': <String: upd_pr_log> } :param physical_router_upd_resp: Dictionary example: { "job_log_msg": <String: job log message>, "warning_info": <Dictionary: warning_info> } """ FilterLog.instance("UpdatePhysicalRouterFilter", prouter_name) _task_log("Starting Device Update") try: _task_log("Creating vnc handle") self.vnc_lib = JobVncApi.vnc_init(job_ctx) _task_log("Parsing additional physical router properties") FilterModule._parse_additional_prop_and_upd_payload( obj_dict_payload, prouter_vendor) _task_log("Updating the physical router") physical_router_upd_resp = \ self._update_physical_router_object( obj_dict_payload, prouter_name ) _task_done() return { 'status': 'success', 'upd_pr_log': FilterLog.instance().dump(), 'physical_router_upd_resp': physical_router_upd_resp } except Exception as ex: _task_error_log(str(ex)) _task_error_log(traceback.format_exc()) return { 'status': 'failure', 'error_msg': str(ex), 'upd_pr_log': FilterLog.instance().dump() }
def _import_chassis_info(self, job_ctx, chassis_payload, prouter_name, prouter_vendor): warning_info = [] chassis_mac_ids = [] chassis_mac_fqnames = [] object_type = "device_chassis" vnc_lib = JobVncApi.vnc_init(job_ctx) # try to create device_chassis objects from chassis_payload for chassis_info in chassis_payload: chassis_mac_fqname = chassis_info.get('fq_name') chassis_mac_id = chassis_mac_fqname[-1].split( prouter_vendor + '_')[1].replace('_', ':') try: try: cls = JobVncApi.get_vnc_cls(object_type) chassis_obj = cls.from_dict(**chassis_info) existing_obj = vnc_lib.device_chassis_read( fq_name=chassis_mac_fqname) existing_obj_dict = vnc_lib.obj_to_dict(existing_obj) for key in chassis_info: to_be_upd_value = chassis_info[key] existing_value = existing_obj_dict.get(key) if to_be_upd_value != existing_value: vnc_lib.device_chassis_update( chassis_obj) break chassis_mac_fqnames.append( chassis_mac_fqname) chassis_mac_ids.append( chassis_mac_id ) except NoIdError: vnc_lib.device_chassis_create(chassis_obj) chassis_mac_fqnames.append( chassis_mac_fqname) chassis_mac_ids.append( chassis_mac_id ) except Exception as exc: _task_error_log(str(exc)) _task_error_log(traceback.format_exc()) warn_info = dict() warn_info['failed_operation'] = 'Device Chassis Creation' warn_info['failure_message'] = 'Error while creating' \ 'chassis_id(' + \ chassis_mac_id + \ ') for ' + prouter_vendor + \ ' device: ' + str(exc) warning_info.append(warn_info) # Now try to link all the created chassis objects as references # to this prouter object_type = "physical_router" device_chassis_ref_list = [] for chassis_fqname in chassis_mac_fqnames: device_chassis_ref_list.append( {'to': chassis_fqname} ) try: pr_dict = { 'fq_name': ['default-global-system-config', prouter_name] } cls = JobVncApi.get_vnc_cls(object_type) physical_router_obj = cls.from_dict(**pr_dict) physical_router_obj.set_device_chassis_list( device_chassis_ref_list) vnc_lib.physical_router_update(physical_router_obj) except Exception as ex: _task_error_log(str(ex)) _task_error_log(traceback.format_exc()) warn_info = dict() warn_info['failed_operation'] = 'Physical Router Updation' warn_info['failure_message'] = 'Error while updating' \ 'chassis_ids refs for ' + \ prouter_vendor + ' device ' + \ prouter_name + ': ' + str(ex) warning_info.append(warn_info) return \ { 'chassis_mac_ids': chassis_mac_ids, 'warning_info': warning_info }
def discover_role(self, job_ctx, prouter_name, prouter_uuid, prouter_vendor_name, prouter_product_name): node_profile_refs = [] try: FilterLog.instance("DiscoverRoleFilter", prouter_name) FilterModule._validate_job_ctx(job_ctx) fabric_fqname = job_ctx.get('fabric_fqname') vnc_lib = JobVncApi.vnc_init(job_ctx) # form the hardware fqname from prouter_vendor_name and # prouter_product_name hw_fq_name = [(prouter_vendor_name + '-' + prouter_product_name ).lower()] # read the hardware object with this fq_name _task_log("Reading the hardware object") try: hw_obj = vnc_lib.hardware_read(fq_name=hw_fq_name) except NoIdError as no_id_exc: _task_log("\nHardware Object not present in " "database: " + str(no_id_exc)) traceback.print_exc(file=sys.stdout) _task_log("Completing role discovery for device") _task_done() return { 'status': 'success', 'fabric_fqname': fabric_fqname, 'np_refs': node_profile_refs, 'device_name': prouter_name, 'role_discovery_log': FilterLog.instance().dump() } _task_done() # get all the node-profile back-refs for this hardware object _task_log("Getting all the node-profile back refs" \ " for the hardware: %s" % hw_fq_name[-1]) np_back_refs = hw_obj.get_node_profile_back_refs() or [] _task_done() # get the fabric object fq_name to check if the node-profile # is in the same fabric _task_log("Fetching the fabric fq_name") fab_fq_name = fabric_fqname.split(":") _task_done() # read the fabric_object to get a list of node-profiles under # this fabric_object _task_log("Reading the fabric object") fabric_obj = vnc_lib.fabric_read(fq_name=fab_fq_name) _task_done() # get the list of node profile_uuids under the given fabric _task_log("Getting the list of node-profile-uuids" \ " under this fabric object .... ") node_profiles_list = fabric_obj.get_node_profile_refs() or [] node_profile_obj_uuid_list = self._get_object_uuid_list( node_profiles_list) _task_done() # check to see which of the node-profile back refs are in the # present fabric. Assumption: at present there is only a single # node-profile that can match a hardware under the current # given fabric _task_log("Checking to see if any node-profile" \ " is under given fabric .... \n") upd_resp, node_profile_refs = self._do_role_discovery( np_back_refs, prouter_name, vnc_lib, prouter_uuid, node_profile_obj_uuid_list) _task_log(upd_resp + "\n") _task_done() return { 'status': 'success', 'np_refs': node_profile_refs, 'fabric_fqname': fabric_fqname, 'device_name': prouter_name, 'role_discovery_log': FilterLog.instance().dump() } except NoIdError as no_id_exc: _task_error_log("Object not present in database: " + str(no_id_exc)) traceback.print_exc(file=sys.stdout) return { 'status': 'failure', 'error_msg': str(no_id_exc), 'role_discovery_log': FilterLog.instance().dump() } except Exception as ex: _task_error_log(str(ex)) traceback.print_exc(file=sys.stdout) return { 'status': 'failure', 'error_msg': str(ex), 'role_discovery_log': FilterLog.instance().dump() }
def discover_role(self, job_ctx, prouter_name, prouter_uuid, prouter_vendor_name, prouter_product_name): node_profile_refs = [] try: FilterLog.instance("DiscoverRoleFilter", prouter_name) FilterModule._validate_job_ctx(job_ctx) fabric_fqname = job_ctx.get('fabric_fqname') vnc_lib = JobVncApi.vnc_init(job_ctx) # form the hardware fqname from prouter_vendor_name and # prouter_product_name hw_fq_name = [ (prouter_vendor_name + '-' + prouter_product_name).lower() ] # read the hardware object with this fq_name _task_log("Reading the hardware object") try: hw_obj = vnc_lib.hardware_read(fq_name=hw_fq_name) except NoIdError as no_id_exc: _task_log("\nHardware Object not present in " "database: " + str(no_id_exc)) traceback.print_exc(file=sys.stdout) _task_log("Completing role discovery for device") _task_done() return { 'status': 'success', 'fabric_fqname': fabric_fqname, 'np_refs': node_profile_refs, 'device_name': prouter_name, 'role_discovery_log': FilterLog.instance().dump() } _task_done() # get all the node-profile back-refs for this hardware object _task_log("Getting all the node-profile back refs" \ " for the hardware: %s" % hw_fq_name[-1]) np_back_refs = hw_obj.get_node_profile_back_refs() or [] _task_done() # get the fabric object fq_name to check if the node-profile # is in the same fabric _task_log("Fetching the fabric fq_name") fab_fq_name = fabric_fqname.split(":") _task_done() # read the fabric_object to get a list of node-profiles under # this fabric_object _task_log("Reading the fabric object") fabric_obj = vnc_lib.fabric_read(fq_name=fab_fq_name) _task_done() # get the list of node profile_uuids under the given fabric _task_log("Getting the list of node-profile-uuids" \ " under this fabric object .... ") node_profiles_list = fabric_obj.get_node_profile_refs() or [] node_profile_obj_uuid_list = self._get_object_uuid_list( node_profiles_list) _task_done() # check to see which of the node-profile back refs are in the # present fabric. Assumption: at present there is only a single # node-profile that can match a hardware under the current # given fabric _task_log("Checking to see if any node-profile" \ " is under given fabric .... \n") upd_resp, node_profile_refs = self._do_role_discovery( np_back_refs, prouter_name, vnc_lib, prouter_uuid, node_profile_obj_uuid_list) _task_log(upd_resp + "\n") _task_done() return { 'status': 'success', 'np_refs': node_profile_refs, 'fabric_fqname': fabric_fqname, 'device_name': prouter_name, 'role_discovery_log': FilterLog.instance().dump() } except NoIdError as no_id_exc: _task_error_log("Object not present in database: " + str(no_id_exc)) traceback.print_exc(file=sys.stdout) return { 'status': 'failure', 'error_msg': str(no_id_exc), 'role_discovery_log': FilterLog.instance().dump() } except Exception as ex: _task_error_log(str(ex)) traceback.print_exc(file=sys.stdout) return { 'status': 'failure', 'error_msg': str(ex), 'role_discovery_log': FilterLog.instance().dump() }
def create_os_node_filter(self, job_ctx, devices_command_outputs): """ Param (devices_command_outputs) is a result from "show lldp neighbors detail" command. This param was gathered automatically in previous task, when above command was run on all leaf devices in fabric. :param devices_command_outputs: Dictionary example: { 'msg': u'All items completed', 'changed': False, 'results': [ { "parsed_output": { "lldp-neighbors-information": { "lldp-neighbor-information": [ { (...) "lldp-local-interface": "xe-0/0/0", (...) "lldp-remote-management-address": "10.5.5.5", (...) "lldp-remote-port-description": "ens256", "lldp-remote-port-id": "00:0c:29:13:37:c5" } ] } } } } ] } :param job_ctx: Dictionary example: { 'job_transaction_descr': 'Discover OS Computes', 'fabric_uuid': '123412341234-123412341234', 'contrail_command_host': '10.10.10.10:9091', 'cc_username': '******', 'cc_password': "******" } :return: Dictionary if success, returns { 'status': 'success' 'os_compute_nodes': { 'nodes': [ { 'name': 'node-1' 'node_type': 'ovs-compute', 'ports': [{ 'address': '00:0c:29:13:37:c5', 'port_name': 'xe-0/0/0', 'switch_name': 'VM283DF6BA00', 'name': 'ens256' }] } ] } } if failure, returns { 'status': 'failure', 'error_msg': <string: error message> } """ try: FilterModule._validate_job_ctx(job_ctx) job_input = job_ctx.get('input') vnc_api = JobVncApi.vnc_init(job_ctx) fabric_uuid = job_input['fabric_uuid'] cluster_id = job_ctx.get('contrail_cluster_id') cluster_token = job_ctx.get('auth_token') cc_host = job_input['contrail_command_host'] cc_username = job_input['cc_username'] cc_password = job_input['cc_password'] cc_node_obj = CreateCCNode(cc_host, cluster_id, cluster_token, cc_username, cc_password) os_compute_nodes = self.create_os_node(vnc_api, devices_command_outputs, fabric_uuid, cc_node_obj) except Exception as e: errmsg = "Unexpected error: %s\n%s" % (str(e), traceback.format_exc()) return { STATUS: FAILURE, ERRMSG: errmsg, } return { STATUS: SUCCESS, OS_COMPUTE_NODES: os_compute_nodes, }
def cli_sync(self, job_ctx, prouter_name, pr_uuid, devices_cli, device_mgmt_ip): # find the appropriate device info try: vnc_lib = JobVncApi.vnc_init(job_ctx) partial_accepted_config = "" action_to_be_performed = "" pr_commit_processed_list = [] pr_cli_obj_raw = "" pr_commit_diff_list = [] for dict_item in devices_cli: if dict_item["device_uuid"] == pr_uuid: # Read the referenced cli object from PR device_cli_objects = dict_item['cli_objects'] device_cli_objects = sorted(device_cli_objects, key = lambda i: i['time']) cli_obj_name = prouter_name + "_" + "cli_config" cli_fq_name = [ 'default-global-system-config', prouter_name, cli_obj_name] pr_cli_obj_raw = vnc_lib.cli_config_read( fq_name=cli_fq_name) pr_cli_obj = vnc_lib.obj_to_dict(pr_cli_obj_raw) pr_commit_diff_list = \ pr_cli_obj["commit_diff_list"]["commit_diff_info"] # Process each of the items, delete items from cli object # if processed successfully. for pr_commit_item in pr_commit_diff_list: for cli_item in device_cli_objects: if cli_item.get('time') == pr_commit_item.get( 'time'): pr_commit_processed_list.append(pr_commit_item) action_to_be_performed = cli_item.get('status') if action_to_be_performed == "accept": partial_accepted_config += \ self._accept_routine(pr_commit_item, device_mgmt_ip) elif action_to_be_performed == "reject": self._reject_routine(pr_commit_item, device_mgmt_ip) else: self._ignore_routine(pr_commit_item, device_mgmt_ip) # Process file one last time for any delete commands for accept # case file_path = PLAYBOOK_BASE + "/manual_config" "/" + \ device_mgmt_ip + "/approve_config.conf" if action_to_be_performed == "accept" and os.path.exists(file_path): complete_accepted_config = self._process_file_for_delete_commands( partial_accepted_config, device_mgmt_ip) original_acc_config = pr_cli_obj_raw.get_accepted_cli_config() if original_acc_config is None: original_acc_config = "" updated_acc_config = original_acc_config + complete_accepted_config pr_cli_obj_raw.set_accepted_cli_config(updated_acc_config) vnc_lib.cli_config_update(pr_cli_obj_raw) data_dict = { "pr_commit_diff_list": pr_commit_diff_list, "pr_commit_item": pr_commit_processed_list, "cli_fq_name": cli_fq_name } self.merge_files(device_mgmt_ip) return data_dict except Exception as ex: errmsg = "Unexpected error: %s\n%s" % ( str(ex), traceback.format_exc() ) _task_error_log(errmsg) return { 'status': 'failure', 'error_msg': errmsg, }
def update_physical_router(self, job_ctx, prouter_name, obj_dict_payload, prouter_vendor): """ :param job_ctx: Dictionary example: { "auth_token": "EB9ABC546F98", "job_input": { "fabric_fq_name": [ "default-global-system-config", "fab01" ], "device_auth": [{ "username": "******", "password": "******" }] } } :param prouter_name: String example: "5c3-qfx8" :param obj_dict_payload: Dictionary example: { "physical_router_dataplane_ip": "10.0.0.2", "physical_router_loopback_ip": "10.0.0.2", "fq_name": ["default-global-system-config","5c3-qfx8"], "physical_router_os_version": "srx-20.65", "additional_properties": { 'Some Key that needs to be tapped': 'Some random annotations spec. to this vendor' } } :param prouter_vendor: String example: "juniper" :return: Dictionary if success, returns { 'status': 'success', 'upd_pr_log': <String: upd_pr_log>, 'physical_router_upd_resp': <Dictionary: physical_router_upd_resp> } if failure, returns { 'status': 'failure', 'error_msg': <String: exception message>, 'upd_pr_log': <String: upd_pr_log> } :param physical_router_upd_resp: Dictionary example: { "job_log_msg": <String: job log message>, "warning_info": <Dictionary: warning_info> } """ FilterLog.instance("UpdatePhysicalRouterFilter", prouter_name) _task_log("Starting Device Update") try: _task_log("Creating vnc handle") self.vnc_lib = JobVncApi.vnc_init(job_ctx) _task_log("Parsing additional physical router properties") FilterModule._parse_additional_prop_and_upd_payload( obj_dict_payload, prouter_vendor) _task_log("Updating the physical router") physical_router_upd_resp = \ self._update_physical_router_object( obj_dict_payload, prouter_name ) _task_done() return { 'status': 'success', 'upd_pr_log': FilterLog.instance().dump(), 'physical_router_upd_resp': physical_router_upd_resp } except Exception as ex: _task_error_log(str(ex)) _task_error_log(traceback.format_exc()) return {'status': 'failure', 'error_msg': str(ex), 'upd_pr_log': FilterLog.instance().dump() }