Merge "LN0739_FM_FR13: support interface to external Prometheus alert manager"
diff --git a/alarm/types.go b/alarm/types.go
index 149671a..5a70f18 100755
--- a/alarm/types.go
+++ b/alarm/types.go
@@ -68,8 +68,8 @@
}
type AlarmConfigParams struct {
- MaxActiveAlarms int `json:"maxactivealarms"`
- MaxAlarmHistory int `json:"maxalarmhistory"`
+ MaxActiveAlarms int `json:"maxactivealarms"`
+ MaxAlarmHistory int `json:"maxalarmhistory"`
}
// RICAlarm is an alarm instance
@@ -89,59 +89,22 @@
// Temp alarm constants & definitions
const (
- RIC_RT_DISTRIBUTION_FAILED int = 8004
- TCP_CONNECTIVITY_LOST_TO_DBAAS int = 8005
- E2_CONNECTIVITY_LOST_TO_GNODEB int = 8006
- E2_CONNECTIVITY_LOST_TO_ENODEB int = 8007
- ACTIVE_ALARM_EXCEED_MAX_THRESHOLD int = 8008
+ RIC_RT_DISTRIBUTION_FAILED int = 8004
+ TCP_CONNECTIVITY_LOST_TO_DBAAS int = 8005
+ E2_CONNECTIVITY_LOST_TO_GNODEB int = 8006
+ E2_CONNECTIVITY_LOST_TO_ENODEB int = 8007
+ ACTIVE_ALARM_EXCEED_MAX_THRESHOLD int = 8008
ALARM_HISTORY_EXCEED_MAX_THRESHOLD int = 8009
)
type AlarmDefinition struct {
- AlarmId int
- AlarmText string
- EventType string
- OperationInstructions string
+ AlarmId int `json:"alarmid"`
+ AlarmText string `json:"alarmtext"`
+ EventType string `json:"eventtype"`
+ OperationInstructions string `json:"operationinstructions"`
}
-var RICAlarmDefinitions = map[int]AlarmDefinition{
- RIC_RT_DISTRIBUTION_FAILED: {
- AlarmId: RIC_RT_DISTRIBUTION_FAILED,
- AlarmText: "RIC ROUTING TABLE DISTRIBUTION FAILED",
- EventType: "Processing error",
- OperationInstructions: "Not defined",
- },
- TCP_CONNECTIVITY_LOST_TO_DBAAS: {
- AlarmId: TCP_CONNECTIVITY_LOST_TO_DBAAS,
- AlarmText: "TCP CONNECTIVITY LOST TO DBAAS",
- EventType: "Communication error",
- OperationInstructions: "Not defined",
- },
- E2_CONNECTIVITY_LOST_TO_GNODEB: {
- AlarmId: E2_CONNECTIVITY_LOST_TO_GNODEB,
- AlarmText: "E2 CONNECTIVITY LOST TO G-NODEB",
- EventType: "Communication error",
- OperationInstructions: "Not defined",
- },
- E2_CONNECTIVITY_LOST_TO_ENODEB: {
- AlarmId: E2_CONNECTIVITY_LOST_TO_ENODEB,
- AlarmText: "E2 CONNECTIVITY LOST TO E-NODEB",
- EventType: "Communication error",
- OperationInstructions: "Not defined",
- },
- ACTIVE_ALARM_EXCEED_MAX_THRESHOLD: {
- AlarmId: ACTIVE_ALARM_EXCEED_MAX_THRESHOLD,
- AlarmText: "ACTIVE ALARM EXCEED MAX THRESHOLD",
- EventType: "Warning",
- OperationInstructions: "Not defined",
- },
- ALARM_HISTORY_EXCEED_MAX_THRESHOLD: {
- AlarmId: ALARM_HISTORY_EXCEED_MAX_THRESHOLD,
- AlarmText: "ALARM HISTORY EXCEED MAX THRESHOLD",
- EventType: "Warning",
- OperationInstructions: "Not defined",
- },
-}
+var RICAlarmDefinitions map[int]*AlarmDefinition
const (
ALARM_MANAGER_HTTP_URL string = "http://service-ricplt-alarmmanager-http.ricplt:8080"
diff --git a/build/Dockerfile b/build/Dockerfile
index b774e02..6572c49 100755
--- a/build/Dockerfile
+++ b/build/Dockerfile
@@ -37,6 +37,7 @@
COPY --from=ubuntu-alarm-manager /go/src/am/manager/alarm-manager /
COPY --from=ubuntu-alarm-manager /go/src/am/config/* /
COPY --from=ubuntu-alarm-manager /usr/local/lib /usr/local/lib
+COPY --from=ubuntu-alarm-manager /go/src/am/definitions/* /
RUN ldconfig
diff --git a/build/build_ubuntu.sh b/build/build_ubuntu.sh
index 29b9d77..3136eca 100755
--- a/build/build_ubuntu.sh
+++ b/build/build_ubuntu.sh
@@ -42,6 +42,9 @@
export CFG_FILE=../config/config-file.json
export RMR_SEED_RT=../config/uta_rtg.rt
+# xApp stuff
+export DEF_FILE=../definitions/alarm-definition.json
+
GO111MODULE=on GO_ENABLED=0 GOOS=linux
# setup version tag
diff --git a/build/run.sh b/build/run.sh
index db9e0e3..1e1ad85 100755
--- a/build/run.sh
+++ b/build/run.sh
@@ -24,5 +24,6 @@
#
export RMR_SEED_RT=./uta_rtg.rt
export RMR_SRC_ID="service-ricplt-alarmmanager-rmr.ricplt"
+export DEF_FILE=./alarm-definition.json
exec ./alarm-manager -f ./config-file.json
diff --git a/cli/alarm-cli.go b/cli/alarm-cli.go
index 97ccb91..a3ea90c 100755
--- a/cli/alarm-cli.go
+++ b/cli/alarm-cli.go
@@ -10,10 +10,14 @@
"net/http"
"os"
"time"
-
+ "strconv"
"gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm"
)
+type CliAlarmDefinitions struct {
+ AlarmDefinitions []*alarm.AlarmDefinition `json:"alarmdefinitions"`
+}
+
func main() {
// configure commando
@@ -85,6 +89,29 @@
SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
postAlarmConfig(flags)
})
+ // Create alarm defenition
+ commando.
+ Register("define").
+ SetShortDescription("Define alarm with given parameters").
+ AddFlag("aid", "alarm identifier", commando.Int, nil).
+ AddFlag("atx", "alarm text", commando.String, nil).
+ AddFlag("ety", "event type", commando.String, nil).
+ AddFlag("oin", "operation instructions", commando.String, nil).
+ AddFlag("host", "Alarm manager host address", commando.String, "localhost").
+ AddFlag("port", "Alarm manager host address", commando.String, "8080").
+ SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
+ postAlarmDefinition(flags)
+ })
+ // Delete alarm defenition
+ commando.
+ Register("undefine").
+ SetShortDescription("Define alarm with given parameters").
+ AddFlag("aid", "alarm identifier", commando.Int, nil).
+ AddFlag("host", "Alarm manager host address", commando.String, "localhost").
+ AddFlag("port", "Alarm manager host address", commando.String, "8080").
+ SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
+ deleteAlarmDefinition(flags)
+ })
// parse command-line arguments
commando.Parse(nil)
@@ -195,3 +222,52 @@
return
}
}
+
+func postAlarmDefinition(flags map[string]commando.FlagValue) {
+ host, _ := flags["host"].GetString()
+ port, _ := flags["port"].GetString()
+ alarmid, _ := flags["aid"].GetInt()
+ alarmtxt, _ := flags["atx"].GetString()
+ etype, _ := flags["ety"].GetString()
+ operation, _ := flags["oin"].GetString()
+ targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/define", host, port)
+
+ var alarmdefinition alarm.AlarmDefinition
+ alarmdefinition.AlarmId = alarmid
+ alarmdefinition.AlarmText = alarmtxt
+ alarmdefinition.EventType = etype
+ alarmdefinition.OperationInstructions = operation
+
+ m := CliAlarmDefinitions{AlarmDefinitions: []*alarm.AlarmDefinition{&alarmdefinition}}
+ jsonData, err := json.Marshal(m)
+ if err != nil {
+ fmt.Println("json.Marshal failed: %v", err)
+ return
+ }
+
+ resp, err := http.Post(targetUrl, "application/json", bytes.NewBuffer(jsonData))
+ if err != nil || resp == nil {
+ fmt.Println("Couldn't post alarm definition due to error: %v", err)
+ return
+ }
+}
+
+func deleteAlarmDefinition(flags map[string]commando.FlagValue) {
+ host, _ := flags["host"].GetString()
+ port, _ := flags["port"].GetString()
+ alarmid, _ := flags["aid"].GetInt()
+ salarmid := strconv.FormatUint(uint64(alarmid), 10)
+ targetUrl := fmt.Sprintf("http://%s:%s/ric/v1/alarms/define/%s", host, port, salarmid)
+
+ client := &http.Client{}
+ req, err := http.NewRequest("DELETE", targetUrl, nil)
+ if err != nil || req == nil {
+ fmt.Println("Couldn't make delete request due to error: %v", err)
+ return
+ }
+ resp, errr := client.Do(req)
+ if errr != nil || resp == nil {
+ fmt.Println("Couldn't send delete request due to error: %v", err)
+ return
+ }
+}
diff --git a/definitions/alarm-definition.json b/definitions/alarm-definition.json
new file mode 100755
index 0000000..96d255f
--- /dev/null
+++ b/definitions/alarm-definition.json
@@ -0,0 +1,40 @@
+{
+ "alarmdefinitions" : [
+ {
+ "alarmId" : 8004,
+ "alarmText" : "RIC ROUTING TABLE DISTRIBUTION FAILED",
+ "eventType" : "Processing error",
+ "operationInstructions" : "Not defined"
+ },
+ {
+ "alarmId" : 8005,
+ "alarmText" : "TCP CONNECTIVITY LOST TO DBAAS",
+ "eventType" : "Communication error",
+ "operationInstructions" : "Not defined"
+ },
+ {
+ "alarmId" : 8006,
+ "alarmText" : "E2 CONNECTIVITY LOST TO G-NODEB",
+ "eventType" : "Communication error",
+ "operationInstructions" : "Not defined"
+ },
+ {
+ "alarmId" : 8007,
+ "alarmText" : "E2 CONNECTIVITY LOST TO E-NODEB",
+ "eventType" : "Communication error",
+ "operationInstructions" : "Not defined"
+ },
+ {
+ "alarmId" : 8008,
+ "alarmText" : "ACTIVE ALARM EXCEED MAX THRESHOLD",
+ "eventType" : "storage warning",
+ "operationInstructions" : "Clear alarms or raise threshold"
+ },
+ {
+ "alarmId" : 8009,
+ "alarmText" : "ALARM HISTORY EXCEED MAX THRESHOLD",
+ "eventType" : "storage warning",
+ "operationInstructions" : "Clear alarms or raise threshold"
+ }
+ ]
+}
diff --git a/go.mod b/go.mod
index 2827b3e..9b21fbb 100644
--- a/go.mod
+++ b/go.mod
@@ -19,6 +19,8 @@
github.com/gorilla/mux v1.7.1
github.com/jedib0t/go-pretty v4.3.0+incompatible // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
+ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
+ github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/prometheus/alertmanager v0.20.0
github.com/spf13/viper v1.6.2
github.com/stretchr/testify v1.5.1
diff --git a/manager/cmd/manager.go b/manager/cmd/manager.go
index 59beeac..f3d3260 100755
--- a/manager/cmd/manager.go
+++ b/manager/cmd/manager.go
@@ -24,16 +24,16 @@
"encoding/json"
"fmt"
"time"
-
+ "os"
+ "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm"
+ app "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
clientruntime "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
"github.com/prometheus/alertmanager/api/v2/client"
"github.com/prometheus/alertmanager/api/v2/client/alert"
"github.com/prometheus/alertmanager/api/v2/models"
"github.com/spf13/viper"
-
- "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm"
- app "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
+ "io/ioutil"
)
func (a *AlarmManager) StartAlertTimer() {
@@ -82,9 +82,15 @@
// Suppress duplicate alarms
idx, found := a.IsMatchFound(m.Alarm)
- if found && m.AlarmAction != alarm.AlarmActionClear {
+ if found && m.AlarmAction == alarm.AlarmActionRaise {
app.Logger.Info("Duplicate alarm found, suppressing ...")
- return nil, nil
+ if m.PerceivedSeverity == a.activeAlarms[idx].PerceivedSeverity {
+ // Duplicate with same severity found
+ return nil, nil
+ } else {
+ // Remove duplicate with different severity
+ a.activeAlarms = a.RemoveAlarm(a.activeAlarms, idx, "active")
+ }
}
// Clear alarm if found from active alarm list
@@ -223,18 +229,55 @@
return
}
+func (a *AlarmManager) ReadAlarmDefinitionFromJson() {
+
+ filename := os.Getenv("DEF_FILE")
+ file, err := ioutil.ReadFile(filename)
+ if err == nil {
+ data := RicAlarmDefinitions{}
+ err = json.Unmarshal([]byte(file), &data)
+ if err == nil {
+ for _, alarmDefinition := range data.AlarmDefinitions {
+ _, exists := alarm.RICAlarmDefinitions[alarmDefinition.AlarmId]
+ if exists {
+ app.Logger.Error("ReadAlarmDefinitionFromJson: alarm definition already exists for %v", alarmDefinition.AlarmId)
+ } else {
+ app.Logger.Debug("ReadAlarmDefinitionFromJson: alarm %v", alarmDefinition.AlarmId)
+ ricAlarmDefintion := new(alarm.AlarmDefinition)
+ ricAlarmDefintion.AlarmId = alarmDefinition.AlarmId
+ ricAlarmDefintion.AlarmText = alarmDefinition.AlarmText
+ ricAlarmDefintion.EventType = alarmDefinition.EventType
+ ricAlarmDefintion.OperationInstructions = alarmDefinition.OperationInstructions
+ alarm.RICAlarmDefinitions[alarmDefinition.AlarmId] = ricAlarmDefintion
+ }
+ }
+ } else {
+ app.Logger.Error("json.Unmarshal failed with error %v", err)
+ }
+ } else {
+ app.Logger.Error("ioutil.ReadFile failed with error %v", err)
+ }
+}
+
func (a *AlarmManager) Run(sdlcheck bool) {
app.Logger.SetMdc("alarmManager", fmt.Sprintf("%s:%s", Version, Hash))
app.SetReadyCB(func(d interface{}) { a.rmrReady = true }, true)
app.Resource.InjectStatusCb(a.StatusCB)
app.AddConfigChangeListener(a.ConfigChangeCB)
+ alarm.RICAlarmDefinitions = make(map[int]*alarm.AlarmDefinition)
+ a.ReadAlarmDefinitionFromJson()
+
app.Resource.InjectRoute("/ric/v1/alarms", a.RaiseAlarm, "POST")
app.Resource.InjectRoute("/ric/v1/alarms", a.ClearAlarm, "DELETE")
app.Resource.InjectRoute("/ric/v1/alarms/active", a.GetActiveAlarms, "GET")
app.Resource.InjectRoute("/ric/v1/alarms/history", a.GetAlarmHistory, "GET")
app.Resource.InjectRoute("/ric/v1/alarms/config", a.SetAlarmConfig, "POST")
app.Resource.InjectRoute("/ric/v1/alarms/config", a.GetAlarmConfig, "GET")
+ app.Resource.InjectRoute("/ric/v1/alarms/define", a.SetAlarmDefinition, "POST")
+ app.Resource.InjectRoute("/ric/v1/alarms/define/{alarmId}", a.DeleteAlarmDefinition, "DELETE")
+ app.Resource.InjectRoute("/ric/v1/alarms/define", a.GetAlarmDefinition, "GET")
+ app.Resource.InjectRoute("/ric/v1/alarms/define/{alarmId}", a.GetAlarmDefinition, "GET")
// Start background timer for re-raising alerts
a.postClear = sdlcheck
diff --git a/manager/cmd/manager_test.go b/manager/cmd/manager_test.go
index 26cf5c5..883b112 100755
--- a/manager/cmd/manager_test.go
+++ b/manager/cmd/manager_test.go
@@ -21,8 +21,13 @@
package main
import (
+ "bytes"
"encoding/json"
"fmt"
+ "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm"
+ "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
+ "github.com/gorilla/mux"
+ "github.com/prometheus/alertmanager/api/v2/models"
"github.com/stretchr/testify/assert"
"io"
"io/ioutil"
@@ -30,13 +35,10 @@
"net/http"
"net/http/httptest"
"os"
+ "strconv"
"strings"
"testing"
"time"
- "github.com/gorilla/mux"
- "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm"
- "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
- "github.com/prometheus/alertmanager/api/v2/models"
)
var alarmManager *AlarmManager
@@ -48,7 +50,7 @@
os.Setenv("ALARM_IF_RMR", "true")
alarmManager = NewAlarmManager("localhost:9093", 500)
go alarmManager.Run(false)
- time.Sleep(time.Duration(2) * time.Second)
+ time.Sleep(time.Duration(10) * time.Second)
// Wait until RMR is up-and-running
for !xapp.Rmr.IsReady() {
@@ -63,7 +65,140 @@
os.Exit(M.Run())
}
+func TestGetPreDefinedAlarmDefinitions(t *testing.T) {
+ xapp.Logger.Info("TestGetPreDefinedAlarmDefinitions")
+ var alarmDefinition alarm.AlarmDefinition
+ req, _ := http.NewRequest("GET", "/ric/v1/alarms/define", nil)
+ vars := map[string]string{"alarmId": strconv.FormatUint(8004, 10)}
+ req = mux.SetURLVars(req, vars)
+ handleFunc := http.HandlerFunc(alarmManager.GetAlarmDefinition)
+ response := executeRequest(req, handleFunc)
+ checkResponseCode(t, http.StatusOK, response.Code)
+ json.NewDecoder(response.Body).Decode(&alarmDefinition)
+ xapp.Logger.Info("alarm definition = %v", alarmDefinition)
+ if alarmDefinition.AlarmId != alarm.RIC_RT_DISTRIBUTION_FAILED || alarmDefinition.AlarmText != "RIC ROUTING TABLE DISTRIBUTION FAILED" {
+ t.Errorf("Incorrect alarm definition")
+ }
+}
+
+func TestSetAlarmDefinitions(t *testing.T) {
+ xapp.Logger.Info("TestSetAlarmDefinitions")
+ var alarm8004Definition alarm.AlarmDefinition
+ alarm8004Definition.AlarmId = alarm.RIC_RT_DISTRIBUTION_FAILED
+ alarm8004Definition.AlarmText = "RIC ROUTING TABLE DISTRIBUTION FAILED"
+ alarm8004Definition.EventType = "Processing error"
+ alarm8004Definition.OperationInstructions = "Not defined"
+
+ var alarm8005Definition alarm.AlarmDefinition
+ alarm8005Definition.AlarmId = alarm.TCP_CONNECTIVITY_LOST_TO_DBAAS
+ alarm8005Definition.AlarmText = "TCP CONNECTIVITY LOST TO DBAAS"
+ alarm8005Definition.EventType = "Communication error"
+ alarm8005Definition.OperationInstructions = "Not defined"
+
+ var alarm8006Definition alarm.AlarmDefinition
+ alarm8006Definition.AlarmId = alarm.E2_CONNECTIVITY_LOST_TO_GNODEB
+ alarm8006Definition.AlarmText = "E2 CONNECTIVITY LOST TO G-NODEB"
+ alarm8006Definition.EventType = "Communication error"
+ alarm8006Definition.OperationInstructions = "Not defined"
+
+ var alarm8007Definition alarm.AlarmDefinition
+ alarm8007Definition.AlarmId = alarm.E2_CONNECTIVITY_LOST_TO_ENODEB
+ alarm8007Definition.AlarmText = "E2 CONNECTIVITY LOST TO E-NODEB"
+ alarm8007Definition.EventType = "Communication error"
+ alarm8007Definition.OperationInstructions = "Not defined"
+
+ var alarm8008Definition alarm.AlarmDefinition
+ alarm8008Definition.AlarmId = alarm.ACTIVE_ALARM_EXCEED_MAX_THRESHOLD
+ alarm8008Definition.AlarmText = "ACTIVE ALARM EXCEED MAX THRESHOLD"
+ alarm8008Definition.EventType = "storage warning"
+ alarm8008Definition.OperationInstructions = "Clear alarms or raise threshold"
+
+ var alarm8009Definition alarm.AlarmDefinition
+ alarm8009Definition.AlarmId = alarm.ALARM_HISTORY_EXCEED_MAX_THRESHOLD
+ alarm8009Definition.AlarmText = "ALARM HISTORY EXCEED MAX THRESHOLD"
+ alarm8009Definition.EventType = "storage warning"
+ alarm8009Definition.OperationInstructions = "Clear alarms or raise threshold"
+
+ pbodyParams := RicAlarmDefinitions{AlarmDefinitions: []*alarm.AlarmDefinition{&alarm8004Definition, &alarm8005Definition, &alarm8006Definition, &alarm8007Definition, &alarm8008Definition, &alarm8009Definition}}
+ pbodyEn, _ := json.Marshal(pbodyParams)
+ req, _ := http.NewRequest("POST", "/ric/v1/alarms/define", bytes.NewBuffer(pbodyEn))
+ handleFunc := http.HandlerFunc(alarmManager.SetAlarmDefinition)
+ response := executeRequest(req, handleFunc)
+ status := checkResponseCode(t, http.StatusOK, response.Code)
+ xapp.Logger.Info("status = %v", status)
+
+}
+
+func TestGetAlarmDefinitions(t *testing.T) {
+ xapp.Logger.Info("TestGetAlarmDefinitions")
+ var alarmDefinition alarm.AlarmDefinition
+ req, _ := http.NewRequest("GET", "/ric/v1/alarms/define", nil)
+ vars := map[string]string{"alarmId": strconv.FormatUint(8004, 10)}
+ req = mux.SetURLVars(req, vars)
+ handleFunc := http.HandlerFunc(alarmManager.GetAlarmDefinition)
+ response := executeRequest(req, handleFunc)
+ checkResponseCode(t, http.StatusOK, response.Code)
+ json.NewDecoder(response.Body).Decode(&alarmDefinition)
+ xapp.Logger.Info("alarm definition = %v", alarmDefinition)
+ if alarmDefinition.AlarmId != alarm.RIC_RT_DISTRIBUTION_FAILED || alarmDefinition.AlarmText != "RIC ROUTING TABLE DISTRIBUTION FAILED" {
+ t.Errorf("Incorrect alarm definition")
+ }
+}
+
+func TestDeleteAlarmDefinitions(t *testing.T) {
+ xapp.Logger.Info("TestDeleteAlarmDefinitions")
+ //Get all
+ var ricAlarmDefinitions RicAlarmDefinitions
+ req, _ := http.NewRequest("GET", "/ric/v1/alarms/define", nil)
+ req = mux.SetURLVars(req, nil)
+ handleFunc := http.HandlerFunc(alarmManager.GetAlarmDefinition)
+ response := executeRequest(req, handleFunc)
+ checkResponseCode(t, http.StatusOK, response.Code)
+ json.NewDecoder(response.Body).Decode(&ricAlarmDefinitions)
+ for _, alarmDefinition := range ricAlarmDefinitions.AlarmDefinitions {
+ xapp.Logger.Info("alarm definition = %v", *alarmDefinition)
+ }
+
+ //Delete 8004
+ req, _ = http.NewRequest("DELETE", "/ric/v1/alarms/define", nil)
+ vars := map[string]string{"alarmId": strconv.FormatUint(8004, 10)}
+ req = mux.SetURLVars(req, vars)
+ handleFunc = http.HandlerFunc(alarmManager.DeleteAlarmDefinition)
+ response = executeRequest(req, handleFunc)
+ checkResponseCode(t, http.StatusOK, response.Code)
+
+ //Get 8004 fail
+ req, _ = http.NewRequest("GET", "/ric/v1/alarms/define", nil)
+ vars = map[string]string{"alarmId": strconv.FormatUint(8004, 10)}
+ req = mux.SetURLVars(req, vars)
+ handleFunc = http.HandlerFunc(alarmManager.GetAlarmDefinition)
+ response = executeRequest(req, handleFunc)
+ checkResponseCode(t, http.StatusBadRequest, response.Code)
+
+ //Set 8004 success
+ var alarm8004Definition alarm.AlarmDefinition
+ alarm8004Definition.AlarmId = alarm.RIC_RT_DISTRIBUTION_FAILED
+ alarm8004Definition.AlarmText = "RIC ROUTING TABLE DISTRIBUTION FAILED"
+ alarm8004Definition.EventType = "Processing error"
+ alarm8004Definition.OperationInstructions = "Not defined"
+ pbodyParams := RicAlarmDefinitions{AlarmDefinitions: []*alarm.AlarmDefinition{&alarm8004Definition}}
+ pbodyEn, _ := json.Marshal(pbodyParams)
+ req, _ = http.NewRequest("POST", "/ric/v1/alarms/define", bytes.NewBuffer(pbodyEn))
+ handleFunc = http.HandlerFunc(alarmManager.SetAlarmDefinition)
+ response = executeRequest(req, handleFunc)
+ checkResponseCode(t, http.StatusOK, response.Code)
+
+ //Get 8004 success
+ req, _ = http.NewRequest("GET", "/ric/v1/alarms/define", nil)
+ vars = map[string]string{"alarmId": strconv.FormatUint(8004, 10)}
+ req = mux.SetURLVars(req, vars)
+ handleFunc = http.HandlerFunc(alarmManager.GetAlarmDefinition)
+ response = executeRequest(req, handleFunc)
+ checkResponseCode(t, http.StatusOK, response.Code)
+}
+
func TestNewAlarmStoredAndPostedSucess(t *testing.T) {
+ xapp.Logger.Info("TestNewAlarmStoredAndPostedSucess")
ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
defer ts.Close()
@@ -74,6 +209,7 @@
}
func TestAlarmClearedSucess(t *testing.T) {
+ xapp.Logger.Info("TestAlarmClearedSucess")
ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
defer ts.Close()
@@ -92,6 +228,7 @@
}
func TestMultipleAlarmsRaisedSucess(t *testing.T) {
+ xapp.Logger.Info("TestMultipleAlarmsRaisedSucess")
ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
defer ts.Close()
@@ -107,6 +244,7 @@
}
func TestMultipleAlarmsClearedSucess(t *testing.T) {
+ xapp.Logger.Info("TestMultipleAlarmsClearedSucess")
ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
defer ts.Close()
@@ -122,6 +260,7 @@
}
func TestAlarmsSuppresedSucess(t *testing.T) {
+ xapp.Logger.Info("TestAlarmsSuppresedSucess")
ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
defer ts.Close()
@@ -134,25 +273,28 @@
assert.Nil(t, alarmer.Clear(a), "clear failed")
}
-
func TestInvalidAlarms(t *testing.T) {
+ xapp.Logger.Info("TestInvalidAlarms")
a := alarmer.NewAlarm(1111, alarm.SeverityMajor, "Some App data", "eth 0 1")
assert.Nil(t, alarmer.Raise(a), "raise failed")
time.Sleep(time.Duration(2) * time.Second)
}
func TestAlarmHandlingErrorCases(t *testing.T) {
+ xapp.Logger.Info("TestAlarmHandlingErrorCases")
ok, err := alarmManager.HandleAlarms(&xapp.RMRParams{})
assert.Equal(t, err.Error(), "unexpected end of JSON input")
assert.Nil(t, ok, "raise failed")
}
func TestConsumeUnknownMessage(t *testing.T) {
+ xapp.Logger.Info("TestConsumeUnknownMessage")
err := alarmManager.Consume(&xapp.RMRParams{})
assert.Nil(t, err, "raise failed")
}
func TestStatusCallback(t *testing.T) {
+ xapp.Logger.Info("TestStatusCallback")
assert.Equal(t, true, alarmManager.StatusCB())
}
@@ -255,4 +397,3 @@
}
return true
}
-
diff --git a/manager/cmd/restapi.go b/manager/cmd/restapi.go
index c455e87..7a09083 100755
--- a/manager/cmd/restapi.go
+++ b/manager/cmd/restapi.go
@@ -22,13 +22,18 @@
import (
"encoding/json"
- "net/http"
- "time"
-
"gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm"
app "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
+ "github.com/gorilla/mux"
+ "net/http"
+ "strconv"
+ "time"
)
+func (a *AlarmManager) respondWithError(w http.ResponseWriter, code int, message string) {
+ a.respondWithJSON(w, code, map[string]string{"error": message})
+}
+
func (a *AlarmManager) respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
@@ -60,6 +65,96 @@
}
}
+func (a *AlarmManager) SetAlarmDefinition(w http.ResponseWriter, r *http.Request) {
+
+ app.Logger.Debug("POST arrived for creating alarm definition ")
+ /* If body is nil then return error */
+ if r.Body == nil {
+ app.Logger.Error("POST - body is empty")
+ a.respondWithError(w, http.StatusBadRequest, "No data in request body.")
+ return
+ }
+ defer r.Body.Close()
+
+ /* Parameters are available. Check if they are valid */
+ var alarmDefinitions RicAlarmDefinitions
+ err := json.NewDecoder(r.Body).Decode(&alarmDefinitions)
+ if err != nil {
+ app.Logger.Error("POST - received alarm definition parameters are invalid - " + err.Error())
+ a.respondWithError(w, http.StatusBadRequest, "Invalid data in request body.")
+ return
+ }
+
+ for _, alarmDefinition := range alarmDefinitions.AlarmDefinitions {
+ _, exists := alarm.RICAlarmDefinitions[alarmDefinition.AlarmId]
+ if exists {
+ app.Logger.Error("POST - alarm definition already exists for %v", alarmDefinition.AlarmId)
+ } else {
+ ricAlarmDefintion := new(alarm.AlarmDefinition)
+ ricAlarmDefintion.AlarmId = alarmDefinition.AlarmId
+ ricAlarmDefintion.AlarmText = alarmDefinition.AlarmText
+ ricAlarmDefintion.EventType = alarmDefinition.EventType
+ ricAlarmDefintion.OperationInstructions = alarmDefinition.OperationInstructions
+ alarm.RICAlarmDefinitions[alarmDefinition.AlarmId] = ricAlarmDefintion
+ }
+ }
+
+ a.respondWithJSON(w, http.StatusOK, nil)
+ return
+}
+
+func (a *AlarmManager) DeleteAlarmDefinition(w http.ResponseWriter, r *http.Request) {
+ pathParams := mux.Vars(r)
+ alarmId, alarmIdok := pathParams["alarmId"]
+ if alarmIdok {
+ if ialarmId, err := strconv.Atoi(alarmId); err == nil {
+ delete(alarm.RICAlarmDefinitions, ialarmId)
+ app.Logger.Debug("DELETE - alarm definition deleted for alarmId %v", ialarmId)
+ } else {
+ app.Logger.Error("DELETE - alarmId string to int conversion failed %v", alarmId)
+ a.respondWithError(w, http.StatusBadRequest, "Invalid path parameter")
+ return
+ }
+ } else {
+ app.Logger.Error("DELETE - alarmId does not exist %v", alarmId)
+ a.respondWithError(w, http.StatusBadRequest, "Invalid path parameter")
+ return
+
+ }
+}
+
+func (a *AlarmManager) GetAlarmDefinition(w http.ResponseWriter, r *http.Request) {
+ var ricAlarmDefinitions RicAlarmDefinitions
+ pathParams := mux.Vars(r)
+ alarmId, alarmIdok := pathParams["alarmId"]
+ if alarmIdok {
+ if ialarmId, err := strconv.Atoi(alarmId); err == nil {
+ alarmDefinition, ok := alarm.RICAlarmDefinitions[ialarmId]
+ if ok {
+ app.Logger.Debug("Successfully returned alarm defintion for alarm id %v", ialarmId)
+ a.respondWithJSON(w, http.StatusOK, alarmDefinition)
+ return
+
+ } else {
+ app.Logger.Error("Requested alarm id not found %v", ialarmId)
+ a.respondWithError(w, http.StatusBadRequest, "Non existent alarmId")
+ return
+ }
+ } else {
+ app.Logger.Error("alarmId string to int conversion failed %v", alarmId)
+ a.respondWithError(w, http.StatusBadRequest, "Invalid alarmId")
+ return
+ }
+ } else {
+ app.Logger.Debug("GET arrived for all alarm definitions ")
+ for _, alarmDefinition := range alarm.RICAlarmDefinitions {
+ ricAlarmDefinitions.AlarmDefinitions = append(ricAlarmDefinitions.AlarmDefinitions, alarmDefinition)
+ }
+ app.Logger.Debug("Successfully returned all alarm definitions")
+ a.respondWithJSON(w, http.StatusOK, ricAlarmDefinitions)
+ }
+}
+
func (a *AlarmManager) doAction(w http.ResponseWriter, r *http.Request, isRaiseAlarm bool) error {
app.Logger.Info("doAction: request received = %t", isRaiseAlarm)
diff --git a/manager/cmd/types.go b/manager/cmd/types.go
index 40a110f..73daf48 100755
--- a/manager/cmd/types.go
+++ b/manager/cmd/types.go
@@ -51,3 +51,6 @@
var Version string
var Hash string
+type RicAlarmDefinitions struct {
+ AlarmDefinitions []*alarm.AlarmDefinition `json:"alarmdefinitions"`
+}