blob: 438ae7400db48fc8c20a2f53da7e09f61d97e091 [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 default_address string = "localhost:31085"
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000045var importerTopic = "importer"
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000046var DataConsumer sarama.Consumer
47
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000048var cc importer.DeviceManagementClient
49var ctx context.Context
50var conn *grpc.ClientConn
nickhuang6b31f8f2019-09-26 02:02:14 +000051
dileepbk86ef0102019-11-13 00:08:33 +000052func GetCurrentDevices() (error, []string) {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000053 logrus.Info("Testing GetCurrentDevices")
dileepbk86ef0102019-11-13 00:08:33 +000054 empty := new(importer.Empty)
mc20a4b5f2019-10-16 20:28:24 +000055 var ret_msg *importer.DeviceListByIp
dileepbk86ef0102019-11-13 00:08:33 +000056 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 Belwalkare1e85ad2019-07-31 23:06:47 +000064func init() {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000065 Formatter := new(logrus.TextFormatter)
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000066 Formatter.TimestampFormat = "02-01-2006 15:04:05"
67 Formatter.FullTimestamp = true
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000068 logrus.SetFormatter(Formatter)
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000069}
70
71func topicListener(topic *string, master sarama.Consumer) {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000072 logrus.Info("Starting topicListener for ", *topic)
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000073 consumer, err := master.ConsumePartition(*topic, 0, sarama.OffsetOldest)
74 if err != nil {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000075 logrus.Errorf("topicListener panic, topic=[%s]: %s", *topic, err.Error())
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000076 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 Belwalkara6ba07d2020-01-10 23:22:34 +000085 logrus.Errorf("Consumer error: %s", err.Err)
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000086 case msg := <-consumer.Messages():
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000087 logrus.Infof("Got message on topic=[%s]: %s", *topic, string(msg.Value))
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000088 case <-signals:
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000089 logrus.Warn("Interrupt is detected")
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000090 os.Exit(1)
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000091 }
92 }
93 }()
94 <-doneCh
95}
96
97func kafkainit() {
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000098 cmd := exec.Command("/bin/sh", "kafka_ip.sh")
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000099 var kafkaIP string
100 var out bytes.Buffer
101 cmd.Stdout = &out
102 err := cmd.Run()
103 if err != nil {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000104 logrus.Info(err)
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +0000105 os.Exit(1)
106 }
107
108 kafkaIP = out.String()
109 kafkaIP = strings.TrimSuffix(kafkaIP, "\n")
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +0000110 kafkaIP = kafkaIP + ":9092"
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000111 logrus.Infof("IP address of kafka-cord-0:%s", kafkaIP)
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +0000112 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}
122func main() {
nickhuang6b31f8f2019-09-26 02:02:14 +0000123 http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000124 logrus.Info("Launching server...")
125 logrus.Info("kafkaInit starting")
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +0000126 kafkainit()
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +0000127
nickhuang6b31f8f2019-09-26 02:02:14 +0000128 ln, err := net.Listen("tcp", ":9999")
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +0000129 if err != nil {
130 fmt.Println("could not listen")
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000131 logrus.Fatalf("did not listen: %v", err)
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +0000132 }
nickhuang6b31f8f2019-09-26 02:02:14 +0000133 defer ln.Close()
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +0000134
mc20a4b5f2019-10-16 20:28:24 +0000135 conn, err = grpc.Dial(default_address, grpc.WithInsecure())
136 if err != nil {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000137 logrus.Fatalf("did not connect: %v", err)
mc20a4b5f2019-10-16 20:28:24 +0000138 }
139 defer conn.Close()
nickhuang6b31f8f2019-09-26 02:02:14 +0000140
mc20a4b5f2019-10-16 20:28:24 +0000141 cc = importer.NewDeviceManagementClient(conn)
142 ctx = context.Background()
nickhuang6b31f8f2019-09-26 02:02:14 +0000143
mc20a4b5f2019-10-16 20:28:24 +0000144 loop := true
nickhuang6b31f8f2019-09-26 02:02:14 +0000145
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000146 for loop {
mccd7e9502019-12-16 22:04:13 +0000147 connS, err := ln.Accept()
148 if err != nil {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000149 logrus.Fatalf("Accept error: %v", err)
mccd7e9502019-12-16 22:04:13 +0000150 }
mc20a4b5f2019-10-16 20:28:24 +0000151 cmdstr, _ := bufio.NewReader(connS).ReadString('\n')
152 cmdstr = strings.TrimSuffix(cmdstr, "\n")
153 s := strings.Split(cmdstr, " ")
154 newmessage := ""
155 cmd := string(s[0])
nickhuang6b31f8f2019-09-26 02:02:14 +0000156
mc20a4b5f2019-10-16 20:28:24 +0000157 switch cmd {
nickhuang6b31f8f2019-09-26 02:02:14 +0000158
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000159 case "attach":
mc20a4b5f2019-10-16 20:28:24 +0000160 if len(s) < 2 {
mccd7e9502019-12-16 22:04:13 +0000161 newmessage = newmessage + "invalid command length" + cmdstr + "\n"
mc20a4b5f2019-10-16 20:28:24 +0000162 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
nickhuang6b31f8f2019-09-26 02:02:14 +0000171 }
mc20a4b5f2019-10-16 20:28:24 +0000172 deviceinfo := new(importer.DeviceInfo)
173 deviceinfo.IpAddress = info[0] + ":" + info[1]
174 freq, err := strconv.ParseUint(info[2], 10, 32)
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000175 if err != nil {
mc20a4b5f2019-10-16 20:28:24 +0000176 newmessage = newmessage + "invalid command " + devinfo + "\n"
177 continue
nickhuang6b31f8f2019-09-26 02:02:14 +0000178 }
mc20a4b5f2019-10-16 20:28:24 +0000179 deviceinfo.Frequency = uint32(freq)
180 devicelist.Device = append(devicelist.Device, deviceinfo)
181 ipattached = append(ipattached, deviceinfo.IpAddress)
182 }
mccd7e9502019-12-16 22:04:13 +0000183 if len(devicelist.Device) == 0 {
184 break
185 }
mc20a4b5f2019-10-16 20:28:24 +0000186 _, err := cc.SendDeviceList(ctx, &devicelist)
187 if err != nil {
188 errStatus, _ := status.FromError(err)
mccd7e9502019-12-16 22:04:13 +0000189 newmessage = newmessage + errStatus.Message()
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000190 logrus.Errorf("attach error - status code %v message %v", errStatus.Code(), errStatus.Message())
mc20a4b5f2019-10-16 20:28:24 +0000191 } else {
mccd7e9502019-12-16 22:04:13 +0000192 sort.Strings(ipattached)
mc20a4b5f2019-10-16 20:28:24 +0000193 ips := strings.Join(ipattached, " ")
194 newmessage = newmessage + ips + " attached\n"
195 }
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000196 case "delete":
mc20a4b5f2019-10-16 20:28:24 +0000197 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:] {
mccd7e9502019-12-16 22:04:13 +0000203 addr := strings.Split(ip, ":")
204 if len(addr) != 2 {
205 newmessage = newmessage + "invalid address " + ip + "\n"
206 continue
207 }
mc20a4b5f2019-10-16 20:28:24 +0000208 devicelist.Ip = append(devicelist.Ip, ip)
209 }
mccd7e9502019-12-16 22:04:13 +0000210 if len(devicelist.Ip) == 0 {
211 break
212 }
mc20a4b5f2019-10-16 20:28:24 +0000213 _, err := cc.DeleteDeviceList(ctx, &devicelist)
214 if err != nil {
215 errStatus, _ := status.FromError(err)
mccd7e9502019-12-16 22:04:13 +0000216 newmessage = newmessage + errStatus.Message()
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000217 logrus.Errorf("delete error - status code %v message %v", errStatus.Code(), errStatus.Message())
mc20a4b5f2019-10-16 20:28:24 +0000218 } else {
mccd7e9502019-12-16 22:04:13 +0000219 sort.Strings(devicelist.Ip)
mc20a4b5f2019-10-16 20:28:24 +0000220 ips := strings.Join(devicelist.Ip, " ")
221 newmessage = newmessage + ips + " deleted\n"
222 }
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000223 case "period":
mc20a4b5f2019-10-16 20:28:24 +0000224 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 Belwalkara6ba07d2020-01-10 23:22:34 +0000237 logrus.Error("ParseUint error!!\n")
mc20a4b5f2019-10-16 20:28:24 +0000238 } else {
239 freqinfo := new(importer.FreqInfo)
240 freqinfo.Frequency = uint32(u)
241 freqinfo.IpAddress = ip
242 _, err := cc.SetFrequency(ctx, freqinfo)
nickhuang6b31f8f2019-09-26 02:02:14 +0000243
mc20a4b5f2019-10-16 20:28:24 +0000244 if err != nil {
245 errStatus, _ := status.FromError(err)
246 newmessage = newmessage + errStatus.Message()
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000247 logrus.Errorf("period error - status code %v message %v", errStatus.Code(), errStatus.Message())
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +0000248 } else {
mc20a4b5f2019-10-16 20:28:24 +0000249 newmessage = newmessage + "data collection interval configured to " + pv + " seconds\n"
nickhuang6b31f8f2019-09-26 02:02:14 +0000250 }
mc20a4b5f2019-10-16 20:28:24 +0000251 }
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)
nickhuang6b31f8f2019-09-26 02:02:14 +0000267 }
mc20a4b5f2019-10-16 20:28:24 +0000268 }
mccd7e9502019-12-16 22:04:13 +0000269 if len(giveneventlist.Events) == 0 {
270 newmessage = newmessage + "No valid event was given\n"
271 }
mc20a4b5f2019-10-16 20:28:24 +0000272 var err error
273 if cmd == "sub" {
mccd7e9502019-12-16 22:04:13 +0000274 _, err = cc.SubscribeGivenEvents(ctx, giveneventlist)
mc20a4b5f2019-10-16 20:28:24 +0000275 } else {
mccd7e9502019-12-16 22:04:13 +0000276 _, err = cc.UnsubscribeGivenEvents(ctx, giveneventlist)
mc20a4b5f2019-10-16 20:28:24 +0000277 }
278 if err != nil {
279 errStatus, _ := status.FromError(err)
280 newmessage = newmessage + errStatus.Message()
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000281 logrus.Errorf("Un/subscribe error - status code %v message %v", errStatus.Code(), errStatus.Message())
mc20a4b5f2019-10-16 20:28:24 +0000282 } else {
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000283 newmessage = newmessage + cmd + " successful\n"
mc20a4b5f2019-10-16 20:28:24 +0000284 }
285 case "showeventlist":
286 if len(s) != 2 {
mccd7e9502019-12-16 22:04:13 +0000287 newmessage = newmessage + "invalid command " + cmdstr + "\n"
mc20a4b5f2019-10-16 20:28:24 +0000288 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 Belwalkara6ba07d2020-01-10 23:22:34 +0000296 logrus.Errorf("showeventlist error - status code %v message %v", errStatus.Code(), errStatus.Message())
mc20a4b5f2019-10-16 20:28:24 +0000297 } else {
298 fmt.Print("showeventlist ", ret_msg.Events)
mccd7e9502019-12-16 22:04:13 +0000299 sort.Strings(ret_msg.Events[:])
300 newmessage = strings.Join(ret_msg.Events[:], " ")
301 newmessage = newmessage + "\n"
mc20a4b5f2019-10-16 20:28:24 +0000302 }
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 Belwalkara6ba07d2020-01-10 23:22:34 +0000313 logrus.Errorf("showdeviceeventlist error - status code %v message %v", errStatus.Code(), errStatus.Message())
mc20a4b5f2019-10-16 20:28:24 +0000314 newmessage = newmessage + errStatus.Message()
315 } else {
316 fmt.Print("showdeviceeventlist ", ret_msg.Events)
mccd7e9502019-12-16 22:04:13 +0000317 sort.Strings(ret_msg.Events[:])
318 newmessage = strings.Join(ret_msg.Events[:], " ")
319 newmessage = newmessage + "\n"
mc20a4b5f2019-10-16 20:28:24 +0000320 }
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 Belwalkara6ba07d2020-01-10 23:22:34 +0000332 logrus.Errorf("cleardeviceeventlist error - status code %v message %v", errStatus.Code(), errStatus.Message())
mc20a4b5f2019-10-16 20:28:24 +0000333 } else {
334 newmessage = newmessage + currentdeviceinfo.IpAddress + " events cleared\n"
335 }
336 case "QUIT":
337 loop = false
338 newmessage = "QUIT"
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +0000339
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000340 case "showdevices":
341 cmd_size := len(s)
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000342 logrus.Infof("cmd is : %s cmd_size: %d", cmd, cmd_size)
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000343 if cmd_size > 2 || cmd_size < 0 {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000344 logrus.Error("error event showdevices !!")
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000345 newmessage = "error event !!"
346 } else {
347 err, currentlist := GetCurrentDevices()
dileepbk86ef0102019-11-13 00:08:33 +0000348
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000349 if err != nil {
350 errStatus, _ := status.FromError(err)
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000351 logrus.Errorf("GetCurrentDevice error: %s Status code: %d", errStatus.Message(), errStatus.Code())
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000352 newmessage = errStatus.Message()
353 fmt.Print("showdevices error!!")
354 } else {
355 fmt.Print("showdevices ", currentlist)
mccd7e9502019-12-16 22:04:13 +0000356 sort.Strings(currentlist[:])
357 newmessage = strings.Join(currentlist[:], " ")
358 newmessage = newmessage + "\n"
dileepbk86ef0102019-11-13 00:08:33 +0000359 }
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000360 }
mc20a4b5f2019-10-16 20:28:24 +0000361 default:
362 newmessage = newmessage + "invalid command " + cmdstr + "\n"
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +0000363 }
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000364 // 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 }
nickhuang6b31f8f2019-09-26 02:02:14 +0000370 }
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +0000371}