[SEBA-854] Add new API and modify existing
Change-Id: Iea9fd003d122f58a4d45c79d806a2011087d1740
diff --git a/demo_test/test.go b/demo_test/test.go
index 70c2df6..05582ad 100644
--- a/demo_test/test.go
+++ b/demo_test/test.go
@@ -15,73 +15,61 @@
package main
import (
- "fmt"
- "net"
- "bufio"
- "os"
- "os/signal"
- "os/exec"
- "github.com/Shopify/sarama"
- "google.golang.org/grpc"
- "golang.org/x/net/context"
- importer "./proto"
- log "github.com/Sirupsen/logrus"
+ importer "./proto"
+ "bufio"
"bytes"
+ "crypto/tls"
+ "fmt"
+ "github.com/Shopify/sarama"
+ log "github.com/Sirupsen/logrus"
+ "golang.org/x/net/context"
+ "google.golang.org/grpc"
+ "google.golang.org/grpc/status"
+ "net"
+ "net/http"
+ "os"
+ "os/exec"
+ "os/signal"
+ "strconv"
"strings"
- "net/http"
- "crypto/tls"
- "strconv"
)
-var REDFISH_ROOT = "/redfish/v1"
-var CONTENT_TYPE = "application/json"
+var REDFISH_ROOT = "/redfish/v1"
+var CONTENT_TYPE = "application/json"
var EVENTS_MAP = map[string]string{
-"add":"ResourceAdded",
-"rm":"ResourceRemoved",
-"alert":"Alert",
-"update":"Update"}
+ "add": "ResourceAdded",
+ "rm": "ResourceRemoved",
+ "alert": "Alert",
+ "update": "Update"}
-var default_address string = "localhost:31085"
-var default_port string = "8888"
-var default_vendor string = "edgecore"
-var default_freq uint64 = 180
-var attach_device_ip string = ""
-var importerTopic = "importer"
+var default_address string = "localhost:31085"
+var default_port string = "8888"
+var default_vendor string = "edgecore"
+var default_freq uint64 = 180
+var attach_device_ip string = ""
+var importerTopic = "importer"
var DataConsumer sarama.Consumer
-var cc importer.DeviceManagementClient
-var ctx context.Context
-var conn * grpc.ClientConn
-
-type Device struct {
- deviceinfo * importer.DeviceInfo
- eventlist * importer.EventList
-}
-
-var devicemap map[string]* Device
+var cc importer.DeviceManagementClient
+var ctx context.Context
+var conn *grpc.ClientConn
/*///////////////////////////////////////////////////////////////////////*/
// Allows user to register the device for data collection and frequency.
//
//
/*///////////////////////////////////////////////////////////////////////*/
-func (s * Device) Attach(aip string, avendor string, afreq uint32) (error, string) {
- fmt.Println("Received Attach\n")
- var default_protocol string = "https"
+func Attach(aip string, avendor string, afreq uint32) error {
+ fmt.Println("Received Attach\n")
+ var default_protocol string = "https"
+ deviceinfo := new(importer.DeviceInfo)
+ deviceinfo.IpAddress = aip
+ deviceinfo.Vendor = avendor
+ deviceinfo.Frequency = afreq
+ deviceinfo.Protocol = default_protocol
+ _, err := cc.SendDeviceInfo(ctx, deviceinfo)
- s.deviceinfo = new(importer.DeviceInfo)
- s.eventlist = new(importer.EventList)
- s.deviceinfo.IpAddress = aip
- s.deviceinfo.Vendor = avendor
- s.deviceinfo.Frequency = afreq
- s.deviceinfo.Protocol = default_protocol
- _, err := cc.SendDeviceInfo(ctx, s.deviceinfo)
-
- if err != nil {
- return err ,"attach error!!"
- }else{
- return nil,""
- }
+ return err
}
/*///////////////////////////////////////////////////////////////////////*/
@@ -89,16 +77,14 @@
//
//
/*///////////////////////////////////////////////////////////////////////*/
-func (s * Device) UpdateFreq(wd uint32)(error, string) {
- fmt.Println("Received Period\n")
- s.deviceinfo.Frequency = wd
- _, err := cc.SetFrequency(ctx, s.deviceinfo)
+func UpdateFreq(ip_address string, wd uint32) error {
+ fmt.Println("Received Period\n")
+ freqinfo := new(importer.FreqInfo)
+ freqinfo.Frequency = wd
+ freqinfo.IpAddress = ip_address
+ _, err := cc.SetFrequency(ctx, freqinfo)
- if err != nil {
- return err, "period error!!"
- }else{
- return nil,""
- }
+ return err
}
/*///////////////////////////////////////////////////////////////////////*/
@@ -106,17 +92,14 @@
//
//
/*///////////////////////////////////////////////////////////////////////*/
-func (s * Device) Subscribe(eventlist []string) (error, string) {
- fmt.Println("Received Subscribe\n")
- s.eventlist.Events = eventlist
- s.eventlist.EventIpAddress = s.deviceinfo.IpAddress
- _, err := cc.SubsrcribeGivenEvents(ctx, s.eventlist)
+func Subscribe(ip_address string, Giveneventlist []string) error {
+ fmt.Println("Received Subscribe\n")
+ giveneventlist := new(importer.GivenEventList)
+ giveneventlist.Events = Giveneventlist
+ giveneventlist.EventIpAddress = ip_address
+ _, err := cc.SubsrcribeGivenEvents(ctx, giveneventlist)
- if err != nil {
- return err, "sub error!!"
- }else{
- return nil,""
- }
+ return err
}
/*///////////////////////////////////////////////////////////////////////*/
@@ -124,17 +107,14 @@
//
//
/*///////////////////////////////////////////////////////////////////////*/
-func (s * Device) UnSubscribe(eventlist []string) (error, string) {
- fmt.Println("Received UnSubscribe\n")
- s.eventlist.Events = eventlist
- s.eventlist.EventIpAddress = s.deviceinfo.IpAddress
- _, err := cc.UnSubsrcribeGivenEvents(ctx, s.eventlist)
+func UnSubscribe(ip_address string, Giveneventlist []string) error {
+ fmt.Println("Received UnSubscribe\n")
+ giveneventlist := new(importer.GivenEventList)
+ giveneventlist.Events = Giveneventlist
+ giveneventlist.EventIpAddress = ip_address
+ _, err := cc.UnSubsrcribeGivenEvents(ctx, giveneventlist)
- if err != nil {
- return err, "unsub error!!"
- }else{
- return nil,""
- }
+ return err
}
/*///////////////////////////////////////////////////////////////////////*/
@@ -142,23 +122,50 @@
//
//
/*///////////////////////////////////////////////////////////////////////*/
-func (s * Device) GetEventSupportList() (error, []string) {
- fmt.Println("Received GetEventSupportList\n")
- var ret_msg * importer.SupportedEventList
- ret_msg, err :=cc.GetEventList(ctx, devicemap[s.deviceinfo.IpAddress].deviceinfo);
- if err != nil {
- return err,ret_msg.Events
- }else{
- fmt.Println("show all event subs:", ret_msg)
- return nil , ret_msg.Events
- }
+func GetEventSupportList(vendor string) (error, []string) {
+ fmt.Println("Received GetEventSupportList\n")
+ vendorinfo := new(importer.VendorInfo)
+ vendorinfo.Vendor = vendor
+ var ret_msg *importer.EventList
+ ret_msg, err := cc.GetEventList(ctx, vendorinfo)
+
+ return err, ret_msg.Events
+}
+
+/*///////////////////////////////////////////////////////////////////////*/
+// Allows user to get the current events subscribed by device
+//
+//
+/*///////////////////////////////////////////////////////////////////////*/
+func GetEventCurrentDeviceList(ip_address string) (error, []string) {
+ fmt.Println("Received GetEventCurrentDeviceList\n")
+ currentdeviceinfo := new(importer.Device)
+ currentdeviceinfo.IpAddress = ip_address
+ var ret_msg *importer.EventList
+ ret_msg, err := cc.GetCurrentEventList(ctx, currentdeviceinfo)
+
+ return err, ret_msg.Events
+}
+
+/*///////////////////////////////////////////////////////////////////////*/
+// Allows user to get the current events subscribed by device
+//
+//
+/*///////////////////////////////////////////////////////////////////////*/
+func ClearCurrentDeviceEventList(ip_address string) error {
+ fmt.Println("Received ClearCurrentDeviceEventList\n")
+ currentdeviceinfo := new(importer.Device)
+ currentdeviceinfo.IpAddress = ip_address
+ _, err := cc.ClearCurrentEventList(ctx, currentdeviceinfo)
+
+ return err
}
func init() {
- Formatter := new(log.TextFormatter)
- Formatter.TimestampFormat = "02-01-2006 15:04:05"
- Formatter.FullTimestamp = true
- log.SetFormatter(Formatter)
+ Formatter := new(log.TextFormatter)
+ Formatter.TimestampFormat = "02-01-2006 15:04:05"
+ Formatter.FullTimestamp = true
+ log.SetFormatter(Formatter)
}
func topicListener(topic *string, master sarama.Consumer) {
@@ -180,7 +187,7 @@
log.Info("Got message on topic=[%s]: %s", *topic, string(msg.Value))
case <-signals:
log.Warn("Interrupt is detected")
- os.Exit(1)
+ os.Exit(1)
}
}
}()
@@ -188,7 +195,7 @@
}
func kafkainit() {
- cmd := exec.Command("/bin/sh","kafka_ip.sh")
+ cmd := exec.Command("/bin/sh", "kafka_ip.sh")
var kafkaIP string
var out bytes.Buffer
cmd.Stdout = &out
@@ -200,8 +207,8 @@
kafkaIP = out.String()
kafkaIP = strings.TrimSuffix(kafkaIP, "\n")
- kafkaIP = kafkaIP +":9092"
- fmt.Println("IP address of kafka-cord-0:",kafkaIP)
+ kafkaIP = kafkaIP + ":9092"
+ fmt.Println("IP address of kafka-cord-0:", kafkaIP)
config := sarama.NewConfig()
config.Consumer.Return.Errors = true
master, err := sarama.NewConsumer([]string{kafkaIP}, config)
@@ -219,21 +226,21 @@
kafkainit()
ln, err := net.Listen("tcp", ":9999")
- if err != nil {
- fmt.Println("could not listen")
- log.Fatal("did not listen: %v", err)
- }
+ if err != nil {
+ fmt.Println("could not listen")
+ log.Fatal("did not listen: %v", err)
+ }
defer ln.Close()
connS, err := ln.Accept()
if err != nil {
- fmt.Println("Accept error")
- log.Fatal("Accept error: %v", err)
- }else{
+ fmt.Println("Accept error")
+ log.Fatal("Accept error: %v", err)
+ } else {
conn, err = grpc.Dial(default_address, grpc.WithInsecure())
if err != nil {
- fmt.Println("could not connect")
+ fmt.Println("could not connect")
log.Fatal("did not connect: %v", err)
}
defer conn.Close()
@@ -241,7 +248,6 @@
cc = importer.NewDeviceManagementClient(conn)
ctx = context.Background()
- devicemap = make(map[string] *Device)
loop := true
for loop == true {
@@ -254,153 +260,193 @@
switch string(cmd) {
- case "attach" :
+ case "attach":
cmd_size := len(s)
- var err error
- var aport string = default_port
- var avendor string = default_vendor
- var uafreq uint64 = default_freq
+ var err error
+ var uafreq uint64
+ if cmd_size == 5 {
+ aip := s[1]
+ aport := s[2]
+ avendor := s[3]
+ afreq := s[4]
+ uafreq, err = strconv.ParseUint(afreq, 10, 64)
- if (cmd_size == 2 || cmd_size == 5){
- aip := s[1]
- if(cmd_size == 5){
- aport = s[2]
- avendor = s[3]
- afreq := s[4]
- uafreq, err = strconv.ParseUint(afreq, 10, 64)
-
- if err != nil {
- fmt.Print("ParseUint error!!")
- }
-
- attach_device_ip = aip + ":" + aport
- }else{
- attach_device_ip = aip + ":" + default_port
+ if err != nil {
+ fmt.Print("ParseUint error!!\n")
}
- if (devicemap[attach_device_ip] == nil){
- dev := new (Device)
- err, newmessage = dev.Attach(attach_device_ip, avendor, uint32(uafreq))
- if err != nil {
- fmt.Print("attach error!!")
- }else{
- fmt.Print("attatch IP:", attach_device_ip)
- newmessage = attach_device_ip
- devicemap[attach_device_ip] = dev
- }
- }else{
- fmt.Print("Change attach IP to %v", attach_device_ip)
+ attach_device_ip = aip + ":" + aport
+
+ err = Attach(attach_device_ip, avendor, uint32(uafreq))
+ if err != nil {
+ errStatus, _ := status.FromError(err)
+ fmt.Println(errStatus.Message())
+ fmt.Println(errStatus.Code())
+ fmt.Print("attach error!!\n")
+ newmessage = errStatus.Message()
+
+ } else {
+ fmt.Print("attatch IP:\n", attach_device_ip)
newmessage = attach_device_ip
}
- }else{
- fmt.Print("Need IP address !!")
+ } else {
+ fmt.Print("Need IP addres,port,vendor,freqs !!\n")
newmessage = "Need IP address !!"
}
- break
- case "period" :
+ case "period":
cmd_size := len(s)
- if (cmd_size == 2 ){
- if (devicemap[attach_device_ip] != nil){
- pv := s[1]
- fmt.Print("pv:", pv)
- u, err := strconv.ParseUint(pv, 10, 64)
+ fmt.Print("cmd_size period %d", cmd_size)
+ if cmd_size == 4 {
+ fip := s[1]
+ fport := s[2]
+ pv := s[3]
+ fmt.Print("pv:", pv)
+ u, err := strconv.ParseUint(pv, 10, 64)
+
+ if err != nil {
+ fmt.Print("ParseUint error!!\n")
+ } else {
+ wd := uint32(u)
+ ip_address := fip + ":" + fport
+ err = UpdateFreq(ip_address, wd)
if err != nil {
- fmt.Print("ParseUint error!!")
- }else{
- wd := uint32(u)
- dev := devicemap[attach_device_ip]
- err, newmessage = dev.UpdateFreq(wd)
-
- if err != nil {
- fmt.Print("period error!!")
- }else{
- newmessage = strings.ToUpper(cmd)
- }
+ errStatus, _ := status.FromError(err)
+ fmt.Println(errStatus.Message())
+ fmt.Println(errStatus.Code())
+ newmessage = errStatus.Message()
+ fmt.Print("period error!!\n")
+ } else {
+ newmessage = strings.ToUpper(cmd)
}
- }else{
- fmt.Print("need attach first!!")
- newmessage = "need attach first!!"
}
- }else{
- fmt.Print("Need period value !!")
+ } else {
+ fmt.Print("Need period value !!\n")
newmessage = "Need period value !!"
}
- break
-
- case "sub","unsub" :
+ case "sub", "unsub":
cmd_size := len(s)
- fmt.Print("cmd is :", cmd)
- if(cmd_size > 4 || cmd_size <0){
+ fmt.Print("cmd is :", cmd, cmd_size)
+ if cmd_size > 6 || cmd_size < 0 {
fmt.Print("error event !!")
newmessage = "error event !!"
- }else{
+ } else {
+ ip := s[1]
+ port := s[2]
+ ip_address := ip + ":" + port
var events_list []string
- for i := 1; i < cmd_size; i++ {
+ for i := 3; i < cmd_size; i++ {
if value, ok := EVENTS_MAP[s[i]]; ok {
- events_list = append(events_list,value)
+ events_list = append(events_list, value)
} else {
fmt.Println("key not found")
}
}
- if (devicemap[attach_device_ip] != nil){
- dev := devicemap[attach_device_ip]
- if(string(cmd) == "sub"){
- err, newmessage = dev.Subscribe(events_list)
- if err != nil {
- fmt.Print("sub error!!")
- newmessage = "sub error!!"
- }else{
- newmessage = strings.ToUpper(cmd)
- }
- }else{
- err, newmessage = dev.UnSubscribe(events_list)
- if err != nil {
- fmt.Print("unsub error!!")
- newmessage = "unsub error!!"
- }else{
- newmessage = strings.ToUpper(cmd)
- }
+ if string(cmd) == "sub" {
+ err = Subscribe(ip_address, events_list)
+ if err != nil {
+ errStatus, _ := status.FromError(err)
+ fmt.Println(errStatus.Message())
+ fmt.Println(errStatus.Code())
+ newmessage = errStatus.Message()
+ fmt.Print("sub error!!")
+ } else {
+ newmessage = strings.ToUpper(cmd)
}
- }else{
- fmt.Print("need attach first !!")
- newmessage = "need attach first !!"
+ } else {
+ err = UnSubscribe(ip_address, events_list)
+ if err != nil {
+ errStatus, _ := status.FromError(err)
+ fmt.Println(errStatus.Message())
+ fmt.Println(errStatus.Code())
+ newmessage = errStatus.Message()
+ fmt.Print("unsub error!!")
+ } else {
+ newmessage = strings.ToUpper(cmd)
+ }
}
}
- break
- case "showeventlist" :
- if (devicemap[attach_device_ip] != nil){
- dev := devicemap[attach_device_ip]
- err, supportlist := dev.GetEventSupportList()
+ case "showeventlist":
+ cmd_size := len(s)
+ fmt.Print("cmd is :", cmd, cmd_size)
+ if cmd_size > 3 || cmd_size < 0 {
+ fmt.Print("error event !!")
+ newmessage = "error event !!"
+ } else {
+ vendor := s[1]
+ err, supportlist := GetEventSupportList(vendor)
if err != nil {
+ errStatus, _ := status.FromError(err)
+ fmt.Println(errStatus.Message())
+ fmt.Println(errStatus.Code())
+ newmessage = errStatus.Message()
fmt.Print("showeventlist error!!")
- }else{
+ } else {
fmt.Print("showeventlist ", supportlist)
- newmessage = strings.Join(supportlist[:],",")
+ newmessage = strings.Join(supportlist[:], ",")
}
- }else{
- fmt.Print("need attach first !!")
- newmessage = "need attach first !!"
}
- break
+ case "showdeviceeventlist":
+ cmd_size := len(s)
+ fmt.Print("cmd is :", cmd, cmd_size)
+ if cmd_size > 4 || cmd_size < 0 {
+ fmt.Print("error event !!")
+ newmessage = "error event !!"
+ } else {
+ eip := s[1]
+ eport := s[2]
+ ip_address := eip + ":" + eport
+ err, currentlist := GetEventCurrentDeviceList(ip_address)
- case "QUIT" :
+ if err != nil {
+ errStatus, _ := status.FromError(err)
+ fmt.Println(errStatus.Message())
+ fmt.Println(errStatus.Code())
+ newmessage = errStatus.Message()
+ fmt.Print("showdeviceeventlist error!!")
+ } else {
+ fmt.Print("showeventlist ", currentlist)
+ newmessage = strings.Join(currentlist[:], ",")
+ }
+ }
+
+ case "cleardeviceeventlist":
+ cmd_size := len(s)
+ fmt.Print("cmd is :", cmd, cmd_size)
+ if cmd_size > 4 || cmd_size < 0 {
+ fmt.Print("error event !!")
+ newmessage = "error event !!"
+ } else {
+ clip := s[1]
+ clport := s[2]
+ ip_address := clip + ":" + clport
+ err = ClearCurrentDeviceEventList(ip_address)
+ if err != nil {
+ errStatus, _ := status.FromError(err)
+ fmt.Println(errStatus.Message())
+ fmt.Println(errStatus.Code())
+ newmessage = errStatus.Message()
+ fmt.Print("cleardeviceeventlist error!!")
+ } else {
+ newmessage = strings.ToUpper(cmd)
+ }
+ }
+
+ case "QUIT":
loop = false
- newmessage="QUIT"
- break
+ newmessage = "QUIT"
- default :
- break
+ default:
}
// send string back to client
connS.Write([]byte(newmessage + "\n"))
- }
+ }
}
}