| khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 1 | /* |
| Joey Armstrong | 11f5a57 | 2024-01-12 19:11:32 -0500 | [diff] [blame] | 2 | * Copyright 2022-2024 Open Networking Foundation (ONF) and the ONF Contributors |
| khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 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 | */ |
| 16 | |
| Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 17 | // Package core provides the utility for olt devices, flows and statistics |
| khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 18 | package core |
| 19 | |
| 20 | import ( |
| 21 | "context" |
| 22 | "fmt" |
| 23 | "time" |
| 24 | |
| khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 26 | "github.com/opencord/voltha-protos/v5/go/common" |
| 27 | "github.com/opencord/voltha-protos/v5/go/health" |
| 28 | ia "github.com/opencord/voltha-protos/v5/go/inter_adapter" |
| 29 | oltia "github.com/opencord/voltha-protos/v5/go/olt_inter_adapter_service" |
| bseeniva | 0b9cbcb | 2026-02-12 19:11:11 +0530 | [diff] [blame] | 30 | "google.golang.org/protobuf/types/known/emptypb" |
| khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 31 | ) |
| 32 | |
| Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 33 | // OpenOLTInterAdapter structure holds a reference to the oltAdapter |
| khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 34 | type OpenOLTInterAdapter struct { |
| bseeniva | 0b9cbcb | 2026-02-12 19:11:11 +0530 | [diff] [blame] | 35 | oltia.UnimplementedOltInterAdapterServiceServer |
| khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 36 | oltAdapter *OpenOLT |
| 37 | exitChannel chan struct{} |
| 38 | } |
| 39 | |
| Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 40 | // NewOpenOLTInterAdapter returns a new instance of OpenOLTInterAdapter |
| khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 41 | func NewOpenOLTInterAdapter(oltAdapter *OpenOLT) *OpenOLTInterAdapter { |
| 42 | return &OpenOLTInterAdapter{oltAdapter: oltAdapter, exitChannel: make(chan struct{})} |
| 43 | } |
| 44 | |
| Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 45 | // Start starts (logs) the device manager |
| khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 46 | func (oo *OpenOLTInterAdapter) Start(ctx context.Context) error { |
| 47 | return nil |
| 48 | } |
| 49 | |
| Joey Armstrong | 3f0e242 | 2023-07-05 18:25:41 -0400 | [diff] [blame] | 50 | // Stop terminates the session |
| khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 51 | func (oo *OpenOLTInterAdapter) Stop(ctx context.Context) error { |
| 52 | close(oo.exitChannel) |
| 53 | return nil |
| 54 | } |
| 55 | |
| 56 | // ProxyOmciRequest proxies an OMCI request from the child adapter |
| bseeniva | 0b9cbcb | 2026-02-12 19:11:11 +0530 | [diff] [blame] | 57 | func (oo *OpenOLTInterAdapter) ProxyOmciRequest(ctx context.Context, request *ia.OmciMessage) (*emptypb.Empty, error) { |
| khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 58 | return oo.oltAdapter.ProxyOmciRequest(ctx, request) |
| 59 | } |
| 60 | |
| 61 | // ProxyOmciRequests proxies an OMCI request from the child adapter |
| bseeniva | 0b9cbcb | 2026-02-12 19:11:11 +0530 | [diff] [blame] | 62 | func (oo *OpenOLTInterAdapter) ProxyOmciRequests(ctx context.Context, request *ia.OmciMessages) (*emptypb.Empty, error) { |
| khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 63 | return oo.oltAdapter.ProxyOmciRequests(ctx, request) |
| 64 | } |
| 65 | |
| 66 | // GetTechProfileInstance returns an instance of a tech profile |
| 67 | func (oo *OpenOLTInterAdapter) GetTechProfileInstance(ctx context.Context, request *ia.TechProfileInstanceRequestMessage) (*ia.TechProfileDownloadMessage, error) { |
| 68 | return oo.oltAdapter.GetTechProfileInstance(ctx, request) |
| 69 | } |
| 70 | |
| 71 | // GetHealthStatus is used by a OltInterAdapterService client to detect a connection |
| 72 | // lost with the gRPC server hosting the OltInterAdapterService service |
| 73 | func (oo *OpenOLTInterAdapter) GetHealthStatus(stream oltia.OltInterAdapterService_GetHealthStatusServer) error { |
| 74 | ctx := context.Background() |
| 75 | logger.Debugw(ctx, "receive-stream-connection", log.Fields{"stream": stream}) |
| 76 | |
| 77 | if stream == nil { |
| 78 | return fmt.Errorf("conn-is-nil %v", stream) |
| 79 | } |
| 80 | initialRequestTime := time.Now() |
| 81 | var remoteClient *common.Connection |
| 82 | var tempClient *common.Connection |
| 83 | var err error |
| 84 | loop: |
| 85 | for { |
| 86 | tempClient, err = stream.Recv() |
| 87 | if err != nil { |
| 88 | logger.Warnw(ctx, "received-stream-error", log.Fields{"remote-client": remoteClient, "error": err}) |
| 89 | break loop |
| 90 | } |
| 91 | err = stream.Send(&health.HealthStatus{State: health.HealthStatus_HEALTHY}) |
| 92 | if err != nil { |
| 93 | logger.Warnw(ctx, "sending-stream-error", log.Fields{"remote-client": remoteClient, "error": err}) |
| 94 | break loop |
| 95 | } |
| 96 | |
| 97 | remoteClient = tempClient |
| 98 | logger.Debugw(ctx, "received-keep-alive", log.Fields{"remote-client": remoteClient}) |
| 99 | |
| 100 | select { |
| 101 | case <-stream.Context().Done(): |
| 102 | logger.Infow(ctx, "stream-keep-alive-context-done", log.Fields{"remote-client": remoteClient, "error": stream.Context().Err()}) |
| 103 | break loop |
| 104 | case <-oo.exitChannel: |
| 105 | logger.Warnw(ctx, "received-stop", log.Fields{"remote-client": remoteClient, "initial-conn-time": initialRequestTime}) |
| 106 | break loop |
| 107 | default: |
| 108 | } |
| 109 | } |
| 110 | logger.Errorw(ctx, "connection-down", log.Fields{"remote-client": remoteClient, "error": err, "initial-conn-time": initialRequestTime}) |
| 111 | return err |
| 112 | } |