amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package dmiserver |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "fmt" |
| 22 | |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 23 | "github.com/Shopify/sarama" |
| 24 | |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 25 | "github.com/golang/protobuf/ptypes/empty" |
| 26 | "github.com/golang/protobuf/ptypes/timestamp" |
| 27 | "github.com/opencord/bbsim/internal/bbsim/devices" |
| 28 | "github.com/opencord/bbsim/internal/common" |
| 29 | dmi "github.com/opencord/device-management-interface/go/dmi" |
| 30 | |
| 31 | guuid "github.com/google/uuid" |
| 32 | "google.golang.org/grpc/codes" |
| 33 | "google.golang.org/grpc/status" |
| 34 | ) |
| 35 | |
Humera Kouser | 18b275c | 2020-11-30 11:30:36 +0530 | [diff] [blame] | 36 | const ( |
hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 37 | kafkaChannelSize = 100 |
Humera Kouser | 18b275c | 2020-11-30 11:30:36 +0530 | [diff] [blame] | 38 | ) |
| 39 | |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 40 | func getUUID(seed string) string { |
| 41 | return guuid.NewMD5(guuid.Nil, []byte(seed)).String() |
| 42 | } |
| 43 | |
| 44 | //StartManagingDevice establishes connection with the device and does checks to ascertain if the device with passed identity can be managed |
| 45 | func (dms *DmiAPIServer) StartManagingDevice(req *dmi.ModifiableComponent, stream dmi.NativeHWManagementService_StartManagingDeviceServer) error { |
| 46 | //Get serial number and generate the UUID based on this serial number. Store this UUID in local cache |
| 47 | logger.Debugf("StartManagingDevice() invoked with request %+v", req) |
| 48 | if req == nil { |
| 49 | return status.Errorf(codes.FailedPrecondition, "request is empty") |
| 50 | } |
| 51 | |
| 52 | if req.Name == "" { |
| 53 | return status.Errorf(codes.InvalidArgument, "'Name' can not be empty in the request") |
| 54 | } |
| 55 | |
| 56 | olt := devices.GetOLT() |
| 57 | |
| 58 | // Uri is the IP address |
| 59 | dms.ipAddress = req.GetUri().GetUri() |
| 60 | dms.deviceSerial = olt.SerialNumber |
| 61 | dms.deviceName = fmt.Sprintf("%s-%s", common.Config.Olt.Vendor, dms.deviceSerial) |
| 62 | |
| 63 | dms.uuid = getUUID(dms.deviceSerial) |
| 64 | |
| 65 | dms.ponTransceiverUuids = make([]string, olt.NumPon) |
| 66 | dms.ponTransceiverCageUuids = make([]string, olt.NumPon) |
| 67 | |
Humera Kouser | 18b275c | 2020-11-30 11:30:36 +0530 | [diff] [blame] | 68 | // Start device metrics generator |
hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 69 | dms.metricChannel = make(chan interface{}, kafkaChannelSize) |
Humera Kouser | 18b275c | 2020-11-30 11:30:36 +0530 | [diff] [blame] | 70 | StartMetricGenerator(dms) |
| 71 | |
hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 72 | // Start device event generator |
| 73 | dms.eventChannel = make(chan interface{}, kafkaChannelSize) |
| 74 | StartEventsGenerator(dms) |
| 75 | |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 76 | var components []*dmi.Component |
| 77 | |
| 78 | // Create and store the component for transceivers and transceiver cages |
| 79 | for i := 0; i < olt.NumPon; i++ { |
| 80 | label := fmt.Sprintf("pon-%d", olt.Pons[i].ID) |
| 81 | dms.ponTransceiverUuids[i] = getUUID(dms.deviceSerial + label) |
| 82 | dms.ponTransceiverCageUuids[i] = getUUID(dms.deviceSerial + "cage" + label) |
| 83 | |
| 84 | transName := fmt.Sprintf("sfp-%d", i) |
| 85 | cageName := fmt.Sprintf("cage-%d", i) |
| 86 | |
| 87 | trans := dmi.Component{ |
| 88 | Name: transName, |
| 89 | Class: dmi.ComponentType_COMPONENT_TYPE_TRANSCEIVER, |
| 90 | Description: "XGS-PON", |
| 91 | Uuid: &dmi.Uuid{ |
| 92 | Uuid: dms.ponTransceiverUuids[i], |
| 93 | }, |
| 94 | Parent: cageName, |
| 95 | } |
| 96 | |
| 97 | cage := dmi.Component{ |
| 98 | Name: cageName, |
| 99 | Class: dmi.ComponentType_COMPONENT_TYPE_CONTAINER, |
| 100 | Description: "cage", |
| 101 | Uuid: &dmi.Uuid{ |
| 102 | Uuid: dms.ponTransceiverCageUuids[i], |
| 103 | }, |
| 104 | Parent: dms.deviceName, |
| 105 | Children: []*dmi.Component{&trans}, |
| 106 | } |
| 107 | |
| 108 | components = append(components, &cage) |
| 109 | } |
| 110 | |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 111 | // create the fans |
| 112 | numFans := 2 |
| 113 | fans := make([]*dmi.Component, numFans) |
| 114 | |
| 115 | for i := 0; i < numFans; i++ { |
| 116 | fans[i] = createFanComponent(i + 1) |
| 117 | } |
| 118 | components = append(components, fans...) |
| 119 | |
hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 120 | // Create 1 disk, 1 processor, 1 ram, 1 temperature sensor and power supply unit |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 121 | components = append(components, createDiskComponent(0)) |
| 122 | components = append(components, createProcessorComponent(0)) |
| 123 | components = append(components, createMemoryComponent(0)) |
| 124 | components = append(components, createInnerSurroundingTempComponentSensor(0)) |
hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 125 | components = append(components, createPowerSupplyComponent(0)) |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 126 | |
| 127 | // create the root component |
| 128 | dms.root = &dmi.Component{ |
| 129 | Name: dms.deviceName, |
| 130 | Class: 0, |
| 131 | Description: "", |
| 132 | Parent: "", |
| 133 | ParentRelPos: 0, |
| 134 | Children: components, |
| 135 | SerialNum: dms.deviceSerial, |
| 136 | MfgName: common.Config.Olt.Vendor, |
| 137 | IsFru: false, |
| 138 | Uri: &dmi.Uri{ |
| 139 | Uri: dms.ipAddress, |
| 140 | }, |
| 141 | Uuid: &dmi.Uuid{ |
| 142 | Uuid: dms.uuid, |
| 143 | }, |
| 144 | State: &dmi.ComponentState{}, |
| 145 | } |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 146 | |
| 147 | logger.Debugf("Generated UUID for the uri %s is %s", dms.ipAddress, dms.uuid) |
| 148 | response := &dmi.StartManagingDeviceResponse{ |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 149 | Status: dmi.Status_OK_STATUS, |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 150 | DeviceUuid: &dmi.Uuid{ |
| 151 | Uuid: dms.uuid, |
| 152 | }, |
| 153 | } |
| 154 | |
| 155 | err := stream.Send(response) |
| 156 | if err != nil { |
| 157 | logger.Errorf("Error while sending response to client %v", err.Error()) |
| 158 | return status.Errorf(codes.Unknown, err.Error()) |
| 159 | } |
| 160 | |
| 161 | return nil |
| 162 | } |
| 163 | |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 164 | func createFanComponent(fanIdx int) *dmi.Component { |
| 165 | fanName := fmt.Sprintf("Thermal/Fans/System Fan/%d", fanIdx) |
| 166 | fanSerial := fmt.Sprintf("bbsim-fan-serial-%d", fanIdx) |
| 167 | return &dmi.Component{ |
| 168 | Name: fanName, |
| 169 | Class: dmi.ComponentType_COMPONENT_TYPE_FAN, |
| 170 | Description: "bbsim-fan", |
| 171 | Parent: "", |
| 172 | ParentRelPos: 0, |
| 173 | SerialNum: fanSerial, |
| 174 | MfgName: "bbsim-fan", |
| 175 | IsFru: false, |
| 176 | Uuid: &dmi.Uuid{ |
| 177 | Uuid: getUUID(fanName), |
| 178 | }, |
| 179 | State: &dmi.ComponentState{}, |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | func createProcessorComponent(cpuIdx int) *dmi.Component { |
| 184 | cpuName := fmt.Sprintf("Systems/1/Processors/%d", cpuIdx) |
| 185 | cpuSerial := fmt.Sprintf("bbsim-cpu-serial-%d", cpuIdx) |
| 186 | return &dmi.Component{ |
| 187 | Name: cpuName, |
| 188 | Class: dmi.ComponentType_COMPONENT_TYPE_CPU, |
| 189 | Description: "bbsim-cpu", |
| 190 | Parent: "", |
| 191 | ParentRelPos: 0, |
| 192 | SerialNum: cpuSerial, |
| 193 | MfgName: "bbsim-cpu", |
| 194 | IsFru: false, |
| 195 | Uuid: &dmi.Uuid{ |
| 196 | Uuid: getUUID(cpuName), |
| 197 | }, |
| 198 | State: &dmi.ComponentState{}, |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | func createMemoryComponent(memIdx int) *dmi.Component { |
| 203 | memName := fmt.Sprintf("Systems/1/Memory/%d", memIdx) |
| 204 | memSerial := fmt.Sprintf("bbsim-ram-serial-%d", memIdx) |
| 205 | return &dmi.Component{ |
| 206 | Name: memName, |
| 207 | Class: dmi.ComponentType_COMPONENT_TYPE_MEMORY, |
| 208 | Description: "bbsim-ram", |
| 209 | Parent: "", |
| 210 | ParentRelPos: 0, |
| 211 | SerialNum: memSerial, |
| 212 | MfgName: "bbsim-ram", |
| 213 | IsFru: false, |
| 214 | Uuid: &dmi.Uuid{ |
| 215 | Uuid: getUUID(memName), |
| 216 | }, |
| 217 | State: &dmi.ComponentState{}, |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | func createDiskComponent(diskIdx int) *dmi.Component { |
| 222 | diskName := fmt.Sprintf("Systems/1/Disk/%d", diskIdx) |
| 223 | diskSerial := fmt.Sprintf("bbsim-disk-serial-%d", diskIdx) |
| 224 | return &dmi.Component{ |
| 225 | Name: diskName, |
| 226 | Class: dmi.ComponentType_COMPONENT_TYPE_STORAGE, |
| 227 | Description: "bbsim-disk", |
| 228 | Parent: "", |
| 229 | ParentRelPos: 0, |
| 230 | SerialNum: diskSerial, |
| 231 | MfgName: "bbsim-disk", |
| 232 | IsFru: false, |
| 233 | Uuid: &dmi.Uuid{ |
| 234 | Uuid: getUUID(diskName), |
| 235 | }, |
| 236 | State: &dmi.ComponentState{}, |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | func createInnerSurroundingTempComponentSensor(sensorIdx int) *dmi.Component { |
| 241 | sensorName := fmt.Sprintf("Systems/1/Sensor/%d", sensorIdx) |
| 242 | sensorSerial := fmt.Sprintf("bbsim-sensor-istemp-serial-%d", sensorIdx) |
| 243 | return &dmi.Component{ |
| 244 | Name: sensorName, |
| 245 | Class: dmi.ComponentType_COMPONENT_TYPE_SENSOR, |
| 246 | Description: "bbsim-istemp", |
| 247 | Parent: "", |
| 248 | ParentRelPos: 0, |
| 249 | SerialNum: sensorSerial, |
| 250 | MfgName: "bbsim-istemp", |
| 251 | IsFru: false, |
| 252 | Uuid: &dmi.Uuid{ |
| 253 | Uuid: getUUID(sensorName), |
| 254 | }, |
| 255 | State: &dmi.ComponentState{}, |
| 256 | } |
| 257 | } |
| 258 | |
hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 259 | func createPowerSupplyComponent(psuIdx int) *dmi.Component { |
| 260 | psuName := fmt.Sprintf("Thermal/PSU/SystemPSU/%d", psuIdx) |
| 261 | psuSerial := fmt.Sprintf("bbsim-psu-serial-%d", psuIdx) |
| 262 | return &dmi.Component{ |
| 263 | Name: psuName, |
| 264 | Class: dmi.ComponentType_COMPONENT_TYPE_POWER_SUPPLY, |
| 265 | Description: "bbsim-psu", |
| 266 | Parent: "", |
| 267 | ParentRelPos: 0, |
| 268 | SerialNum: psuSerial, |
| 269 | MfgName: "bbsim-psu", |
| 270 | IsFru: false, |
| 271 | Uuid: &dmi.Uuid{ |
| 272 | Uuid: getUUID(psuName), |
| 273 | }, |
| 274 | State: &dmi.ComponentState{}, |
| 275 | } |
| 276 | } |
| 277 | |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 278 | //StopManagingDevice stops management of a device and cleans up any context and caches for that device |
Humera Kouser | 18b275c | 2020-11-30 11:30:36 +0530 | [diff] [blame] | 279 | func (dms *DmiAPIServer) StopManagingDevice(ctx context.Context, req *dmi.StopManagingDeviceRequest) (*dmi.StopManagingDeviceResponse, error) { |
| 280 | logger.Debugf("StopManagingDevice API invoked") |
| 281 | if req == nil { |
ssiddiqui | 6ca4070 | 2021-03-08 18:20:21 +0530 | [diff] [blame] | 282 | return &dmi.StopManagingDeviceResponse{Status: dmi.Status_ERROR_STATUS, Reason: dmi.StopManagingDeviceResponse_UNDEFINED_REASON}, status.Errorf(codes.FailedPrecondition, "request is empty") |
Humera Kouser | 18b275c | 2020-11-30 11:30:36 +0530 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | if req.Name == "" { |
ssiddiqui | 6ca4070 | 2021-03-08 18:20:21 +0530 | [diff] [blame] | 286 | return &dmi.StopManagingDeviceResponse{Status: dmi.Status_ERROR_STATUS, Reason: dmi.StopManagingDeviceResponse_UNKNOWN_DEVICE}, |
Humera Kouser | 18b275c | 2020-11-30 11:30:36 +0530 | [diff] [blame] | 287 | status.Errorf(codes.InvalidArgument, "'Name' can not be empty in the request") |
| 288 | } |
| 289 | |
| 290 | // Stop the components/go routines created |
| 291 | StopMetricGenerator() |
| 292 | |
| 293 | if dms.mPublisherCancelFunc != nil { |
| 294 | dms.mPublisherCancelFunc() |
| 295 | } |
| 296 | |
| 297 | dms.deviceName = "" |
| 298 | dms.kafkaEndpoint = "" |
| 299 | dms.ipAddress = "" |
| 300 | dms.deviceSerial = "" |
| 301 | dms.ponTransceiverUuids = nil |
| 302 | dms.ponTransceiverCageUuids = nil |
| 303 | dms.uuid = "" |
| 304 | dms.root = nil |
| 305 | dms.metricChannel = nil |
| 306 | |
| 307 | logger.Infof("Stopped managing the device") |
| 308 | return &dmi.StopManagingDeviceResponse{Status: dmi.Status_OK_STATUS}, nil |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | //GetPhysicalInventory gets the HW inventory details of the Device |
| 312 | func (dms *DmiAPIServer) GetPhysicalInventory(req *dmi.PhysicalInventoryRequest, stream dmi.NativeHWManagementService_GetPhysicalInventoryServer) error { |
| 313 | if req == nil || req.DeviceUuid == nil || req.DeviceUuid.Uuid == "" { |
| 314 | return status.Errorf(codes.InvalidArgument, "device-UUID missing in the request") |
| 315 | } |
| 316 | |
| 317 | // Function to send the response back on the stream |
| 318 | sendResponseBackOnStream := func(stream dmi.NativeHWManagementService_GetPhysicalInventoryServer, msg *dmi.PhysicalInventoryResponse) error { |
| 319 | err := stream.Send(msg) |
| 320 | if err != nil { |
| 321 | logger.Errorf("Error sending response to client, error: %v", err) |
| 322 | return status.Errorf(codes.Internal, "Error sending response to client "+err.Error()) |
| 323 | } |
| 324 | return nil |
| 325 | } |
| 326 | |
| 327 | if req.DeviceUuid.Uuid != dms.uuid { |
| 328 | logger.Errorf("Requested uuid =%s, uuid of existing device = %s", req.DeviceUuid.Uuid, dms.uuid) |
| 329 | // Wrong uuid, return error |
| 330 | errResponse := &dmi.PhysicalInventoryResponse{ |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 331 | Status: dmi.Status_ERROR_STATUS, |
ssiddiqui | 6ca4070 | 2021-03-08 18:20:21 +0530 | [diff] [blame] | 332 | Reason: dmi.PhysicalInventoryResponse_UNKNOWN_DEVICE, |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 333 | Inventory: &dmi.Hardware{}, |
| 334 | } |
| 335 | |
| 336 | return sendResponseBackOnStream(stream, errResponse) |
| 337 | } |
| 338 | |
| 339 | response := &dmi.PhysicalInventoryResponse{ |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 340 | Status: dmi.Status_OK_STATUS, |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 341 | Inventory: &dmi.Hardware{ |
| 342 | LastChange: ×tamp.Timestamp{ |
| 343 | Seconds: 0, |
| 344 | Nanos: 0, |
| 345 | }, |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 346 | Root: dms.root, |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 347 | }, |
| 348 | } |
| 349 | return sendResponseBackOnStream(stream, response) |
| 350 | } |
| 351 | |
| 352 | //Contains tells whether arr contains element. |
| 353 | func Contains(arr []string, element string) bool { |
| 354 | for _, item := range arr { |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 355 | if element == item { |
| 356 | return true |
| 357 | } |
| 358 | } |
| 359 | return false |
| 360 | } |
| 361 | |
| 362 | func findComponent(l []*dmi.Component, compUUID string) *dmi.Component { |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 363 | var foundComp *dmi.Component |
| 364 | |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 365 | for _, comp := range l { |
| 366 | logger.Debugf("findComponent slice comp = %v compUUID = %s", comp, compUUID) |
| 367 | if comp.Uuid.Uuid == compUUID { |
| 368 | return comp |
| 369 | } |
| 370 | |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 371 | foundComp = findComponent(comp.GetChildren(), compUUID) |
| 372 | if foundComp != nil { |
| 373 | return foundComp |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
| 377 | return nil |
| 378 | } |
| 379 | |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 380 | func findComponentsOfType(l []*dmi.Component, compType dmi.ComponentType) []*dmi.Component { |
| 381 | var comps []*dmi.Component |
| 382 | findComponents(l, compType, &comps) |
| 383 | return comps |
| 384 | } |
| 385 | |
| 386 | func findComponents(l []*dmi.Component, compType dmi.ComponentType, collector *[]*dmi.Component) { |
| 387 | |
| 388 | for _, comp := range l { |
| 389 | if comp.Class == compType { |
| 390 | *collector = append(*collector, comp) |
| 391 | //logger.Debugf("Added collector = %v", *collector) |
| 392 | } |
| 393 | |
| 394 | findComponents(comp.GetChildren(), compType, collector) |
| 395 | } |
| 396 | } |
| 397 | |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 398 | func sendGetHWComponentResponse(c *dmi.Component, stream dmi.NativeHWManagementService_GetHWComponentInfoServer) error { |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 399 | apiStatus := dmi.Status_OK_STATUS |
ssiddiqui | 6ca4070 | 2021-03-08 18:20:21 +0530 | [diff] [blame] | 400 | reason := dmi.HWComponentInfoGetResponse_UNDEFINED_REASON |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 401 | |
| 402 | if c == nil { |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 403 | apiStatus = dmi.Status_ERROR_STATUS |
ssiddiqui | 6ca4070 | 2021-03-08 18:20:21 +0530 | [diff] [blame] | 404 | reason = dmi.HWComponentInfoGetResponse_UNKNOWN_DEVICE |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | response := &dmi.HWComponentInfoGetResponse{ |
| 408 | Status: apiStatus, |
| 409 | Reason: reason, |
| 410 | Component: c, |
| 411 | } |
| 412 | |
| 413 | err := stream.Send(response) |
| 414 | if err != nil { |
| 415 | logger.Errorf("Error sending response to client, error: %v", err) |
| 416 | return status.Errorf(codes.Internal, "Error sending response to client "+err.Error()) |
| 417 | } |
| 418 | return nil |
| 419 | } |
| 420 | |
| 421 | //GetHWComponentInfo gets the details of a particular HW component |
| 422 | func (dms *DmiAPIServer) GetHWComponentInfo(req *dmi.HWComponentInfoGetRequest, stream dmi.NativeHWManagementService_GetHWComponentInfoServer) error { |
| 423 | logger.Debugf("GetHWComponentInfo() invoked with request %+v", req) |
| 424 | |
| 425 | if req == nil { |
| 426 | return status.Errorf(codes.FailedPrecondition, "can not entertain nil request") |
| 427 | } |
| 428 | if stream == nil { |
| 429 | logger.Errorf("stream to send is nil, not sending response from gRPC server ") |
| 430 | return status.Errorf(codes.Internal, "stream to send is nil, can not send response from gRPC server") |
| 431 | } |
| 432 | |
Humera Kouser | 18b275c | 2020-11-30 11:30:36 +0530 | [diff] [blame] | 433 | //if component list is empty, return error |
| 434 | if dms.root == nil { |
| 435 | logger.Errorf("Error occurred, device is not managed") |
| 436 | return status.Errorf(codes.Internal, "Error occurred, device is not managed, please start managing device") |
| 437 | } |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 438 | // Search for the component and return it |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 439 | c := findComponent(dms.root.Children, req.ComponentUuid.Uuid) |
| 440 | |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 441 | return sendGetHWComponentResponse(c, stream) |
| 442 | } |
| 443 | |
| 444 | //SetHWComponentInfo sets the permissible attributes of a HW component |
| 445 | func (dms *DmiAPIServer) SetHWComponentInfo(context.Context, *dmi.HWComponentInfoSetRequest) (*dmi.HWComponentInfoSetResponse, error) { |
| 446 | return nil, status.Errorf(codes.Unimplemented, "rpc SetHWComponentInfo not implemented") |
| 447 | } |
| 448 | |
| 449 | //SetLoggingEndpoint sets the location to which logs need to be shipped |
ssiddiqui | 6ca4070 | 2021-03-08 18:20:21 +0530 | [diff] [blame] | 450 | func (dms *DmiAPIServer) SetLoggingEndpoint(_ context.Context, request *dmi.SetLoggingEndpointRequest) (*dmi.SetRemoteEndpointResponse, error) { |
| 451 | logger.Debugf("SetLoggingEndpoint called with request %+v", request) |
| 452 | errRetFunc := func(stat dmi.Status, reason dmi.SetRemoteEndpointResponse_Reason) (*dmi.SetRemoteEndpointResponse, error) { |
| 453 | return &dmi.SetRemoteEndpointResponse{ |
| 454 | Status: stat, |
| 455 | Reason: reason, |
| 456 | }, status.Errorf(codes.InvalidArgument, "invalid request") |
| 457 | } |
| 458 | |
| 459 | //check the validity of the request |
| 460 | if request == nil { |
| 461 | return errRetFunc(dmi.Status_ERROR_STATUS, dmi.SetRemoteEndpointResponse_UNKNOWN_DEVICE) |
| 462 | } |
| 463 | if request.LoggingEndpoint == "" { |
| 464 | return errRetFunc(dmi.Status_ERROR_STATUS, dmi.SetRemoteEndpointResponse_LOGGING_ENDPOINT_ERROR) |
| 465 | } |
| 466 | if request.LoggingProtocol == "" { |
| 467 | return errRetFunc(dmi.Status_ERROR_STATUS, dmi.SetRemoteEndpointResponse_LOGGING_ENDPOINT_PROTOCOL_ERROR) |
| 468 | } |
| 469 | if request.DeviceUuid == nil || request.DeviceUuid.Uuid != dms.uuid { |
| 470 | return errRetFunc(dmi.Status_ERROR_STATUS, dmi.SetRemoteEndpointResponse_UNKNOWN_DEVICE) |
| 471 | } |
| 472 | |
| 473 | dms.loggingEndpoint = request.LoggingEndpoint |
| 474 | dms.loggingProtocol = request.LoggingProtocol |
| 475 | |
| 476 | return &dmi.SetRemoteEndpointResponse{ |
| 477 | Status: dmi.Status_OK_STATUS, |
| 478 | }, nil |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | //GetLoggingEndpoint gets the configured location to which the logs are being shipped |
ssiddiqui | 6ca4070 | 2021-03-08 18:20:21 +0530 | [diff] [blame] | 482 | func (dms *DmiAPIServer) GetLoggingEndpoint(_ context.Context, request *dmi.HardwareID) (*dmi.GetLoggingEndpointResponse, error) { |
| 483 | logger.Debugf("GetLoggingEndpoint called with request %+v", request) |
| 484 | if request == nil || request.Uuid == nil || request.Uuid.Uuid == "" { |
| 485 | return &dmi.GetLoggingEndpointResponse{ |
| 486 | Status: dmi.Status_ERROR_STATUS, |
| 487 | Reason: dmi.GetLoggingEndpointResponse_UNKNOWN_DEVICE, |
| 488 | }, status.Errorf(codes.InvalidArgument, "invalid request") |
| 489 | } |
| 490 | if request.Uuid.Uuid != dms.uuid { |
| 491 | return &dmi.GetLoggingEndpointResponse{ |
| 492 | Status: dmi.Status_ERROR_STATUS, |
| 493 | Reason: dmi.GetLoggingEndpointResponse_UNKNOWN_DEVICE, |
| 494 | }, nil |
| 495 | } |
| 496 | |
| 497 | return &dmi.GetLoggingEndpointResponse{ |
| 498 | Status: dmi.Status_OK_STATUS, |
| 499 | LoggingEndpoint: dms.loggingEndpoint, |
| 500 | LoggingProtocol: dms.loggingProtocol, |
| 501 | }, nil |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | //SetMsgBusEndpoint sets the location of the Message Bus to which events and metrics are shipped |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 505 | func (dms *DmiAPIServer) SetMsgBusEndpoint(ctx context.Context, request *dmi.SetMsgBusEndpointRequest) (*dmi.SetRemoteEndpointResponse, error) { |
| 506 | logger.Debugf("SetMsgBusEndpoint() invoked with request: %+v and context: %v", request, ctx) |
| 507 | if request == nil || request.MsgbusEndpoint == "" { |
ssiddiqui | 6ca4070 | 2021-03-08 18:20:21 +0530 | [diff] [blame] | 508 | return &dmi.SetRemoteEndpointResponse{Status: dmi.Status_ERROR_STATUS, Reason: dmi.SetRemoteEndpointResponse_MSGBUS_ENDPOINT_ERROR}, |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 509 | status.Errorf(codes.FailedPrecondition, "request is nil") |
| 510 | } |
| 511 | olt := devices.GetOLT() |
| 512 | dms.kafkaEndpoint = request.MsgbusEndpoint |
| 513 | |
| 514 | // close the old publisher |
| 515 | if dms.mPublisherCancelFunc != nil { |
| 516 | dms.mPublisherCancelFunc() |
| 517 | } |
| 518 | |
| 519 | // initialize a new publisher |
| 520 | var nCtx context.Context |
| 521 | nCtx, dms.mPublisherCancelFunc = context.WithCancel(context.Background()) |
| 522 | // initialize a publisher |
| 523 | if err := InitializeDMKafkaPublishers(sarama.NewAsyncProducer, olt.ID, dms.kafkaEndpoint); err == nil { |
hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 524 | // start a go routine which will read from channel and publish on kafka topic dm.metrics |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 525 | go DMKafkaPublisher(nCtx, dms.metricChannel, "dm.metrics") |
hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 526 | // start a go routine which will read from channel and publish on kafka topic dm.events |
| 527 | go DMKafkaPublisher(nCtx, dms.eventChannel, "dm.events") |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 528 | } else { |
hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 529 | logger.Errorf("Failed to start metric kafka publisher: %v", err) |
ssiddiqui | 6ca4070 | 2021-03-08 18:20:21 +0530 | [diff] [blame] | 530 | return &dmi.SetRemoteEndpointResponse{Status: dmi.Status_ERROR_STATUS, Reason: dmi.SetRemoteEndpointResponse_MSGBUS_ENDPOINT_ERROR}, err |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 531 | } |
| 532 | |
ssiddiqui | 6ca4070 | 2021-03-08 18:20:21 +0530 | [diff] [blame] | 533 | return &dmi.SetRemoteEndpointResponse{Status: dmi.Status_OK_STATUS, Reason: dmi.SetRemoteEndpointResponse_UNDEFINED_REASON}, nil |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | //GetMsgBusEndpoint gets the configured location to which the events and metrics are being shipped |
| 537 | func (dms *DmiAPIServer) GetMsgBusEndpoint(context.Context, *empty.Empty) (*dmi.GetMsgBusEndpointResponse, error) { |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 538 | logger.Debugf("GetMsgBusEndpoint() invoked") |
| 539 | if dms.kafkaEndpoint != "" { |
| 540 | return &dmi.GetMsgBusEndpointResponse{ |
| 541 | Status: dmi.Status_OK_STATUS, |
ssiddiqui | 6ca4070 | 2021-03-08 18:20:21 +0530 | [diff] [blame] | 542 | Reason: dmi.GetMsgBusEndpointResponse_UNDEFINED_REASON, |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 543 | MsgbusEndpoint: dms.kafkaEndpoint, |
| 544 | }, nil |
| 545 | } |
| 546 | return &dmi.GetMsgBusEndpointResponse{ |
| 547 | Status: dmi.Status_ERROR_STATUS, |
ssiddiqui | 6ca4070 | 2021-03-08 18:20:21 +0530 | [diff] [blame] | 548 | Reason: dmi.GetMsgBusEndpointResponse_INTERNAL_ERROR, |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 549 | MsgbusEndpoint: "", |
| 550 | }, nil |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | //GetManagedDevices returns an object containing a list of devices managed by this entity |
| 554 | func (dms *DmiAPIServer) GetManagedDevices(context.Context, *empty.Empty) (*dmi.ManagedDevicesResponse, error) { |
| 555 | retResponse := dmi.ManagedDevicesResponse{} |
| 556 | //If our uuid is empty, we return empty list; else we fill details and return |
| 557 | if dms.uuid != "" { |
| 558 | root := dmi.ModifiableComponent{ |
| 559 | Name: dms.deviceName, |
| 560 | Uri: &dmi.Uri{ |
| 561 | Uri: dms.ipAddress, |
| 562 | }, |
| 563 | } |
| 564 | |
| 565 | retResponse.Devices = append(retResponse.Devices, &root) |
| 566 | } |
| 567 | |
| 568 | return &retResponse, nil |
| 569 | } |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 570 | |
| 571 | //GetLogLevel Gets the configured log level for a certain entity on a certain device. |
| 572 | func (dms *DmiAPIServer) GetLogLevel(context.Context, *dmi.GetLogLevelRequest) (*dmi.GetLogLevelResponse, error) { |
| 573 | return &dmi.GetLogLevelResponse{ |
| 574 | Status: dmi.Status_OK_STATUS, |
| 575 | DeviceUuid: &dmi.Uuid{ |
| 576 | Uuid: dms.uuid, |
| 577 | }, |
| 578 | LogLevels: []*dmi.EntitiesLogLevel{}, |
| 579 | }, nil |
| 580 | } |
| 581 | |
| 582 | // SetLogLevel Sets the log level of the device, for each given entity to a certain level. |
| 583 | func (dms *DmiAPIServer) SetLogLevel(context.Context, *dmi.SetLogLevelRequest) (*dmi.SetLogLevelResponse, error) { |
| 584 | return &dmi.SetLogLevelResponse{ |
| 585 | Status: dmi.Status_OK_STATUS, |
| 586 | DeviceUuid: &dmi.Uuid{ |
| 587 | Uuid: dms.uuid, |
| 588 | }, |
| 589 | }, nil |
| 590 | } |
| 591 | |
| 592 | // GetLoggableEntities Gets the entities of a device on which log can be configured. |
| 593 | func (dms *DmiAPIServer) GetLoggableEntities(context.Context, *dmi.GetLoggableEntitiesRequest) (*dmi.GetLogLevelResponse, error) { |
| 594 | return &dmi.GetLogLevelResponse{ |
| 595 | Status: dmi.Status_OK_STATUS, |
| 596 | DeviceUuid: &dmi.Uuid{ |
| 597 | Uuid: dms.uuid, |
| 598 | }, |
| 599 | LogLevels: []*dmi.EntitiesLogLevel{}, |
| 600 | }, nil |
| 601 | } |