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 ( |
| 21 | "strconv" |
| 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" |
| 27 | "github.com/opencord/voltha-lib-go/v3/pkg/adapters" |
| 28 | com "github.com/opencord/voltha-lib-go/v3/pkg/adapters/common" |
| 29 | "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore" |
| 30 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
| 31 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 32 | mock_etcd "github.com/opencord/voltha-lib-go/v3/pkg/mocks/etcd" |
| 33 | "github.com/opencord/voltha-lib-go/v3/pkg/version" |
| 34 | "github.com/opencord/voltha-protos/v3/go/voltha" |
| 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 |
| 47 | func CreateMockAdapter(adapterType int, kafkaClient kafka.Client, coreInstanceID string, coreName string, adapterName string) (adapters.IAdapter, error) { |
| 48 | var err error |
| 49 | var adapter adapters.IAdapter |
| 50 | adapterKafkaICProxy := kafka.NewInterContainerProxy( |
| 51 | kafka.MsgClient(kafkaClient), |
| 52 | kafka.DefaultTopic(&kafka.Topic{Name: adapterName})) |
| 53 | adapterCoreProxy := com.NewCoreProxy(adapterKafkaICProxy, adapterName, coreName) |
| 54 | var adapterReqHandler *com.RequestHandlerProxy |
| 55 | switch adapterType { |
| 56 | case OltAdapter: |
| 57 | adapter = cm.NewOLTAdapter(adapterCoreProxy) |
| 58 | case OnuAdapter: |
| 59 | adapter = cm.NewONUAdapter(adapterCoreProxy) |
| 60 | default: |
| 61 | logger.Fatalf("invalid-adapter-type-%d", adapterType) |
| 62 | } |
| 63 | adapterReqHandler = com.NewRequestHandlerProxy(coreInstanceID, adapter, adapterCoreProxy) |
| 64 | |
| 65 | if err = adapterKafkaICProxy.Start(); err != nil { |
| 66 | logger.Errorw("Failure-starting-adapter-intercontainerProxy", log.Fields{"error": err}) |
| 67 | return nil, err |
| 68 | } |
| 69 | if err = adapterKafkaICProxy.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: adapterName}, adapterReqHandler); err != nil { |
| 70 | logger.Errorw("Failure-to-subscribe-onu-request-handler", log.Fields{"error": err}) |
| 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 |
| 77 | func CreateAndregisterAdapters(t *testing.T, kClient kafka.Client, coreInstanceID string, oltAdapterName string, onuAdapterName string, adapterMgr *adapter.Manager) (*cm.OLTAdapter, *cm.ONUAdapter) { |
| 78 | // Setup the mock OLT adapter |
| 79 | oltAdapter, err := CreateMockAdapter(OltAdapter, kClient, coreInstanceID, "rw_core", oltAdapterName) |
| 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} |
| 95 | if _, err := adapterMgr.RegisterAdapter(registrationData, deviceTypes); err != nil { |
| 96 | logger.Errorw("failed-to-register-adapter", log.Fields{"error": err}) |
| 97 | assert.NotNil(t, err) |
| 98 | } |
| 99 | |
| 100 | // Setup the mock ONU adapter |
| 101 | onuAdapter, err := CreateMockAdapter(OnuAdapter, kClient, coreInstanceID, "rw_core", onuAdapterName) |
| 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} |
| 117 | if _, err := adapterMgr.RegisterAdapter(registrationData, deviceTypes); err != nil { |
| 118 | logger.Errorw("failed-to-register-adapter", log.Fields{"error": err}) |
| 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. |
| 125 | func StartEmbeddedEtcdServer(configName, storageDir, logLevel string) (*mock_etcd.EtcdServer, int, error) { |
| 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 | } |
| 134 | etcdServer := mock_etcd.StartEtcdServer(mock_etcd.MKConfig(configName, kvClientPort, peerPort, storageDir, logLevel)) |
| 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 |
| 142 | func StopEmbeddedEtcdServer(server *mock_etcd.EtcdServer) { |
| 143 | if server != nil { |
| 144 | server.Stop() |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | //SetupKVClient creates a new etcd client |
| 149 | func SetupKVClient(cf *config.RWCoreFlags, coreInstanceID string) kvstore.Client { |
| 150 | addr := cf.KVStoreHost + ":" + strconv.Itoa(cf.KVStorePort) |
| 151 | client, err := kvstore.NewEtcdClient(addr, cf.KVStoreTimeout, log.FatalLevel) |
| 152 | if err != nil { |
| 153 | panic("no kv client") |
| 154 | } |
| 155 | return client |
| 156 | } |