blob: 5c78dff2de45d6fc7a869cb25db7b7aa2fcddbd5 [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
24 "github.com/golang/protobuf/ptypes/empty"
khenaidoo0927c722021-12-15 16:49:32 -050025 "github.com/opencord/voltha-lib-go/v7/pkg/log"
khenaidoo26721882021-08-11 17:42:52 -040026 "github.com/opencord/voltha-protos/v5/go/common"
khenaidooa5feb8e2021-10-19 17:29:22 -040027 ca "github.com/opencord/voltha-protos/v5/go/core_adapter"
khenaidoo0927c722021-12-15 16:49:32 -050028 "github.com/opencord/voltha-protos/v5/go/core_service"
khenaidooa5feb8e2021-10-19 17:29:22 -040029 "github.com/opencord/voltha-protos/v5/go/health"
khenaidoo26721882021-08-11 17:42:52 -040030 "github.com/opencord/voltha-protos/v5/go/voltha"
31)
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 {
35 exitChannel chan struct{}
36}
37
38func NewMockCoreServiceHandler() *MockCoreServiceHandler {
39 return &MockCoreServiceHandler{exitChannel: make(chan struct{})}
40}
41
42func (handler *MockCoreServiceHandler) Start() {
43 logger.Debug(context.Background(), "starting-mock-core-service")
44}
45
46func (handler *MockCoreServiceHandler) Stop() {
47 logger.Debug(context.Background(), "stopping-mock-core-service")
48 close(handler.exitChannel)
49}
khenaidoo26721882021-08-11 17:42:52 -040050
khenaidooa5feb8e2021-10-19 17:29:22 -040051func (handler *MockCoreServiceHandler) RegisterAdapter(ctx context.Context, reg *ca.AdapterRegistration) (*empty.Empty, error) {
khenaidoo26721882021-08-11 17:42:52 -040052 //logger.Debugw(ctx, "registration-received", log.Fields{"input": reg})
53 return &empty.Empty{}, nil
54}
55
56func (handler *MockCoreServiceHandler) DeviceUpdate(context.Context, *voltha.Device) (*empty.Empty, error) {
57 return &empty.Empty{}, nil
58}
59
60func (handler *MockCoreServiceHandler) PortCreated(context.Context, *voltha.Port) (*empty.Empty, error) {
61 return &empty.Empty{}, nil
62}
63
khenaidooa5feb8e2021-10-19 17:29:22 -040064func (handler *MockCoreServiceHandler) PortsStateUpdate(context.Context, *ca.PortStateFilter) (*empty.Empty, error) {
khenaidoo26721882021-08-11 17:42:52 -040065 return &empty.Empty{}, nil
66}
67
68func (handler *MockCoreServiceHandler) DeleteAllPorts(context.Context, *common.ID) (*empty.Empty, error) {
69 return &empty.Empty{}, nil
70}
71
khenaidooa5feb8e2021-10-19 17:29:22 -040072func (handler *MockCoreServiceHandler) GetDevicePort(context.Context, *ca.PortFilter) (*voltha.Port, error) {
khenaidoo26721882021-08-11 17:42:52 -040073 return &voltha.Port{}, nil
74}
75
76func (handler *MockCoreServiceHandler) ListDevicePorts(context.Context, *common.ID) (*voltha.Ports, error) {
77 return &voltha.Ports{}, nil
78}
79
khenaidooa5feb8e2021-10-19 17:29:22 -040080func (handler *MockCoreServiceHandler) DeviceStateUpdate(context.Context, *ca.DeviceStateFilter) (*empty.Empty, error) {
khenaidoo26721882021-08-11 17:42:52 -040081 return &empty.Empty{}, nil
82}
83
84func (handler *MockCoreServiceHandler) DevicePMConfigUpdate(context.Context, *voltha.PmConfigs) (*empty.Empty, error) {
85 return &empty.Empty{}, nil
86}
87
khenaidooa5feb8e2021-10-19 17:29:22 -040088func (handler *MockCoreServiceHandler) ChildDeviceDetected(context.Context, *ca.DeviceDiscovery) (*voltha.Device, error) {
khenaidoo26721882021-08-11 17:42:52 -040089 return &voltha.Device{}, nil
90}
91
92func (handler *MockCoreServiceHandler) ChildDevicesLost(context.Context, *common.ID) (*empty.Empty, error) {
93 return &empty.Empty{}, nil
94}
95
96func (handler *MockCoreServiceHandler) ChildDevicesDetected(context.Context, *common.ID) (*empty.Empty, error) {
97 time.Sleep(50 * time.Millisecond)
98 return &empty.Empty{}, nil
99}
100
101func (handler *MockCoreServiceHandler) GetDevice(ctx context.Context, id *common.ID) (*voltha.Device, error) {
102 time.Sleep(50 * time.Millisecond)
103 vlan, _ := strconv.Atoi(id.Id)
104 return &voltha.Device{
105 Id: id.Id,
106 Type: "test-1234",
107 Vlan: uint32(vlan),
108 }, nil
109}
110
khenaidooa5feb8e2021-10-19 17:29:22 -0400111func (handler *MockCoreServiceHandler) GetChildDevice(context.Context, *ca.ChildDeviceFilter) (*voltha.Device, error) {
khenaidoo26721882021-08-11 17:42:52 -0400112 return nil, nil
113}
114
115func (handler *MockCoreServiceHandler) GetChildDevices(context.Context, *common.ID) (*voltha.Devices, error) {
116 return &voltha.Devices{}, nil
117}
118
khenaidooa5feb8e2021-10-19 17:29:22 -0400119func (handler *MockCoreServiceHandler) SendPacketIn(context.Context, *ca.PacketIn) (*empty.Empty, error) {
khenaidoo26721882021-08-11 17:42:52 -0400120 return &empty.Empty{}, nil
121}
122
khenaidooa5feb8e2021-10-19 17:29:22 -0400123func (handler *MockCoreServiceHandler) DeviceReasonUpdate(context.Context, *ca.DeviceReason) (*empty.Empty, error) {
khenaidoo26721882021-08-11 17:42:52 -0400124 return &empty.Empty{}, nil
125}
126
khenaidooa5feb8e2021-10-19 17:29:22 -0400127func (handler *MockCoreServiceHandler) PortStateUpdate(context.Context, *ca.PortState) (*empty.Empty, error) {
khenaidoo26721882021-08-11 17:42:52 -0400128 return &empty.Empty{}, nil
129}
130
131// Additional API found in the Core - unused?
132func (handler *MockCoreServiceHandler) ReconcileChildDevices(context.Context, *common.ID) (*empty.Empty, error) {
133 return &empty.Empty{}, nil
134}
135
136func (handler *MockCoreServiceHandler) GetChildDeviceWithProxyAddress(context.Context, *voltha.Device_ProxyAddress) (*voltha.Device, error) {
137 return &voltha.Device{}, nil
138}
139
khenaidooa5feb8e2021-10-19 17:29:22 -0400140func (handler *MockCoreServiceHandler) GetPorts(context.Context, *ca.PortFilter) (*voltha.Ports, error) {
khenaidoo26721882021-08-11 17:42:52 -0400141 return &voltha.Ports{}, nil
142}
143
khenaidooa5feb8e2021-10-19 17:29:22 -0400144func (handler *MockCoreServiceHandler) ChildrenStateUpdate(context.Context, *ca.DeviceStateFilter) (*empty.Empty, error) {
khenaidoo26721882021-08-11 17:42:52 -0400145 return &empty.Empty{}, nil
146}
147
148func (handler *MockCoreServiceHandler) UpdateImageDownload(context.Context, *voltha.ImageDownload) (*empty.Empty, error) {
149 return &empty.Empty{}, nil
150}
151
khenaidoo0927c722021-12-15 16:49:32 -0500152func (handler *MockCoreServiceHandler) GetHealthStatus(stream core_service.CoreService_GetHealthStatusServer) error {
153 logger.Debugw(context.Background(), "keep-alive-connection", log.Fields{"stream": stream})
154 if stream == nil {
155 return fmt.Errorf("stream-is-nil %v", stream)
156 }
157 var err error
158 var remoteClient *common.Connection
159 var tempClient *common.Connection
160 ctx := context.Background()
161loop:
162 for {
163 tempClient, err = stream.Recv()
164 if err != nil {
165 logger.Warnw(ctx, "received-stream-error", log.Fields{"remote-client": remoteClient, "error": err})
166 break loop
167 }
168 // Send a response back
169 err = stream.Send(&health.HealthStatus{State: health.HealthStatus_HEALTHY})
170 if err != nil {
171 logger.Warnw(ctx, "sending-stream-error", log.Fields{"remote-client": remoteClient, "error": err})
172 break loop
173 }
174
175 remoteClient = tempClient
176 logger.Debugw(ctx, "received-keep-alive", log.Fields{"remote-client": remoteClient})
177 select {
178 case <-stream.Context().Done():
179 logger.Infow(ctx, "stream-keep-alive-context-done", log.Fields{"remote-client": remoteClient, "error": stream.Context().Err()})
180 break loop
181 case <-handler.exitChannel:
182 logger.Warnw(ctx, "received-stop", log.Fields{"remote-client": remoteClient})
183 break loop
184 default:
185 }
186 }
187 logger.Errorw(context.Background(), "connection-down", log.Fields{"remote-client": remoteClient, "error": err})
188 return err
khenaidoo26721882021-08-11 17:42:52 -0400189}