Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions acl_loader/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
import json
import syslog
import tabulate
from natsort import natsorted
import sonic_device_util

import openconfig_acl
import pyangbind.lib.pybindJSON as pybindJSON
from swsssdk import ConfigDBConnector
from swsssdk import SonicV2Connector
from swsssdk import SonicDBConfig
from natsort import natsorted
from sonic_py_common import device_info
from swsssdk import ConfigDBConnector, SonicV2Connector, SonicDBConfig


def info(msg):
Expand Down Expand Up @@ -142,7 +140,7 @@ def __init__(self):

# Getting all front asic namespace and correspding config and state DB connector

namespaces = sonic_device_util.get_all_namespaces()
namespaces = device_info.get_all_namespaces()
for front_asic_namespaces in namespaces['front_ns']:
self.per_npu_configdb[front_asic_namespaces] = ConfigDBConnector(use_unix_socket_path=True, namespace=front_asic_namespaces)
self.per_npu_configdb[front_asic_namespaces].connect()
Expand Down
132 changes: 55 additions & 77 deletions config/main.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
#!/usr/sbin/env python

import sys
import os
import click
import ipaddress
import json
import subprocess
import netaddr
import re
import syslog
import time
import netifaces
import os
import re
import subprocess
import sys
import threading
import time

import sonic_device_util
import ipaddress
from swsssdk import ConfigDBConnector, SonicV2Connector, SonicDBConfig
from minigraph import parse_device_desc_xml
from sonic_py_common import device_info, logger
from swsssdk import ConfigDBConnector, SonicV2Connector, SonicDBConfig

import aaa
import mlnx
Expand All @@ -39,33 +38,12 @@

DEFAULT_NAMESPACE = ''

# Global logger instance
log = logger.Logger(SYSLOG_IDENTIFIER)

asic_type = None
config_db = None

# ========================== Syslog wrappers ==========================

def log_debug(msg):
syslog.openlog(SYSLOG_IDENTIFIER)
syslog.syslog(syslog.LOG_DEBUG, msg)
syslog.closelog()


def log_info(msg):
syslog.openlog(SYSLOG_IDENTIFIER)
syslog.syslog(syslog.LOG_INFO, msg)
syslog.closelog()


def log_warning(msg):
syslog.openlog(SYSLOG_IDENTIFIER)
syslog.syslog(syslog.LOG_WARNING, msg)
syslog.closelog()


def log_error(msg):
syslog.openlog(SYSLOG_IDENTIFIER)
syslog.syslog(syslog.LOG_ERR, msg)
syslog.closelog()

class AbbreviationGroup(click.Group):
"""This subclass of click.Group supports abbreviated subgroup/subcommand names
Expand Down Expand Up @@ -115,17 +93,17 @@ def execute_systemctl_per_asic_instance(inst, event, service, action):
click.echo("Executing {} of service {}@{}...".format(action, service, inst))
run_command("systemctl {} {}@{}.service".format(action, service, inst))
except SystemExit as e:
log_error("Failed to execute {} of service {}@{} with error {}".format(action, service, inst, e))
log.log_error("Failed to execute {} of service {}@{} with error {}".format(action, service, inst, e))
# Set the event object if there is a failure and exception was raised.
event.set()

# Execute action on list of systemd services
def execute_systemctl(list_of_services, action):
num_asic = sonic_device_util.get_num_npus()
num_asic = device_info.get_num_npus()
generated_services_list, generated_multi_instance_services = _get_sonic_generated_services(num_asic)
if ((generated_services_list == []) and
(generated_multi_instance_services == [])):
log_error("Failed to get generated services")
log.log_error("Failed to get generated services")
return

for service in list_of_services:
Expand All @@ -134,12 +112,12 @@ def execute_systemctl(list_of_services, action):
click.echo("Executing {} of service {}...".format(action, service))
run_command("systemctl {} {}".format(action, service))
except SystemExit as e:
log_error("Failed to execute {} of service {} with error {}".format(action, service, e))
log.log_error("Failed to execute {} of service {} with error {}".format(action, service, e))
raise

if (service + '.service' in generated_multi_instance_services):
# With Multi NPU, Start a thread per instance to do the "action" on multi instance services.
if sonic_device_util.is_multi_npu():
if device_info.is_multi_npu():
threads = []
# Use this event object to co-ordinate if any threads raised exception
e = threading.Event()
Expand Down Expand Up @@ -175,10 +153,10 @@ def run_command(command, display_cmd=False, ignore_error=False):

# Validate whether a given namespace name is valid in the device.
def validate_namespace(namespace):
if not sonic_device_util.is_multi_npu():
if not device_info.is_multi_npu():
return True

namespaces = sonic_device_util.get_all_namespaces()
namespaces = device_info.get_all_namespaces()
if namespace in namespaces['front_ns'] + namespaces['back_ns']:
return True
else:
Expand Down Expand Up @@ -518,18 +496,18 @@ def _get_disabled_services_list():
if feature_table is not None:
for feature_name in feature_table.keys():
if not feature_name:
log_warning("Feature is None")
log.log_warning("Feature is None")
continue

state = feature_table[feature_name]['state']
if not state:
log_warning("Enable state of feature '{}' is None".format(feature_name))
log.log_warning("Status of feature '{}' is None".format(feature_name))
continue

if state == "disabled":
disabled_services_list.append(feature_name)
else:
log_warning("Unable to retreive FEATURE table")
log.log_warning("Unable to retreive FEATURE table")

return disabled_services_list

Expand Down Expand Up @@ -632,7 +610,7 @@ def config():
global asic_type

try:
version_info = sonic_device_util.get_sonic_version_info()
version_info = device_info.get_sonic_version_info()
asic_type = version_info['asic_type']
except KeyError, TypeError:
raise click.Abort()
Expand Down Expand Up @@ -664,11 +642,11 @@ def save(filename):
"""Export current config DB to a file on disk.\n
<filename> : Names of configuration file(s) to save, separated by comma with no spaces in between
"""
num_asic = sonic_device_util.get_num_npus()
num_asic = device_info.get_num_npus()
cfg_files = []

num_cfg_file = 1
if sonic_device_util.is_multi_npu():
if device_info.is_multi_npu():
num_cfg_file += num_asic

# If the user give the filename[s], extract the file names.
Expand Down Expand Up @@ -720,11 +698,11 @@ def load(filename, yes):
if not yes:
click.confirm(message, abort=True)

num_asic = sonic_device_util.get_num_npus()
num_asic = device_info.get_num_npus()
cfg_files = []

num_cfg_file = 1
if sonic_device_util.is_multi_npu():
if device_info.is_multi_npu():
num_cfg_file += num_asic

# If the user give the filename[s], extract the file names.
Expand Down Expand Up @@ -784,13 +762,13 @@ def reload(filename, yes, load_sysinfo, no_service_restart):
if not yes:
click.confirm(message, abort=True)

log_info("'reload' executing...")
log.log_info("'reload' executing...")

num_asic = sonic_device_util.get_num_npus()
num_asic = device_info.get_num_npus()
cfg_files = []

num_cfg_file = 1
if sonic_device_util.is_multi_npu():
if device_info.is_multi_npu():
num_cfg_file += num_asic

# If the user give the filename[s], extract the file names.
Expand All @@ -813,7 +791,7 @@ def reload(filename, yes, load_sysinfo, no_service_restart):

#Stop services before config push
if not no_service_restart:
log_info("'reload' stopping services...")
log.log_info("'reload' stopping services...")
_stop_services()

""" In Single AISC platforms we have single DB service. In multi-ASIC platforms we have a global DB
Expand Down Expand Up @@ -888,7 +866,7 @@ def reload(filename, yes, load_sysinfo, no_service_restart):
# status from all services before we attempt to restart them
if not no_service_restart:
_reset_failed_services()
log_info("'reload' restarting services...")
log.log_info("'reload' restarting services...")
_restart_services()

@config.command("load_mgmt_config")
Expand Down Expand Up @@ -921,7 +899,7 @@ def load_mgmt_config(filename):
@click.option('-n', '--no_service_restart', default=False, is_flag=True, help='Do not restart docker services')
def load_minigraph(no_service_restart):
"""Reconfigure based on minigraph."""
log_info("'load_minigraph' executing...")
log.log_info("'load_minigraph' executing...")

# get the device type
command = "{} -m -v DEVICE_METADATA.localhost.type".format(SONIC_CFGGEN_PATH)
Expand All @@ -935,16 +913,16 @@ def load_minigraph(no_service_restart):

#Stop services before config push
if not no_service_restart:
log_info("'load_minigraph' stopping services...")
log.log_info("'load_minigraph' stopping services...")
_stop_services()

# For Single Asic platform the namespace list has the empty string
# for mulit Asic platform the empty string to generate the config
# for host
namespace_list = [DEFAULT_NAMESPACE]
num_npus = sonic_device_util.get_num_npus()
num_npus = device_info.get_num_npus()
if num_npus > 1:
namespace_list += sonic_device_util.get_namespaces()
namespace_list += device_info.get_namespaces()

for namespace in namespace_list:
if namespace is DEFAULT_NAMESPACE:
Expand Down Expand Up @@ -989,7 +967,7 @@ def load_minigraph(no_service_restart):
if not no_service_restart:
_reset_failed_services()
#FIXME: After config DB daemon is implemented, we'll no longer need to restart every service.
log_info("'load_minigraph' restarting services...")
log.log_info("'load_minigraph' restarting services...")
_restart_services()
click.echo("Please note setting loaded from minigraph will be lost after system reboot. To preserve setting, run `config save`.")

Expand Down Expand Up @@ -1113,7 +1091,7 @@ def add(session_name, src_ip, dst_ip, dscp, ttl, gre_type, queue, policer):
"""
For multi-npu platforms we need to program all front asic namespaces
"""
namespaces = sonic_device_util.get_all_namespaces()
namespaces = device_info.get_all_namespaces()
if not namespaces['front_ns']:
config_db = ConfigDBConnector()
config_db.connect()
Expand All @@ -1135,7 +1113,7 @@ def remove(session_name):
"""
For multi-npu platforms we need to program all front asic namespaces
"""
namespaces = sonic_device_util.get_all_namespaces()
namespaces = device_info.get_all_namespaces()
if not namespaces['front_ns']:
config_db = ConfigDBConnector()
config_db.connect()
Expand Down Expand Up @@ -1247,8 +1225,8 @@ def clear():
@qos.command('reload')
def reload():
_clear_qos()
platform = sonic_device_util.get_platform()
hwsku = sonic_device_util.get_hwsku()
platform = device_info.get_platform()
hwsku = device_info.get_hwsku()
buffer_template_file = os.path.join('/usr/share/sonic/device/', platform, hwsku, 'buffers.json.j2')
if os.path.isfile(buffer_template_file):
command = "{} -d -t {} >/tmp/buffers.json".format(SONIC_CFGGEN_PATH, buffer_template_file)
Expand Down Expand Up @@ -1692,12 +1670,12 @@ def all(verbose):
"""Shut down all BGP sessions
In the case of Multi-Asic platform, we shut only the EBGP sessions with external neighbors.
"""
log_info("'bgp shutdown all' executing...")
log.log_info("'bgp shutdown all' executing...")
namespaces = [DEFAULT_NAMESPACE]
ignore_local_hosts = False

if sonic_device_util.is_multi_npu():
ns_list = sonic_device_util.get_all_namespaces()
if device_info.is_multi_npu():
ns_list = device_info.get_all_namespaces()
namespaces = ns_list['front_ns']
ignore_local_hosts = True

Expand All @@ -1718,12 +1696,12 @@ def neighbor(ipaddr_or_hostname, verbose):
"""Shut down BGP session by neighbor IP address or hostname.
User can specify either internal or external BGP neighbor to shutdown
"""
log_info("'bgp shutdown neighbor {}' executing...".format(ipaddr_or_hostname))
log.log_info("'bgp shutdown neighbor {}' executing...".format(ipaddr_or_hostname))
namespaces = [DEFAULT_NAMESPACE]
found_neighbor = False

if sonic_device_util.is_multi_npu():
ns_list = sonic_device_util.get_all_namespaces()
if device_info.is_multi_npu():
ns_list = device_info.get_all_namespaces()
namespaces = ns_list['front_ns'] + ns_list['back_ns']

# Connect to CONFIG_DB in linux host (in case of single ASIC) or CONFIG_DB in all the
Expand All @@ -1749,12 +1727,12 @@ def all(verbose):
"""Start up all BGP sessions
In the case of Multi-Asic platform, we startup only the EBGP sessions with external neighbors.
"""
log_info("'bgp startup all' executing...")
log.log_info("'bgp startup all' executing...")
namespaces = [DEFAULT_NAMESPACE]
ignore_local_hosts = False

if sonic_device_util.is_multi_npu():
ns_list = sonic_device_util.get_all_namespaces()
if device_info.is_multi_npu():
ns_list = device_info.get_all_namespaces()
namespaces = ns_list['front_ns']
ignore_local_hosts = True

Expand All @@ -1772,15 +1750,15 @@ def all(verbose):
@click.argument('ipaddr_or_hostname', metavar='<ipaddr_or_hostname>', required=True)
@click.option('-v', '--verbose', is_flag=True, help="Enable verbose output")
def neighbor(ipaddr_or_hostname, verbose):
log_info("'bgp startup neighbor {}' executing...".format(ipaddr_or_hostname))
log.log_info("'bgp startup neighbor {}' executing...".format(ipaddr_or_hostname))
"""Start up BGP session by neighbor IP address or hostname.
User can specify either internal or external BGP neighbor to startup
"""
namespaces = [DEFAULT_NAMESPACE]
found_neighbor = False

if sonic_device_util.is_multi_npu():
ns_list = sonic_device_util.get_all_namespaces()
if device_info.is_multi_npu():
ns_list = device_info.get_all_namespaces()
namespaces = ns_list['front_ns'] + ns_list['back_ns']

# Connect to CONFIG_DB in linux host (in case of single ASIC) or CONFIG_DB in all the
Expand Down Expand Up @@ -1812,8 +1790,8 @@ def remove_neighbor(neighbor_ip_or_hostname):
namespaces = [DEFAULT_NAMESPACE]
removed_neighbor = False

if sonic_device_util.is_multi_npu():
ns_list = sonic_device_util.get_all_namespaces()
if device_info.is_multi_npu():
ns_list = device_info.get_all_namespaces()
namespaces = ns_list['front_ns'] + ns_list['back_ns']

# Connect to CONFIG_DB in linux host (in case of single ASIC) or CONFIG_DB in all the
Expand Down Expand Up @@ -2894,7 +2872,7 @@ def enable(ctx):
ctx.fail("Unable to check sflow status {}".format(e))

if out != "active":
log_info("sflow service is not enabled. Starting sflow docker...")
log.log_info("sflow service is not enabled. Starting sflow docker...")
run_command("sudo systemctl enable sflow")
run_command("sudo systemctl start sflow")

Expand Down
Loading