blob: d9d42dfb1fce6fd62cf9e5fc5330306a28c5b97e [file] [log] [blame]
Mahir Gunyel03de0d32020-06-03 01:36:59 -07001/*
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
18package test
19
20import (
Rohan Agrawal31f21802020-06-12 05:38:46 +000021 "context"
Mahir Gunyel03de0d32020-06-03 01:36:59 -070022 "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"
Maninderdfadc982020-10-28 14:04:33 +053027 "github.com/opencord/voltha-lib-go/v4/pkg/adapters"
28 com "github.com/opencord/voltha-lib-go/v4/pkg/adapters/common"
29 "github.com/opencord/voltha-lib-go/v4/pkg/db/kvstore"
30 "github.com/opencord/voltha-lib-go/v4/pkg/kafka"
31 "github.com/opencord/voltha-lib-go/v4/pkg/log"
32 mock_etcd "github.com/opencord/voltha-lib-go/v4/pkg/mocks/etcd"
33 "github.com/opencord/voltha-lib-go/v4/pkg/version"
34 "github.com/opencord/voltha-protos/v4/go/voltha"
Mahir Gunyel03de0d32020-06-03 01:36:59 -070035 "github.com/phayes/freeport"
36 "github.com/stretchr/testify/assert"
37 "google.golang.org/grpc/codes"
38 "google.golang.org/grpc/status"
39)
40
41const (
42 OltAdapter = iota
43 OnuAdapter
44)
45
46//CreateMockAdapter creates mock OLT and ONU adapters
Rohan Agrawal31f21802020-06-12 05:38:46 +000047func CreateMockAdapter(ctx context.Context, adapterType int, kafkaClient kafka.Client, coreInstanceID string, coreName string, adapterName string) (adapters.IAdapter, error) {
Mahir Gunyel03de0d32020-06-03 01:36:59 -070048 var err error
49 var adapter adapters.IAdapter
50 adapterKafkaICProxy := kafka.NewInterContainerProxy(
51 kafka.MsgClient(kafkaClient),
52 kafka.DefaultTopic(&kafka.Topic{Name: adapterName}))
Rohan Agrawal31f21802020-06-12 05:38:46 +000053 adapterCoreProxy := com.NewCoreProxy(ctx, adapterKafkaICProxy, adapterName, coreName)
Mahir Gunyel03de0d32020-06-03 01:36:59 -070054 var adapterReqHandler *com.RequestHandlerProxy
55 switch adapterType {
56 case OltAdapter:
Rohan Agrawal31f21802020-06-12 05:38:46 +000057 adapter = cm.NewOLTAdapter(ctx, adapterCoreProxy)
Mahir Gunyel03de0d32020-06-03 01:36:59 -070058 case OnuAdapter:
Rohan Agrawal31f21802020-06-12 05:38:46 +000059 adapter = cm.NewONUAdapter(ctx, adapterCoreProxy)
Mahir Gunyel03de0d32020-06-03 01:36:59 -070060 default:
Rohan Agrawal31f21802020-06-12 05:38:46 +000061 logger.Fatalf(ctx, "invalid-adapter-type-%d", adapterType)
Mahir Gunyel03de0d32020-06-03 01:36:59 -070062 }
63 adapterReqHandler = com.NewRequestHandlerProxy(coreInstanceID, adapter, adapterCoreProxy)
64
Rohan Agrawal31f21802020-06-12 05:38:46 +000065 if err = adapterKafkaICProxy.Start(ctx); err != nil {
66 logger.Errorw(ctx, "Failure-starting-adapter-intercontainerProxy", log.Fields{"error": err})
Mahir Gunyel03de0d32020-06-03 01:36:59 -070067 return nil, err
68 }
Rohan Agrawal31f21802020-06-12 05:38:46 +000069 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 Gunyel03de0d32020-06-03 01:36:59 -070071 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 Agrawal31f21802020-06-12 05:38:46 +000077func CreateAndregisterAdapters(ctx context.Context, t *testing.T, kClient kafka.Client, coreInstanceID string, oltAdapterName string, onuAdapterName string, adapterMgr *adapter.Manager) (*cm.OLTAdapter, *cm.ONUAdapter) {
Mahir Gunyel03de0d32020-06-03 01:36:59 -070078 // Setup the mock OLT adapter
Rohan Agrawal31f21802020-06-12 05:38:46 +000079 oltAdapter, err := CreateMockAdapter(ctx, OltAdapter, kClient, coreInstanceID, "rw_core", oltAdapterName)
Mahir Gunyel03de0d32020-06-03 01:36:59 -070080 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 Agrawal31f21802020-06-12 05:38:46 +000095 if _, err := adapterMgr.RegisterAdapter(ctx, registrationData, deviceTypes); err != nil {
96 logger.Errorw(ctx, "failed-to-register-adapter", log.Fields{"error": err})
Mahir Gunyel03de0d32020-06-03 01:36:59 -070097 assert.NotNil(t, err)
98 }
99
100 // Setup the mock ONU adapter
Rohan Agrawal31f21802020-06-12 05:38:46 +0000101 onuAdapter, err := CreateMockAdapter(ctx, OnuAdapter, kClient, coreInstanceID, "rw_core", onuAdapterName)
Mahir Gunyel03de0d32020-06-03 01:36:59 -0700102
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 Agrawal31f21802020-06-12 05:38:46 +0000117 if _, err := adapterMgr.RegisterAdapter(ctx, registrationData, deviceTypes); err != nil {
118 logger.Errorw(ctx, "failed-to-register-adapter", log.Fields{"error": err})
Mahir Gunyel03de0d32020-06-03 01:36:59 -0700119 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 Agrawal31f21802020-06-12 05:38:46 +0000125func StartEmbeddedEtcdServer(ctx context.Context, configName, storageDir, logLevel string) (*mock_etcd.EtcdServer, int, error) {
Mahir Gunyel03de0d32020-06-03 01:36:59 -0700126 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 Agrawal31f21802020-06-12 05:38:46 +0000134 etcdServer := mock_etcd.StartEtcdServer(ctx, mock_etcd.MKConfig(ctx, configName, kvClientPort, peerPort, storageDir, logLevel))
Mahir Gunyel03de0d32020-06-03 01:36:59 -0700135 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 Agrawal31f21802020-06-12 05:38:46 +0000142func StopEmbeddedEtcdServer(ctx context.Context, server *mock_etcd.EtcdServer) {
Mahir Gunyel03de0d32020-06-03 01:36:59 -0700143 if server != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000144 server.Stop(ctx)
Mahir Gunyel03de0d32020-06-03 01:36:59 -0700145 }
146}
147
148//SetupKVClient creates a new etcd client
Rohan Agrawal31f21802020-06-12 05:38:46 +0000149func SetupKVClient(ctx context.Context, cf *config.RWCoreFlags, coreInstanceID string) kvstore.Client {
150 client, err := kvstore.NewEtcdClient(ctx, cf.KVStoreAddress, cf.KVStoreTimeout, log.FatalLevel)
Mahir Gunyel03de0d32020-06-03 01:36:59 -0700151 if err != nil {
152 panic("no kv client")
153 }
154 return client
155}