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