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 | |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 17 | package core |
| 18 | |
| 19 | import ( |
| 20 | "context" |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 21 | "fmt" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 22 | "reflect" |
| 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 | |
David Bainbridge | fd27f4b | 2020-03-26 18:27:41 -0700 | [diff] [blame^] | 26 | "github.com/golang/protobuf/ptypes" |
| 27 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
| 28 | |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 29 | "github.com/gogo/protobuf/proto" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 30 | "github.com/opencord/voltha-go/db/model" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 31 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 32 | "github.com/opencord/voltha-lib-go/v3/pkg/probe" |
| 33 | "github.com/opencord/voltha-protos/v3/go/voltha" |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 34 | ) |
| 35 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 36 | // sentinel constants |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 37 | const ( |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 38 | SentinelAdapterID = "adapter_sentinel" |
| 39 | SentinelDevicetypeID = "device_type_sentinel" |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 40 | ) |
| 41 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 42 | // AdapterAgent represents adapter agent |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 43 | type AdapterAgent struct { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 44 | adapter *voltha.Adapter |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 45 | deviceTypes map[string]*voltha.DeviceType |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 46 | lock sync.RWMutex |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | func newAdapterAgent(adapter *voltha.Adapter, deviceTypes *voltha.DeviceTypes) *AdapterAgent { |
| 50 | var adapterAgent AdapterAgent |
| 51 | adapterAgent.adapter = adapter |
| 52 | adapterAgent.lock = sync.RWMutex{} |
| 53 | adapterAgent.deviceTypes = make(map[string]*voltha.DeviceType) |
| 54 | if deviceTypes != nil { |
| 55 | for _, dType := range deviceTypes.Items { |
| 56 | adapterAgent.deviceTypes[dType.Id] = dType |
| 57 | } |
| 58 | } |
| 59 | return &adapterAgent |
| 60 | } |
| 61 | |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 62 | func (aa *AdapterAgent) getDeviceType(deviceType string) *voltha.DeviceType { |
| 63 | aa.lock.RLock() |
| 64 | defer aa.lock.RUnlock() |
| 65 | if _, exist := aa.deviceTypes[deviceType]; exist { |
| 66 | return aa.deviceTypes[deviceType] |
| 67 | } |
| 68 | return nil |
| 69 | } |
| 70 | |
| 71 | func (aa *AdapterAgent) getAdapter() *voltha.Adapter { |
| 72 | aa.lock.RLock() |
| 73 | defer aa.lock.RUnlock() |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 74 | logger.Debugw("getAdapter", log.Fields{"adapter": aa.adapter}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 75 | return aa.adapter |
| 76 | } |
| 77 | |
| 78 | func (aa *AdapterAgent) updateAdapter(adapter *voltha.Adapter) { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 79 | aa.lock.Lock() |
| 80 | defer aa.lock.Unlock() |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 81 | aa.adapter = adapter |
| 82 | } |
| 83 | |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 84 | func (aa *AdapterAgent) updateDeviceType(deviceType *voltha.DeviceType) { |
| 85 | aa.lock.Lock() |
| 86 | defer aa.lock.Unlock() |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 87 | aa.deviceTypes[deviceType.Id] = deviceType |
| 88 | } |
| 89 | |
Kent Hagerman | 16ce36a | 2019-12-17 13:40:53 -0500 | [diff] [blame] | 90 | // updateCommunicationTime updates the message to the specified time. |
| 91 | // No attempt is made to save the time to the db, so only recent times are guaranteed to be accurate. |
| 92 | func (aa *AdapterAgent) updateCommunicationTime(new time.Time) { |
| 93 | // only update if new time is not in the future, and either the old time is invalid or new time > old time |
| 94 | if last, err := ptypes.Timestamp(aa.adapter.LastCommunication); !new.After(time.Now()) && (err != nil || new.After(last)) { |
| 95 | timestamp, err := ptypes.TimestampProto(new) |
| 96 | if err != nil { |
| 97 | return // if the new time cannot be encoded, just ignore it |
| 98 | } |
| 99 | |
| 100 | aa.lock.Lock() |
| 101 | defer aa.lock.Unlock() |
| 102 | aa.adapter.LastCommunication = timestamp |
| 103 | } |
| 104 | } |
| 105 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 106 | // AdapterManager represents adapter manager attributes |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 107 | type AdapterManager struct { |
| 108 | adapterAgents map[string]*AdapterAgent |
| 109 | deviceTypeToAdapterMap map[string]string |
| 110 | clusterDataProxy *model.Proxy |
| 111 | adapterProxy *model.Proxy |
| 112 | deviceTypeProxy *model.Proxy |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 113 | deviceMgr *DeviceManager |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 114 | coreInstanceID string |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 115 | exitChannel chan int |
| 116 | lockAdaptersMap sync.RWMutex |
| 117 | lockdDeviceTypeToAdapterMap sync.RWMutex |
| 118 | } |
| 119 | |
Kent Hagerman | 16ce36a | 2019-12-17 13:40:53 -0500 | [diff] [blame] | 120 | func newAdapterManager(cdProxy *model.Proxy, coreInstanceID string, kafkaClient kafka.Client, deviceMgr *DeviceManager) *AdapterManager { |
| 121 | aMgr := &AdapterManager{ |
| 122 | exitChannel: make(chan int, 1), |
| 123 | coreInstanceID: coreInstanceID, |
| 124 | clusterDataProxy: cdProxy, |
| 125 | adapterAgents: make(map[string]*AdapterAgent), |
| 126 | deviceTypeToAdapterMap: make(map[string]string), |
| 127 | deviceMgr: deviceMgr, |
| 128 | } |
| 129 | kafkaClient.SubscribeForMetadata(aMgr.updateLastAdapterCommunication) |
| 130 | return aMgr |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 131 | } |
| 132 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 133 | func (aMgr *AdapterManager) start(ctx context.Context) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 134 | logger.Info("starting-adapter-manager") |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 135 | |
| 136 | // Load the existing adapterAgents and device types - this will also ensure the correct paths have been |
| 137 | // created if there are no data in the dB to start |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 138 | err := aMgr.loadAdaptersAndDevicetypesInMemory() |
| 139 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 140 | logger.Errorw("Failed-to-load-adapters-and-device-types-in-memeory", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 141 | return err |
| 142 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 143 | |
| 144 | //// Create the proxies |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 145 | aMgr.adapterProxy, err = aMgr.clusterDataProxy.CreateProxy(ctx, "/adapters", false) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 146 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 147 | logger.Errorw("Failed-to-create-adapter-proxy", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 148 | return err |
| 149 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 150 | aMgr.deviceTypeProxy, err = aMgr.clusterDataProxy.CreateProxy(ctx, "/device_types", false) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 151 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 152 | logger.Errorw("Failed-to-create-device-proxy", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 153 | return err |
| 154 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 155 | |
| 156 | // Register the callbacks |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 157 | aMgr.adapterProxy.RegisterCallback(model.PostUpdate, aMgr.adapterUpdated) |
| 158 | aMgr.deviceTypeProxy.RegisterCallback(model.PostUpdate, aMgr.deviceTypesUpdated) |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 159 | probe.UpdateStatusFromContext(ctx, "adapter-manager", probe.ServiceStatusRunning) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 160 | logger.Info("adapter-manager-started") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 161 | return nil |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 162 | } |
| 163 | |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 164 | //loadAdaptersAndDevicetypesInMemory loads the existing set of adapters and device types in memory |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 165 | func (aMgr *AdapterManager) loadAdaptersAndDevicetypesInMemory() error { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 166 | // Load the adapters |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 167 | adaptersIf, err := aMgr.clusterDataProxy.List(context.Background(), "/adapters", 0, false, "") |
| 168 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 169 | logger.Errorw("Failed-to-list-adapters-from-cluster-data-proxy", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 170 | return err |
| 171 | } |
| 172 | if adaptersIf != nil { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 173 | for _, adapterIf := range adaptersIf.([]interface{}) { |
| 174 | if adapter, ok := adapterIf.(*voltha.Adapter); ok { |
khenaidoo | e7be133 | 2020-01-24 18:58:33 -0500 | [diff] [blame] | 175 | if err := aMgr.addAdapter(adapter, false); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 176 | logger.Errorw("failed to add adapter", log.Fields{"adapterId": adapter.Id}) |
khenaidoo | e7be133 | 2020-01-24 18:58:33 -0500 | [diff] [blame] | 177 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 178 | logger.Debugw("adapter added successfully", log.Fields{"adapterId": adapter.Id}) |
khenaidoo | e7be133 | 2020-01-24 18:58:33 -0500 | [diff] [blame] | 179 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 183 | logger.Debug("no-existing-adapter-found") |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 184 | // No adapter data. In order to have a proxy setup for that path let's create a fake adapter |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 185 | return aMgr.addAdapter(&voltha.Adapter{Id: SentinelAdapterID}, true) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // Load the device types |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 189 | deviceTypesIf, err := aMgr.clusterDataProxy.List(context.Background(), "/device_types", 0, false, "") |
| 190 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 191 | logger.Errorw("Failed-to-list-device-types-from-cluster-data-proxy", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 192 | return err |
| 193 | } |
| 194 | if deviceTypesIf != nil { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 195 | dTypes := &voltha.DeviceTypes{Items: []*voltha.DeviceType{}} |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 196 | for _, deviceTypeIf := range deviceTypesIf.([]interface{}) { |
| 197 | if dType, ok := deviceTypeIf.(*voltha.DeviceType); ok { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 198 | logger.Debugw("found-existing-device-types", log.Fields{"deviceTypes": dTypes}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 199 | dTypes.Items = append(dTypes.Items, dType) |
khenaidoo | e7be133 | 2020-01-24 18:58:33 -0500 | [diff] [blame] | 200 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 201 | logger.Errorw("not an voltha device type", log.Fields{"interface": deviceTypeIf}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 202 | } |
| 203 | } |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 204 | return aMgr.addDeviceTypes(dTypes, false) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 205 | } |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 206 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 207 | logger.Debug("no-existing-device-type-found") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 208 | // No device types data. In order to have a proxy setup for that path let's create a fake device type |
| 209 | return aMgr.addDeviceTypes(&voltha.DeviceTypes{Items: []*voltha.DeviceType{{Id: SentinelDevicetypeID, Adapter: SentinelAdapterID}}}, true) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 210 | } |
| 211 | |
Kent Hagerman | 16ce36a | 2019-12-17 13:40:53 -0500 | [diff] [blame] | 212 | func (aMgr *AdapterManager) updateLastAdapterCommunication(adapterID string, timestamp int64) { |
| 213 | aMgr.lockAdaptersMap.RLock() |
| 214 | adapterAgent, have := aMgr.adapterAgents[adapterID] |
| 215 | aMgr.lockAdaptersMap.RUnlock() |
| 216 | |
| 217 | if have { |
| 218 | adapterAgent.updateCommunicationTime(time.Unix(timestamp/1000, timestamp%1000*1000)) |
| 219 | } |
| 220 | } |
| 221 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 222 | func (aMgr *AdapterManager) addAdapter(adapter *voltha.Adapter, saveToDb bool) error { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 223 | aMgr.lockAdaptersMap.Lock() |
| 224 | defer aMgr.lockAdaptersMap.Unlock() |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 225 | logger.Debugw("adding-adapter", log.Fields{"adapter": adapter}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 226 | if _, exist := aMgr.adapterAgents[adapter.Id]; !exist { |
| 227 | clonedAdapter := (proto.Clone(adapter)).(*voltha.Adapter) |
| 228 | aMgr.adapterAgents[adapter.Id] = newAdapterAgent(clonedAdapter, nil) |
| 229 | if saveToDb { |
| 230 | // Save the adapter to the KV store - first check if it already exist |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 231 | kvAdapter, err := aMgr.clusterDataProxy.Get(context.Background(), "/adapters/"+adapter.Id, 0, false, "") |
| 232 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 233 | logger.Errorw("failed-to-get-adapters-from-cluster-proxy", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 234 | return err |
| 235 | } |
| 236 | if kvAdapter == nil { |
| 237 | added, err := aMgr.clusterDataProxy.AddWithID(context.Background(), "/adapters", adapter.Id, clonedAdapter, "") |
| 238 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 239 | logger.Errorw("failed-to-save-adapter-to-cluster-proxy", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 240 | return err |
| 241 | } |
| 242 | if added == nil { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 243 | //TODO: Errors when saving to KV would require a separate go routine to be launched and try the saving again |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 244 | logger.Errorw("failed-to-save-adapter", log.Fields{"adapter": adapter}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 245 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 246 | logger.Debugw("adapter-saved-to-KV-Store", log.Fields{"adapter": adapter}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | } |
| 250 | } |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 251 | return nil |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 252 | } |
| 253 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 254 | func (aMgr *AdapterManager) addDeviceTypes(deviceTypes *voltha.DeviceTypes, saveToDb bool) error { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 255 | if deviceTypes == nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 256 | return fmt.Errorf("no-device-type") |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 257 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 258 | logger.Debugw("adding-device-types", log.Fields{"deviceTypes": deviceTypes}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 259 | aMgr.lockAdaptersMap.Lock() |
| 260 | defer aMgr.lockAdaptersMap.Unlock() |
| 261 | aMgr.lockdDeviceTypeToAdapterMap.Lock() |
| 262 | defer aMgr.lockdDeviceTypeToAdapterMap.Unlock() |
| 263 | for _, deviceType := range deviceTypes.Items { |
| 264 | clonedDType := (proto.Clone(deviceType)).(*voltha.DeviceType) |
| 265 | if adapterAgent, exist := aMgr.adapterAgents[clonedDType.Adapter]; exist { |
| 266 | adapterAgent.updateDeviceType(clonedDType) |
| 267 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 268 | logger.Debugw("adapter-not-exist", log.Fields{"deviceTypes": deviceTypes, "adapterId": clonedDType.Adapter}) |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 269 | aMgr.adapterAgents[clonedDType.Adapter] = newAdapterAgent(&voltha.Adapter{Id: clonedDType.Adapter}, deviceTypes) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 270 | } |
| 271 | aMgr.deviceTypeToAdapterMap[clonedDType.Id] = clonedDType.Adapter |
| 272 | } |
| 273 | if saveToDb { |
| 274 | // Save the device types to the KV store as well |
| 275 | for _, deviceType := range deviceTypes.Items { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 276 | dType, err := aMgr.clusterDataProxy.Get(context.Background(), "/device_types/"+deviceType.Id, 0, false, "") |
| 277 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 278 | logger.Errorw("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] | 279 | return err |
| 280 | } |
| 281 | if dType == nil { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 282 | // Does not exist - save it |
| 283 | clonedDType := (proto.Clone(deviceType)).(*voltha.DeviceType) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 284 | added, err := aMgr.clusterDataProxy.AddWithID(context.Background(), "/device_types", deviceType.Id, clonedDType, "") |
| 285 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 286 | logger.Errorw("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] | 287 | return err |
| 288 | } |
| 289 | if added == nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 290 | logger.Errorw("failed-to-save-deviceType", log.Fields{"deviceType": deviceType}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 291 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 292 | logger.Debugw("device-type-saved-to-KV-Store", log.Fields{"deviceType": deviceType}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | } |
| 296 | } |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 297 | return nil |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | func (aMgr *AdapterManager) listAdapters(ctx context.Context) (*voltha.Adapters, error) { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 301 | result := &voltha.Adapters{Items: []*voltha.Adapter{}} |
| 302 | aMgr.lockAdaptersMap.RLock() |
| 303 | defer aMgr.lockAdaptersMap.RUnlock() |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 304 | for _, adapterAgent := range aMgr.adapterAgents { |
| 305 | if a := adapterAgent.getAdapter(); a != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 306 | if a.Id != SentinelAdapterID { // don't report the sentinel |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 307 | result.Items = append(result.Items, (proto.Clone(a)).(*voltha.Adapter)) |
| 308 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | return result, nil |
| 312 | } |
| 313 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 314 | func (aMgr *AdapterManager) getAdapter(adapterID string) *voltha.Adapter { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 315 | aMgr.lockAdaptersMap.RLock() |
| 316 | defer aMgr.lockAdaptersMap.RUnlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 317 | if adapterAgent, ok := aMgr.adapterAgents[adapterID]; ok { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 318 | return adapterAgent.getAdapter() |
| 319 | } |
| 320 | return nil |
| 321 | } |
| 322 | |
| 323 | //updateAdapter updates an adapter if it exist. Otherwise, it creates it. |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 324 | func (aMgr *AdapterManager) updateAdapter(adapter *voltha.Adapter) { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 325 | aMgr.lockAdaptersMap.Lock() |
| 326 | defer aMgr.lockAdaptersMap.Unlock() |
A R Karthick | ed2a77b | 2019-10-08 01:40:51 +0000 | [diff] [blame] | 327 | aMgr.updateAdapterWithoutLock(adapter) |
| 328 | } |
| 329 | |
| 330 | func (aMgr *AdapterManager) updateAdapterWithoutLock(adapter *voltha.Adapter) { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 331 | if adapterAgent, ok := aMgr.adapterAgents[adapter.Id]; ok { |
| 332 | adapterAgent.updateAdapter(adapter) |
| 333 | } else { |
| 334 | aMgr.adapterAgents[adapter.Id] = newAdapterAgent(adapter, nil) |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | //updateDeviceType updates an adapter if it exist. Otherwise, it creates it. |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 339 | func (aMgr *AdapterManager) updateDeviceType(deviceType *voltha.DeviceType) { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 340 | aMgr.lockAdaptersMap.Lock() |
| 341 | defer aMgr.lockAdaptersMap.Unlock() |
| 342 | aMgr.lockdDeviceTypeToAdapterMap.Lock() |
| 343 | defer aMgr.lockdDeviceTypeToAdapterMap.Unlock() |
A R Karthick | ed2a77b | 2019-10-08 01:40:51 +0000 | [diff] [blame] | 344 | aMgr.updateDeviceTypeWithoutLock(deviceType) |
| 345 | } |
| 346 | |
| 347 | func (aMgr *AdapterManager) updateDeviceTypeWithoutLock(deviceType *voltha.DeviceType) { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 348 | if adapterAgent, exist := aMgr.adapterAgents[deviceType.Adapter]; exist { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 349 | adapterAgent.updateDeviceType(deviceType) |
| 350 | } else { |
| 351 | aMgr.adapterAgents[deviceType.Adapter] = newAdapterAgent(&voltha.Adapter{Id: deviceType.Adapter}, |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 352 | &voltha.DeviceTypes{Items: []*voltha.DeviceType{deviceType}}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 353 | } |
| 354 | aMgr.deviceTypeToAdapterMap[deviceType.Id] = deviceType.Adapter |
| 355 | } |
| 356 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 357 | func (aMgr *AdapterManager) registerAdapter(adapter *voltha.Adapter, deviceTypes *voltha.DeviceTypes) (*voltha.CoreInstance, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 358 | logger.Debugw("registerAdapter", log.Fields{"adapter": adapter, "deviceTypes": deviceTypes.Items}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 359 | |
| 360 | if aMgr.getAdapter(adapter.Id) != nil { |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 361 | // Already registered - Adapter may have restarted. Trigger the reconcile process for that adapter |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 362 | go func() { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 363 | err := aMgr.deviceMgr.adapterRestarted(context.Background(), adapter) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 364 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 365 | logger.Errorw("unable-to-restart-adapter", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 366 | } |
| 367 | }() |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 368 | return &voltha.CoreInstance{InstanceId: aMgr.coreInstanceID}, nil |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 369 | } |
| 370 | // Save the adapter and the device types |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 371 | if err := aMgr.addAdapter(adapter, true); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 372 | logger.Errorw("failed-to-add-adapter", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 373 | return nil, err |
| 374 | } |
| 375 | if err := aMgr.addDeviceTypes(deviceTypes, true); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 376 | logger.Errorw("failed-to-add-device-types", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 377 | return nil, err |
| 378 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 379 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 380 | logger.Debugw("adapter-registered", log.Fields{"adapter": adapter.Id}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 381 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 382 | return &voltha.CoreInstance{InstanceId: aMgr.coreInstanceID}, nil |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | //getAdapterName returns the name of the device adapter that service this device type |
| 386 | func (aMgr *AdapterManager) getAdapterName(deviceType string) (string, error) { |
| 387 | aMgr.lockdDeviceTypeToAdapterMap.Lock() |
| 388 | defer aMgr.lockdDeviceTypeToAdapterMap.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 389 | if adapterID, exist := aMgr.deviceTypeToAdapterMap[deviceType]; exist { |
| 390 | return adapterID, nil |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 391 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 392 | return "", fmt.Errorf("Adapter-not-registered-for-device-type %s", deviceType) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 393 | } |
| 394 | |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 395 | func (aMgr *AdapterManager) listDeviceTypes() []*voltha.DeviceType { |
| 396 | aMgr.lockdDeviceTypeToAdapterMap.Lock() |
| 397 | defer aMgr.lockdDeviceTypeToAdapterMap.Unlock() |
| 398 | |
| 399 | deviceTypes := make([]*voltha.DeviceType, 0, len(aMgr.deviceTypeToAdapterMap)) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 400 | for deviceTypeID, adapterID := range aMgr.deviceTypeToAdapterMap { |
| 401 | if adapterAgent, have := aMgr.adapterAgents[adapterID]; have { |
| 402 | if deviceType := adapterAgent.getDeviceType(deviceTypeID); deviceType != nil { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 403 | if deviceType.Id != SentinelDevicetypeID { // don't report the sentinel |
| 404 | deviceTypes = append(deviceTypes, deviceType) |
| 405 | } |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | } |
| 409 | return deviceTypes |
| 410 | } |
| 411 | |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 412 | // getDeviceType returns the device type proto definition given the name of the device type |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 413 | func (aMgr *AdapterManager) getDeviceType(deviceType string) *voltha.DeviceType { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 414 | aMgr.lockdDeviceTypeToAdapterMap.Lock() |
| 415 | defer aMgr.lockdDeviceTypeToAdapterMap.Unlock() |
Kent Hagerman | c2c73ff | 2019-11-20 16:22:32 -0500 | [diff] [blame] | 416 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 417 | if adapterID, exist := aMgr.deviceTypeToAdapterMap[deviceType]; exist { |
| 418 | if adapterAgent := aMgr.adapterAgents[adapterID]; adapterAgent != nil { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 419 | return adapterAgent.getDeviceType(deviceType) |
| 420 | } |
| 421 | } |
| 422 | return nil |
| 423 | } |
| 424 | |
| 425 | //adapterUpdated is a callback invoked when an adapter change has been noticed |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 426 | func (aMgr *AdapterManager) adapterUpdated(ctx context.Context, args ...interface{}) interface{} { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 427 | logger.Debugw("updateAdapter-callback", log.Fields{"argsLen": len(args)}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 428 | |
| 429 | var previousData *voltha.Adapters |
| 430 | var latestData *voltha.Adapters |
| 431 | |
| 432 | var ok bool |
| 433 | if previousData, ok = args[0].(*voltha.Adapters); !ok { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 434 | logger.Errorw("invalid-args", log.Fields{"args0": args[0]}) |
khenaidoo | 433f54a | 2019-02-05 14:02:57 -0500 | [diff] [blame] | 435 | return nil |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 436 | } |
| 437 | if latestData, ok = args[1].(*voltha.Adapters); !ok { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 438 | logger.Errorw("invalid-args", log.Fields{"args1": args[1]}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 439 | return nil |
| 440 | } |
| 441 | |
khenaidoo | 433f54a | 2019-02-05 14:02:57 -0500 | [diff] [blame] | 442 | if previousData != nil && latestData != nil { |
| 443 | if reflect.DeepEqual(previousData.Items, latestData.Items) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 444 | logger.Debug("update-not-required") |
khenaidoo | 433f54a | 2019-02-05 14:02:57 -0500 | [diff] [blame] | 445 | return nil |
| 446 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 447 | } |
khenaidoo | 433f54a | 2019-02-05 14:02:57 -0500 | [diff] [blame] | 448 | |
| 449 | if latestData != nil { |
| 450 | for _, adapter := range latestData.Items { |
| 451 | aMgr.updateAdapter(adapter) |
| 452 | } |
| 453 | } |
| 454 | |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 455 | return nil |
| 456 | } |
| 457 | |
| 458 | //deviceTypesUpdated is a callback invoked when a device type change has been noticed |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 459 | func (aMgr *AdapterManager) deviceTypesUpdated(ctx context.Context, args ...interface{}) interface{} { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 460 | logger.Debugw("deviceTypesUpdated-callback", log.Fields{"argsLen": len(args)}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 461 | |
| 462 | var previousData *voltha.DeviceTypes |
| 463 | var latestData *voltha.DeviceTypes |
| 464 | |
| 465 | var ok bool |
| 466 | if previousData, ok = args[0].(*voltha.DeviceTypes); !ok { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 467 | logger.Errorw("invalid-args", log.Fields{"args0": args[0]}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 468 | return nil |
| 469 | } |
| 470 | |
khenaidoo | 433f54a | 2019-02-05 14:02:57 -0500 | [diff] [blame] | 471 | if latestData, ok = args[1].(*voltha.DeviceTypes); !ok { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 472 | logger.Errorw("invalid-args", log.Fields{"args1": args[1]}) |
khenaidoo | 433f54a | 2019-02-05 14:02:57 -0500 | [diff] [blame] | 473 | return nil |
| 474 | } |
| 475 | |
| 476 | if previousData != nil && latestData != nil { |
| 477 | if reflect.DeepEqual(previousData.Items, latestData.Items) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 478 | logger.Debug("update-not-required") |
khenaidoo | 433f54a | 2019-02-05 14:02:57 -0500 | [diff] [blame] | 479 | return nil |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | if latestData != nil { |
| 484 | for _, dType := range latestData.Items { |
| 485 | aMgr.updateDeviceType(dType) |
| 486 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 487 | } |
| 488 | return nil |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 489 | } |