diff --git a/ansible/library/bgp_facts.py b/ansible/library/bgp_facts.py index 00b0c493ad5..f08c6c85b8c 100644 --- a/ansible/library/bgp_facts.py +++ b/ansible/library/bgp_facts.py @@ -48,34 +48,46 @@ class BgpModule(object): def __init__(self): + self.instances =[] self.module = AnsibleModule( argument_spec=dict( + num_npus=dict(), ), supports_check_mode=True) + m_args = self.module.params + + if 'num_npus' in m_args and m_args['num_npus'] is not '1': + npus = int(m_args['num_npus']) + for npu in range(0, npus): + self.instances.append("bgp{}".format(npu)) + else: + self.instances.append("bgp") + self.out = None self.facts = {} - + self.facts['bgp_neighbors'] = {} return def run(self): """ Main method of the class """ - self.collect_data('summary') - self.parse_summary() - self.collect_data('neighbor') - self.parse_neighbors() - self.get_statistics() + for instance in self.instances: + self.collect_data('summary', instance) + self.parse_summary() + self.collect_data('neighbor',instance) + self.parse_neighbors() + self.get_statistics() self.module.exit_json(ansible_facts=self.facts) - def collect_data(self, command_str): + def collect_data(self, command_str,instance): """ Collect bgp information by reading output of 'vtysh' command line tool """ + docker_cmd = 'docker exec -i {} vtysh -c "show ip bgp {}" '.format(instance,command_str) try: - rc, self.out, err = self.module.run_command('docker exec -i bgp vtysh -c "show ip bgp ' + command_str + '"', - executable='/bin/bash', use_unsafe_shell=True) + rc, self.out, err = self.module.run_command(docker_cmd, executable='/bin/bash', use_unsafe_shell=True) except Exception as e: self.module.fail_json(msg=str(e)) @@ -176,7 +188,7 @@ def parse_neighbors(self): except Exception as e: self.module.fail_json(msg=str(e)) - self.facts['bgp_neighbors'] = neighbors + self.facts['bgp_neighbors'].update(neighbors) return def get_statistics(self): diff --git a/ansible/library/config_facts.py b/ansible/library/config_facts.py index 72bb178e7b0..39d21faf32c 100644 --- a/ansible/library/config_facts.py +++ b/ansible/library/config_facts.py @@ -118,8 +118,13 @@ def main(): m_args = module.params try: config = {} + if m_args["source"] == "persistent": - with open(PERSISTENT_CONFIG_PATH, "r") as f: + if 'filename' in m_args and m_args['filename'] is not None: + cfg_file_path = "%s" % m_args['filename'] + else: + cfg_file_path = PERSISTENT_CONFIG_PATH + with open(cfg_file_path, "r") as f: config = json.load(f) elif m_args["source"] == "running": config = get_running_config(module) diff --git a/tests/common/devices.py b/tests/common/devices.py index e20dec213a8..6931d0b80ca 100644 --- a/tests/common/devices.py +++ b/tests/common/devices.py @@ -94,6 +94,37 @@ def __init__(self, ansible_adhoc, hostname, gather_facts=False): if gather_facts: self.gather_facts() + def _get_critical_services_for_multi_npu(): + """ + Update the critical_services with the service names for multi-npu platforms + """ + m_service = [] + for service in self.CRITICAL_SERVICES: + for npu in self.facts["num_npu"]: + npu_service = service+npu + m_service.insert(npu, npu_service) + self.CRITICAL_SERVICES = m_service + print self.CRITICAL_SERVICES + + def _get_npu_info(self): + """ + Check if the DUT is multi-npu platfrom and store the number of npus in the facts + """ + asic_conf_file_path = os.path.join('/usr/share/sonic/device', self.facts["platform"], 'asic.conf') + try: + output = self.shell('cat %s' % asic_conf_file_path)["stdout_lines"] + print output + for line in output: + num_npu=line.split("=",1)[1].strip() + print "num_npu = {}".format(num_npu) + self.facts["num_npu"] = int(num_npu) + except: + self.facts["num_npu"] =1 + + if self.facts["num_npu"] > 1: + self._get_critical_services_for_multi_npu + + def _platform_info(self): platform_info = self.command("show platform summary")["stdout_lines"] for line in platform_info: @@ -110,6 +141,7 @@ def gather_facts(self): """ self.facts = {} self._platform_info() + self._get_npu_info() logging.debug("SonicHost facts: %s" % json.dumps(self.facts)) def get_service_props(self, service, props=["ActiveState", "SubState"]): @@ -227,6 +259,35 @@ def get_pmon_daemon_list(self): logging.info("Pmon daemon list for this platform is %s" % str(daemon_list)) return daemon_list + def num_npus(self): + """ + return the number of NPUs on the DUT + """ + return self.facts["num_npu"] + + def get_syncd_docker_names(self): + """ + @summary: get the list of syncd dockers names for the number of NPUs present on the DUT + for a single NPU dut the list will have only "syncd" in it + """ + syncd_docker_names = [] + if self.facts["num_npu"] == 1: + syncd_docker_names.append("syncd") + else: + num_npus = int(self.facts["num_npu"]) + for npu in range(0,num_npus): + syncd_docker_names.append("syncd{}".format(npu)) + return syncd_docker_names + def get_swss_docker_names(self): + swss_docker_names = [] + if self.facts["num_npu"] == 1: + swss_docker_names.append("swss") + else: + num_npus = self.facts["num_npu"] + for npu in range(0,num_npus): + swss_docker_names.append("swss{}".format(npu)) + return swss_docker_names + def get_up_time(self): up_time_text = self.command("uptime -s")["stdout"] return datetime.strptime(up_time_text, "%Y-%m-%d %H:%M:%S") diff --git a/tests/test_bgp_fact.py b/tests/test_bgp_fact.py index 5570b12f130..19122249069 100644 --- a/tests/test_bgp_fact.py +++ b/tests/test_bgp_fact.py @@ -1,12 +1,12 @@ from ansible_host import AnsibleHost -def test_bgp_facts(ansible_adhoc, testbed): +def test_bgp_facts(ansible_adhoc, testbed,duthost): """compare the bgp facts between observed states and target state""" hostname = testbed['dut'] ans_host = AnsibleHost(ansible_adhoc, hostname) - - bgp_facts = ans_host.bgp_facts()['ansible_facts'] + npus = duthost.num_npus() + bgp_facts = ans_host.bgp_facts(num_npus=npus)['ansible_facts'] mg_facts = ans_host.minigraph_facts(host=hostname)['ansible_facts'] for k, v in bgp_facts['bgp_neighbors'].items():