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