khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [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 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 17 | package adapter |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 18 | |
| 19 | import ( |
| 20 | "context" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 21 | "errors" |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 22 | "fmt" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 23 | "sync" |
Kent Hagerman | 16ce36a | 2019-12-17 13:40:53 -0500 | [diff] [blame] | 24 | "time" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 25 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 26 | "github.com/opencord/voltha-lib-go/v7/pkg/db" |
| 27 | vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc" |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 28 | "github.com/opencord/voltha-protos/v5/go/adapter_service" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 29 | "github.com/opencord/voltha-protos/v5/go/common" |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 30 | "github.com/opencord/voltha-protos/v5/go/core_adapter" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 31 | |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 32 | "github.com/gogo/protobuf/proto" |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 33 | "github.com/golang/protobuf/ptypes/empty" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 34 | "github.com/opencord/voltha-go/db/model" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 35 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 36 | "github.com/opencord/voltha-lib-go/v7/pkg/probe" |
| 37 | "github.com/opencord/voltha-protos/v5/go/voltha" |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 38 | "google.golang.org/grpc/codes" |
| 39 | "google.golang.org/grpc/status" |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 40 | ) |
| 41 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 42 | // Manager represents adapter manager attributes |
| 43 | type Manager struct { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 44 | adapterAgents map[string]*agent |
| 45 | adapterEndpoints map[Endpoint]*agent |
| 46 | deviceTypes map[string]*voltha.DeviceType |
| 47 | adapterDbProxy *model.Proxy |
| 48 | deviceTypeDbProxy *model.Proxy |
| 49 | onAdapterRestart vgrpc.RestartedHandler |
| 50 | endpointMgr EndpointManager |
| 51 | lockAdapterAgentsMap sync.RWMutex |
| 52 | lockDeviceTypesMap sync.RWMutex |
| 53 | lockAdapterEndPointsMap sync.RWMutex |
| 54 | liveProbeInterval time.Duration |
khenaidoo | 25057da | 2021-12-08 14:40:45 -0500 | [diff] [blame] | 55 | coreEndpoint string |
Girish Gowdra | 11ddb23 | 2022-05-26 12:19:59 -0700 | [diff] [blame] | 56 | rollingUpdateMap map[string]bool |
| 57 | rollingUpdateLock sync.RWMutex |
| 58 | rxStreamCloseChMap map[string]chan bool |
| 59 | rxStreamCloseChLock sync.RWMutex |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 60 | } |
| 61 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 62 | // SetAdapterRestartedCallback is used to set the callback that needs to be invoked on an adapter restart |
| 63 | func (aMgr *Manager) SetAdapterRestartedCallback(onAdapterRestart vgrpc.RestartedHandler) { |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 64 | aMgr.onAdapterRestart = onAdapterRestart |
| 65 | } |
| 66 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 67 | func NewAdapterManager( |
khenaidoo | 25057da | 2021-12-08 14:40:45 -0500 | [diff] [blame] | 68 | coreEndpoint string, |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 69 | dbPath *model.Path, |
| 70 | coreInstanceID string, |
| 71 | backend *db.Backend, |
| 72 | liveProbeInterval time.Duration, |
| 73 | ) *Manager { |
| 74 | return &Manager{ |
Girish Gowdra | 11ddb23 | 2022-05-26 12:19:59 -0700 | [diff] [blame] | 75 | adapterDbProxy: dbPath.Proxy("adapters"), |
| 76 | deviceTypeDbProxy: dbPath.Proxy("device_types"), |
| 77 | deviceTypes: make(map[string]*voltha.DeviceType), |
| 78 | adapterAgents: make(map[string]*agent), |
| 79 | adapterEndpoints: make(map[Endpoint]*agent), |
| 80 | endpointMgr: NewEndpointManager(backend), |
| 81 | liveProbeInterval: liveProbeInterval, |
| 82 | coreEndpoint: coreEndpoint, |
| 83 | rollingUpdateMap: make(map[string]bool), |
| 84 | rxStreamCloseChMap: make(map[string]chan bool), |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | func (aMgr *Manager) Start(ctx context.Context, serviceName string) { |
| 89 | probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusPreparing) |
| 90 | logger.Infow(ctx, "starting-service", log.Fields{"service": serviceName}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 91 | |
| 92 | // Load the existing adapterAgents and device types - this will also ensure the correct paths have been |
| 93 | // created if there are no data in the dB to start |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 94 | err := aMgr.loadAdaptersAndDevicetypesInMemory(ctx) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 95 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 96 | logger.Fatalw(ctx, "failed-to-load-adapters-and-device-types-in-memory", log.Fields{"service": serviceName, "error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 97 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 98 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 99 | probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusRunning) |
| 100 | logger.Infow(ctx, "service-started", log.Fields{"service": serviceName}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 101 | } |
| 102 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 103 | func (aMgr *Manager) Stop(ctx context.Context) { |
| 104 | // Stop all adapters |
| 105 | aMgr.lockAdapterAgentsMap.RLock() |
| 106 | defer aMgr.lockAdapterAgentsMap.RUnlock() |
khenaidoo | a46458b | 2021-12-15 16:50:44 -0500 | [diff] [blame] | 107 | var wg sync.WaitGroup |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 108 | for _, adapterAgent := range aMgr.adapterAgents { |
khenaidoo | a46458b | 2021-12-15 16:50:44 -0500 | [diff] [blame] | 109 | // Run the agent stop in its own go routine to notify to the |
| 110 | // adapters that the Core is no longer a client |
| 111 | wg.Add(1) |
| 112 | go func(agt *agent) { |
| 113 | agt.stop(ctx) |
| 114 | wg.Done() |
| 115 | }(adapterAgent) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 116 | } |
khenaidoo | a46458b | 2021-12-15 16:50:44 -0500 | [diff] [blame] | 117 | // Wait for all tests to complete |
| 118 | wg.Wait() |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 119 | } |
| 120 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 121 | func (aMgr *Manager) GetAdapterEndpoint(ctx context.Context, deviceID string, deviceType string) (string, error) { |
| 122 | endPoint, err := aMgr.endpointMgr.GetEndpoint(ctx, deviceID, deviceType) |
| 123 | if err != nil { |
| 124 | return "", err |
| 125 | } |
| 126 | return string(endPoint), nil |
| 127 | } |
| 128 | |
| 129 | func (aMgr *Manager) GetAdapterWithEndpoint(ctx context.Context, endPoint string) (*voltha.Adapter, error) { |
| 130 | aMgr.lockAdapterEndPointsMap.RLock() |
| 131 | agent, have := aMgr.adapterEndpoints[Endpoint(endPoint)] |
| 132 | aMgr.lockAdapterEndPointsMap.RUnlock() |
Kent Hagerman | 16ce36a | 2019-12-17 13:40:53 -0500 | [diff] [blame] | 133 | |
| 134 | if have { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 135 | return agent.getAdapter(ctx), nil |
Kent Hagerman | 16ce36a | 2019-12-17 13:40:53 -0500 | [diff] [blame] | 136 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 137 | |
| 138 | return nil, errors.New("Not found") |
| 139 | } |
| 140 | |
| 141 | func (aMgr *Manager) GetAdapterNameWithEndpoint(ctx context.Context, endPoint string) (string, error) { |
| 142 | aMgr.lockAdapterEndPointsMap.RLock() |
| 143 | agent, have := aMgr.adapterEndpoints[Endpoint(endPoint)] |
| 144 | aMgr.lockAdapterEndPointsMap.RUnlock() |
| 145 | |
| 146 | if have { |
| 147 | return agent.adapter.Id, nil |
| 148 | } |
| 149 | |
| 150 | return "", errors.New("Not found") |
| 151 | } |
| 152 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 153 | func (aMgr *Manager) GetAdapterClient(_ context.Context, endpoint string) (adapter_service.AdapterServiceClient, error) { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 154 | if endpoint == "" { |
| 155 | return nil, errors.New("endpoint-cannot-be-empty") |
| 156 | } |
| 157 | aMgr.lockAdapterEndPointsMap.RLock() |
| 158 | defer aMgr.lockAdapterEndPointsMap.RUnlock() |
| 159 | |
| 160 | if agent, have := aMgr.adapterEndpoints[Endpoint(endpoint)]; have { |
| 161 | return agent.getClient() |
| 162 | } |
| 163 | |
| 164 | return nil, fmt.Errorf("Endpoint-not-found-%s", endpoint) |
Kent Hagerman | 16ce36a | 2019-12-17 13:40:53 -0500 | [diff] [blame] | 165 | } |
| 166 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 167 | func (aMgr *Manager) addAdapter(ctx context.Context, adapter *voltha.Adapter, saveToDb bool) error { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 168 | aMgr.lockAdapterAgentsMap.Lock() |
| 169 | aMgr.lockAdapterEndPointsMap.Lock() |
| 170 | defer aMgr.lockAdapterEndPointsMap.Unlock() |
| 171 | defer aMgr.lockAdapterAgentsMap.Unlock() |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 172 | logger.Debugw(ctx, "adding-adapter", log.Fields{"adapterId": adapter.Id, "vendor": adapter.Vendor, |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 173 | "currentReplica": adapter.CurrentReplica, "totalReplicas": adapter.TotalReplicas, "endpoint": adapter.Endpoint}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 174 | if _, exist := aMgr.adapterAgents[adapter.Id]; !exist { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 175 | if saveToDb { |
| 176 | // Save the adapter to the KV store - first check if it already exist |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 177 | if have, err := aMgr.adapterDbProxy.Get(log.WithSpanFromContext(context.Background(), ctx), adapter.Id, &voltha.Adapter{}); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 178 | logger.Errorw(ctx, "failed-to-get-adapters-from-cluster-proxy", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 179 | return err |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 180 | } else if !have { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 181 | if err := aMgr.adapterDbProxy.Set(log.WithSpanFromContext(context.Background(), ctx), adapter.Id, adapter); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 182 | logger.Errorw(ctx, "failed-to-save-adapter", log.Fields{"adapterId": adapter.Id, "vendor": adapter.Vendor, |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 183 | "currentReplica": adapter.CurrentReplica, "totalReplicas": adapter.TotalReplicas, "endpoint": adapter.Endpoint, "replica": adapter.CurrentReplica, "total": adapter.TotalReplicas}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 184 | return err |
| 185 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 186 | logger.Debugw(ctx, "adapter-saved-to-KV-Store", log.Fields{"adapterId": adapter.Id, "vendor": adapter.Vendor, |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 187 | "currentReplica": adapter.CurrentReplica, "totalReplicas": adapter.TotalReplicas, "endpoint": adapter.Endpoint, "replica": adapter.CurrentReplica, "total": adapter.TotalReplicas}) |
| 188 | } else { |
Girish Kumar | 3e8ee21 | 2020-08-19 17:50:11 +0000 | [diff] [blame] | 189 | logger.Warnw(ctx, "adding-adapter-already-in-KV-store", log.Fields{ |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 190 | "adapterName": adapter.Id, |
| 191 | "adapterReplica": adapter.CurrentReplica, |
| 192 | }) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 193 | } |
| 194 | } |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 195 | clonedAdapter := (proto.Clone(adapter)).(*voltha.Adapter) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 196 | // Use a muted adapter restart handler which is invoked by the corresponding gRPC client on an adapter restart. |
| 197 | // This handler just log the restart event. The actual action taken following an adapter restart |
| 198 | // will be done when an adapter re-registers itself. |
khenaidoo | 25057da | 2021-12-08 14:40:45 -0500 | [diff] [blame] | 199 | aMgr.adapterAgents[adapter.Id] = newAdapterAgent(aMgr.coreEndpoint, clonedAdapter, aMgr.mutedAdapterRestartedHandler, aMgr.liveProbeInterval) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 200 | aMgr.adapterEndpoints[Endpoint(adapter.Endpoint)] = aMgr.adapterAgents[adapter.Id] |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 201 | } |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 202 | return nil |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 203 | } |
| 204 | |
Girish Gowdra | 11ddb23 | 2022-05-26 12:19:59 -0700 | [diff] [blame] | 205 | func (aMgr *Manager) updateAdapter(ctx context.Context, adapter *voltha.Adapter, saveToDb bool) error { |
| 206 | aMgr.lockAdapterAgentsMap.Lock() |
| 207 | defer aMgr.lockAdapterAgentsMap.Unlock() |
| 208 | logger.Debugw(ctx, "updating-adapter", log.Fields{"adapterId": adapter.Id, "vendor": adapter.Vendor, |
| 209 | "currentReplica": adapter.CurrentReplica, "totalReplicas": adapter.TotalReplicas, "endpoint": adapter.Endpoint, |
| 210 | "version": adapter.Version}) |
| 211 | if _, exist := aMgr.adapterAgents[adapter.Id]; !exist { |
| 212 | logger.Errorw(ctx, "adapter-does-not-exist", log.Fields{"adapterName": adapter.Id}) |
| 213 | return fmt.Errorf("does-not-exist") |
| 214 | } |
| 215 | if saveToDb { |
| 216 | // Update the adapter to the KV store |
| 217 | if err := aMgr.adapterDbProxy.Set(log.WithSpanFromContext(context.Background(), ctx), adapter.Id, adapter); err != nil { |
| 218 | logger.Errorw(ctx, "failed-to-update-adapter", log.Fields{"adapterId": adapter.Id, "vendor": adapter.Vendor, |
| 219 | "currentReplica": adapter.CurrentReplica, "totalReplicas": adapter.TotalReplicas, |
| 220 | "endpoint": adapter.Endpoint, "replica": adapter.CurrentReplica, "total": adapter.TotalReplicas, |
| 221 | "version": adapter.Version}) |
| 222 | return err |
| 223 | } |
| 224 | logger.Debugw(ctx, "adapter-updated-to-KV-Store", log.Fields{"adapterId": adapter.Id, "vendor": adapter.Vendor, |
| 225 | "currentReplica": adapter.CurrentReplica, "totalReplicas": adapter.TotalReplicas, "endpoint": adapter.Endpoint, |
| 226 | "replica": adapter.CurrentReplica, "total": adapter.TotalReplicas, "version": adapter.Version}) |
| 227 | } |
| 228 | clonedAdapter := (proto.Clone(adapter)).(*voltha.Adapter) |
| 229 | // Use a muted adapter restart handler which is invoked by the corresponding gRPC client on an adapter restart. |
| 230 | // This handler just log the restart event. The actual action taken following an adapter restart |
| 231 | // will be done when an adapter re-registers itself. |
| 232 | aMgr.adapterAgents[adapter.Id] = newAdapterAgent(aMgr.coreEndpoint, clonedAdapter, aMgr.mutedAdapterRestartedHandler, aMgr.liveProbeInterval) |
| 233 | aMgr.adapterEndpoints[Endpoint(adapter.Endpoint)] = aMgr.adapterAgents[adapter.Id] |
| 234 | return nil |
| 235 | } |
| 236 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 237 | func (aMgr *Manager) addDeviceTypes(ctx context.Context, deviceTypes *voltha.DeviceTypes, saveToDb bool) error { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 238 | if deviceTypes == nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 239 | return fmt.Errorf("no-device-type") |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 240 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 241 | logger.Debugw(ctx, "adding-device-types", log.Fields{"deviceTypes": deviceTypes}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 242 | aMgr.lockAdapterAgentsMap.Lock() |
| 243 | defer aMgr.lockAdapterAgentsMap.Unlock() |
| 244 | aMgr.lockDeviceTypesMap.Lock() |
| 245 | defer aMgr.lockDeviceTypesMap.Unlock() |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 246 | |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 247 | // create an in memory map to fetch the entire voltha.DeviceType from a device.Type string |
| 248 | for _, deviceType := range deviceTypes.Items { |
| 249 | aMgr.deviceTypes[deviceType.Id] = deviceType |
| 250 | } |
| 251 | |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 252 | if saveToDb { |
Kent Hagerman | d9cc2e9 | 2019-11-04 13:28:15 -0500 | [diff] [blame] | 253 | // Save the device types to the KV store |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 254 | for _, deviceType := range deviceTypes.Items { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 255 | if have, err := aMgr.deviceTypeDbProxy.Get(log.WithSpanFromContext(context.Background(), ctx), deviceType.Id, &voltha.DeviceType{}); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 256 | logger.Errorw(ctx, "Failed-to--device-types-from-cluster-data-proxy", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 257 | return err |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 258 | } else if !have { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 259 | // Does not exist - save it |
| 260 | clonedDType := (proto.Clone(deviceType)).(*voltha.DeviceType) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 261 | if err := aMgr.deviceTypeDbProxy.Set(log.WithSpanFromContext(context.Background(), ctx), deviceType.Id, clonedDType); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 262 | logger.Errorw(ctx, "Failed-to-add-device-types-to-cluster-data-proxy", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 263 | return err |
| 264 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 265 | logger.Debugw(ctx, "device-type-saved-to-KV-Store", log.Fields{"deviceType": deviceType}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | } |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 269 | return nil |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 270 | } |
| 271 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 272 | //loadAdaptersAndDevicetypesInMemory loads the existing set of adapters and device types in memory |
| 273 | func (aMgr *Manager) loadAdaptersAndDevicetypesInMemory(ctx context.Context) error { |
| 274 | // Load the adapters |
| 275 | var adapters []*voltha.Adapter |
| 276 | if err := aMgr.adapterDbProxy.List(log.WithSpanFromContext(context.Background(), ctx), &adapters); err != nil { |
| 277 | logger.Errorw(ctx, "Failed-to-list-adapters-from-cluster-data-proxy", log.Fields{"error": err}) |
| 278 | return err |
| 279 | } |
| 280 | |
| 281 | logger.Debugw(ctx, "retrieved-adapters", log.Fields{"count": len(adapters)}) |
| 282 | |
| 283 | if len(adapters) != 0 { |
| 284 | for _, adapter := range adapters { |
| 285 | if err := aMgr.addAdapter(ctx, adapter, false); err != nil { |
| 286 | logger.Errorw(ctx, "failed-to-add-adapter", log.Fields{"adapterId": adapter.Id}) |
| 287 | } else { |
| 288 | logger.Debugw(ctx, "adapter-added-successfully", log.Fields{"adapterId": adapter.Id}) |
| 289 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 290 | } |
| 291 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 292 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 293 | // Load the device types |
| 294 | var deviceTypes []*voltha.DeviceType |
| 295 | if err := aMgr.deviceTypeDbProxy.List(log.WithSpanFromContext(context.Background(), ctx), &deviceTypes); err != nil { |
| 296 | logger.Errorw(ctx, "Failed-to-list-device-types-from-cluster-data-proxy", log.Fields{"error": err}) |
| 297 | return err |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 298 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 299 | |
| 300 | logger.Debugw(ctx, "retrieved-devicetypes", log.Fields{"count": len(deviceTypes)}) |
| 301 | |
| 302 | if len(deviceTypes) != 0 { |
| 303 | dTypes := &voltha.DeviceTypes{Items: []*voltha.DeviceType{}} |
| 304 | for _, dType := range deviceTypes { |
| 305 | logger.Debugw(ctx, "found-existing-device-types", log.Fields{"deviceTypes": deviceTypes}) |
| 306 | dTypes.Items = append(dTypes.Items, dType) |
| 307 | } |
| 308 | if err := aMgr.addDeviceTypes(ctx, dTypes, false); err != nil { |
| 309 | logger.Errorw(ctx, "failed-to-add-device-type", log.Fields{"deviceTypes": deviceTypes}) |
| 310 | } else { |
| 311 | logger.Debugw(ctx, "device-type-added-successfully", log.Fields{"deviceTypes": deviceTypes}) |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // Start the adapter agents - this will trigger the connection to the adapter |
| 316 | aMgr.lockAdapterAgentsMap.RLock() |
| 317 | defer aMgr.lockAdapterAgentsMap.RUnlock() |
| 318 | for _, adapterAgent := range aMgr.adapterAgents { |
| 319 | subCtx := log.WithSpanFromContext(context.Background(), ctx) |
| 320 | if err := adapterAgent.start(subCtx); err != nil { |
| 321 | logger.Errorw(ctx, "failed-to-start-adapter", log.Fields{"adapter-endpoint": adapterAgent.adapterAPIEndPoint}) |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | logger.Debug(ctx, "no-existing-device-type-found") |
| 326 | |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 327 | return nil |
| 328 | } |
| 329 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 330 | func (aMgr *Manager) RegisterAdapter(ctx context.Context, registration *core_adapter.AdapterRegistration) (*empty.Empty, error) { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 331 | adapter := registration.Adapter |
| 332 | deviceTypes := registration.DTypes |
| 333 | logger.Infow(ctx, "RegisterAdapter", log.Fields{"adapter": adapter, "deviceTypes": deviceTypes.Items}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 334 | |
Matteo Scandolo | 57ee9aa | 2020-04-17 09:20:42 -0700 | [diff] [blame] | 335 | if adapter.Type == "" { |
Girish Kumar | 3e8ee21 | 2020-08-19 17:50:11 +0000 | [diff] [blame] | 336 | logger.Errorw(ctx, "adapter-not-specifying-type", log.Fields{ |
Matteo Scandolo | 57ee9aa | 2020-04-17 09:20:42 -0700 | [diff] [blame] | 337 | "adapterId": adapter.Id, |
| 338 | "vendor": adapter.Vendor, |
| 339 | "type": adapter.Type, |
| 340 | }) |
| 341 | return nil, status.Error(codes.InvalidArgument, "adapter-not-specifying-type") |
| 342 | } |
| 343 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 344 | if adpt, _ := aMgr.getAdapter(ctx, adapter.Id); adpt != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 345 | agt, err := aMgr.getAgent(ctx, adpt.Id) |
| 346 | if err != nil { |
| 347 | logger.Errorw(ctx, "no-adapter-agent", log.Fields{"error": err}) |
| 348 | return nil, err |
| 349 | } |
Girish Gowdra | 11ddb23 | 2022-05-26 12:19:59 -0700 | [diff] [blame] | 350 | if adapter.Version != adpt.Version { |
| 351 | // Rolling update scenario - could be downgrade or upgrade |
| 352 | logger.Infow(ctx, "rolling-update", |
| 353 | log.Fields{"adapter": adpt.Id, "endpoint": adpt.Endpoint, "old-version": adpt.Version, "new-version": adapter.Version}) |
| 354 | // Stop the gRPC connection to the old adapter |
| 355 | agt.stop(ctx) |
| 356 | if err = aMgr.updateAdapter(ctx, adapter, true); err != nil { |
| 357 | return nil, err |
| 358 | } |
| 359 | aMgr.SetRollingUpdate(ctx, adapter.Endpoint, true) |
| 360 | } else { |
| 361 | // Adapter registered and version is the same. The adapter may have restarted. |
| 362 | // Trigger the reconcile process for that adapter |
| 363 | logger.Warnw(ctx, "adapter-restarted", log.Fields{"adapter": adpt.Id, "endpoint": adpt.Endpoint}) |
| 364 | agt.resetConnection(ctx) |
| 365 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 366 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 367 | go func() { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 368 | err := aMgr.onAdapterRestart(log.WithSpanFromContext(context.Background(), ctx), adpt.Endpoint) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 369 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 370 | logger.Errorw(ctx, "unable-to-restart-adapter", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 371 | } |
| 372 | }() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 373 | return &empty.Empty{}, nil |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 374 | } |
| 375 | // Save the adapter and the device types |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 376 | if err := aMgr.addAdapter(ctx, adapter, true); err != nil { |
| 377 | logger.Errorw(ctx, "failed-to-add-adapter", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 378 | return nil, err |
| 379 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 380 | if err := aMgr.addDeviceTypes(ctx, deviceTypes, true); err != nil { |
| 381 | logger.Errorw(ctx, "failed-to-add-device-types", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 382 | return nil, err |
| 383 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 384 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 385 | logger.Debugw(ctx, "adapter-registered", log.Fields{"adapterId": adapter.Id, "vendor": adapter.Vendor, |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 386 | "currentReplica": adapter.CurrentReplica, "totalReplicas": adapter.TotalReplicas, "endpoint": adapter.Endpoint}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 387 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 388 | // Setup the endpoints for this adapter |
| 389 | if err := aMgr.endpointMgr.RegisterAdapter(ctx, adapter, deviceTypes); err != nil { |
| 390 | logger.Errorw(ctx, "failed-to-register-adapter", log.Fields{"error": err}) |
| 391 | } |
| 392 | |
| 393 | // Start adapter instance - this will trigger the connection to the adapter |
| 394 | if agent, err := aMgr.getAgent(ctx, adapter.Id); agent != nil { |
| 395 | subCtx := log.WithSpanFromContext(context.Background(), ctx) |
| 396 | if err := agent.start(subCtx); err != nil { |
| 397 | logger.Errorw(ctx, "failed-to-start-adapter", log.Fields{"error": err}) |
| 398 | return nil, err |
| 399 | } |
| 400 | } else { |
| 401 | logger.Fatalw(ctx, "adapter-absent", log.Fields{"error": err, "adapter": adapter.Id}) |
| 402 | } |
| 403 | |
| 404 | return &empty.Empty{}, nil |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 405 | } |
| 406 | |
Girish Gowdra | 11ddb23 | 2022-05-26 12:19:59 -0700 | [diff] [blame] | 407 | func (aMgr *Manager) StartAdapterWithEndPoint(ctx context.Context, endpoint string) error { |
| 408 | aMgr.lockAdapterAgentsMap.RLock() |
| 409 | defer aMgr.lockAdapterAgentsMap.RUnlock() |
| 410 | subCtx := log.WithSpanFromContext(context.Background(), ctx) |
| 411 | for _, adapterAgent := range aMgr.adapterAgents { |
| 412 | if adapterAgent.adapter.Endpoint == endpoint { |
| 413 | if err := adapterAgent.start(subCtx); err != nil { |
| 414 | logger.Errorw(subCtx, "failed-to-start-adapter", log.Fields{"error": err}) |
| 415 | return err |
| 416 | } |
| 417 | return nil |
| 418 | } |
| 419 | } |
| 420 | logger.Errorw(ctx, "adapter-agent-not-found-for-endpoint", log.Fields{"endpoint": endpoint}) |
| 421 | return fmt.Errorf("adapter-agent-not-found-for-endpoint-%s", endpoint) |
| 422 | } |
| 423 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 424 | func (aMgr *Manager) GetAdapterTypeByVendorID(vendorID string) (string, error) { |
| 425 | aMgr.lockDeviceTypesMap.RLock() |
| 426 | defer aMgr.lockDeviceTypesMap.RUnlock() |
| 427 | for _, dType := range aMgr.deviceTypes { |
| 428 | for _, v := range dType.VendorIds { |
| 429 | if v == vendorID { |
| 430 | return dType.AdapterType, nil |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | return "", fmt.Errorf("vendor id %s not found", vendorID) |
| 435 | } |
| 436 | |
| 437 | // GetAdapterType returns the name of the device adapter that services this device type |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 438 | func (aMgr *Manager) GetAdapterType(deviceType string) (string, error) { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 439 | aMgr.lockDeviceTypesMap.Lock() |
| 440 | defer aMgr.lockDeviceTypesMap.Unlock() |
| 441 | for _, dt := range aMgr.deviceTypes { |
| 442 | if deviceType == dt.Id { |
| 443 | return dt.AdapterType, nil |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 444 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 445 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 446 | return "", fmt.Errorf("adapter-not-registered-for-device-type %s", deviceType) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 447 | } |
| 448 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 449 | // ListDeviceTypes returns all the device types known to the system |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 450 | func (aMgr *Manager) ListDeviceTypes(ctx context.Context, _ *empty.Empty) (*voltha.DeviceTypes, error) { |
| 451 | logger.Debug(ctx, "ListDeviceTypes") |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 452 | aMgr.lockDeviceTypesMap.Lock() |
| 453 | defer aMgr.lockDeviceTypesMap.Unlock() |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 454 | |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 455 | deviceTypes := make([]*voltha.DeviceType, 0, len(aMgr.deviceTypes)) |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 456 | for _, deviceType := range aMgr.deviceTypes { |
| 457 | deviceTypes = append(deviceTypes, deviceType) |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 458 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 459 | return &voltha.DeviceTypes{Items: deviceTypes}, nil |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 460 | } |
| 461 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 462 | // GetDeviceType returns the device type proto definition given the name of the device type |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 463 | func (aMgr *Manager) GetDeviceType(ctx context.Context, deviceType *common.ID) (*voltha.DeviceType, error) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 464 | logger.Debugw(ctx, "GetDeviceType", log.Fields{"typeid": deviceType.Id}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 465 | aMgr.lockDeviceTypesMap.Lock() |
| 466 | defer aMgr.lockDeviceTypesMap.Unlock() |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 467 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 468 | dType, exist := aMgr.deviceTypes[deviceType.Id] |
| 469 | if !exist { |
| 470 | return nil, status.Errorf(codes.NotFound, "device_type-%s", deviceType.Id) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 471 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 472 | return dType, nil |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 473 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 474 | |
| 475 | // ListAdapters returns the contents of all adapters known to the system |
| 476 | func (aMgr *Manager) ListAdapters(ctx context.Context, _ *empty.Empty) (*voltha.Adapters, error) { |
| 477 | logger.Debug(ctx, "Listing adapters") |
| 478 | result := &voltha.Adapters{Items: []*voltha.Adapter{}} |
| 479 | aMgr.lockAdapterAgentsMap.RLock() |
| 480 | defer aMgr.lockAdapterAgentsMap.RUnlock() |
| 481 | for _, adapterAgent := range aMgr.adapterAgents { |
| 482 | if a := adapterAgent.getAdapter(ctx); a != nil { |
| 483 | result.Items = append(result.Items, (proto.Clone(a)).(*voltha.Adapter)) |
| 484 | } |
| 485 | } |
| 486 | logger.Debugw(ctx, "Listing adapters", log.Fields{"result": result}) |
| 487 | return result, nil |
| 488 | } |
| 489 | |
Girish Gowdra | 11ddb23 | 2022-05-26 12:19:59 -0700 | [diff] [blame] | 490 | func (aMgr *Manager) GetRollingUpdate(ctx context.Context, endpoint string) (bool, bool) { |
| 491 | aMgr.rollingUpdateLock.RLock() |
| 492 | defer aMgr.rollingUpdateLock.RUnlock() |
| 493 | val, ok := aMgr.rollingUpdateMap[endpoint] |
| 494 | return val, ok |
| 495 | } |
| 496 | |
| 497 | func (aMgr *Manager) SetRollingUpdate(ctx context.Context, endpoint string, status bool) { |
| 498 | aMgr.rollingUpdateLock.Lock() |
| 499 | defer aMgr.rollingUpdateLock.Unlock() |
| 500 | if res, ok := aMgr.rollingUpdateMap[endpoint]; ok { |
| 501 | logger.Warnw(ctx, "possible duplicate rolling update - overwriting", log.Fields{"old-status": res, "endpoint": endpoint}) |
| 502 | } |
| 503 | aMgr.rollingUpdateMap[endpoint] = status |
| 504 | } |
| 505 | |
| 506 | func (aMgr *Manager) DeleteRollingUpdate(ctx context.Context, endpoint string) { |
| 507 | aMgr.rollingUpdateLock.Lock() |
| 508 | defer aMgr.rollingUpdateLock.Unlock() |
| 509 | delete(aMgr.rollingUpdateMap, endpoint) |
| 510 | } |
| 511 | |
| 512 | func (aMgr *Manager) RegisterOnRxStreamCloseChMap(ctx context.Context, endpoint string) { |
| 513 | aMgr.rxStreamCloseChLock.Lock() |
| 514 | defer aMgr.rxStreamCloseChLock.Unlock() |
| 515 | if _, ok := aMgr.rxStreamCloseChMap[endpoint]; ok { |
| 516 | logger.Warnw(ctx, "duplicate entry on rxStreamCloseChMap - overwriting", log.Fields{"endpoint": endpoint}) |
| 517 | // First close the old channel |
| 518 | close(aMgr.rxStreamCloseChMap[endpoint]) |
| 519 | } |
| 520 | aMgr.rxStreamCloseChMap[endpoint] = make(chan bool, 1) |
| 521 | } |
| 522 | |
| 523 | func (aMgr *Manager) SignalOnRxStreamCloseCh(ctx context.Context, endpoint string) { |
| 524 | var closeCh chan bool |
| 525 | ok := false |
| 526 | aMgr.rxStreamCloseChLock.RLock() |
| 527 | if closeCh, ok = aMgr.rxStreamCloseChMap[endpoint]; !ok { |
| 528 | logger.Infow(ctx, "no entry on rxStreamCloseChMap", log.Fields{"endpoint": endpoint}) |
| 529 | aMgr.rxStreamCloseChLock.RUnlock() |
| 530 | return |
| 531 | } |
| 532 | aMgr.rxStreamCloseChLock.RUnlock() |
| 533 | |
| 534 | // close the rx channel |
| 535 | closeCh <- true |
| 536 | |
| 537 | aMgr.rxStreamCloseChLock.Lock() |
| 538 | defer aMgr.rxStreamCloseChLock.Unlock() |
| 539 | delete(aMgr.rxStreamCloseChMap, endpoint) |
| 540 | } |
| 541 | |
| 542 | func (aMgr *Manager) WaitOnRxStreamCloseCh(ctx context.Context, endpoint string) { |
| 543 | var closeCh chan bool |
| 544 | ok := false |
| 545 | aMgr.rxStreamCloseChLock.RLock() |
| 546 | if closeCh, ok = aMgr.rxStreamCloseChMap[endpoint]; !ok { |
| 547 | logger.Warnw(ctx, "no entry on rxStreamCloseChMap", log.Fields{"endpoint": endpoint}) |
| 548 | aMgr.rxStreamCloseChLock.RUnlock() |
| 549 | return |
| 550 | } |
| 551 | aMgr.rxStreamCloseChLock.RUnlock() |
| 552 | |
| 553 | select { |
| 554 | case <-closeCh: |
| 555 | logger.Infow(ctx, "rx stream closed for endpoint", log.Fields{"endpoint": endpoint}) |
| 556 | case <-time.After(60 * time.Second): |
| 557 | logger.Warnw(ctx, "timeout waiting for rx stream close", log.Fields{"endpoint": endpoint}) |
| 558 | } |
| 559 | } |
| 560 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 561 | func (aMgr *Manager) getAgent(ctx context.Context, adapterID string) (*agent, error) { |
| 562 | aMgr.lockAdapterAgentsMap.RLock() |
| 563 | defer aMgr.lockAdapterAgentsMap.RUnlock() |
| 564 | if adapterAgent, ok := aMgr.adapterAgents[adapterID]; ok { |
| 565 | return adapterAgent, nil |
| 566 | } |
| 567 | return nil, errors.New("Not found") |
| 568 | } |
| 569 | |
| 570 | func (aMgr *Manager) getAdapter(ctx context.Context, adapterID string) (*voltha.Adapter, error) { |
| 571 | aMgr.lockAdapterAgentsMap.RLock() |
| 572 | defer aMgr.lockAdapterAgentsMap.RUnlock() |
| 573 | if adapterAgent, ok := aMgr.adapterAgents[adapterID]; ok { |
| 574 | return adapterAgent.getAdapter(ctx), nil |
| 575 | } |
| 576 | return nil, errors.New("Not found") |
| 577 | } |
| 578 | |
| 579 | // mutedAdapterRestartedHandler will be invoked by the grpc client on an adapter restart. |
| 580 | // Since the Adapter will re-register itself and that will trigger the reconcile process, |
| 581 | // therefore this handler does nothing, other than logging the event. |
| 582 | func (aMgr *Manager) mutedAdapterRestartedHandler(ctx context.Context, endpoint string) error { |
| 583 | logger.Infow(ctx, "muted-adapter-restarted", log.Fields{"endpoint": endpoint}) |
| 584 | return nil |
| 585 | } |
| 586 | |
| 587 | func (aMgr *Manager) WaitUntilConnectionsToAdaptersAreUp(ctx context.Context, connectionRetryInterval time.Duration) error { |
| 588 | logger.Infow(ctx, "waiting-for-adapters-to-be-up", log.Fields{"retry-interval": connectionRetryInterval}) |
| 589 | for { |
| 590 | aMgr.lockAdapterAgentsMap.Lock() |
| 591 | numAdapters := len(aMgr.adapterAgents) |
| 592 | if numAdapters == 0 { |
| 593 | // No adapter registered yet |
| 594 | aMgr.lockAdapterAgentsMap.Unlock() |
| 595 | logger.Info(ctx, "no-adapter-registered") |
| 596 | return nil |
| 597 | } |
| 598 | // A case of Core restart |
| 599 | agentsUp := true |
| 600 | adapterloop: |
| 601 | for _, agt := range aMgr.adapterAgents { |
| 602 | agentsUp = agentsUp && agt.IsConnectionUp() |
| 603 | if !agentsUp { |
| 604 | break adapterloop |
| 605 | } |
| 606 | } |
| 607 | aMgr.lockAdapterAgentsMap.Unlock() |
| 608 | if agentsUp { |
| 609 | logger.Infow(ctx, "adapter-connections-ready", log.Fields{"adapter-count": numAdapters}) |
| 610 | return nil |
| 611 | } |
| 612 | logger.Warnw(ctx, "adapter-connections-not-ready", log.Fields{"adapter-count": numAdapters}) |
| 613 | select { |
| 614 | case <-time.After(connectionRetryInterval): |
| 615 | logger.Infow(ctx, "retrying-adapter-connections", log.Fields{"adapter-count": numAdapters}) |
| 616 | continue |
| 617 | case <-ctx.Done(): |
| 618 | logger.Errorw(ctx, "context-timeout", log.Fields{"adapter-count": numAdapters, "err": ctx.Err()}) |
| 619 | return ctx.Err() |
| 620 | } |
| 621 | } |
| 622 | } |