Commit 333bb286 authored by Michel Roy's avatar Michel Roy Committed by Kevin Di Lallo
Browse files

model package update & fixes

parent 34f92d89
Loading
Loading
Loading
Loading
+3 −5
Original line number Diff line number Diff line
@@ -83,17 +83,15 @@ func eventHandler(channel string, payload string) {
}

func processActiveScenarioUpdate(event string) {
	if event == "TERMINATE" {
	if event == mod.EventTerminate {
		terminateScenario(activeScenarioName)
		activeScenarioName = ""
	} else if event == "ACTIVATE" {
	} else if event == mod.EventActivate {
		// Cache name for later deletion
		activeScenarioName = activeModel.GetScenarioName()
		activateScenario()
	} else if event == "UPDATE" {
		log.Debug("Reveived UPDATE event - do nothing")
	} else {
		log.Warn("Reveived unknown event: " + event)
		log.Debug("Reveived event: ", event, " - Do nothing")
	}
}

+63 −16
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ package model
import (
	"encoding/json"
	"errors"
	"fmt"
	"reflect"
	"strings"

@@ -29,14 +28,27 @@ import (
)

// const activeScenarioEvents = "activeScenarioEvents"
const activeScenarioEvents = "ctrl-engine-active"
const ActiveScenarioEvents = "ctrl-engine-active"

// const activeScenarioKey = "activeScenarioKey"
const activeScenarioKey = "ctrl-engine:active"

var DbAddress = "meep-redis-master:6379"
// Context keys for model type 1
const (
	PhyLoc string = "PhyLoc"
	NetLoc string = "NetLoc"
	Zone   string = "Zone"
	Domain string = "Domain"
)

var redisTable = 0
// Event types (basic)
const (
	EventActivate  = "ACTIVATE"
	EventTerminate = "TERMINATE"
	EventUpdate    = "UPDATE"
)

type NodeContext map[string]string

// Model - Implements a Meep Model
type Model struct {
@@ -53,6 +65,9 @@ type Model struct {
	nodeMap       *NodeMap
}

var DbAddress = "meep-redis-master:6379"
var redisTable = 0

// NewModel - Create a model object
func NewModel(dbAddr string, module string, name string) (m *Model, err error) {
	if name == "" {
@@ -71,7 +86,7 @@ func NewModel(dbAddr string, module string, name string) (m *Model, err error) {
	m.module = module
	m.Active = false
	m.subscribed = false
	m.ActiveChannel = activeScenarioEvents
	m.ActiveChannel = ActiveScenarioEvents
	m.activeKey = activeScenarioKey
	m.scenario = new(ceModel.Scenario)
	m.nodeMap = NewNodeMap()
@@ -161,7 +176,7 @@ func (m *Model) Activate() (err error) {
		log.Error(err.Error())
		return err
	}
	err = m.rc.Publish(m.ActiveChannel, "")
	err = m.rc.Publish(m.ActiveChannel, EventActivate)
	if err != nil {
		log.Error(err.Error())
		return err
@@ -179,7 +194,7 @@ func (m *Model) Deactivate() (err error) {
			log.Error(err.Error())
			return err
		}
		err = m.rc.Publish(m.ActiveChannel, "")
		err = m.rc.Publish(m.ActiveChannel, EventTerminate)
		if err != nil {
			log.Error(err.Error())
			return err
@@ -331,7 +346,7 @@ func (m *Model) UpdateNetChar(nc *ceModel.EventNetworkCharacteristicsUpdate) (er

//GetScenarioName - Get the scenario name
func (m *Model) GetScenarioName() string {
	fmt.Printf("%+v", m)
	// fmt.Printf("%+v", m)
	if m.scenario != nil {
		return m.scenario.Name
	}
@@ -356,7 +371,7 @@ func (m *Model) GetNodeNames(typ string) []string {
	return list
}

//GetNodeEdges - Get a map of node edges for the current scenario
//GetEdges - Get a map of node edges for the current scenario
func (m *Model) GetEdges() (edgeMap map[string]string) {
	edgeMap = make(map[string]string)
	for k, node := range m.nodeMap.nameMap {
@@ -382,6 +397,18 @@ func (m *Model) GetNode(name string) (node interface{}) {
	return node
}

// GetNodeContext - Get a node context
// 		Returned value is of type interface{}
//    Good practice: returned node should be type asserted with val,ok := node.(someType) to prevent panic
func (m *Model) GetNodeContext(name string) (ctx interface{}) {
	ctx = nil
	n := m.nodeMap.nameMap[name]
	if n != nil {
		ctx = n.context
	}
	return ctx
}

//---Internal Funcs---

func (m *Model) parseNodes() (err error) {
@@ -390,19 +417,38 @@ func (m *Model) parseNodes() (err error) {
			// Parse through scenario and fill external node service mappings
			for iDomain := range m.scenario.Deployment.Domains {
				domain := &m.scenario.Deployment.Domains[iDomain]
				m.nodeMap.AddNode(NewNode(domain.Name, domain.Type_, domain, &domain.Zones, m.scenario.Deployment))
				ctx := make(NodeContext)
				ctx[Domain] = domain.Name
				m.nodeMap.AddNode(NewNode(domain.Name, domain.Type_, domain, &domain.Zones, m.scenario.Deployment, ctx))
				for iZone := range domain.Zones {
					zone := &domain.Zones[iZone]
					m.nodeMap.AddNode(NewNode(zone.Name, zone.Type_, zone, &zone.NetworkLocations, domain))
					ctx := make(NodeContext)
					ctx[Domain] = domain.Name
					ctx[Zone] = zone.Name
					m.nodeMap.AddNode(NewNode(zone.Name, zone.Type_, zone, &zone.NetworkLocations, domain, ctx))
					for iNL := range zone.NetworkLocations {
						nl := &zone.NetworkLocations[iNL]
						m.nodeMap.AddNode(NewNode(nl.Name, nl.Type_, nl, &nl.PhysicalLocations, zone))
						ctx := make(NodeContext)
						ctx[Domain] = domain.Name
						ctx[Zone] = zone.Name
						ctx[NetLoc] = nl.Name
						m.nodeMap.AddNode(NewNode(nl.Name, nl.Type_, nl, &nl.PhysicalLocations, zone, ctx))
						for iPL := range nl.PhysicalLocations {
							pl := &nl.PhysicalLocations[iPL]
							m.nodeMap.AddNode(NewNode(pl.Name, pl.Type_, pl, &pl.Processes, nl))
							ctx := make(NodeContext)
							ctx[Domain] = domain.Name
							ctx[Zone] = zone.Name
							ctx[NetLoc] = nl.Name
							ctx[PhyLoc] = pl.Name
							m.nodeMap.AddNode(NewNode(pl.Name, pl.Type_, pl, &pl.Processes, nl, ctx))
							for iProc := range pl.Processes {
								proc := &pl.Processes[iProc]
								m.nodeMap.AddNode(NewNode(proc.Name, proc.Type_, proc, nil, pl))
								ctx := make(NodeContext)
								ctx[Domain] = domain.Name
								ctx[Zone] = zone.Name
								ctx[NetLoc] = nl.Name
								ctx[PhyLoc] = pl.Name
								m.nodeMap.AddNode(NewNode(proc.Name, proc.Type_, proc, nil, pl, ctx))
							}
						}
					}
@@ -417,6 +463,7 @@ func (m *Model) updateSvcMap() (err error) {
	if m.scenario.Deployment == nil {
		m.svcMap = nil
	} else {
		m.svcMap = make([]ceModel.NodeServiceMaps, 0)
		// Parse through scenario and fill external node service mappings
		for _, domain := range m.scenario.Deployment.Domains {
			for _, zone := range domain.Zones {
@@ -461,7 +508,7 @@ func (m *Model) refresh() (err error) {
			log.Error(err.Error())
			return err
		}
		err = m.rc.Publish(m.ActiveChannel, "")
		err = m.rc.Publish(m.ActiveChannel, EventUpdate)
		if err != nil {
			log.Error(err.Error())
			return err
@@ -581,7 +628,7 @@ func (m *Model) internalListener(channel string, payload string) {
	j, err := m.rc.JSONGetEntry(m.activeKey, ".")
	log.Debug("Scenario Event:", j)
	if err != nil {
		log.Debug("Scenario was deleted, create a new one")
		log.Debug("Scenario was deleted")
		// Scenario was deleted
		m.scenario = new(ceModel.Scenario)
		m.nodeMap = NewNodeMap()
+55 −0
Original line number Diff line number Diff line
@@ -847,4 +847,59 @@ func TestGetters(t *testing.T) {
		t.Errorf("Zone1 edge - expected operator1 -- got %s", edges["zone1"])
	}

	fmt.Println("Get context for invalid node")
	ctx := m.GetNodeContext("NOT-A-NODE")
	if ctx != nil {
		t.Errorf("Node context should not exist")
	}

	fmt.Println("Get Operator context")
	ctx = m.GetNodeContext("operator1")
	if ctx == nil {
		t.Errorf("Node context should not exist")
	}
	nodeCtx, ok := ctx.(NodeContext)
	if !ok || len(nodeCtx) != 1 || nodeCtx[Domain] != "operator1" {
		t.Errorf("Invalid Operator context")
	}

	fmt.Println("Get Zone context")
	ctx = m.GetNodeContext("zone1")
	if ctx == nil {
		t.Errorf("Node context should not exist")
	}
	nodeCtx, ok = ctx.(NodeContext)
	if !ok || len(nodeCtx) != 2 || nodeCtx[Domain] != "operator1" || nodeCtx[Zone] != "zone1" {
		t.Errorf("Invalid Operator context")
	}

	fmt.Println("Get Net Location context")
	ctx = m.GetNodeContext("zone1-poa1")
	if ctx == nil {
		t.Errorf("Node context should not exist")
	}
	nodeCtx, ok = ctx.(NodeContext)
	if !ok || len(nodeCtx) != 3 || nodeCtx[Domain] != "operator1" || nodeCtx[Zone] != "zone1" || nodeCtx[NetLoc] != "zone1-poa1" {
		t.Errorf("Invalid Operator context")
	}

	fmt.Println("Get Phy Location context")
	ctx = m.GetNodeContext("zone1-fog1")
	if ctx == nil {
		t.Errorf("Node context should not exist")
	}
	nodeCtx, ok = ctx.(NodeContext)
	if !ok || len(nodeCtx) != 4 || nodeCtx[Domain] != "operator1" || nodeCtx[Zone] != "zone1" || nodeCtx[NetLoc] != "zone1-poa1" || nodeCtx[PhyLoc] != "zone1-fog1" {
		t.Errorf("Invalid Operator context")
	}

	fmt.Println("Get App context")
	ctx = m.GetNodeContext("ue1-iperf")
	if ctx == nil {
		t.Errorf("Node context should not exist")
	}
	nodeCtx, ok = ctx.(NodeContext)
	if !ok || len(nodeCtx) != 4 || nodeCtx[Domain] != "operator1" || nodeCtx[Zone] != "zone1" || nodeCtx[NetLoc] != "zone1-poa1" || nodeCtx[PhyLoc] != "ue1" {
		t.Errorf("Invalid Operator context")
	}
}
+3 −1
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ type Node struct {
	object   interface{}
	child    interface{}
	parent   interface{}
	context  interface{}
}

// NodeMap - Model node map
@@ -40,13 +41,14 @@ func NewNodeMap() (nm *NodeMap) {
}

// NewNode - allocate a Node
func NewNode(name string, nodeType string, object interface{}, child interface{}, parent interface{}) (n *Node) {
func NewNode(name string, nodeType string, object interface{}, child interface{}, parent interface{}, context interface{}) (n *Node) {
	n = new(Node)
	n.name = name
	n.nodeType = nodeType
	n.object = object
	n.child = child
	n.parent = parent
	n.context = context
	return n
}

+45 −5
Original line number Diff line number Diff line
@@ -51,7 +51,10 @@ func TestNodeMapDomains(t *testing.T) {
	////fmt.Printf("  Scenario has %d domains\n", len(scenario.Deployment.Domains))
	for i := range scenario.Deployment.Domains {
		domain := &scenario.Deployment.Domains[i]
		nm.AddNode(NewNode(domain.Name, domain.Type_, domain, &domain.Zones, scenario.Deployment))
		context := make(map[string]string)
		context["pl"] = "phyLoc"
		context["nl"] = "netLoc"
		nm.AddNode(NewNode(domain.Name, domain.Type_, domain, &domain.Zones, scenario.Deployment, context))
		////fmt.Printf("  domain%d: object @ %p\n%+v\n", i, &scenario.Deployment.Domains[i], domain)
		////fmt.Printf("  domain%d: child @ %p\n%+v\n", i, &domain.Zones, domain.Zones)
		////fmt.Printf("  domain%d: parent @ %p\n%+v\n", i, scenario.Deployment, *scenario.Deployment)
@@ -100,6 +103,11 @@ func TestNodeMapDomains(t *testing.T) {
	if scenario.Deployment.InterDomainLatency != 500 {
		t.Errorf("Failed changing Deployment InterDomainLatency")
	}
	// Verify Node context
	context := n.context.(map[string]string)
	if context["pl"] != "phyLoc" || context["nl"] != "netLoc" {
		t.Errorf("Failed to set context entries")
	}
}

func TestNodeMapZone(t *testing.T) {
@@ -129,7 +137,10 @@ func TestNodeMapZone(t *testing.T) {
	//fmt.Printf("  scenario.Deployment.Domains[1] has %d zones\n", len(scenario.Deployment.Domains[1].Zones))
	for i := range domain.Zones {
		zone := &domain.Zones[i]
		nm.AddNode(NewNode(zone.Name, zone.Type_, zone, &zone.NetworkLocations, domain))
		context := make(map[string]string)
		context["pl"] = "phyLoc"
		context["nl"] = "netLoc"
		nm.AddNode(NewNode(zone.Name, zone.Type_, zone, &zone.NetworkLocations, domain, context))
		//fmt.Printf("  zone%d: object @ %p\n%+v\n", i, zone, *zone)
		//fmt.Printf("  zone%d: child @ %p\n%+v\n", i, &zone.NetworkLocations, zone.NetworkLocations)
		//fmt.Printf("  zone%d: parent @ %p\n%+v\n", i, domain, *domain)
@@ -168,6 +179,11 @@ func TestNodeMapZone(t *testing.T) {
	if domain.Id != testID {
		t.Errorf("Failed changing Deployment InterDomainLatency")
	}
	// Verify Node context
	context := n.context.(map[string]string)
	if context["pl"] != "phyLoc" || context["nl"] != "netLoc" {
		t.Errorf("Failed to set context entries")
	}
}

func TestNodeMapNetworkLocation(t *testing.T) {
@@ -197,7 +213,10 @@ func TestNodeMapNetworkLocation(t *testing.T) {
	//fmt.Printf("  scenario.Deployment.Domains[1].Zones[1] has %d NL\n", len(zone.NetworkLocations))
	for i := range zone.NetworkLocations {
		nl := &zone.NetworkLocations[i]
		nm.AddNode(NewNode(nl.Name, nl.Type_, nl, &nl.PhysicalLocations, zone))
		context := make(map[string]string)
		context["pl"] = "phyLoc"
		context["nl"] = "netLoc"
		nm.AddNode(NewNode(nl.Name, nl.Type_, nl, &nl.PhysicalLocations, zone, context))
		//fmt.Printf("  nl%d: object @ %p\n%+v\n", i, nl, *nl)
		//fmt.Printf("  nl%d: child @ %p\n%+v\n", i, &nl.PhysicalLocations, nl.PhysicalLocations)
		//fmt.Printf("  nl%d: parent @ %p\n%+v\n", i, zone, *zone)
@@ -236,6 +255,11 @@ func TestNodeMapNetworkLocation(t *testing.T) {
	if zone.Id != testID {
		t.Errorf("Failed changing Zone id")
	}
	// Verify Node context
	context := n.context.(map[string]string)
	if context["pl"] != "phyLoc" || context["nl"] != "netLoc" {
		t.Errorf("Failed to set context entries")
	}
}

func TestNodeMapPhysicalLocation(t *testing.T) {
@@ -265,7 +289,10 @@ func TestNodeMapPhysicalLocation(t *testing.T) {
	//fmt.Printf("  scenario.Deployment.Domains[1].Zones[1].NetworkLocations[1] has %d PL\n", len(nl.PhysicalLocations))
	for i := range nl.PhysicalLocations {
		pl := &nl.PhysicalLocations[i]
		nm.AddNode(NewNode(pl.Name, pl.Type_, pl, &pl.Processes, nl))
		context := make(map[string]string)
		context["pl"] = "phyLoc"
		context["nl"] = "netLoc"
		nm.AddNode(NewNode(pl.Name, pl.Type_, pl, &pl.Processes, nl, context))
		//fmt.Printf("  nl%d: object @ %p\n%+v\n", i, pl, *pl)
		//fmt.Printf("  nl%d: child @ %p\n%+v\n", i, &pl.Processes, pl.Processes)
		//fmt.Printf("  nl%d: parent @ %p\n%+v\n", i, nl, *nl)
@@ -304,6 +331,11 @@ func TestNodeMapPhysicalLocation(t *testing.T) {
	if nl.Id != testID {
		t.Errorf("Failed changing NL id")
	}
	// Verify Node context
	context := n.context.(map[string]string)
	if context["pl"] != "phyLoc" || context["nl"] != "netLoc" {
		t.Errorf("Failed to set context entries")
	}
}

func TestNodeMapProcess(t *testing.T) {
@@ -333,7 +365,10 @@ func TestNodeMapProcess(t *testing.T) {
	//fmt.Printf("  scenario.Deployment.Domains[1].Zones[1].NetworkLocations[1].PhysicalLocation[0] has %d processes\n", len(pl.Processes))
	for i := range pl.Processes {
		proc := &pl.Processes[i]
		nm.AddNode(NewNode(proc.Name, proc.Type_, proc, nil, pl))
		context := make(map[string]string)
		context["pl"] = "phyLoc"
		context["nl"] = "netLoc"
		nm.AddNode(NewNode(proc.Name, proc.Type_, proc, nil, pl, context))
		//fmt.Printf("  nl%d: object @ %p\n%+v\n", i, proc, *proc)
		//fmt.Printf("  nl%d: child @ nil\n%+v\n", i, nil)
		//fmt.Printf("  nl%d: parent @ %p\n%+v\n", i, pl, *pl)
@@ -365,4 +400,9 @@ func TestNodeMapProcess(t *testing.T) {
	if pl.Id != testID {
		t.Errorf("Failed changing PL id")
	}
	// Verify Node context
	context := n.context.(map[string]string)
	if context["pl"] != "phyLoc" || context["nl"] != "netLoc" {
		t.Errorf("Failed to set context entries")
	}
}