blob: a6d81869f49496988671143984e7004549455152 [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 (
Rohan Agrawal31f21802020-06-12 05:38:46 +000020 "context"
Kent Hagerman2b216042020-04-03 18:28:56 -040021 "github.com/golang/protobuf/ptypes"
yasin sapli5458a1c2021-06-14 22:24:38 +000022 "github.com/opencord/voltha-lib-go/v5/pkg/log"
Maninderdfadc982020-10-28 14:04:33 +053023 "github.com/opencord/voltha-protos/v4/go/voltha"
Kent Hagerman2b216042020-04-03 18:28:56 -040024 "sync"
25 "time"
26)
27
28// agent represents adapter agent
29type agent struct {
30 adapter *voltha.Adapter
31 lock sync.RWMutex
32}
33
34func newAdapterAgent(adapter *voltha.Adapter) *agent {
35 return &agent{
36 adapter: adapter,
37 }
38}
39
Rohan Agrawal31f21802020-06-12 05:38:46 +000040func (aa *agent) getAdapter(ctx context.Context) *voltha.Adapter {
Kent Hagerman2b216042020-04-03 18:28:56 -040041 aa.lock.RLock()
42 defer aa.lock.RUnlock()
Rohan Agrawal31f21802020-06-12 05:38:46 +000043 logger.Debugw(ctx, "getAdapter", log.Fields{"adapter": aa.adapter})
Kent Hagerman2b216042020-04-03 18:28:56 -040044 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.
49func (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. Bainbridge5809b5b2020-08-27 00:07:41 +000051 aa.lock.Lock()
52 defer aa.lock.Unlock()
Kent Hagerman2b216042020-04-03 18:28:56 -040053 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 Hagerman2b216042020-04-03 18:28:56 -040059 aa.adapter.LastCommunication = timestamp
60 }
61}