Implement health check for xApp
[ric-plt/ricdms.git] / pkg / health / health.go
1 //==================================================================================
2 //  Copyright (c) 2022 Samsung
3 //
4 //   Licensed under the Apache License, Version 2.0 (the "License");
5 //   you may not use this file except in compliance with the License.
6 //   You may obtain a copy of the License at
7 //
8 //       http://www.apache.org/licenses/LICENSE-2.0
9 //
10 //   Unless required by applicable law or agreed to in writing, software
11 //   distributed under the License is distributed on an "AS IS" BASIS,
12 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 //   See the License for the specific language governing permissions and
14 //   limitations under the License.
15 //
16 //   This source code is part of the near-RT RIC (RAN Intelligent Controller)
17 //   platform project (RICP).
18 //==================================================================================
19 //
20
21 package health
22
23 import (
24         "fmt"
25         "net/http"
26
27         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/models"
28         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms"
29 )
30
31 var (
32         HEALTHY = "Service is running healthy"
33 )
34
35 type IHealthChecker interface {
36         GetStatus() *models.Status
37         GetxAppStatus(appName, namespace string) *models.Status
38 }
39
40 type HealthChecker struct {
41 }
42
43 func NewHealthChecker() IHealthChecker {
44         return &HealthChecker{}
45 }
46 func (h *HealthChecker) GetStatus() *models.Status {
47         return &models.Status{
48                 Status: &HEALTHY,
49         }
50 }
51
52 func (h *HealthChecker) GetxAppStatus(appName, namespace string) *models.Status {
53         resp, err := http.Get(fmt.Sprintf(ricdms.Config.GETxAPPHealthURL, appName, namespace))
54         if err != nil {
55                 ricdms.Logger.Error("Received error while fetching health info: %v", err)
56                 return nil
57         }
58
59         if resp.StatusCode != http.StatusOK {
60                 ricdms.Logger.Error("xApp is not healthy (http status=%s)", resp.Status)
61                 return nil
62         }
63
64         return &models.Status{
65                 Status: &HEALTHY,
66         }
67 }