[SEBA-627] Add more test case of testing of importer
Change-Id: Ief2671f7ba1d4855066a8cc28d41b8e3b5f6983f
diff --git a/demo_test/test.go b/demo_test/test.go
index b0502ac..70c2df6 100644
--- a/demo_test/test.go
+++ b/demo_test/test.go
@@ -16,6 +16,8 @@
import (
"fmt"
+ "net"
+ "bufio"
"os"
"os/signal"
"os/exec"
@@ -24,23 +26,134 @@
"golang.org/x/net/context"
importer "./proto"
log "github.com/Sirupsen/logrus"
- "time"
"bytes"
"strings"
-
+ "net/http"
+ "crypto/tls"
+ "strconv"
)
-const (
- address = "localhost:31085"
- vendor = "edgecore"
-// device_ip = "192.168.3.44:9888"
- device_ip = "192.168.4.27:8888"
- protocol = "https"
- freq = 180
-)
-var importerTopic = "importer"
+var REDFISH_ROOT = "/redfish/v1"
+var CONTENT_TYPE = "application/json"
+var EVENTS_MAP = map[string]string{
+"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 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
+
+/*///////////////////////////////////////////////////////////////////////*/
+// 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"
+
+ 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,""
+ }
+}
+
+/*///////////////////////////////////////////////////////////////////////*/
+// Allows user to change the frequency of data collection
+//
+//
+/*///////////////////////////////////////////////////////////////////////*/
+func (s * Device) UpdateFreq(wd uint32)(error, string) {
+ fmt.Println("Received Period\n")
+ s.deviceinfo.Frequency = wd
+ _, err := cc.SetFrequency(ctx, s.deviceinfo)
+
+ if err != nil {
+ return err, "period error!!"
+ }else{
+ return nil,""
+ }
+}
+
+/*///////////////////////////////////////////////////////////////////////*/
+// Allows user to unsubscribe events
+//
+//
+/*///////////////////////////////////////////////////////////////////////*/
+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)
+
+ if err != nil {
+ return err, "sub error!!"
+ }else{
+ return nil,""
+ }
+}
+
+/*///////////////////////////////////////////////////////////////////////*/
+// Allows user to unsubscribe events
+//
+//
+/*///////////////////////////////////////////////////////////////////////*/
+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)
+
+ if err != nil {
+ return err, "unsub error!!"
+ }else{
+ return nil,""
+ }
+}
+
+/*///////////////////////////////////////////////////////////////////////*/
+// Allows user to get the events supported by device
+//
+//
+/*///////////////////////////////////////////////////////////////////////*/
+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 init() {
Formatter := new(log.TextFormatter)
Formatter.TimestampFormat = "02-01-2006 15:04:05"
@@ -67,7 +180,7 @@
log.Info("Got message on topic=[%s]: %s", *topic, string(msg.Value))
case <-signals:
log.Warn("Interrupt is detected")
- doneCh <- struct{}{}
+ os.Exit(1)
}
}
}()
@@ -100,37 +213,194 @@
go topicListener(&importerTopic, master)
}
func main() {
+ http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
+ fmt.Println("Launching server...")
log.Info("kafkaInit starting")
kafkainit()
- // Set up a connection to the server.
- fmt.Println("Starting connection")
- conn, err := grpc.Dial(address, grpc.WithInsecure())
- if err != nil {
- fmt.Println("could not connect")
- log.Fatal("did not connect: %v", err)
- }
- defer conn.Close()
- c := importer.NewDeviceManagementClient(conn)
- ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
- defer cancel()
- deviceinfo := new(importer.DeviceInfo)
- deviceinfo.IpAddress = device_ip
- deviceinfo.Vendor = vendor
- deviceinfo.Frequency = freq
- deviceinfo.Protocol = protocol
- _, err = c.SendDeviceInfo(ctx, deviceinfo)
- if err != nil {
- log.Fatal("could not SendDeviceInfo: %v", err)
- }
- quit := make(chan os.Signal)
- signal.Notify(quit, os.Interrupt)
-
- select {
- case sig := <-quit:
- fmt.Println("Shutting down:", sig)
- DataConsumer.Close()
- panic(err)
+ ln, err := net.Listen("tcp", ":9999")
+ 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{
+
+ conn, err = grpc.Dial(default_address, grpc.WithInsecure())
+ if err != nil {
+ fmt.Println("could not connect")
+ log.Fatal("did not connect: %v", err)
+ }
+ defer conn.Close()
+
+ cc = importer.NewDeviceManagementClient(conn)
+ ctx = context.Background()
+
+ devicemap = make(map[string] *Device)
+ loop := true
+
+ for loop == true {
+ cmd, _ := bufio.NewReader(connS).ReadString('\n')
+
+ cmd = strings.TrimSuffix(cmd, "\n")
+ s := strings.Split(cmd, ":")
+ newmessage := "cmd error!!"
+ cmd = s[0]
+
+ switch string(cmd) {
+
+ case "attach" :
+ cmd_size := len(s)
+ var err error
+ var aport string = default_port
+ var avendor string = default_vendor
+ var uafreq uint64 = default_freq
+
+ 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 (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)
+ newmessage = attach_device_ip
+ }
+ }else{
+ fmt.Print("Need IP address !!")
+ newmessage = "Need IP address !!"
+
+ }
+ break
+
+ 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)
+
+ 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)
+ }
+ }
+ }else{
+ fmt.Print("need attach first!!")
+ newmessage = "need attach first!!"
+ }
+ }else{
+ fmt.Print("Need period value !!")
+ newmessage = "Need period value !!"
+ }
+
+ break
+
+ case "sub","unsub" :
+ cmd_size := len(s)
+ fmt.Print("cmd is :", cmd)
+ if(cmd_size > 4 || cmd_size <0){
+ fmt.Print("error event !!")
+ newmessage = "error event !!"
+ }else{
+ var events_list []string
+ for i := 1; i < cmd_size; i++ {
+ if value, ok := EVENTS_MAP[s[i]]; ok {
+ 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)
+ }
+ }
+ }else{
+ fmt.Print("need attach first !!")
+ newmessage = "need attach first !!"
+ }
+ }
+ break
+
+ case "showeventlist" :
+ if (devicemap[attach_device_ip] != nil){
+ dev := devicemap[attach_device_ip]
+ err, supportlist := dev.GetEventSupportList()
+
+ if err != nil {
+ fmt.Print("showeventlist error!!")
+ }else{
+ fmt.Print("showeventlist ", supportlist)
+ newmessage = strings.Join(supportlist[:],",")
+ }
+ }else{
+ fmt.Print("need attach first !!")
+ newmessage = "need attach first !!"
+ }
+
+ break
+
+ case "QUIT" :
+ loop = false
+ newmessage="QUIT"
+ break
+
+ default :
+ break
+ }
+ // send string back to client
+ connS.Write([]byte(newmessage + "\n"))
+ }
+ }
}