-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathvr-xrv9k.go
109 lines (90 loc) · 3.13 KB
/
vr-xrv9k.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2020 Nokia
// Licensed under the BSD 3-Clause License.
// SPDX-License-Identifier: BSD-3-Clause
package vr_xrv9k
import (
"context"
"fmt"
"path"
"regexp"
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_xrv9k", "vr-xrv9k", "vr-cisco_xrv9k"}
defaultCredentials = nodes.NewCredentials("clab", "clab@123")
InterfaceRegexp = regexp.MustCompile(`(?:Gi|GigabitEthernet|Te|TenGigE|TenGigabitEthernet)\s?0/0/0/(?P<port>\d+)`)
InterfaceOffset = 0
InterfaceHelp = "GigabitEthernet0/0/0/X, Gi0/0/0/X or TenGigabitEthernet0/0/0/X, TenGigE0/0/0/X, Te0/0/0/X (where X >= 0) or ethX (where X >= 1)"
)
const (
scrapliPlatformName = "cisco_iosxr"
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(vrXRV9K)
}, defaultCredentials)
}
type vrXRV9K struct {
nodes.VRNode
}
func (n *vrXRV9K) Init(cfg *types.NodeConfig, opts ...nodes.NodeOption) error {
// Init VRNode
n.VRNode = *nodes.NewVRNode(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{
"USERNAME": defaultCredentials.GetUsername(),
"PASSWORD": defaultCredentials.GetPassword(),
"CONNECTION_MODE": nodes.VrDefConnMode,
"VCPU": "2",
"RAM": "16384",
"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 --vcpu %s --ram %s --trace",
n.Cfg.Env["USERNAME"], n.Cfg.Env["PASSWORD"], n.Cfg.ShortName,
n.Cfg.Env["CONNECTION_MODE"], n.Cfg.Env["VCPU"], n.Cfg.Env["RAM"])
n.InterfaceRegexp = InterfaceRegexp
n.InterfaceOffset = InterfaceOffset
n.InterfaceHelp = InterfaceHelp
return nil
}
func (n *vrXRV9K) PreDeploy(_ context.Context, params *nodes.PreDeployParams) error {
utils.CreateDirectory(n.Cfg.LabDir, 0777)
_, err := n.LoadOrGenerateCertificate(params.Cert, params.TopologyName)
if err != nil {
return nil
}
return nodes.LoadStartupConfigFileVr(n, configDirName, startupCfgFName)
}
func (n *vrXRV9K) 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
}