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