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