|
| 1 | +package pass1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "github.com/hknutzen/Netspoc/go/pkg/conf" |
| 8 | + "github.com/hknutzen/Netspoc/go/pkg/oslink" |
| 9 | + "github.com/spf13/pflag" |
| 10 | + "io" |
| 11 | +) |
| 12 | + |
| 13 | +func ExportNetvisMain(d oslink.Data) int { |
| 14 | + fs := pflag.NewFlagSet(d.Args[0], pflag.ContinueOnError) |
| 15 | + |
| 16 | + // Setup custom usage function. |
| 17 | + fs.Usage = func() { |
| 18 | + fmt.Fprintf(d.Stderr, |
| 19 | + "Usage: %s [options] FILE|DIR\n%s", |
| 20 | + d.Args[0], fs.FlagUsages()) |
| 21 | + } |
| 22 | + |
| 23 | + // Command line flags |
| 24 | + quiet := fs.BoolP("quiet", "q", false, "Don't print progress messages") |
| 25 | + if err := fs.Parse(d.Args[1:]); err != nil { |
| 26 | + if errors.Is(err, pflag.ErrHelp) { |
| 27 | + return 1 |
| 28 | + } |
| 29 | + fmt.Fprintf(d.Stderr, "Error: %s\n", err) |
| 30 | + fs.Usage() |
| 31 | + return 1 |
| 32 | + } |
| 33 | + |
| 34 | + // Argument processing |
| 35 | + args := fs.Args() |
| 36 | + if len(args) <= 0 || len(args) > 1 { |
| 37 | + fs.Usage() |
| 38 | + return 1 |
| 39 | + } |
| 40 | + path := args[0] |
| 41 | + |
| 42 | + dummyArgs := []string{ |
| 43 | + fmt.Sprintf("--quiet=%v", *quiet), |
| 44 | + } |
| 45 | + cnf := conf.ConfigFromArgsAndFile(dummyArgs, path) |
| 46 | + |
| 47 | + return toplevelSpoc(d, cnf, func(c *spoc) { |
| 48 | + c.exportNetvis(d.Stdout, path) |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +type visBase struct { |
| 53 | + Id string `json:"id"` |
| 54 | + Type string `json:"type"` |
| 55 | + InArea string `json:"in_area,omitempty"` |
| 56 | + Address string `json:"address,omitempty"` |
| 57 | + Neighbors []visNeighbor `json:"neighbors"` |
| 58 | +} |
| 59 | + |
| 60 | +type visNeighbor struct { |
| 61 | + Id string `json:"id"` |
| 62 | + NeighborCount int `json:"neighbor_count"` |
| 63 | + IsTunnel bool `json:"is_tunnel,omitempty"` |
| 64 | +} |
| 65 | +type visNetwork struct { |
| 66 | + visBase |
| 67 | + Hosts []string `json:"hosts"` |
| 68 | +} |
| 69 | + |
| 70 | +type visRouter = visBase |
| 71 | + |
| 72 | +func (c *spoc) exportNetvis(stdout io.Writer, path string) { |
| 73 | + c.readNetspoc(path) |
| 74 | + c.setZone() |
| 75 | + networks := make(map[string]visNetwork) |
| 76 | + routers := make(map[string]visRouter) |
| 77 | + |
| 78 | + for _, n := range c.symTable.network { |
| 79 | + getVisNetwork(n, networks) |
| 80 | + } |
| 81 | + for _, r := range c.symTable.router { |
| 82 | + getVisRouter(r, routers) |
| 83 | + } |
| 84 | + data := struct { |
| 85 | + Network map[string]visNetwork `json:"network"` |
| 86 | + Router map[string]visRouter `json:"router"` |
| 87 | + }{ |
| 88 | + networks, routers, |
| 89 | + } |
| 90 | + out, _ := json.Marshal(data) |
| 91 | + fmt.Fprintln(stdout, string(out)) |
| 92 | + |
| 93 | +} |
| 94 | + |
| 95 | +func getVisNetwork(net *network, networks map[string]visNetwork) { |
| 96 | + var node visNetwork |
| 97 | + node.Id = net.name |
| 98 | + node.Type = "network" |
| 99 | + if net.ipType != unnumberedIP { |
| 100 | + node.Address = net.ipp.String() |
| 101 | + } |
| 102 | + |
| 103 | + if a := net.zone.inArea; a != nil { |
| 104 | + node.InArea = a.name |
| 105 | + } |
| 106 | + |
| 107 | + getVisNeigh := func(intf *routerIntf) visNeighbor { |
| 108 | + r := intf.router |
| 109 | + if r.origRouter != nil { |
| 110 | + r = r.origRouter |
| 111 | + } |
| 112 | + return visNeighbor{Id: r.name, NeighborCount: len(r.interfaces)} |
| 113 | + } |
| 114 | + |
| 115 | + for _, in := range net.interfaces { |
| 116 | + node.Neighbors = append(node.Neighbors, getVisNeigh(in)) |
| 117 | + } |
| 118 | + for _, h := range net.hosts { |
| 119 | + node.Hosts = append(node.Hosts, h.name) |
| 120 | + } |
| 121 | + networks[net.name] = node |
| 122 | +} |
| 123 | + |
| 124 | +func getVisRouter(r *router, routers map[string]visRouter) { |
| 125 | + var node visRouter |
| 126 | + node.Id = r.name |
| 127 | + |
| 128 | + intfs := r.origIntfs |
| 129 | + if intfs == nil { |
| 130 | + intfs = r.interfaces |
| 131 | + } |
| 132 | + routerArea := intfs[0].network.zone.inArea |
| 133 | + |
| 134 | + var typ = "router" |
| 135 | + if r.managed == "" { |
| 136 | + if r.routingOnly { |
| 137 | + typ += ": routing_only" |
| 138 | + } |
| 139 | + } else { |
| 140 | + typ += ": " + r.managed |
| 141 | + } |
| 142 | + |
| 143 | + node.Type = typ |
| 144 | + |
| 145 | + seen := make(map[*area]bool) |
| 146 | + for _, intf := range intfs { |
| 147 | + if intf.network.ipType == tunnelIP { |
| 148 | + for _, intf2 := range intf.network.interfaces { |
| 149 | + if intf2.router != r { |
| 150 | + node.Neighbors = append(node.Neighbors, |
| 151 | + visNeighbor{Id: intf2.router.name, NeighborCount: len(intf2.router.interfaces), IsTunnel: true}) |
| 152 | + } |
| 153 | + } |
| 154 | + } else { |
| 155 | + node.Neighbors = append(node.Neighbors, |
| 156 | + visNeighbor{Id: intf.network.name, NeighborCount: len(intf.network.interfaces)}) |
| 157 | + a := intf.network.zone.inArea |
| 158 | + if a != routerArea { |
| 159 | + routerArea = nil |
| 160 | + } |
| 161 | + if a != nil && !seen[a] && r.managed != "" { |
| 162 | + node.Neighbors = append(node.Neighbors, |
| 163 | + visNeighbor{Id: a.name, NeighborCount: len(a.border) + len(a.inclusiveBorder)}) |
| 164 | + seen[a] = true |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + if routerArea != nil { |
| 169 | + node.InArea = routerArea.name |
| 170 | + } |
| 171 | + routers[r.name] = node |
| 172 | +} |
0 commit comments