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 |
Matteo Scandolo | 0a413a8 | 2020-04-15 13:51:14 -0700 | [diff] [blame] | 32 | CurrentReplica int32 |
| 33 | TotalReplicas int32 |
| 34 | Endpoint string |
| 35 | Type string |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | func (adapter *Adapter) PopulateFrom(val *dynamic.Message) { |
Matteo Scandolo | 0a413a8 | 2020-04-15 13:51:14 -0700 | [diff] [blame] | 39 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 40 | adapter.Id = val.GetFieldByName("id").(string) |
| 41 | adapter.Vendor = val.GetFieldByName("vendor").(string) |
| 42 | adapter.Version = val.GetFieldByName("version").(string) |
Kent Hagerman | e00f4f6 | 2020-01-22 13:53:10 -0500 | [diff] [blame] | 43 | |
Matteo Scandolo | 0a413a8 | 2020-04-15 13:51:14 -0700 | [diff] [blame] | 44 | if value, err := val.TryGetFieldByName("currentReplica"); err != nil { |
| 45 | adapter.CurrentReplica = 0 |
| 46 | } else { |
| 47 | adapter.CurrentReplica = value.(int32) |
| 48 | } |
| 49 | if value, err := val.TryGetFieldByName("totalReplicas"); err != nil { |
| 50 | adapter.TotalReplicas = 0 |
| 51 | } else { |
| 52 | adapter.TotalReplicas = value.(int32) |
| 53 | } |
| 54 | if value, err := val.TryGetFieldByName("endpoint"); err != nil { |
| 55 | adapter.Endpoint = "UNKNOWN" |
| 56 | } else { |
| 57 | adapter.Endpoint = value.(string) |
| 58 | } |
| 59 | if value, err := val.TryGetFieldByName("type"); err != nil { |
| 60 | adapter.Type = "UNKNOWN" |
| 61 | } else { |
| 62 | adapter.Type = value.(string) |
| 63 | } |
| 64 | |
Kent Hagerman | e00f4f6 | 2020-01-22 13:53:10 -0500 | [diff] [blame] | 65 | if lastCommunication, err := val.TryGetFieldByName("last_communication"); err != nil { |
| 66 | adapter.LastCommunication = "UNKNOWN" |
| 67 | adapter.SinceLastCommunication = "UNKNOWN" |
| 68 | } else { |
| 69 | if lastCommunication, err := ptypes.Timestamp(lastCommunication.(*timestamp.Timestamp)); err != nil { |
| 70 | adapter.LastCommunication = "NEVER" |
| 71 | adapter.SinceLastCommunication = "NEVER" |
| 72 | } else { |
| 73 | adapter.LastCommunication = lastCommunication.Truncate(time.Second).Format(time.RFC3339) |
David K. Bainbridge | 2b62761 | 2020-02-18 14:50:13 -0800 | [diff] [blame] | 74 | adapter.SinceLastCommunication = time.Since(lastCommunication).Truncate(time.Second).String() |
Kent Hagerman | e00f4f6 | 2020-01-22 13:53:10 -0500 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 78 | } |