Dinesh Belwalkar | 0121796 | 2019-05-23 21:51:16 +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 | "crypto/tls" |
| 19 | "encoding/json" |
Dinesh Belwalkar | 0121796 | 2019-05-23 21:51:16 +0000 | [diff] [blame] | 20 | "fmt" |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 21 | "github.com/Shopify/sarama" |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 22 | empty "github.com/golang/protobuf/ptypes/empty" |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 23 | "github.com/opencord/device-management/proto" |
| 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/codes" |
| 28 | "google.golang.org/grpc/status" |
| 29 | "io/ioutil" |
Dinesh Belwalkar | 4122960 | 2019-06-21 16:58:06 +0000 | [diff] [blame] | 30 | "net" |
Dinesh Belwalkar | b6b2b30 | 2019-06-06 17:30:44 +0000 | [diff] [blame] | 31 | "net/http" |
Dinesh Belwalkar | 8af8d7d | 2019-05-29 21:00:50 +0000 | [diff] [blame] | 32 | "os" |
| 33 | "os/signal" |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 34 | "path" |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 35 | "time" |
Dinesh Belwalkar | 0121796 | 2019-05-23 21:51:16 +0000 | [diff] [blame] | 36 | ) |
| 37 | |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 38 | //globals |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 39 | const RF_DEFAULT_PROTOCOL = "https://" |
| 40 | const RF_DATA_COLLECT_THRESHOLD = 5 |
| 41 | const RF_DATA_COLLECT_DUMMY_INTERVAL = 1000 |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 42 | const CONTENT_TYPE = "application/json" |
| 43 | |
Dinesh Belwalkar | b6b2b30 | 2019-06-06 17:30:44 +0000 | [diff] [blame] | 44 | var ( |
Dinesh Belwalkar | b6b2b30 | 2019-06-06 17:30:44 +0000 | [diff] [blame] | 45 | importerTopic = "importer" |
Dinesh Belwalkar | b6b2b30 | 2019-06-06 17:30:44 +0000 | [diff] [blame] | 46 | ) |
| 47 | |
| 48 | var DataProducer sarama.AsyncProducer |
| 49 | |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 50 | var redfish_resources = [...]string{"/redfish/v1/Chassis", "/redfish/v1/Systems", "/redfish/v1/EthernetSwitches"} |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 51 | var pvmount = os.Getenv("DEVICE_MANAGEMENT_PVMOUNT") |
| 52 | var subscriptionListPath string |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 53 | |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 54 | type scheduler struct { |
| 55 | getdata *time.Ticker |
| 56 | quit chan bool |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 57 | getdataend chan bool |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 60 | type device struct { |
| 61 | Subscriptions map[string]string `json:"ss"` |
| 62 | Freq uint32 `json:"freq"` |
| 63 | Datacollector scheduler `json:"-"` |
| 64 | Freqchan chan uint32 `json:"-"` |
| 65 | Eventtypes []string `json:"eventtypes"` |
| 66 | Datafile *os.File `json:"-"` |
Dinesh Belwalkar | 4122960 | 2019-06-21 16:58:06 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | type Server struct { |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 70 | devicemap map[string]*device |
| 71 | gRPCserver *grpc.Server |
| 72 | dataproducer sarama.AsyncProducer |
| 73 | httpclient *http.Client |
Dinesh Belwalkar | 4122960 | 2019-06-21 16:58:06 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 76 | func (s *Server) ClearCurrentEventList(c context.Context, info *importer.Device) (*empty.Empty, error) { |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 77 | fmt.Println("Received ClearCurrentEventList") |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 78 | ip_address := info.IpAddress |
| 79 | _, found := s.devicemap[ip_address] |
| 80 | if !found { |
| 81 | return nil, status.Errorf(codes.NotFound, "Device not registered") |
| 82 | } |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 83 | for event, _ := range s.devicemap[ip_address].Subscriptions { |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 84 | rtn := s.remove_subscription(ip_address, event) |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 85 | if !rtn { |
| 86 | log.WithFields(log.Fields{ |
| 87 | "Event": event, |
| 88 | }).Info("Error removing event") |
| 89 | } |
| 90 | } |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 91 | s.update_data_file(ip_address) |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 92 | return &empty.Empty{}, nil |
| 93 | } |
| 94 | |
| 95 | func (s *Server) GetCurrentEventList(c context.Context, info *importer.Device) (*importer.EventList, error) { |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 96 | fmt.Println("Received GetCurrentEventList") |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 97 | _, found := s.devicemap[info.IpAddress] |
| 98 | if !found { |
| 99 | return nil, status.Errorf(codes.NotFound, "Device not registered") |
| 100 | } |
| 101 | currentevents := new(importer.EventList) |
| 102 | for event, _ := range s.devicemap[info.IpAddress].Subscriptions { |
| 103 | currentevents.Events = append(currentevents.Events, event) |
| 104 | } |
| 105 | return currentevents, nil |
| 106 | } |
| 107 | |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 108 | func (s *Server) GetEventList(c context.Context, info *importer.Device) (*importer.EventList, error) { |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 109 | fmt.Println("Received GetEventList") |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 110 | eventstobesubscribed := new(importer.EventList) |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 111 | // eventstobesubscribed.Events = s.devicemap[info.IpAddress].Eventtypes |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 112 | eventstobesubscribed.Events = s.get_event_types(info.IpAddress) |
| 113 | if eventstobesubscribed.Events == nil { |
| 114 | return nil, status.Errorf(codes.NotFound, "No events found") |
| 115 | } |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 116 | return eventstobesubscribed, nil |
Dinesh Belwalkar | f57ee2e | 2019-07-11 17:46:00 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 119 | func (s *Server) SetFrequency(c context.Context, info *importer.FreqInfo) (*empty.Empty, error) { |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 120 | fmt.Println("Received SetFrequency") |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 121 | _, found := s.devicemap[info.IpAddress] |
| 122 | if !found { |
| 123 | return nil, status.Errorf(codes.NotFound, "Device not registered") |
| 124 | } |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 125 | if info.Frequency > 0 && info.Frequency < RF_DATA_COLLECT_THRESHOLD { |
| 126 | return nil, status.Errorf(codes.InvalidArgument, "Invalid frequency") |
| 127 | } |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 128 | s.devicemap[info.IpAddress].Freqchan <- info.Frequency |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 129 | s.devicemap[info.IpAddress].Freq = info.Frequency |
| 130 | s.update_data_file(info.IpAddress) |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 131 | return &empty.Empty{}, nil |
Dinesh Belwalkar | f57ee2e | 2019-07-11 17:46:00 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 134 | func (s *Server) SubsrcribeGivenEvents(c context.Context, subeventlist *importer.GivenEventList) (*empty.Empty, error) { |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 135 | errstring := "" |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 136 | fmt.Println("Received SubsrcribeEvents") |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 137 | //Call API to subscribe events |
| 138 | ip_address := subeventlist.EventIpAddress |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 139 | _, found := s.devicemap[ip_address] |
| 140 | if !found { |
| 141 | return nil, status.Errorf(codes.NotFound, "Device not registered") |
| 142 | } |
| 143 | if len(subeventlist.Events) <= 0 { |
| 144 | return nil, status.Errorf(codes.InvalidArgument, "Event list is empty") |
| 145 | } |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 146 | for _, event := range subeventlist.Events { |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 147 | if _, ok := s.devicemap[ip_address].Subscriptions[event]; !ok { |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 148 | rtn := s.add_subscription(ip_address, event) |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 149 | if !rtn { |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 150 | errstring = errstring + "failed to subscribe event " + ip_address + " " + event + "\n" |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 151 | log.WithFields(log.Fields{ |
| 152 | "Event": event, |
| 153 | }).Info("Error adding event") |
| 154 | } |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 155 | } else { |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 156 | errstring = errstring + "event " + event + " already subscribed\n" |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 157 | log.WithFields(log.Fields{ |
| 158 | "Event": event, |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 159 | }).Info("Already Subscribed") |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 160 | } |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 161 | } |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 162 | s.update_data_file(ip_address) |
| 163 | if errstring != "" { |
| 164 | return &empty.Empty{}, status.Errorf(codes.InvalidArgument, errstring) |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 165 | } |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 166 | return &empty.Empty{}, nil |
Dinesh Belwalkar | f57ee2e | 2019-07-11 17:46:00 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 169 | func (s *Server) UnSubsrcribeGivenEvents(c context.Context, unsubeventlist *importer.GivenEventList) (*empty.Empty, error) { |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 170 | errstring := "" |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 171 | fmt.Println("Received UnSubsrcribeEvents") |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 172 | ip_address := unsubeventlist.EventIpAddress |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 173 | _, found := s.devicemap[ip_address] |
| 174 | if !found { |
| 175 | return nil, status.Errorf(codes.NotFound, "Device not registered") |
| 176 | } |
| 177 | |
| 178 | if len(unsubeventlist.Events) <= 0 { |
| 179 | return nil, status.Errorf(codes.InvalidArgument, "Event list is empty") |
| 180 | } |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 181 | //Call API to unsubscribe events |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 182 | for _, event := range unsubeventlist.Events { |
| 183 | if _, ok := s.devicemap[ip_address].Subscriptions[event]; ok { |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 184 | rtn := s.remove_subscription(ip_address, event) |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 185 | if !rtn { |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 186 | errstring = errstring + "failed to unsubscribe event " + ip_address + " " + event + "\n" |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 187 | log.WithFields(log.Fields{ |
| 188 | "Event": event, |
| 189 | }).Info("Error removing event") |
| 190 | } |
| 191 | } else { |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 192 | errstring = errstring + "event " + event + " not found\n" |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 193 | log.WithFields(log.Fields{ |
| 194 | "Event": event, |
| 195 | }).Info("was not Subscribed") |
| 196 | } |
| 197 | } |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 198 | s.update_data_file(ip_address) |
Dinesh Belwalkar | f57ee2e | 2019-07-11 17:46:00 +0000 | [diff] [blame] | 199 | |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 200 | if errstring != "" { |
| 201 | return &empty.Empty{}, status.Errorf(codes.InvalidArgument, errstring) |
| 202 | } |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 203 | return &empty.Empty{}, nil |
Dinesh Belwalkar | f57ee2e | 2019-07-11 17:46:00 +0000 | [diff] [blame] | 204 | } |
| 205 | |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 206 | func (s *Server) update_data_file(ip_address string) { |
| 207 | f := s.devicemap[ip_address].Datafile |
| 208 | if f != nil { |
| 209 | b, err := json.Marshal(s.devicemap[ip_address]) |
| 210 | if err != nil { |
| 211 | fmt.Println(err) |
| 212 | } else { |
| 213 | f.Truncate(0) |
| 214 | f.Seek(0, 0) |
| 215 | n, err := f.Write(b) |
| 216 | if err != nil { |
| 217 | fmt.Println("err wrote", n, "bytes") |
| 218 | fmt.Println(err) |
| 219 | } |
| 220 | } |
| 221 | } else { |
| 222 | fmt.Println("file handle is nil", ip_address) |
| 223 | } |
| 224 | } |
| 225 | |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 226 | func (s *Server) collect_data(ip_address string) { |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 227 | freqchan := s.devicemap[ip_address].Freqchan |
| 228 | ticker := s.devicemap[ip_address].Datacollector.getdata |
| 229 | donechan := s.devicemap[ip_address].Datacollector.quit |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 230 | for { |
| 231 | select { |
Dinesh Belwalkar | a0493ad | 2019-07-22 19:58:42 +0000 | [diff] [blame] | 232 | case freq := <-freqchan: |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 233 | ticker.Stop() |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 234 | if freq > 0 { |
| 235 | ticker = time.NewTicker(time.Duration(freq) * time.Second) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 236 | s.devicemap[ip_address].Datacollector.getdata = ticker |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 237 | } |
| 238 | case err := <-s.dataproducer.Errors(): |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 239 | fmt.Println("Failed to produce message:", err) |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 240 | case <-ticker.C: |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 241 | for _, resource := range redfish_resources { |
| 242 | data := s.get_status(ip_address, resource) |
| 243 | for _, str := range data { |
| 244 | str = "Device IP: " + ip_address + " " + str |
| 245 | fmt.Printf("collected data %s\n ...", str) |
| 246 | b := []byte(str) |
| 247 | msg := &sarama.ProducerMessage{Topic: importerTopic, Value: sarama.StringEncoder(b)} |
| 248 | select { |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 249 | case s.dataproducer.Input() <- msg: |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 250 | fmt.Println("Produce message") |
| 251 | default: |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | } |
| 255 | case <-donechan: |
| 256 | ticker.Stop() |
| 257 | fmt.Println("getdata ticker stopped") |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 258 | s.devicemap[ip_address].Datacollector.getdataend <- true |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 259 | return |
| 260 | } |
| 261 | } |
| 262 | } |
Dinesh Belwalkar | f57ee2e | 2019-07-11 17:46:00 +0000 | [diff] [blame] | 263 | |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 264 | func (s *Server) DeleteDeviceList(c context.Context, list *importer.DeviceListByIp) (*empty.Empty, error) { |
| 265 | fmt.Println("DeleteDeviceList received") |
| 266 | errstring := "" |
| 267 | for _, ip := range list.Ip { |
| 268 | if _, ok := s.devicemap[ip]; !ok { |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 269 | fmt.Printf("Device not found %s\n ", ip) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 270 | errstring = errstring + "Device " + ip + " not found\n" |
| 271 | continue |
| 272 | } |
| 273 | for event, _ := range s.devicemap[ip].Subscriptions { |
| 274 | rtn := s.remove_subscription(ip, event) |
| 275 | if !rtn { |
| 276 | log.WithFields(log.Fields{ |
| 277 | "Event": event, |
| 278 | }).Info("Error removing event") |
| 279 | } |
| 280 | } |
| 281 | fmt.Println("deleting device", ip) |
| 282 | s.devicemap[ip].Datacollector.quit <- true |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 283 | |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 284 | f := s.devicemap[ip].Datafile |
| 285 | if f != nil { |
| 286 | fmt.Println("deleteing file", f.Name()) |
| 287 | err := f.Close() |
| 288 | if err != nil { |
| 289 | fmt.Println("error closing file ", f.Name(), err) |
| 290 | errstring = errstring + "error closing file " + f.Name() + "\n" |
| 291 | } |
| 292 | err = os.Remove(f.Name()) |
| 293 | if err != nil { |
| 294 | fmt.Println("error deleting file ", f.Name(), err) |
| 295 | } |
| 296 | } else { |
| 297 | errstring = errstring + "file " + ip + " not found\n" |
| 298 | } |
| 299 | <-s.devicemap[ip].Datacollector.getdataend |
| 300 | delete(s.devicemap, ip) |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 301 | } |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 302 | if errstring != "" { |
| 303 | return &empty.Empty{}, status.Errorf(codes.InvalidArgument, errstring) |
Dinesh Belwalkar | 4122960 | 2019-06-21 16:58:06 +0000 | [diff] [blame] | 304 | } |
Dinesh Belwalkar | f57ee2e | 2019-07-11 17:46:00 +0000 | [diff] [blame] | 305 | return &empty.Empty{}, nil |
Dinesh Belwalkar | 4122960 | 2019-06-21 16:58:06 +0000 | [diff] [blame] | 306 | } |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 307 | |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 308 | func (s *Server) SendDeviceList(c context.Context, list *importer.DeviceList) (*empty.Empty, error) { |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 309 | errstring := "" |
| 310 | for _, dev := range list.Device { |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 311 | ip_address := dev.IpAddress |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 312 | if _, ok := s.devicemap[dev.IpAddress]; ok { |
| 313 | fmt.Printf("Device %s already exists", ip_address) |
| 314 | errstring = errstring + "Device " + ip_address + " already exists\n" |
| 315 | continue |
| 316 | } |
| 317 | |
| 318 | if dev.Frequency > 0 && dev.Frequency < RF_DATA_COLLECT_THRESHOLD { |
| 319 | fmt.Printf("Device %s data collection frequency %d out of range", ip_address, dev.Frequency) |
| 320 | errstring = errstring + "Device " + ip_address + " data collection frequency out of range\n" |
| 321 | continue |
| 322 | } |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 323 | d := device{ |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 324 | Subscriptions: make(map[string]string), |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 325 | Freq: dev.Frequency, |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 326 | Datacollector: scheduler{ |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 327 | quit: make(chan bool), |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 328 | getdataend: make(chan bool), |
| 329 | }, |
| 330 | Freqchan: make(chan uint32), |
| 331 | } |
| 332 | s.devicemap[ip_address] = &d |
| 333 | fmt.Printf("Configuring %s\n", ip_address) |
| 334 | |
| 335 | /* if initial interval is 0, create a dummy ticker, which is stopped right away, so getdata is not nil */ |
| 336 | freq := dev.Frequency |
| 337 | if freq == 0 { |
| 338 | freq = RF_DATA_COLLECT_DUMMY_INTERVAL |
| 339 | } |
| 340 | s.devicemap[ip_address].Datacollector.getdata = time.NewTicker(time.Duration(freq) * time.Second) |
| 341 | if dev.Frequency == 0 { |
| 342 | s.devicemap[ip_address].Datacollector.getdata.Stop() |
| 343 | } |
| 344 | |
| 345 | eventtypes := s.get_event_types(ip_address) |
| 346 | if eventtypes != nil { |
| 347 | for _, event := range eventtypes { |
| 348 | s.devicemap[ip_address].Eventtypes = append(s.devicemap[ip_address].Eventtypes, event) |
| 349 | if s.add_subscription(ip_address, event) == false { |
| 350 | errstring = errstring + "failed to subscribe event " + ip_address + " " + event + "\n" |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | go s.collect_data(ip_address) |
| 355 | s.devicemap[ip_address].Datafile = get_data_file(ip_address) |
| 356 | s.update_data_file(ip_address) |
| 357 | } |
| 358 | if errstring != "" { |
| 359 | return &empty.Empty{}, status.Errorf(codes.InvalidArgument, errstring) |
| 360 | } |
| 361 | return &empty.Empty{}, nil |
| 362 | } |
| 363 | |
| 364 | func (s *Server) GetCurrentDevices(c context.Context, e *importer.Empty) (*importer.DeviceListByIp, error) { |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 365 | fmt.Println("In Received GetCurrentDevices") |
dileepbk | 86ef010 | 2019-11-13 00:08:33 +0000 | [diff] [blame] | 366 | |
| 367 | if len(s.devicemap) == 0 { |
| 368 | return nil, status.Errorf(codes.NotFound, "Devices not registered") |
| 369 | } |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 370 | dl := new(importer.DeviceListByIp) |
dileepbk | 86ef010 | 2019-11-13 00:08:33 +0000 | [diff] [blame] | 371 | for k, v := range s.devicemap { |
| 372 | if v != nil { |
| 373 | fmt.Printf("IpAdd[%s] \n", k) |
| 374 | dl.Ip = append(dl.Ip, k) |
| 375 | } |
| 376 | } |
| 377 | return dl, nil |
| 378 | } |
| 379 | |
Dinesh Belwalkar | 4122960 | 2019-06-21 16:58:06 +0000 | [diff] [blame] | 380 | func NewGrpcServer(grpcport string) (l net.Listener, g *grpc.Server, e error) { |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 381 | fmt.Printf("Listening %s\n", grpcport) |
| 382 | g = grpc.NewServer() |
| 383 | l, e = net.Listen("tcp", grpcport) |
| 384 | return |
Dinesh Belwalkar | 4122960 | 2019-06-21 16:58:06 +0000 | [diff] [blame] | 385 | } |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 386 | func (s *Server) startgrpcserver() error { |
Dinesh Belwalkar | 4122960 | 2019-06-21 16:58:06 +0000 | [diff] [blame] | 387 | fmt.Println("starting gRPC Server") |
| 388 | grpcport := ":50051" |
| 389 | listener, gserver, err := NewGrpcServer(grpcport) |
| 390 | if err != nil { |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 391 | fmt.Println("Failed to create gRPC server: ", err) |
Dinesh Belwalkar | 4122960 | 2019-06-21 16:58:06 +0000 | [diff] [blame] | 392 | return err |
| 393 | } |
| 394 | s.gRPCserver = gserver |
| 395 | importer.RegisterDeviceManagementServer(gserver, s) |
| 396 | if err := gserver.Serve(listener); err != nil { |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 397 | fmt.Println("Failed to run gRPC server: ", err) |
Dinesh Belwalkar | 4122960 | 2019-06-21 16:58:06 +0000 | [diff] [blame] | 398 | return err |
| 399 | } |
| 400 | return nil |
| 401 | |
| 402 | } |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 403 | func (s *Server) kafkaCloseProducer() { |
| 404 | if err := s.dataproducer.Close(); err != nil { |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 405 | panic(err) |
| 406 | } |
| 407 | |
| 408 | } |
Dinesh Belwalkar | 4122960 | 2019-06-21 16:58:06 +0000 | [diff] [blame] | 409 | func (s *Server) kafkaInit() { |
| 410 | fmt.Println("Starting kafka init to Connect to broker: ") |
Dinesh Belwalkar | b6b2b30 | 2019-06-06 17:30:44 +0000 | [diff] [blame] | 411 | config := sarama.NewConfig() |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 412 | config.Producer.RequiredAcks = sarama.WaitForAll |
Dinesh Belwalkar | b6b2b30 | 2019-06-06 17:30:44 +0000 | [diff] [blame] | 413 | config.Producer.Retry.Max = 10 |
Dinesh Belwalkar | b6b2b30 | 2019-06-06 17:30:44 +0000 | [diff] [blame] | 414 | producer, err := sarama.NewAsyncProducer([]string{"cord-kafka.default.svc.cluster.local:9092"}, config) |
| 415 | if err != nil { |
| 416 | panic(err) |
| 417 | } |
Dinesh Belwalkar | 4122960 | 2019-06-21 16:58:06 +0000 | [diff] [blame] | 418 | s.dataproducer = producer |
Dinesh Belwalkar | b6b2b30 | 2019-06-06 17:30:44 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Dinesh Belwalkar | 4122960 | 2019-06-21 16:58:06 +0000 | [diff] [blame] | 421 | func (s *Server) handle_events(w http.ResponseWriter, r *http.Request) { |
Dinesh Belwalkar | b6b2b30 | 2019-06-06 17:30:44 +0000 | [diff] [blame] | 422 | signals := make(chan os.Signal, 1) |
| 423 | signal.Notify(signals, os.Interrupt) |
| 424 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 425 | fmt.Println(" IN Handle Event ") |
| 426 | if r.Method == "POST" { |
Dinesh Belwalkar | b6b2b30 | 2019-06-06 17:30:44 +0000 | [diff] [blame] | 427 | Body, err := ioutil.ReadAll(r.Body) |
| 428 | if err != nil { |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 429 | fmt.Println("Error getting HTTP data", err) |
Dinesh Belwalkar | b6b2b30 | 2019-06-06 17:30:44 +0000 | [diff] [blame] | 430 | } |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 431 | defer r.Body.Close() |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 432 | fmt.Println("Received Event Message ") |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 433 | fmt.Printf("%s\n", Body) |
| 434 | message := &sarama.ProducerMessage{ |
| 435 | Topic: importerTopic, |
| 436 | Value: sarama.StringEncoder(Body), |
| 437 | } |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 438 | s.dataproducer.Input() <- message |
Dinesh Belwalkar | b6b2b30 | 2019-06-06 17:30:44 +0000 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
Dinesh Belwalkar | 4122960 | 2019-06-21 16:58:06 +0000 | [diff] [blame] | 442 | func (s *Server) runServer() { |
Dinesh Belwalkar | b6b2b30 | 2019-06-06 17:30:44 +0000 | [diff] [blame] | 443 | fmt.Println("Starting HTTP Server") |
Dinesh Belwalkar | 4122960 | 2019-06-21 16:58:06 +0000 | [diff] [blame] | 444 | http.HandleFunc("/", s.handle_events) |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 445 | http.ListenAndServeTLS(":8080", "https-server.crt", "https-server.key", nil) |
Dinesh Belwalkar | b6b2b30 | 2019-06-06 17:30:44 +0000 | [diff] [blame] | 446 | } |
| 447 | |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 448 | func (s *Server) init_data_persistence() { |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 449 | fmt.Println("Retrieving persisted data") |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 450 | subscriptionListPath = pvmount + "/subscriptions" |
| 451 | if err := os.MkdirAll(subscriptionListPath, 0777); err != nil { |
| 452 | fmt.Println(err) |
| 453 | } else { |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 454 | files, err := ioutil.ReadDir(subscriptionListPath) |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 455 | if err != nil { |
| 456 | fmt.Println(err) |
| 457 | } else { |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 458 | for _, f := range files { |
| 459 | b, err := ioutil.ReadFile(path.Join(subscriptionListPath, f.Name())) |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 460 | if err != nil { |
| 461 | fmt.Println(err) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 462 | } else if f.Size() > 0 { |
| 463 | ip := f.Name() |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 464 | d := device{} |
| 465 | json.Unmarshal(b, &d) |
| 466 | s.devicemap[ip] = &d |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 467 | freq := s.devicemap[ip].Freq |
| 468 | |
| 469 | /* if initial interval is 0, create a dummy ticker, which is stopped right away, so getdata is not nil */ |
| 470 | if freq == 0 { |
| 471 | freq = RF_DATA_COLLECT_DUMMY_INTERVAL |
| 472 | } |
| 473 | s.devicemap[ip].Datacollector.getdata = time.NewTicker(time.Duration(freq) * time.Second) |
| 474 | if s.devicemap[ip].Freq == 0 { |
| 475 | s.devicemap[ip].Datacollector.getdata.Stop() |
| 476 | } |
| 477 | |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 478 | s.devicemap[ip].Datacollector.quit = make(chan bool) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 479 | s.devicemap[ip].Datacollector.getdataend = make(chan bool) |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 480 | s.devicemap[ip].Freqchan = make(chan uint32) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 481 | s.devicemap[ip].Datafile = get_data_file(ip) |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 482 | go s.collect_data(ip) |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
Dinesh Belwalkar | b6b2b30 | 2019-06-06 17:30:44 +0000 | [diff] [blame] | 489 | func init() { |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 490 | Formatter := new(log.TextFormatter) |
| 491 | Formatter.TimestampFormat = "02-01-2006 15:04:05" |
| 492 | Formatter.FullTimestamp = true |
| 493 | log.SetFormatter(Formatter) |
Dinesh Belwalkar | b6b2b30 | 2019-06-06 17:30:44 +0000 | [diff] [blame] | 494 | fmt.Println("Connecting to broker: ") |
| 495 | fmt.Println("Listening to http server") |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 496 | log.Info("log Connecting to broker:") |
| 497 | log.Info("log Listening to http server ") |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 498 | //sarama.Logger = log.New() |
Dinesh Belwalkar | b6b2b30 | 2019-06-06 17:30:44 +0000 | [diff] [blame] | 499 | } |
| 500 | |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 501 | func get_data_file(ip string) *os.File { |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 502 | if pvmount == "" { |
| 503 | return nil |
| 504 | } |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 505 | f, err := os.OpenFile(subscriptionListPath+"/"+ip, os.O_CREATE|os.O_RDWR, 0664) |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 506 | if err != nil { |
| 507 | fmt.Println(err) |
| 508 | } |
| 509 | return f |
| 510 | } |
Dinesh Belwalkar | b6b2b30 | 2019-06-06 17:30:44 +0000 | [diff] [blame] | 511 | |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 512 | func (s *Server) close_data_files() { |
| 513 | for ip, _ := range s.devicemap { |
| 514 | s.devicemap[ip].Datafile.Close() |
| 515 | } |
| 516 | } |
| 517 | |
Dinesh Belwalkar | 0121796 | 2019-05-23 21:51:16 +0000 | [diff] [blame] | 518 | func main() { |
| 519 | fmt.Println("Starting Device-management Container") |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 520 | |
| 521 | http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} |
| 522 | client := &http.Client{ |
| 523 | Timeout: 10 * time.Second, |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 524 | } |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 525 | |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 526 | s := Server{ |
| 527 | devicemap: make(map[string]*device), |
| 528 | httpclient: client, |
Dinesh Belwalkar | 4122960 | 2019-06-21 16:58:06 +0000 | [diff] [blame] | 529 | } |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 530 | |
| 531 | s.kafkaInit() |
Dinesh Belwalkar | 4122960 | 2019-06-21 16:58:06 +0000 | [diff] [blame] | 532 | go s.runServer() |
| 533 | go s.startgrpcserver() |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 534 | |
| 535 | if pvmount != "" { |
| 536 | s.init_data_persistence() |
| 537 | } |
| 538 | |
Dinesh Belwalkar | 8af8d7d | 2019-05-29 21:00:50 +0000 | [diff] [blame] | 539 | quit := make(chan os.Signal) |
| 540 | signal.Notify(quit, os.Interrupt) |
| 541 | |
| 542 | select { |
| 543 | case sig := <-quit: |
| 544 | fmt.Println("Shutting down:", sig) |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 545 | s.kafkaCloseProducer() |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 546 | s.close_data_files() |
Dinesh Belwalkar | 8af8d7d | 2019-05-29 21:00:50 +0000 | [diff] [blame] | 547 | } |
Dinesh Belwalkar | 0121796 | 2019-05-23 21:51:16 +0000 | [diff] [blame] | 548 | } |