blob: 2143a84d043509d17c5e4476ce7f906ad9c7c5ec [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 (
20 "github.com/golang/protobuf/ptypes"
21 "github.com/opencord/voltha-lib-go/v3/pkg/log"
22 "github.com/opencord/voltha-protos/v3/go/voltha"
23 "sync"
24 "time"
25)
26
27// agent represents adapter agent
28type agent struct {
29 adapter *voltha.Adapter
30 lock sync.RWMutex
31}
32
33func newAdapterAgent(adapter *voltha.Adapter) *agent {
34 return &agent{
35 adapter: adapter,
36 }
37}
38
39func (aa *agent) getAdapter() *voltha.Adapter {
40 aa.lock.RLock()
41 defer aa.lock.RUnlock()
42 logger.Debugw("getAdapter", log.Fields{"adapter": aa.adapter})
43 return aa.adapter
44}
45
46// updateCommunicationTime updates the message to the specified time.
47// No attempt is made to save the time to the db, so only recent times are guaranteed to be accurate.
48func (aa *agent) updateCommunicationTime(new time.Time) {
49 // only update if new time is not in the future, and either the old time is invalid or new time > old time
50 if last, err := ptypes.Timestamp(aa.adapter.LastCommunication); !new.After(time.Now()) && (err != nil || new.After(last)) {
51 timestamp, err := ptypes.TimestampProto(new)
52 if err != nil {
53 return // if the new time cannot be encoded, just ignore it
54 }
55
56 aa.lock.Lock()
57 defer aa.lock.Unlock()
58 aa.adapter.LastCommunication = timestamp
59 }
60}