blob: 8d929a0aa6e36b28759fab4a0a740f93e152658d [file] [log] [blame]
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +00001// 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
15package main
16
17import (
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000018 "bufio"
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000019 "bytes"
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000020 "crypto/tls"
21 "fmt"
22 "github.com/Shopify/sarama"
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +000023 "github.com/opencord/device-management/demo_test/proto"
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000024 logrus "github.com/sirupsen/logrus"
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000025 "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 Belwalkara6ba07d2020-01-10 23:22:34 +000033 "sort"
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000034 "strconv"
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000035 "strings"
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000036)
37
nickhuang6b31f8f2019-09-26 02:02:14 +000038var EVENTS_MAP = map[string]string{
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000039 "add": "ResourceAdded",
40 "rm": "ResourceRemoved",
41 "alert": "Alert",
42 "update": "Update"}
nickhuang6b31f8f2019-09-26 02:02:14 +000043
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000044var importerTopic = "importer"
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000045var DataConsumer sarama.Consumer
46
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000047var cc importer.DeviceManagementClient
48var ctx context.Context
49var conn *grpc.ClientConn
nickhuang6b31f8f2019-09-26 02:02:14 +000050
dileepbk86ef0102019-11-13 00:08:33 +000051func GetCurrentDevices() (error, []string) {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000052 logrus.Info("Testing GetCurrentDevices")
dileepbk86ef0102019-11-13 00:08:33 +000053 empty := new(importer.Empty)
mc20a4b5f2019-10-16 20:28:24 +000054 var ret_msg *importer.DeviceListByIp
dileepbk86ef0102019-11-13 00:08:33 +000055 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 Belwalkare1e85ad2019-07-31 23:06:47 +000063func init() {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000064 Formatter := new(logrus.TextFormatter)
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000065 Formatter.TimestampFormat = "02-01-2006 15:04:05"
66 Formatter.FullTimestamp = true
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000067 logrus.SetFormatter(Formatter)
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000068}
69
70func topicListener(topic *string, master sarama.Consumer) {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000071 logrus.Info("Starting topicListener for ", *topic)
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000072 consumer, err := master.ConsumePartition(*topic, 0, sarama.OffsetOldest)
73 if err != nil {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000074 logrus.Errorf("topicListener panic, topic=[%s]: %s", *topic, err.Error())
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000075 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 Belwalkara6ba07d2020-01-10 23:22:34 +000084 logrus.Errorf("Consumer error: %s", err.Err)
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000085 case msg := <-consumer.Messages():
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000086 logrus.Infof("Got message on topic=[%s]: %s", *topic, string(msg.Value))
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000087 case <-signals:
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000088 logrus.Warn("Interrupt is detected")
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000089 os.Exit(1)
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000090 }
91 }
92 }()
93 <-doneCh
94}
95
96func kafkainit() {
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000097 var kafkaIP string
Scott Bakerbdb962b2020-04-03 10:53:36 -070098 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 Belwalkare1e85ad2019-07-31 23:06:47 +0000113 }
114
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +0000115 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 Bakerbdb962b2020-04-03 10:53:36 -0700125
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +0000126func main() {
Scott Bakerbdb962b2020-04-03 10:53:36 -0700127 ParseCommandLine()
128 ProcessGlobalOptions()
129 ShowGlobalOptions()
130
nickhuang6b31f8f2019-09-26 02:02:14 +0000131 http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000132 logrus.Info("Launching server...")
133 logrus.Info("kafkaInit starting")
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +0000134 kafkainit()
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +0000135
Scott Bakerbdb962b2020-04-03 10:53:36 -0700136 ln, err := net.Listen("tcp", GlobalConfig.Local)
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +0000137 if err != nil {
138 fmt.Println("could not listen")
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000139 logrus.Fatalf("did not listen: %v", err)
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +0000140 }
nickhuang6b31f8f2019-09-26 02:02:14 +0000141 defer ln.Close()
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +0000142
Scott Bakerbdb962b2020-04-03 10:53:36 -0700143 conn, err = grpc.Dial(GlobalConfig.Importer, grpc.WithInsecure())
mc20a4b5f2019-10-16 20:28:24 +0000144 if err != nil {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000145 logrus.Fatalf("did not connect: %v", err)
mc20a4b5f2019-10-16 20:28:24 +0000146 }
147 defer conn.Close()
nickhuang6b31f8f2019-09-26 02:02:14 +0000148
mc20a4b5f2019-10-16 20:28:24 +0000149 cc = importer.NewDeviceManagementClient(conn)
150 ctx = context.Background()
nickhuang6b31f8f2019-09-26 02:02:14 +0000151
mc20a4b5f2019-10-16 20:28:24 +0000152 loop := true
nickhuang6b31f8f2019-09-26 02:02:14 +0000153
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000154 for loop {
mccd7e9502019-12-16 22:04:13 +0000155 connS, err := ln.Accept()
156 if err != nil {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000157 logrus.Fatalf("Accept error: %v", err)
mccd7e9502019-12-16 22:04:13 +0000158 }
mc20a4b5f2019-10-16 20:28:24 +0000159 cmdstr, _ := bufio.NewReader(connS).ReadString('\n')
160 cmdstr = strings.TrimSuffix(cmdstr, "\n")
161 s := strings.Split(cmdstr, " ")
162 newmessage := ""
163 cmd := string(s[0])
nickhuang6b31f8f2019-09-26 02:02:14 +0000164
mc20a4b5f2019-10-16 20:28:24 +0000165 switch cmd {
nickhuang6b31f8f2019-09-26 02:02:14 +0000166
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000167 case "attach":
mc20a4b5f2019-10-16 20:28:24 +0000168 if len(s) < 2 {
mccd7e9502019-12-16 22:04:13 +0000169 newmessage = newmessage + "invalid command length" + cmdstr + "\n"
mc20a4b5f2019-10-16 20:28:24 +0000170 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
nickhuang6b31f8f2019-09-26 02:02:14 +0000179 }
mc20a4b5f2019-10-16 20:28:24 +0000180 deviceinfo := new(importer.DeviceInfo)
181 deviceinfo.IpAddress = info[0] + ":" + info[1]
182 freq, err := strconv.ParseUint(info[2], 10, 32)
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000183 if err != nil {
mc20a4b5f2019-10-16 20:28:24 +0000184 newmessage = newmessage + "invalid command " + devinfo + "\n"
185 continue
nickhuang6b31f8f2019-09-26 02:02:14 +0000186 }
mc20a4b5f2019-10-16 20:28:24 +0000187 deviceinfo.Frequency = uint32(freq)
188 devicelist.Device = append(devicelist.Device, deviceinfo)
189 ipattached = append(ipattached, deviceinfo.IpAddress)
190 }
mccd7e9502019-12-16 22:04:13 +0000191 if len(devicelist.Device) == 0 {
192 break
193 }
mc20a4b5f2019-10-16 20:28:24 +0000194 _, err := cc.SendDeviceList(ctx, &devicelist)
195 if err != nil {
196 errStatus, _ := status.FromError(err)
mccd7e9502019-12-16 22:04:13 +0000197 newmessage = newmessage + errStatus.Message()
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000198 logrus.Errorf("attach error - status code %v message %v", errStatus.Code(), errStatus.Message())
mc20a4b5f2019-10-16 20:28:24 +0000199 } else {
mccd7e9502019-12-16 22:04:13 +0000200 sort.Strings(ipattached)
mc20a4b5f2019-10-16 20:28:24 +0000201 ips := strings.Join(ipattached, " ")
202 newmessage = newmessage + ips + " attached\n"
203 }
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000204 case "delete":
mc20a4b5f2019-10-16 20:28:24 +0000205 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:] {
mccd7e9502019-12-16 22:04:13 +0000211 addr := strings.Split(ip, ":")
212 if len(addr) != 2 {
213 newmessage = newmessage + "invalid address " + ip + "\n"
214 continue
215 }
mc20a4b5f2019-10-16 20:28:24 +0000216 devicelist.Ip = append(devicelist.Ip, ip)
217 }
mccd7e9502019-12-16 22:04:13 +0000218 if len(devicelist.Ip) == 0 {
219 break
220 }
mc20a4b5f2019-10-16 20:28:24 +0000221 _, err := cc.DeleteDeviceList(ctx, &devicelist)
222 if err != nil {
223 errStatus, _ := status.FromError(err)
mccd7e9502019-12-16 22:04:13 +0000224 newmessage = newmessage + errStatus.Message()
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000225 logrus.Errorf("delete error - status code %v message %v", errStatus.Code(), errStatus.Message())
mc20a4b5f2019-10-16 20:28:24 +0000226 } else {
mccd7e9502019-12-16 22:04:13 +0000227 sort.Strings(devicelist.Ip)
mc20a4b5f2019-10-16 20:28:24 +0000228 ips := strings.Join(devicelist.Ip, " ")
229 newmessage = newmessage + ips + " deleted\n"
230 }
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000231 case "period":
mc20a4b5f2019-10-16 20:28:24 +0000232 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 Belwalkara6ba07d2020-01-10 23:22:34 +0000245 logrus.Error("ParseUint error!!\n")
mc20a4b5f2019-10-16 20:28:24 +0000246 } else {
247 freqinfo := new(importer.FreqInfo)
248 freqinfo.Frequency = uint32(u)
249 freqinfo.IpAddress = ip
250 _, err := cc.SetFrequency(ctx, freqinfo)
nickhuang6b31f8f2019-09-26 02:02:14 +0000251
mc20a4b5f2019-10-16 20:28:24 +0000252 if err != nil {
253 errStatus, _ := status.FromError(err)
254 newmessage = newmessage + errStatus.Message()
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000255 logrus.Errorf("period error - status code %v message %v", errStatus.Code(), errStatus.Message())
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +0000256 } else {
mc20a4b5f2019-10-16 20:28:24 +0000257 newmessage = newmessage + "data collection interval configured to " + pv + " seconds\n"
nickhuang6b31f8f2019-09-26 02:02:14 +0000258 }
mc20a4b5f2019-10-16 20:28:24 +0000259 }
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)
nickhuang6b31f8f2019-09-26 02:02:14 +0000275 }
mc20a4b5f2019-10-16 20:28:24 +0000276 }
mccd7e9502019-12-16 22:04:13 +0000277 if len(giveneventlist.Events) == 0 {
278 newmessage = newmessage + "No valid event was given\n"
279 }
mc20a4b5f2019-10-16 20:28:24 +0000280 var err error
281 if cmd == "sub" {
mccd7e9502019-12-16 22:04:13 +0000282 _, err = cc.SubscribeGivenEvents(ctx, giveneventlist)
mc20a4b5f2019-10-16 20:28:24 +0000283 } else {
mccd7e9502019-12-16 22:04:13 +0000284 _, err = cc.UnsubscribeGivenEvents(ctx, giveneventlist)
mc20a4b5f2019-10-16 20:28:24 +0000285 }
286 if err != nil {
287 errStatus, _ := status.FromError(err)
288 newmessage = newmessage + errStatus.Message()
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000289 logrus.Errorf("Un/subscribe error - status code %v message %v", errStatus.Code(), errStatus.Message())
mc20a4b5f2019-10-16 20:28:24 +0000290 } else {
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000291 newmessage = newmessage + cmd + " successful\n"
mc20a4b5f2019-10-16 20:28:24 +0000292 }
293 case "showeventlist":
294 if len(s) != 2 {
mccd7e9502019-12-16 22:04:13 +0000295 newmessage = newmessage + "invalid command " + cmdstr + "\n"
mc20a4b5f2019-10-16 20:28:24 +0000296 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 Belwalkara6ba07d2020-01-10 23:22:34 +0000304 logrus.Errorf("showeventlist error - status code %v message %v", errStatus.Code(), errStatus.Message())
mc20a4b5f2019-10-16 20:28:24 +0000305 } else {
306 fmt.Print("showeventlist ", ret_msg.Events)
mccd7e9502019-12-16 22:04:13 +0000307 sort.Strings(ret_msg.Events[:])
308 newmessage = strings.Join(ret_msg.Events[:], " ")
309 newmessage = newmessage + "\n"
mc20a4b5f2019-10-16 20:28:24 +0000310 }
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 Belwalkara6ba07d2020-01-10 23:22:34 +0000321 logrus.Errorf("showdeviceeventlist error - status code %v message %v", errStatus.Code(), errStatus.Message())
mc20a4b5f2019-10-16 20:28:24 +0000322 newmessage = newmessage + errStatus.Message()
323 } else {
324 fmt.Print("showdeviceeventlist ", ret_msg.Events)
mccd7e9502019-12-16 22:04:13 +0000325 sort.Strings(ret_msg.Events[:])
326 newmessage = strings.Join(ret_msg.Events[:], " ")
327 newmessage = newmessage + "\n"
mc20a4b5f2019-10-16 20:28:24 +0000328 }
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 Belwalkara6ba07d2020-01-10 23:22:34 +0000340 logrus.Errorf("cleardeviceeventlist error - status code %v message %v", errStatus.Code(), errStatus.Message())
mc20a4b5f2019-10-16 20:28:24 +0000341 } else {
342 newmessage = newmessage + currentdeviceinfo.IpAddress + " events cleared\n"
343 }
344 case "QUIT":
345 loop = false
346 newmessage = "QUIT"
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +0000347
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000348 case "showdevices":
349 cmd_size := len(s)
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000350 logrus.Infof("cmd is : %s cmd_size: %d", cmd, cmd_size)
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000351 if cmd_size > 2 || cmd_size < 0 {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000352 logrus.Error("error event showdevices !!")
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000353 newmessage = "error event !!"
354 } else {
355 err, currentlist := GetCurrentDevices()
dileepbk86ef0102019-11-13 00:08:33 +0000356
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000357 if err != nil {
358 errStatus, _ := status.FromError(err)
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000359 logrus.Errorf("GetCurrentDevice error: %s Status code: %d", errStatus.Message(), errStatus.Code())
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000360 newmessage = errStatus.Message()
361 fmt.Print("showdevices error!!")
362 } else {
363 fmt.Print("showdevices ", currentlist)
mccd7e9502019-12-16 22:04:13 +0000364 sort.Strings(currentlist[:])
365 newmessage = strings.Join(currentlist[:], " ")
366 newmessage = newmessage + "\n"
dileepbk86ef0102019-11-13 00:08:33 +0000367 }
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000368 }
mc20a4b5f2019-10-16 20:28:24 +0000369 default:
370 newmessage = newmessage + "invalid command " + cmdstr + "\n"
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +0000371 }
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000372 // 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 }
nickhuang6b31f8f2019-09-26 02:02:14 +0000378 }
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +0000379}