| khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 1 | /* |
| Joey Armstrong | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 2 | * Copyright 2022-2024 Open Networking Foundation (ONF) and the ONF Contributors |
| khenaidoo | f333355 | 2021-12-15 16:52:31 -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 | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 17 | // Package core provides the utility for onu devices, flows and statistics |
| khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 18 | package core |
| 19 | |
| 20 | import ( |
| 21 | "context" |
| 22 | "fmt" |
| 23 | "time" |
| 24 | |
| khenaidoo | f333355 | 2021-12-15 16:52:31 -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 | "github.com/opencord/voltha-protos/v5/go/onu_inter_adapter_service" |
| bseeniva | 71b1b66 | 2026-02-12 19:07:50 +0530 | [diff] [blame^] | 30 | "google.golang.org/protobuf/types/known/emptypb" |
| khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 31 | ) |
| 32 | |
| Joey Armstrong | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 33 | // OpenONUACInterAdapter structure holds a reference to ONU adapter |
| khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 34 | type OpenONUACInterAdapter struct { |
| bseeniva | 71b1b66 | 2026-02-12 19:07:50 +0530 | [diff] [blame^] | 35 | onu_inter_adapter_service.UnimplementedOnuInterAdapterServiceServer |
| khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 36 | onuAdapter *OpenONUAC |
| 37 | exitChannel chan struct{} |
| 38 | } |
| 39 | |
| Joey Armstrong | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 40 | // NewOpenONUACAdapter returns a new instance of OpenONUACAdapter |
| khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 41 | func NewOpenONUACAdapter(ctx context.Context, onuAdapter *OpenONUAC) *OpenONUACInterAdapter { |
| 42 | return &OpenONUACInterAdapter{onuAdapter: onuAdapter, exitChannel: make(chan struct{})} |
| 43 | } |
| 44 | |
| Joey Armstrong | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 45 | // Start starts (logs) the adapter |
| khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 46 | func (oo *OpenONUACInterAdapter) Start(ctx context.Context) error { |
| 47 | logger.Info(ctx, "starting-openonu-inter-adapter") |
| 48 | return nil |
| 49 | } |
| 50 | |
| Joey Armstrong | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 51 | // OnuIndication redirects the request the the core ONU adapter handler |
| bseeniva | 71b1b66 | 2026-02-12 19:07:50 +0530 | [diff] [blame^] | 52 | func (oo *OpenONUACInterAdapter) OnuIndication(ctx context.Context, onuInd *ia.OnuIndicationMessage) (*emptypb.Empty, error) { |
| khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 53 | return oo.onuAdapter.OnuIndication(ctx, onuInd) |
| 54 | } |
| 55 | |
| Joey Armstrong | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 56 | // OmciIndication redirects the request the the core ONU adapter handler |
| bseeniva | 71b1b66 | 2026-02-12 19:07:50 +0530 | [diff] [blame^] | 57 | func (oo *OpenONUACInterAdapter) OmciIndication(ctx context.Context, msg *ia.OmciMessage) (*emptypb.Empty, error) { |
| khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 58 | return oo.onuAdapter.OmciIndication(ctx, msg) |
| 59 | } |
| 60 | |
| Joey Armstrong | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 61 | // DownloadTechProfile redirects the request the the core ONU adapter handler |
| bseeniva | 71b1b66 | 2026-02-12 19:07:50 +0530 | [diff] [blame^] | 62 | func (oo *OpenONUACInterAdapter) DownloadTechProfile(ctx context.Context, tProfile *ia.TechProfileDownloadMessage) (*emptypb.Empty, error) { |
| khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 63 | return oo.onuAdapter.DownloadTechProfile(ctx, tProfile) |
| 64 | } |
| 65 | |
| Joey Armstrong | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 66 | // DeleteGemPort redirects the request the the core ONU adapter handler |
| bseeniva | 71b1b66 | 2026-02-12 19:07:50 +0530 | [diff] [blame^] | 67 | func (oo *OpenONUACInterAdapter) DeleteGemPort(ctx context.Context, gPort *ia.DeleteGemPortMessage) (*emptypb.Empty, error) { |
| khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 68 | return oo.onuAdapter.DeleteGemPort(ctx, gPort) |
| 69 | } |
| 70 | |
| Joey Armstrong | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 71 | // DeleteTCont redirects the request the the core ONU adapter handler |
| bseeniva | 71b1b66 | 2026-02-12 19:07:50 +0530 | [diff] [blame^] | 72 | func (oo *OpenONUACInterAdapter) DeleteTCont(ctx context.Context, tConf *ia.DeleteTcontMessage) (*emptypb.Empty, error) { |
| khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 73 | return oo.onuAdapter.DeleteTCont(ctx, tConf) |
| 74 | } |
| 75 | |
| Joey Armstrong | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 76 | // Stop terminates the session |
| khenaidoo | f333355 | 2021-12-15 16:52:31 -0500 | [diff] [blame] | 77 | func (oo *OpenONUACInterAdapter) Stop(ctx context.Context) error { |
| 78 | close(oo.exitChannel) |
| 79 | logger.Info(ctx, "openonu-inter-adapter-stopped") |
| 80 | return nil |
| 81 | } |
| 82 | |
| 83 | // GetHealthStatus is used by a OnuInterAdapterService client to detect a connection |
| 84 | // lost with the gRPC server hosting the OnuInterAdapterService service |
| 85 | func (oo *OpenONUACInterAdapter) GetHealthStatus(stream onu_inter_adapter_service.OnuInterAdapterService_GetHealthStatusServer) error { |
| 86 | ctx := context.Background() |
| 87 | logger.Debugw(ctx, "receive-stream-connection", log.Fields{"stream": stream}) |
| 88 | |
| 89 | if stream == nil { |
| 90 | return fmt.Errorf("conn-is-nil %v", stream) |
| 91 | } |
| 92 | initialRequestTime := time.Now() |
| 93 | var remoteClient *common.Connection |
| 94 | var tempClient *common.Connection |
| 95 | var err error |
| 96 | loop: |
| 97 | for { |
| 98 | tempClient, err = stream.Recv() |
| 99 | if err != nil { |
| 100 | logger.Warnw(ctx, "received-stream-error", log.Fields{"remote-client": remoteClient, "error": err}) |
| 101 | break loop |
| 102 | } |
| 103 | // Send a response back |
| 104 | err = stream.Send(&health.HealthStatus{State: health.HealthStatus_HEALTHY}) |
| 105 | if err != nil { |
| 106 | logger.Warnw(ctx, "sending-stream-error", log.Fields{"remote-client": remoteClient, "error": err}) |
| 107 | break loop |
| 108 | } |
| 109 | |
| 110 | remoteClient = tempClient |
| 111 | logger.Debugw(ctx, "received-keep-alive", log.Fields{"remote-client": remoteClient}) |
| 112 | oo.onuAdapter.updateReachabilityFromRemote(context.Background(), remoteClient) |
| 113 | |
| 114 | select { |
| 115 | case <-stream.Context().Done(): |
| 116 | logger.Infow(ctx, "stream-keep-alive-context-done", log.Fields{"remote-client": remoteClient, "error": stream.Context().Err()}) |
| 117 | break loop |
| 118 | case <-oo.exitChannel: |
| 119 | logger.Warnw(ctx, "received-stop", log.Fields{"remote-client": remoteClient, "initial-conn-time": initialRequestTime}) |
| 120 | break loop |
| 121 | default: |
| 122 | } |
| 123 | } |
| 124 | logger.Errorw(ctx, "connection-down", log.Fields{"remote-client": remoteClient, "error": err, "initial-conn-time": initialRequestTime}) |
| 125 | return err |
| 126 | } |