khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 1 | /* |
Joey Armstrong | f850dca | 2024-01-18 18:25:46 -0500 | [diff] [blame] | 2 | * Copyright 2021-2024 Open Networking Foundation (ONF) and the ONF Contributors |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 3 | |
Mahir Gunyel | 7e4cd8a | 2023-07-24 09:44:07 +0300 | [diff] [blame] | 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 |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 7 | |
Mahir Gunyel | 7e4cd8a | 2023-07-24 09:44:07 +0300 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 9 | |
Mahir Gunyel | 7e4cd8a | 2023-07-24 09:44:07 +0300 | [diff] [blame] | 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. |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 15 | */ |
| 16 | package grpc |
| 17 | |
| 18 | import ( |
| 19 | "context" |
khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 20 | "fmt" |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 21 | "strconv" |
| 22 | "time" |
| 23 | |
| 24 | "github.com/golang/protobuf/ptypes/empty" |
khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 26 | "github.com/opencord/voltha-protos/v5/go/common" |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 27 | ca "github.com/opencord/voltha-protos/v5/go/core_adapter" |
khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 28 | "github.com/opencord/voltha-protos/v5/go/core_service" |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 29 | "github.com/opencord/voltha-protos/v5/go/health" |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 30 | "github.com/opencord/voltha-protos/v5/go/voltha" |
| 31 | ) |
| 32 | |
Mahir Gunyel | 7e4cd8a | 2023-07-24 09:44:07 +0300 | [diff] [blame] | 33 | // MockCoreServiceHandler implements the methods in the core service |
khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 34 | type MockCoreServiceHandler struct { |
| 35 | exitChannel chan struct{} |
| 36 | } |
| 37 | |
| 38 | func NewMockCoreServiceHandler() *MockCoreServiceHandler { |
| 39 | return &MockCoreServiceHandler{exitChannel: make(chan struct{})} |
| 40 | } |
| 41 | |
| 42 | func (handler *MockCoreServiceHandler) Start() { |
| 43 | logger.Debug(context.Background(), "starting-mock-core-service") |
| 44 | } |
| 45 | |
| 46 | func (handler *MockCoreServiceHandler) Stop() { |
| 47 | logger.Debug(context.Background(), "stopping-mock-core-service") |
| 48 | close(handler.exitChannel) |
| 49 | } |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 50 | |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 51 | func (handler *MockCoreServiceHandler) RegisterAdapter(ctx context.Context, reg *ca.AdapterRegistration) (*empty.Empty, error) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 52 | //logger.Debugw(ctx, "registration-received", log.Fields{"input": reg}) |
| 53 | return &empty.Empty{}, nil |
| 54 | } |
| 55 | |
| 56 | func (handler *MockCoreServiceHandler) DeviceUpdate(context.Context, *voltha.Device) (*empty.Empty, error) { |
| 57 | return &empty.Empty{}, nil |
| 58 | } |
| 59 | |
| 60 | func (handler *MockCoreServiceHandler) PortCreated(context.Context, *voltha.Port) (*empty.Empty, error) { |
| 61 | return &empty.Empty{}, nil |
| 62 | } |
| 63 | |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 64 | func (handler *MockCoreServiceHandler) PortsStateUpdate(context.Context, *ca.PortStateFilter) (*empty.Empty, error) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 65 | return &empty.Empty{}, nil |
| 66 | } |
| 67 | |
| 68 | func (handler *MockCoreServiceHandler) DeleteAllPorts(context.Context, *common.ID) (*empty.Empty, error) { |
| 69 | return &empty.Empty{}, nil |
| 70 | } |
| 71 | |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 72 | func (handler *MockCoreServiceHandler) GetDevicePort(context.Context, *ca.PortFilter) (*voltha.Port, error) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 73 | return &voltha.Port{}, nil |
| 74 | } |
| 75 | |
| 76 | func (handler *MockCoreServiceHandler) ListDevicePorts(context.Context, *common.ID) (*voltha.Ports, error) { |
| 77 | return &voltha.Ports{}, nil |
| 78 | } |
| 79 | |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 80 | func (handler *MockCoreServiceHandler) DeviceStateUpdate(context.Context, *ca.DeviceStateFilter) (*empty.Empty, error) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 81 | return &empty.Empty{}, nil |
| 82 | } |
| 83 | |
| 84 | func (handler *MockCoreServiceHandler) DevicePMConfigUpdate(context.Context, *voltha.PmConfigs) (*empty.Empty, error) { |
| 85 | return &empty.Empty{}, nil |
| 86 | } |
| 87 | |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 88 | func (handler *MockCoreServiceHandler) ChildDeviceDetected(context.Context, *ca.DeviceDiscovery) (*voltha.Device, error) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 89 | return &voltha.Device{}, nil |
| 90 | } |
| 91 | |
| 92 | func (handler *MockCoreServiceHandler) ChildDevicesLost(context.Context, *common.ID) (*empty.Empty, error) { |
| 93 | return &empty.Empty{}, nil |
| 94 | } |
| 95 | |
| 96 | func (handler *MockCoreServiceHandler) ChildDevicesDetected(context.Context, *common.ID) (*empty.Empty, error) { |
| 97 | time.Sleep(50 * time.Millisecond) |
| 98 | return &empty.Empty{}, nil |
| 99 | } |
| 100 | |
| 101 | func (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 | |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 111 | func (handler *MockCoreServiceHandler) GetChildDevice(context.Context, *ca.ChildDeviceFilter) (*voltha.Device, error) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 112 | return nil, nil |
| 113 | } |
| 114 | |
| 115 | func (handler *MockCoreServiceHandler) GetChildDevices(context.Context, *common.ID) (*voltha.Devices, error) { |
| 116 | return &voltha.Devices{}, nil |
| 117 | } |
| 118 | |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 119 | func (handler *MockCoreServiceHandler) SendPacketIn(context.Context, *ca.PacketIn) (*empty.Empty, error) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 120 | return &empty.Empty{}, nil |
| 121 | } |
| 122 | |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 123 | func (handler *MockCoreServiceHandler) DeviceReasonUpdate(context.Context, *ca.DeviceReason) (*empty.Empty, error) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 124 | return &empty.Empty{}, nil |
| 125 | } |
| 126 | |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 127 | func (handler *MockCoreServiceHandler) PortStateUpdate(context.Context, *ca.PortState) (*empty.Empty, error) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 128 | return &empty.Empty{}, nil |
| 129 | } |
| 130 | |
| 131 | // Additional API found in the Core - unused? |
| 132 | func (handler *MockCoreServiceHandler) ReconcileChildDevices(context.Context, *common.ID) (*empty.Empty, error) { |
| 133 | return &empty.Empty{}, nil |
| 134 | } |
| 135 | |
| 136 | func (handler *MockCoreServiceHandler) GetChildDeviceWithProxyAddress(context.Context, *voltha.Device_ProxyAddress) (*voltha.Device, error) { |
| 137 | return &voltha.Device{}, nil |
| 138 | } |
| 139 | |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 140 | func (handler *MockCoreServiceHandler) GetPorts(context.Context, *ca.PortFilter) (*voltha.Ports, error) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 141 | return &voltha.Ports{}, nil |
| 142 | } |
| 143 | |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 144 | func (handler *MockCoreServiceHandler) ChildrenStateUpdate(context.Context, *ca.DeviceStateFilter) (*empty.Empty, error) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 145 | return &empty.Empty{}, nil |
| 146 | } |
| 147 | |
| 148 | func (handler *MockCoreServiceHandler) UpdateImageDownload(context.Context, *voltha.ImageDownload) (*empty.Empty, error) { |
| 149 | return &empty.Empty{}, nil |
| 150 | } |
| 151 | |
khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 152 | func (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() |
| 161 | loop: |
| 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 |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 189 | } |