fix more issues with circular dependencies and tests, fix bug in permissions

This commit is contained in:
Sonja Happ 2021-10-19 11:04:39 +02:00
parent 302b1cc470
commit e7679a79b7
12 changed files with 530 additions and 688 deletions

View file

@ -54,38 +54,33 @@ func CheckScenarioPermissions(c *gin.Context, operation CRUD, scenarioIDsource s
return false, so
}
hasAccess := false
u := User{}
err = db.Find(&u, userID.(uint)).Error
if err != nil {
hasAccess = false
helper.UnprocessableEntityError(c, "Access denied (user has no access or scenario is locked).")
return false, so
}
if u.Role == "Admin" {
hasAccess = true
return true, so
}
scenarioUser := User{}
err = db.Order("ID asc").Model(&so).Where("ID = ?", userID.(uint)).Related(&scenarioUser, "Users").Error
if err != nil {
hasAccess = false
}
if !scenarioUser.Active {
hasAccess = false
} else if so.IsLocked && operation != Read {
hasAccess = false
} else {
hasAccess = true
}
if hasAccess == false {
helper.UnprocessableEntityError(c, "Access denied (user has no access or scenario is locked).")
return false, so
}
return true, so
if !scenarioUser.Active {
helper.UnprocessableEntityError(c, "Access denied (user has no access or scenario is locked).")
return false, so
} else if so.IsLocked && operation != Read {
helper.UnprocessableEntityError(c, "Access denied (user has no access or scenario is locked).")
return false, so
} else {
return true, so
}
}

View file

@ -23,7 +23,6 @@ package component_configuration
import (
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/signal"
"log"
)
@ -185,53 +184,3 @@ func (m *ComponentConfiguration) delete() error {
return nil
}
func (m *ComponentConfiguration) Duplicate(scenarioID uint, icIds map[uint]string, signalMap *map[uint]uint) error {
db := database.GetDB()
var dup ComponentConfiguration
dup.Name = m.Name
dup.StartParameters = m.StartParameters
dup.ScenarioID = scenarioID
if icIds[m.ICID] == "" {
dup.ICID = m.ICID
} else {
var duplicatedIC database.InfrastructureComponent
err := db.Find(&duplicatedIC, "UUID = ?", icIds[m.ICID]).Error
if err != nil {
log.Print(err)
return err
}
dup.ICID = duplicatedIC.ID
}
// save duplicate to DB and create associations with IC and scenario
err := dup.addToScenario()
if err != nil {
return err
}
// duplication of signals
var sigs []signal.Signal
err = db.Order("ID asc").Model(&m).Related(&sigs, "OutputMapping").Error
smap := *signalMap
for _, s := range sigs {
var sigDup signal.Signal
sigDup.Direction = s.Direction
sigDup.Index = s.Index
sigDup.Name = s.Name // + ` ` + userName
sigDup.ScalingFactor = s.ScalingFactor
sigDup.Unit = s.Unit
sigDup.ConfigID = dup.ID
err = sigDup.AddToConfig()
if err != nil {
return err
}
smap[s.ID] = sigDup.ID
}
return nil
}

View file

@ -23,8 +23,6 @@ package dashboard
import (
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/widget"
"log"
)
type Dashboard struct {
@ -108,35 +106,3 @@ func (d *Dashboard) delete() error {
return err
}
func (d *Dashboard) Duplicate(scenarioID uint, signalMap map[uint]uint) error {
var duplicateD Dashboard
duplicateD.Grid = d.Grid
duplicateD.Name = d.Name
duplicateD.ScenarioID = scenarioID
duplicateD.Height = d.Height
err := duplicateD.addToScenario()
if err != nil {
return err
}
// add widgets to duplicated dashboard
var widgets []widget.Widget
db := database.GetDB()
err = db.Order("ID asc").Model(d).Related(&widgets, "Widgets").Error
if err != nil {
log.Printf("Error getting widgets for dashboard %d: %s", d.ID, err)
}
for _, w := range widgets {
err = w.Duplicate(duplicateD.ID, signalMap)
if err != nil {
log.Printf("error creating duplicate for widget %d: %s", w.ID, err)
continue
}
}
return nil
}

View file

@ -247,38 +247,3 @@ func (f *File) Delete() error {
return err
}
func (f *File) Duplicate(scenarioID uint) error {
var dup File
dup.Name = f.Name
dup.Key = f.Key
dup.Type = f.Type
dup.Size = f.Size
dup.Date = f.Date
dup.ScenarioID = scenarioID
dup.FileData = f.FileData
dup.ImageHeight = f.ImageHeight
dup.ImageWidth = f.ImageWidth
// file duplicate will point to the same data blob in the DB (SQL or postgres)
// Add duplicate File object with parameters to DB
err := dup.save()
if err != nil {
return err
}
// Create association of duplicate file to scenario ID of duplicate file
db := database.GetDB()
var so database.Scenario
err = db.Find(&so, scenarioID).Error
if err != nil {
return err
}
err = db.Model(&so).Association("Files").Append(&dup).Error
return err
}

View file

@ -30,7 +30,6 @@ import (
"github.com/streadway/amqp"
"log"
"strings"
"time"
)
type Action struct {
@ -75,50 +74,6 @@ type ICUpdate struct {
Action string `json:"action"`
}
type Container struct {
Name string `json:"name"`
Image string `json:"image"`
}
type TemplateSpec struct {
Containers []Container `json:"containers"`
}
type JobTemplate struct {
Spec TemplateSpec `json:"spec"`
}
type JobSpec struct {
Active string `json:"activeDeadlineSeconds"`
Template JobTemplate `json:"template"`
}
type JobMetaData struct {
JobName string `json:"name"`
}
type KubernetesJob struct {
Spec JobSpec `json:"spec"`
MetaData JobMetaData `json:"metadata"`
}
type ICPropertiesToCopy struct {
Job KubernetesJob `json:"job"`
UUID string `json:"uuid"`
Name string `json:"name"`
Description string `json:"description"`
Location string `json:"location"`
Owner string `json:"owner"`
Category string `json:"category"`
Type string `json:"type"`
}
type ICUpdateToCopy struct {
Properties ICPropertiesToCopy `json:"properties"`
Status json.RawMessage `json:"status"`
Schema json.RawMessage `json:"schema"`
}
func ProcessMessage(message amqp.Delivery) {
var payload ICUpdate
@ -316,42 +271,3 @@ func sendActionAMQP(action Action, destinationUUID string) error {
err = session.Send(payload, destinationUUID)
return err
}
func (s *InfrastructureComponent) RequestICcreateAMQPsimpleManager(userName string) (string, error) {
//WARNING: this function only works with the kubernetes-simple manager of VILLAScontroller
if s.Category != "simulator" || s.Type == "kubernetes" {
return "", nil
}
newUUID := uuid.New().String()
log.Printf("New IC UUID: %s", newUUID)
var lastUpdate ICUpdateToCopy
log.Println(s.StatusUpdateRaw.RawMessage)
err := json.Unmarshal(s.StatusUpdateRaw.RawMessage, &lastUpdate)
if err != nil {
return newUUID, err
}
msg := `{"name": "` + lastUpdate.Properties.Name + ` ` + userName + `",` +
`"location": "` + lastUpdate.Properties.Location + `",` +
`"category": "` + lastUpdate.Properties.Category + `",` +
`"type": "` + lastUpdate.Properties.Type + `",` +
`"uuid": "` + newUUID + `",` +
`"jobname": "` + lastUpdate.Properties.Job.MetaData.JobName + `-` + userName + `",` +
`"activeDeadlineSeconds": "` + lastUpdate.Properties.Job.Spec.Active + `",` +
`"containername": "` + lastUpdate.Properties.Job.Spec.Template.Spec.Containers[0].Name + `-` + userName + `",` +
`"image": "` + lastUpdate.Properties.Job.Spec.Template.Spec.Containers[0].Image + `",` +
`"uuid": "` + newUUID + `"}`
actionCreate := Action{
Act: "create",
When: time.Now().Unix(),
Parameters: json.RawMessage(msg),
}
err = sendActionAMQP(actionCreate, s.Manager)
return newUUID, err
}

View file

@ -24,13 +24,7 @@ package scenario
import (
"fmt"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
component_configuration "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/component-configuration"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/dashboard"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/file"
infrastructure_component "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/infrastructure-component"
"github.com/jinzhu/gorm"
"log"
"time"
)
type Scenario struct {
@ -156,161 +150,3 @@ func (s *Scenario) delete() error {
return nil
}
func (s *Scenario) DuplicateScenarioForUser(user *database.User) <-chan error {
errs := make(chan error, 1)
go func() {
// get all component configs of the scenario
db := database.GetDB()
var configs []database.ComponentConfiguration
err := db.Order("ID asc").Model(s).Related(&configs, "ComponentConfigurations").Error
if err != nil {
log.Printf("Warning: scenario to duplicate (id=%d) has no component configurations", s.ID)
}
// iterate over component configs to check for ICs to duplicate
duplicatedICuuids := make(map[uint]string) // key: original 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 infrastructure_component.InfrastructureComponent
err = ic.ByID(icID)
if err != nil {
errs <- fmt.Errorf("Cannot find IC with id %d in DB, will not duplicate for User %s: %s", icID, user.Username, err)
continue
}
// create new kubernetes simulator OR use existing IC
if ic.Category == "simulator" && ic.Type == "kubernetes" {
duplicateUUID, err := ic.RequestICcreateAMQPsimpleManager(user.Username)
if err != nil {
errs <- fmt.Errorf("Duplication of IC (id=%d) unsuccessful, err: %s", icID, err)
continue
}
duplicatedICuuids[ic.ID] = duplicateUUID
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 = 20 // seconds
for i := 0; i < timeout; i++ {
// duplicate scenario after all duplicated ICs have been found in the DB
if icsToWaitFor == 0 {
err := s.duplicateScenario(duplicatedICuuids, user)
if err != nil {
errs <- fmt.Errorf("duplicate scenario %v fails with error %v", s.Name, err.Error())
}
close(errs)
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 duplicated IC with UUID %s", uuid)
var duplicatedIC database.InfrastructureComponent
err = db.Find(&duplicatedIC, "UUID = ?", uuid).Error
if err != nil {
errs <- fmt.Errorf("Error looking up duplicated IC: %s", err)
} else {
icsToWaitFor--
uuid = ""
}
}
}
errs <- fmt.Errorf("ALERT! Timed out while waiting for IC duplication, scenario not properly duplicated")
close(errs)
}()
return errs
}
func (s *Scenario) duplicateScenario(icIds map[uint]string, user *database.User) error {
db := database.GetDB()
var duplicateSo Scenario
duplicateSo.Name = s.Name + ` ` + user.Username
duplicateSo.StartParameters.RawMessage = s.StartParameters.RawMessage
err := duplicateSo.save()
if err != nil {
log.Printf("Could not create duplicate of scenario %d", s.ID)
return err
}
// associate user to new scenario
err = duplicateSo.addUser(user)
if err != nil {
log.Printf("Could not associate User %s to scenario %d", user.Username, duplicateSo.ID)
}
log.Println("Associated user to duplicated scenario")
// duplicate files
var files []file.File
err = db.Order("ID asc").Model(s).Related(&files, "Files").Error
if err != nil {
log.Printf("error getting files for scenario %d", s.ID)
}
for _, f := range files {
err = f.Duplicate(duplicateSo.ID)
if err != nil {
log.Print("error creating duplicate file %d: %s", f.ID, err)
continue
}
}
var configs []component_configuration.ComponentConfiguration
// map existing signal IDs to duplicated signal IDs for widget duplication
signalMap := make(map[uint]uint)
err = db.Order("ID asc").Model(s).Related(&configs, "ComponentConfigurations").Error
if err == nil {
for _, c := range configs {
err = c.Duplicate(duplicateSo.ID, icIds, &signalMap)
//err = duplicateComponentConfig(&c, duplicateSo, icIds, userName, &signalMap)
if err != nil {
log.Printf("Error duplicating component config %d: %s", c.ID, err)
continue
}
}
} else {
return err
}
var dabs []dashboard.Dashboard
err = db.Order("ID asc").Model(s).Related(&dabs, "Dashboards").Error
if err != nil {
log.Printf("Error getting dashboards for scenario %d: %s", s.ID, err)
}
for _, dab := range dabs {
err = dab.Duplicate(duplicateSo.ID, signalMap)
if err != nil {
log.Printf("Error duplicating dashboard %d: %s", dab.ID, err)
continue
}
}
return err
}

View file

@ -55,11 +55,13 @@ type UserRequest struct {
var newScenario1 = ScenarioRequest{
Name: "Scenario1",
StartParameters: postgres.Jsonb{json.RawMessage(`{"parameter1" : "testValue1A", "parameter2" : "testValue2A", "parameter3" : 42}`)},
IsLocked: false,
}
var newScenario2 = ScenarioRequest{
Name: "Scenario2",
StartParameters: postgres.Jsonb{json.RawMessage(`{"parameter1" : "testValue1B", "parameter2" : "testValue2B", "parameter3" : 55}`)},
IsLocked: false,
}
func TestMain(m *testing.M) {
@ -708,13 +710,3 @@ func TestRemoveUserFromScenario(t *testing.T) {
assert.Equalf(t, 404, code, "Response body: \n%v\n", resp)
}
func TestDuplicateScenarioForUser(t *testing.T) {
database.DropTables()
database.MigrateModels()
assert.NoError(t, database.AddTestUsers())
// TODO test duplicate scenario for user function here!!
}

View file

@ -23,7 +23,6 @@ package user
import (
"fmt"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/scenario"
"log"
"net/http"
"strings"
@ -32,11 +31,16 @@ 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/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
)
var session *helper.AMQPsession
func SetAMQPSession(s *helper.AMQPsession) {
session = s
}
type tokenClaims struct {
UserID uint `json:"id"`
Role string `json:"role"`
@ -263,7 +267,7 @@ func authenticateExternal(c *gin.Context) (User, error) {
for _, group := range groups {
if groupedArr, ok := configuration.ScenarioGroupMap[group]; ok {
for _, groupedScenario := range groupedArr {
var so scenario.Scenario
var so database.Scenario
err := db.Find(&so, groupedScenario.Scenario).Error
if err != nil {
log.Printf(`Cannot find scenario %s (id=%d) for adding/duplication.
@ -280,7 +284,7 @@ func authenticateExternal(c *gin.Context) (User, error) {
if groupedScenario.Duplicate {
if err := <-so.DuplicateScenarioForUser(&myUser.User); err != nil {
if err := <-duplicateScenarioForUser(so, &myUser.User); err != nil {
return User{}, err
}
@ -306,268 +310,3 @@ func isAlreadyDuplicated(duplicatedName string) bool {
return (len(scenarios) > 0)
}
func duplicateScenarioForUser(so *database.Scenario, user *database.User) <-chan error {
errs := make(chan error, 1)
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: original 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 infrastructure_component.InfrastructureComponent
err = ic.ByID(icID)
if err != nil {
log.Printf("Cannot find IC with id %d in DB, will not duplicate for User %s", icID, user.Username)
continue
}
// create new kubernetes simulator OR use existing IC
if ic.Category == "simulator" && ic.Type == "kubernetes" {
duplicateUUID, err := ic.RequestICcreateAMQPsimpleManager(user.Username)
if err != nil {
log.Printf("Duplication of IC (id=%d) unsuccessful, err: %s", icID, err)
continue
}
duplicatedICuuids[ic.ID] = duplicateUUID
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 = 20 // seconds
for i := 0; i < timeout; i++ {
// duplicate scenario after all duplicated ICs have been found in the DB
if icsToWaitFor == 0 {
err := duplicateScenario(so, &duplicatedScenario, duplicatedICuuids, user.Username)
if err != nil {
errs <- fmt.Errorf("duplicate scenario %v fails with error %v", so.Name, err.Error())
}
// 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.Println("Associated user to duplicated scenario")
close(errs)
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 duplicated IC with UUID %s", uuid)
var duplicatedIC database.InfrastructureComponent
err = db.Find(&duplicatedIC, "UUID = ?", uuid).Error
if err != nil {
log.Printf("Error looking up duplicated IC: %s", err)
} else {
icsToWaitFor--
uuid = ""
}
}
}
close(errs)
log.Printf("ALERT! Timed out while waiting for IC duplication, scenario not properly duplicated")
}()
return errs
}
func duplicateComponentConfig(config *database.ComponentConfiguration,
duplicateSo *database.Scenario, icIds map[uint]string, userName string, signalMap *map[uint]uint) error {
var configDpl database.ComponentConfiguration
configDpl.Name = config.Name
configDpl.StartParameters = config.StartParameters
configDpl.ScenarioID = duplicateSo.ID
configDpl.OutputMapping = config.OutputMapping
configDpl.InputMapping = config.InputMapping
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 + ` ` + userName
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, userName string) error {
duplicateSo.Name = so.Name + ` ` + userName
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
}
err = duplicateFiles(so, duplicateSo)
if err != nil {
return err
}
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, userName, &signalMap)
if err != nil {
return err
}
}
}
return duplicateDashboards(so, duplicateSo, signalMap, userName)
}
func duplicateFiles(originalSo *database.Scenario, duplicateSo *database.Scenario) error {
db := database.GetDB()
var files []database.File
err := db.Order("ID asc").Model(originalSo).Related(&files, "Files").Error
if err != nil {
log.Printf("error getting files for scenario %d", originalSo.ID)
}
for _, file := range files {
var duplicateF database.File
duplicateF.Name = file.Name
duplicateF.Key = file.Key
duplicateF.Type = file.Type
duplicateF.Size = file.Size
duplicateF.Date = file.Date
duplicateF.ScenarioID = duplicateSo.ID
duplicateF.FileData = file.FileData
duplicateF.ImageHeight = file.ImageHeight
duplicateF.ImageWidth = file.ImageWidth
err = db.Create(&duplicateF).Error
if err != nil {
log.Print("error creating duplicate file")
return err
}
}
return nil
}
func duplicateDashboards(originalSo *database.Scenario, duplicateSo *database.Scenario,
signalMap map[uint]uint, userName 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: %s", originalSo.ID, err)
}
for _, dab := range dabs {
var duplicateD database.Dashboard
duplicateD.Grid = dab.Grid
duplicateD.Name = dab.Name + ` ` + userName
duplicateD.ScenarioID = duplicateSo.ID
duplicateD.Height = dab.Height
err = db.Create(&duplicateD).Error
if err != nil {
log.Printf("Error creating duplicate dashboard '%s': %s", dab.Name, err)
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: %s", dab.ID, err)
}
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.Printf("Error associating duplicate widget and dashboard: %s", err)
return err
}
}
}
return nil
}

View file

@ -0,0 +1,495 @@
/** User package, authentication endpoint.
*
* @author Sonja Happ <sonja.happ@eonerc.rwth-aachen.de>
* @copyright 2014-2019, 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 user
import (
"encoding/json"
"fmt"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"github.com/google/uuid"
"log"
"time"
)
func duplicateScenarioForUser(s database.Scenario, user *database.User) <-chan error {
errs := make(chan error, 1)
go func() {
// get all component configs of the scenario
db := database.GetDB()
var configs []database.ComponentConfiguration
err := db.Order("ID asc").Model(s).Related(&configs, "ComponentConfigurations").Error
if err != nil {
log.Printf("Warning: scenario to duplicate (id=%d) has no component configurations", s.ID)
}
// iterate over component configs to check for ICs to duplicate
duplicatedICuuids := make(map[uint]string) // key: original 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 {
errs <- fmt.Errorf("Cannot find IC with id %d in DB, will not duplicate for User %s: %s", icID, user.Username, err)
continue
}
// create new kubernetes simulator OR use existing IC
if ic.Category == "simulator" && ic.Type == "kubernetes" {
duplicateUUID, err := duplicateIC(ic, user.Username)
if err != nil {
errs <- fmt.Errorf("Duplication of IC (id=%d) unsuccessful, err: %s", icID, err)
continue
}
duplicatedICuuids[ic.ID] = duplicateUUID
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 = 20 // seconds
for i := 0; i < timeout; i++ {
// duplicate scenario after all duplicated ICs have been found in the DB
if icsToWaitFor == 0 {
err := duplicateScenario(s, duplicatedICuuids, user)
if err != nil {
errs <- fmt.Errorf("duplicate scenario %v fails with error %v", s.Name, err.Error())
}
close(errs)
return
} else {
time.Sleep(1 * time.Second)
}
// check for new ICs with previously created UUIDs
for _, uuid_r := range externalUUIDs {
if uuid_r == "" {
continue
}
log.Printf("Looking for duplicated IC with UUID %s", uuid_r)
var duplicatedIC database.InfrastructureComponent
err = db.Find(&duplicatedIC, "UUID = ?", uuid_r).Error
if err != nil {
errs <- fmt.Errorf("Error looking up duplicated IC: %s", err)
} else {
icsToWaitFor--
uuid_r = ""
}
}
}
errs <- fmt.Errorf("ALERT! Timed out while waiting for IC duplication, scenario not properly duplicated")
close(errs)
}()
return errs
}
func duplicateScenario(s database.Scenario, icIds map[uint]string, user *database.User) error {
db := database.GetDB()
var duplicateSo database.Scenario
duplicateSo.Name = s.Name + ` ` + user.Username
duplicateSo.StartParameters.RawMessage = s.StartParameters.RawMessage
err := db.Create(&duplicateSo).Error
if err != nil {
log.Printf("Could not create duplicate of scenario %d", s.ID)
return err
}
// associate user to new scenario
err = db.Model(&duplicateSo).Association("Users").Append(user).Error
if err != nil {
log.Printf("Could not associate User %s to scenario %d", user.Username, duplicateSo.ID)
}
log.Println("Associated user to duplicated scenario")
// duplicate files
var files []database.File
err = db.Order("ID asc").Model(s).Related(&files, "Files").Error
if err != nil {
log.Printf("error getting files for scenario %d", s.ID)
}
for _, f := range files {
err = duplicateFile(f, duplicateSo.ID)
if err != nil {
log.Printf("error creating duplicate file %d: %s", f.ID, err)
continue
}
}
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(s).Related(&configs, "ComponentConfigurations").Error
if err == nil {
for _, c := range configs {
err = duplicateComponentConfig(c, duplicateSo.ID, icIds, &signalMap)
if err != nil {
log.Printf("Error duplicating component config %d: %s", c.ID, err)
continue
}
}
} else {
return err
}
var dabs []database.Dashboard
err = db.Order("ID asc").Model(s).Related(&dabs, "Dashboards").Error
if err != nil {
log.Printf("Error getting dashboards for scenario %d: %s", s.ID, err)
}
for _, dab := range dabs {
err = duplicateDashboard(dab, duplicateSo.ID, signalMap)
if err != nil {
log.Printf("Error duplicating dashboard %d: %s", dab.ID, err)
continue
}
}
return err
}
func duplicateFile(f database.File, scenarioID uint) error {
var dup database.File
dup.Name = f.Name
dup.Key = f.Key
dup.Type = f.Type
dup.Size = f.Size
dup.Date = f.Date
dup.ScenarioID = scenarioID
dup.FileData = f.FileData
dup.ImageHeight = f.ImageHeight
dup.ImageWidth = f.ImageWidth
// file duplicate will point to the same data blob in the DB (SQL or postgres)
// Add duplicate File object with parameters to DB
db := database.GetDB()
err := db.Create(&dup).Error
if err != nil {
return err
}
// Create association of duplicate file to scenario ID of duplicate file
var so database.Scenario
err = db.Find(&so, scenarioID).Error
if err != nil {
return err
}
err = db.Model(&so).Association("Files").Append(&dup).Error
return err
}
func duplicateComponentConfig(m database.ComponentConfiguration, scenarioID uint, icIds map[uint]string, signalMap *map[uint]uint) error {
db := database.GetDB()
var dup database.ComponentConfiguration
dup.Name = m.Name
dup.StartParameters = m.StartParameters
dup.ScenarioID = scenarioID
if icIds[m.ICID] == "" {
dup.ICID = m.ICID
} else {
var duplicatedIC database.InfrastructureComponent
err := db.Find(&duplicatedIC, "UUID = ?", icIds[m.ICID]).Error
if err != nil {
log.Print(err)
return err
}
dup.ICID = duplicatedIC.ID
}
// save duplicate to DB and create associations with IC and scenario
var so database.Scenario
err := db.Find(&so, m.ScenarioID).Error
if err != nil {
return err
}
// save component configuration to DB
err = db.Create(&dup).Error
if err != nil {
return err
}
// associate IC with component configuration
var ic database.InfrastructureComponent
err = db.Find(&ic, dup.ICID).Error
if err != nil {
return err
}
err = db.Model(&ic).Association("ComponentConfigurations").Append(&dup).Error
if err != nil {
return err
}
// associate component configuration with scenario
err = db.Model(&so).Association("ComponentConfigurations").Append(&dup).Error
if err != nil {
return err
}
// duplication of signals
var sigs []database.Signal
err = db.Order("ID asc").Model(&m).Related(&sigs, "OutputMapping").Error
smap := *signalMap
for _, s := range sigs {
var sigDup database.Signal
sigDup.Direction = s.Direction
sigDup.Index = s.Index
sigDup.Name = s.Name // + ` ` + userName
sigDup.ScalingFactor = s.ScalingFactor
sigDup.Unit = s.Unit
sigDup.ConfigID = dup.ID
// save signal to DB
err = db.Create(&sigDup).Error
if err != nil {
return err
}
// associate signal with component configuration in correct direction
if s.Direction == "in" {
err = db.Model(&dup).Association("InputMapping").Append(&s).Error
} else {
err = db.Model(&dup).Association("OutputMapping").Append(&s).Error
}
if err != nil {
return err
}
smap[s.ID] = sigDup.ID
}
return nil
}
func duplicateDashboard(d database.Dashboard, scenarioID uint, signalMap map[uint]uint) error {
var duplicateD database.Dashboard
duplicateD.Grid = d.Grid
duplicateD.Name = d.Name
duplicateD.ScenarioID = scenarioID
duplicateD.Height = d.Height
db := database.GetDB()
var so database.Scenario
err := db.Find(&so, duplicateD.ScenarioID).Error
if err != nil {
return err
}
// save dashboard to DB
err = db.Create(&duplicateD).Error
if err != nil {
return err
}
// associate dashboard with scenario
err = db.Model(&so).Association("Dashboards").Append(&duplicateD).Error
if err != nil {
return err
}
// add widgets to duplicated dashboard
var widgets []database.Widget
err = db.Order("ID asc").Model(d).Related(&widgets, "Widgets").Error
if err != nil {
log.Printf("Error getting widgets for dashboard %d: %s", d.ID, err)
}
for _, w := range widgets {
err = duplicateWidget(w, duplicateD.ID, signalMap)
if err != nil {
log.Printf("error creating duplicate for widget %d: %s", w.ID, err)
continue
}
}
return nil
}
func duplicateWidget(w database.Widget, dashboardID uint, signalMap map[uint]uint) error {
var duplicateW database.Widget
duplicateW.DashboardID = dashboardID
duplicateW.CustomProperties = w.CustomProperties
duplicateW.Height = w.Height
duplicateW.Width = w.Width
duplicateW.MinHeight = w.MinHeight
duplicateW.MinWidth = w.MinWidth
duplicateW.Name = w.Name
duplicateW.Type = w.Type
duplicateW.X = w.X
duplicateW.Y = w.Y
duplicateW.Z = w.Z
duplicateW.SignalIDs = []int64{}
for _, id := range w.SignalIDs {
duplicateW.SignalIDs = append(duplicateW.SignalIDs, int64(signalMap[uint(id)]))
}
db := database.GetDB()
var dab database.Dashboard
err := db.Find(&dab, duplicateW.DashboardID).Error
if err != nil {
return err
}
// save widget to DB
err = db.Create(&duplicateW).Error
if err != nil {
return err
}
// associate widget with dashboard
err = db.Model(&dab).Association("Widgets").Append(&duplicateW).Error
return err
}
func duplicateIC(ic database.InfrastructureComponent, userName string) (string, error) {
//WARNING: this function only works with the kubernetes-simple manager of VILLAScontroller
if ic.Category != "simulator" || ic.Type == "kubernetes" {
return "", nil
}
newUUID := uuid.New().String()
log.Printf("New IC UUID: %s", newUUID)
type Container struct {
Name string `json:"name"`
Image string `json:"image"`
}
type TemplateSpec struct {
Containers []Container `json:"containers"`
}
type JobTemplate struct {
Spec TemplateSpec `json:"spec"`
}
type JobSpec struct {
Active string `json:"activeDeadlineSeconds"`
Template JobTemplate `json:"template"`
}
type JobMetaData struct {
JobName string `json:"name"`
}
type KubernetesJob struct {
Spec JobSpec `json:"spec"`
MetaData JobMetaData `json:"metadata"`
}
type ICPropertiesToCopy struct {
Job KubernetesJob `json:"job"`
UUID string `json:"uuid"`
Name string `json:"name"`
Description string `json:"description"`
Location string `json:"location"`
Owner string `json:"owner"`
Category string `json:"category"`
Type string `json:"type"`
}
type ICUpdateToCopy struct {
Properties ICPropertiesToCopy `json:"properties"`
Status json.RawMessage `json:"status"`
Schema json.RawMessage `json:"schema"`
}
var lastUpdate ICUpdateToCopy
log.Println(ic.StatusUpdateRaw.RawMessage)
err := json.Unmarshal(ic.StatusUpdateRaw.RawMessage, &lastUpdate)
if err != nil {
return newUUID, err
}
msg := `{"name": "` + lastUpdate.Properties.Name + ` ` + userName + `",` +
`"location": "` + lastUpdate.Properties.Location + `",` +
`"category": "` + lastUpdate.Properties.Category + `",` +
`"type": "` + lastUpdate.Properties.Type + `",` +
`"uuid": "` + newUUID + `",` +
`"jobname": "` + lastUpdate.Properties.Job.MetaData.JobName + `-` + userName + `",` +
`"activeDeadlineSeconds": "` + lastUpdate.Properties.Job.Spec.Active + `",` +
`"containername": "` + lastUpdate.Properties.Job.Spec.Template.Spec.Containers[0].Name + `-` + userName + `",` +
`"image": "` + lastUpdate.Properties.Job.Spec.Template.Spec.Containers[0].Image + `",` +
`"uuid": "` + newUUID + `"}`
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"`
}
actionCreate := Action{
Act: "create",
When: time.Now().Unix(),
Parameters: json.RawMessage(msg),
}
payload, err := json.Marshal(actionCreate)
if err != nil {
return "", err
}
if session.IsReady {
err = session.Send(payload, ic.Manager)
return newUUID, err
} else {
return "", fmt.Errorf("could not send IC create action, AMQP session is not ready")
}
}

View file

@ -828,3 +828,13 @@ func TestDeleteUser(t *testing.T) {
assert.Equal(t, finalNumber, initialNumber-1)
}
func TestDuplicateScenarioForUser(t *testing.T) {
database.DropTables()
database.MigrateModels()
assert.NoError(t, database.AddTestUsers())
// TODO test duplicate scenario for user function here!!
}

View file

@ -58,7 +58,7 @@ func (w *Widget) addToDashboard() error {
return err
}
// associate dashboard with simulation
// associate widget with dashboard
err = db.Model(&dab).Association("Widgets").Append(w).Error
return err
@ -105,26 +105,3 @@ func (w *Widget) delete() error {
return err
}
func (w *Widget) Duplicate(dashboardID uint, signalMap map[uint]uint) error {
var duplicateW Widget
duplicateW.DashboardID = dashboardID
duplicateW.CustomProperties = w.CustomProperties
duplicateW.Height = w.Height
duplicateW.Width = w.Width
duplicateW.MinHeight = w.MinHeight
duplicateW.MinWidth = w.MinWidth
duplicateW.Name = w.Name
duplicateW.Type = w.Type
duplicateW.X = w.X
duplicateW.Y = w.Y
duplicateW.Z = w.Z
duplicateW.SignalIDs = []int64{}
for _, id := range w.SignalIDs {
duplicateW.SignalIDs = append(duplicateW.SignalIDs, int64(signalMap[uint(id)]))
}
err := duplicateW.addToDashboard()
return err
}

View file

@ -23,6 +23,7 @@ package main
import (
"fmt"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/user"
"log"
"time"
@ -126,6 +127,7 @@ func main() {
session := helper.NewAMQPSession("villas-amqp-session", amqpurl, "villas", infrastructure_component.ProcessMessage)
healthz.SetAMQPSession(session) // healthz needs to know the amqp session to check the health of the backend
infrastructure_component.SetAMQPSession(session) // IC needs to know the session to send amqp messages
user.SetAMQPSession(session) // User needs to know the session to duplicate ICs upon login
// send Ping to all externally managed ICs
for {