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