blob: 84c6dd263c9c8cc334012b1bb8810acf41800549 [file] [log] [blame]
khenaidooefff76e2021-12-15 16:51:30 -05001/*
Joey Armstrong11f5a572024-01-12 19:11:32 -05002 * Copyright 2022-2024 Open Networking Foundation (ONF) and the ONF Contributors
khenaidooefff76e2021-12-15 16:51:30 -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 Armstrong3f0e2422023-07-05 18:25:41 -040017// Package core provides the utility for olt devices, flows and statistics
khenaidooefff76e2021-12-15 16:51:30 -050018package core
19
20import (
21 "context"
22 "fmt"
23 "time"
24
khenaidooefff76e2021-12-15 16:51:30 -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 oltia "github.com/opencord/voltha-protos/v5/go/olt_inter_adapter_service"
bseeniva0b9cbcb2026-02-12 19:11:11 +053030 "google.golang.org/protobuf/types/known/emptypb"
khenaidooefff76e2021-12-15 16:51:30 -050031)
32
Joey Armstrong3f0e2422023-07-05 18:25:41 -040033// OpenOLTInterAdapter structure holds a reference to the oltAdapter
khenaidooefff76e2021-12-15 16:51:30 -050034type OpenOLTInterAdapter struct {
bseeniva0b9cbcb2026-02-12 19:11:11 +053035 oltia.UnimplementedOltInterAdapterServiceServer
khenaidooefff76e2021-12-15 16:51:30 -050036 oltAdapter *OpenOLT
37 exitChannel chan struct{}
38}
39
Joey Armstrong3f0e2422023-07-05 18:25:41 -040040// NewOpenOLTInterAdapter returns a new instance of OpenOLTInterAdapter
khenaidooefff76e2021-12-15 16:51:30 -050041func NewOpenOLTInterAdapter(oltAdapter *OpenOLT) *OpenOLTInterAdapter {
42 return &OpenOLTInterAdapter{oltAdapter: oltAdapter, exitChannel: make(chan struct{})}
43}
44
Joey Armstrong3f0e2422023-07-05 18:25:41 -040045// Start starts (logs) the device manager
khenaidooefff76e2021-12-15 16:51:30 -050046func (oo *OpenOLTInterAdapter) Start(ctx context.Context) error {
47 return nil
48}
49
Joey Armstrong3f0e2422023-07-05 18:25:41 -040050// Stop terminates the session
khenaidooefff76e2021-12-15 16:51:30 -050051func (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
bseeniva0b9cbcb2026-02-12 19:11:11 +053057func (oo *OpenOLTInterAdapter) ProxyOmciRequest(ctx context.Context, request *ia.OmciMessage) (*emptypb.Empty, error) {
khenaidooefff76e2021-12-15 16:51:30 -050058 return oo.oltAdapter.ProxyOmciRequest(ctx, request)
59}
60
61// ProxyOmciRequests proxies an OMCI request from the child adapter
bseeniva0b9cbcb2026-02-12 19:11:11 +053062func (oo *OpenOLTInterAdapter) ProxyOmciRequests(ctx context.Context, request *ia.OmciMessages) (*emptypb.Empty, error) {
khenaidooefff76e2021-12-15 16:51:30 -050063 return oo.oltAdapter.ProxyOmciRequests(ctx, request)
64}
65
66// GetTechProfileInstance returns an instance of a tech profile
67func (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
73func (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
84loop:
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}