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" |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 21 | "github.com/golang/protobuf/ptypes" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame] | 22 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 23 | "github.com/opencord/voltha-protos/v4/go/voltha" |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 24 | "sync" |
| 25 | "time" |
| 26 | ) |
| 27 | |
| 28 | // agent represents adapter agent |
| 29 | type agent struct { |
| 30 | adapter *voltha.Adapter |
| 31 | lock sync.RWMutex |
| 32 | } |
| 33 | |
| 34 | func newAdapterAgent(adapter *voltha.Adapter) *agent { |
| 35 | return &agent{ |
| 36 | adapter: adapter, |
| 37 | } |
| 38 | } |
| 39 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 40 | func (aa *agent) getAdapter(ctx context.Context) *voltha.Adapter { |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 41 | aa.lock.RLock() |
| 42 | defer aa.lock.RUnlock() |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 43 | logger.Debugw(ctx, "getAdapter", log.Fields{"adapter": aa.adapter}) |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 44 | return aa.adapter |
| 45 | } |
| 46 | |
| 47 | // updateCommunicationTime updates the message to the specified time. |
| 48 | // No attempt is made to save the time to the db, so only recent times are guaranteed to be accurate. |
| 49 | func (aa *agent) updateCommunicationTime(new time.Time) { |
| 50 | // 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] | 51 | aa.lock.Lock() |
| 52 | defer aa.lock.Unlock() |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 53 | if last, err := ptypes.Timestamp(aa.adapter.LastCommunication); !new.After(time.Now()) && (err != nil || new.After(last)) { |
| 54 | timestamp, err := ptypes.TimestampProto(new) |
| 55 | if err != nil { |
| 56 | return // if the new time cannot be encoded, just ignore it |
| 57 | } |
| 58 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 59 | aa.adapter.LastCommunication = timestamp |
| 60 | } |
| 61 | } |