//BgpGetOper inspects the oper state of bgp object
192065
func (ac *APIController) BgpGetOper(bgp *contivModel.BgpInspect) error {
	var obj BgpInspect
	var host string

	srvList, err := ac.objdbClient.GetService("netplugin")
	if err != nil {
		log.Errorf("Error getting netplugin nodes. Err: %v", err)
		return err
	}

	for _, srv := range srvList {
		if srv.Hostname == bgp.Config.Hostname {
			host = srv.HostAddr
		}
	}

	url := "http://" + host + ":9090/inspect/bgp"
	r, err := http.Get(url)
	if err != nil {
		return err
	}
	defer r.Body.Close()

	switch {
	case r.StatusCode == int(404):
		return errors.New("page not found")
	case r.StatusCode == int(403):
		return errors.New("access denied")
	case r.StatusCode == int(500):
		response, err := ioutil.ReadAll(r.Body)
		if err != nil {
			return err
		}
		return errors.New(string(response))
	case r.StatusCode != int(200):
		log.Debugf("GET Status '%s' status code %d \n", r.Status, r.StatusCode)
		return errors.New(r.Status)
	}

	response, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return err
	}

	if err := json.Unmarshal(response, &obj); err != nil {
		return err
	}
	//Assuming bgp peer state will be only for one neighbor
	if obj.Peers != nil {
		nConf := obj.Peers[0]
		bgp.Oper.NeighborStatus = string(nConf.State.SessionState)
		bgp.Oper.AdminStatus = nConf.State.AdminState
	}

	if obj.Dsts != nil {
		for _, dst := range obj.Dsts {
			bgp.Oper.Routes = append(bgp.Oper.Routes, dst)
		}
		bgp.Oper.NumRoutes = len(bgp.Oper.Routes)
	}

	return nil
}
321214
func (ac *APIController) BgpDelete(bgpCfg *contivModel.Bgp) error {

	log.Infof("Received delete for Bgp config on {%+v} ", bgpCfg.Hostname)
	// Get the state driver
	stateDriver, err := utils.GetStateDriver()
	if err != nil {
		return err
	}

	err = master.DeleteBgp(stateDriver, bgpCfg.Hostname)
	if err != nil {
		log.Errorf("Error Deleting Bgp neighbor. Err: %v", err)
		return err
	}
	return nil
}
625043
func GetOperEndpointGroup(obj *EndpointGroupInspect) error {
	// Check if we handle this object
	if objCallbackHandler.EndpointGroupCb == nil {
		log.Errorf("No callback registered for endpointGroup object")
		return errors.New("Invalid object type")
	}

	// Perform callback
	err := objCallbackHandler.EndpointGroupCb.EndpointGroupGetOper(obj)
	if err != nil {
		log.Errorf("EndpointGroupDelete retruned error for: %+v. Err: %v", obj, err)
		return err
	}

	return nil
}
449026
func (nc *nccClient) BGPAdvertiseVIP(vip net.IP) error {
	return nc.call("SeesawNCC.BGPAdvertiseVIP", vip, nil)
}
180515
func (cli *Client) StreamrouterStreamprocsGet(appname string) (*pb.StreamrouterStreamprocsReply, error) {
	req := &pb.StreamrouterStreamprocsRequest{Appname: appname}
	ctx, cancel := context.WithTimeout(context.Background(), cli.timeout)
	defer cancel()
	rpl, err := cli.streamrouterStreamprocsClient.Get(ctx, req)
	return rpl, err
}
25471
func (b *RouteBuilder) Operation(name string) *RouteBuilder {
	b.operation = name
	return b
}
1345520
func (ac *APIController) PolicyGetOper(policy *contivModel.PolicyInspect) error {
	log.Infof("Received PolicyInspect: %+v", policy)

	// Get the state driver
	stateDriver, err := utils.GetStateDriver()
	if err != nil {
		return err
	}

	// To hold total number of Endpoint count
	var policyEPCount int

	policyCfg := &mastercfg.EpgPolicy{}
	policyCfg.StateDriver = stateDriver

	// Policy is attached to EPG. So we need to fetch EPGs as well.
	epgCfg := &mastercfg.EndpointGroupState{}
	epgCfg.StateDriver = stateDriver

	// Get all the Endpoints
	readEp := &mastercfg.CfgEndpointState{}
	readEp.StateDriver = stateDriver
	epCfgs, epErr := readEp.ReadAll()

	// Get all the EPGs on which this policy is applied
	epgs := policy.Config.LinkSets.EndpointGroups

	// Scan all the EPGs which are under this policy
	for _, epg := range epgs {
		log.Infof("EPG Object : %+v", epg)

		// Reversing key from TenantName:EPGName to EPGName:TenantName
		sList := strings.Split(epg.ObjKey, ":")
		if sList == nil {
			log.Errorf("EPG key %+v is not in valid format", epg.ObjKey)
			return err
		}
		epgID := sList[1] + ":" + sList[0]

		log.Infof("EPG ID : %s", epgID)

		if err := epgCfg.Read(epgID); err != nil {
			log.Errorf("Error fetching endpointGroup from mastercfg: %s", epgID)
			return err
		}

		policyEPCount = policyEPCount + epgCfg.EpCount

		if epErr == nil {
			for _, epCfg := range epCfgs {
				ep := epCfg.(*mastercfg.CfgEndpointState)
				if ep.EndpointGroupKey == epgID {
					epOper := contivModel.EndpointOper{}
					epOper.Network = ep.NetID
					epOper.EndpointID = ep.EndpointID
					epOper.ServiceName = ep.ServiceName
					epOper.EndpointGroupID = ep.EndpointGroupID
					epOper.EndpointGroupKey = ep.EndpointGroupKey
					epOper.IpAddress = []string{ep.IPAddress, ep.IPv6Address}
					epOper.MacAddress = ep.MacAddress
					epOper.HomingHost = ep.HomingHost
					epOper.IntfName = ep.IntfName
					epOper.VtepIP = ep.VtepIP
					epOper.Labels = fmt.Sprintf("%s", ep.Labels)
					epOper.ContainerID = ep.ContainerID
					epOper.ContainerName = ep.EPCommonName
					policy.Oper.Endpoints = append(policy.Oper.Endpoints, epOper)
				}
			}
		}
	} // End of main for loop

	policy.Oper.NumEndpoints = policyEPCount

	return nil
}
1062829
func recordOperation(operation string, start time.Time) {
	metrics.NetworkPluginOperationsLatency.WithLabelValues(operation).Observe(metrics.SinceInSeconds(start))
	metrics.DeprecatedNetworkPluginOperationsLatency.WithLabelValues(operation).Observe(metrics.SinceInMicroseconds(start))
}
302184
func GetOperTenant(obj *TenantInspect) error {
	// Check if we handle this object
	if objCallbackHandler.TenantCb == nil {
		log.Errorf("No callback registered for tenant object")
		return errors.New("Invalid object type")
	}

	// Perform callback
	err := objCallbackHandler.TenantCb.TenantGetOper(obj)
	if err != nil {
		log.Errorf("TenantDelete retruned error for: %+v. Err: %v", obj, err)
		return err
	}

	return nil
}
1284983
func (c *Client) BLPop(timeout int, keys ...string) (k, v string, err error) {
	return c.blbrPop("BLPOP", timeout, keys...)
}
109761
func (c *ContivClient) BgpDelete(hostname string) error {
	// build key and URL
	keyStr := hostname
	url := c.baseURL + "/api/v1/Bgps/" + keyStr + "/"

	// http get the object
	err := c.httpDelete(url)
	if err != nil {
		log.Debugf("Error deleting Bgp %s. Err: %v", keyStr, err)
		return err
	}

	return nil
}
1013532
func MapGetByIndexOp(binName string, index int, returnType mapReturnType) *Operation {
	return newCDTCreateOperationValue1(_CDT_MAP_GET_BY_INDEX, _MAP_READ, binName, index, returnType)
}
247409
func (d *OvsDriver) InspectBgp() ([]byte, error) {

	// get vlan switch state
	bgpState, err := d.switchDb["vlan"].InspectBgp()
	if err != nil {
		return []byte{}, err
	}

	// json marshall the map
	jsonState, err := json.Marshal(bgpState)
	if err != nil {
		log.Errorf("Error encoding epstats. Err: %v", err)
		return []byte{}, err
	}

	return jsonState, nil
}
642797
func (p *XPod) protectedSandboxOperation(op sandboxOp, timeout time.Duration, comment string) error {
	dangerousOp := func(sb *hypervisor.Vm, errChan chan<- error) {
		defer func() {
			err := recover()
			if err != nil {
				p.Log(ERROR, err)
				if re, ok := err.(error); ok {
					errChan <- re
				} else {
					errChan <- fmt.Errorf("%v", err)
				}
			}
		}()

		if sb != nil {
			errChan <- op(sb)
		} else {
			p.Log(WARNING, "%s: sandbox not existed", comment)
			errChan <- nil
		}
	}

	errChan := make(chan error, 1)

	p.statusLock.Lock()
	go dangerousOp(p.sandbox, errChan)
	p.statusLock.Unlock()

	var timeoutChan <-chan time.Time
	if timeout < 0 {
		timeoutChan = make(chan time.Time, 1)
	} else {
		timeoutChan = time.After(timeout)
	}

	select {
	case err, ok := <-errChan:
		if !ok {
			err := fmt.Errorf("%s: failed to get operation result", comment)
			p.Log(ERROR, err)
			return err
		}
		return err
	case <-timeoutChan:
		err := fmt.Errorf("%s: timeout (%v) during waiting operation result", comment, timeout)
		p.Log(ERROR, err)
		return err
	}
}
1175108
func (p *Parser) parseOperator() (op selection.Operator, err error) {
	tok, lit := p.consume(KeyAndOperator)
	switch tok {
	// DoesNotExistToken shouldn't be here because it's a unary operator, not a binary operator
	case InToken:
		op = selection.In
	case EqualsToken:
		op = selection.Equals
	case DoubleEqualsToken:
		op = selection.DoubleEquals
	case GreaterThanToken:
		op = selection.GreaterThan
	case LessThanToken:
		op = selection.LessThan
	case NotInToken:
		op = selection.NotIn
	case NotEqualsToken:
		op = selection.NotEquals
	default:
		return "", fmt.Errorf("found '%s', expected: '=', '!=', '==', 'in', notin'", lit)
	}
	return op, nil
}
805761
func (ac *APIController) ServiceLBGetOper(serviceLB *contivModel.ServiceLBInspect) error {
	log.Infof("Received Service load balancer inspect : %+v", serviceLB)

	// Get the state driver
	stateDriver, err := utils.GetStateDriver()
	if err != nil {
		return err
	}
	serviceID := master.GetServiceID(serviceLB.Config.ServiceName, serviceLB.Config.TenantName)
	service := mastercfg.ServiceLBDb[serviceID]
	if service == nil {
		return errors.New("invalid Service name. Oper state does not exist")
	}
	serviceLB.Oper.ServiceVip = service.IPAddress
	count := 0
	for _, provider := range service.Providers {

		epCfg := &mastercfg.CfgEndpointState{}
		epCfg.StateDriver = stateDriver
		err := epCfg.Read(provider.EpIDKey)
		if err != nil {
			continue
		}
		epOper := contivModel.EndpointOper{}
		epOper.Network = epCfg.NetID
		epOper.EndpointID = epCfg.EndpointID
		epOper.ServiceName = service.ServiceName //FIXME:fill in service name in endpoint
		epOper.EndpointGroupID = epCfg.EndpointGroupID
		epOper.EndpointGroupKey = epCfg.EndpointGroupKey
		epOper.IpAddress = []string{epCfg.IPAddress, epCfg.IPv6Address}
		epOper.MacAddress = epCfg.MacAddress
		epOper.HomingHost = epCfg.HomingHost
		epOper.IntfName = epCfg.IntfName
		epOper.VtepIP = epCfg.VtepIP
		epOper.Labels = fmt.Sprintf("%s", epCfg.Labels)
		epOper.ContainerID = epCfg.ContainerID
		epOper.ContainerName = epCfg.EPCommonName
		serviceLB.Oper.Providers = append(serviceLB.Oper.Providers, epOper)
		count++
		epCfg = nil
	}
	serviceLB.Oper.NumProviders = count
	return nil

}
797933
func (ac *APIController) BgpCreate(bgpCfg *contivModel.Bgp) error {
	log.Infof("Received BgpCreate: %+v", bgpCfg)

	if bgpCfg.Hostname == "" {
		return core.Errorf("Invalid host name")
	}

	// Get the state driver
	stateDriver, err := utils.GetStateDriver()
	if err != nil {
		return err
	}

	// Build bgp config
	bgpIntentCfg := intent.ConfigBgp{
		Hostname:   bgpCfg.Hostname,
		RouterIP:   bgpCfg.Routerip,
		As:         bgpCfg.As,
		NeighborAs: bgpCfg.NeighborAs,
		Neighbor:   bgpCfg.Neighbor,
	}

	// Add the Bgp neighbor
	err = master.AddBgp(stateDriver, &bgpIntentCfg)
	if err != nil {
		log.Errorf("Error creating Bgp neighbor {%+v}. Err: %v", bgpCfg.Neighbor, err)
		return err
	}
	return nil
}