blob: 8365956971003c4af17406ce0a396e0a51a82bac [file] [log] [blame]
khenaidoo106c61a2021-08-11 18:05:46 -04001/*
2 * Copyright 2021-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 */
16package grpc
17
18import (
19 "context"
khenaidooefff76e2021-12-15 16:51:30 -050020 "fmt"
khenaidoo106c61a2021-08-11 18:05:46 -040021 "strconv"
22 "time"
23
24 "github.com/golang/protobuf/ptypes/empty"
khenaidooefff76e2021-12-15 16:51:30 -050025 "github.com/opencord/voltha-lib-go/v7/pkg/log"
khenaidoo106c61a2021-08-11 18:05:46 -040026 "github.com/opencord/voltha-protos/v5/go/common"
khenaidoodc2116e2021-10-19 17:33:19 -040027 ca "github.com/opencord/voltha-protos/v5/go/core_adapter"
khenaidooefff76e2021-12-15 16:51:30 -050028 "github.com/opencord/voltha-protos/v5/go/core_service"
khenaidoodc2116e2021-10-19 17:33:19 -040029 "github.com/opencord/voltha-protos/v5/go/health"
khenaidoo106c61a2021-08-11 18:05:46 -040030 "github.com/opencord/voltha-protos/v5/go/voltha"
31)
32
33//MockCoreServiceHandler implements the methods in the core service
khenaidooefff76e2021-12-15 16:51:30 -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}
khenaidoo106c61a2021-08-11 18:05:46 -040050
khenaidoodc2116e2021-10-19 17:33:19 -040051func (handler *MockCoreServiceHandler) RegisterAdapter(ctx context.Context, reg *ca.AdapterRegistration) (*empty.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -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
khenaidoodc2116e2021-10-19 17:33:19 -040064func (handler *MockCoreServiceHandler) PortsStateUpdate(context.Context, *ca.PortStateFilter) (*empty.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -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
khenaidoodc2116e2021-10-19 17:33:19 -040072func (handler *MockCoreServiceHandler) GetDevicePort(context.Context, *ca.PortFilter) (*voltha.Port, error) {
khenaidoo106c61a2021-08-11 18:05:46 -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
khenaidoodc2116e2021-10-19 17:33:19 -040080func (handler *MockCoreServiceHandler) DeviceStateUpdate(context.Context, *ca.DeviceStateFilter) (*empty.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -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
khenaidoodc2116e2021-10-19 17:33:19 -040088func (handler *MockCoreServiceHandler) ChildDeviceDetected(context.Context, *ca.DeviceDiscovery) (*voltha.Device, error) {
khenaidoo106c61a2021-08-11 18:05:46 -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
khenaidoodc2116e2021-10-19 17:33:19 -0400111func (handler *MockCoreServiceHandler) GetChildDevice(context.Context, *ca.ChildDeviceFilter) (*voltha.Device, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400112 return nil, nil
113}
114
115func (handler *MockCoreServiceHandler) GetChildDevices(context.Context, *common.ID) (*voltha.Devices, error) {
116 return &voltha.Devices{}, nil
117}
118
khenaidoodc2116e2021-10-19 17:33:19 -0400119func (handler *MockCoreServiceHandler) SendPacketIn(context.Context, *ca.PacketIn) (*empty.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400120 return &empty.Empty{}, nil
121}
122
khenaidoodc2116e2021-10-19 17:33:19 -0400123func (handler *MockCoreServiceHandler) DeviceReasonUpdate(context.Context, *ca.DeviceReason) (*empty.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400124 return &empty.Empty{}, nil
125}
126
khenaidoodc2116e2021-10-19 17:33:19 -0400127func (handler *MockCoreServiceHandler) PortStateUpdate(context.Context, *ca.PortState) (*empty.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -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
khenaidoodc2116e2021-10-19 17:33:19 -0400140func (handler *MockCoreServiceHandler) GetPorts(context.Context, *ca.PortFilter) (*voltha.Ports, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400141 return &voltha.Ports{}, nil
142}
143
khenaidoodc2116e2021-10-19 17:33:19 -0400144func (handler *MockCoreServiceHandler) ChildrenStateUpdate(context.Context, *ca.DeviceStateFilter) (*empty.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -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
khenaidooefff76e2021-12-15 16:51:30 -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
khenaidoo106c61a2021-08-11 18:05:46 -0400189}