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