Skip to content

Commit

Permalink
Support for Cisco ACI APIC.
Browse files Browse the repository at this point in the history
  • Loading branch information
udhos committed Jan 31, 2017
1 parent a557468 commit 9b4db33
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Please send pull requests for new plataforms.

- [Cisco IOS](https://github.com/udhos/jazigo/blob/master/dev/model_cisco.go)
- [Cisco IOS XR](https://github.com/udhos/jazigo/blob/master/dev/model_cisco_iosxr.go)
- [Cisco ACI APIC](https://github.com/udhos/jazigo/blob/master/dev/model_cisco_apic.go)
- [Juniper JunOS](https://github.com/udhos/jazigo/blob/master/dev/model_junos.go)
- [Mikrotik](https://github.com/udhos/jazigo/blob/master/dev/model_mikrotik.go)
- [HTTP](https://github.com/udhos/jazigo/blob/master/dev/model_http.go) (collect output of http GET method)
Expand Down
35 changes: 34 additions & 1 deletion dev/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev

import (
"bytes"
"unicode"
)

func removeControlChars(logger hasPrintf, debug bool, buf, suffix []byte) ([]byte, []byte) {
Expand Down Expand Up @@ -81,9 +82,20 @@ func removeControlChars(logger hasPrintf, debug bool, buf, suffix []byte) ([]byt
if j := i + 1; j < len(suffix) {
switch suffix[j] {
case '[':

if size, ok := prefixNumberM(suffix[j+1:]); ok {
// remove N control chars: ESC [ d d d m
// i j <----->
// size
k := j + size
suffix = append(suffix[:i], suffix[k+1:]...) // cut bytes i..k
i-- // handle i again
continue
}

if k := j + 1; k < len(suffix) {
switch suffix[k] {
case 'A', 'B', 'C', 'D':
case 'A', 'B', 'C', 'D', 'J', 'K':
// remove 3 control chars: ESC [ x
suffix = append(suffix[:i], suffix[k+1:]...) // cut bytes i..k
i-- // handle i again
Expand Down Expand Up @@ -118,3 +130,24 @@ func removeControlChars(logger hasPrintf, debug bool, buf, suffix []byte) ([]byt

return buf, suffix
}

func prefixNumberM(s []byte) (int, bool) {

foundDigit := false

for i, c := range s {
switch {
case c == 'm':
if foundDigit {
return i + 1, true
}
return 0, false
case unicode.IsDigit(rune(c)):
foundDigit = true
default:
return 0, false
}
}

return 0, false
}
21 changes: 21 additions & 0 deletions dev/control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@ func TestControl1(t *testing.T) {
control(t, debug, logger, "suffix-middleCR2", five, middleCR, empty, []byte("45"))
}

func TestPrefixM(t *testing.T) {
expectPrefixM(t, []byte("1m"), 2, true)
expectPrefixM(t, []byte("12m"), 3, true)
expectPrefixM(t, []byte("12mx"), 3, true)
expectPrefixM(t, []byte(""), 0, false)
expectPrefixM(t, []byte("1"), 0, false)
expectPrefixM(t, []byte("m"), 0, false)
expectPrefixM(t, []byte("12"), 0, false)
expectPrefixM(t, []byte("12a"), 0, false)
expectPrefixM(t, []byte("a"), 0, false)
expectPrefixM(t, []byte("a1"), 0, false)
expectPrefixM(t, []byte("x12m"), 0, false)
}

func expectPrefixM(t *testing.T, input []byte, wantSize int, wantFound bool) {
size, found := prefixNumberM(input)
if size != wantSize || found != wantFound {
t.Errorf("expectPrefixM: input=%v wantSize=%d wantFound=%v gotSize=%d gotFound=%v", input, wantSize, wantFound, size, found)
}
}

func control(t *testing.T, debug bool, logger hasPrintf, label string, inputBuf, inputSuffix, expectedBuf, expectedSuffix []byte) {
buf := clone(inputBuf)
suffix := clone(inputSuffix)
Expand Down
1 change: 1 addition & 0 deletions dev/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (d *Device) Holdtime(now time.Time, holdtime time.Duration) time.Duration {
}

func RegisterModels(logger hasPrintf, t *DeviceTable) {
registerModelCiscoAPIC(logger, t)
registerModelCiscoIOS(logger, t)
registerModelCiscoIOSXR(logger, t)
registerModelLinux(logger, t)
Expand Down
29 changes: 29 additions & 0 deletions dev/model_cisco_apic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev

import (
"time"

"github.com/udhos/jazigo/conf"
)

func registerModelCiscoAPIC(logger hasPrintf, t *DeviceTable) {
a := conf.NewDevAttr()

promptPattern := `\S+#\s*$`

a.DisabledPromptPattern = promptPattern
a.EnabledPromptPattern = promptPattern
a.CommandList = []string{"show ver", "conf", "terminal length 0", "show running-config"}
a.ReadTimeout = 10 * time.Second
a.MatchTimeout = 20 * time.Second
a.SendTimeout = 5 * time.Second
a.CommandReadTimeout = 20 * time.Second // larger timeout for slow 'sh run'
a.CommandMatchTimeout = 30 * time.Second // larger timeout for slow 'sh run'
a.QuoteSentCommandsFormat = `!![%s]`

m := &Model{name: "cisco-apic"}
m.defaultAttr = a
if err := t.SetModel(m, logger); err != nil {
logger.Printf("registerModelCiscoAPIC: %v", err)
}
}

0 comments on commit 9b4db33

Please sign in to comment.