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