blob: 711f8aa682842167763965653117a0554714525e [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
Matteo Scandolo0a413a82020-04-15 13:51:14 -070032 CurrentReplica int32
33 TotalReplicas int32
34 Endpoint string
35 Type string
Zack Williamse940c7a2019-08-21 14:25:39 -070036}
37
38func (adapter *Adapter) PopulateFrom(val *dynamic.Message) {
Matteo Scandolo0a413a82020-04-15 13:51:14 -070039
Zack Williamse940c7a2019-08-21 14:25:39 -070040 adapter.Id = val.GetFieldByName("id").(string)
41 adapter.Vendor = val.GetFieldByName("vendor").(string)
42 adapter.Version = val.GetFieldByName("version").(string)
Kent Hagermane00f4f62020-01-22 13:53:10 -050043
Matteo Scandolo0a413a82020-04-15 13:51:14 -070044 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 Hagermane00f4f62020-01-22 13:53:10 -050065 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. Bainbridge2b627612020-02-18 14:50:13 -080074 adapter.SinceLastCommunication = time.Since(lastCommunication).Truncate(time.Second).String()
Kent Hagermane00f4f62020-01-22 13:53:10 -050075 }
76 }
77
Zack Williamse940c7a2019-08-21 14:25:39 -070078}