Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-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 holder |
| 18 | |
| 19 | import ( |
| 20 | "sync" |
| 21 | |
| 22 | "github.com/opencord/voltha-protos/v5/go/voltha" |
| 23 | ) |
| 24 | |
| 25 | // VolthaServiceClientHolder provides a consistent (voluntarily unmutable) reference |
| 26 | // point for a mutable value that represents a GRPC service interface to |
| 27 | // VOLTHA |
| 28 | type VolthaServiceClientHolder struct { |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 29 | VolthaSvcClient voltha.VolthaServiceClient |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 30 | mutex sync.RWMutex |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | // VolthaServiceClientReference structure |
| 34 | type VolthaServiceClientReference struct { |
| 35 | } |
| 36 | |
| 37 | // Clear sets the held value to nil (not set) |
| 38 | func (h *VolthaServiceClientHolder) Clear() { |
| 39 | h.mutex.Lock() |
| 40 | defer h.mutex.Unlock() |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 41 | h.VolthaSvcClient = nil |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // Set assigns the value being held to the specified value |
| 45 | func (h *VolthaServiceClientHolder) Set(client voltha.VolthaServiceClient) { |
| 46 | h.mutex.Lock() |
| 47 | defer h.mutex.Unlock() |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 48 | h.VolthaSvcClient = client |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // Get returns the currently held value |
| 52 | func (h *VolthaServiceClientHolder) Get() voltha.VolthaServiceClient { |
| 53 | h.mutex.RLock() |
| 54 | defer h.mutex.RUnlock() |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 55 | return h.VolthaSvcClient |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 56 | } |