khenaidoo | 2bc4828 | 2019-07-16 18:13:46 -0400 | [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 | */ |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 16 | package device |
khenaidoo | 2bc4828 | 2019-07-16 18:13:46 -0400 | [diff] [blame] | 17 | |
| 18 | import ( |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 19 | "context" |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 20 | "math/rand" |
Neha Sharma | d1387da | 2020-05-07 20:07:28 +0000 | [diff] [blame] | 21 | "strconv" |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 22 | "sync" |
| 23 | "testing" |
| 24 | "time" |
| 25 | |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 26 | "github.com/gogo/protobuf/proto" |
Mahir Gunyel | addb66a | 2020-04-29 18:08:50 -0700 | [diff] [blame] | 27 | "github.com/opencord/voltha-go/db/model" |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 28 | "github.com/opencord/voltha-go/rw_core/config" |
Mahir Gunyel | addb66a | 2020-04-29 18:08:50 -0700 | [diff] [blame] | 29 | "github.com/opencord/voltha-go/rw_core/core/adapter" |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 30 | tst "github.com/opencord/voltha-go/rw_core/test" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame] | 31 | com "github.com/opencord/voltha-lib-go/v4/pkg/adapters/common" |
| 32 | "github.com/opencord/voltha-lib-go/v4/pkg/db" |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 33 | "github.com/opencord/voltha-lib-go/v4/pkg/events" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame] | 34 | fu "github.com/opencord/voltha-lib-go/v4/pkg/flows" |
| 35 | "github.com/opencord/voltha-lib-go/v4/pkg/kafka" |
| 36 | mock_etcd "github.com/opencord/voltha-lib-go/v4/pkg/mocks/etcd" |
| 37 | mock_kafka "github.com/opencord/voltha-lib-go/v4/pkg/mocks/kafka" |
| 38 | ofp "github.com/opencord/voltha-protos/v4/go/openflow_13" |
| 39 | "github.com/opencord/voltha-protos/v4/go/voltha" |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 40 | "github.com/phayes/freeport" |
khenaidoo | 2bc4828 | 2019-07-16 18:13:46 -0400 | [diff] [blame] | 41 | "github.com/stretchr/testify/assert" |
khenaidoo | 2bc4828 | 2019-07-16 18:13:46 -0400 | [diff] [blame] | 42 | ) |
| 43 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 44 | type LDATest struct { |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 45 | etcdServer *mock_etcd.EtcdServer |
| 46 | deviceMgr *Manager |
| 47 | kmp kafka.InterContainerProxy |
| 48 | logicalDeviceMgr *LogicalManager |
| 49 | kClient kafka.Client |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 50 | kEventClient kafka.Client |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 51 | kvClientPort int |
| 52 | oltAdapterName string |
| 53 | onuAdapterName string |
| 54 | coreInstanceID string |
| 55 | defaultTimeout time.Duration |
| 56 | maxTimeout time.Duration |
| 57 | logicalDevice *voltha.LogicalDevice |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 58 | logicalPorts map[uint32]*voltha.LogicalPort |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 59 | deviceIds []string |
| 60 | done chan int |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 61 | } |
| 62 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 63 | func newLDATest(ctx context.Context) *LDATest { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 64 | test := &LDATest{} |
| 65 | // Start the embedded etcd server |
| 66 | var err error |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 67 | test.etcdServer, test.kvClientPort, err = tst.StartEmbeddedEtcdServer(ctx, "voltha.rwcore.lda.test", "voltha.rwcore.lda.etcd", "error") |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 68 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 69 | logger.Fatal(ctx, err) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 70 | } |
| 71 | // Create the kafka client |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 72 | test.kClient = mock_kafka.NewKafkaClient() |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 73 | test.kEventClient = mock_kafka.NewKafkaClient() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 74 | test.oltAdapterName = "olt_adapter_mock" |
| 75 | test.onuAdapterName = "onu_adapter_mock" |
| 76 | test.coreInstanceID = "rw-da-test" |
| 77 | test.defaultTimeout = 5 * time.Second |
| 78 | test.maxTimeout = 20 * time.Second |
| 79 | test.done = make(chan int) |
| 80 | test.deviceIds = []string{com.GetRandomString(10), com.GetRandomString(10), com.GetRandomString(10)} |
| 81 | test.logicalDevice = &voltha.LogicalDevice{ |
| 82 | Desc: &ofp.OfpDesc{ |
| 83 | HwDesc: "olt_adapter_mock", |
| 84 | SwDesc: "olt_adapter_mock", |
| 85 | SerialNum: com.GetRandomSerialNumber(), |
| 86 | }, |
| 87 | SwitchFeatures: &ofp.OfpSwitchFeatures{ |
| 88 | NBuffers: 256, |
| 89 | NTables: 2, |
| 90 | Capabilities: uint32(ofp.OfpCapabilities_OFPC_FLOW_STATS | |
| 91 | ofp.OfpCapabilities_OFPC_TABLE_STATS | |
| 92 | ofp.OfpCapabilities_OFPC_PORT_STATS | |
| 93 | ofp.OfpCapabilities_OFPC_GROUP_STATS), |
| 94 | }, |
| 95 | RootDeviceId: test.deviceIds[0], |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 96 | } |
| 97 | test.logicalPorts = map[uint32]*voltha.LogicalPort{ |
| 98 | 1: { |
| 99 | Id: "1001", |
| 100 | DeviceId: test.deviceIds[0], |
| 101 | DevicePortNo: 1, |
| 102 | RootPort: true, |
| 103 | OfpPort: &ofp.OfpPort{ |
| 104 | PortNo: 1, |
| 105 | Name: "port1", |
| 106 | Config: 4, |
| 107 | State: 4, |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 108 | }, |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 109 | }, |
| 110 | 2: { |
| 111 | Id: "1002", |
| 112 | DeviceId: test.deviceIds[1], |
| 113 | DevicePortNo: 2, |
| 114 | RootPort: false, |
| 115 | OfpPort: &ofp.OfpPort{ |
| 116 | PortNo: 2, |
| 117 | Name: "port2", |
| 118 | Config: 4, |
| 119 | State: 4, |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 120 | }, |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 121 | }, |
| 122 | 3: { |
| 123 | Id: "1003", |
| 124 | DeviceId: test.deviceIds[2], |
| 125 | DevicePortNo: 3, |
| 126 | RootPort: false, |
| 127 | OfpPort: &ofp.OfpPort{ |
| 128 | PortNo: 3, |
| 129 | Name: "port3", |
| 130 | Config: 4, |
| 131 | State: 4, |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 132 | }, |
| 133 | }, |
| 134 | } |
| 135 | return test |
| 136 | } |
| 137 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 138 | func (lda *LDATest) startCore(ctx context.Context, inCompeteMode bool) { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 139 | cfg := config.NewRWCoreFlags() |
serkant.uluderya | 8ff291d | 2020-05-20 00:58:00 -0700 | [diff] [blame] | 140 | cfg.CoreTopic = "rw_core" |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 141 | cfg.EventTopic = "voltha.events" |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 142 | cfg.DefaultRequestTimeout = lda.defaultTimeout |
Neha Sharma | d1387da | 2020-05-07 20:07:28 +0000 | [diff] [blame] | 143 | cfg.KVStoreAddress = "127.0.0.1" + ":" + strconv.Itoa(lda.kvClientPort) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 144 | grpcPort, err := freeport.GetFreePort() |
| 145 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 146 | logger.Fatal(ctx, "Cannot get a freeport for grpc") |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 147 | } |
Neha Sharma | d1387da | 2020-05-07 20:07:28 +0000 | [diff] [blame] | 148 | cfg.GrpcAddress = "127.0.0.1" + ":" + strconv.Itoa(grpcPort) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 149 | client := tst.SetupKVClient(ctx, cfg, lda.coreInstanceID) |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 150 | backend := &db.Backend{ |
| 151 | Client: client, |
| 152 | StoreType: cfg.KVStoreType, |
Neha Sharma | d1387da | 2020-05-07 20:07:28 +0000 | [diff] [blame] | 153 | Address: cfg.KVStoreAddress, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 154 | Timeout: cfg.KVStoreTimeout, |
serkant.uluderya | 8ff291d | 2020-05-20 00:58:00 -0700 | [diff] [blame] | 155 | LivenessChannelInterval: cfg.LiveProbeInterval / 2} |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 156 | lda.kmp = kafka.NewInterContainerProxy( |
Neha Sharma | d1387da | 2020-05-07 20:07:28 +0000 | [diff] [blame] | 157 | kafka.InterContainerAddress(cfg.KafkaAdapterAddress), |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 158 | kafka.MsgClient(lda.kClient), |
David Bainbridge | 9ae1313 | 2020-06-22 17:28:01 -0700 | [diff] [blame] | 159 | kafka.DefaultTopic(&kafka.Topic{Name: cfg.CoreTopic})) |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 160 | |
| 161 | endpointMgr := kafka.NewEndpointManager(backend) |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 162 | proxy := model.NewDBPath(backend) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 163 | adapterMgr := adapter.NewAdapterManager(ctx, proxy, lda.coreInstanceID, lda.kClient) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 164 | eventProxy := events.NewEventProxy(events.MsgClient(lda.kEventClient), events.MsgTopic(kafka.Topic{Name: cfg.EventTopic})) |
Maninder | 0aabf0c | 2021-03-17 14:55:14 +0530 | [diff] [blame] | 165 | lda.deviceMgr, lda.logicalDeviceMgr = NewManagers(proxy, adapterMgr, lda.kmp, endpointMgr, cfg, lda.coreInstanceID, eventProxy) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 166 | if err = lda.kmp.Start(ctx); err != nil { |
| 167 | logger.Fatal(ctx, "Cannot start InterContainerProxy") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 168 | } |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 169 | adapterMgr.Start(context.Background()) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 170 | } |
| 171 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 172 | func (lda *LDATest) stopAll(ctx context.Context) { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 173 | if lda.kClient != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 174 | lda.kClient.Stop(ctx) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 175 | } |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 176 | if lda.kmp != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 177 | lda.kmp.Stop(ctx) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 178 | } |
| 179 | if lda.etcdServer != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 180 | tst.StopEmbeddedEtcdServer(ctx, lda.etcdServer) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 181 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 182 | if lda.kEventClient != nil { |
| 183 | lda.kEventClient.Stop(ctx) |
| 184 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 185 | } |
| 186 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 187 | func (lda *LDATest) createLogicalDeviceAgent(t *testing.T) *LogicalAgent { |
| 188 | lDeviceMgr := lda.logicalDeviceMgr |
| 189 | deviceMgr := lda.deviceMgr |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 190 | clonedLD := proto.Clone(lda.logicalDevice).(*voltha.LogicalDevice) |
| 191 | clonedLD.Id = com.GetRandomString(10) |
| 192 | clonedLD.DatapathId = rand.Uint64() |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 193 | lDeviceAgent := newLogicalAgent(context.Background(), clonedLD.Id, clonedLD.Id, clonedLD.RootDeviceId, lDeviceMgr, deviceMgr, lDeviceMgr.dbPath, lDeviceMgr.ldProxy, lDeviceMgr.defaultTimeout) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 194 | lDeviceAgent.logicalDevice = clonedLD |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 195 | for _, port := range lda.logicalPorts { |
| 196 | clonedPort := proto.Clone(port).(*voltha.LogicalPort) |
| 197 | handle, created, err := lDeviceAgent.portLoader.LockOrCreate(context.Background(), clonedPort) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 198 | if err != nil { |
| 199 | panic(err) |
| 200 | } |
| 201 | handle.Unlock() |
| 202 | if !created { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 203 | t.Errorf("port %d already exists", clonedPort.OfpPort.PortNo) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 204 | } |
| 205 | } |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 206 | err := lDeviceAgent.ldProxy.Set(context.Background(), clonedLD.Id, clonedLD) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 207 | assert.Nil(t, err) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 208 | lDeviceMgr.addLogicalDeviceAgentToMap(lDeviceAgent) |
| 209 | return lDeviceAgent |
| 210 | } |
| 211 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 212 | func (lda *LDATest) updateLogicalDeviceConcurrently(t *testing.T, ldAgent *LogicalAgent, globalWG *sync.WaitGroup) { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 213 | originalLogicalPorts := ldAgent.listLogicalDevicePorts(context.Background()) |
| 214 | assert.NotNil(t, originalLogicalPorts) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 215 | var localWG sync.WaitGroup |
| 216 | |
| 217 | // Change the state of the first port to FAILED |
| 218 | localWG.Add(1) |
| 219 | go func() { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 220 | err := ldAgent.updatePortState(context.Background(), 1, voltha.OperStatus_FAILED) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 221 | assert.Nil(t, err) |
| 222 | localWG.Done() |
| 223 | }() |
| 224 | |
| 225 | // Change the state of the second port to TESTING |
| 226 | localWG.Add(1) |
| 227 | go func() { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 228 | err := ldAgent.updatePortState(context.Background(), 2, voltha.OperStatus_TESTING) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 229 | assert.Nil(t, err) |
| 230 | localWG.Done() |
| 231 | }() |
| 232 | |
| 233 | // Change the state of the third port to UNKNOWN and then back to ACTIVE |
| 234 | localWG.Add(1) |
| 235 | go func() { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 236 | err := ldAgent.updatePortState(context.Background(), 3, voltha.OperStatus_UNKNOWN) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 237 | assert.Nil(t, err) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 238 | err = ldAgent.updatePortState(context.Background(), 3, voltha.OperStatus_ACTIVE) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 239 | assert.Nil(t, err) |
| 240 | localWG.Done() |
| 241 | }() |
| 242 | |
| 243 | // Add a meter to the logical device |
| 244 | meterMod := &ofp.OfpMeterMod{ |
| 245 | Command: ofp.OfpMeterModCommand_OFPMC_ADD, |
| 246 | Flags: rand.Uint32(), |
| 247 | MeterId: rand.Uint32(), |
| 248 | Bands: []*ofp.OfpMeterBandHeader{ |
| 249 | {Type: ofp.OfpMeterBandType_OFPMBT_EXPERIMENTER, |
| 250 | Rate: rand.Uint32(), |
| 251 | BurstSize: rand.Uint32(), |
| 252 | Data: nil, |
| 253 | }, |
| 254 | }, |
| 255 | } |
| 256 | localWG.Add(1) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 257 | ctx := context.Background() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 258 | go func() { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 259 | err := ldAgent.meterAdd(ctx, meterMod) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 260 | assert.Nil(t, err) |
| 261 | localWG.Done() |
| 262 | }() |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 263 | // wait for go routines to be done |
| 264 | localWG.Wait() |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 265 | meterEntry := fu.MeterEntryFromMeterMod(ctx, meterMod) |
Mahir Gunyel | addb66a | 2020-04-29 18:08:50 -0700 | [diff] [blame] | 266 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 267 | meterHandle, have := ldAgent.meterLoader.Lock(meterMod.MeterId) |
| 268 | assert.Equal(t, have, true) |
| 269 | if have { |
| 270 | assert.True(t, proto.Equal(meterEntry, meterHandle.GetReadOnly())) |
| 271 | meterHandle.Unlock() |
| 272 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 273 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 274 | expectedLogicalPorts := make(map[uint32]*voltha.LogicalPort) |
| 275 | for _, port := range originalLogicalPorts { |
| 276 | clonedPort := proto.Clone(port).(*voltha.LogicalPort) |
| 277 | switch clonedPort.OfpPort.PortNo { |
| 278 | case 1: |
| 279 | clonedPort.OfpPort.Config = originalLogicalPorts[1].OfpPort.Config | uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
| 280 | clonedPort.OfpPort.State = uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) |
| 281 | case 2: |
| 282 | clonedPort.OfpPort.Config = originalLogicalPorts[1].OfpPort.Config | uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
| 283 | clonedPort.OfpPort.State = uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) |
| 284 | case 3: |
| 285 | clonedPort.OfpPort.Config = originalLogicalPorts[1].OfpPort.Config & ^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) |
| 286 | clonedPort.OfpPort.State = uint32(ofp.OfpPortState_OFPPS_LIVE) |
| 287 | } |
| 288 | expectedLogicalPorts[clonedPort.OfpPort.PortNo] = clonedPort |
| 289 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 290 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 291 | updatedLogicalDevicePorts := ldAgent.listLogicalDevicePorts(ctx) |
| 292 | assert.Equal(t, len(expectedLogicalPorts), len(updatedLogicalDevicePorts)) |
| 293 | for _, p := range updatedLogicalDevicePorts { |
| 294 | assert.True(t, proto.Equal(p, expectedLogicalPorts[p.OfpPort.PortNo])) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 295 | } |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 296 | globalWG.Done() |
| 297 | } |
| 298 | |
Himani Chawla | 40af270 | 2021-01-27 15:06:30 +0530 | [diff] [blame] | 299 | func (lda *LDATest) stopLogicalAgentAndCheckEventQueueIsEmpty(ctx context.Context, t *testing.T, ldAgent *LogicalAgent) { |
| 300 | queueIsEmpty := false |
| 301 | err := ldAgent.stop(ctx) |
| 302 | assert.Nil(t, err) |
| 303 | qp := ldAgent.orderedEvents.assignQueuePosition() |
| 304 | if qp.prev != nil { // we will be definitely hitting this case as we pushed events on the queue before |
| 305 | // If previous channel is closed which it should be now, |
| 306 | // only then we can know that queue is empty. |
| 307 | _, ok := <-qp.prev |
| 308 | if !ok { |
| 309 | queueIsEmpty = true |
| 310 | } else { |
| 311 | queueIsEmpty = false |
| 312 | } |
| 313 | } else { |
| 314 | queueIsEmpty = true |
| 315 | } |
| 316 | close(qp.next) |
| 317 | assert.True(t, queueIsEmpty) |
| 318 | } |
| 319 | |
| 320 | func (lda *LDATest) updateLogicalDevice(t *testing.T, ldAgent *LogicalAgent) { |
| 321 | originalLogicalPorts := ldAgent.listLogicalDevicePorts(context.Background()) |
| 322 | assert.NotNil(t, originalLogicalPorts) |
| 323 | |
| 324 | // Change the state of the first port to FAILED |
| 325 | err := ldAgent.updatePortState(context.Background(), 1, voltha.OperStatus_FAILED) |
| 326 | assert.Nil(t, err) |
| 327 | |
| 328 | // Change the state of the second port to TESTING |
| 329 | err = ldAgent.updatePortState(context.Background(), 2, voltha.OperStatus_TESTING) |
| 330 | assert.Nil(t, err) |
| 331 | |
| 332 | // Change the state of the third port to ACTIVE |
| 333 | err = ldAgent.updatePortState(context.Background(), 3, voltha.OperStatus_ACTIVE) |
| 334 | assert.Nil(t, err) |
| 335 | |
| 336 | } |
| 337 | |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 338 | func TestConcurrentLogicalDeviceUpdate(t *testing.T) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 339 | ctx := context.Background() |
| 340 | lda := newLDATest(ctx) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 341 | assert.NotNil(t, lda) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 342 | defer lda.stopAll(ctx) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 343 | |
| 344 | // Start the Core |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 345 | lda.startCore(ctx, false) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 346 | |
| 347 | var wg sync.WaitGroup |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 348 | numConCurrentLogicalDeviceAgents := 3 |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 349 | for i := 0; i < numConCurrentLogicalDeviceAgents; i++ { |
| 350 | wg.Add(1) |
| 351 | a := lda.createLogicalDeviceAgent(t) |
| 352 | go lda.updateLogicalDeviceConcurrently(t, a, &wg) |
| 353 | } |
| 354 | |
| 355 | wg.Wait() |
| 356 | } |
Himani Chawla | 40af270 | 2021-01-27 15:06:30 +0530 | [diff] [blame] | 357 | |
| 358 | func TestLogicalAgentStopWithEventsInQueue(t *testing.T) { |
| 359 | ctx := context.Background() |
| 360 | lda := newLDATest(ctx) |
| 361 | assert.NotNil(t, lda) |
| 362 | defer lda.stopAll(ctx) |
| 363 | |
| 364 | // Start the Core |
| 365 | lda.startCore(ctx, false) |
| 366 | |
| 367 | a := lda.createLogicalDeviceAgent(t) |
| 368 | lda.updateLogicalDevice(t, a) |
| 369 | lda.stopLogicalAgentAndCheckEventQueueIsEmpty(ctx, t, a) |
| 370 | } |