Skip to content

Commit

Permalink
Add vr-c8000v kind
Browse files Browse the repository at this point in the history
This is right now a verbatim copy of the existing vr-csr kind. I expect
they will diverge in the future though, so it is best to create a copy
now rather than encourage branching logic in the same file.
  • Loading branch information
mzagozen committed Mar 27, 2024
1 parent 4926307 commit a439971
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
2 changes: 2 additions & 0 deletions clab/register.go
Expand Up @@ -24,6 +24,7 @@ import (
sonic "github.com/srl-labs/containerlab/nodes/sonic"
srl "github.com/srl-labs/containerlab/nodes/srl"
vr_aoscx "github.com/srl-labs/containerlab/nodes/vr_aoscx"
vr_c8000v "github.com/srl-labs/containerlab/nodes/vr_c8000v"
vr_csr "github.com/srl-labs/containerlab/nodes/vr_csr"
vr_freebsd "github.com/srl-labs/containerlab/nodes/vr_freebsd"
vr_ftdv "github.com/srl-labs/containerlab/nodes/vr_ftdv"
Expand Down Expand Up @@ -62,6 +63,7 @@ func (c *CLab) RegisterNodes() {
srl.Register(c.Reg)
vr_aoscx.Register(c.Reg)
vr_csr.Register(c.Reg)
vr_c8000v.Register(c.Reg)
vr_freebsd.Register(c.Reg)
vr_ftosv.Register(c.Reg)
vr_n9kv.Register(c.Reg)
Expand Down
102 changes: 102 additions & 0 deletions nodes/vr_c8000v/vr-c8000v.go
@@ -0,0 +1,102 @@
// Copyright 2020 Nokia
// Licensed under the BSD 3-Clause License.
// SPDX-License-Identifier: BSD-3-Clause

package vr_c8000v

import (
"context"
"fmt"
"path"

log "github.com/sirupsen/logrus"
"github.com/srl-labs/containerlab/netconf"
"github.com/srl-labs/containerlab/nodes"
"github.com/srl-labs/containerlab/types"
"github.com/srl-labs/containerlab/utils"
)

var (
kindnames = []string{"cisco_8000v", "vr-c8000v"}
defaultCredentials = nodes.NewCredentials("admin", "admin")
)

const (
scrapliPlatformName = "cisco_iosxe"

configDirName = "config"
startupCfgFName = "startup-config.cfg"
)

// Register registers the node in the NodeRegistry.
func Register(r *nodes.NodeRegistry) {
r.Register(kindnames, func() nodes.Node {
return new(vrC8000v)
}, defaultCredentials)
}

type vrC8000v struct {
nodes.DefaultNode
}

func (n *vrC8000v) Init(cfg *types.NodeConfig, opts ...nodes.NodeOption) error {
// Init DefaultNode
n.DefaultNode = *nodes.NewDefaultNode(n)
// set virtualization requirement
n.HostRequirements.VirtRequired = true

n.Cfg = cfg
for _, o := range opts {
o(n)
}
// env vars are used to set launch.py arguments in vrnetlab container
defEnv := map[string]string{
"CONNECTION_MODE": nodes.VrDefConnMode,
"USERNAME": defaultCredentials.GetUsername(),
"PASSWORD": defaultCredentials.GetPassword(),
"DOCKER_NET_V4_ADDR": n.Mgmt.IPv4Subnet,
"DOCKER_NET_V6_ADDR": n.Mgmt.IPv6Subnet,
}
n.Cfg.Env = utils.MergeStringMaps(defEnv, n.Cfg.Env)

// mount config dir to support startup-config functionality
n.Cfg.Binds = append(n.Cfg.Binds, fmt.Sprint(path.Join(n.Cfg.LabDir, configDirName), ":/config"))

if n.Cfg.Env["CONNECTION_MODE"] == "macvtap" {
// mount dev dir to enable macvtap
n.Cfg.Binds = append(n.Cfg.Binds, "/dev:/dev")
}

n.Cfg.Cmd = fmt.Sprintf("--username %s --password %s --hostname %s --connection-mode %s --trace",
n.Cfg.Env["USERNAME"], n.Cfg.Env["PASSWORD"], n.Cfg.ShortName, n.Cfg.Env["CONNECTION_MODE"])

return nil
}

func (s *vrC8000v) PreDeploy(_ context.Context, params *nodes.PreDeployParams) error {
utils.CreateDirectory(s.Cfg.LabDir, 0777)
_, err := s.LoadOrGenerateCertificate(params.Cert, params.TopologyName)
if err != nil {
return nil
}
return nodes.LoadStartupConfigFileVr(s, configDirName, startupCfgFName)
}

func (n *vrC8000v) SaveConfig(_ context.Context) error {
err := netconf.SaveConfig(n.Cfg.LongName,
defaultCredentials.GetUsername(),
defaultCredentials.GetPassword(),
scrapliPlatformName,
)
if err != nil {
return err
}

log.Infof("saved %s running configuration to startup configuration file\n", n.Cfg.ShortName)
return nil
}

// CheckInterfaceName checks if a name of the interface referenced in the topology file correct.
func (n *vrC8000v) CheckInterfaceName() error {
return nodes.GenericVMInterfaceCheck(n.Cfg.ShortName, n.Endpoints)
}

0 comments on commit a439971

Please sign in to comment.