blob: 68e562d88aeb55a3e16220871f1b4de611c1c00a [file] [log] [blame]
khenaidoof3333552021-12-15 16:52:31 -05001/*
Joey Armstrong89c812c2024-01-12 19:00:20 -05002 * Copyright 2022-2024 Open Networking Foundation (ONF) and the ONF Contributors
khenaidoof3333552021-12-15 16:52:31 -05003 *
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 Armstrong89c812c2024-01-12 19:00:20 -050017// Package core provides the utility for onu devices, flows and statistics
khenaidoof3333552021-12-15 16:52:31 -050018package core
19
20import (
21 "context"
22 "fmt"
23 "time"
24
khenaidoof3333552021-12-15 16:52:31 -050025 "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"
bseeniva71b1b662026-02-12 19:07:50 +053030 "google.golang.org/protobuf/types/known/emptypb"
khenaidoof3333552021-12-15 16:52:31 -050031)
32
Joey Armstrong89c812c2024-01-12 19:00:20 -050033// OpenONUACInterAdapter structure holds a reference to ONU adapter
khenaidoof3333552021-12-15 16:52:31 -050034type OpenONUACInterAdapter struct {
bseeniva71b1b662026-02-12 19:07:50 +053035 onu_inter_adapter_service.UnimplementedOnuInterAdapterServiceServer
khenaidoof3333552021-12-15 16:52:31 -050036 onuAdapter *OpenONUAC
37 exitChannel chan struct{}
38}
39
Joey Armstrong89c812c2024-01-12 19:00:20 -050040// NewOpenONUACAdapter returns a new instance of OpenONUACAdapter
khenaidoof3333552021-12-15 16:52:31 -050041func NewOpenONUACAdapter(ctx context.Context, onuAdapter *OpenONUAC) *OpenONUACInterAdapter {
42 return &OpenONUACInterAdapter{onuAdapter: onuAdapter, exitChannel: make(chan struct{})}
43}
44
Joey Armstrong89c812c2024-01-12 19:00:20 -050045// Start starts (logs) the adapter
khenaidoof3333552021-12-15 16:52:31 -050046func (oo *OpenONUACInterAdapter) Start(ctx context.Context) error {
47 logger.Info(ctx, "starting-openonu-inter-adapter")
48 return nil
49}
50
Joey Armstrong89c812c2024-01-12 19:00:20 -050051// OnuIndication redirects the request the the core ONU adapter handler
bseeniva71b1b662026-02-12 19:07:50 +053052func (oo *OpenONUACInterAdapter) OnuIndication(ctx context.Context, onuInd *ia.OnuIndicationMessage) (*emptypb.Empty, error) {
khenaidoof3333552021-12-15 16:52:31 -050053 return oo.onuAdapter.OnuIndication(ctx, onuInd)
54}
55
Joey Armstrong89c812c2024-01-12 19:00:20 -050056// OmciIndication redirects the request the the core ONU adapter handler
bseeniva71b1b662026-02-12 19:07:50 +053057func (oo *OpenONUACInterAdapter) OmciIndication(ctx context.Context, msg *ia.OmciMessage) (*emptypb.Empty, error) {
khenaidoof3333552021-12-15 16:52:31 -050058 return oo.onuAdapter.OmciIndication(ctx, msg)
59}
60
Joey Armstrong89c812c2024-01-12 19:00:20 -050061// DownloadTechProfile redirects the request the the core ONU adapter handler
bseeniva71b1b662026-02-12 19:07:50 +053062func (oo *OpenONUACInterAdapter) DownloadTechProfile(ctx context.Context, tProfile *ia.TechProfileDownloadMessage) (*emptypb.Empty, error) {
khenaidoof3333552021-12-15 16:52:31 -050063 return oo.onuAdapter.DownloadTechProfile(ctx, tProfile)
64}
65
Joey Armstrong89c812c2024-01-12 19:00:20 -050066// DeleteGemPort redirects the request the the core ONU adapter handler
bseeniva71b1b662026-02-12 19:07:50 +053067func (oo *OpenONUACInterAdapter) DeleteGemPort(ctx context.Context, gPort *ia.DeleteGemPortMessage) (*emptypb.Empty, error) {
khenaidoof3333552021-12-15 16:52:31 -050068 return oo.onuAdapter.DeleteGemPort(ctx, gPort)
69}
70
Joey Armstrong89c812c2024-01-12 19:00:20 -050071// DeleteTCont redirects the request the the core ONU adapter handler
bseeniva71b1b662026-02-12 19:07:50 +053072func (oo *OpenONUACInterAdapter) DeleteTCont(ctx context.Context, tConf *ia.DeleteTcontMessage) (*emptypb.Empty, error) {
khenaidoof3333552021-12-15 16:52:31 -050073 return oo.onuAdapter.DeleteTCont(ctx, tConf)
74}
75
Joey Armstrong89c812c2024-01-12 19:00:20 -050076// Stop terminates the session
khenaidoof3333552021-12-15 16:52:31 -050077func (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
85func (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
96loop:
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}