Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 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 | 25057da | 2021-12-08 14:40:45 -0500 | [diff] [blame^] | 28 | "github.com/opencord/voltha-protos/v5/go/common" |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 29 | "github.com/opencord/voltha-protos/v5/go/health" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 30 | "github.com/opencord/voltha-protos/v5/go/voltha" |
| 31 | "google.golang.org/grpc" |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | // agent represents adapter agent |
| 35 | type agent struct { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 36 | adapter *voltha.Adapter |
| 37 | lock sync.RWMutex |
| 38 | adapterAPIEndPoint string |
| 39 | vClient *vgrpc.Client |
| 40 | adapterLock sync.RWMutex |
| 41 | onAdapterRestart vgrpc.RestartedHandler |
| 42 | liveProbeInterval time.Duration |
khenaidoo | 25057da | 2021-12-08 14:40:45 -0500 | [diff] [blame^] | 43 | coreEndpoint string |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 44 | } |
| 45 | |
khenaidoo | 25057da | 2021-12-08 14:40:45 -0500 | [diff] [blame^] | 46 | func setAndTestAdapterServiceHandler(ctx context.Context, conn *grpc.ClientConn, clientConn *common.Connection) interface{} { |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 47 | svc := adapter_service.NewAdapterServiceClient(conn) |
khenaidoo | 25057da | 2021-12-08 14:40:45 -0500 | [diff] [blame^] | 48 | if h, err := svc.GetHealthStatus(ctx, clientConn); err != nil || h.State != health.HealthStatus_HEALTHY { |
| 49 | logger.Debugw(ctx, "remote-connection-not-ready", log.Fields{"error": err, "health": h, "requester": clientConn, "target": conn.Target()}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 50 | return nil |
| 51 | } |
| 52 | return svc |
| 53 | } |
| 54 | |
khenaidoo | 25057da | 2021-12-08 14:40:45 -0500 | [diff] [blame^] | 55 | 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] | 56 | return &agent{ |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 57 | adapter: adapter, |
| 58 | onAdapterRestart: onAdapterRestart, |
| 59 | adapterAPIEndPoint: adapter.Endpoint, |
| 60 | liveProbeInterval: liveProbeInterval, |
khenaidoo | 25057da | 2021-12-08 14:40:45 -0500 | [diff] [blame^] | 61 | coreEndpoint: coreEndpoint, |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| 65 | func (aa *agent) start(ctx context.Context) error { |
| 66 | // Establish grpc connection to Core |
| 67 | var err error |
khenaidoo | 25057da | 2021-12-08 14:40:45 -0500 | [diff] [blame^] | 68 | if aa.vClient, err = vgrpc.NewClient( |
| 69 | aa.coreEndpoint, |
| 70 | aa.adapterAPIEndPoint, |
| 71 | aa.onAdapterRestart); err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 72 | return err |
| 73 | } |
| 74 | |
| 75 | // Add a liveness communication update |
| 76 | aa.vClient.SubscribeForLiveness(aa.updateCommunicationTime) |
| 77 | |
| 78 | go aa.vClient.Start(ctx, setAndTestAdapterServiceHandler) |
| 79 | return nil |
| 80 | } |
| 81 | |
| 82 | func (aa *agent) stop(ctx context.Context) { |
| 83 | // Close the client |
| 84 | if aa.vClient != nil { |
| 85 | aa.vClient.Stop(ctx) |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 89 | func (aa *agent) getAdapter(ctx context.Context) *voltha.Adapter { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 90 | aa.adapterLock.RLock() |
| 91 | defer aa.adapterLock.RUnlock() |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 92 | return aa.adapter |
| 93 | } |
| 94 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 95 | func (aa *agent) getClient() (adapter_service.AdapterServiceClient, error) { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 96 | client, err := aa.vClient.GetClient() |
| 97 | if err != nil { |
| 98 | return nil, err |
| 99 | } |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 100 | c, ok := client.(adapter_service.AdapterServiceClient) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 101 | if ok { |
| 102 | return c, nil |
| 103 | } |
| 104 | return nil, errors.New("invalid client returned") |
| 105 | } |
| 106 | |
| 107 | func (aa *agent) resetConnection(ctx context.Context) { |
| 108 | if aa.vClient != nil { |
| 109 | aa.vClient.Reset(ctx) |
| 110 | } |
| 111 | } |
| 112 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 113 | // updateCommunicationTime updates the message to the specified time. |
| 114 | // No attempt is made to save the time to the db, so only recent times are guaranteed to be accurate. |
| 115 | func (aa *agent) updateCommunicationTime(new time.Time) { |
| 116 | // 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] | 117 | aa.lock.Lock() |
| 118 | defer aa.lock.Unlock() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 119 | timestamp := time.Unix(aa.adapter.LastCommunication, 0) |
| 120 | if !new.After(time.Now()) && new.After(timestamp) { |
| 121 | timestamp = new |
| 122 | aa.adapter.LastCommunication = timestamp.Unix() |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 123 | } |
| 124 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 125 | |
| 126 | func (aa *agent) IsConnectionUp() bool { |
| 127 | _, err := aa.getClient() |
| 128 | return err == nil |
| 129 | } |