khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -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 | */ |
| 16 | package core |
| 17 | |
| 18 | import ( |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 19 | "context" |
| 20 | "fmt" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 21 | "strconv" |
| 22 | "time" |
| 23 | |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 24 | "github.com/golang/protobuf/ptypes/empty" |
| 25 | "github.com/google/uuid" |
| 26 | "github.com/opencord/voltha-go/rw_core/config" |
| 27 | cm "github.com/opencord/voltha-go/rw_core/mocks" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 28 | "github.com/opencord/voltha-lib-go/v3/pkg/adapters" |
| 29 | com "github.com/opencord/voltha-lib-go/v3/pkg/adapters/common" |
| 30 | "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore" |
| 31 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
| 32 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 33 | lm "github.com/opencord/voltha-lib-go/v3/pkg/mocks" |
| 34 | "github.com/opencord/voltha-protos/v3/go/voltha" |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 35 | "github.com/phayes/freeport" |
| 36 | "google.golang.org/grpc/codes" |
| 37 | "google.golang.org/grpc/metadata" |
| 38 | "google.golang.org/grpc/status" |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 39 | ) |
| 40 | |
| 41 | const ( |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 42 | logLevel = log.FatalLevel |
| 43 | volthaSerialNumberKey = "voltha_serial_number" |
| 44 | retryInterval = 50 * time.Millisecond |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 45 | ) |
| 46 | |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 47 | const ( |
| 48 | OltAdapter = iota |
| 49 | OnuAdapter |
| 50 | ) |
| 51 | |
| 52 | var ( |
| 53 | coreInCompeteMode bool |
| 54 | ) |
| 55 | |
| 56 | type isLogicalDeviceConditionSatisfied func(ld *voltha.LogicalDevice) bool |
| 57 | type isDeviceConditionSatisfied func(ld *voltha.Device) bool |
| 58 | type isDevicesConditionSatisfied func(ds *voltha.Devices) bool |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 59 | type isLogicalDevicesConditionSatisfied func(lds *voltha.LogicalDevices) bool |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 60 | type isConditionSatisfied func() bool |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 61 | |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 62 | func init() { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 63 | _, err := log.AddPackage(log.JSON, logLevel, log.Fields{"instanceId": "coreTests"}) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 64 | if err != nil { |
| 65 | panic(err) |
| 66 | } |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 67 | // Update all loggers to log level specified as input parameter |
| 68 | log.SetAllLogLevel(log.ErrorLevel) |
| 69 | |
| 70 | //Default mode is two rw-core running in a pair of competing cores |
| 71 | coreInCompeteMode = true |
| 72 | } |
| 73 | |
| 74 | func setCoreCompeteMode(mode bool) { |
| 75 | coreInCompeteMode = mode |
| 76 | } |
| 77 | |
| 78 | func getContext() context.Context { |
| 79 | if coreInCompeteMode { |
| 80 | return metadata.NewIncomingContext(context.Background(), metadata.Pairs(volthaSerialNumberKey, uuid.New().String())) |
| 81 | } |
| 82 | return context.Background() |
| 83 | } |
| 84 | |
| 85 | //startEmbeddedEtcdServer creates and starts an Embedded etcd server locally. |
| 86 | func startEmbeddedEtcdServer(configName, storageDir, logLevel string) (*lm.EtcdServer, int, error) { |
| 87 | kvClientPort, err := freeport.GetFreePort() |
| 88 | if err != nil { |
| 89 | return nil, 0, err |
| 90 | } |
| 91 | peerPort, err := freeport.GetFreePort() |
| 92 | if err != nil { |
| 93 | return nil, 0, err |
| 94 | } |
| 95 | etcdServer := lm.StartEtcdServer(lm.MKConfig(configName, kvClientPort, peerPort, storageDir, logLevel)) |
| 96 | if etcdServer == nil { |
| 97 | return nil, 0, status.Error(codes.Internal, "Embedded server failed to start") |
| 98 | } |
| 99 | return etcdServer, kvClientPort, nil |
| 100 | } |
| 101 | |
| 102 | func stopEmbeddedEtcdServer(server *lm.EtcdServer) { |
| 103 | if server != nil { |
| 104 | server.Stop() |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func setupKVClient(cf *config.RWCoreFlags, coreInstanceID string) kvstore.Client { |
| 109 | addr := cf.KVStoreHost + ":" + strconv.Itoa(cf.KVStorePort) |
| 110 | client, err := kvstore.NewEtcdClient(addr, cf.KVStoreTimeout) |
| 111 | if err != nil { |
| 112 | panic("no kv client") |
| 113 | } |
| 114 | // Setup KV transaction context |
| 115 | txnPrefix := cf.KVStoreDataPrefix + "/transactions/" |
| 116 | if err = SetTransactionContext(coreInstanceID, |
| 117 | txnPrefix, |
| 118 | client, |
| 119 | cf.KVStoreTimeout); err != nil { |
| 120 | log.Fatal("creating-transaction-context-failed") |
| 121 | } |
| 122 | return client |
| 123 | } |
| 124 | |
| 125 | func createMockAdapter(adapterType int, kafkaClient kafka.Client, coreInstanceID string, coreName string, adapterName string) (adapters.IAdapter, error) { |
| 126 | var err error |
| 127 | var adapter adapters.IAdapter |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 128 | adapterKafkaICProxy := kafka.NewInterContainerProxy( |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 129 | kafka.MsgClient(kafkaClient), |
| 130 | kafka.DefaultTopic(&kafka.Topic{Name: adapterName})) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 131 | adapterCoreProxy := com.NewCoreProxy(adapterKafkaICProxy, adapterName, coreName) |
| 132 | var adapterReqHandler *com.RequestHandlerProxy |
| 133 | switch adapterType { |
| 134 | case OltAdapter: |
| 135 | adapter = cm.NewOLTAdapter(adapterCoreProxy) |
| 136 | case OnuAdapter: |
| 137 | adapter = cm.NewONUAdapter(adapterCoreProxy) |
| 138 | default: |
| 139 | log.Fatalf("invalid-adapter-type-%d", adapterType) |
| 140 | } |
| 141 | adapterReqHandler = com.NewRequestHandlerProxy(coreInstanceID, adapter, adapterCoreProxy) |
| 142 | |
| 143 | if err = adapterKafkaICProxy.Start(); err != nil { |
| 144 | log.Errorw("Failure-starting-adapter-intercontainerProxy", log.Fields{"error": err}) |
| 145 | return nil, err |
| 146 | } |
| 147 | if err = adapterKafkaICProxy.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: adapterName}, adapterReqHandler); err != nil { |
| 148 | log.Errorw("Failure-to-subscribe-onu-request-handler", log.Fields{"error": err}) |
| 149 | return nil, err |
| 150 | } |
| 151 | return adapter, nil |
| 152 | } |
| 153 | |
| 154 | func waitUntilDeviceReadiness(deviceID string, |
| 155 | timeout time.Duration, |
| 156 | verificationFunction isDeviceConditionSatisfied, |
| 157 | nbi *APIHandler) error { |
| 158 | ch := make(chan int, 1) |
| 159 | done := false |
| 160 | go func() { |
| 161 | for { |
| 162 | device, _ := nbi.GetDevice(getContext(), &voltha.ID{Id: deviceID}) |
Chaitrashree G S | e8ad020 | 2020-02-27 18:48:00 -0500 | [diff] [blame] | 163 | if verificationFunction(device) { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 164 | ch <- 1 |
| 165 | break |
| 166 | } |
| 167 | if done { |
| 168 | break |
| 169 | } |
| 170 | time.Sleep(retryInterval) |
| 171 | } |
| 172 | }() |
| 173 | timer := time.NewTimer(timeout) |
| 174 | defer timer.Stop() |
| 175 | select { |
| 176 | case <-ch: |
| 177 | return nil |
| 178 | case <-timer.C: |
| 179 | done = true |
| 180 | return fmt.Errorf("expected-states-not-reached-for-device%s", deviceID) |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | func waitUntilLogicalDeviceReadiness(oltDeviceID string, |
| 185 | timeout time.Duration, |
| 186 | nbi *APIHandler, |
| 187 | verificationFunction isLogicalDeviceConditionSatisfied, |
| 188 | ) error { |
| 189 | ch := make(chan int, 1) |
| 190 | done := false |
| 191 | go func() { |
| 192 | for { |
| 193 | // Get the logical device from the olt device |
| 194 | d, _ := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDeviceID}) |
| 195 | if d != nil && d.ParentId != "" { |
| 196 | ld, _ := nbi.GetLogicalDevice(getContext(), &voltha.ID{Id: d.ParentId}) |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 197 | if verificationFunction(ld) { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 198 | ch <- 1 |
| 199 | break |
| 200 | } |
| 201 | if done { |
| 202 | break |
| 203 | } |
| 204 | } |
| 205 | time.Sleep(retryInterval) |
| 206 | } |
| 207 | }() |
| 208 | timer := time.NewTimer(timeout) |
| 209 | defer timer.Stop() |
| 210 | select { |
| 211 | case <-ch: |
| 212 | return nil |
| 213 | case <-timer.C: |
| 214 | done = true |
| 215 | return fmt.Errorf("timeout-waiting-for-logical-device-readiness%s", oltDeviceID) |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | func waitUntilConditionForDevices(timeout time.Duration, nbi *APIHandler, verificationFunction isDevicesConditionSatisfied) error { |
| 220 | ch := make(chan int, 1) |
| 221 | done := false |
| 222 | go func() { |
| 223 | for { |
| 224 | devices, _ := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 225 | if verificationFunction(devices) { |
| 226 | ch <- 1 |
| 227 | break |
| 228 | } |
| 229 | if done { |
| 230 | break |
| 231 | } |
| 232 | |
| 233 | time.Sleep(retryInterval) |
| 234 | } |
| 235 | }() |
| 236 | timer := time.NewTimer(timeout) |
| 237 | defer timer.Stop() |
| 238 | select { |
| 239 | case <-ch: |
| 240 | return nil |
| 241 | case <-timer.C: |
| 242 | done = true |
| 243 | return fmt.Errorf("timeout-waiting-devices") |
| 244 | } |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 245 | } |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 246 | |
| 247 | func waitUntilConditionForLogicalDevices(timeout time.Duration, nbi *APIHandler, verificationFunction isLogicalDevicesConditionSatisfied) error { |
| 248 | ch := make(chan int, 1) |
| 249 | done := false |
| 250 | go func() { |
| 251 | for { |
| 252 | lDevices, _ := nbi.ListLogicalDevices(getContext(), &empty.Empty{}) |
| 253 | if verificationFunction(lDevices) { |
| 254 | ch <- 1 |
| 255 | break |
| 256 | } |
| 257 | if done { |
| 258 | break |
| 259 | } |
| 260 | |
| 261 | time.Sleep(retryInterval) |
| 262 | } |
| 263 | }() |
| 264 | timer := time.NewTimer(timeout) |
| 265 | defer timer.Stop() |
| 266 | select { |
| 267 | case <-ch: |
| 268 | return nil |
| 269 | case <-timer.C: |
| 270 | done = true |
| 271 | return fmt.Errorf("timeout-waiting-logical-devices") |
| 272 | } |
| 273 | } |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 274 | |
| 275 | func waitUntilCondition(timeout time.Duration, nbi *APIHandler, verificationFunction isConditionSatisfied) error { |
| 276 | ch := make(chan int, 1) |
| 277 | done := false |
| 278 | go func() { |
| 279 | for { |
| 280 | if verificationFunction() { |
| 281 | ch <- 1 |
| 282 | break |
| 283 | } |
| 284 | if done { |
| 285 | break |
| 286 | } |
| 287 | time.Sleep(retryInterval) |
| 288 | } |
| 289 | }() |
| 290 | timer := time.NewTimer(timeout) |
| 291 | defer timer.Stop() |
| 292 | select { |
| 293 | case <-ch: |
| 294 | return nil |
| 295 | case <-timer.C: |
| 296 | done = true |
| 297 | return fmt.Errorf("timeout-waiting-for-condition") |
| 298 | } |
| 299 | } |