Adapt layout of AMQP update to VILLAScontroller protocol, add uptime in IC creation and updates via AMQP; need to check data type of uptime #31

This commit is contained in:
Sonja Happ 2020-10-30 15:16:53 +01:00
parent e2bebbc676
commit d0c6cca76c
5 changed files with 61 additions and 51 deletions

View file

@ -129,7 +129,7 @@ type InfrastructureComponent struct {
// Type of IC (RTDS, VILLASnode, RTDS, etc.) // Type of IC (RTDS, VILLASnode, RTDS, etc.)
Type string `json:"type" gorm:"default:''"` Type string `json:"type" gorm:"default:''"`
// Uptime of the IC // Uptime of the IC
Uptime int `json:"uptime" gorm:"default:0"` Uptime float64 `json:"uptime" gorm:"default:0"`
// State of the IC // State of the IC
State string `json:"state" gorm:"default:''"` State string `json:"state" gorm:"default:''"`
// Time of last state update // Time of last state update

View file

@ -98,7 +98,7 @@ var ICA = database.InfrastructureComponent{
Type: "VILLASnode Signal Generator", Type: "VILLASnode Signal Generator",
Category: "Signal Generator", Category: "Signal Generator",
Name: "ACS Demo Signals", Name: "ACS Demo Signals",
Uptime: 0, Uptime: -1.0,
State: "idle", State: "idle",
Location: "k8s", Location: "k8s",
Description: "A signal generator for testing purposes", Description: "A signal generator for testing purposes",
@ -113,7 +113,7 @@ var ICB = database.InfrastructureComponent{
Type: "DPsim", Type: "DPsim",
Category: "Simulator", Category: "Simulator",
Name: "Test DPsim Simulator", Name: "Test DPsim Simulator",
Uptime: 0, Uptime: -1.0,
State: "running", State: "running",
Location: "ACS Laboratory", Location: "ACS Laboratory",
Description: "This is a test description", Description: "This is a test description",

View file

@ -56,9 +56,9 @@ type Action struct {
} }
type ICUpdate struct { type ICUpdate struct {
State *string `json:"state"` Status *struct {
Properties struct {
UUID string `json:"uuid"` UUID string `json:"uuid"`
State *string `json:"state"`
Name *string `json:"name"` Name *string `json:"name"`
Category *string `json:"category"` Category *string `json:"category"`
Type *string `json:"type"` Type *string `json:"type"`
@ -66,7 +66,8 @@ type ICUpdate struct {
WS_url *string `json:"ws_url"` WS_url *string `json:"ws_url"`
API_url *string `json:"api_url"` API_url *string `json:"api_url"`
Description *string `json:"description"` Description *string `json:"description"`
} `json:"properties"` Uptime *float64 `json:"uptime"` // TODO check if data type of uptime is float64 or int
} `json:"status"`
// TODO add JSON start parameter scheme // TODO add JSON start parameter scheme
} }
@ -261,9 +262,9 @@ func processMessage(message amqp.Delivery) error {
return fmt.Errorf("AMQP: Could not unmarshal message to JSON: %v err: %v", string(message.Body), err) return fmt.Errorf("AMQP: Could not unmarshal message to JSON: %v err: %v", string(message.Body), err)
} }
if payload.State != nil { if payload.Status != nil {
// if a message contains a "state" field, it is an update for an IC // if a message contains a "state" field, it is an update for an IC
ICUUID := payload.Properties.UUID ICUUID := payload.Status.UUID
_, err = uuid.Parse(ICUUID) _, err = uuid.Parse(ICUUID)
if err != nil { if err != nil {

View file

@ -93,20 +93,20 @@ func (s *InfrastructureComponent) getConfigs() ([]database.ComponentConfiguratio
func createNewICviaAMQP(payload ICUpdate) error { func createNewICviaAMQP(payload ICUpdate) error {
var newICReq AddICRequest var newICReq AddICRequest
newICReq.InfrastructureComponent.UUID = payload.Properties.UUID newICReq.InfrastructureComponent.UUID = payload.Status.UUID
if payload.Properties.Name == nil || if payload.Status.Name == nil ||
payload.Properties.Category == nil || payload.Status.Category == nil ||
payload.Properties.Type == nil { payload.Status.Type == nil {
// cannot create new IC because required information (name, type, and/or category missing) // cannot create new IC because required information (name, type, and/or category missing)
return fmt.Errorf("AMQP: Cannot create new IC, required field(s) is/are missing: name, type, category") return fmt.Errorf("AMQP: Cannot create new IC, required field(s) is/are missing: name, type, category")
} }
newICReq.InfrastructureComponent.Name = *payload.Properties.Name newICReq.InfrastructureComponent.Name = *payload.Status.Name
newICReq.InfrastructureComponent.Category = *payload.Properties.Category newICReq.InfrastructureComponent.Category = *payload.Status.Category
newICReq.InfrastructureComponent.Type = *payload.Properties.Type newICReq.InfrastructureComponent.Type = *payload.Status.Type
// add optional params // add optional params
if payload.State != nil { if payload.Status.State != nil {
newICReq.InfrastructureComponent.State = *payload.State newICReq.InfrastructureComponent.State = *payload.Status.State
} else { } else {
newICReq.InfrastructureComponent.State = "unknown" newICReq.InfrastructureComponent.State = "unknown"
} }
@ -116,17 +116,20 @@ func createNewICviaAMQP(payload ICUpdate) error {
return nil return nil
} }
if payload.Properties.WS_url != nil { if payload.Status.WS_url != nil {
newICReq.InfrastructureComponent.WebsocketURL = *payload.Properties.WS_url newICReq.InfrastructureComponent.WebsocketURL = *payload.Status.WS_url
} }
if payload.Properties.API_url != nil { if payload.Status.API_url != nil {
newICReq.InfrastructureComponent.APIURL = *payload.Properties.API_url newICReq.InfrastructureComponent.APIURL = *payload.Status.API_url
} }
if payload.Properties.Location != nil { if payload.Status.Location != nil {
newICReq.InfrastructureComponent.Location = *payload.Properties.Location newICReq.InfrastructureComponent.Location = *payload.Status.Location
} }
if payload.Properties.Description != nil { if payload.Status.Description != nil {
newICReq.InfrastructureComponent.Description = *payload.Properties.Description newICReq.InfrastructureComponent.Description = *payload.Status.Description
}
if payload.Status.Uptime != nil {
newICReq.InfrastructureComponent.Uptime = *payload.Status.Uptime
} }
// TODO add JSON start parameter scheme // TODO add JSON start parameter scheme
@ -157,10 +160,10 @@ func createNewICviaAMQP(payload ICUpdate) error {
func (s *InfrastructureComponent) updateICviaAMQP(payload ICUpdate) error { func (s *InfrastructureComponent) updateICviaAMQP(payload ICUpdate) error {
var updatedICReq UpdateICRequest var updatedICReq UpdateICRequest
if payload.State != nil { if payload.Status.State != nil {
updatedICReq.InfrastructureComponent.State = *payload.State updatedICReq.InfrastructureComponent.State = *payload.Status.State
if *payload.State == "gone" { if *payload.Status.State == "gone" {
// remove IC from DB // remove IC from DB
log.Println("########## AMQP: Deleting IC with state gone") log.Println("########## AMQP: Deleting IC with state gone")
err := s.delete(true) err := s.delete(true)
@ -172,27 +175,30 @@ func (s *InfrastructureComponent) updateICviaAMQP(payload ICUpdate) error {
} }
} }
if payload.Properties.Type != nil { if payload.Status.Type != nil {
updatedICReq.InfrastructureComponent.Type = *payload.Properties.Type updatedICReq.InfrastructureComponent.Type = *payload.Status.Type
} }
if payload.Properties.Category != nil { if payload.Status.Category != nil {
updatedICReq.InfrastructureComponent.Category = *payload.Properties.Category updatedICReq.InfrastructureComponent.Category = *payload.Status.Category
} }
if payload.Properties.Name != nil { if payload.Status.Name != nil {
updatedICReq.InfrastructureComponent.Name = *payload.Properties.Name updatedICReq.InfrastructureComponent.Name = *payload.Status.Name
} }
if payload.Properties.WS_url != nil { if payload.Status.WS_url != nil {
updatedICReq.InfrastructureComponent.WebsocketURL = *payload.Properties.WS_url updatedICReq.InfrastructureComponent.WebsocketURL = *payload.Status.WS_url
} }
if payload.Properties.API_url != nil { if payload.Status.API_url != nil {
updatedICReq.InfrastructureComponent.APIURL = *payload.Properties.API_url updatedICReq.InfrastructureComponent.APIURL = *payload.Status.API_url
} }
if payload.Properties.Location != nil { if payload.Status.Location != nil {
//postgres.Jsonb{json.RawMessage(`{"location" : " ` + *payload.Properties.Location + `"}`)} //postgres.Jsonb{json.RawMessage(`{"location" : " ` + *payload.Status.Location + `"}`)}
updatedICReq.InfrastructureComponent.Location = *payload.Properties.Location updatedICReq.InfrastructureComponent.Location = *payload.Status.Location
} }
if payload.Properties.Description != nil { if payload.Status.Description != nil {
updatedICReq.InfrastructureComponent.Description = *payload.Properties.Description updatedICReq.InfrastructureComponent.Description = *payload.Status.Description
}
if payload.Status.Uptime != nil {
updatedICReq.InfrastructureComponent.Uptime = *payload.Status.Uptime
} }
// TODO add JSON start parameter scheme // TODO add JSON start parameter scheme

View file

@ -46,6 +46,7 @@ type validNewIC struct {
Description string `form:"Description" validate:"omitempty"` Description string `form:"Description" validate:"omitempty"`
StartParameterScheme postgres.Jsonb `form:"StartParameterScheme" validate:"omitempty"` StartParameterScheme postgres.Jsonb `form:"StartParameterScheme" validate:"omitempty"`
ManagedExternally *bool `form:"ManagedExternally" validate:"required"` ManagedExternally *bool `form:"ManagedExternally" validate:"required"`
Uptime float64 `form:"Uptime" validate:"omitempty"`
} }
type validUpdatedIC struct { type validUpdatedIC struct {
@ -60,6 +61,7 @@ type validUpdatedIC struct {
Description string `form:"Description" validate:"omitempty"` Description string `form:"Description" validate:"omitempty"`
StartParameterScheme postgres.Jsonb `form:"StartParameterScheme" validate:"omitempty"` StartParameterScheme postgres.Jsonb `form:"StartParameterScheme" validate:"omitempty"`
ManagedExternally *bool `form:"ManagedExternally" validate:"required"` ManagedExternally *bool `form:"ManagedExternally" validate:"required"`
Uptime float64 `form:"Uptime" validate:"omitempty"`
} }
type AddICRequest struct { type AddICRequest struct {
@ -148,6 +150,7 @@ func (r *AddICRequest) createIC(receivedViaAMQP bool) (InfrastructureComponent,
s.Description = r.InfrastructureComponent.Description s.Description = r.InfrastructureComponent.Description
s.StartParameterScheme = r.InfrastructureComponent.StartParameterScheme s.StartParameterScheme = r.InfrastructureComponent.StartParameterScheme
s.ManagedExternally = *r.InfrastructureComponent.ManagedExternally s.ManagedExternally = *r.InfrastructureComponent.ManagedExternally
s.Uptime = -1.0 // no uptime available
if r.InfrastructureComponent.State != "" { if r.InfrastructureComponent.State != "" {
s.State = r.InfrastructureComponent.State s.State = r.InfrastructureComponent.State
} else { } else {