khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 1 | /* |
Joey Armstrong | 9cdee9f | 2024-01-03 04:56:14 -0500 | [diff] [blame^] | 2 | * Copyright 2021-2024 Open Networking Foundation (ONF) and the ONF Contributors |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 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 | package grpc |
| 17 | |
| 18 | import ( |
| 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" |
khenaidoo | a5feb8e | 2021-10-19 17:29:22 -0400 | [diff] [blame] | 25 | "github.com/opencord/voltha-protos/v5/go/adapter_service" |
| 26 | "github.com/opencord/voltha-protos/v5/go/core_service" |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 27 | "github.com/phayes/freeport" |
| 28 | "google.golang.org/grpc" |
| 29 | ) |
| 30 | |
| 31 | const ( |
| 32 | mockGrpcServer = "mock-grpc-server" |
| 33 | ) |
| 34 | |
| 35 | type MockGRPCServer struct { |
| 36 | ApiEndpoint string |
| 37 | server *vgrpc.GrpcServer |
| 38 | probe *probe.Probe |
| 39 | } |
| 40 | |
| 41 | func 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 | |
khenaidoo | a5feb8e | 2021-10-19 17:29:22 -0400 | [diff] [blame] | 63 | func (s *MockGRPCServer) AddCoreService(ctx context.Context, srv core_service.CoreServiceServer) { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 64 | s.server.AddService(func(server *grpc.Server) { |
khenaidoo | a5feb8e | 2021-10-19 17:29:22 -0400 | [diff] [blame] | 65 | core_service.RegisterCoreServiceServer(server, srv) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 66 | }) |
| 67 | } |
| 68 | |
khenaidoo | a5feb8e | 2021-10-19 17:29:22 -0400 | [diff] [blame] | 69 | func (s *MockGRPCServer) AddAdapterService(ctx context.Context, srv adapter_service.AdapterServiceServer) { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 70 | s.server.AddService(func(server *grpc.Server) { |
khenaidoo | a5feb8e | 2021-10-19 17:29:22 -0400 | [diff] [blame] | 71 | adapter_service.RegisterAdapterServiceServer(server, srv) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 72 | }) |
| 73 | } |
| 74 | |
| 75 | func (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 | |
| 81 | func (s *MockGRPCServer) Stop() { |
| 82 | if s.server != nil { |
| 83 | s.server.Stop() |
| 84 | } |
| 85 | } |