blob: d27f60097f2f1e0b1c170d537af2ff1a01f05955 [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
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 Armstrong3f0e2422023-07-05 18:25:41 -040033// OpenOLTInterAdapter structure holds a reference to the oltAdapter
khenaidooefff76e2021-12-15 16:51:30 -050034type OpenOLTInterAdapter struct {
35 oltAdapter *OpenOLT
36 exitChannel chan struct{}
37}
38
Joey Armstrong3f0e2422023-07-05 18:25:41 -040039// NewOpenOLTInterAdapter returns a new instance of OpenOLTInterAdapter
khenaidooefff76e2021-12-15 16:51:30 -050040func NewOpenOLTInterAdapter(oltAdapter *OpenOLT) *OpenOLTInterAdapter {
41 return &OpenOLTInterAdapter{oltAdapter: oltAdapter, exitChannel: make(chan struct{})}
42}
43
Joey Armstrong3f0e2422023-07-05 18:25:41 -040044// Start starts (logs) the device manager
khenaidooefff76e2021-12-15 16:51:30 -050045func (oo *OpenOLTInterAdapter) Start(ctx context.Context) error {
46 return nil
47}
48
Joey Armstrong3f0e2422023-07-05 18:25:41 -040049// Stop terminates the session
khenaidooefff76e2021-12-15 16:51:30 -050050func (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
56func (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
61func (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
66func (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
72func (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
83loop:
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}