From: naman.gupta Date: Thu, 17 Nov 2022 12:50:03 +0000 (+0530) Subject: Implement Health Check Api. X-Git-Tag: 3.0.0~18 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=a5d992815f470a349dea86d616509222eb051f97;p=ric-plt%2Fa1.git Implement Health Check Api. Implement Health Check Api. Signed-off-by: naman.gupta Change-Id: Id091fa42eb6df5995f9d5d3995a075a59bb827f9 --- diff --git a/a1-go/pkg/restapi/operations/a1_mediator/a1_controller_get_healthcheck_responses.go b/a1-go/pkg/restapi/operations/a1_mediator/a1_controller_get_healthcheck_responses.go index 20cf0ca..84f3630 100644 --- a/a1-go/pkg/restapi/operations/a1_mediator/a1_controller_get_healthcheck_responses.go +++ b/a1-go/pkg/restapi/operations/a1_mediator/a1_controller_get_healthcheck_responses.go @@ -55,3 +55,27 @@ func (o *A1ControllerGetHealthcheckOK) WriteResponse(rw http.ResponseWriter, pro rw.WriteHeader(200) } + +// A1ControllerGetHealthcheckInternalServerErrorCode is the HTTP code returned for type A1ControllerGetHealthcheckInternalServerError +const A1ControllerGetHealthcheckInternalServerErrorCode int = 500 + +/*A1ControllerGetHealthcheckInternalServerError Internal error to signal A1 is not healthy. Client should attempt to retry later. + +swagger:response a1ControllerGetHealthcheckInternalServerError +*/ +type A1ControllerGetHealthcheckInternalServerError struct { +} + +// NewA1ControllerGetHealthcheckInternalServerError creates A1ControllerGetHealthcheckInternalServerError with default headers values +func NewA1ControllerGetHealthcheckInternalServerError() *A1ControllerGetHealthcheckInternalServerError { + + return &A1ControllerGetHealthcheckInternalServerError{} +} + +// WriteResponse to the client +func (o *A1ControllerGetHealthcheckInternalServerError) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) { + + rw.Header().Del(runtime.HeaderContentType) //Remove Content-Type on empty responses + + rw.WriteHeader(500) +} diff --git a/a1-go/pkg/restful/restful.go b/a1-go/pkg/restful/restful.go index d8d1e92..44b3422 100644 --- a/a1-go/pkg/restful/restful.go +++ b/a1-go/pkg/restful/restful.go @@ -50,6 +50,16 @@ func (r *Restful) setupHandler() *operations.A1API { } api := operations.NewA1API(swaggerSpec) + + api.A1MediatorA1ControllerGetHealthcheckHandler = a1_mediator.A1ControllerGetHealthcheckHandlerFunc(func(param a1_mediator.A1ControllerGetHealthcheckParams) middleware.Responder { + a1.Logger.Debug("handler for get Health Check of A1") + resp := r.rh.GetA1Health() + if resp == false { + return a1_mediator.NewA1ControllerGetHealthcheckInternalServerError() + } + return a1_mediator.NewA1ControllerGetHealthcheckOK() + }) + api.A1MediatorA1ControllerGetAllPolicyTypesHandler = a1_mediator.A1ControllerGetAllPolicyTypesHandlerFunc(func(param a1_mediator.A1ControllerGetAllPolicyTypesParams) middleware.Responder { a1.Logger.Debug("handler for get all policy type") return a1_mediator.NewA1ControllerGetAllPolicyTypesOK().WithPayload(r.rh.GetAllPolicyType()) diff --git a/a1-go/pkg/resthooks/resthooks.go b/a1-go/pkg/resthooks/resthooks.go index c57c2e8..8fb07df 100644 --- a/a1-go/pkg/resthooks/resthooks.go +++ b/a1-go/pkg/resthooks/resthooks.go @@ -95,6 +95,15 @@ func createResthook(sdlInst iSdl, rmrSenderInst rmr.IRmrSender) *Resthook { } } +func (rh *Resthook) GetA1Health() bool { + data, _ := rh.db.GetAll(a1MediatorNs) + if data != nil { + a1.Logger.Debug("Database connected and A1 is healthy") + return true + } + return false +} + func (rh *Resthook) GetAllPolicyType() []models.PolicyTypeID { var policyTypeIDs []models.PolicyTypeID diff --git a/a1-go/pkg/resthooks/resthooks_test.go b/a1-go/pkg/resthooks/resthooks_test.go index 8c6b59a..6d7679e 100644 --- a/a1-go/pkg/resthooks/resthooks_test.go +++ b/a1-go/pkg/resthooks/resthooks_test.go @@ -58,6 +58,17 @@ func TestMain(m *testing.M) { os.Exit(code) } +func TestHealth(t *testing.T) { + resp := rh.GetA1Health() + if resp == true { + a1.Logger.Debug("A1 is healthy ") + assert.Equal(t, true, resp) + } else { + a1.Logger.Debug("A1 is unhealthy") + assert.Equal(t, false, resp) + } +} + func TestGetAllPolicyType(t *testing.T) { resp := rh.GetAllPolicyType() assert.Equal(t, 2, len(resp))