From d0c6cca76c8d01e15d73bc297095adacb75545e3 Mon Sep 17 00:00:00 2001 From: Sonja Happ Date: Fri, 30 Oct 2020 15:16:53 +0100 Subject: [PATCH] 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 --- database/models.go | 2 +- helper/test_data.go | 4 +- routes/infrastructure-component/amqpclient.go | 27 +++---- routes/infrastructure-component/ic_methods.go | 76 ++++++++++--------- .../infrastructure-component/ic_validators.go | 3 + 5 files changed, 61 insertions(+), 51 deletions(-) diff --git a/database/models.go b/database/models.go index 8ffa86d..787a7c3 100644 --- a/database/models.go +++ b/database/models.go @@ -129,7 +129,7 @@ type InfrastructureComponent struct { // Type of IC (RTDS, VILLASnode, RTDS, etc.) Type string `json:"type" gorm:"default:''"` // Uptime of the IC - Uptime int `json:"uptime" gorm:"default:0"` + Uptime float64 `json:"uptime" gorm:"default:0"` // State of the IC State string `json:"state" gorm:"default:''"` // Time of last state update diff --git a/helper/test_data.go b/helper/test_data.go index 3d53c3f..5dbcde4 100644 --- a/helper/test_data.go +++ b/helper/test_data.go @@ -98,7 +98,7 @@ var ICA = database.InfrastructureComponent{ Type: "VILLASnode Signal Generator", Category: "Signal Generator", Name: "ACS Demo Signals", - Uptime: 0, + Uptime: -1.0, State: "idle", Location: "k8s", Description: "A signal generator for testing purposes", @@ -113,7 +113,7 @@ var ICB = database.InfrastructureComponent{ Type: "DPsim", Category: "Simulator", Name: "Test DPsim Simulator", - Uptime: 0, + Uptime: -1.0, State: "running", Location: "ACS Laboratory", Description: "This is a test description", diff --git a/routes/infrastructure-component/amqpclient.go b/routes/infrastructure-component/amqpclient.go index 19cd17e..1d064b3 100644 --- a/routes/infrastructure-component/amqpclient.go +++ b/routes/infrastructure-component/amqpclient.go @@ -56,17 +56,18 @@ type Action struct { } type ICUpdate struct { - State *string `json:"state"` - Properties struct { - UUID string `json:"uuid"` - Name *string `json:"name"` - Category *string `json:"category"` - Type *string `json:"type"` - Location *string `json:"location"` - WS_url *string `json:"ws_url"` - API_url *string `json:"api_url"` - Description *string `json:"description"` - } `json:"properties"` + Status *struct { + UUID string `json:"uuid"` + State *string `json:"state"` + Name *string `json:"name"` + Category *string `json:"category"` + Type *string `json:"type"` + Location *string `json:"location"` + WS_url *string `json:"ws_url"` + API_url *string `json:"api_url"` + Description *string `json:"description"` + Uptime *float64 `json:"uptime"` // TODO check if data type of uptime is float64 or int + } `json:"status"` // 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) } - if payload.State != nil { + if payload.Status != nil { // 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) if err != nil { diff --git a/routes/infrastructure-component/ic_methods.go b/routes/infrastructure-component/ic_methods.go index c6a55ff..3a757b2 100644 --- a/routes/infrastructure-component/ic_methods.go +++ b/routes/infrastructure-component/ic_methods.go @@ -93,20 +93,20 @@ func (s *InfrastructureComponent) getConfigs() ([]database.ComponentConfiguratio func createNewICviaAMQP(payload ICUpdate) error { var newICReq AddICRequest - newICReq.InfrastructureComponent.UUID = payload.Properties.UUID - if payload.Properties.Name == nil || - payload.Properties.Category == nil || - payload.Properties.Type == nil { + newICReq.InfrastructureComponent.UUID = payload.Status.UUID + if payload.Status.Name == nil || + payload.Status.Category == nil || + payload.Status.Type == nil { // 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") } - newICReq.InfrastructureComponent.Name = *payload.Properties.Name - newICReq.InfrastructureComponent.Category = *payload.Properties.Category - newICReq.InfrastructureComponent.Type = *payload.Properties.Type + newICReq.InfrastructureComponent.Name = *payload.Status.Name + newICReq.InfrastructureComponent.Category = *payload.Status.Category + newICReq.InfrastructureComponent.Type = *payload.Status.Type // add optional params - if payload.State != nil { - newICReq.InfrastructureComponent.State = *payload.State + if payload.Status.State != nil { + newICReq.InfrastructureComponent.State = *payload.Status.State } else { newICReq.InfrastructureComponent.State = "unknown" } @@ -116,17 +116,20 @@ func createNewICviaAMQP(payload ICUpdate) error { return nil } - if payload.Properties.WS_url != nil { - newICReq.InfrastructureComponent.WebsocketURL = *payload.Properties.WS_url + if payload.Status.WS_url != nil { + newICReq.InfrastructureComponent.WebsocketURL = *payload.Status.WS_url } - if payload.Properties.API_url != nil { - newICReq.InfrastructureComponent.APIURL = *payload.Properties.API_url + if payload.Status.API_url != nil { + newICReq.InfrastructureComponent.APIURL = *payload.Status.API_url } - if payload.Properties.Location != nil { - newICReq.InfrastructureComponent.Location = *payload.Properties.Location + if payload.Status.Location != nil { + newICReq.InfrastructureComponent.Location = *payload.Status.Location } - if payload.Properties.Description != nil { - newICReq.InfrastructureComponent.Description = *payload.Properties.Description + if payload.Status.Description != nil { + newICReq.InfrastructureComponent.Description = *payload.Status.Description + } + if payload.Status.Uptime != nil { + newICReq.InfrastructureComponent.Uptime = *payload.Status.Uptime } // TODO add JSON start parameter scheme @@ -157,10 +160,10 @@ func createNewICviaAMQP(payload ICUpdate) error { func (s *InfrastructureComponent) updateICviaAMQP(payload ICUpdate) error { var updatedICReq UpdateICRequest - if payload.State != nil { - updatedICReq.InfrastructureComponent.State = *payload.State + if payload.Status.State != nil { + updatedICReq.InfrastructureComponent.State = *payload.Status.State - if *payload.State == "gone" { + if *payload.Status.State == "gone" { // remove IC from DB log.Println("########## AMQP: Deleting IC with state gone") err := s.delete(true) @@ -172,27 +175,30 @@ func (s *InfrastructureComponent) updateICviaAMQP(payload ICUpdate) error { } } - if payload.Properties.Type != nil { - updatedICReq.InfrastructureComponent.Type = *payload.Properties.Type + if payload.Status.Type != nil { + updatedICReq.InfrastructureComponent.Type = *payload.Status.Type } - if payload.Properties.Category != nil { - updatedICReq.InfrastructureComponent.Category = *payload.Properties.Category + if payload.Status.Category != nil { + updatedICReq.InfrastructureComponent.Category = *payload.Status.Category } - if payload.Properties.Name != nil { - updatedICReq.InfrastructureComponent.Name = *payload.Properties.Name + if payload.Status.Name != nil { + updatedICReq.InfrastructureComponent.Name = *payload.Status.Name } - if payload.Properties.WS_url != nil { - updatedICReq.InfrastructureComponent.WebsocketURL = *payload.Properties.WS_url + if payload.Status.WS_url != nil { + updatedICReq.InfrastructureComponent.WebsocketURL = *payload.Status.WS_url } - if payload.Properties.API_url != nil { - updatedICReq.InfrastructureComponent.APIURL = *payload.Properties.API_url + if payload.Status.API_url != nil { + updatedICReq.InfrastructureComponent.APIURL = *payload.Status.API_url } - if payload.Properties.Location != nil { - //postgres.Jsonb{json.RawMessage(`{"location" : " ` + *payload.Properties.Location + `"}`)} - updatedICReq.InfrastructureComponent.Location = *payload.Properties.Location + if payload.Status.Location != nil { + //postgres.Jsonb{json.RawMessage(`{"location" : " ` + *payload.Status.Location + `"}`)} + updatedICReq.InfrastructureComponent.Location = *payload.Status.Location } - if payload.Properties.Description != nil { - updatedICReq.InfrastructureComponent.Description = *payload.Properties.Description + if payload.Status.Description != nil { + updatedICReq.InfrastructureComponent.Description = *payload.Status.Description + } + if payload.Status.Uptime != nil { + updatedICReq.InfrastructureComponent.Uptime = *payload.Status.Uptime } // TODO add JSON start parameter scheme diff --git a/routes/infrastructure-component/ic_validators.go b/routes/infrastructure-component/ic_validators.go index 46a9eff..9f6bb05 100644 --- a/routes/infrastructure-component/ic_validators.go +++ b/routes/infrastructure-component/ic_validators.go @@ -46,6 +46,7 @@ type validNewIC struct { Description string `form:"Description" validate:"omitempty"` StartParameterScheme postgres.Jsonb `form:"StartParameterScheme" validate:"omitempty"` ManagedExternally *bool `form:"ManagedExternally" validate:"required"` + Uptime float64 `form:"Uptime" validate:"omitempty"` } type validUpdatedIC struct { @@ -60,6 +61,7 @@ type validUpdatedIC struct { Description string `form:"Description" validate:"omitempty"` StartParameterScheme postgres.Jsonb `form:"StartParameterScheme" validate:"omitempty"` ManagedExternally *bool `form:"ManagedExternally" validate:"required"` + Uptime float64 `form:"Uptime" validate:"omitempty"` } type AddICRequest struct { @@ -148,6 +150,7 @@ func (r *AddICRequest) createIC(receivedViaAMQP bool) (InfrastructureComponent, s.Description = r.InfrastructureComponent.Description s.StartParameterScheme = r.InfrastructureComponent.StartParameterScheme s.ManagedExternally = *r.InfrastructureComponent.ManagedExternally + s.Uptime = -1.0 // no uptime available if r.InfrastructureComponent.State != "" { s.State = r.InfrastructureComponent.State } else {