khenaidoo | fffcc8a | 2019-03-13 11:54:41 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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 | package core |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "fmt" |
| 21 | "github.com/opencord/voltha-go/common/log" |
| 22 | "github.com/opencord/voltha-go/db/kvstore" |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 23 | "github.com/opencord/voltha-go/rw_core/utils" |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 24 | "github.com/opencord/voltha-protos/go/voltha" |
khenaidoo | fffcc8a | 2019-03-13 11:54:41 -0400 | [diff] [blame] | 25 | "google.golang.org/grpc/codes" |
| 26 | "google.golang.org/grpc/status" |
| 27 | "sync" |
| 28 | "time" |
| 29 | ) |
| 30 | |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 31 | func init() { |
| 32 | log.AddPackage(log.JSON, log.WarnLevel, nil) |
| 33 | } |
| 34 | |
khenaidoo | fffcc8a | 2019-03-13 11:54:41 -0400 | [diff] [blame] | 35 | type ownership struct { |
| 36 | id string |
| 37 | owned bool |
| 38 | chnl chan int |
| 39 | } |
| 40 | |
| 41 | type DeviceOwnership struct { |
| 42 | instanceId string |
| 43 | exitChannel chan int |
| 44 | kvClient kvstore.Client |
| 45 | reservationTimeout int64 // Duration in seconds |
| 46 | ownershipPrefix string |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 47 | deviceMgr *DeviceManager |
| 48 | logicalDeviceMgr *LogicalDeviceManager |
khenaidoo | fffcc8a | 2019-03-13 11:54:41 -0400 | [diff] [blame] | 49 | deviceMap map[string]*ownership |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame^] | 50 | deviceMapLock sync.RWMutex |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 51 | deviceToKeyMap map[string]string |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame^] | 52 | deviceToKeyMapLock sync.RWMutex |
| 53 | ownershipLock sync.RWMutex |
khenaidoo | fffcc8a | 2019-03-13 11:54:41 -0400 | [diff] [blame] | 54 | } |
| 55 | |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 56 | func NewDeviceOwnership(id string, kvClient kvstore.Client, deviceMgr *DeviceManager, logicalDeviceMgr *LogicalDeviceManager, ownershipPrefix string, reservationTimeout int64) *DeviceOwnership { |
khenaidoo | fffcc8a | 2019-03-13 11:54:41 -0400 | [diff] [blame] | 57 | var deviceOwnership DeviceOwnership |
| 58 | deviceOwnership.instanceId = id |
| 59 | deviceOwnership.exitChannel = make(chan int, 1) |
| 60 | deviceOwnership.kvClient = kvClient |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 61 | deviceOwnership.deviceMgr = deviceMgr |
| 62 | deviceOwnership.logicalDeviceMgr = logicalDeviceMgr |
khenaidoo | fffcc8a | 2019-03-13 11:54:41 -0400 | [diff] [blame] | 63 | deviceOwnership.ownershipPrefix = ownershipPrefix |
| 64 | deviceOwnership.reservationTimeout = reservationTimeout |
| 65 | deviceOwnership.deviceMap = make(map[string]*ownership) |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame^] | 66 | deviceOwnership.deviceMapLock = sync.RWMutex{} |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 67 | deviceOwnership.deviceToKeyMap = make(map[string]string) |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame^] | 68 | deviceOwnership.deviceToKeyMapLock = sync.RWMutex{} |
| 69 | deviceOwnership.ownershipLock = sync.RWMutex{} |
khenaidoo | fffcc8a | 2019-03-13 11:54:41 -0400 | [diff] [blame] | 70 | return &deviceOwnership |
| 71 | } |
| 72 | |
| 73 | func (da *DeviceOwnership) Start(ctx context.Context) { |
| 74 | log.Info("starting-deviceOwnership", log.Fields{"instanceId": da.instanceId}) |
| 75 | log.Info("deviceOwnership-started") |
| 76 | } |
| 77 | |
| 78 | func (da *DeviceOwnership) Stop(ctx context.Context) { |
| 79 | log.Info("stopping-deviceOwnership") |
| 80 | da.exitChannel <- 1 |
| 81 | // Need to flush all device reservations |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 82 | da.abandonAllDevices() |
khenaidoo | fffcc8a | 2019-03-13 11:54:41 -0400 | [diff] [blame] | 83 | log.Info("deviceOwnership-stopped") |
| 84 | } |
| 85 | |
| 86 | func (da *DeviceOwnership) tryToReserveKey(id string) bool { |
| 87 | var currOwner string |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 88 | //Try to reserve the key |
khenaidoo | fffcc8a | 2019-03-13 11:54:41 -0400 | [diff] [blame] | 89 | kvKey := fmt.Sprintf("%s_%s", da.ownershipPrefix, id) |
| 90 | value, err := da.kvClient.Reserve(kvKey, da.instanceId, da.reservationTimeout) |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 91 | if err != nil { |
| 92 | log.Errorw("error", log.Fields{"error": err, "id": id, "instanceId": da.instanceId}) |
| 93 | } |
khenaidoo | fffcc8a | 2019-03-13 11:54:41 -0400 | [diff] [blame] | 94 | if value != nil { |
| 95 | if currOwner, err = kvstore.ToString(value); err != nil { |
| 96 | log.Error("unexpected-owner-type") |
| 97 | } |
| 98 | return currOwner == da.instanceId |
| 99 | } |
| 100 | return false |
| 101 | } |
| 102 | |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 103 | func (da *DeviceOwnership) renewReservation(id string) bool { |
| 104 | // Try to reserve the key |
| 105 | kvKey := fmt.Sprintf("%s_%s", da.ownershipPrefix, id) |
| 106 | if err := da.kvClient.RenewReservation(kvKey); err != nil { |
| 107 | log.Errorw("reservation-renewal-error", log.Fields{"error": err, "instance": da.instanceId}) |
| 108 | return false |
| 109 | } |
| 110 | return true |
| 111 | } |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 112 | |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 113 | func (da *DeviceOwnership) MonitorOwnership(id string, chnl chan int) { |
khenaidoo | 4554f7c | 2019-05-29 22:13:15 -0400 | [diff] [blame] | 114 | log.Debugw("start-device-monitoring", log.Fields{"id": id}) |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 115 | op := "starting" |
| 116 | exit := false |
| 117 | ticker := time.NewTicker(time.Duration(da.reservationTimeout) / 3 * time.Second) |
khenaidoo | fffcc8a | 2019-03-13 11:54:41 -0400 | [diff] [blame] | 118 | for { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 119 | select { |
| 120 | case <-da.exitChannel: |
khenaidoo | 4554f7c | 2019-05-29 22:13:15 -0400 | [diff] [blame] | 121 | log.Debugw("closing-monitoring", log.Fields{"Id": id}) |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 122 | exit = true |
| 123 | case <-ticker.C: |
| 124 | log.Debugw(fmt.Sprintf("%s-reservation", op), log.Fields{"Id": id}) |
| 125 | case <-chnl: |
khenaidoo | 4554f7c | 2019-05-29 22:13:15 -0400 | [diff] [blame] | 126 | log.Debugw("closing-device-monitoring", log.Fields{"Id": id}) |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 127 | exit = true |
| 128 | } |
| 129 | if exit { |
khenaidoo | 4554f7c | 2019-05-29 22:13:15 -0400 | [diff] [blame] | 130 | log.Infow("exiting-device-monitoring", log.Fields{"Id": id}) |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 131 | ticker.Stop() |
| 132 | break |
| 133 | } |
| 134 | deviceOwned, ownedByMe := da.getOwnership(id) |
| 135 | if deviceOwned && ownedByMe { |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 136 | // Device owned; renew reservation |
| 137 | op = "renew" |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 138 | if da.renewReservation(id) { |
| 139 | log.Debugw("reservation-renewed", log.Fields{"id": id, "instanceId": da.instanceId}) |
| 140 | } else { |
| 141 | log.Debugw("reservation-not-renewed", log.Fields{"id": id, "instanceId": da.instanceId}) |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 142 | } |
| 143 | } else { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 144 | // Device not owned or not owned by me; try to seize ownership |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 145 | op = "retry" |
| 146 | if err := da.setOwnership(id, da.tryToReserveKey(id)); err != nil { |
| 147 | log.Errorw("unexpected-error", log.Fields{"error": err}) |
| 148 | } |
khenaidoo | fffcc8a | 2019-03-13 11:54:41 -0400 | [diff] [blame] | 149 | } |
khenaidoo | fffcc8a | 2019-03-13 11:54:41 -0400 | [diff] [blame] | 150 | } |
khenaidoo | 4554f7c | 2019-05-29 22:13:15 -0400 | [diff] [blame] | 151 | log.Debugw("device-monitoring-stopped", log.Fields{"id": id}) |
khenaidoo | fffcc8a | 2019-03-13 11:54:41 -0400 | [diff] [blame] | 152 | } |
| 153 | |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 154 | func (da *DeviceOwnership) getOwnership(id string) (bool, bool) { |
khenaidoo | fffcc8a | 2019-03-13 11:54:41 -0400 | [diff] [blame] | 155 | da.deviceMapLock.RLock() |
| 156 | defer da.deviceMapLock.RUnlock() |
| 157 | if val, exist := da.deviceMap[id]; exist { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 158 | return true, val.owned |
khenaidoo | fffcc8a | 2019-03-13 11:54:41 -0400 | [diff] [blame] | 159 | } |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 160 | return false, false |
khenaidoo | fffcc8a | 2019-03-13 11:54:41 -0400 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | func (da *DeviceOwnership) setOwnership(id string, owner bool) error { |
| 164 | da.deviceMapLock.Lock() |
| 165 | defer da.deviceMapLock.Unlock() |
| 166 | if _, exist := da.deviceMap[id]; exist { |
| 167 | if da.deviceMap[id].owned != owner { |
| 168 | log.Debugw("ownership-changed", log.Fields{"Id": id, "owner": owner}) |
| 169 | } |
| 170 | da.deviceMap[id].owned = owner |
| 171 | return nil |
| 172 | } |
| 173 | return status.Error(codes.NotFound, fmt.Sprintf("id-inexistent-%s", id)) |
| 174 | } |
| 175 | |
| 176 | // OwnedByMe returns where this Core instance active owns this device. This function will automatically |
| 177 | // trigger the process to monitor the device and update the device ownership regularly. |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 178 | func (da *DeviceOwnership) OwnedByMe(id interface{}) bool { |
| 179 | // Retrieve the ownership key based on the id |
| 180 | var ownershipKey string |
| 181 | var err error |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 182 | var idStr string |
| 183 | var cache bool |
| 184 | if ownershipKey, idStr, cache, err = da.getOwnershipKey(id); err != nil { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 185 | log.Warnw("no-ownershipkey", log.Fields{"error": err}) |
| 186 | return false |
| 187 | } |
| 188 | |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 189 | // Update the deviceToKey map, if not from cache |
| 190 | if !cache { |
| 191 | da.deviceToKeyMapLock.Lock() |
| 192 | da.deviceToKeyMap[idStr] = ownershipKey |
| 193 | da.deviceToKeyMapLock.Unlock() |
| 194 | } |
| 195 | |
khenaidoo | 4554f7c | 2019-05-29 22:13:15 -0400 | [diff] [blame] | 196 | // Add a lock to prevent creation of two separate monitoring routines for the same device. When a NB request for a |
| 197 | // device not in memory is received this results in this function being called in rapid succession, once when |
| 198 | // loading the device and once when handling the NB request. |
| 199 | da.ownershipLock.Lock() |
| 200 | defer da.ownershipLock.Unlock() |
| 201 | |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 202 | deviceOwned, ownedByMe := da.getOwnership(ownershipKey) |
| 203 | if deviceOwned { |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 204 | log.Debugw("ownership", log.Fields{"Id": ownershipKey, "owned": ownedByMe}) |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 205 | return ownedByMe |
| 206 | } |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 207 | // Not owned by me or maybe nobody else. Try to reserve it |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 208 | reservedByMe := da.tryToReserveKey(ownershipKey) |
| 209 | myChnl := make(chan int) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 210 | |
| 211 | da.deviceMapLock.Lock() |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 212 | da.deviceMap[ownershipKey] = &ownership{ |
| 213 | id: ownershipKey, |
| 214 | owned: reservedByMe, |
| 215 | chnl: myChnl} |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 216 | da.deviceMapLock.Unlock() |
| 217 | |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 218 | log.Debugw("set-new-ownership", log.Fields{"Id": ownershipKey, "owned": reservedByMe}) |
| 219 | go da.MonitorOwnership(ownershipKey, myChnl) |
| 220 | return reservedByMe |
khenaidoo | fffcc8a | 2019-03-13 11:54:41 -0400 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | //AbandonDevice must be invoked whenever a device is deleted from the Core |
| 224 | func (da *DeviceOwnership) AbandonDevice(id string) error { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 225 | if id == "" { |
| 226 | return status.Error(codes.FailedPrecondition, "id-nil") |
| 227 | } |
khenaidoo | fffcc8a | 2019-03-13 11:54:41 -0400 | [diff] [blame] | 228 | da.deviceMapLock.Lock() |
| 229 | defer da.deviceMapLock.Unlock() |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 230 | if o, exist := da.deviceMap[id]; exist { // id is ownership key |
| 231 | // Need to clean up all deviceToKeyMap entries using this device as key |
| 232 | da.deviceToKeyMapLock.Lock() |
| 233 | defer da.deviceToKeyMapLock.Unlock() |
| 234 | for k, v := range da.deviceToKeyMap { |
| 235 | if id == v { |
| 236 | delete(da.deviceToKeyMap, k) |
| 237 | } |
| 238 | } |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 239 | // Remove the device reference from the deviceMap |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 240 | delete(da.deviceMap, id) |
| 241 | |
khenaidoo | fffcc8a | 2019-03-13 11:54:41 -0400 | [diff] [blame] | 242 | // Stop the Go routine monitoring the device |
| 243 | close(o.chnl) |
| 244 | delete(da.deviceMap, id) |
| 245 | log.Debugw("abandoning-device", log.Fields{"Id": id}) |
| 246 | return nil |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 247 | } else { // id is not ownership key |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 248 | da.deleteDeviceKey(id) |
khenaidoo | fffcc8a | 2019-03-13 11:54:41 -0400 | [diff] [blame] | 249 | } |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 250 | return nil |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 251 | } |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 252 | |
| 253 | //abandonAllDevices must be invoked whenever a device is deleted from the Core |
| 254 | func (da *DeviceOwnership) abandonAllDevices() { |
| 255 | da.deviceMapLock.Lock() |
| 256 | defer da.deviceMapLock.Unlock() |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 257 | da.deviceToKeyMapLock.Lock() |
| 258 | defer da.deviceToKeyMapLock.Unlock() |
| 259 | for k, _ := range da.deviceToKeyMap { |
| 260 | delete(da.deviceToKeyMap, k) |
| 261 | } |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 262 | for _, val := range da.deviceMap { |
| 263 | close(val.chnl) |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | func (da *DeviceOwnership) getDeviceKey(id string) (string, error) { |
| 268 | da.deviceToKeyMapLock.RLock() |
| 269 | defer da.deviceToKeyMapLock.RUnlock() |
| 270 | if val, exist := da.deviceToKeyMap[id]; exist { |
| 271 | return val, nil |
| 272 | } |
| 273 | return "", status.Error(codes.NotFound, fmt.Sprintf("not-present-%s", id)) |
| 274 | } |
| 275 | |
| 276 | func (da *DeviceOwnership) updateDeviceKey(id string, key string) error { |
| 277 | da.deviceToKeyMapLock.Lock() |
| 278 | defer da.deviceToKeyMapLock.Unlock() |
| 279 | if _, exist := da.deviceToKeyMap[id]; exist { |
| 280 | return status.Error(codes.AlreadyExists, fmt.Sprintf("already-present-%s", id)) |
| 281 | } |
| 282 | da.deviceToKeyMap[id] = key |
| 283 | return nil |
| 284 | } |
| 285 | |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 286 | func (da *DeviceOwnership) deleteDeviceKey(id string) { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 287 | da.deviceToKeyMapLock.Lock() |
| 288 | defer da.deviceToKeyMapLock.Unlock() |
| 289 | if _, exist := da.deviceToKeyMap[id]; exist { |
| 290 | delete(da.deviceToKeyMap, id) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 291 | } |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 292 | } |
| 293 | |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 294 | // getOwnershipKey returns the ownership key that the id param uses. Ownership key is the parent |
| 295 | // device Id of a child device or the rootdevice of a logical device. This function also returns the |
| 296 | // id in string format of the id param via the ref output as well as if the data was retrieved from cache |
| 297 | func (da *DeviceOwnership) getOwnershipKey(id interface{}) (ownershipKey string, ref string, cached bool, err error) { |
khenaidoo | 3d3b8c2 | 2019-05-22 18:10:39 -0400 | [diff] [blame] | 298 | |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 299 | if id == nil { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 300 | return "", "", false, status.Error(codes.InvalidArgument, "nil-id") |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 301 | } |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 302 | da.deviceToKeyMapLock.RLock() |
| 303 | defer da.deviceToKeyMapLock.RUnlock() |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 304 | var device *voltha.Device |
| 305 | var lDevice *voltha.LogicalDevice |
| 306 | // The id can either be a device Id or a logical device id. |
| 307 | if dId, ok := id.(*utils.DeviceID); ok { |
| 308 | // Use cache if present |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 309 | if val, exist := da.deviceToKeyMap[dId.Id]; exist { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 310 | return val, dId.Id, true, nil |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 311 | } |
| 312 | if device, _ = da.deviceMgr.GetDevice(dId.Id); device == nil { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 313 | return "", dId.Id, false, status.Errorf(codes.NotFound, "id-absent-%s", dId) |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 314 | } |
| 315 | if device.Root { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 316 | return device.Id, dId.Id, false, nil |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 317 | } else { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 318 | return device.ParentId, dId.Id, false, nil |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 319 | } |
| 320 | } else if ldId, ok := id.(*utils.LogicalDeviceID); ok { |
| 321 | // Use cache if present |
khenaidoo | 8f47419 | 2019-04-03 17:20:44 -0400 | [diff] [blame] | 322 | if val, exist := da.deviceToKeyMap[ldId.Id]; exist { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 323 | return val, ldId.Id, true, nil |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 324 | } |
| 325 | if lDevice, _ = da.logicalDeviceMgr.getLogicalDevice(ldId.Id); lDevice == nil { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 326 | return "", ldId.Id, false, status.Errorf(codes.NotFound, "id-absent-%s", dId) |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 327 | } |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 328 | return lDevice.RootDeviceId, ldId.Id, false, nil |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 329 | } |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 330 | return "", "", false, status.Error(codes.NotFound, fmt.Sprintf("id-%v", id)) |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 331 | } |