Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 1 | // Copyright 2018 Open Networking Foundation |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 18 | "bufio" |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 19 | "bytes" |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 20 | "crypto/tls" |
| 21 | "fmt" |
| 22 | "github.com/Shopify/sarama" |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 23 | "github.com/opencord/device-management/demo_test/proto" |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 24 | logrus "github.com/sirupsen/logrus" |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 25 | "golang.org/x/net/context" |
| 26 | "google.golang.org/grpc" |
| 27 | "google.golang.org/grpc/status" |
| 28 | "net" |
| 29 | "net/http" |
| 30 | "os" |
| 31 | "os/exec" |
| 32 | "os/signal" |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 33 | "sort" |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 34 | "strconv" |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 35 | "strings" |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 36 | ) |
| 37 | |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 38 | var EVENTS_MAP = map[string]string{ |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 39 | "add": "ResourceAdded", |
| 40 | "rm": "ResourceRemoved", |
| 41 | "alert": "Alert", |
| 42 | "update": "Update"} |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 43 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 44 | var default_address string = "localhost:31085" |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 45 | var importerTopic = "importer" |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 46 | var DataConsumer sarama.Consumer |
| 47 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 48 | var cc importer.DeviceManagementClient |
| 49 | var ctx context.Context |
| 50 | var conn *grpc.ClientConn |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 51 | |
dileepbk | 86ef010 | 2019-11-13 00:08:33 +0000 | [diff] [blame] | 52 | func GetCurrentDevices() (error, []string) { |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 53 | logrus.Info("Testing GetCurrentDevices") |
dileepbk | 86ef010 | 2019-11-13 00:08:33 +0000 | [diff] [blame] | 54 | empty := new(importer.Empty) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 55 | var ret_msg *importer.DeviceListByIp |
dileepbk | 86ef010 | 2019-11-13 00:08:33 +0000 | [diff] [blame] | 56 | ret_msg, err := cc.GetCurrentDevices(ctx, empty) |
| 57 | if err != nil { |
| 58 | return err, nil |
| 59 | } else { |
| 60 | return err, ret_msg.Ip |
| 61 | } |
| 62 | } |
| 63 | |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 64 | func init() { |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 65 | Formatter := new(logrus.TextFormatter) |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 66 | Formatter.TimestampFormat = "02-01-2006 15:04:05" |
| 67 | Formatter.FullTimestamp = true |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 68 | logrus.SetFormatter(Formatter) |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | func topicListener(topic *string, master sarama.Consumer) { |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 72 | logrus.Info("Starting topicListener for ", *topic) |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 73 | consumer, err := master.ConsumePartition(*topic, 0, sarama.OffsetOldest) |
| 74 | if err != nil { |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 75 | logrus.Errorf("topicListener panic, topic=[%s]: %s", *topic, err.Error()) |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 76 | os.Exit(1) |
| 77 | } |
| 78 | signals := make(chan os.Signal, 1) |
| 79 | signal.Notify(signals, os.Interrupt) |
| 80 | doneCh := make(chan struct{}) |
| 81 | go func() { |
| 82 | for { |
| 83 | select { |
| 84 | case err := <-consumer.Errors(): |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 85 | logrus.Errorf("Consumer error: %s", err.Err) |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 86 | case msg := <-consumer.Messages(): |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 87 | logrus.Infof("Got message on topic=[%s]: %s", *topic, string(msg.Value)) |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 88 | case <-signals: |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 89 | logrus.Warn("Interrupt is detected") |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 90 | os.Exit(1) |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | }() |
| 94 | <-doneCh |
| 95 | } |
| 96 | |
| 97 | func kafkainit() { |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 98 | cmd := exec.Command("/bin/sh", "kafka_ip.sh") |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 99 | var kafkaIP string |
| 100 | var out bytes.Buffer |
| 101 | cmd.Stdout = &out |
| 102 | err := cmd.Run() |
| 103 | if err != nil { |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 104 | logrus.Info(err) |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 105 | os.Exit(1) |
| 106 | } |
| 107 | |
| 108 | kafkaIP = out.String() |
| 109 | kafkaIP = strings.TrimSuffix(kafkaIP, "\n") |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 110 | kafkaIP = kafkaIP + ":9092" |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 111 | logrus.Infof("IP address of kafka-cord-0:%s", kafkaIP) |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 112 | config := sarama.NewConfig() |
| 113 | config.Consumer.Return.Errors = true |
| 114 | master, err := sarama.NewConsumer([]string{kafkaIP}, config) |
| 115 | if err != nil { |
| 116 | panic(err) |
| 117 | } |
| 118 | DataConsumer = master |
| 119 | |
| 120 | go topicListener(&importerTopic, master) |
| 121 | } |
| 122 | func main() { |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 123 | http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 124 | logrus.Info("Launching server...") |
| 125 | logrus.Info("kafkaInit starting") |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 126 | kafkainit() |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 127 | |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 128 | ln, err := net.Listen("tcp", ":9999") |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 129 | if err != nil { |
| 130 | fmt.Println("could not listen") |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 131 | logrus.Fatalf("did not listen: %v", err) |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 132 | } |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 133 | defer ln.Close() |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 134 | |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 135 | conn, err = grpc.Dial(default_address, grpc.WithInsecure()) |
| 136 | if err != nil { |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 137 | logrus.Fatalf("did not connect: %v", err) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 138 | } |
| 139 | defer conn.Close() |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 140 | |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 141 | cc = importer.NewDeviceManagementClient(conn) |
| 142 | ctx = context.Background() |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 143 | |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 144 | loop := true |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 145 | |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 146 | for loop { |
mc | cd7e950 | 2019-12-16 22:04:13 +0000 | [diff] [blame] | 147 | connS, err := ln.Accept() |
| 148 | if err != nil { |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 149 | logrus.Fatalf("Accept error: %v", err) |
mc | cd7e950 | 2019-12-16 22:04:13 +0000 | [diff] [blame] | 150 | } |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 151 | cmdstr, _ := bufio.NewReader(connS).ReadString('\n') |
| 152 | cmdstr = strings.TrimSuffix(cmdstr, "\n") |
| 153 | s := strings.Split(cmdstr, " ") |
| 154 | newmessage := "" |
| 155 | cmd := string(s[0]) |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 156 | |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 157 | switch cmd { |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 158 | |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 159 | case "attach": |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 160 | if len(s) < 2 { |
mc | cd7e950 | 2019-12-16 22:04:13 +0000 | [diff] [blame] | 161 | newmessage = newmessage + "invalid command length" + cmdstr + "\n" |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 162 | break |
| 163 | } |
| 164 | var devicelist importer.DeviceList |
| 165 | var ipattached []string |
| 166 | for _, devinfo := range s[1:] { |
| 167 | info := strings.Split(devinfo, ":") |
| 168 | if len(info) != 3 { |
| 169 | newmessage = newmessage + "invalid command " + devinfo + "\n" |
| 170 | continue |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 171 | } |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 172 | deviceinfo := new(importer.DeviceInfo) |
| 173 | deviceinfo.IpAddress = info[0] + ":" + info[1] |
| 174 | freq, err := strconv.ParseUint(info[2], 10, 32) |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 175 | if err != nil { |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 176 | newmessage = newmessage + "invalid command " + devinfo + "\n" |
| 177 | continue |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 178 | } |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 179 | deviceinfo.Frequency = uint32(freq) |
| 180 | devicelist.Device = append(devicelist.Device, deviceinfo) |
| 181 | ipattached = append(ipattached, deviceinfo.IpAddress) |
| 182 | } |
mc | cd7e950 | 2019-12-16 22:04:13 +0000 | [diff] [blame] | 183 | if len(devicelist.Device) == 0 { |
| 184 | break |
| 185 | } |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 186 | _, err := cc.SendDeviceList(ctx, &devicelist) |
| 187 | if err != nil { |
| 188 | errStatus, _ := status.FromError(err) |
mc | cd7e950 | 2019-12-16 22:04:13 +0000 | [diff] [blame] | 189 | newmessage = newmessage + errStatus.Message() |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 190 | logrus.Errorf("attach error - status code %v message %v", errStatus.Code(), errStatus.Message()) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 191 | } else { |
mc | cd7e950 | 2019-12-16 22:04:13 +0000 | [diff] [blame] | 192 | sort.Strings(ipattached) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 193 | ips := strings.Join(ipattached, " ") |
| 194 | newmessage = newmessage + ips + " attached\n" |
| 195 | } |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 196 | case "delete": |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 197 | if len(s) < 2 { |
| 198 | newmessage = newmessage + "invalid command " + cmdstr + "\n" |
| 199 | break |
| 200 | } |
| 201 | var devicelist importer.DeviceListByIp |
| 202 | for _, ip := range s[1:] { |
mc | cd7e950 | 2019-12-16 22:04:13 +0000 | [diff] [blame] | 203 | addr := strings.Split(ip, ":") |
| 204 | if len(addr) != 2 { |
| 205 | newmessage = newmessage + "invalid address " + ip + "\n" |
| 206 | continue |
| 207 | } |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 208 | devicelist.Ip = append(devicelist.Ip, ip) |
| 209 | } |
mc | cd7e950 | 2019-12-16 22:04:13 +0000 | [diff] [blame] | 210 | if len(devicelist.Ip) == 0 { |
| 211 | break |
| 212 | } |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 213 | _, err := cc.DeleteDeviceList(ctx, &devicelist) |
| 214 | if err != nil { |
| 215 | errStatus, _ := status.FromError(err) |
mc | cd7e950 | 2019-12-16 22:04:13 +0000 | [diff] [blame] | 216 | newmessage = newmessage + errStatus.Message() |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 217 | logrus.Errorf("delete error - status code %v message %v", errStatus.Code(), errStatus.Message()) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 218 | } else { |
mc | cd7e950 | 2019-12-16 22:04:13 +0000 | [diff] [blame] | 219 | sort.Strings(devicelist.Ip) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 220 | ips := strings.Join(devicelist.Ip, " ") |
| 221 | newmessage = newmessage + ips + " deleted\n" |
| 222 | } |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 223 | case "period": |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 224 | if len(s) != 2 { |
| 225 | newmessage = newmessage + "invalid command " + cmdstr + "\n" |
| 226 | break |
| 227 | } |
| 228 | args := strings.Split(s[1], ":") |
| 229 | if len(args) != 3 { |
| 230 | newmessage = newmessage + "invalid command " + s[1] + "\n" |
| 231 | break |
| 232 | } |
| 233 | ip := args[0] + ":" + args[1] |
| 234 | pv := args[2] |
| 235 | u, err := strconv.ParseUint(pv, 10, 64) |
| 236 | if err != nil { |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 237 | logrus.Error("ParseUint error!!\n") |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 238 | } else { |
| 239 | freqinfo := new(importer.FreqInfo) |
| 240 | freqinfo.Frequency = uint32(u) |
| 241 | freqinfo.IpAddress = ip |
| 242 | _, err := cc.SetFrequency(ctx, freqinfo) |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 243 | |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 244 | if err != nil { |
| 245 | errStatus, _ := status.FromError(err) |
| 246 | newmessage = newmessage + errStatus.Message() |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 247 | logrus.Errorf("period error - status code %v message %v", errStatus.Code(), errStatus.Message()) |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 248 | } else { |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 249 | newmessage = newmessage + "data collection interval configured to " + pv + " seconds\n" |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 250 | } |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 251 | } |
| 252 | case "sub", "unsub": |
| 253 | if len(s) != 2 { |
| 254 | newmessage = newmessage + "invalid command " + cmdstr + "\n" |
| 255 | break |
| 256 | } |
| 257 | args := strings.Split(s[1], ":") |
| 258 | if len(args) < 3 { |
| 259 | newmessage = newmessage + "invalid command " + s[1] + "\n" |
| 260 | break |
| 261 | } |
| 262 | giveneventlist := new(importer.GivenEventList) |
| 263 | giveneventlist.EventIpAddress = args[0] + ":" + args[1] |
| 264 | for _, event := range args[2:] { |
| 265 | if value, ok := EVENTS_MAP[event]; ok { |
| 266 | giveneventlist.Events = append(giveneventlist.Events, value) |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 267 | } |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 268 | } |
mc | cd7e950 | 2019-12-16 22:04:13 +0000 | [diff] [blame] | 269 | if len(giveneventlist.Events) == 0 { |
| 270 | newmessage = newmessage + "No valid event was given\n" |
| 271 | } |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 272 | var err error |
| 273 | if cmd == "sub" { |
mc | cd7e950 | 2019-12-16 22:04:13 +0000 | [diff] [blame] | 274 | _, err = cc.SubscribeGivenEvents(ctx, giveneventlist) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 275 | } else { |
mc | cd7e950 | 2019-12-16 22:04:13 +0000 | [diff] [blame] | 276 | _, err = cc.UnsubscribeGivenEvents(ctx, giveneventlist) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 277 | } |
| 278 | if err != nil { |
| 279 | errStatus, _ := status.FromError(err) |
| 280 | newmessage = newmessage + errStatus.Message() |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 281 | logrus.Errorf("Un/subscribe error - status code %v message %v", errStatus.Code(), errStatus.Message()) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 282 | } else { |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 283 | newmessage = newmessage + cmd + " successful\n" |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 284 | } |
| 285 | case "showeventlist": |
| 286 | if len(s) != 2 { |
mc | cd7e950 | 2019-12-16 22:04:13 +0000 | [diff] [blame] | 287 | newmessage = newmessage + "invalid command " + cmdstr + "\n" |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 288 | break |
| 289 | } |
| 290 | currentdeviceinfo := new(importer.Device) |
| 291 | currentdeviceinfo.IpAddress = s[1] |
| 292 | ret_msg, err := cc.GetEventList(ctx, currentdeviceinfo) |
| 293 | if err != nil { |
| 294 | errStatus, _ := status.FromError(err) |
| 295 | newmessage = errStatus.Message() |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 296 | logrus.Errorf("showeventlist error - status code %v message %v", errStatus.Code(), errStatus.Message()) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 297 | } else { |
| 298 | fmt.Print("showeventlist ", ret_msg.Events) |
mc | cd7e950 | 2019-12-16 22:04:13 +0000 | [diff] [blame] | 299 | sort.Strings(ret_msg.Events[:]) |
| 300 | newmessage = strings.Join(ret_msg.Events[:], " ") |
| 301 | newmessage = newmessage + "\n" |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 302 | } |
| 303 | case "showdeviceeventlist": |
| 304 | if len(s) != 2 { |
| 305 | newmessage = newmessage + "invalid command " + s[1] + "\n" |
| 306 | break |
| 307 | } |
| 308 | currentdeviceinfo := new(importer.Device) |
| 309 | currentdeviceinfo.IpAddress = s[1] |
| 310 | ret_msg, err := cc.GetCurrentEventList(ctx, currentdeviceinfo) |
| 311 | if err != nil { |
| 312 | errStatus, _ := status.FromError(err) |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 313 | logrus.Errorf("showdeviceeventlist error - status code %v message %v", errStatus.Code(), errStatus.Message()) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 314 | newmessage = newmessage + errStatus.Message() |
| 315 | } else { |
| 316 | fmt.Print("showdeviceeventlist ", ret_msg.Events) |
mc | cd7e950 | 2019-12-16 22:04:13 +0000 | [diff] [blame] | 317 | sort.Strings(ret_msg.Events[:]) |
| 318 | newmessage = strings.Join(ret_msg.Events[:], " ") |
| 319 | newmessage = newmessage + "\n" |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 320 | } |
| 321 | case "cleardeviceeventlist": |
| 322 | if len(s) != 2 { |
| 323 | newmessage = newmessage + "invalid command " + s[1] + "\n" |
| 324 | break |
| 325 | } |
| 326 | currentdeviceinfo := new(importer.Device) |
| 327 | currentdeviceinfo.IpAddress = s[1] |
| 328 | _, err := cc.ClearCurrentEventList(ctx, currentdeviceinfo) |
| 329 | if err != nil { |
| 330 | errStatus, _ := status.FromError(err) |
| 331 | newmessage = newmessage + errStatus.Message() |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 332 | logrus.Errorf("cleardeviceeventlist error - status code %v message %v", errStatus.Code(), errStatus.Message()) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 333 | } else { |
| 334 | newmessage = newmessage + currentdeviceinfo.IpAddress + " events cleared\n" |
| 335 | } |
| 336 | case "QUIT": |
| 337 | loop = false |
| 338 | newmessage = "QUIT" |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 339 | |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 340 | case "showdevices": |
| 341 | cmd_size := len(s) |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 342 | logrus.Infof("cmd is : %s cmd_size: %d", cmd, cmd_size) |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 343 | if cmd_size > 2 || cmd_size < 0 { |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 344 | logrus.Error("error event showdevices !!") |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 345 | newmessage = "error event !!" |
| 346 | } else { |
| 347 | err, currentlist := GetCurrentDevices() |
dileepbk | 86ef010 | 2019-11-13 00:08:33 +0000 | [diff] [blame] | 348 | |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 349 | if err != nil { |
| 350 | errStatus, _ := status.FromError(err) |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 351 | logrus.Errorf("GetCurrentDevice error: %s Status code: %d", errStatus.Message(), errStatus.Code()) |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 352 | newmessage = errStatus.Message() |
| 353 | fmt.Print("showdevices error!!") |
| 354 | } else { |
| 355 | fmt.Print("showdevices ", currentlist) |
mc | cd7e950 | 2019-12-16 22:04:13 +0000 | [diff] [blame] | 356 | sort.Strings(currentlist[:]) |
| 357 | newmessage = strings.Join(currentlist[:], " ") |
| 358 | newmessage = newmessage + "\n" |
dileepbk | 86ef010 | 2019-11-13 00:08:33 +0000 | [diff] [blame] | 359 | } |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 360 | } |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 361 | default: |
| 362 | newmessage = newmessage + "invalid command " + cmdstr + "\n" |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 363 | } |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 364 | // send string back to client |
| 365 | n, err := connS.Write([]byte(newmessage + ";")) |
| 366 | if err != nil { |
| 367 | logrus.Errorf("err writing to client:%s, n:%d", err, n) |
| 368 | return |
| 369 | } |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 370 | } |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 371 | } |