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