blob: b616a90f764d97477454ac467fda4654493d3a9b [file] [log] [blame]
khenaidoofffcc8a2019-03-13 11:54:41 -04001/*
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 */
npujar1d86a522019-11-14 17:11:16 +053016
khenaidoofffcc8a2019-03-13 11:54:41 -040017package core
18
19import (
20 "context"
21 "fmt"
npujar1d86a522019-11-14 17:11:16 +053022 "sync"
23 "time"
24
khenaidoo1ce37ad2019-03-24 22:07:24 -040025 "github.com/opencord/voltha-go/rw_core/utils"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080026 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
27 "github.com/opencord/voltha-lib-go/v3/pkg/log"
28 "github.com/opencord/voltha-protos/v3/go/voltha"
khenaidoofffcc8a2019-03-13 11:54:41 -040029 "google.golang.org/grpc/codes"
30 "google.golang.org/grpc/status"
khenaidoofffcc8a2019-03-13 11:54:41 -040031)
32
khenaidoo1ce37ad2019-03-24 22:07:24 -040033func init() {
npujar1d86a522019-11-14 17:11:16 +053034 _, 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 }
khenaidoo1ce37ad2019-03-24 22:07:24 -040038}
39
khenaidoofffcc8a2019-03-13 11:54:41 -040040type ownership struct {
41 id string
42 owned bool
43 chnl chan int
44}
45
npujar1d86a522019-11-14 17:11:16 +053046// DeviceOwnership represent device ownership attributes
khenaidoofffcc8a2019-03-13 11:54:41 -040047type DeviceOwnership struct {
npujar1d86a522019-11-14 17:11:16 +053048 instanceID string
khenaidoofffcc8a2019-03-13 11:54:41 -040049 exitChannel chan int
50 kvClient kvstore.Client
51 reservationTimeout int64 // Duration in seconds
52 ownershipPrefix string
khenaidoo1ce37ad2019-03-24 22:07:24 -040053 deviceMgr *DeviceManager
54 logicalDeviceMgr *LogicalDeviceManager
khenaidoofffcc8a2019-03-13 11:54:41 -040055 deviceMap map[string]*ownership
khenaidoo631fe542019-05-31 15:44:43 -040056 deviceMapLock sync.RWMutex
khenaidoo1ce37ad2019-03-24 22:07:24 -040057 deviceToKeyMap map[string]string
khenaidoo631fe542019-05-31 15:44:43 -040058 deviceToKeyMapLock sync.RWMutex
59 ownershipLock sync.RWMutex
khenaidoofffcc8a2019-03-13 11:54:41 -040060}
61
npujar1d86a522019-11-14 17:11:16 +053062// NewDeviceOwnership creates device ownership instance
khenaidoo1ce37ad2019-03-24 22:07:24 -040063func NewDeviceOwnership(id string, kvClient kvstore.Client, deviceMgr *DeviceManager, logicalDeviceMgr *LogicalDeviceManager, ownershipPrefix string, reservationTimeout int64) *DeviceOwnership {
khenaidoofffcc8a2019-03-13 11:54:41 -040064 var deviceOwnership DeviceOwnership
npujar1d86a522019-11-14 17:11:16 +053065 deviceOwnership.instanceID = id
khenaidoofffcc8a2019-03-13 11:54:41 -040066 deviceOwnership.exitChannel = make(chan int, 1)
67 deviceOwnership.kvClient = kvClient
khenaidoo1ce37ad2019-03-24 22:07:24 -040068 deviceOwnership.deviceMgr = deviceMgr
69 deviceOwnership.logicalDeviceMgr = logicalDeviceMgr
khenaidoofffcc8a2019-03-13 11:54:41 -040070 deviceOwnership.ownershipPrefix = ownershipPrefix
71 deviceOwnership.reservationTimeout = reservationTimeout
72 deviceOwnership.deviceMap = make(map[string]*ownership)
khenaidoo631fe542019-05-31 15:44:43 -040073 deviceOwnership.deviceMapLock = sync.RWMutex{}
khenaidoo1ce37ad2019-03-24 22:07:24 -040074 deviceOwnership.deviceToKeyMap = make(map[string]string)
khenaidoo631fe542019-05-31 15:44:43 -040075 deviceOwnership.deviceToKeyMapLock = sync.RWMutex{}
76 deviceOwnership.ownershipLock = sync.RWMutex{}
khenaidoofffcc8a2019-03-13 11:54:41 -040077 return &deviceOwnership
78}
79
npujar1d86a522019-11-14 17:11:16 +053080// Start starts device device ownership
khenaidoofffcc8a2019-03-13 11:54:41 -040081func (da *DeviceOwnership) Start(ctx context.Context) {
npujar1d86a522019-11-14 17:11:16 +053082 log.Info("starting-deviceOwnership", log.Fields{"instanceId": da.instanceID})
khenaidoofffcc8a2019-03-13 11:54:41 -040083 log.Info("deviceOwnership-started")
84}
85
npujar1d86a522019-11-14 17:11:16 +053086// Stop stops device ownership
khenaidoofffcc8a2019-03-13 11:54:41 -040087func (da *DeviceOwnership) Stop(ctx context.Context) {
88 log.Info("stopping-deviceOwnership")
89 da.exitChannel <- 1
90 // Need to flush all device reservations
khenaidoo1ce37ad2019-03-24 22:07:24 -040091 da.abandonAllDevices()
khenaidoofffcc8a2019-03-13 11:54:41 -040092 log.Info("deviceOwnership-stopped")
93}
94
npujar467fe752020-01-16 20:17:45 +053095func (da *DeviceOwnership) tryToReserveKey(ctx context.Context, id string) bool {
khenaidoofffcc8a2019-03-13 11:54:41 -040096 var currOwner string
khenaidoo1ce37ad2019-03-24 22:07:24 -040097 //Try to reserve the key
khenaidoofffcc8a2019-03-13 11:54:41 -040098 kvKey := fmt.Sprintf("%s_%s", da.ownershipPrefix, id)
npujar467fe752020-01-16 20:17:45 +053099 value, err := da.kvClient.Reserve(ctx, kvKey, da.instanceID, da.reservationTimeout)
khenaidoo1ce37ad2019-03-24 22:07:24 -0400100 if err != nil {
npujar1d86a522019-11-14 17:11:16 +0530101 log.Errorw("error", log.Fields{"error": err, "id": id, "instanceId": da.instanceID})
khenaidoo1ce37ad2019-03-24 22:07:24 -0400102 }
khenaidoofffcc8a2019-03-13 11:54:41 -0400103 if value != nil {
104 if currOwner, err = kvstore.ToString(value); err != nil {
105 log.Error("unexpected-owner-type")
106 }
npujar1d86a522019-11-14 17:11:16 +0530107 return currOwner == da.instanceID
khenaidoofffcc8a2019-03-13 11:54:41 -0400108 }
109 return false
110}
111
npujar467fe752020-01-16 20:17:45 +0530112func (da *DeviceOwnership) renewReservation(ctx context.Context, id string) bool {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400113 // Try to reserve the key
114 kvKey := fmt.Sprintf("%s_%s", da.ownershipPrefix, id)
npujar467fe752020-01-16 20:17:45 +0530115 if err := da.kvClient.RenewReservation(ctx, kvKey); err != nil {
npujar1d86a522019-11-14 17:11:16 +0530116 log.Errorw("reservation-renewal-error", log.Fields{"error": err, "instance": da.instanceID})
khenaidoo1ce37ad2019-03-24 22:07:24 -0400117 return false
118 }
119 return true
120}
Richard Jankowski199fd862019-03-18 14:49:51 -0400121
npujar467fe752020-01-16 20:17:45 +0530122func (da *DeviceOwnership) monitorOwnership(ctx context.Context, id string, chnl chan int) {
khenaidoo4554f7c2019-05-29 22:13:15 -0400123 log.Debugw("start-device-monitoring", log.Fields{"id": id})
khenaidoo1ce37ad2019-03-24 22:07:24 -0400124 op := "starting"
125 exit := false
126 ticker := time.NewTicker(time.Duration(da.reservationTimeout) / 3 * time.Second)
khenaidoofffcc8a2019-03-13 11:54:41 -0400127 for {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400128 select {
129 case <-da.exitChannel:
khenaidoo4554f7c2019-05-29 22:13:15 -0400130 log.Debugw("closing-monitoring", log.Fields{"Id": id})
khenaidoo1ce37ad2019-03-24 22:07:24 -0400131 exit = true
132 case <-ticker.C:
133 log.Debugw(fmt.Sprintf("%s-reservation", op), log.Fields{"Id": id})
134 case <-chnl:
khenaidoo4554f7c2019-05-29 22:13:15 -0400135 log.Debugw("closing-device-monitoring", log.Fields{"Id": id})
khenaidoo1ce37ad2019-03-24 22:07:24 -0400136 exit = true
137 }
138 if exit {
khenaidoo4554f7c2019-05-29 22:13:15 -0400139 log.Infow("exiting-device-monitoring", log.Fields{"Id": id})
khenaidoo1ce37ad2019-03-24 22:07:24 -0400140 ticker.Stop()
141 break
142 }
143 deviceOwned, ownedByMe := da.getOwnership(id)
144 if deviceOwned && ownedByMe {
Richard Jankowski199fd862019-03-18 14:49:51 -0400145 // Device owned; renew reservation
146 op = "renew"
npujar467fe752020-01-16 20:17:45 +0530147 if da.renewReservation(ctx, id) {
npujar1d86a522019-11-14 17:11:16 +0530148 log.Debugw("reservation-renewed", log.Fields{"id": id, "instanceId": da.instanceID})
khenaidoo1ce37ad2019-03-24 22:07:24 -0400149 } else {
npujar1d86a522019-11-14 17:11:16 +0530150 log.Debugw("reservation-not-renewed", log.Fields{"id": id, "instanceId": da.instanceID})
Richard Jankowski199fd862019-03-18 14:49:51 -0400151 }
152 } else {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400153 // Device not owned or not owned by me; try to seize ownership
Richard Jankowski199fd862019-03-18 14:49:51 -0400154 op = "retry"
npujar467fe752020-01-16 20:17:45 +0530155 if err := da.setOwnership(id, da.tryToReserveKey(ctx, id)); err != nil {
Richard Jankowski199fd862019-03-18 14:49:51 -0400156 log.Errorw("unexpected-error", log.Fields{"error": err})
157 }
khenaidoofffcc8a2019-03-13 11:54:41 -0400158 }
khenaidoofffcc8a2019-03-13 11:54:41 -0400159 }
khenaidoo4554f7c2019-05-29 22:13:15 -0400160 log.Debugw("device-monitoring-stopped", log.Fields{"id": id})
khenaidoofffcc8a2019-03-13 11:54:41 -0400161}
162
khenaidoo1ce37ad2019-03-24 22:07:24 -0400163func (da *DeviceOwnership) getOwnership(id string) (bool, bool) {
khenaidoofffcc8a2019-03-13 11:54:41 -0400164 da.deviceMapLock.RLock()
165 defer da.deviceMapLock.RUnlock()
166 if val, exist := da.deviceMap[id]; exist {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400167 return true, val.owned
khenaidoofffcc8a2019-03-13 11:54:41 -0400168 }
khenaidoo1ce37ad2019-03-24 22:07:24 -0400169 return false, false
khenaidoofffcc8a2019-03-13 11:54:41 -0400170}
171
172func (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
npujar1d86a522019-11-14 17:11:16 +0530185// GetAllDeviceIdsOwnedByMe returns all the deviceIds (root device Ids) that is managed by this Core
khenaidooba6b6c42019-08-02 09:11:56 -0400186func (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
khenaidoo09771ef2019-10-11 14:25:02 -0400198// OwnedByMe returns whether this Core instance active owns this device. This function will automatically
khenaidoofffcc8a2019-03-13 11:54:41 -0400199// trigger the process to monitor the device and update the device ownership regularly.
npujar467fe752020-01-16 20:17:45 +0530200func (da *DeviceOwnership) OwnedByMe(ctx context.Context, id interface{}) (bool, error) {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400201 // Retrieve the ownership key based on the id
202 var ownershipKey string
203 var err error
khenaidoo6d62c002019-05-15 21:57:03 -0400204 var idStr string
205 var cache bool
npujar467fe752020-01-16 20:17:45 +0530206 if ownershipKey, idStr, cache, err = da.getOwnershipKey(ctx, id); err != nil {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400207 log.Warnw("no-ownershipkey", log.Fields{"error": err})
khenaidoo09771ef2019-10-11 14:25:02 -0400208 return false, err
khenaidoo1ce37ad2019-03-24 22:07:24 -0400209 }
210
khenaidoo6d62c002019-05-15 21:57:03 -0400211 // 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
khenaidoo4554f7c2019-05-29 22:13:15 -0400218 // 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
khenaidoo1ce37ad2019-03-24 22:07:24 -0400224 deviceOwned, ownedByMe := da.getOwnership(ownershipKey)
225 if deviceOwned {
khenaidoo3d3b8c22019-05-22 18:10:39 -0400226 log.Debugw("ownership", log.Fields{"Id": ownershipKey, "owned": ownedByMe})
khenaidoo09771ef2019-10-11 14:25:02 -0400227 return ownedByMe, nil
khenaidoo1ce37ad2019-03-24 22:07:24 -0400228 }
khenaidoo6d62c002019-05-15 21:57:03 -0400229 // Not owned by me or maybe nobody else. Try to reserve it
npujar467fe752020-01-16 20:17:45 +0530230 reservedByMe := da.tryToReserveKey(ctx, ownershipKey)
khenaidoo1ce37ad2019-03-24 22:07:24 -0400231 myChnl := make(chan int)
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400232
233 da.deviceMapLock.Lock()
khenaidoo6d62c002019-05-15 21:57:03 -0400234 da.deviceMap[ownershipKey] = &ownership{
235 id: ownershipKey,
236 owned: reservedByMe,
237 chnl: myChnl}
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400238 da.deviceMapLock.Unlock()
239
khenaidoo1ce37ad2019-03-24 22:07:24 -0400240 log.Debugw("set-new-ownership", log.Fields{"Id": ownershipKey, "owned": reservedByMe})
npujar467fe752020-01-16 20:17:45 +0530241 go da.monitorOwnership(context.Background(), ownershipKey, myChnl)
khenaidoo09771ef2019-10-11 14:25:02 -0400242 return reservedByMe, nil
khenaidoofffcc8a2019-03-13 11:54:41 -0400243}
244
245//AbandonDevice must be invoked whenever a device is deleted from the Core
246func (da *DeviceOwnership) AbandonDevice(id string) error {
khenaidoo6d62c002019-05-15 21:57:03 -0400247 if id == "" {
248 return status.Error(codes.FailedPrecondition, "id-nil")
249 }
khenaidoofffcc8a2019-03-13 11:54:41 -0400250 da.deviceMapLock.Lock()
251 defer da.deviceMapLock.Unlock()
npujar1d86a522019-11-14 17:11:16 +0530252 o, exist := da.deviceMap[id]
253 if exist { // id is ownership key
khenaidoo0a822f92019-05-08 15:15:57 -0400254 // 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 }
khenaidoo6d62c002019-05-15 21:57:03 -0400262 // Remove the device reference from the deviceMap
khenaidoo0a822f92019-05-08 15:15:57 -0400263 delete(da.deviceMap, id)
264
khenaidoofffcc8a2019-03-13 11:54:41 -0400265 // 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 }
npujar1d86a522019-11-14 17:11:16 +0530271 // id is not ownership key
272 da.deleteDeviceKey(id)
khenaidoo0a822f92019-05-08 15:15:57 -0400273 return nil
Richard Jankowski199fd862019-03-18 14:49:51 -0400274}
khenaidoo1ce37ad2019-03-24 22:07:24 -0400275
276//abandonAllDevices must be invoked whenever a device is deleted from the Core
277func (da *DeviceOwnership) abandonAllDevices() {
278 da.deviceMapLock.Lock()
279 defer da.deviceMapLock.Unlock()
khenaidoo0a822f92019-05-08 15:15:57 -0400280 da.deviceToKeyMapLock.Lock()
281 defer da.deviceToKeyMapLock.Unlock()
npujar1d86a522019-11-14 17:11:16 +0530282 for k := range da.deviceToKeyMap {
khenaidoo0a822f92019-05-08 15:15:57 -0400283 delete(da.deviceToKeyMap, k)
284 }
khenaidoo1ce37ad2019-03-24 22:07:24 -0400285 for _, val := range da.deviceMap {
286 close(val.chnl)
287 }
288}
289
khenaidoo6d62c002019-05-15 21:57:03 -0400290func (da *DeviceOwnership) deleteDeviceKey(id string) {
khenaidoo0a822f92019-05-08 15:15:57 -0400291 da.deviceToKeyMapLock.Lock()
292 defer da.deviceToKeyMapLock.Unlock()
293 if _, exist := da.deviceToKeyMap[id]; exist {
294 delete(da.deviceToKeyMap, id)
khenaidoo0a822f92019-05-08 15:15:57 -0400295 }
khenaidoo0a822f92019-05-08 15:15:57 -0400296}
297
khenaidoo6d62c002019-05-15 21:57:03 -0400298// 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
npujar467fe752020-01-16 20:17:45 +0530301func (da *DeviceOwnership) getOwnershipKey(ctx context.Context, id interface{}) (ownershipKey string, ref string, cached bool, err error) {
khenaidoo3d3b8c22019-05-22 18:10:39 -0400302
khenaidoo1ce37ad2019-03-24 22:07:24 -0400303 if id == nil {
khenaidoo6d62c002019-05-15 21:57:03 -0400304 return "", "", false, status.Error(codes.InvalidArgument, "nil-id")
khenaidoo1ce37ad2019-03-24 22:07:24 -0400305 }
khenaidoo6d62c002019-05-15 21:57:03 -0400306 da.deviceToKeyMapLock.RLock()
307 defer da.deviceToKeyMapLock.RUnlock()
khenaidoo1ce37ad2019-03-24 22:07:24 -0400308 var device *voltha.Device
309 var lDevice *voltha.LogicalDevice
310 // The id can either be a device Id or a logical device id.
npujar1d86a522019-11-14 17:11:16 +0530311 if dID, ok := id.(*utils.DeviceID); ok {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400312 // Use cache if present
npujar1d86a522019-11-14 17:11:16 +0530313 if val, exist := da.deviceToKeyMap[dID.ID]; exist {
314 return val, dID.ID, true, nil
khenaidoo1ce37ad2019-03-24 22:07:24 -0400315 }
npujar467fe752020-01-16 20:17:45 +0530316 if device, _ = da.deviceMgr.GetDevice(ctx, dID.ID); device == nil {
npujar1d86a522019-11-14 17:11:16 +0530317 return "", dID.ID, false, status.Errorf(codes.NotFound, "id-absent-%s", dID)
khenaidoo1ce37ad2019-03-24 22:07:24 -0400318 }
319 if device.Root {
npujar1d86a522019-11-14 17:11:16 +0530320 return device.Id, dID.ID, false, nil
khenaidoo1ce37ad2019-03-24 22:07:24 -0400321 }
npujar1d86a522019-11-14 17:11:16 +0530322 return device.ParentId, dID.ID, false, nil
323 } else if ldID, ok := id.(*utils.LogicalDeviceID); ok {
khenaidoo1ce37ad2019-03-24 22:07:24 -0400324 // Use cache if present
npujar1d86a522019-11-14 17:11:16 +0530325 if val, exist := da.deviceToKeyMap[ldID.ID]; exist {
326 return val, ldID.ID, true, nil
khenaidoo1ce37ad2019-03-24 22:07:24 -0400327 }
npujar467fe752020-01-16 20:17:45 +0530328 if lDevice, _ = da.logicalDeviceMgr.getLogicalDevice(ctx, ldID.ID); lDevice == nil {
npujar1d86a522019-11-14 17:11:16 +0530329 return "", ldID.ID, false, status.Errorf(codes.NotFound, "id-absent-%s", dID)
khenaidoo1ce37ad2019-03-24 22:07:24 -0400330 }
npujar1d86a522019-11-14 17:11:16 +0530331 return lDevice.RootDeviceId, ldID.ID, false, nil
khenaidoo1ce37ad2019-03-24 22:07:24 -0400332 }
khenaidoo6d62c002019-05-15 21:57:03 -0400333 return "", "", false, status.Error(codes.NotFound, fmt.Sprintf("id-%v", id))
khenaidoo1ce37ad2019-03-24 22:07:24 -0400334}