LN0739_FM_FR12: support for options to dynamically create the AlarmDefinitions
- provided options to dynamically configure the AlarmDefinitions
- alarm manager to suppress the alarms if specific problem is unknown to it
(already existign functionality with static AlarmDefinitions ).
- provided CLI interface to define alarm definitions
- provided GET and DELETE interface for alarm definitions
- corrected the run.sh script
- corrections for build failure
Change-Id: I78871f429dda255cd5b56ef69d65ca687c933c93
Signed-off-by: vipin <vipin.mavila@nokia.com>
diff --git a/manager/cmd/manager.go b/manager/cmd/manager.go
index 9a42187..397cd3e 100755
--- a/manager/cmd/manager.go
+++ b/manager/cmd/manager.go
@@ -225,12 +225,17 @@
app.Resource.InjectStatusCb(a.StatusCB)
app.AddConfigChangeListener(a.ConfigChangeCB)
+ alarm.RICAlarmDefinitions = make(map[int]*alarm.AlarmDefinition)
+
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")
// 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..7398969 100755
--- a/manager/cmd/manager_test.go
+++ b/manager/cmd/manager_test.go
@@ -34,6 +34,8 @@
"testing"
"time"
"github.com/gorilla/mux"
+ "strconv"
+ "bytes"
"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"
@@ -63,7 +65,124 @@
os.Exit(M.Run())
}
+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 +193,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 +212,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 +228,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 +244,7 @@
}
func TestAlarmsSuppresedSucess(t *testing.T) {
+ xapp.Logger.Info("TestAlarmsSuppresedSucess")
ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
defer ts.Close()
@@ -136,23 +259,27 @@
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 +382,3 @@
}
return true
}
-
diff --git a/manager/cmd/restapi.go b/manager/cmd/restapi.go
index c455e87..3a18a72 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,95 @@
}
}
+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)
+ } 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"`
+}