blob: e3073089a68817e14e5fa768f7d184cf8b6ea2b3 [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 (
19 "github.com/jhump/protoreflect/dynamic"
20)
21
22type PeerPort struct {
23 DeviceId string `json:"deviceid"`
24 PortNo uint32 `json:"portno"`
25}
26
27type ProxyAddress struct {
28 DeviceId string `json:"deviceId"`
29 DeviceType string `json:"devicetype,omitempty"`
30 ChannelId uint32 `json:"channelid"`
31 ChannelGroupId uint32 `json:"channelgroup"`
32 ChannelTermination string `json:"channeltermination,omitempty"`
33 OnuId uint32 `json:"onuid"`
34 OnuSessionId uint32 `json:"onusessionid"`
35}
36
37type Device struct {
38 Id string `json:"id"`
39 Type string `json:"type"`
40 Root bool `json:"root"`
41 ParentId string `json:"parentid"`
42 ParentPortNo uint32 `json:"parentportno"`
43 Vendor string `json:"vendor"`
44 Model string `json:"model"`
45 HardwareVersion string `json:"hardwareversion"`
46 FirmwareVersion string `json:"firmwareversion"`
47 SerialNumber string `json:"serialnumber"`
48 VendorId string `json:"vendorid"`
49 Adapter string `json:"adapter"`
50 Vlan uint32 `json:"vlan"`
51 MacAddress string `json:"macaddress"`
52 Address string `json:"address"`
53 ExtraArgs string `json:"extraargs"`
54 ProxyAddress *ProxyAddress `json:"proxyaddress,omitempty"`
55 AdminState string `json:"adminstate"`
56 OperStatus string `json:"operstatus"`
57 Reason string `json:"reason"`
58 ConnectStatus string `json:"connectstatus"`
59 Ports []DevicePort `json:"ports"`
60 Flows []Flow `json:"flows"`
61}
62
63type DevicePort struct {
64 PortNo uint32 `json:"portno"`
65 Label string `json:"label"`
66 Type string `json:"type"`
67 AdminState string `json:"adminstate"`
68 OperStatus string `json:"operstatus"`
69 DeviceId string `json:"deviceid"`
70 Peers []PeerPort `json:"peers"`
71}
72
73func (d *Device) PopulateFrom(val *dynamic.Message) {
74 d.Id = val.GetFieldByName("id").(string)
75 d.Type = val.GetFieldByName("type").(string)
76 d.Root = val.GetFieldByName("root").(bool)
77 d.ParentId = val.GetFieldByName("parent_id").(string)
78 d.ParentPortNo = val.GetFieldByName("parent_port_no").(uint32)
79 d.Vendor = val.GetFieldByName("vendor").(string)
80 d.Model = val.GetFieldByName("model").(string)
81 d.HardwareVersion = val.GetFieldByName("hardware_version").(string)
82 d.FirmwareVersion = val.GetFieldByName("firmware_version").(string)
83 d.SerialNumber = val.GetFieldByName("serial_number").(string)
84 d.VendorId = val.GetFieldByName("vendor_id").(string)
85 d.Adapter = val.GetFieldByName("adapter").(string)
86 d.MacAddress = val.GetFieldByName("mac_address").(string)
87 d.Vlan = val.GetFieldByName("vlan").(uint32)
88 d.Address = val.GetFieldByName("host_and_port").(string)
89 if len(d.Address) == 0 {
90 d.Address = val.GetFieldByName("ipv4_address").(string)
91 }
92 if len(d.Address) == 0 {
93 d.Address = val.GetFieldByName("ipv6_address").(string)
94 }
95 if len(d.Address) == 0 {
96 d.Address = "unknown"
97 }
98 d.ExtraArgs = val.GetFieldByName("extra_args").(string)
99 proxy := val.GetFieldByName("proxy_address").(*dynamic.Message)
100 d.ProxyAddress = nil
101 if proxy != nil {
102 d.ProxyAddress = &ProxyAddress{
103 DeviceId: proxy.GetFieldByName("device_id").(string),
104 ChannelId: proxy.GetFieldByName("channel_id").(uint32),
105 ChannelGroupId: proxy.GetFieldByName("channel_group_id").(uint32),
106 OnuId: proxy.GetFieldByName("onu_id").(uint32),
107 OnuSessionId: proxy.GetFieldByName("onu_session_id").(uint32),
108 }
109 v, err := proxy.TryGetFieldByName("device_type")
110 if err == nil {
111 d.ProxyAddress.DeviceType = v.(string)
112 }
113 v, err = proxy.TryGetFieldByName("channel_termination")
114 if err == nil {
115 d.ProxyAddress.ChannelTermination = v.(string)
116 }
117 }
Neha Sharma19ca2bf2020-05-11 15:34:17 +0000118
119 var err error
120 d.AdminState, err = GetEnumValue(val, "admin_state")
121 if err != nil {
122 d.AdminState = "UNKNOWN"
123 }
124
125 d.OperStatus, err = GetEnumValue(val, "oper_status")
126 if err != nil {
127 d.OperStatus = "UNKNOWN"
128 }
129
Zack Williamse940c7a2019-08-21 14:25:39 -0700130 d.Reason = val.GetFieldByName("reason").(string)
Neha Sharma19ca2bf2020-05-11 15:34:17 +0000131 d.ConnectStatus, err = GetEnumValue(val, "connect_status")
132 if err != nil {
133 d.ConnectStatus = "UNKNOWN"
134 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700135
136 ports := val.GetFieldByName("ports").([]interface{})
137 d.Ports = make([]DevicePort, len(ports))
138 for i, port := range ports {
139 d.Ports[i].PopulateFrom(port.(*dynamic.Message))
140 }
141 flows := val.GetFieldByName("flows").(*dynamic.Message)
142 if flows == nil {
143 d.Flows = make([]Flow, 0)
144 } else {
145 items := flows.GetFieldByName("items").([]interface{})
146 d.Flows = make([]Flow, len(items))
147 for i, flow := range items {
148 d.Flows[i].PopulateFrom(flow.(*dynamic.Message))
149 }
150 }
151}
152
153func (port *DevicePort) PopulateFrom(val *dynamic.Message) {
Neha Sharma19ca2bf2020-05-11 15:34:17 +0000154 var err error
Zack Williamse940c7a2019-08-21 14:25:39 -0700155 port.PortNo = val.GetFieldByName("port_no").(uint32)
Neha Sharma19ca2bf2020-05-11 15:34:17 +0000156 port.Type, err = GetEnumValue(val, "type")
157 if err != nil {
158 port.Type = "UNKNOWN"
159 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700160 port.Label = val.GetFieldByName("label").(string)
Neha Sharma19ca2bf2020-05-11 15:34:17 +0000161 port.AdminState, err = GetEnumValue(val, "admin_state")
162 if err != nil {
163 port.AdminState = "UNKNOWN"
164 }
165
166 port.OperStatus, err = GetEnumValue(val, "oper_status")
167 if err != nil {
168 port.OperStatus = "UNKNOWN"
169 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700170 port.DeviceId = val.GetFieldByName("device_id").(string)
171 peers := val.GetFieldByName("peers").([]interface{})
172 port.Peers = make([]PeerPort, len(peers))
173 for j, peer := range peers {
174 p := peer.(*dynamic.Message)
175 port.Peers[j].DeviceId = p.GetFieldByName("device_id").(string)
176 port.Peers[j].PortNo = p.GetFieldByName("port_no").(uint32)
177 }
178}