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