blob: 1470de17569b3d888c5eca2a85c29af70da030a0 [file] [log] [blame]
khenaidoo26721882021-08-11 17:42:52 -04001/*
Joey Armstrong7f8436c2023-07-09 20:23:27 -04002 * Copyright 2021-2023 Open Networking Foundation (ONF) and the ONF Contributors
khenaidoo26721882021-08-11 17:42:52 -04003 *
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 */
16package grpc
17
18import (
19 "context"
20 "strconv"
21
22 vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc"
23 "github.com/opencord/voltha-lib-go/v7/pkg/log"
24 "github.com/opencord/voltha-lib-go/v7/pkg/probe"
khenaidooa5feb8e2021-10-19 17:29:22 -040025 "github.com/opencord/voltha-protos/v5/go/adapter_service"
26 "github.com/opencord/voltha-protos/v5/go/core_service"
khenaidoo26721882021-08-11 17:42:52 -040027 "github.com/phayes/freeport"
28 "google.golang.org/grpc"
29)
30
31const (
32 mockGrpcServer = "mock-grpc-server"
33)
34
35type MockGRPCServer struct {
36 ApiEndpoint string
37 server *vgrpc.GrpcServer
38 probe *probe.Probe
39}
40
41func NewMockGRPCServer(ctx context.Context) (*MockGRPCServer, error) {
42 grpcPort, err := freeport.GetFreePort()
43 if err != nil {
44 return nil, err
45 }
46 s := &MockGRPCServer{
47 ApiEndpoint: "127.0.0.1:" + strconv.Itoa(grpcPort),
48 probe: &probe.Probe{},
49 }
50 probePort, err := freeport.GetFreePort()
51 if err != nil {
52 return nil, err
53 }
54 probeEndpoint := "127.0.0.1:" + strconv.Itoa(probePort)
55 go s.probe.ListenAndServe(ctx, probeEndpoint)
56 s.probe.RegisterService(ctx, mockGrpcServer)
57 s.server = vgrpc.NewGrpcServer(s.ApiEndpoint, nil, false, s.probe)
58
59 logger.Infow(ctx, "mock-grpc-server-created", log.Fields{"endpoint": s.ApiEndpoint})
60 return s, nil
61}
62
khenaidooa5feb8e2021-10-19 17:29:22 -040063func (s *MockGRPCServer) AddCoreService(ctx context.Context, srv core_service.CoreServiceServer) {
khenaidoo26721882021-08-11 17:42:52 -040064 s.server.AddService(func(server *grpc.Server) {
khenaidooa5feb8e2021-10-19 17:29:22 -040065 core_service.RegisterCoreServiceServer(server, srv)
khenaidoo26721882021-08-11 17:42:52 -040066 })
67}
68
khenaidooa5feb8e2021-10-19 17:29:22 -040069func (s *MockGRPCServer) AddAdapterService(ctx context.Context, srv adapter_service.AdapterServiceServer) {
khenaidoo26721882021-08-11 17:42:52 -040070 s.server.AddService(func(server *grpc.Server) {
khenaidooa5feb8e2021-10-19 17:29:22 -040071 adapter_service.RegisterAdapterServiceServer(server, srv)
khenaidoo26721882021-08-11 17:42:52 -040072 })
73}
74
75func (s *MockGRPCServer) Start(ctx context.Context) {
76 s.probe.UpdateStatus(ctx, mockGrpcServer, probe.ServiceStatusRunning)
77 s.server.Start(ctx)
78 s.probe.UpdateStatus(ctx, mockGrpcServer, probe.ServiceStatusStopped)
79}
80
81func (s *MockGRPCServer) Stop() {
82 if s.server != nil {
83 s.server.Stop()
84 }
85}