From cd6cc6ad28c32ccce6a2833917f7408a798e45fc Mon Sep 17 00:00:00 2001 From: subhash kumar singh Date: Tue, 23 Aug 2022 10:26:13 +0000 Subject: [PATCH] Health check API for service Impleted health check API for the RICDMS service. Signed-off-by: subhash kumar singh Change-Id: I770499e6164b72864414c646d14d64f0ce9585b4 --- go.mod | 1 + go.sum | 1 + pkg/health/health.go | 43 +++++++++++++++++++++++++ pkg/restful/restful.go | 17 +++++++++- pkg/resthooks/resthooks.go | 39 +++++++++++++++++++++++ pkg/resthooks/resthooks_test.go | 70 +++++++++++++++++++++++++++++++++++++++++ pkg/resthooks/types.go | 5 +++ 7 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 pkg/health/health.go create mode 100644 pkg/resthooks/resthooks.go create mode 100644 pkg/resthooks/resthooks_test.go diff --git a/go.mod b/go.mod index 2f87777..346e895 100644 --- a/go.mod +++ b/go.mod @@ -49,6 +49,7 @@ require ( github.com/mitchellh/mapstructure v1.4.3 // indirect github.com/oklog/ulid v1.3.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/objx v0.1.1 // indirect go.mongodb.org/mongo-driver v1.8.3 // indirect golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/go.sum b/go.sum index b2801ab..fc51efc 100644 --- a/go.sum +++ b/go.sum @@ -124,6 +124,7 @@ github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTd github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= diff --git a/pkg/health/health.go b/pkg/health/health.go new file mode 100644 index 0000000..d007da4 --- /dev/null +++ b/pkg/health/health.go @@ -0,0 +1,43 @@ +//================================================================================== +// Copyright (c) 2022 Samsung +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// This source code is part of the near-RT RIC (RAN Intelligent Controller) +// platform project (RICP). +//================================================================================== +// + +package health + +import "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/models" + +var ( + HEALTHY = "Service is running healthy" +) + +type IHealthChecker interface { + GetStatus() *models.Status +} + +type HealthChecker struct { +} + +func NewHealthChecker() IHealthChecker { + return &HealthChecker{} +} +func (h *HealthChecker) GetStatus() *models.Status { + return &models.Status{ + Status: &HEALTHY, + } +} diff --git a/pkg/restful/restful.go b/pkg/restful/restful.go index 2b8dc96..4255e2b 100644 --- a/pkg/restful/restful.go +++ b/pkg/restful/restful.go @@ -23,14 +23,22 @@ import ( "log" "os" + ph "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/health" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations" + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/health" + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/resthooks" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms" "github.com/go-openapi/loads" + "github.com/go-openapi/runtime/middleware" ) func NewRestful() *Restful { - r := &Restful{} + r := &Restful{ + rh: resthooks.NewResthook( + ph.NewHealthChecker(), + ), + } r.setupHandler() return r } @@ -42,6 +50,13 @@ func (r *Restful) setupHandler() { } api := operations.NewRICDMSAPI(swaggerSpec) + + api.HealthGetHealthCheckHandler = health.GetHealthCheckHandlerFunc(func(ghcp health.GetHealthCheckParams) middleware.Responder { + ricdms.Logger.Debug("==> HealthCheck API invoked.") + resp := r.rh.GetDMSHealth() + return resp + }) + r.api = api } diff --git a/pkg/resthooks/resthooks.go b/pkg/resthooks/resthooks.go new file mode 100644 index 0000000..500b32e --- /dev/null +++ b/pkg/resthooks/resthooks.go @@ -0,0 +1,39 @@ +//================================================================================== +// Copyright (c) 2022 Samsung +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// This source code is part of the near-RT RIC (RAN Intelligent Controller) +// platform project (RICP). +//================================================================================== +// + +package resthooks + +import ( + ph "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/health" + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/health" + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms" + "github.com/go-openapi/runtime/middleware" +) + +func NewResthook(h ph.IHealthChecker) *Resthook { + return &Resthook{ + HealthChecker: h, + } +} + +func (rh *Resthook) GetDMSHealth() (resp middleware.Responder) { + ricdms.Logger.Debug("healthchecker : %v\n", rh.HealthChecker) + return health.NewGetHealthCheckOK().WithPayload(rh.HealthChecker.GetStatus()) +} diff --git a/pkg/resthooks/resthooks_test.go b/pkg/resthooks/resthooks_test.go new file mode 100644 index 0000000..e7cbfbd --- /dev/null +++ b/pkg/resthooks/resthooks_test.go @@ -0,0 +1,70 @@ +//================================================================================== +// Copyright (c) 2022 Samsung +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// This source code is part of the near-RT RIC (RAN Intelligent Controller) +// platform project (RICP). +//================================================================================== +// +package resthooks + +import ( + "os" + "testing" + + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/health" + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/models" + h "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/health" + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" +) + +var rh *Resthook +var successStatus *models.Status + +func TestMain(m *testing.M) { + + successStatus = &models.Status{ + Status: &health.HEALTHY, + } + ricdms.Init() + rh = &Resthook{ + HealthChecker: HealthCheckerMock{}, + } + code := m.Run() + os.Exit(code) +} + +func TestHealth(t *testing.T) { + resp := rh.GetDMSHealth() + switch resp.(type) { + case *h.GetHealthCheckOK: + assert.Equal(t, successStatus, resp.(*h.GetHealthCheckOK).Payload) + + case *h.GetHealthCheckInternalServerError: + assert.Fail(t, "Internal Server generated: %v", resp) + + default: + assert.Fail(t, "Unknown type of resp : %v", resp) + } +} + +type HealthCheckerMock struct { + mock.Mock +} + +func (h HealthCheckerMock) GetStatus() *models.Status { + return successStatus +} diff --git a/pkg/resthooks/types.go b/pkg/resthooks/types.go index 7c3df5d..40a46aa 100644 --- a/pkg/resthooks/types.go +++ b/pkg/resthooks/types.go @@ -19,5 +19,10 @@ // package resthooks +import ( + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/health" +) + type Resthook struct { + HealthChecker health.IHealthChecker } -- 2.16.6