blob: 168dad12417d92360c3c789b072936a357e62b71 [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 Belwalkare63f7f92019-11-22 23:11:16 +000018 "github.com/opencord/device-management/demo_test/proto"
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000019 "bufio"
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000020 "bytes"
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000021 "crypto/tls"
22 "fmt"
23 "github.com/Shopify/sarama"
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +000024 log "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"
33 "strconv"
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000034 "strings"
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000035)
36
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000037var REDFISH_ROOT = "/redfish/v1"
38var CONTENT_TYPE = "application/json"
nickhuang6b31f8f2019-09-26 02:02:14 +000039var EVENTS_MAP = map[string]string{
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000040 "add": "ResourceAdded",
41 "rm": "ResourceRemoved",
42 "alert": "Alert",
43 "update": "Update"}
nickhuang6b31f8f2019-09-26 02:02:14 +000044
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000045var default_address string = "localhost:31085"
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000046var importerTopic = "importer"
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000047var DataConsumer sarama.Consumer
48
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000049var cc importer.DeviceManagementClient
50var ctx context.Context
51var conn *grpc.ClientConn
nickhuang6b31f8f2019-09-26 02:02:14 +000052
dileepbk86ef0102019-11-13 00:08:33 +000053func GetCurrentDevices() (error, []string) {
54 fmt.Println("Testing GetCurrentDevices\n")
55 empty := new(importer.Empty)
mc20a4b5f2019-10-16 20:28:24 +000056 var ret_msg *importer.DeviceListByIp
dileepbk86ef0102019-11-13 00:08:33 +000057 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 Belwalkare1e85ad2019-07-31 23:06:47 +000065func init() {
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000066 Formatter := new(log.TextFormatter)
67 Formatter.TimestampFormat = "02-01-2006 15:04:05"
68 Formatter.FullTimestamp = true
69 log.SetFormatter(Formatter)
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000070}
71
72func 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 Belwalkarb5db83f2019-10-24 17:27:58 +000091 os.Exit(1)
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000092 }
93 }
94 }()
95 <-doneCh
96}
97
98func kafkainit() {
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000099 cmd := exec.Command("/bin/sh", "kafka_ip.sh")
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +0000100 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 Belwalkarb5db83f2019-10-24 17:27:58 +0000111 kafkaIP = kafkaIP + ":9092"
112 fmt.Println("IP address of kafka-cord-0:", kafkaIP)
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +0000113 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}
123func main() {
nickhuang6b31f8f2019-09-26 02:02:14 +0000124 http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
125 fmt.Println("Launching server...")
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +0000126 log.Info("kafkaInit starting")
127 kafkainit()
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +0000128
nickhuang6b31f8f2019-09-26 02:02:14 +0000129 ln, err := net.Listen("tcp", ":9999")
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +0000130 if err != nil {
131 fmt.Println("could not listen")
132 log.Fatal("did not listen: %v", err)
133 }
nickhuang6b31f8f2019-09-26 02:02:14 +0000134 defer ln.Close()
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +0000135
nickhuang6b31f8f2019-09-26 02:02:14 +0000136 connS, err := ln.Accept()
137 if err != nil {
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +0000138 fmt.Println("Accept error")
139 log.Fatal("Accept error: %v", err)
mc20a4b5f2019-10-16 20:28:24 +0000140 }
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()
nickhuang6b31f8f2019-09-26 02:02:14 +0000147
mc20a4b5f2019-10-16 20:28:24 +0000148 cc = importer.NewDeviceManagementClient(conn)
149 ctx = context.Background()
nickhuang6b31f8f2019-09-26 02:02:14 +0000150
mc20a4b5f2019-10-16 20:28:24 +0000151 loop := true
nickhuang6b31f8f2019-09-26 02:02:14 +0000152
mc20a4b5f2019-10-16 20:28:24 +0000153 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])
nickhuang6b31f8f2019-09-26 02:02:14 +0000159
mc20a4b5f2019-10-16 20:28:24 +0000160 switch cmd {
nickhuang6b31f8f2019-09-26 02:02:14 +0000161
mc20a4b5f2019-10-16 20:28:24 +0000162 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
nickhuang6b31f8f2019-09-26 02:02:14 +0000174 }
mc20a4b5f2019-10-16 20:28:24 +0000175 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
nickhuang6b31f8f2019-09-26 02:02:14 +0000181 }
mc20a4b5f2019-10-16 20:28:24 +0000182 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)
nickhuang6b31f8f2019-09-26 02:02:14 +0000233
mc20a4b5f2019-10-16 20:28:24 +0000234 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 Belwalkarb5db83f2019-10-24 17:27:58 +0000238 } else {
mc20a4b5f2019-10-16 20:28:24 +0000239 newmessage = newmessage + "data collection interval configured to " + pv + " seconds\n"
nickhuang6b31f8f2019-09-26 02:02:14 +0000240 }
mc20a4b5f2019-10-16 20:28:24 +0000241 }
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)
nickhuang6b31f8f2019-09-26 02:02:14 +0000257 }
mc20a4b5f2019-10-16 20:28:24 +0000258 }
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 Belwalkarb5db83f2019-10-24 17:27:58 +0000322
dileepbk86ef0102019-11-13 00:08:33 +0000323 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 }
mc20a4b5f2019-10-16 20:28:24 +0000343 default:
344 newmessage = newmessage + "invalid command " + cmdstr + "\n"
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +0000345 }
mc20a4b5f2019-10-16 20:28:24 +0000346 // send string back to client
347 connS.Write([]byte(newmessage + "\n"))
nickhuang6b31f8f2019-09-26 02:02:14 +0000348 }
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +0000349}