Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package model |
| 17 | |
| 18 | import ( |
Kent Hagerman | e00f4f6 | 2020-01-22 13:53:10 -0500 | [diff] [blame] | 19 | "github.com/golang/protobuf/ptypes" |
| 20 | "github.com/golang/protobuf/ptypes/timestamp" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 21 | "github.com/jhump/protoreflect/dynamic" |
Kent Hagerman | e00f4f6 | 2020-01-22 13:53:10 -0500 | [diff] [blame] | 22 | "time" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | type Adapter struct { |
Kent Hagerman | e00f4f6 | 2020-01-22 13:53:10 -0500 | [diff] [blame] | 26 | Id string |
| 27 | Vendor string |
| 28 | Version string |
| 29 | LogLevel string |
| 30 | LastCommunication string |
| 31 | SinceLastCommunication string |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | func (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 Hagerman | e00f4f6 | 2020-01-22 13:53:10 -0500 | [diff] [blame] | 38 | |
| 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) |
David K. Bainbridge | 2b62761 | 2020-02-18 14:50:13 -0800 | [diff] [blame] | 48 | adapter.SinceLastCommunication = time.Since(lastCommunication).Truncate(time.Second).String() |
Kent Hagerman | e00f4f6 | 2020-01-22 13:53:10 -0500 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 52 | var config *dynamic.Message = val.GetFieldByName("config").(*dynamic.Message) |
| 53 | if config != nil { |
| 54 | adapter.LogLevel = GetEnumValue(config, "log_level") |
| 55 | } |
| 56 | } |