Add feature for duplicating scenario on external login

This commit is contained in:
Sonja Happ 2021-09-17 20:32:36 +02:00 committed by Iris Marie Köster
parent 8584b4ac22
commit 7b30b46b27
12 changed files with 696 additions and 279 deletions

View file

@ -38,7 +38,12 @@ import (
// Global configuration
var GlobalConfig *config.Config = nil
var ScenarioGroupMap = map[string][]int{}
type GroupedScenario struct {
Scenario int `yaml:"scenario"`
Duplicate bool `default:"false" yaml:"duplicate"`
}
var ScenarioGroupMap = map[string][]GroupedScenario{}
func InitConfig() error {
if GlobalConfig != nil {
@ -182,35 +187,45 @@ func InitConfig() error {
return nil
}
func remove(arr []GroupedScenario, index int) []GroupedScenario {
arr[index] = arr[len(arr)-1]
return arr[:len(arr)-1]
}
func ReadGroupsFile(path string) error {
_, err := os.Stat(path)
if err == nil {
yamlFile, err := os.Open(path)
if err != nil {
return fmt.Errorf("error opening yaml file for groups: %v", err)
}
log.Println("Successfully opened yaml groups file", path)
defer yamlFile.Close()
byteValue, _ := ioutil.ReadAll(yamlFile)
err = yaml.Unmarshal(byteValue, &ScenarioGroupMap)
if err != nil {
return fmt.Errorf("error unmarshalling yaml into ScenarioGroupMap: %v", err)
}
log.Println("ScenarioGroupMap", ScenarioGroupMap)
return nil
} else if os.IsNotExist(err) {
log.Println("File does not exist, no goups/scenarios mapping created:", path)
return nil
} else {
log.Println("Something is wrong with this file path:", path)
return nil
if err != nil {
return err
}
yamlFile, err := os.Open(path)
if err != nil {
return fmt.Errorf("error opening yaml file for groups: %v", err)
}
log.Println("Successfully opened yaml groups file", path)
defer yamlFile.Close()
byteValue, _ := ioutil.ReadAll(yamlFile)
err = yaml.Unmarshal(byteValue, &ScenarioGroupMap)
if err != nil {
return fmt.Errorf("error unmarshalling yaml into ScenarioGroupMap: %v", err)
}
for _, group := range ScenarioGroupMap {
for i, scenario := range group {
// remove invalid values that might have been introduced by typos
// (Unmarshal sets default values when it doesn't find a field)
if scenario.Scenario == 0 {
log.Println("Removing entry from ScenarioGroupMap, check for typos in the yaml!")
remove(group, i)
}
}
}
log.Println("ScenarioGroupMap", ScenarioGroupMap)
return nil
}

View file

@ -1,12 +1,16 @@
moodle-l2pmanager@13306:
- 1
- 2
- 3
# Warning: this file is used for testing - please consider when making changes
moodle-l2pmanager@13306:
- scenario: 1
- scenario: 2
- scenario: 3
testGroup1:
- 4
- 5
- 6
- scenario: 1
duplicate: true
- scenario: 2
duplicate: false
- scenario: 3
testGroup2:
- 7
- 8
- 9
- scenario: 1
- scenario: 4
duplicate: true
- scenario: 2

265
helper/amqp.go Normal file
View file

@ -0,0 +1,265 @@
/** helper package, AMQP client.
*
* @author Sonja Happ <sonja.happ@eonerc.rwth-aachen.de>
* @copyright 2014-2021, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLASweb-backend-go
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
package helper
import (
"encoding/json"
"fmt"
"log"
"time"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"github.com/google/uuid"
"github.com/streadway/amqp"
)
type AMQPclient struct {
connection *amqp.Connection
sendCh *amqp.Channel
recvCh *amqp.Channel
}
type Action struct {
Act string `json:"action"`
When int64 `json:"when"`
Parameters json.RawMessage `json:"parameters,omitempty"`
Model json.RawMessage `json:"model,omitempty"`
Results json.RawMessage `json:"results,omitempty"`
}
var client AMQPclient
const VILLAS_EXCHANGE = "villas"
type callback func(amqp.Delivery) error
func ConnectAMQP(uri string, cb callback) error {
var err error
// connect to broker
client.connection, err = amqp.Dial(uri)
if err != nil {
return fmt.Errorf("AMQP: failed to connect to RabbitMQ broker %v, error: %v", uri, err)
}
// create sendCh
client.sendCh, err = client.connection.Channel()
if err != nil {
return fmt.Errorf("AMQP: failed to open a sendCh, error: %v", err)
}
// declare exchange
err = client.sendCh.ExchangeDeclare(VILLAS_EXCHANGE,
"headers",
true,
false,
false,
false,
nil)
if err != nil {
return fmt.Errorf("AMQP: failed to declare the exchange, error: %v", err)
}
// add a queue for the ICs
ICQueue, err := client.sendCh.QueueDeclare("infrastructure_components",
true,
false,
false,
false,
nil)
if err != nil {
return fmt.Errorf("AMQP: failed to declare the queue, error: %v", err)
}
err = client.sendCh.QueueBind(ICQueue.Name, "", VILLAS_EXCHANGE, false, nil)
if err != nil {
return fmt.Errorf("AMQP: failed to bind the queue, error: %v", err)
}
// create receive channel
client.recvCh, err = client.connection.Channel()
if err != nil {
return fmt.Errorf("AMQP: failed to open a recvCh, error: %v", err)
}
// start deliveries
messages, err := client.recvCh.Consume(ICQueue.Name,
"",
true,
false,
false,
false,
nil)
if err != nil {
return fmt.Errorf("AMQP: failed to start deliveries: %v", err)
}
// consume deliveries
go func() {
for {
for message := range messages {
err = cb(message)
if err != nil {
log.Println("AMQP: Error processing message: ", err.Error())
}
}
}
}()
log.Printf(" AMQP: Waiting for messages... ")
return nil
}
func SendActionAMQP(action Action, destinationUUID string) error {
payload, err := json.Marshal(action)
if err != nil {
return err
}
msg := amqp.Publishing{
DeliveryMode: 2,
Timestamp: time.Now(),
ContentType: "application/json",
ContentEncoding: "utf-8",
Priority: 0,
Body: payload,
}
// set message headers
msg.Headers = make(map[string]interface{}) // empty map
msg.Headers["uuid"] = destinationUUID
err = CheckConnection()
if err != nil {
return err
}
//log.Println("AMQP: Sending message", string(msg.Body))
err = client.sendCh.Publish(VILLAS_EXCHANGE,
"",
false,
false,
msg)
return PublishAMQP(msg)
}
func PublishAMQP(msg amqp.Publishing) error {
err := client.sendCh.Publish(VILLAS_EXCHANGE,
"",
false,
false,
msg)
return err
}
func SendPing(uuid string) error {
var ping Action
ping.Act = "ping"
payload, err := json.Marshal(ping)
if err != nil {
return err
}
msg := amqp.Publishing{
DeliveryMode: 2,
Timestamp: time.Now(),
ContentType: "application/json",
ContentEncoding: "utf-8",
Priority: 0,
Body: payload,
}
// set message headers
msg.Headers = make(map[string]interface{}) // empty map
msg.Headers["uuid"] = uuid // leave uuid empty if ping should go to all ICs
err = CheckConnection()
if err != nil {
return err
}
err = client.sendCh.Publish(VILLAS_EXCHANGE,
"",
false,
false,
msg)
return err
}
func CheckConnection() error {
if client.connection != nil {
if client.connection.IsClosed() {
return fmt.Errorf("connection to broker is closed")
}
} else {
return fmt.Errorf("connection is nil")
}
return nil
}
func RequestICcreateAMQP(ic *database.InfrastructureComponent, managerUUID string) (string, error) {
newUUID := uuid.New().String()
// TODO: where to get the properties part from?
msg := `{"name": "` + ic.Name + `",` +
`"location": "` + ic.Location + `",` +
`"category": "` + ic.Category + `",` +
`"type": "` + ic.Type + `",` +
`"uuid": "` + newUUID + `",` +
`"realm": "de.rwth-aachen.eonerc.acs",` +
`"properties": {` +
`"job": {` +
`"apiVersion": "batch/v1",` +
`"kind": "Job",` +
`"metadata": {` +
`"name": "dpsim"` +
`},` +
`"spec": {` +
`"activeDeadlineSeconds": 3600,` +
`"backoffLimit": 1,` +
`"ttlSecondsAfterFinished": 3600,` +
`"template": {` +
`"spec": {` +
`"restartPolicy": "Never",` +
`"containers": [{` +
`"image": "dpsimrwth/slew-villas",` +
`"name": "slew-dpsim"` +
`}]}}}}}}`
log.Print(msg)
actionCreate := Action{
Act: "create",
When: time.Now().Unix(),
Parameters: json.RawMessage(msg),
}
err := SendActionAMQP(actionCreate, managerUUID)
return newUUID, err
}

View file

@ -62,3 +62,13 @@ func Find(slice []string, val string) (int, bool) {
}
return -1, false
}
// Returns whether slice contains the given element
func Contains(slice []uint, element uint) bool {
for _, elem := range slice {
if elem == element {
return true
}
}
return false
}

View file

@ -29,7 +29,6 @@ import (
"git.rwth-aachen.de/acs/public/villas/web-backend-go/configuration"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
infrastructure_component "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/infrastructure-component"
"github.com/gin-gonic/gin"
)
@ -69,7 +68,7 @@ func getHealth(c *gin.Context) {
}
if len(url) != 0 {
err = infrastructure_component.CheckConnection()
err = helper.CheckConnection()
if err != nil {
log.Println(err.Error())
c.JSON(http.StatusInternalServerError, gin.H{

View file

@ -80,7 +80,7 @@ func TestHealthz(t *testing.T) {
amqpURI := "amqp://" + user + ":" + pass + "@" + host
log.Println("AMQP URI is", amqpURI)
err = infrastructure_component.ConnectAMQP(amqpURI)
err = helper.ConnectAMQP(amqpURI, infrastructure_component.ProcessMessage)
assert.NoError(t, err)
// test healthz endpoint for connected DB and AMQP client

View file

@ -26,8 +26,8 @@ import (
"fmt"
"log"
"strings"
"time"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/jinzhu/gorm"
@ -35,22 +35,6 @@ import (
"github.com/streadway/amqp"
)
const VILLAS_EXCHANGE = "villas"
type AMQPclient struct {
connection *amqp.Connection
sendCh *amqp.Channel
recvCh *amqp.Channel
}
type Action struct {
Act string `json:"action"`
When int64 `json:"when"`
Parameters json.RawMessage `json:"parameters,omitempty"`
Model json.RawMessage `json:"model,omitempty"`
Results json.RawMessage `json:"results,omitempty"`
}
type ICStatus struct {
State string `json:"state"`
Version string `json:"version"`
@ -85,174 +69,11 @@ type ICUpdate struct {
Action string `json:"action"`
}
var client AMQPclient
func ConnectAMQP(uri string) error {
var err error
// connect to broker
client.connection, err = amqp.Dial(uri)
if err != nil {
return fmt.Errorf("AMQP: failed to connect to RabbitMQ broker %v, error: %v", uri, err)
}
// create sendCh
client.sendCh, err = client.connection.Channel()
if err != nil {
return fmt.Errorf("AMQP: failed to open a sendCh, error: %v", err)
}
// declare exchange
err = client.sendCh.ExchangeDeclare(VILLAS_EXCHANGE,
"headers",
true,
false,
false,
false,
nil)
if err != nil {
return fmt.Errorf("AMQP: failed to declare the exchange, error: %v", err)
}
// add a queue for the ICs
ICQueue, err := client.sendCh.QueueDeclare("infrastructure_components",
true,
false,
false,
false,
nil)
if err != nil {
return fmt.Errorf("AMQP: failed to declare the queue, error: %v", err)
}
err = client.sendCh.QueueBind(ICQueue.Name, "", VILLAS_EXCHANGE, false, nil)
if err != nil {
return fmt.Errorf("AMQP: failed to bind the queue, error: %v", err)
}
// create receive channel
client.recvCh, err = client.connection.Channel()
if err != nil {
return fmt.Errorf("AMQP: failed to open a recvCh, error: %v", err)
}
// start deliveries
messages, err := client.recvCh.Consume(ICQueue.Name,
"",
true,
false,
false,
false,
nil)
if err != nil {
return fmt.Errorf("AMQP: failed to start deliveries: %v", err)
}
// consume deliveries
go func() {
for {
for message := range messages {
err = processMessage(message)
if err != nil {
log.Println("AMQP: Error processing message: ", err.Error())
}
}
}
}()
log.Printf(" AMQP: Waiting for messages... ")
return nil
}
func sendActionAMQP(action Action, destinationUUID string) error {
payload, err := json.Marshal(action)
if err != nil {
return err
}
msg := amqp.Publishing{
DeliveryMode: 2,
Timestamp: time.Now(),
ContentType: "application/json",
ContentEncoding: "utf-8",
Priority: 0,
Body: payload,
}
// set message headers
msg.Headers = make(map[string]interface{}) // empty map
msg.Headers["uuid"] = destinationUUID
err = CheckConnection()
if err != nil {
return err
}
//log.Println("AMQP: Sending message", string(msg.Body))
err = client.sendCh.Publish(VILLAS_EXCHANGE,
"",
false,
false,
msg)
return err
}
func SendPing(uuid string) error {
var ping Action
ping.Act = "ping"
payload, err := json.Marshal(ping)
if err != nil {
return err
}
msg := amqp.Publishing{
DeliveryMode: 2,
Timestamp: time.Now(),
ContentType: "application/json",
ContentEncoding: "utf-8",
Priority: 0,
Body: payload,
}
// set message headers
msg.Headers = make(map[string]interface{}) // empty map
msg.Headers["uuid"] = uuid // leave uuid empty if ping should go to all ICs
err = CheckConnection()
if err != nil {
return err
}
err = client.sendCh.Publish(VILLAS_EXCHANGE,
"",
false,
false,
msg)
return err
}
func CheckConnection() error {
if client.connection != nil {
if client.connection.IsClosed() {
return fmt.Errorf("connection to broker is closed")
}
} else {
return fmt.Errorf("connection is nil")
}
return nil
}
func StartAMQP(AMQPurl string, api *gin.RouterGroup) error {
if AMQPurl != "" {
log.Println("Starting AMQP client")
err := ConnectAMQP(AMQPurl)
err := helper.ConnectAMQP(AMQPurl, ProcessMessage)
if err != nil {
return err
}
@ -266,7 +87,7 @@ func StartAMQP(AMQPurl string, api *gin.RouterGroup) error {
return nil
}
func processMessage(message amqp.Delivery) error {
func ProcessMessage(message amqp.Delivery) error {
var payload ICUpdate
err := json.Unmarshal(message.Body, &payload)
@ -358,7 +179,7 @@ func createExternalIC(payload ICUpdate, ICUUID string, body []byte) error {
log.Println("AMQP: Created IC with UUID ", newIC.UUID)
// send ping to get full status update of this IC
err = SendPing(ICUUID)
err = helper.SendPing(ICUUID)
return err
}

View file

@ -289,7 +289,7 @@ func sendActionToIC(c *gin.Context) {
return
}
var actions []Action
var actions []helper.Action
err := c.BindJSON(&actions)
if err != nil {
helper.BadRequestError(c, "Error binding form data to JSON: "+err.Error())
@ -300,7 +300,7 @@ func sendActionToIC(c *gin.Context) {
if (action.Act == "delete" || action.Act == "create") && s.Category != "manager" {
helper.BadRequestError(c, "cannot send a delete or create action to an IC of category "+s.Category)
}
err = sendActionAMQP(action, s.UUID)
err = helper.SendActionAMQP(action, s.UUID)
if err != nil {
helper.InternalServerError(c, "Unable to send actions to IC: "+err.Error())
return

View file

@ -134,7 +134,7 @@ func TestMain(m *testing.M) {
scenario.RegisterScenarioEndpoints(api.Group("/scenarios"))
// check AMQP connection
err = CheckConnection()
err = helper.CheckConnection()
if err.Error() != "connection is nil" {
return
}
@ -320,14 +320,10 @@ func TestUpdateICAsAdmin(t *testing.T) {
Headers: headers,
}
err = CheckConnection()
err = helper.CheckConnection()
assert.NoError(t, err)
err = client.sendCh.Publish(VILLAS_EXCHANGE,
"",
false,
false,
msg)
err = helper.PublishAMQP(msg)
assert.NoError(t, err)
// Wait until externally managed IC is created (happens async)
@ -377,6 +373,48 @@ func TestUpdateICAsUser(t *testing.T) {
}
func TestCreateICviaAMQP(t *testing.T) {
if os.Getenv("CI") != "" {
t.Skip("Skipping testing in CI environment")
}
database.DropTables()
database.MigrateModels()
assert.NoError(t, helper.AddTestUsers())
// authenticate as user
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// Count the number of all the ICs before sending Action
numberOfICs, err := helper.LengthOfResponse(router, token,
"/api/v2/ic", "GET", nil)
assert.NoError(t, err)
assert.Equal(t, 0, numberOfICs)
err = helper.CheckConnection()
assert.NoError(t, err)
var ic database.InfrastructureComponent
ic.Name = "kubernetes simulator dpsim (backend test)"
ic.Location = "iko backend dev"
ic.Category = "simulator"
ic.Type = "kubernetes"
// send create Action to kubernetes manager via AMQP broker
uuidManager := "444fb73e-7e74-11eb-8f63-f3a5b3ab82f6"
_, err = helper.RequestICcreateAMQP(&ic, uuidManager)
assert.NoError(t, err)
// Wait until externally managed IC is created (happens async)
time.Sleep(2 * time.Second)
// check whether an external IC was created
numberOfICs, err = helper.LengthOfResponse(router, token,
"/api/v2/ic", "GET", nil)
assert.NoError(t, err)
assert.Equal(t, 1, numberOfICs)
}
func TestDeleteICAsAdmin(t *testing.T) {
database.DropTables()
database.MigrateModels()
@ -444,14 +482,10 @@ func TestDeleteICAsAdmin(t *testing.T) {
Headers: headers,
}
err = CheckConnection()
err = helper.CheckConnection()
assert.NoError(t, err)
err = client.sendCh.Publish(VILLAS_EXCHANGE,
"",
false,
false,
msg)
err = helper.PublishAMQP(msg)
assert.NoError(t, err)
// Wait until externally managed IC is created (happens async)
@ -617,7 +651,7 @@ func TestSendActionToIC(t *testing.T) {
assert.NoError(t, err)
// create action to be sent to IC
action1 := Action{
action1 := helper.Action{
Act: "start",
When: time.Now().Unix(),
}
@ -630,7 +664,7 @@ func TestSendActionToIC(t *testing.T) {
paramsRaw, err := json.Marshal(&params)
action1.Parameters = paramsRaw
actions := [1]Action{action1}
actions := [1]helper.Action{action1}
// Send action to IC
code, resp, err = helper.TestEndpoint(router, token,
@ -676,14 +710,10 @@ func TestCreateUpdateViaAMQPRecv(t *testing.T) {
Headers: headers,
}
err = CheckConnection()
err = helper.CheckConnection()
assert.NoError(t, err)
err = client.sendCh.Publish(VILLAS_EXCHANGE,
"",
false,
false,
msg)
err = helper.PublishAMQP(msg)
assert.NoError(t, err)
time.Sleep(waitingTime * time.Second)
@ -723,11 +753,7 @@ func TestCreateUpdateViaAMQPRecv(t *testing.T) {
Headers: headersA,
}
err = client.sendCh.Publish(VILLAS_EXCHANGE,
"",
false,
false,
msg)
err = helper.PublishAMQP(msg)
assert.NoError(t, err)
time.Sleep(waitingTime * time.Second)
@ -753,11 +779,7 @@ func TestCreateUpdateViaAMQPRecv(t *testing.T) {
Headers: headersA,
}
err = client.sendCh.Publish(VILLAS_EXCHANGE,
"",
false,
false,
msg)
err = helper.PublishAMQP(msg)
assert.NoError(t, err)
time.Sleep(waitingTime * time.Second)
@ -811,13 +833,9 @@ func TestDeleteICViaAMQPRecv(t *testing.T) {
Headers: headers,
}
err = CheckConnection()
err = helper.CheckConnection()
assert.NoError(t, err)
err = client.sendCh.Publish(VILLAS_EXCHANGE,
"",
false,
false,
msg)
err = helper.PublishAMQP(msg)
assert.NoError(t, err)
time.Sleep(waitingTime * time.Second)
@ -885,11 +903,7 @@ func TestDeleteICViaAMQPRecv(t *testing.T) {
}
// attempt to delete IC (should not work immediately because IC is still associated with component config)
err = client.sendCh.Publish(VILLAS_EXCHANGE,
"",
false,
false,
msg)
err = helper.PublishAMQP(msg)
assert.NoError(t, err)
time.Sleep(waitingTime * time.Second)

View file

@ -220,6 +220,241 @@ func authenticateInternal(c *gin.Context) (User, error) {
return myUser, nil
}
func duplicateDashboards(originalSo *database.Scenario, duplicateSo *database.Scenario,
signalMap map[uint]uint, appendix string) error {
db := database.GetDB()
var dabs []database.Dashboard
err := db.Order("ID asc").Model(originalSo).Related(&dabs, "Dashboards").Error
if err != nil {
log.Printf("error getting dashboards for scenario %d", originalSo.ID)
}
for _, dab := range dabs {
var duplicateD database.Dashboard
duplicateD.Grid = dab.Grid
duplicateD.Name = dab.Name + appendix
duplicateD.ScenarioID = duplicateSo.ID
duplicateD.Height = dab.Height
err = db.Create(&duplicateD).Error
if err != nil {
log.Print("error creating duplicate dashboard")
continue
}
// add widgets to duplicated dashboards
var widgets []database.Widget
err = db.Order("ID asc").Model(&dab).Related(&widgets, "Widgets").Error
if err != nil {
log.Printf("error getting widgets for dashboard %d", dab.ID)
}
for _, widget := range widgets {
var duplicateW database.Widget
duplicateW.DashboardID = duplicateD.ID
duplicateW.CustomProperties = widget.CustomProperties
duplicateW.Height = widget.Height
duplicateW.Width = widget.Width
duplicateW.MinHeight = widget.MinHeight
duplicateW.MinWidth = widget.MinWidth
duplicateW.Name = widget.Name
duplicateW.Type = widget.Type
duplicateW.X = widget.X
duplicateW.Y = widget.Y
duplicateW.SignalIDs = []int64{}
for _, id := range widget.SignalIDs {
duplicateW.SignalIDs = append(duplicateW.SignalIDs, int64(signalMap[uint(id)]))
}
err = db.Create(&duplicateW).Error
if err != nil {
log.Print("error creating duplicate widget")
continue
}
// associate dashboard with simulation
err = db.Model(&duplicateD).Association("Widgets").Append(&duplicateW).Error
if err != nil {
log.Print("error associating duplicate widget and dashboard")
}
}
}
return nil
}
func duplicateComponentConfig(config *database.ComponentConfiguration,
duplicateSo *database.Scenario, icIds map[uint]string, appendix string, signalMap *map[uint]uint) error {
var configDpl database.ComponentConfiguration
configDpl.Name = config.Name
configDpl.StartParameters = config.StartParameters
configDpl.ScenarioID = duplicateSo.ID
configDpl.OutputLength = config.OutputLength
configDpl.InputLength = config.InputLength
db := database.GetDB()
if icIds[config.ICID] == "" {
configDpl.ICID = config.ICID
} else {
var duplicatedIC database.InfrastructureComponent
err := db.Find(&duplicatedIC, "UUID = ?", icIds[config.ICID]).Error
if err != nil {
log.Print(err)
return err
}
configDpl.ICID = duplicatedIC.ID
}
err := db.Create(&configDpl).Error
if err != nil {
log.Print(err)
return err
}
// get all signals corresponding to component config
var sigs []database.Signal
err = db.Order("ID asc").Model(&config).Related(&sigs, "OutputMapping").Error
smap := *signalMap
for _, signal := range sigs {
var sig database.Signal
sig.Direction = signal.Direction
sig.Index = signal.Index
sig.Name = signal.Name + appendix
sig.ScalingFactor = signal.ScalingFactor
sig.Unit = signal.Unit
sig.ConfigID = configDpl.ID
err = db.Create(&sig).Error
if err == nil {
smap[signal.ID] = sig.ID
}
}
return err
}
func duplicateScenario(so *database.Scenario, duplicateSo *database.Scenario, icIds map[uint]string, appendix string) error {
duplicateSo.Name = so.Name + appendix
duplicateSo.StartParameters.RawMessage = so.StartParameters.RawMessage
db := database.GetDB()
err := db.Create(&duplicateSo).Error
if err != nil {
log.Printf("Could not create duplicate of scenario %d", so.ID)
return err
}
log.Print("created duplicate scenario")
var configs []database.ComponentConfiguration
// map existing signal IDs to duplicated signal IDs for widget duplication
signalMap := make(map[uint]uint)
err = db.Order("ID asc").Model(so).Related(&configs, "ComponentConfigurations").Error
if err == nil {
for _, config := range configs {
err = duplicateComponentConfig(&config, duplicateSo, icIds, appendix, &signalMap)
if err != nil {
return err
}
}
}
err = duplicateDashboards(so, duplicateSo, signalMap, appendix)
return err
}
func DuplicateScenarioForUser(so *database.Scenario, user *database.User) {
go func() {
// get all component configs of the scenario
db := database.GetDB()
var configs []database.ComponentConfiguration
err := db.Order("ID asc").Model(so).Related(&configs, "ComponentConfigurations").Error
if err != nil {
log.Printf("Warning: scenario to duplicate (id=%d) has no component configurations", so.ID)
}
// iterate over component configs to check for ICs to duplicate
duplicatedICuuids := make(map[uint]string) // key: icID; value: UUID of duplicate
var externalUUIDs []string // external ICs to wait for
for _, config := range configs {
icID := config.ICID
if duplicatedICuuids[icID] != "" { // this IC was already added
continue
}
var ic database.InfrastructureComponent
err = db.Find(&ic, icID).Error
if err != nil {
log.Printf("Cannot find IC with id %d in DB, will not duplicate for User %s", icID, user.Username)
continue
}
if ic.Category == "simulator" && ic.Type == "kubernetes" {
duplicateUUID, err := helper.RequestICcreateAMQP(&ic, ic.Manager)
duplicatedICuuids[ic.ID] = duplicateUUID
if err != nil { // TODO: should this function call be interrupted here?
log.Printf("Duplication of IC (id=%d) unsuccessful", icID)
continue
}
externalUUIDs = append(externalUUIDs, duplicateUUID)
} else { // use existing IC
duplicatedICuuids[ic.ID] = ""
err = nil
}
}
// copy scenario after all new external ICs are in DB
icsToWaitFor := len(externalUUIDs)
var duplicatedScenario database.Scenario
var timeout = 5 // seconds
for i := 0; i < timeout; i++ {
log.Printf("i = %d", i)
if icsToWaitFor == 0 {
appendix := fmt.Sprintf("--%s-%d-%d", user.Username, user.ID, so.ID)
duplicateScenario(so, &duplicatedScenario, duplicatedICuuids, appendix)
// associate user to new scenario
err = db.Model(&duplicatedScenario).Association("Users").Append(user).Error
if err != nil {
log.Printf("Could not associate User %s to scenario %d", user.Username, duplicatedScenario.ID)
}
log.Print("associated user to duplicated scenario")
return
} else {
time.Sleep(1 * time.Second)
}
// check for new ICs with previously created UUIDs
for _, uuid := range externalUUIDs {
if uuid == "" {
continue
}
log.Printf("looking for IC with UUID %s", uuid)
var duplicatedIC database.InfrastructureComponent
err = db.Find(&duplicatedIC, "UUID = ?", uuid).Error
// TODO: check if not found or other error
if err != nil {
log.Print(err)
} else {
icsToWaitFor--
uuid = ""
}
}
}
}()
}
func isAlreadyDuplicated(duplicatedName string) bool {
db := database.GetDB()
var scenarios []database.Scenario
db.Find(&scenarios, "name = ?", duplicatedName)
if len(scenarios) > 0 {
return true
}
return false
}
func authenticateExternal(c *gin.Context) (User, error) {
var myUser User
username := c.Request.Header.Get("X-Forwarded-User")
@ -237,7 +472,11 @@ func authenticateExternal(c *gin.Context) (User, error) {
groups := strings.Split(c.Request.Header.Get("X-Forwarded-Groups"), ",")
// preferred_username := c.Request.Header.Get("X-Forwarded-Preferred-Username")
if err := myUser.ByUsername(username); err != nil {
// check if user already exists
err := myUser.ByUsername(username)
if err != nil {
// this is the first login, create new user
role := "User"
if _, found := helper.Find(groups, "admin"); found {
role = "Admin"
@ -255,22 +494,33 @@ func authenticateExternal(c *gin.Context) (User, error) {
// Add users to scenarios based on static map
db := database.GetDB()
for _, group := range groups {
if soIDs, ok := configuration.ScenarioGroupMap[group]; ok {
for _, soID := range soIDs {
if groupedArr, ok := configuration.ScenarioGroupMap[group]; ok {
for _, groupedScenario := range groupedArr {
var so database.Scenario
err := db.Find(&so, soID).Error
err := db.Find(&so, groupedScenario.Scenario).Error
if err != nil {
log.Printf("Failed to add user %s (id=%d) to scenario %s (id=%d): %s\n", myUser.Username, myUser.ID, so.Name, so.ID, err)
log.Printf(`Cannot find scenario %s (id=%d) for adding/duplication.
Affecting user %s (id=%d): %s\n`, so.Name, so.ID, myUser.Username, myUser.ID, err)
continue
}
err = db.Model(&so).Association("Users").Append(&(myUser.User)).Error
if err != nil {
log.Printf("Failed to add user %s (id=%d) to scenario %s (id=%d): %s\n", myUser.Username, myUser.ID, so.Name, so.ID, err)
continue
duplicateName := fmt.Sprintf("%s--%s-%d-%d", so.Name, myUser.Username, myUser.ID, so.ID)
alreadyDuplicated := isAlreadyDuplicated(duplicateName)
if alreadyDuplicated {
log.Printf("Scenario %d already duplicated for user %s", so.ID, myUser.Username)
return myUser, nil
}
log.Printf("Added user %s (id=%d) to scenario %s (id=%d)", myUser.Username, myUser.ID, so.Name, so.ID)
if groupedScenario.Duplicate {
DuplicateScenarioForUser(&so, &myUser.User)
} else {
err = db.Model(&so).Association("Users").Append(&(myUser.User)).Error
if err != nil {
log.Printf("Failed to add user %s (id=%d) to scenario %s (id=%d): %s\n", myUser.Username, myUser.ID, so.Name, so.ID, err)
continue
}
log.Printf("Added user %s (id=%d) to scenario %s (id=%d)", myUser.Username, myUser.ID, so.Name, so.ID)
}
}
}
}

View file

@ -28,6 +28,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
@ -130,6 +131,43 @@ func TestAuthenticate(t *testing.T) {
}
func TestUserGroups(t *testing.T) {
// Create new user
// (user, email and groups are read from request headers in real case)
var myUser User
username := "Fridolin"
email := "Fridolin@rwth-aachen.de"
role := "User"
userGroups := strings.Split("testGroup1,testGroup2", ",")
err := myUser.ByUsername(username)
assert.Error(t, err)
myUser, err = NewUser(username, "", email, role, true)
assert.NoError(t, err)
// Read groups file
err = configuration.ReadGroupsFile("notexisting.yaml")
assert.Error(t, err)
err = configuration.ReadGroupsFile("../../configuration/groups.yaml")
assert.NoError(t, err)
// Check whether duplicate flag is saved correctly in configuration
for _, group := range userGroups {
if gsarray, ok := configuration.ScenarioGroupMap[group]; ok {
for _, groupedScenario := range gsarray {
if group == "testGroup1" && groupedScenario.Scenario == 1 {
assert.Equal(t, true, groupedScenario.Duplicate)
} else if group == "testGroup2" && groupedScenario.Scenario == 4 {
assert.Equal(t, true, groupedScenario.Duplicate)
} else {
assert.Equal(t, false, groupedScenario.Duplicate)
}
}
}
}
}
func TestAuthenticateQueryToken(t *testing.T) {
database.DropTables()

View file

@ -28,6 +28,7 @@ import (
"git.rwth-aachen.de/acs/public/villas/web-backend-go/configuration"
"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"
infrastructure_component "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/infrastructure-component"
"github.com/gin-gonic/gin"
@ -128,7 +129,7 @@ func main() {
}
// send Ping to all externally managed ICs
err = infrastructure_component.SendPing("")
err = helper.SendPing("")
if err != nil {
log.Println("error sending ping action via AMQP: ", err)
}