Example #1
0
// mockAgent creates the agent with the configuration
// needed for tests without the need to go through
// configuration files.
func mockAgent() Agent {

	host0 := common.HostMessage{Ip: "172.17.0.1", RomanaIp: "127.0.0.1/8"}

	romanaIP, romanaNet, _ := net.ParseCIDR(host0.RomanaIp)
	networkConfig := &NetworkConfig{}
	networkConfig.currentHostIP = net.ParseIP(host0.Ip)
	networkConfig.currentHostGW = romanaIP
	networkConfig.currentHostGWNet = *romanaNet
	networkConfig.currentHostGWNetSize, _ = romanaNet.Mask.Size()
	networkConfig.currentHostIndex = 0

	host1 := common.HostMessage{Ip: "192.168.0.12", RomanaIp: "10.65.0.0/16"}
	networkConfig.hosts = []common.HostMessage{host0, host1}

	dc := common.Datacenter{}
	dc.Cidr = "10.0.0.0/8"
	dc.PortBits = 8
	dc.TenantBits = 4
	dc.SegmentBits = 4
	dc.EndpointSpaceBits = 0
	dc.EndpointBits = 8

	networkConfig.dc = dc

	agent := &Agent{networkConfig: networkConfig}
	helper := NewAgentHelper(agent)
	agent.Helper = &helper

	return *agent
}
Example #2
0
// SetConfig implements SetConfig function of the Service interface.
// Returns an error if cannot connect to the data store
func (topology *TopologySvc) SetConfig(config common.ServiceConfig) error {
	topology.config = config
	dcMap := config.ServiceSpecific["datacenter"].(map[string]interface{})
	dc := common.Datacenter{}
	dc.IpVersion = uint(dcMap["ip_version"].(float64))
	if dc.IpVersion != 4 {
		return common.NewError("Only IPv4 is currently supported.")
	}
	dc.Cidr = dcMap["cidr"].(string)
	_, ipNet, err := net.ParseCIDR(dc.Cidr)
	if err != nil {
		return err
	}
	prefixBits, _ := ipNet.Mask.Size()
	dc.PrefixBits = uint(prefixBits)

	dc.PortBits = uint(dcMap["host_bits"].(float64))
	dc.TenantBits = uint(dcMap["tenant_bits"].(float64))
	dc.SegmentBits = uint(dcMap["segment_bits"].(float64))
	dc.EndpointBits = uint(dcMap["endpoint_bits"].(float64))
	dc.EndpointSpaceBits = uint(dcMap["endpoint_space_bits"].(float64))
	if dc.EndpointBits == 0 {
		return common.NewError("Endpoint bits may not be 0")
	}
	bitSum := dc.PrefixBits + dc.PortBits + dc.TenantBits + dc.SegmentBits + dc.EndpointBits + dc.EndpointSpaceBits
	if bitSum != 32 {
		bitSumStr := fmt.Sprintf("%s+%d+%d+%d+%d+%d", dc.Cidr, dc.PortBits, dc.TenantBits, dc.SegmentBits, dc.EndpointBits, dc.EndpointSpaceBits)
		return common.NewError("Sum of prefix, port, tenant, segment, endpoint and endpoint space bits must be exactly 32, but it is %s=%d", bitSumStr, bitSum)
	}

	// TODO this should have worked but it doesn't...
	//	err := mapstructure.Decode(dcMap, &dc)
	//	if err != nil {
	//		return err
	//	}
	log.Printf("Datacenter information: was %s, decoded to %+v\n", dcMap, dc)
	topology.datacenter = &dc
	storeConfig := config.ServiceSpecific["store"].(map[string]interface{})
	topology.store = topoStore{}
	topology.store.ServiceStore = &topology.store
	return topology.store.SetConfig(storeConfig)
}
Example #3
0
// SetConfig implements SetConfig function of the Service interface.
// Returns an error if cannot connect to the data store
func (topology *TopologySvc) SetConfig(config common.ServiceConfig) error {
	log.Println(config)
	topology.config = config
	dcMap := config.ServiceSpecific["datacenter"].(map[string]interface{})
	dc := common.Datacenter{}
	dc.IpVersion = uint(dcMap["ip_version"].(float64))
	dc.Cidr = dcMap["cidr"].(string)
	_, ipNet, err := net.ParseCIDR(dc.Cidr)
	if err != nil {
		return err
	}
	prefixBits, _ := ipNet.Mask.Size()
	dc.PrefixBits = uint(prefixBits)

	dc.PortBits = uint(dcMap["host_bits"].(float64))
	dc.TenantBits = uint(dcMap["tenant_bits"].(float64))
	dc.SegmentBits = uint(dcMap["segment_bits"].(float64))
	dc.EndpointBits = uint(dcMap["endpoint_bits"].(float64))
	dc.EndpointSpaceBits = uint(dcMap["endpoint_space_bits"].(float64))
	// TODO this should have worked but it doesn't...
	//	err := mapstructure.Decode(dcMap, &dc)
	//	if err != nil {
	//		return err
	//	}
	log.Printf("Datacenter information: was %s, decoded to %s\n", dcMap, dc)
	topology.datacenter = &dc
	storeConfig := config.ServiceSpecific["store"].(map[string]interface{})
	storeType := strings.ToLower(storeConfig["type"].(string))
	switch storeType {
	case "mysql":
		topology.store = &mysqlStore{}

	case "mock":
		topology.store = &mockStore{}

	default:
		return errors.New("Unknown store type: " + storeType)
	}
	return topology.store.setConfig(storeConfig)
}
Example #4
0
File: mocks.go Project: romana/core
// mockAgent creates the agent with the configuration
// needed for tests without the need to go through
// configuration files.
func mockAgent() Agent {

	host0 := common.Host{Ip: "172.17.0.1", RomanaIp: "127.0.0.1/8"}

	// romanaIP, romanaNet, _ := net.ParseCIDR(host0.RomanaIp)

	networkConfig := &NetworkConfig{}
	networkConfig.romanaGW = net.ParseIP(host0.Ip)

	host1 := common.Host{Ip: "192.168.0.12", RomanaIp: "10.65.0.0/16"}
	networkConfig.otherHosts = []common.Host{host1}

	dc := common.Datacenter{}
	dc.Cidr = "10.0.0.0/8"
	dc.PortBits = 8
	dc.TenantBits = 4
	dc.SegmentBits = 4
	dc.EndpointSpaceBits = 0
	dc.EndpointBits = 8

	networkConfig.dc = dc
	agent := &Agent{networkConfig: networkConfig}
	helper := NewAgentHelper(agent)
	agent.Helper = &helper

	storeConfig := common.ServiceConfig{ServiceSpecific: map[string]interface{}{
		"type":     "sqlite3",
		"database": "/tmp/agent.db"}}
	agent.store = agentStore{}
	agent.store.ServiceStore = &agent.store
	agent.store.SetConfig(storeConfig.ServiceSpecific)

	agent.CreateSchema(true) // overwrite

	return *agent
}