LN0739_FM_FR12_EXT: added support for deleting the alarm definitions through cli
An extention to feature LN0739_FM_FR12

Change-Id: Id7a9a514af38d1501c65bfdb97fa66ac01348d0f
Signed-off-by: vipin <vipin.mavila@nokia.com>
diff --git a/alarm/types.go b/alarm/types.go
index ea6651f..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,19 +89,19 @@
 
 // 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  `json:"alarmid"`
-	AlarmText             string  `json:"alarmtext"`
-	EventType             string  `json:"eventtype"`
-	OperationInstructions string  `json:"operationinstructions"`
+	AlarmId               int    `json:"alarmid"`
+	AlarmText             string `json:"alarmtext"`
+	EventType             string `json:"eventtype"`
+	OperationInstructions string `json:"operationinstructions"`
 }
 
 var RICAlarmDefinitions map[int]*AlarmDefinition
diff --git a/cli/alarm-cli.go b/cli/alarm-cli.go
index 0fd5b18..a3ea90c 100755
--- a/cli/alarm-cli.go
+++ b/cli/alarm-cli.go
@@ -10,12 +10,12 @@
 	"net/http"
 	"os"
 	"time"
-
+        "strconv"
 	"gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm"
 )
 
 type CliAlarmDefinitions struct {
-        AlarmDefinitions []*alarm.AlarmDefinition `json:"alarmdefinitions"`
+	AlarmDefinitions []*alarm.AlarmDefinition `json:"alarmdefinitions"`
 }
 
 func main() {
@@ -92,16 +92,26 @@
 	// 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).
+		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)
-                })
+		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)
@@ -214,30 +224,50 @@
 }
 
 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)
+	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
+	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
-        }
+	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 fetch post alarm configuration due to error: %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/manager/cmd/manager.go b/manager/cmd/manager.go
index 8b4ac74..564a2d4 100755
--- a/manager/cmd/manager.go
+++ b/manager/cmd/manager.go
@@ -82,7 +82,7 @@
 
 	// Suppress duplicate alarms
 	idx, found := a.IsMatchFound(m.Alarm)
-	if found && m.AlarmAction == alarm.AlarmActionRaise  {
+	if found && m.AlarmAction == alarm.AlarmActionRaise {
 		app.Logger.Info("Duplicate alarm found, suppressing ...")
 		if m.PerceivedSeverity == a.activeAlarms[idx].PerceivedSeverity {
 			// Duplicate with same severity found
@@ -93,7 +93,6 @@
 		}
 	}
 
-
 	// Clear alarm if found from active alarm list
 	if m.AlarmAction == alarm.AlarmActionClear {
 		if found {
@@ -243,6 +242,7 @@
 	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 7398969..838de79 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,15 +35,10 @@
 	"net/http"
 	"net/http/httptest"
 	"os"
+	"strconv"
 	"strings"
 	"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"
 )
 
 var alarmManager *AlarmManager
@@ -145,11 +145,11 @@
 
 	//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)
+	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)
@@ -257,7 +257,6 @@
 	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")
diff --git a/manager/cmd/restapi.go b/manager/cmd/restapi.go
index 3a18a72..7a09083 100755
--- a/manager/cmd/restapi.go
+++ b/manager/cmd/restapi.go
@@ -109,6 +109,7 @@
 	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")