Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1 | /* |
Joey Armstrong | 5f51f2e | 2023-01-17 17:06:26 -0500 | [diff] [blame^] | 2 | * Copyright 2020-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 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 adapter |
| 18 | |
| 19 | import ( |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 20 | "context" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 21 | "errors" |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 22 | "sync" |
| 23 | "time" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 24 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 25 | vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc" |
| 26 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 27 | "github.com/opencord/voltha-protos/v5/go/adapter_service" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 28 | "github.com/opencord/voltha-protos/v5/go/voltha" |
| 29 | "google.golang.org/grpc" |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | // agent represents adapter agent |
| 33 | type agent struct { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 34 | 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 |
khenaidoo | 25057da | 2021-12-08 14:40:45 -0500 | [diff] [blame] | 41 | coreEndpoint string |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 42 | } |
| 43 | |
khenaidoo | a46458b | 2021-12-15 16:50:44 -0500 | [diff] [blame] | 44 | func getAdapterServiceClientHandler(ctx context.Context, conn *grpc.ClientConn) interface{} { |
| 45 | if conn == nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 46 | return nil |
| 47 | } |
khenaidoo | a46458b | 2021-12-15 16:50:44 -0500 | [diff] [blame] | 48 | return adapter_service.NewAdapterServiceClient(conn) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 49 | } |
| 50 | |
khenaidoo | 25057da | 2021-12-08 14:40:45 -0500 | [diff] [blame] | 51 | func newAdapterAgent(coreEndpoint string, adapter *voltha.Adapter, onAdapterRestart vgrpc.RestartedHandler, liveProbeInterval time.Duration) *agent { |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 52 | return &agent{ |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 53 | adapter: adapter, |
| 54 | onAdapterRestart: onAdapterRestart, |
| 55 | adapterAPIEndPoint: adapter.Endpoint, |
| 56 | liveProbeInterval: liveProbeInterval, |
khenaidoo | 25057da | 2021-12-08 14:40:45 -0500 | [diff] [blame] | 57 | coreEndpoint: coreEndpoint, |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | func (aa *agent) start(ctx context.Context) error { |
| 62 | // Establish grpc connection to Core |
| 63 | var err error |
khenaidoo | 25057da | 2021-12-08 14:40:45 -0500 | [diff] [blame] | 64 | if aa.vClient, err = vgrpc.NewClient( |
| 65 | aa.coreEndpoint, |
| 66 | aa.adapterAPIEndPoint, |
khenaidoo | a46458b | 2021-12-15 16:50:44 -0500 | [diff] [blame] | 67 | "adapter_service.AdapterService", |
khenaidoo | 25057da | 2021-12-08 14:40:45 -0500 | [diff] [blame] | 68 | aa.onAdapterRestart); err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 69 | return err |
| 70 | } |
| 71 | |
| 72 | // Add a liveness communication update |
| 73 | aa.vClient.SubscribeForLiveness(aa.updateCommunicationTime) |
| 74 | |
khenaidoo | a46458b | 2021-12-15 16:50:44 -0500 | [diff] [blame] | 75 | go aa.vClient.Start(ctx, getAdapterServiceClientHandler) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 76 | return nil |
| 77 | } |
| 78 | |
| 79 | func (aa *agent) stop(ctx context.Context) { |
| 80 | // Close the client |
khenaidoo | a46458b | 2021-12-15 16:50:44 -0500 | [diff] [blame] | 81 | logger.Infow(ctx, "stopping-adapter-agent", log.Fields{"adapter": aa.adapter}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 82 | if aa.vClient != nil { |
| 83 | aa.vClient.Stop(ctx) |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 87 | func (aa *agent) getAdapter(ctx context.Context) *voltha.Adapter { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 88 | aa.adapterLock.RLock() |
| 89 | defer aa.adapterLock.RUnlock() |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 90 | return aa.adapter |
| 91 | } |
| 92 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 93 | func (aa *agent) getClient() (adapter_service.AdapterServiceClient, error) { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 94 | client, err := aa.vClient.GetClient() |
| 95 | if err != nil { |
| 96 | return nil, err |
| 97 | } |
Girish Gowdra | 11ddb23 | 2022-05-26 12:19:59 -0700 | [diff] [blame] | 98 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 99 | c, ok := client.(adapter_service.AdapterServiceClient) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 100 | if ok { |
| 101 | return c, nil |
| 102 | } |
| 103 | return nil, errors.New("invalid client returned") |
| 104 | } |
| 105 | |
| 106 | func (aa *agent) resetConnection(ctx context.Context) { |
| 107 | if aa.vClient != nil { |
| 108 | aa.vClient.Reset(ctx) |
| 109 | } |
| 110 | } |
| 111 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 112 | // 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. |
| 114 | func (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. Bainbridge | 5809b5b | 2020-08-27 00:07:41 +0000 | [diff] [blame] | 116 | aa.lock.Lock() |
| 117 | defer aa.lock.Unlock() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 118 | 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 Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 122 | } |
| 123 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 124 | |
| 125 | func (aa *agent) IsConnectionUp() bool { |
| 126 | _, err := aa.getClient() |
| 127 | return err == nil |
| 128 | } |