blob: e69510272fa95440b7ff040e5618a4a47c1f3b49 [file] [log] [blame]
khenaidoo26721882021-08-11 17:42:52 -04001/*
Joey Armstrong9cdee9f2024-01-03 04:56:14 -05002* Copyright 2021-2024 Open Networking Foundation (ONF) and the ONF Contributors
khenaidoo26721882021-08-11 17:42:52 -04003
Joey Armstrong7f8436c2023-07-09 20:23:27 -04004* 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
khenaidoo26721882021-08-11 17:42:52 -04007
Joey Armstrong7f8436c2023-07-09 20:23:27 -04008* http://www.apache.org/licenses/LICENSE-2.0
khenaidoo26721882021-08-11 17:42:52 -04009
Joey Armstrong7f8436c2023-07-09 20:23:27 -040010* 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.
khenaidoo26721882021-08-11 17:42:52 -040015 */
16package grpc
17
18import (
19 "context"
khenaidoo0927c722021-12-15 16:49:32 -050020 "fmt"
khenaidoo26721882021-08-11 17:42:52 -040021 "strconv"
22 "time"
23
khenaidoo0927c722021-12-15 16:49:32 -050024 "github.com/opencord/voltha-lib-go/v7/pkg/log"
khenaidoo26721882021-08-11 17:42:52 -040025 "github.com/opencord/voltha-protos/v5/go/common"
khenaidooa5feb8e2021-10-19 17:29:22 -040026 ca "github.com/opencord/voltha-protos/v5/go/core_adapter"
khenaidoo0927c722021-12-15 16:49:32 -050027 "github.com/opencord/voltha-protos/v5/go/core_service"
khenaidooa5feb8e2021-10-19 17:29:22 -040028 "github.com/opencord/voltha-protos/v5/go/health"
khenaidoo26721882021-08-11 17:42:52 -040029 "github.com/opencord/voltha-protos/v5/go/voltha"
Abhay Kumar062cda52025-12-23 06:49:37 +000030 "google.golang.org/protobuf/types/known/emptypb"
khenaidoo26721882021-08-11 17:42:52 -040031)
32
Joey Armstrong7f8436c2023-07-09 20:23:27 -040033// MockCoreServiceHandler implements the methods in the core service
khenaidoo0927c722021-12-15 16:49:32 -050034type MockCoreServiceHandler struct {
Abhay Kumar062cda52025-12-23 06:49:37 +000035 core_service.UnimplementedCoreServiceServer
khenaidoo0927c722021-12-15 16:49:32 -050036 exitChannel chan struct{}
37}
38
39func NewMockCoreServiceHandler() *MockCoreServiceHandler {
40 return &MockCoreServiceHandler{exitChannel: make(chan struct{})}
41}
42
43func (handler *MockCoreServiceHandler) Start() {
44 logger.Debug(context.Background(), "starting-mock-core-service")
45}
46
47func (handler *MockCoreServiceHandler) Stop() {
48 logger.Debug(context.Background(), "stopping-mock-core-service")
49 close(handler.exitChannel)
50}
khenaidoo26721882021-08-11 17:42:52 -040051
Abhay Kumar062cda52025-12-23 06:49:37 +000052func (handler *MockCoreServiceHandler) RegisterAdapter(ctx context.Context, reg *ca.AdapterRegistration) (*emptypb.Empty, error) {
khenaidoo26721882021-08-11 17:42:52 -040053 //logger.Debugw(ctx, "registration-received", log.Fields{"input": reg})
Abhay Kumar062cda52025-12-23 06:49:37 +000054 return &emptypb.Empty{}, nil
khenaidoo26721882021-08-11 17:42:52 -040055}
56
Abhay Kumar062cda52025-12-23 06:49:37 +000057func (handler *MockCoreServiceHandler) DeviceUpdate(context.Context, *voltha.Device) (*emptypb.Empty, error) {
58 return &emptypb.Empty{}, nil
khenaidoo26721882021-08-11 17:42:52 -040059}
60
Abhay Kumar062cda52025-12-23 06:49:37 +000061func (handler *MockCoreServiceHandler) PortCreated(context.Context, *voltha.Port) (*emptypb.Empty, error) {
62 return &emptypb.Empty{}, nil
khenaidoo26721882021-08-11 17:42:52 -040063}
64
Abhay Kumar062cda52025-12-23 06:49:37 +000065func (handler *MockCoreServiceHandler) PortsStateUpdate(context.Context, *ca.PortStateFilter) (*emptypb.Empty, error) {
66 return &emptypb.Empty{}, nil
khenaidoo26721882021-08-11 17:42:52 -040067}
68
Abhay Kumar062cda52025-12-23 06:49:37 +000069func (handler *MockCoreServiceHandler) DeleteAllPorts(context.Context, *common.ID) (*emptypb.Empty, error) {
70 return &emptypb.Empty{}, nil
khenaidoo26721882021-08-11 17:42:52 -040071}
72
khenaidooa5feb8e2021-10-19 17:29:22 -040073func (handler *MockCoreServiceHandler) GetDevicePort(context.Context, *ca.PortFilter) (*voltha.Port, error) {
khenaidoo26721882021-08-11 17:42:52 -040074 return &voltha.Port{}, nil
75}
76
77func (handler *MockCoreServiceHandler) ListDevicePorts(context.Context, *common.ID) (*voltha.Ports, error) {
78 return &voltha.Ports{}, nil
79}
80
Abhay Kumar062cda52025-12-23 06:49:37 +000081func (handler *MockCoreServiceHandler) DeviceStateUpdate(context.Context, *ca.DeviceStateFilter) (*emptypb.Empty, error) {
82 return &emptypb.Empty{}, nil
khenaidoo26721882021-08-11 17:42:52 -040083}
84
Abhay Kumar062cda52025-12-23 06:49:37 +000085func (handler *MockCoreServiceHandler) DevicePMConfigUpdate(context.Context, *voltha.PmConfigs) (*emptypb.Empty, error) {
86 return &emptypb.Empty{}, nil
khenaidoo26721882021-08-11 17:42:52 -040087}
88
khenaidooa5feb8e2021-10-19 17:29:22 -040089func (handler *MockCoreServiceHandler) ChildDeviceDetected(context.Context, *ca.DeviceDiscovery) (*voltha.Device, error) {
khenaidoo26721882021-08-11 17:42:52 -040090 return &voltha.Device{}, nil
91}
92
Abhay Kumar062cda52025-12-23 06:49:37 +000093func (handler *MockCoreServiceHandler) ChildDevicesLost(context.Context, *common.ID) (*emptypb.Empty, error) {
94 return &emptypb.Empty{}, nil
khenaidoo26721882021-08-11 17:42:52 -040095}
96
Abhay Kumar062cda52025-12-23 06:49:37 +000097func (handler *MockCoreServiceHandler) ChildDevicesDetected(context.Context, *common.ID) (*emptypb.Empty, error) {
khenaidoo26721882021-08-11 17:42:52 -040098 time.Sleep(50 * time.Millisecond)
Abhay Kumar062cda52025-12-23 06:49:37 +000099 return &emptypb.Empty{}, nil
khenaidoo26721882021-08-11 17:42:52 -0400100}
101
102func (handler *MockCoreServiceHandler) GetDevice(ctx context.Context, id *common.ID) (*voltha.Device, error) {
103 time.Sleep(50 * time.Millisecond)
104 vlan, _ := strconv.Atoi(id.Id)
105 return &voltha.Device{
106 Id: id.Id,
107 Type: "test-1234",
108 Vlan: uint32(vlan),
109 }, nil
110}
111
khenaidooa5feb8e2021-10-19 17:29:22 -0400112func (handler *MockCoreServiceHandler) GetChildDevice(context.Context, *ca.ChildDeviceFilter) (*voltha.Device, error) {
khenaidoo26721882021-08-11 17:42:52 -0400113 return nil, nil
114}
115
116func (handler *MockCoreServiceHandler) GetChildDevices(context.Context, *common.ID) (*voltha.Devices, error) {
117 return &voltha.Devices{}, nil
118}
119
Abhay Kumar062cda52025-12-23 06:49:37 +0000120func (handler *MockCoreServiceHandler) SendPacketIn(context.Context, *ca.PacketIn) (*emptypb.Empty, error) {
121 return &emptypb.Empty{}, nil
khenaidoo26721882021-08-11 17:42:52 -0400122}
123
Abhay Kumar062cda52025-12-23 06:49:37 +0000124func (handler *MockCoreServiceHandler) DeviceReasonUpdate(context.Context, *ca.DeviceReason) (*emptypb.Empty, error) {
125 return &emptypb.Empty{}, nil
khenaidoo26721882021-08-11 17:42:52 -0400126}
127
Abhay Kumar062cda52025-12-23 06:49:37 +0000128func (handler *MockCoreServiceHandler) PortStateUpdate(context.Context, *ca.PortState) (*emptypb.Empty, error) {
129 return &emptypb.Empty{}, nil
khenaidoo26721882021-08-11 17:42:52 -0400130}
131
132// Additional API found in the Core - unused?
Abhay Kumar062cda52025-12-23 06:49:37 +0000133func (handler *MockCoreServiceHandler) ReconcileChildDevices(context.Context, *common.ID) (*emptypb.Empty, error) {
134 return &emptypb.Empty{}, nil
khenaidoo26721882021-08-11 17:42:52 -0400135}
136
137func (handler *MockCoreServiceHandler) GetChildDeviceWithProxyAddress(context.Context, *voltha.Device_ProxyAddress) (*voltha.Device, error) {
138 return &voltha.Device{}, nil
139}
140
khenaidooa5feb8e2021-10-19 17:29:22 -0400141func (handler *MockCoreServiceHandler) GetPorts(context.Context, *ca.PortFilter) (*voltha.Ports, error) {
khenaidoo26721882021-08-11 17:42:52 -0400142 return &voltha.Ports{}, nil
143}
144
Abhay Kumar062cda52025-12-23 06:49:37 +0000145func (handler *MockCoreServiceHandler) ChildrenStateUpdate(context.Context, *ca.DeviceStateFilter) (*emptypb.Empty, error) {
146 return &emptypb.Empty{}, nil
khenaidoo26721882021-08-11 17:42:52 -0400147}
148
Abhay Kumar062cda52025-12-23 06:49:37 +0000149func (handler *MockCoreServiceHandler) UpdateImageDownload(context.Context, *voltha.ImageDownload) (*emptypb.Empty, error) {
150 return &emptypb.Empty{}, nil
khenaidoo26721882021-08-11 17:42:52 -0400151}
152
khenaidoo0927c722021-12-15 16:49:32 -0500153func (handler *MockCoreServiceHandler) GetHealthStatus(stream core_service.CoreService_GetHealthStatusServer) error {
154 logger.Debugw(context.Background(), "keep-alive-connection", log.Fields{"stream": stream})
155 if stream == nil {
156 return fmt.Errorf("stream-is-nil %v", stream)
157 }
158 var err error
159 var remoteClient *common.Connection
160 var tempClient *common.Connection
161 ctx := context.Background()
162loop:
163 for {
164 tempClient, err = stream.Recv()
165 if err != nil {
166 logger.Warnw(ctx, "received-stream-error", log.Fields{"remote-client": remoteClient, "error": err})
167 break loop
168 }
169 // Send a response back
170 err = stream.Send(&health.HealthStatus{State: health.HealthStatus_HEALTHY})
171 if err != nil {
172 logger.Warnw(ctx, "sending-stream-error", log.Fields{"remote-client": remoteClient, "error": err})
173 break loop
174 }
175
176 remoteClient = tempClient
177 logger.Debugw(ctx, "received-keep-alive", log.Fields{"remote-client": remoteClient})
178 select {
179 case <-stream.Context().Done():
180 logger.Infow(ctx, "stream-keep-alive-context-done", log.Fields{"remote-client": remoteClient, "error": stream.Context().Err()})
181 break loop
182 case <-handler.exitChannel:
183 logger.Warnw(ctx, "received-stop", log.Fields{"remote-client": remoteClient})
184 break loop
185 default:
186 }
187 }
188 logger.Errorw(context.Background(), "connection-down", log.Fields{"remote-client": remoteClient, "error": err})
189 return err
khenaidoo26721882021-08-11 17:42:52 -0400190}