blob: bfec875fa211400e0341f6b9d9ccd9afc5bbb121 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
2 * Copyright 2019-present Ciena Corporation
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 */
16package model
17
18import (
Kent Hagermane00f4f62020-01-22 13:53:10 -050019 "github.com/golang/protobuf/ptypes"
20 "github.com/golang/protobuf/ptypes/timestamp"
Zack Williamse940c7a2019-08-21 14:25:39 -070021 "github.com/jhump/protoreflect/dynamic"
Kent Hagermane00f4f62020-01-22 13:53:10 -050022 "time"
Zack Williamse940c7a2019-08-21 14:25:39 -070023)
24
25type Adapter struct {
Kent Hagermane00f4f62020-01-22 13:53:10 -050026 Id string
27 Vendor string
28 Version string
29 LogLevel string
30 LastCommunication string
31 SinceLastCommunication string
Zack Williamse940c7a2019-08-21 14:25:39 -070032}
33
34func (adapter *Adapter) PopulateFrom(val *dynamic.Message) {
35 adapter.Id = val.GetFieldByName("id").(string)
36 adapter.Vendor = val.GetFieldByName("vendor").(string)
37 adapter.Version = val.GetFieldByName("version").(string)
Kent Hagermane00f4f62020-01-22 13:53:10 -050038
39 if lastCommunication, err := val.TryGetFieldByName("last_communication"); err != nil {
40 adapter.LastCommunication = "UNKNOWN"
41 adapter.SinceLastCommunication = "UNKNOWN"
42 } else {
43 if lastCommunication, err := ptypes.Timestamp(lastCommunication.(*timestamp.Timestamp)); err != nil {
44 adapter.LastCommunication = "NEVER"
45 adapter.SinceLastCommunication = "NEVER"
46 } else {
47 adapter.LastCommunication = lastCommunication.Truncate(time.Second).Format(time.RFC3339)
48 adapter.SinceLastCommunication = time.Now().Sub(lastCommunication).Truncate(time.Second).String()
49 }
50 }
51
Zack Williamse940c7a2019-08-21 14:25:39 -070052 var config *dynamic.Message = val.GetFieldByName("config").(*dynamic.Message)
53 if config != nil {
54 adapter.LogLevel = GetEnumValue(config, "log_level")
55 }
56}