Skip to content

Commit

Permalink
Move GCU to subclass
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Jun 1, 2022
1 parent 220a605 commit d4ab077
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 42 deletions.
56 changes: 14 additions & 42 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import click
import ipaddress
import json
import jsonpatch
import netaddr
import netifaces
import os
Expand Down Expand Up @@ -1893,49 +1892,35 @@ def portchannel(db, ctx, namespace):
@click.pass_context
def add_portchannel(ctx, portchannel_name, min_links, fallback):
"""Add port channel"""
db = ctx.obj['db']


if is_portchannel_present_in_db(db, portchannel_name):
ctx.fail("{} already exists!".format(portchannel_name))

gcu_json_input = []
fvs = {'admin_status': 'up',
'mtu': '9100',
'lacp_key': 'auto'}
db = ValidatedConfigDBConnector()
db.connect()

fvs = {"admin_status": "up",
"mtu": "9100",
"lacp_key": "auto"}
if min_links != 0:
fvs['min_links'] = str(min_links)
if fallback != 'false':
fvs['fallback'] = 'true'

gcu_json = {"op": "add",
"path": "/PORTCHANNEL/{}".format(portchannel_name)}
gcu_json["value"] = fvs
gcu_json_input.append(gcu_json)
gcu_patch = jsonpatch.JsonPatch(gcu_json_input)
format = ConfigFormat.CONFIGDB.name
config_format = ConfigFormat[format.upper()]
GenericUpdater().apply_patch(patch=gcu_patch, config_format=config_format, verbose=False, dry_run=False, ignore_non_yang_tables=False, ignore_paths=None)
fvs["min_links"] = str(min_links)
if fallback != "false":
fvs["fallback"] = "true"
db.set_entry("add", "/PORTCHANNEL/{}".format(portchannel_name), fvs)


@portchannel.command('del')
@click.argument('portchannel_name', metavar='<portchannel_name>', required=True)
@click.pass_context
def remove_portchannel(ctx, portchannel_name):
"""Remove port channel"""

db = ctx.obj['db']
db = ValidatedConfigDBConnector()
db.connect()

if len([(k, v) for k, v in db.get_table('PORTCHANNEL_MEMBER') if k == portchannel_name]) != 0:
click.echo("Error: Portchannel {} contains members. Remove members before deleting Portchannel!".format(portchannel_name))
else:
gcu_json_input = []
gcu_json = {"op": "remove",
"path": "/PORTCHANNEL/{}".format(portchannel_name)}
gcu_json_input.append(gcu_json)
gcu_patch = jsonpatch.JsonPatch(gcu_json_input)
format = ConfigFormat.CONFIGDB.name
config_format = ConfigFormat[format.upper()]
GenericUpdater().apply_patch(patch=gcu_patch, config_format=config_format, verbose=False, dry_run=False, ignore_non_yang_tables=False, ignore_paths=None)
db.set_entry("remove", "/PORTCHANNEL/{}".format(portchannel_name), None)

@portchannel.group(cls=clicommon.AbbreviationGroup, name='member')
@click.pass_context
Expand All @@ -1952,19 +1937,6 @@ def add_portchannel_member(ctx, portchannel_name, port_name):
if clicommon.is_port_mirror_dst_port(db, port_name):
ctx.fail("{} is configured as mirror destination port".format(port_name))

# Check if the member interface given by user is valid in the namespace.
if port_name.startswith("Ethernet") is False or interface_name_is_valid(db, port_name) is False:
ctx.fail("Interface name is invalid. Please enter a valid interface name!!")

# Dont proceed if the port channel name is not valid
if is_portchannel_name_valid(portchannel_name) is False:
ctx.fail("{} is invalid!, name should have prefix '{}' and suffix '{}'"
.format(portchannel_name, CFG_PORTCHANNEL_PREFIX, CFG_PORTCHANNEL_NO))

# Dont proceed if the port channel does not exist
if is_portchannel_present_in_db(db, portchannel_name) is False:
ctx.fail("{} is not present.".format(portchannel_name))

# Dont allow a port to be member of port channel if it is configured with an IP address
for key,value in db.get_table('INTERFACE').items():
if type(key) == tuple:
Expand Down
18 changes: 18 additions & 0 deletions config/validated_config_db_connector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import jsonpatch

from swsscommon.swsscommon import SonicV2Connector, ConfigDBConnector
from generic_config_updater.generic_updater import GenericUpdater, ConfigFormat

class ValidatedConfigDBConnector(ConfigDBConnector):

def set_entry(self, op, path, value):
gcu_json_input = []
gcu_json = {"op": "{}".format(op),
"path": "{}".format(path)}
if value:
gcu_json["value"] = value
gcu_json_input.append(gcu_json)
gcu_patch = jsonpatch.JsonPatch(gcu_json_input)
format = ConfigFormat.CONFIGDB.name
config_format = ConfigFormat[format.upper()]
GenericUpdater().apply_patch(patch=gcu_patch, config_format=config_format, verbose=False, dry_run=False, ignore_non_yang_tables=False, ignore_paths=None)

0 comments on commit d4ab077

Please sign in to comment.