blob: 0a2b632d232d434400a7ec16e4153a68bfa90259 [file] [log] [blame]
Kent Hagerman2b216042020-04-03 18:28:56 -04001/*
2 * Copyright 2020-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
17package adapter
18
19import (
Rohan Agrawal31f21802020-06-12 05:38:46 +000020 "context"
khenaidood948f772021-08-11 17:49:24 -040021 "errors"
Kent Hagerman2b216042020-04-03 18:28:56 -040022 "sync"
23 "time"
khenaidood948f772021-08-11 17:49:24 -040024
khenaidood948f772021-08-11 17:49:24 -040025 vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc"
26 "github.com/opencord/voltha-lib-go/v7/pkg/log"
khenaidoo9beaaf12021-10-19 17:32:01 -040027 "github.com/opencord/voltha-protos/v5/go/adapter_service"
khenaidood948f772021-08-11 17:49:24 -040028 "github.com/opencord/voltha-protos/v5/go/voltha"
29 "google.golang.org/grpc"
Kent Hagerman2b216042020-04-03 18:28:56 -040030)
31
32// agent represents adapter agent
33type agent struct {
khenaidood948f772021-08-11 17:49:24 -040034 adapter *voltha.Adapter
35 lock sync.RWMutex
36 adapterAPIEndPoint string
37 vClient *vgrpc.Client
38 adapterLock sync.RWMutex
39 onAdapterRestart vgrpc.RestartedHandler
40 liveProbeInterval time.Duration
khenaidoo25057da2021-12-08 14:40:45 -050041 coreEndpoint string
Kent Hagerman2b216042020-04-03 18:28:56 -040042}
43
khenaidooa46458b2021-12-15 16:50:44 -050044func getAdapterServiceClientHandler(ctx context.Context, conn *grpc.ClientConn) interface{} {
45 if conn == nil {
khenaidood948f772021-08-11 17:49:24 -040046 return nil
47 }
khenaidooa46458b2021-12-15 16:50:44 -050048 return adapter_service.NewAdapterServiceClient(conn)
khenaidood948f772021-08-11 17:49:24 -040049}
50
khenaidoo25057da2021-12-08 14:40:45 -050051func newAdapterAgent(coreEndpoint string, adapter *voltha.Adapter, onAdapterRestart vgrpc.RestartedHandler, liveProbeInterval time.Duration) *agent {
Kent Hagerman2b216042020-04-03 18:28:56 -040052 return &agent{
khenaidood948f772021-08-11 17:49:24 -040053 adapter: adapter,
54 onAdapterRestart: onAdapterRestart,
55 adapterAPIEndPoint: adapter.Endpoint,
56 liveProbeInterval: liveProbeInterval,
khenaidoo25057da2021-12-08 14:40:45 -050057 coreEndpoint: coreEndpoint,
khenaidood948f772021-08-11 17:49:24 -040058 }
59}
60
61func (aa *agent) start(ctx context.Context) error {
62 // Establish grpc connection to Core
63 var err error
khenaidoo25057da2021-12-08 14:40:45 -050064 if aa.vClient, err = vgrpc.NewClient(
65 aa.coreEndpoint,
66 aa.adapterAPIEndPoint,
khenaidooa46458b2021-12-15 16:50:44 -050067 "adapter_service.AdapterService",
khenaidoo25057da2021-12-08 14:40:45 -050068 aa.onAdapterRestart); err != nil {
khenaidood948f772021-08-11 17:49:24 -040069 return err
70 }
71
72 // Add a liveness communication update
73 aa.vClient.SubscribeForLiveness(aa.updateCommunicationTime)
74
khenaidooa46458b2021-12-15 16:50:44 -050075 go aa.vClient.Start(ctx, getAdapterServiceClientHandler)
khenaidood948f772021-08-11 17:49:24 -040076 return nil
77}
78
79func (aa *agent) stop(ctx context.Context) {
80 // Close the client
khenaidooa46458b2021-12-15 16:50:44 -050081 logger.Infow(ctx, "stopping-adapter-agent", log.Fields{"adapter": aa.adapter})
khenaidood948f772021-08-11 17:49:24 -040082 if aa.vClient != nil {
83 aa.vClient.Stop(ctx)
Kent Hagerman2b216042020-04-03 18:28:56 -040084 }
85}
86
Rohan Agrawal31f21802020-06-12 05:38:46 +000087func (aa *agent) getAdapter(ctx context.Context) *voltha.Adapter {
khenaidood948f772021-08-11 17:49:24 -040088 aa.adapterLock.RLock()
89 defer aa.adapterLock.RUnlock()
Kent Hagerman2b216042020-04-03 18:28:56 -040090 return aa.adapter
91}
92
khenaidoo9beaaf12021-10-19 17:32:01 -040093func (aa *agent) getClient() (adapter_service.AdapterServiceClient, error) {
khenaidood948f772021-08-11 17:49:24 -040094 client, err := aa.vClient.GetClient()
95 if err != nil {
96 return nil, err
97 }
Girish Gowdra11ddb232022-05-26 12:19:59 -070098
khenaidoo9beaaf12021-10-19 17:32:01 -040099 c, ok := client.(adapter_service.AdapterServiceClient)
khenaidood948f772021-08-11 17:49:24 -0400100 if ok {
101 return c, nil
102 }
103 return nil, errors.New("invalid client returned")
104}
105
106func (aa *agent) resetConnection(ctx context.Context) {
107 if aa.vClient != nil {
108 aa.vClient.Reset(ctx)
109 }
110}
111
Kent Hagerman2b216042020-04-03 18:28:56 -0400112// updateCommunicationTime updates the message to the specified time.
113// No attempt is made to save the time to the db, so only recent times are guaranteed to be accurate.
114func (aa *agent) updateCommunicationTime(new time.Time) {
115 // only update if new time is not in the future, and either the old time is invalid or new time > old time
David K. Bainbridge5809b5b2020-08-27 00:07:41 +0000116 aa.lock.Lock()
117 defer aa.lock.Unlock()
khenaidood948f772021-08-11 17:49:24 -0400118 timestamp := time.Unix(aa.adapter.LastCommunication, 0)
119 if !new.After(time.Now()) && new.After(timestamp) {
120 timestamp = new
121 aa.adapter.LastCommunication = timestamp.Unix()
Kent Hagerman2b216042020-04-03 18:28:56 -0400122 }
123}
khenaidood948f772021-08-11 17:49:24 -0400124
125func (aa *agent) IsConnectionUp() bool {
126 _, err := aa.getClient()
127 return err == nil
128}