Commit 36112143 authored by Sonja Happ's avatar Sonja Happ
Browse files

Merge branch 'renaming-simulator-simmodel' into 'master'

Renaming simulator and simmodel

See merge request !14
parents 9539c42e ef1dede7
......@@ -95,10 +95,10 @@ test:scenario:
variables:
TEST_FOLDER: routes/scenario
test:simulationmodel:
extends: test:scenario
test:component-configuration:
extends: test:database
variables:
TEST_FOLDER: routes/simulationmodel
TEST_FOLDER: routes/component-configuration
test:signal:
extends: test:database
......@@ -115,10 +115,10 @@ test:widget:
variables:
TEST_FOLDER: routes/widget
test:simulator:
test:infrastructure-component:
extends: test:database
variables:
TEST_FOLDER: routes/simulator
TEST_FOLDER: routes/infrastructure-component
test:file:
extends: test:database
......
......@@ -24,32 +24,32 @@ package amqp
import (
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/simulator"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/infrastructure-component"
"github.com/gin-gonic/gin"
"net/http"
"time"
)
func RegisterAMQPEndpoint(r *gin.RouterGroup) {
r.POST("/:simulatorID/action", sendActionToSimulator)
r.POST("/ICID/action", sendActionToIC)
}
// sendActionToSimulator godoc
// @Summary Send an action to simulator (only available if backend server is started with -amqp parameter)
// @ID sendActionToSimulator
// sendActionToIC godoc
// @Summary Send an action to IC (only available if backend server is started with -amqp parameter)
// @ID sendActionToIC
// @Tags AMQP
// @Produce json
// @Param inputAction query string true "Action for simulator"
// @Param inputAction query string true "Action for IC"
// @Success 200 {object} docs.ResponseError "Action sent successfully"
// @Failure 400 {object} docs.ResponseError "Bad request"
// @Failure 404 {object} docs.ResponseError "Not found"
// @Failure 422 {object} docs.ResponseError "Unprocessable entity"
// @Failure 500 {object} docs.ResponseError "Internal server error"
// @Param simulatorID path int true "Simulator ID"
// @Router /simulators/{simulatorID}/action [post]
func sendActionToSimulator(c *gin.Context) {
// @Param ICID path int true "InfrastructureComponent ID"
// @Router /ic/{ICID}/action [post]
func sendActionToIC(c *gin.Context) {
ok, s := simulator.CheckPermissions(c, database.ModelSimulatorAction, database.Update, true)
ok, s := infrastructure_component.CheckPermissions(c, database.ModelInfrastructureComponentAction, database.Update, true)
if !ok {
return
}
......@@ -70,7 +70,7 @@ func sendActionToSimulator(c *gin.Context) {
err = SendActionAMQP(action, s.UUID)
if err != nil {
helper.InternalServerError(c, "Unable to send actions to simulator: "+err.Error())
helper.InternalServerError(c, "Unable to send actions to IC: "+err.Error())
return
}
}
......
......@@ -77,8 +77,8 @@ func ConnectAMQP(uri string) error {
return fmt.Errorf("AMQP: failed to declare the exchange")
}
// add a queue for the simulators
simulatorQueue, err := client.channel.QueueDeclare("simulators",
// add a queue for the ICs
ICQueue, err := client.channel.QueueDeclare("infrastructure_components",
true,
false,
false,
......@@ -88,13 +88,13 @@ func ConnectAMQP(uri string) error {
return fmt.Errorf("AMQP: failed to declare the queue")
}
err = client.channel.QueueBind(simulatorQueue.Name, "", VILLAS_EXCHANGE, false, nil)
err = client.channel.QueueBind(ICQueue.Name, "", VILLAS_EXCHANGE, false, nil)
if err != nil {
return fmt.Errorf("AMQP: failed to bind the queue")
}
// consume deliveries
client.replies, err = client.channel.Consume(simulatorQueue.Name,
client.replies, err = client.channel.Consume(ICQueue.Name,
"",
false,
false,
......@@ -120,15 +120,15 @@ func ConnectAMQP(uri string) error {
continue
}
var sToBeUpdated database.Simulator
var sToBeUpdated database.InfrastructureComponent
db := database.GetDB()
simulatorUUID := gjson.Get(content, "properties.uuid").String()
if simulatorUUID == "" {
log.Println("AMQP: Could not extract UUID of simulator from content of received message, SIMULATOR NOT UPDATED")
ICUUID := gjson.Get(content, "properties.uuid").String()
if ICUUID == "" {
log.Println("AMQP: Could not extract UUID of IC from content of received message, COMPONENT NOT UPDATED")
} else {
err = db.Where("UUID = ?", simulatorUUID).Find(sToBeUpdated).Error
err = db.Where("UUID = ?", ICUUID).Find(sToBeUpdated).Error
if err != nil {
log.Println("AMQP: Unable to find simulator with UUID: ", gjson.Get(content, "properties.uuid"), " DB error message: ", err)
log.Println("AMQP: Unable to find IC with UUID: ", gjson.Get(content, "properties.uuid"), " DB error message: ", err)
}
err = db.Model(&sToBeUpdated).Updates(map[string]interface{}{
......@@ -140,10 +140,10 @@ func ConnectAMQP(uri string) error {
"RawProperties": gjson.Get(content, "properties"),
}).Error
if err != nil {
log.Println("AMQP: Unable to update simulator in DB: ", err)
log.Println("AMQP: Unable to update IC in DB: ", err)
}
log.Println("AMQP: Updated simulator with UUID ", gjson.Get(content, "properties.uuid"))
log.Println("AMQP: Updated IC with UUID ", gjson.Get(content, "properties.uuid"))
}
}
}()
......@@ -182,7 +182,7 @@ func SendActionAMQP(action Action, uuid string) error {
}
func PingAMQP() error {
log.Println("AMQP: sending ping command to all simulators")
log.Println("AMQP: sending ping command to all ICs")
var a Action
a.Act = "ping"
......
......@@ -55,9 +55,9 @@ read_users () {
-X GET | jq '.' && printf '\n'
}
read_simulators () {
printf "> GET "$apiBase"/simulators\n"
curl "$apiBase"/simulators -s \
read_infrastructure_components () {
printf "> GET "$apiBase"/ic\n"
curl "$apiBase"/ic -s \
-H "Contet-Type: application/json" \
-H "Authorization: Bearer $(< auth.jwt)" \
-X GET | jq '.' && printf '\n'
......@@ -114,7 +114,7 @@ login "$admin"
#create_user "$userC"
#read_users
#read_user 1
#read_simulators
#read_infrastructure_components
create_user "$newUserW"
#read_users
read_user 4
......
......@@ -102,9 +102,9 @@ func GetDB() *gorm.DB {
// TODO: Remove that function from the codebase and substitute the body
// to the Dummy*() where it is called
func DropTables(db *gorm.DB) {
db.DropTableIfExists(&Simulator{})
db.DropTableIfExists(&InfrastructureComponent{})
db.DropTableIfExists(&Signal{})
db.DropTableIfExists(&SimulationModel{})
db.DropTableIfExists(&ComponentConfiguration{})
db.DropTableIfExists(&File{})
db.DropTableIfExists(&Scenario{})
db.DropTableIfExists(&User{})
......@@ -116,9 +116,9 @@ func DropTables(db *gorm.DB) {
// AutoMigrate the models
func MigrateModels(db *gorm.DB) {
db.AutoMigrate(&Simulator{})
db.AutoMigrate(&InfrastructureComponent{})
db.AutoMigrate(&Signal{})
db.AutoMigrate(&SimulationModel{})
db.AutoMigrate(&ComponentConfiguration{})
db.AutoMigrate(&File{})
db.AutoMigrate(&Scenario{})
db.AutoMigrate(&User{})
......
......@@ -107,8 +107,8 @@ func TestScenarioAssociations(t *testing.T) {
user0 := User0
userA := UserA
userB := UserB
modelA := SimulationModelA
modelB := SimulationModelB
configA := ConfigA
configB := ConfigB
dashboardA := DashboardA
dashboardB := DashboardB
......@@ -121,9 +121,9 @@ func TestScenarioAssociations(t *testing.T) {
assert.NoError(t, db.Create(&userA).Error) // Normal User
assert.NoError(t, db.Create(&userB).Error) // Normal User
// add simulation models to DB
assert.NoError(t, db.Create(&modelA).Error)
assert.NoError(t, db.Create(&modelB).Error)
// add component configurations to DB
assert.NoError(t, db.Create(&configA).Error)
assert.NoError(t, db.Create(&configB).Error)
// add dashboards to DB
assert.NoError(t, db.Create(&dashboardA).Error)
......@@ -136,9 +136,9 @@ func TestScenarioAssociations(t *testing.T) {
assert.NoError(t, db.Model(&scenarioB).Association("Users").Append(&userA).Error)
assert.NoError(t, db.Model(&scenarioB).Association("Users").Append(&userB).Error)
// add scenario has many simulation models associations
assert.NoError(t, db.Model(&scenarioA).Association("SimulationModels").Append(&modelA).Error)
assert.NoError(t, db.Model(&scenarioA).Association("SimulationModels").Append(&modelB).Error)
// add scenario has many component configurations associations
assert.NoError(t, db.Model(&scenarioA).Association("ComponentConfigurations").Append(&configA).Error)
assert.NoError(t, db.Model(&scenarioA).Association("ComponentConfigurations").Append(&configB).Error)
// Scenario HM Dashboards
assert.NoError(t, db.Model(&scenarioA).Association("Dashboards").Append(&dashboardA).Error)
......@@ -156,12 +156,12 @@ func TestScenarioAssociations(t *testing.T) {
"Expected to have %v Users. Has %v.", 2, len(users))
}
// Get simulation models of scenario1
var models []SimulationModel
assert.NoError(t, db.Model(&scenario1).Related(&models, "SimulationModels").Error)
if len(models) != 2 {
// Get component configurations of scenario1
var configs []ComponentConfiguration
assert.NoError(t, db.Model(&scenario1).Related(&configs, "ComponentConfigurations").Error)
if len(configs) != 2 {
assert.Fail(t, "Scenario Associations",
"Expected to have %v simulation models. Has %v.", 2, len(models))
"Expected to have %v component configs. Has %v.", 2, len(configs))
}
// Get dashboards of scenario1
......@@ -173,50 +173,50 @@ func TestScenarioAssociations(t *testing.T) {
}
}
func TestSimulatorAssociations(t *testing.T) {
func TestICAssociations(t *testing.T) {
DropTables(db)
MigrateModels(db)
// create copies of global test data
simulatorA := SimulatorA
simulatorB := SimulatorB
modelA := SimulationModelA
modelB := SimulationModelB
// add simulators to DB
assert.NoError(t, db.Create(&simulatorA).Error)
assert.NoError(t, db.Create(&simulatorB).Error)
// add simulation models to DB
assert.NoError(t, db.Create(&modelA).Error)
assert.NoError(t, db.Create(&modelB).Error)
// add simulator has many simulation models association to DB
assert.NoError(t, db.Model(&simulatorA).Association("SimulationModels").Append(&modelA).Error)
assert.NoError(t, db.Model(&simulatorA).Association("SimulationModels").Append(&modelB).Error)
var simulator1 Simulator
assert.NoError(t, db.Find(&simulator1, 1).Error, fmt.Sprintf("Find Simulator with ID=1"))
assert.EqualValues(t, "Host_A", simulator1.Host)
// Get simulation models of simulator1
var models []SimulationModel
assert.NoError(t, db.Model(&simulator1).Association("SimulationModels").Find(&models).Error)
if len(models) != 2 {
assert.Fail(t, "Simulator Associations",
"Expected to have %v SimulationModels. Has %v.", 2, len(models))
icA := ICA
icB := ICB
configA := ConfigA
configB := ConfigB
// add ICs to DB
assert.NoError(t, db.Create(&icA).Error)
assert.NoError(t, db.Create(&icB).Error)
// add component configurations to DB
assert.NoError(t, db.Create(&configA).Error)
assert.NoError(t, db.Create(&configB).Error)
// add IC has many component configurations association to DB
assert.NoError(t, db.Model(&icA).Association("ComponentConfigurations").Append(&configA).Error)
assert.NoError(t, db.Model(&icA).Association("ComponentConfigurations").Append(&configB).Error)
var ic1 InfrastructureComponent
assert.NoError(t, db.Find(&ic1, 1).Error, fmt.Sprintf("Find InfrastructureComponent with ID=1"))
assert.EqualValues(t, "Host_A", ic1.Host)
// Get Component Configurations of ic1
var configs []ComponentConfiguration
assert.NoError(t, db.Model(&ic1).Association("ComponentConfigurations").Find(&configs).Error)
if len(configs) != 2 {
assert.Fail(t, "InfrastructureComponent Associations",
"Expected to have %v Component Configurations. Has %v.", 2, len(configs))
}
}
func TestSimulationModelAssociations(t *testing.T) {
func TestComponentConfigurationAssociations(t *testing.T) {
DropTables(db)
MigrateModels(db)
// create copies of global test data
modelA := SimulationModelA
modelB := SimulationModelB
configA := ConfigA
configB := ConfigB
outSignalA := OutSignalA
outSignalB := OutSignalB
inSignalA := InSignalA
......@@ -225,12 +225,12 @@ func TestSimulationModelAssociations(t *testing.T) {
fileB := FileB
fileC := FileC
fileD := FileD
simulatorA := SimulatorA
simulatorB := SimulatorB
icA := ICA
icB := ICB
// add simulation models to DB
assert.NoError(t, db.Create(&modelA).Error)
assert.NoError(t, db.Create(&modelB).Error)
// add Component Configurations to DB
assert.NoError(t, db.Create(&configA).Error)
assert.NoError(t, db.Create(&configB).Error)
// add signals to DB
assert.NoError(t, db.Create(&outSignalA).Error)
......@@ -244,46 +244,46 @@ func TestSimulationModelAssociations(t *testing.T) {
assert.NoError(t, db.Create(&fileC).Error)
assert.NoError(t, db.Create(&fileD).Error)
// add simulators to DB
assert.NoError(t, db.Create(&simulatorA).Error)
assert.NoError(t, db.Create(&simulatorB).Error)
// add ICs to DB
assert.NoError(t, db.Create(&icA).Error)
assert.NoError(t, db.Create(&icB).Error)
// add simulation model has many signals associations
assert.NoError(t, db.Model(&modelA).Association("InputMapping").Append(&inSignalA).Error)
assert.NoError(t, db.Model(&modelA).Association("InputMapping").Append(&inSignalB).Error)
assert.NoError(t, db.Model(&modelA).Association("OutputMapping").Append(&outSignalA).Error)
assert.NoError(t, db.Model(&modelA).Association("OutputMapping").Append(&outSignalB).Error)
// add Component Configuration has many signals associations
assert.NoError(t, db.Model(&configA).Association("InputMapping").Append(&inSignalA).Error)
assert.NoError(t, db.Model(&configA).Association("InputMapping").Append(&inSignalB).Error)
assert.NoError(t, db.Model(&configA).Association("OutputMapping").Append(&outSignalA).Error)
assert.NoError(t, db.Model(&configA).Association("OutputMapping").Append(&outSignalB).Error)
// add simulation model has many files associations
assert.NoError(t, db.Model(&modelA).Association("Files").Append(&fileC).Error)
assert.NoError(t, db.Model(&modelA).Association("Files").Append(&fileD).Error)
// add Component Configuration has many files associations
assert.NoError(t, db.Model(&configA).Association("Files").Append(&fileC).Error)
assert.NoError(t, db.Model(&configA).Association("Files").Append(&fileD).Error)
// associate simulation models with simulators
assert.NoError(t, db.Model(&simulatorA).Association("SimulationModels").Append(&modelA).Error)
assert.NoError(t, db.Model(&simulatorA).Association("SimulationModels").Append(&modelB).Error)
// associate Component Configurations with IC
assert.NoError(t, db.Model(&icA).Association("ComponentConfigurations").Append(&configA).Error)
assert.NoError(t, db.Model(&icA).Association("ComponentConfigurations").Append(&configB).Error)
var model1 SimulationModel
assert.NoError(t, db.Find(&model1, 1).Error, fmt.Sprintf("Find SimulationModel with ID=1"))
assert.EqualValues(t, "SimulationModel_A", model1.Name)
var config1 ComponentConfiguration
assert.NoError(t, db.Find(&config1, 1).Error, fmt.Sprintf("Find ComponentConfiguration with ID=1"))
assert.EqualValues(t, ConfigA.Name, config1.Name)
// Check simulator ID
if model1.SimulatorID != 1 {
assert.Fail(t, "Simulation Model expected to have Simulator ID 1, but is %v", model1.SimulatorID)
// Check IC ID
if config1.ICID != 1 {
assert.Fail(t, "Component Configurations expected to have Infrastructure Component ID 1, but is %v", config1.ICID)
}
// Get OutputMapping signals of model1
// Get OutputMapping signals of config1
var signals []Signal
assert.NoError(t, db.Model(&model1).Where("Direction = ?", "out").Related(&signals, "OutputMapping").Error)
assert.NoError(t, db.Model(&config1).Where("Direction = ?", "out").Related(&signals, "OutputMapping").Error)
if len(signals) != 2 {
assert.Fail(t, "SimulationModel Associations",
assert.Fail(t, "ComponentConfiguration Associations",
"Expected to have %v Output Signals. Has %v.", 2, len(signals))
}
// Get files of model1
// Get files of config1
var files []File
assert.NoError(t, db.Model(&model1).Related(&files, "Files").Error)
assert.NoError(t, db.Model(&config1).Related(&files, "Files").Error)
if len(files) != 2 {
assert.Fail(t, "SimulationModel Associations",
assert.Fail(t, "ComponentConfiguration Associations",
"Expected to have %v Files. Has %v.", 2, len(files))
}
}
......
......@@ -65,35 +65,35 @@ type Scenario struct {
StartParameters postgres.Jsonb `json:"startParameters"`
// Users that have access to the scenario
Users []*User `json:"-" gorm:"many2many:user_scenarios;"`
// SimulationModels that belong to the scenario
SimulationModels []SimulationModel `json:"-" gorm:"foreignkey:ScenarioID" `
// ComponentConfigurations that belong to the scenario
ComponentConfigurations []ComponentConfiguration `json:"-" gorm:"foreignkey:ScenarioID" `
// Dashboards that belong to the Scenario
Dashboards []Dashboard `json:"-" gorm:"foreignkey:ScenarioID" `
}
// SimulationModel data model
type SimulationModel struct {
// ComponentConfiguration data model
type ComponentConfiguration struct {
Model
// Name of simulation model
// Name of Component Configuration
Name string `json:"name" gorm:"not null"`
// Number of output signals
OutputLength int `json:"outputLength" gorm:"default:0"`
// Number of input signals
InputLength int `json:"inputLength" gorm:"default:0"`
// Start parameters of simulation model as JSON
// Start parameters of Component Configuration as JSON
StartParameters postgres.Jsonb `json:"startParameters"`
// ID of Scenario to which simulation model belongs
// ID of Scenario to which Component Configuration belongs
ScenarioID uint `json:"scenarioID"`
// ID of simulator associated with simulation model
SimulatorID uint `json:"simulatorID"`
// Mapping of output signals of the simulation model, order of signals is important
OutputMapping []Signal `json:"-" gorm:"foreignkey:SimulationModelID"`
// Mapping of input signals of the simulation model, order of signals is important
InputMapping []Signal `json:"-" gorm:"foreignkey:SimulationModelID"`
// Files of simulation model (can be CIM and other simulation model file formats)
Files []File `json:"-" gorm:"foreignkey:SimulationModelID"`
// Currently selected simulation model FileID
SelectedModelFileID uint `json:"selectedModelFileID" gorm:"default:0"`
// ID of IC associated with Component Configuration
ICID uint `json:"icID"`
// Mapping of output signals of the ComponentConfiguration, order of signals is important
OutputMapping []Signal `json:"-" gorm:"foreignkey:ConfigID"`
// Mapping of input signals of the Component Configuration, order of signals is important
InputMapping []Signal `json:"-" gorm:"foreignkey:ConfigID"`
// Files of Component Configuration (can be CIM and other ComponentConfiguration file formats)
Files []File `json:"-" gorm:"foreignkey:ConfigID"`
// Currently selected FileID
SelectedFileID uint `json:"selectedFileID" gorm:"default:0"`
}
// Signal data model
......@@ -107,31 +107,31 @@ type Signal struct {
Index uint `json:"index"`
// Direction of the signal (in or out)
Direction string `json:"direction"`
// ID of simulation model
SimulationModelID uint `json:"simulationModelID"`
// ID of Component Configuration
ConfigID uint `json:"configID"`
}
// Simulator data model
type Simulator struct {
// InfrastructureComponent data model
type InfrastructureComponent struct {
Model
// UUID of the simulator
// UUID of the IC
UUID string `json:"uuid" gorm:"not null"`
// Host if the simulator
// Host if the IC
Host string `json:"host" gorm:"default:''"`
// Model type supported by the simulator
// Model type supported by the IC
Modeltype string `json:"modelType" gorm:"default:''"`
// Uptime of the simulator
// Uptime of the IC
Uptime int `json:"uptime" gorm:"default:0"`
// State of the simulator
// State of the IC
State string `json:"state" gorm:"default:''"`
// Time of last state update
StateUpdateAt string `json:"stateUpdateAt" gorm:"default:''"`
// Properties of simulator as JSON string
// Properties of IC as JSON string
Properties postgres.Jsonb `json:"properties"`
// Raw properties of simulator as JSON string
// Raw properties of IC as JSON string
RawProperties postgres.Jsonb `json:"rawProperties"`
// SimulationModels in which the simulator is used
SimulationModels []SimulationModel `json:"-" gorm:"foreignkey:SimulatorID"`
// ComponentConfigurations in which the IC is used
ComponentConfigurations []ComponentConfiguration `json:"-" gorm:"foreignkey:ICID"`
}
// Dashboard data model
......@@ -195,8 +195,8 @@ type File struct {
ImageWidth uint `json:"imageWidth"`
// Last modification time of file
Date string `json:"date"`
// ID of model to which file belongs
SimulationModelID uint `json:"simulationModelID"`
// ID of Component Configuration to which file belongs
ConfigID uint `json:"configID"`
// ID of widget to which file belongs
WidgetID uint `json:"widgetID"`
// File itself
......
......@@ -38,11 +38,11 @@ type ModelName string
const ModelUser = ModelName("user")
const ModelUsers = ModelName("users")
const ModelScenario = ModelName("scenario")
const ModelSimulator = ModelName("simulator")
const ModelSimulatorAction = ModelName("simulatoraction")
const ModelInfrastructureComponent = ModelName("ic")
const ModelInfrastructureComponentAction = ModelName("icaction")
const ModelDashboard = ModelName("dashboard")
const ModelWidget = ModelName("widget")
const ModelSimulationModel = ModelName("simulationmodel")
const ModelComponentConfiguration = ModelName("component-configuration")
const ModelSignal = ModelName("signal")
const ModelFile = ModelName("file")
......@@ -73,40 +73,40 @@ var none = Permission{Create: false, Read: false, Update: false, Delete: false}
// allowed to do a certain action on a given model based on his role
var Roles = RoleActions{
"Admin": {
ModelUser: crud,
ModelUsers: crud,
ModelScenario: crud,
ModelSimulationModel: crud,
ModelSimulator: crud,
ModelSimulatorAction: crud,
ModelWidget: crud,
ModelDashboard: crud,
ModelSignal: crud,
ModelFile: crud,
ModelUser: crud,
ModelUsers: crud,
ModelScenario: crud,
ModelComponentConfiguration: crud,
ModelInfrastructureComponent: crud,
ModelInfrastructureComponentAction: crud,
ModelWidget: crud,
ModelDashboard: crud,
ModelSignal: crud,
ModelFile: crud,
},
"User": {
ModelUser: _ru_,
ModelUsers: none,
ModelScenario: crud,
ModelSimulationModel: crud,
ModelSimulator: _r__,
ModelSimulatorAction: _ru_,
ModelWidget: crud,
ModelDashboard: crud,
ModelSignal: crud,
ModelFile: crud,
ModelUser: _ru_,
ModelUsers: none,
ModelScenario: crud,
ModelComponentConfiguration: crud,
ModelInfrastructureComponent: _r__,
ModelInfrastructureComponentAction: _ru_,
ModelWidget: crud,
ModelDashboard: crud,
ModelSignal: crud,
ModelFile: crud,
},