예제 #1
0
    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
예제 #2
0
    def _update_dataplane_ip(self, vnc_lib, dataplane_ip, prouter_name):
        warning_info = {}
        object_type = "physical_router"
        if dataplane_ip == "":
            return "", "", warning_info
        else:
            try:
                obj_dict = {
                    "uuid": None,
                    "fq_name": ["default-global-system-config", prouter_name],
                    "physical_router_dataplane_ip": dataplane_ip,
                    "physical_router_loopback_ip": dataplane_ip
                }
                cls = JobVncApi.get_vnc_cls(object_type)
                physical_router_obj = cls.from_dict(**obj_dict)
                vnc_lib.physical_router_update(physical_router_obj)
                upd_resp = "\nUpdated device with dataplane ip: "
            except Exception as ex:
                _task_error_log(str(ex))
                _task_error_log(traceback.format_exc())
                upd_resp = "There was a problem while updating the" \
                           " device with dataplane ip: "
                warning_info = {
                    "device_name": prouter_name,
                    "dataplane_ip": dataplane_ip,
                    "warning_message": str(ex)
                }

        return dataplane_ip, upd_resp, warning_info
    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 _get_password(device_obj):
     """
     Get and return decrypted password.
     """
     password = device_obj.physical_router_user_credentials.get_password()
     return JobVncApi.decrypt_password(encrypted_password=password,
                                       pwd_key=device_obj.uuid)
예제 #5
0
    def _create_logical_interfaces(self, vnc_lib,
                                   logical_interfaces_payload):
        object_type = "logical_interface"
        success_intfs_names = []
        log_intf_failed_info = []

        for log_interface_dict in logical_interfaces_payload:
            try:
                try:
                    cls = JobVncApi.get_vnc_cls(object_type)
                    log_interface_obj = cls.from_dict(**log_interface_dict)
                    existing_obj = vnc_lib.logical_interface_read(
                        fq_name=log_interface_dict.get('fq_name'))
                    existing_obj_dict = vnc_lib.obj_to_dict(existing_obj)
                    for key in log_interface_dict:
                        to_be_upd_value = log_interface_dict[key]
                        existing_value = existing_obj_dict.get(key)
                        if to_be_upd_value != existing_value:
                            vnc_lib.logical_interface_update(log_interface_obj)
                            break
                    success_intfs_names.append(
                        log_interface_dict['fq_name'][-1])
                except NoIdError as exc:
                    vnc_lib.logical_interface_create(log_interface_obj)
                    success_intfs_names.append(
                        log_interface_dict['fq_name'][-1])
            except Exception as exc:
                _task_error_log(str(exc))
                _task_error_log(traceback.format_exc())
                log_intf_failed_info.append({
                    "log_interface_name": log_interface_dict['fq_name'][-1],
                    "failure_msg": str(exc)
                })
        return success_intfs_names, log_intf_failed_info
예제 #6
0
 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,
         }
예제 #7
0
    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
예제 #8
0
 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 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()
            }
예제 #10
0
    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
        }
예제 #11
0
 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,
         }
예제 #12
0
    def _create_logical_interfaces(self, vnc_lib,
                                   logical_interfaces_payload):
        object_type = "logical_interface"
        success_intfs_names = []
        log_intf_failed_info = []

        for log_interface_dict in logical_interfaces_payload:
            try:
                try:
                    cls = JobVncApi.get_vnc_cls(object_type)
                    log_interface_obj = cls.from_dict(**log_interface_dict)
                    existing_obj = vnc_lib.logical_interface_read(
                        fq_name=log_interface_dict.get('fq_name'))
                    existing_obj_dict = vnc_lib.obj_to_dict(existing_obj)
                    for key in log_interface_dict:
                        to_be_upd_value = log_interface_dict[key]
                        existing_value = existing_obj_dict.get(key)
                        if to_be_upd_value != existing_value:
                            vnc_lib.logical_interface_update(log_interface_obj)
                            break
                    success_intfs_names.append(
                        log_interface_dict['fq_name'][-1])
                except NoIdError as exc:
                    vnc_lib.logical_interface_create(log_interface_obj)
                    success_intfs_names.append(
                        log_interface_dict['fq_name'][-1])
            except Exception as exc:
                _task_error_log(str(exc))
                _task_error_log(traceback.format_exc())
                log_intf_failed_info.append({
                    "log_interface_name": log_interface_dict['fq_name'][-1],
                    "failure_msg": str(exc)
                })
        return success_intfs_names, log_intf_failed_info
    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))
예제 #14
0
 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
예제 #15
0
 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,
        }
예제 #17
0
    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)
예제 #18
0
 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,
         }
예제 #19
0
    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
예제 #21
0
    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
예제 #22
0
 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 _update_physical_router_object(self, obj_dict_payload, prouter_name):

        warning_info = {}

        object_type = "physical_router"

        try:
            cls = JobVncApi.get_vnc_cls(object_type)
            physical_router_obj = cls.from_dict(**obj_dict_payload)
            self.vnc_lib.physical_router_update(physical_router_obj)
            job_log_msg = "- loopback_ip: %s\n   - OS Version: %s\n" \
                          % (
                              physical_router_obj.
                              get_physical_router_loopback_ip(),
                              physical_router_obj.
                              get_physical_router_os_version()
                          )

        except NoIdError as exc:
            _task_error_log(str(exc))
            _task_error_log(traceback.format_exc())
            job_log_msg = "The device being updated was not found" \
                          " in the vnc database"
            warning_info = {
                "device_name": prouter_name,
                "obj_dict_payload": obj_dict_payload,
                "warning_message": str(exc)
            }

        except Exception as ex:
            _task_error_log(str(ex))
            _task_error_log(traceback.format_exc())
            job_log_msg = "There was a problem while updating the" \
                " device"
            warning_info = {
                "device_name": prouter_name,
                "obj_dict_payload": obj_dict_payload,
                "warning_message": str(ex)
            }

        return {
            "warning_info": warning_info,
            "job_log_msg": job_log_msg
        }
    def _update_physical_router_object(self, obj_dict_payload, prouter_name):

        warning_info = {}

        object_type = "physical_router"

        try:
            cls = JobVncApi.get_vnc_cls(object_type)
            physical_router_obj = cls.from_dict(**obj_dict_payload)
            self.vnc_lib.physical_router_update(physical_router_obj)
            job_log_msg = "- loopback_ip: %s\n   - OS Version: %s\n" \
                          % (
                              physical_router_obj.
                                  get_physical_router_loopback_ip(),
                              physical_router_obj.
                                  get_physical_router_os_version()
                          )

        except NoIdError as exc:
            _task_error_log(str(exc))
            _task_error_log(traceback.format_exc())
            job_log_msg = "The device being updated was not found" \
                          " in the vnc database"
            warning_info = {
                "device_name": prouter_name,
                "obj_dict_payload": obj_dict_payload,
                "warning_message": str(exc)
            }

        except Exception as ex:
            _task_error_log(str(ex))
            _task_error_log(traceback.format_exc())
            job_log_msg = "There was a problem while updating the" \
                       " device"
            warning_info = {
                "device_name": prouter_name,
                "obj_dict_payload": obj_dict_payload,
                "warning_message": str(ex)
            }

        return {
            "warning_info": warning_info,
            "job_log_msg": job_log_msg
        }
 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,
         }
예제 #26
0
    def get_ztp_tftp_config(cls, job_ctx, fabric_uuid):
        tftp_config = {}
        try:
            vncapi = VncApi(auth_type=VncApi._KEYSTONE_AUTHN_STRATEGY,
                            auth_token=job_ctx.get('auth_token'))
            fabric = vncapi.fabric_read(id=fabric_uuid)
            fabric_dict = vncapi.obj_to_dict(fabric)
            fabric_creds = fabric_dict.get('fabric_credentials')
            if fabric_creds:
                device_creds = fabric_creds.get('device_credential')
                if device_creds:
                    dev_cred = device_creds[0]
                    password = JobVncApi.decrypt_password(
                        encrypted_password=dev_cred['credential']['password'],
                        admin_password=job_ctx.get(
                            'vnc_api_init_params').get(
                            'admin_password'))
                    tftp_config['password'] = password
        except Exception as ex:
            logging.error("Error getting ZTP TFTP configuration: {}".format(ex))

        return tftp_config
    def get_ztp_tftp_config(cls, job_ctx, fabric_uuid):
        tftp_config = {}
        try:
            vncapi = VncApi(auth_type=VncApi._KEYSTONE_AUTHN_STRATEGY,
                            auth_token=job_ctx.get('auth_token'))
            fabric = vncapi.fabric_read(id=fabric_uuid)
            fabric_dict = vncapi.obj_to_dict(fabric)
            fabric_creds = fabric_dict.get('fabric_credentials')
            if fabric_creds:
                device_creds = fabric_creds.get('device_credential')
                if device_creds:
                    dev_cred = device_creds[0]
                    password = JobVncApi.decrypt_password(
                        encrypted_password=dev_cred['credential']['password'],
                        admin_password=job_ctx.get('vnc_api_init_params').get(
                            'admin_password'))
                    tftp_config['password'] = password
        except Exception as ex:
            logging.error(
                "Error getting ZTP TFTP configuration: {}".format(ex))

        return tftp_config
예제 #28
0
    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,
         }
예제 #30
0
    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()
            }
예제 #31
0
    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 _get_password(self, device_obj):
     return JobVncApi.decrypt_password(
         encrypted_password=device_obj.physical_router_user_credentials.\
             get_password(),
         admin_password=self.job_ctx.get('vnc_api_init_params').\
             get('admin_password'))
예제 #33
0
    def initial_processing(self, concurrent):
        self.serial_num_flag = False
        self.all_serial_num = []
        serial_num = []
        self.per_greenlet_percentage = None

        self.job_ctx['current_task_index'] = 2

        try:
            total_percent = self.job_ctx.get('playbook_job_percentage')
            if total_percent:
                total_percent = float(total_percent)

            # Calculate the total percentage of this entire greenlet based task
            # This will be equal to the percentage alloted to this task in the
            # weightage array off the total job percentage. For example:
            # if the task weightage array is [10, 85, 5] and total job %
            # is 95. Then the 2nd task's effective total percentage is 85% of
            # 95%
            total_task_percentage = self.module.calculate_job_percentage(
                self.job_ctx.get('total_task_count'),
                task_seq_number=self.job_ctx.get('current_task_index'),
                total_percent=total_percent,
                task_weightage_array=self.job_ctx.get(
                    'task_weightage_array'))[0]

            # Based on the number of greenlets spawned (i.e num of sub tasks)
            # split the total_task_percentage equally amongst the greenlets.
            self.logger.info("Number of greenlets: {} and total_percent: "
                             "{}".format(concurrent, total_task_percentage))
            self.per_greenlet_percentage = \
                self.module.calculate_job_percentage(
                    concurrent, total_percent=total_task_percentage)[0]
            self.logger.info("Per greenlet percent: "
                             "{}".format(self.per_greenlet_percentage))

            self.vncapi = VncApi(auth_type=VncApi._KEYSTONE_AUTHN_STRATEGY,
                                 auth_token=self.job_ctx.get('auth_token'))
        except Exception as ex:
            self.logger.info("Percentage calculation failed with error "
                             "{}".format(str(ex)))

        try:
            self.vncapi = VncApi(auth_type=VncApi._KEYSTONE_AUTHN_STRATEGY,
                                 auth_token=self.job_ctx.get('auth_token'))
        except Exception as ex:
            self.module.results['failed'] = True
            self.module.results['msg'] = "Failed to connect to API server " \
                "due to error: %s"\
                % str(ex)
            self.module.exit_json(**self.module.results)

        # get credentials and serial number if greenfield
        if self.total_retry_timeout:
            # get device credentials
            fabric = self.vncapi.fabric_read(id=self.fabric_uuid)
            fabric_object = self.vncapi.obj_to_dict(fabric)
            self.credentials = fabric_object.get('fabric_credentials').get(
                'device_credential')

            # get serial numbers
            fabric_namespace_obj_list = self.vncapi.fabric_namespaces_list(
                parent_id=self.fabric_uuid, detail=True)
            fabric_namespace_list = self.vncapi.obj_to_dict(
                fabric_namespace_obj_list)

            for namespace in fabric_namespace_list:
                if namespace.get('fabric_namespace_type') == "SERIAL_NUM":
                    self.serial_num_flag = True
                    serial_num.append(namespace.get(
                        'fabric_namespace_value').get('serial_num'))

            if len(serial_num) > 1:
                for outer_list in serial_num:
                    for sn in outer_list:
                        self.all_serial_num.append(sn)

        else:
            self.credentials = self.module.params['credentials']

        for cred in self.credentials:
            if cred.get('credential', {}).get('password'):
                cred['credential']['password'] = JobVncApi.decrypt_password(
                    encrypted_password=cred.get('credential', {}).get('password'),
                    admin_password=self.job_ctx.get('vnc_api_init_params').get(
                        'admin_password'))
예제 #34
0
 def _get_password(self, device_obj):
     return JobVncApi.decrypt_password(
         encrypted_password=device_obj.physical_router_user_credentials.
         get_password(),
         pwd_key=device_obj.uuid)
예제 #35
0
    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 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 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,
        }
예제 #38
0
 def _get_password(self, device_obj):
     return JobVncApi.decrypt_password(
         encrypted_password=device_obj.physical_router_user_credentials.\
             get_password(),
         admin_password=self.job_ctx.get('vnc_api_init_params').\
             get('admin_password'))
예제 #39
0
    def read_device_data(self,
                         device_list,
                         request_params,
                         job_exec_id,
                         is_delete=False):
        device_data = dict()

        for device_id in device_list:
            if not is_delete:
                try:
                    (ok, result) = self.db_read("physical-router", device_id, [
                        'physical_router_user_credentials',
                        'physical_router_management_ip', 'fq_name',
                        'physical_router_device_family',
                        'physical_router_vendor_name',
                        'physical_router_product_name', 'fabric_refs'
                    ])
                    if not ok:
                        msg = "Error while reading the physical router " \
                              "with id %s : %s" % (device_id, result)
                        raise JobException(msg, job_exec_id)
                except NoIdError as ex:
                    msg = "Device not found" \
                          "%s: %s" % (device_id, str(ex))
                    raise JobException(msg, job_exec_id)
                except Exception as e:
                    msg = "Exception while reading device %s %s " % \
                          (device_id, str(e))
                    raise JobException(msg, job_exec_id)

                device_fq_name = result.get('fq_name')
                device_mgmt_ip = result.get('physical_router_management_ip')
                user_cred = result.get('physical_router_user_credentials')

                device_family = result.get("physical_router_device_family")
                device_vendor_name = result.get("physical_router_vendor_name")
                device_product_name = result.get(
                    "physical_router_product_name")

                fabric_refs = result.get('fabric_refs')
                if fabric_refs:
                    fabric_fq_name = result.get('fabric_refs')[0].get('to')
                    fabric_fq_name_str = ':'.join(fabric_fq_name)
                    request_params['fabric_fq_name'] = fabric_fq_name_str
            else:
                device_mgmt_ip = request_params.get(
                    'input', {}).get('device_management_ip')
                device_abs_cfg = request_params.get(
                    'input', {}).get('device_abstract_config')

                system = device_abs_cfg.get('system', {})
                device_name = system.get('name')
                device_username = system.get('credentials',
                                             {}).get('user_name')
                device_password = system.get('credentials', {}).get('password')
                user_cred = {
                    "username": device_username,
                    "password": device_password
                }
                device_family = system.get('device_family')
                device_vendor_name = system.get('vendor_name')
                device_product_name = system.get('product_name')
                device_fq_name = ["default-global-system-config", device_name]
                self.read_fabric_data(request_params, job_exec_id, is_delete)

            device_json = {"device_management_ip": device_mgmt_ip}
            device_json.update({"device_fqname": device_fq_name})

            if user_cred:
                device_json.update(
                    {"device_username": user_cred.get('username')})
                decrypt_password = JobVncApi.decrypt_password(
                    encrypted_password=user_cred.get('password'),
                    pwd_key=device_id)
                device_json.update({"device_password": decrypt_password})
            if device_family:
                device_json.update({"device_family": device_family})

            if device_vendor_name:
                device_json.update({"device_vendor": device_vendor_name})

            if device_product_name:
                device_json.update({"device_product": device_product_name})

            device_data.update({device_id: device_json})

        if len(device_data) > 0:
            request_params.update({"device_json": device_data})
    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()
                   }
예제 #41
0
    def initial_processing(self, concurrent):
        self.serial_num_flag = False
        self.all_serial_num = []
        serial_num = []
        self.per_greenlet_percentage = None

        self.job_ctx['current_task_index'] = 2

        try:
            total_percent = self.job_ctx.get('playbook_job_percentage')
            if total_percent:
                total_percent = float(total_percent)

            # Calculate the total percentage of this entire greenlet based task
            # This will be equal to the percentage alloted to this task in the
            # weightage array off the total job percentage. For example:
            # if the task weightage array is [10, 85, 5] and total job %
            # is 95. Then the 2nd task's effective total percentage is 85% of
            # 95%
            total_task_percentage = self.module.calculate_job_percentage(
                self.job_ctx.get('total_task_count'),
                task_seq_number=self.job_ctx.get('current_task_index'),
                total_percent=total_percent,
                task_weightage_array=self.job_ctx.get(
                    'task_weightage_array'))[0]

            # Based on the number of greenlets spawned (i.e num of sub tasks)
            # split the total_task_percentage equally amongst the greenlets.
            self.logger.info("Number of greenlets: {} and total_percent: "
                             "{}".format(concurrent, total_task_percentage))
            self.per_greenlet_percentage = \
                self.module.calculate_job_percentage(
                    concurrent, total_percent=total_task_percentage)[0]
            self.logger.info("Per greenlet percent: "
                             "{}".format(self.per_greenlet_percentage))

            self.vncapi = VncApi(auth_type=VncApi._KEYSTONE_AUTHN_STRATEGY,
                                 auth_token=self.job_ctx.get('auth_token'))
        except Exception as ex:
            self.logger.info("Percentage calculation failed with error "
                             "{}".format(str(ex)))

        try:
            self.vncapi = VncApi(auth_type=VncApi._KEYSTONE_AUTHN_STRATEGY,
                                 auth_token=self.job_ctx.get('auth_token'))
        except Exception as ex:
            self.module.results['failed'] = True
            self.module.results['msg'] = "Failed to connect to API server " \
                "due to error: %s"\
                % str(ex)
            self.module.exit_json(**self.module.results)

        # get credentials and serial number if greenfield
        if self.total_retry_timeout:
            # get device credentials
            fabric = self.vncapi.fabric_read(id=self.fabric_uuid)
            fabric_object = self.vncapi.obj_to_dict(fabric)
            self.credentials = fabric_object.get('fabric_credentials').get(
                'device_credential')

            # get serial numbers
            fabric_namespace_obj_list = self.vncapi.fabric_namespaces_list(
                parent_id=self.fabric_uuid, detail=True)
            fabric_namespace_list = self.vncapi.obj_to_dict(
                fabric_namespace_obj_list)

            for namespace in fabric_namespace_list:
                if namespace.get('fabric_namespace_type') == "SERIAL_NUM":
                    self.serial_num_flag = True
                    serial_num.append(namespace.get(
                        'fabric_namespace_value').get('serial_num'))

            if len(serial_num) > 1:
                for outer_list in serial_num:
                    for sn in outer_list:
                        self.all_serial_num.append(sn)

        else:
            self.credentials = self.module.params['credentials']

        for cred in self.credentials:
            if cred.get('credential', {}).get('password'):
                cred['credential']['password'] = JobVncApi.decrypt_password(
                    encrypted_password=cred.get('credential', {}).get('password'),
                    admin_password=self.job_ctx.get('vnc_api_init_params').get(
                        'admin_password'))
예제 #42
0
    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
            }
예제 #43
0
 def decrypt_device_password(cls, encrypted_password, secret_key):
     return JobVncApi.decrypt_password(
         encrypted_password=encrypted_password, pwd_key=secret_key)