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