Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static Port channel commit #79

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
67 changes: 52 additions & 15 deletions src/north/cli/goldstone/north/cli/portchannel.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def get_list(session, ds, include_implicit_defaults=True):
)


def add_interfaces(session, id, ifnames):
def add_interfaces(session, id, ifnames, mode):
session.set(f"{pcxpath(id)}/config/mode", mode)
session.apply()
ishidawataru marked this conversation as resolved.
Show resolved Hide resolved
prefix = "/goldstone-interfaces:interfaces"
for ifname in ifnames:
xpath = f"{prefix}/interface[name='{ifname}']"
Expand Down Expand Up @@ -97,6 +99,7 @@ def remove_interfaces(session, ifnames):


def run_conf(session):

n = 0
for data in get_list(session, "running", False):
stdout.info("portchannel {}".format(data["config"]["portchannel-id"]))
Expand All @@ -113,26 +116,49 @@ def run_conf(session):


def show(session, id=None):

if id != None:
items = session.get_operational(pcxpath(id))
else:
items = get_list(session, "operational")

rows = []
for item in items:
rows.append(
[
item["portchannel-id"],
item["state"]["oper-status"].lower(),
item["state"]["admin-status"].lower(),
", ".join(natsorted(list(item["state"].get("interface", [])))),
]
)
if "config" in item and item["config"]["mode"] == "dynamic":
rows.append(
[
item["portchannel-id"],
item["state"]["oper-status"].lower(),
item["state"]["admin-status"].lower(),
", ".join(natsorted(list(item["state"].get("interface", [])))),
item["config"]["mode"],
]
)
elif "config" in item and item["config"]["mode"] == "static":
rows.append(
[
item["portchannel-id"],
item["state"]["oper-status"].lower(),
item["state"]["admin-status"].lower(),
", ".join(natsorted(list(item["config"].get("interface", [])))),
ishidawataru marked this conversation as resolved.
Show resolved Hide resolved
item["config"]["mode"],
]
)
elif "config" in item and item["config"]["mode"] == "none":
rows.append(
[
item["portchannel-id"],
"-",
item["config"]["admin-status"].lower(),
", ".join(natsorted(list(item["config"].get("interface", [])))),
item["config"]["mode"],
]
)

stdout.info(
tabulate(
rows,
["Portchannel ID", "oper-status", "admin-status", "Interfaces"],
["Portchannel ID", "Oper-Status", "Admin-Status", "Interfaces", "Mode"],
)
)

Expand Down Expand Up @@ -265,19 +291,30 @@ def get_portchannel(conn, ifname):
return pc


class PortChannelModeOptions(ConfigCommand):
COMMAND_DICT = {"dynamic": Command, "static": Command}

class PortChannelModeCommand(ConfigCommand):
def __init__(
self, context: Context = None, parent: Command = None, name=None, **options
):
super().__init__(context, parent, name, **options)
if self.root.name != "no":
self.add_command("mode",PortChannelModeOptions)

class InterfacePortchannelCommand(ConfigCommand):
def arguments(self):
if self.root.name != "no":
return get_id(self.conn)
return []
for id in get_id(self.conn):
self.add_command(str(id), PortChannelModeCommand)

def exec(self, line):
if self.root.name == "no":
remove_interfaces(self.conn, self.context.ifnames)
else:
if len(line) != 1:
raise InvalidInput(f"usage: {self.name_all()} <portchannel_id>")
add_interfaces(self.conn, line[0], self.context.ifnames)
if len(line) != 3 and (line[2] != "dynamic" or line[2] != "static"):
raise InvalidInput(f"usage: {self.name_all()} <portchannel_id> mode [dynamic|static]")
add_interfaces(self.conn, line[0], self.context.ifnames, line[2])

@classmethod
def to_command(cls, conn, data, **options):
Expand Down
56 changes: 44 additions & 12 deletions src/south/sonic/goldstone/south/sonic/portchannel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
CallbackFailedError,
)

def _decode(string):
if hasattr(string, "decode"):
return string.decode("utf-8")
return str(string)

class PortChannelChangeHandler(ChangeHandler):
def __init__(self, server, change):
Expand All @@ -26,24 +30,41 @@ def __init__(self, server, change):
class PortChannelIDHandler(PortChannelChangeHandler):
def apply(self, user):
if self.type in ["created", "modified"]:
value = self.server.get_default("admin-status")
self.server.sonic.set_config_db(
self.pid, "admin-status", value, "PORTCHANNEL"
)
self.mode = self.change.value
admin = self.server.get_default("admin-status")
mtu = value = self.server.get_default("mtu")
if self.mode == "dynamic":
prasadsriramps marked this conversation as resolved.
Show resolved Hide resolved
self.server.sonic.set_config_db(self.pid, "mode", self.mode, "PORTMODE")
self.server.sonic.set_config_db(self.pid, "admin-status", admin, "PORTCHANNEL")
self.server.sonic.set_config_db(self.pid, "mtu", mtu, "PORTCHANNEL")
elif self.mode == "static":
self.server.sonic.set_config_db(
self.pid, "static", "true", "PORTCHANNEL"
)
self.server.sonic.set_config_db(self.pid, "mode", self.mode, "PORTMODE")
self.server.sonic.set_config_db(self.pid, "admin-status", admin, "PORTCHANNEL")
self.server.sonic.set_config_db(self.pid, "mtu", mtu, "PORTCHANNEL")
else:
self.server.sonic.sonic_db.delete(
self.server.sonic.sonic_db.CONFIG_DB, f"PORTCHANNEL|{self.pid}"
self.server.sonic.sonic_db.CONFIG_DB, "PORTMODE|{}".format(self.pid)
ishidawataru marked this conversation as resolved.
Show resolved Hide resolved
)
self.server.sonic.sonic_db.delete(
self.server.sonic.sonic_db.CONFIG_DB, "PORTCHANNEL|{}".format(self.pid)
ishidawataru marked this conversation as resolved.
Show resolved Hide resolved
)


class AdminStatusHandler(PortChannelChangeHandler):
def apply(self, user):
self.mode = _decode(
self.server.sonic.sonic_db.get(self.server.sonic.sonic_db.CONFIG_DB, f"PORTMODE|{self.pid}", "mode")
)
if self.type in ["created", "modified"]:
value = self.change.value
else:
value = self.server.get_default("admin-status")
logger.debug(f"set {self.pid}'s admin-status to {value}")
self.server.sonic.set_config_db(self.pid, "admin-status", value, "PORTCHANNEL")
if self.type not in ["deleted"] and self.mode in ["dynamic","static"]:
self.server.sonic.set_config_db(self.pid, "admin-status", value, "PORTCHANNEL")

def revert(self, user):
# TODO
Expand All @@ -52,12 +73,16 @@ def revert(self, user):

class MTUHandler(PortChannelChangeHandler):
def apply(self, user):
self.mode = _decode(
self.server.sonic.sonic_db.get(self.server.sonic.sonic_db.CONFIG_DB, f"PORTMODE|{self.pid}", "mode")
)
if self.type in ["created", "modified"]:
value = self.change.value
else:
value = self.server.get_default("mtu")
logger.debug(f"set {self.pid}'s mtu to {value}")
self.server.sonic.set_config_db(self.pid, "mtu", value, "PORTCHANNEL")
if self.type not in ["deleted"] and self.mode in ["dynamic","static"]:
self.server.sonic.set_config_db(self.pid, "mtu", value, "PORTCHANNEL")


class InterfaceHandler(PortChannelChangeHandler):
Expand All @@ -71,6 +96,7 @@ def validate(self, user):
def apply(self, user):
if self.type in ["created", "modified"]:
ifname = self.xpath[-1][2][0][1]
self.server.sonic.set_config_db(self.pid, "admin-status", "up", "PORTCHANNEL")
self.server.sonic.sonic_db.set(
self.server.sonic.sonic_db.CONFIG_DB,
f"PORTCHANNEL_MEMBER|{self.pid}|{ifname}",
Expand All @@ -94,7 +120,8 @@ def __init__(self, conn, sonic):
"portchannel-group": {
"portchannel-id": NoOp,
"config": {
"portchannel-id": PortChannelIDHandler,
"portchannel-id": NoOp,
"mode": PortChannelIDHandler,
"admin-status": AdminStatusHandler,
"mtu": MTUHandler,
"interface": InterfaceHandler,
Expand Down Expand Up @@ -165,10 +192,15 @@ async def reconcile(self):
)
for pc in pc_list:
pid = pc["portchannel-id"]
for leaf in ["admin-status", "mtu"]:
default = self.get_default(leaf)
value = pc["config"].get(leaf, default)
self.sonic.set_config_db(pid, leaf, value, "PORTCHANNEL")
mode = pc["config"].get("mode",'')
if mode in ["dynamic","static"]:
for leaf in ["admin-status", "mtu"]:
default = self.get_default(leaf)
value = pc["config"].get(leaf, default)
self.sonic.set_config_db(pid, "mode", mode, "PORTMODE")
if mode == "static":
self.sonic.set_config_db(pid, "static", "true", "PORTCHANNEL")
self.sonic.set_config_db(pid, leaf, value, "PORTCHANNEL")
for intf in pc["config"].get("interface", []):
self.sonic.set_config_db(
pid + "|" + intf, "NULL", "NULL", "PORTCHANNEL_MEMBER"
Expand Down
12 changes: 11 additions & 1 deletion yang/goldstone-portchannel.yang
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module goldstone-portchannel {
description
"Goldstone portchannel";

revision 2021-05-30 {
revision 2023-07-10 {
description
"Initial revision.";
}
Expand Down Expand Up @@ -49,6 +49,16 @@ module goldstone-portchannel {
default 9100;
}

leaf mode {
type enumeration{
enum dynamic;
enum static;
enum none;
}
description "portchannel mode";
default none;
}

uses gs-if:interface-common-config;

leaf-list interface {
Expand Down