예제 #1
0
파일: openswitch.go 프로젝트: hzhou8/gobgp
func (m *OpsConfigManager) handleNeighborUpdate(update ovsdb.TableUpdate) []*server.GrpcRequest {
	_, id, _ := m.getBGPRouterUUID()
	addrs, ids, err := m.getBGPNeighborUUIDs(id)
	if err != nil {
		return nil
	}
	reqs := make([]*server.GrpcRequest, 0, len(addrs))
	for k, v := range update.Rows {
		for idx, id := range ids {
			if uuid.Equal(id, uuid.Parse(k)) {
				asn, ok := v.New.Fields["remote_as"].(float64)
				if !ok {
					log.Debugf("remote-as is not configured yet")
					continue
				}
				reqs = append(reqs, server.NewGrpcRequest(server.REQ_MOD_NEIGHBOR, "", bgp.RouteFamily(0), &api.ModNeighborArguments{
					Operation: api.Operation_ADD,
					Peer: &api.Peer{
						Conf: &api.PeerConf{
							NeighborAddress: addrs[idx].String(),
							PeerAs:          uint32(asn),
						},
					},
				}))
			}
		}
	}
	return reqs
}
예제 #2
0
파일: openswitch.go 프로젝트: hzhou8/gobgp
func (m *OpsConfigManager) getBGPNeighborUUIDs(id uuid.UUID) ([]net.IP, []uuid.UUID, error) {
	global, ok := m.cache["BGP_Router"]
	if !ok {
		return nil, nil, fmt.Errorf("BGP_Router table not found")
	}
	for k, v := range global {
		if uuid.Equal(id, uuid.Parse(k)) {
			neighbors := v.Fields["bgp_neighbors"].(ovsdb.OvsMap).GoMap
			if len(neighbors) < 1 {
				return nil, nil, fmt.Errorf("no bgp neighbor configured")
			}
			addrs := make([]net.IP, 0, len(neighbors))
			ids := make([]uuid.UUID, 0, len(neighbors))
			for k, v := range neighbors {
				addrs = append(addrs, net.ParseIP(k.(string)))
				id := extractUUID(v)
				if id == nil {
					return nil, nil, fmt.Errorf("invalid uuid schema")
				}
				ids = append(ids, id)
			}
			return addrs, ids, nil
		}
	}
	return nil, nil, fmt.Errorf("not found")
}
예제 #3
0
파일: openswitch.go 프로젝트: hzhou8/gobgp
func (m *OpsConfigManager) handleBgpRouterUpdate(update ovsdb.TableUpdate) []*server.GrpcRequest {
	asn, id, err := m.getBGPRouterUUID()
	if err != nil {
		log.Debugf("%s", err)
		return nil
	}
	reqs := []*server.GrpcRequest{}
	for k, v := range update.Rows {
		if uuid.Equal(id, uuid.Parse(k)) {
			initial := false
			if len(v.Old.Fields) == 0 {
				log.WithFields(log.Fields{
					"Topic": "openswitch",
				}).Debug("new bgp router")
				initial = true
			}
			if _, ok := v.Old.Fields["router_id"]; initial || ok {
				r, ok := v.New.Fields["router_id"].(string)
				if !ok {
					log.Debugf("router-id is not configured yet")
					return nil
				}
				reqs = append(reqs, server.NewGrpcRequest(server.REQ_MOD_GLOBAL_CONFIG, "", bgp.RouteFamily(0), &api.ModGlobalConfigArguments{
					Operation: api.Operation_ADD,
					Global: &api.Global{
						As:       asn,
						RouterId: r,
					},
				}))
			}
			if o, ok := v.Old.Fields["bgp_neighbors"]; ok {
				oldNeighMap := o.(ovsdb.OvsMap).GoMap
				newNeighMap := v.New.Fields["bgp_neighbors"].(ovsdb.OvsMap).GoMap
				for k, _ := range oldNeighMap {
					if _, ok := newNeighMap[k]; !ok {
						reqs = append(reqs, server.NewGrpcRequest(server.REQ_MOD_NEIGHBOR, "", bgp.RouteFamily(0), &api.ModNeighborArguments{
							Operation: api.Operation_DEL,
							Peer: &api.Peer{
								Conf: &api.PeerConf{
									NeighborAddress: k.(string),
								},
							},
						}))
					}
				}
			}
		}
	}
	return reqs
}
예제 #4
0
파일: uuid.go 프로젝트: qorio/omni
func Equals(uuid1, uuid2 UUID) bool {
	return uuid.Equal(uuid.UUID(uuid1), uuid.UUID(uuid2))
}
예제 #5
0
func interfaceStatus(client *contrail.Client, flagSet *flag.FlagSet) {
	if len(interfaceStatusOpts.vrouter) == 0 {
		fmt.Fprintln(os.Stderr, "virtual-router must be specified.")
		os.Exit(1)
	}
	url := fmt.Sprintf("http://%s:8085/Snh_ItfReq",
		interfaceStatusOpts.vrouter)
	resp, err := http.Get(url)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	// see: controller/src/vnsw/agent/oper/agent.sandesh
	// struct ItfSandeshData
	type InterfaceData struct {
		Name             string `xml:"name"`
		Uuid             string `xml:"uuid"`
		VrfName          string `xml:"vrf_name"`
		Status           string `xml:"active"`
		NetworkName      string `xml:"vn_name"`
		InstanceName     string `xml:"vm_name"`
		IpAddress        string `xml:"ip_addr"`
		LinkLocalAddress string `xml:"mdata_ip_addr"`
	}

	type Envelope struct {
		XMLName xml.Name        `xml:"ItfResp"`
		Data    []InterfaceData `xml:"itf_list>list>ItfSandeshData"`
	}

	var m Envelope
	err = xml.Unmarshal(body, &m)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	const interfaceStatusTmpl = `
  Interface: {{.Uuid}}
    Instance: {{.InstanceName}}
    Network: {{.NetworkName}}
    IpAddress: {{.IpAddress}} Link Local: {{.LinkLocalAddress}}
    Status: {{.Status}}
`
	var writer *tabwriter.Writer
	var tmpl *template.Template

	if interfaceStatusOpts.detail {
		tmpl = template.Must(template.New("interface-status").Parse(
			interfaceStatusTmpl))
	} else {
		writer = new(tabwriter.Writer)
		writer.Init(os.Stdout, 0, 0, 1, ' ', 0)
		fmt.Fprintf(writer, "Instance\tNetwork\tIpAddress\n")
	}
	for _, ifdata := range m.Data {
		id := uuid.Parse(ifdata.Uuid)
		if uuid.Equal(id, uuid.NIL) {
			continue
		}
		if interfaceStatusOpts.detail {
			tmpl.Execute(os.Stdout, ifdata)
		} else {
			netname := strings.Split(ifdata.NetworkName, ":")
			fmt.Fprintf(writer, "%.34s\t%-.28s\t%s\n",
				ifdata.InstanceName, netname[2], ifdata.IpAddress)
		}
	}

	if !interfaceStatusOpts.detail {
		writer.Flush()
	}
}