Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-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 | |
| 17 | // Package core Common Logger initialization |
| 18 | package test |
| 19 | |
| 20 | import ( |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 21 | "context" |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 22 | "testing" |
| 23 | |
| 24 | "github.com/opencord/voltha-go/rw_core/config" |
| 25 | "github.com/opencord/voltha-go/rw_core/core/adapter" |
| 26 | cm "github.com/opencord/voltha-go/rw_core/mocks" |
yasin sapli | 5458a1c | 2021-06-14 22:24:38 +0000 | [diff] [blame] | 27 | "github.com/opencord/voltha-lib-go/v5/pkg/adapters" |
| 28 | com "github.com/opencord/voltha-lib-go/v5/pkg/adapters/common" |
| 29 | "github.com/opencord/voltha-lib-go/v5/pkg/db/kvstore" |
| 30 | "github.com/opencord/voltha-lib-go/v5/pkg/kafka" |
| 31 | "github.com/opencord/voltha-lib-go/v5/pkg/log" |
| 32 | mock_etcd "github.com/opencord/voltha-lib-go/v5/pkg/mocks/etcd" |
| 33 | "github.com/opencord/voltha-lib-go/v5/pkg/version" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame] | 34 | "github.com/opencord/voltha-protos/v4/go/voltha" |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 35 | "github.com/phayes/freeport" |
| 36 | "github.com/stretchr/testify/assert" |
| 37 | "google.golang.org/grpc/codes" |
| 38 | "google.golang.org/grpc/status" |
| 39 | ) |
| 40 | |
| 41 | const ( |
| 42 | OltAdapter = iota |
| 43 | OnuAdapter |
| 44 | ) |
| 45 | |
| 46 | //CreateMockAdapter creates mock OLT and ONU adapters |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 47 | func CreateMockAdapter(ctx context.Context, adapterType int, kafkaClient kafka.Client, coreInstanceID string, coreName string, adapterName string) (adapters.IAdapter, error) { |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 48 | var err error |
| 49 | var adapter adapters.IAdapter |
| 50 | adapterKafkaICProxy := kafka.NewInterContainerProxy( |
| 51 | kafka.MsgClient(kafkaClient), |
| 52 | kafka.DefaultTopic(&kafka.Topic{Name: adapterName})) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 53 | adapterCoreProxy := com.NewCoreProxy(ctx, adapterKafkaICProxy, adapterName, coreName) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 54 | var adapterReqHandler *com.RequestHandlerProxy |
| 55 | switch adapterType { |
| 56 | case OltAdapter: |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 57 | adapter = cm.NewOLTAdapter(ctx, adapterCoreProxy) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 58 | case OnuAdapter: |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 59 | adapter = cm.NewONUAdapter(ctx, adapterCoreProxy) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 60 | default: |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 61 | logger.Fatalf(ctx, "invalid-adapter-type-%d", adapterType) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 62 | } |
| 63 | adapterReqHandler = com.NewRequestHandlerProxy(coreInstanceID, adapter, adapterCoreProxy) |
| 64 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 65 | if err = adapterKafkaICProxy.Start(ctx); err != nil { |
| 66 | logger.Errorw(ctx, "Failure-starting-adapter-intercontainerProxy", log.Fields{"error": err}) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 67 | return nil, err |
| 68 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 69 | if err = adapterKafkaICProxy.SubscribeWithRequestHandlerInterface(ctx, kafka.Topic{Name: adapterName}, adapterReqHandler); err != nil { |
| 70 | logger.Errorw(ctx, "Failure-to-subscribe-onu-request-handler", log.Fields{"error": err}) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 71 | return nil, err |
| 72 | } |
| 73 | return adapter, nil |
| 74 | } |
| 75 | |
| 76 | //CreateAndregisterAdapters creates mock ONU and OLT adapters and egisters them to rw-core |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 77 | func CreateAndregisterAdapters(ctx context.Context, t *testing.T, kClient kafka.Client, coreInstanceID string, oltAdapterName string, onuAdapterName string, adapterMgr *adapter.Manager) (*cm.OLTAdapter, *cm.ONUAdapter) { |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 78 | // Setup the mock OLT adapter |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 79 | oltAdapter, err := CreateMockAdapter(ctx, OltAdapter, kClient, coreInstanceID, "rw_core", oltAdapterName) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 80 | assert.Nil(t, err) |
| 81 | assert.NotNil(t, oltAdapter) |
| 82 | |
| 83 | // Register the adapter |
| 84 | registrationData := &voltha.Adapter{ |
| 85 | Id: oltAdapterName, |
| 86 | Vendor: "Voltha-olt", |
| 87 | Version: version.VersionInfo.Version, |
| 88 | Type: oltAdapterName, |
| 89 | CurrentReplica: 1, |
| 90 | TotalReplicas: 1, |
| 91 | Endpoint: oltAdapterName, |
| 92 | } |
| 93 | types := []*voltha.DeviceType{{Id: oltAdapterName, Adapter: oltAdapterName, AcceptsAddRemoveFlowUpdates: true}} |
| 94 | deviceTypes := &voltha.DeviceTypes{Items: types} |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 95 | if _, err := adapterMgr.RegisterAdapter(ctx, registrationData, deviceTypes); err != nil { |
| 96 | logger.Errorw(ctx, "failed-to-register-adapter", log.Fields{"error": err}) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 97 | assert.NotNil(t, err) |
| 98 | } |
| 99 | |
| 100 | // Setup the mock ONU adapter |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 101 | onuAdapter, err := CreateMockAdapter(ctx, OnuAdapter, kClient, coreInstanceID, "rw_core", onuAdapterName) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 102 | |
| 103 | assert.Nil(t, err) |
| 104 | assert.NotNil(t, onuAdapter) |
| 105 | // Register the adapter |
| 106 | registrationData = &voltha.Adapter{ |
| 107 | Id: onuAdapterName, |
| 108 | Vendor: "Voltha-onu", |
| 109 | Version: version.VersionInfo.Version, |
| 110 | Type: onuAdapterName, |
| 111 | CurrentReplica: 1, |
| 112 | TotalReplicas: 1, |
| 113 | Endpoint: onuAdapterName, |
| 114 | } |
| 115 | types = []*voltha.DeviceType{{Id: onuAdapterName, Adapter: onuAdapterName, AcceptsAddRemoveFlowUpdates: true}} |
| 116 | deviceTypes = &voltha.DeviceTypes{Items: types} |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 117 | if _, err := adapterMgr.RegisterAdapter(ctx, registrationData, deviceTypes); err != nil { |
| 118 | logger.Errorw(ctx, "failed-to-register-adapter", log.Fields{"error": err}) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 119 | assert.NotNil(t, err) |
| 120 | } |
| 121 | return oltAdapter.(*cm.OLTAdapter), onuAdapter.(*cm.ONUAdapter) |
| 122 | } |
| 123 | |
| 124 | //StartEmbeddedEtcdServer creates and starts an Embedded etcd server locally. |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 125 | func StartEmbeddedEtcdServer(ctx context.Context, configName, storageDir, logLevel string) (*mock_etcd.EtcdServer, int, error) { |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 126 | kvClientPort, err := freeport.GetFreePort() |
| 127 | if err != nil { |
| 128 | return nil, 0, err |
| 129 | } |
| 130 | peerPort, err := freeport.GetFreePort() |
| 131 | if err != nil { |
| 132 | return nil, 0, err |
| 133 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 134 | etcdServer := mock_etcd.StartEtcdServer(ctx, mock_etcd.MKConfig(ctx, configName, kvClientPort, peerPort, storageDir, logLevel)) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 135 | if etcdServer == nil { |
| 136 | return nil, 0, status.Error(codes.Internal, "Embedded server failed to start") |
| 137 | } |
| 138 | return etcdServer, kvClientPort, nil |
| 139 | } |
| 140 | |
| 141 | //StopEmbeddedEtcdServer stops the embedded etcd server |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 142 | func StopEmbeddedEtcdServer(ctx context.Context, server *mock_etcd.EtcdServer) { |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 143 | if server != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 144 | server.Stop(ctx) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | //SetupKVClient creates a new etcd client |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 149 | func SetupKVClient(ctx context.Context, cf *config.RWCoreFlags, coreInstanceID string) kvstore.Client { |
| 150 | client, err := kvstore.NewEtcdClient(ctx, cf.KVStoreAddress, cf.KVStoreTimeout, log.FatalLevel) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 151 | if err != nil { |
| 152 | panic("no kv client") |
| 153 | } |
| 154 | return client |
| 155 | } |