blob: 2cdfe57b41bca71bbbd89d838a4e7137ea4f22a8 [file] [log] [blame]
Himani Chawla40acc122021-05-26 18:52:29 +05301/*
2 * Copyright 2021-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 commands
17
18import (
Himani Chawla553a1392021-06-10 23:39:17 +053019 "fmt"
Akash Soni51b6b7a2024-11-20 11:39:38 +053020 "strings"
21
Himani Chawla40acc122021-05-26 18:52:29 +053022 "github.com/opencord/voltctl/pkg/model"
David K. Bainbridgebd6b2882021-08-26 13:31:02 +000023 "github.com/opencord/voltha-protos/v5/go/extension"
Himani Chawla40acc122021-05-26 18:52:29 +053024)
25
26type tagBuilder struct {
27 firstField bool
28 tag strings.Builder
29}
30
31func NewTagBuilder() tagBuilder {
32 return tagBuilder{
33 firstField: true,
34 tag: strings.Builder{},
35 }
36}
37func (t *tagBuilder) buildOutputString() string {
38 return t.tag.String()
39}
40func (t *tagBuilder) addFieldInFormat(name string) {
41 if !t.firstField {
Himani Chawla553a1392021-06-10 23:39:17 +053042 t.tag.WriteString("\n")
Himani Chawla40acc122021-05-26 18:52:29 +053043 }
44 t.firstField = false
Himani Chawla553a1392021-06-10 23:39:17 +053045 str := fmt.Sprintf("%s: ", name)
46 t.tag.WriteString(str)
Himani Chawla40acc122021-05-26 18:52:29 +053047 t.tag.WriteString("{{.")
48 t.tag.WriteString(name)
49 t.tag.WriteString("}}")
50}
Himani Chawla40acc122021-05-26 18:52:29 +053051
52/*
53 * Construct a template format string based on the fields required by the
54 * results.
55 */
56func buildOnuStatsOutputFormat(counters *extension.GetOnuCountersResponse) (model.OnuStats, string) {
57 onuStats := model.OnuStats{}
58 tagBuilder := NewTagBuilder()
Himani Chawla40acc122021-05-26 18:52:29 +053059 if counters.IsIntfId != nil {
60 intfId := counters.GetIntfId()
61 onuStats.IntfId = &intfId
62 tagBuilder.addFieldInFormat("IntfId")
63 }
64 if counters.IsOnuId != nil {
65 onuId := counters.GetOnuId()
66 onuStats.OnuId = &onuId
67 tagBuilder.addFieldInFormat("OnuId")
68 }
69 if counters.IsPositiveDrift != nil {
70 positiveDrift := counters.GetPositiveDrift()
71 onuStats.PositiveDrift = &positiveDrift
72 tagBuilder.addFieldInFormat("PositiveDrift")
73 }
74 if counters.IsNegativeDrift != nil {
75 negativeDrift := counters.GetNegativeDrift()
76 onuStats.NegativeDrift = &negativeDrift
77 tagBuilder.addFieldInFormat("NegativeDrift")
78 }
79 if counters.IsDelimiterMissDetection != nil {
80 delimiterMissDet := counters.GetDelimiterMissDetection()
81 onuStats.DelimiterMissDetection = &delimiterMissDet
82 tagBuilder.addFieldInFormat("DelimiterMissDetection")
83 }
84 if counters.IsBipErrors != nil {
85 bipErrors := counters.GetBipErrors()
86 onuStats.BipErrors = &bipErrors
87 tagBuilder.addFieldInFormat("BipErrors")
88 }
89 if counters.IsBipUnits != nil {
90 bipUnits := counters.GetBipUnits()
91 onuStats.BipUnits = &bipUnits
92 tagBuilder.addFieldInFormat("BipUnits")
93 }
94 if counters.IsFecCorrectedSymbols != nil {
95 fecCorrectedSymbols := counters.GetFecCorrectedSymbols()
96 onuStats.FecCorrectedSymbols = &fecCorrectedSymbols
97 tagBuilder.addFieldInFormat("FecCorrectedSymbols")
98 }
99 if counters.IsFecCodewordsCorrected != nil {
100 fecCodewordsCorrected := counters.GetFecCodewordsCorrected()
101 onuStats.FecCodewordsCorrected = &fecCodewordsCorrected
102 tagBuilder.addFieldInFormat("FecCodewordsCorrected")
103 }
104 if counters.IsFecCodewordsUncorrectable != nil {
105 fecCodewordsUncorrectable := counters.GetFecCodewordsUncorrectable()
106 onuStats.FecCodewordsUncorrectable = &fecCodewordsUncorrectable
107 tagBuilder.addFieldInFormat("FecCodewordsUncorrectable")
108 }
109 if counters.IsFecCodewords != nil {
110 fecCodewords := counters.GetFecCodewords()
111 onuStats.FecCodewords = &fecCodewords
112 tagBuilder.addFieldInFormat("FecCodewords")
113 }
114 if counters.IsFecCorrectedUnits != nil {
115 fecCorrectedUnits := counters.GetFecCorrectedUnits()
116 onuStats.FecCorrectedUnits = &fecCorrectedUnits
117 tagBuilder.addFieldInFormat("FecCorrectedUnits")
118 }
119 if counters.IsXgemKeyErrors != nil {
120 xgemKeyErrors := counters.GetXgemKeyErrors()
121 onuStats.XgemKeyErrors = &xgemKeyErrors
122 tagBuilder.addFieldInFormat("XgemKeyErrors")
123 }
124 if counters.IsXgemLoss != nil {
125 xgemLoss := counters.GetXgemLoss()
126 onuStats.XgemLoss = &xgemLoss
127 tagBuilder.addFieldInFormat("XgemLoss")
128 }
129 if counters.IsRxPloamsError != nil {
130 rxPloamsError := counters.GetRxPloamsError()
131 onuStats.RxPloamsError = &rxPloamsError
132 tagBuilder.addFieldInFormat("RxPloamsError")
133 }
134 if counters.IsRxPloamsNonIdle != nil {
135 rxPloamsNonIdle := counters.GetRxPloamsNonIdle()
136 onuStats.RxPloamsNonIdle = &rxPloamsNonIdle
137 tagBuilder.addFieldInFormat("RxPloamsNonIdle")
138 }
139 if counters.IsRxOmci != nil {
140 rxOmci := counters.GetRxOmci()
141 onuStats.RxOmci = &rxOmci
142 tagBuilder.addFieldInFormat("RxOmci")
143 }
144 if counters.IsTxOmci != nil {
145 txOmci := counters.GetTxOmci()
146 onuStats.TxOmci = &txOmci
147 tagBuilder.addFieldInFormat("TxOmci")
148 }
149 if counters.IsRxOmciPacketsCrcError != nil {
150 rxOmciPacketsCrcError := counters.GetRxOmciPacketsCrcError()
151 onuStats.RxOmciPacketsCrcError = &rxOmciPacketsCrcError
152 tagBuilder.addFieldInFormat("RxOmciPacketsCrcError")
153 }
154 if counters.IsRxBytes != nil {
155 rxBytes := counters.GetRxBytes()
156 onuStats.RxBytes = &rxBytes
157 tagBuilder.addFieldInFormat("RxBytes")
158 }
159 if counters.IsRxPackets != nil {
160 rxPackets := counters.GetRxPackets()
161 onuStats.RxPackets = &rxPackets
162 tagBuilder.addFieldInFormat("RxPackets")
163 }
164 if counters.IsTxBytes != nil {
165 txBytes := counters.GetTxBytes()
166 onuStats.TxBytes = &txBytes
167 tagBuilder.addFieldInFormat("TxBytes")
168 }
169 if counters.IsTxPackets != nil {
170 txPackets := counters.GetTxPackets()
171 onuStats.TxPackets = &txPackets
172 tagBuilder.addFieldInFormat("TxPackets")
173 }
174 if counters.IsBerReported != nil {
175 berReported := counters.GetBerReported()
176 onuStats.BerReported = &berReported
177 tagBuilder.addFieldInFormat("BerReported")
178 }
179 if counters.IsLcdgErrors != nil {
180 lcdgErrors := counters.GetLcdgErrors()
181 onuStats.LcdgErrors = &lcdgErrors
182 tagBuilder.addFieldInFormat("LcdgErrors")
183 }
184 if counters.IsRdiErrors != nil {
185 rdiErrors := counters.GetRdiErrors()
186 onuStats.RdiErrors = &rdiErrors
187 tagBuilder.addFieldInFormat("RdiErrors")
188 }
189 if counters.IsTimestamp != nil {
190 timestamp := counters.GetTimestamp()
191 onuStats.Timestamp = &timestamp
192 tagBuilder.addFieldInFormat("Timestamp")
193 }
194 return onuStats, tagBuilder.buildOutputString()
195}
Himani Chawla553a1392021-06-10 23:39:17 +0530196
Akash Soni51b6b7a2024-11-20 11:39:38 +0530197func buildOffloadAppStatsOutputFormat(stats *extension.GetOffloadedAppsStatisticsResponse) (map[string]interface{}, string) {
198 formatStr := "table" // Default format
Akash Soni51b6b7a2024-11-20 11:39:38 +0530199 if stats == nil {
200 return map[string]interface{}{
201 "error": "No stats available in response",
202 }, formatStr
203 }
204
205 switch data := stats.GetStats().(type) {
206 case *extension.GetOffloadedAppsStatisticsResponse_Dhcpv4RaStats:
207 return map[string]interface{}{
208 "in_bad_packets_from_client": data.Dhcpv4RaStats.InBadPacketsFromClient,
209 "in_bad_packets_from_server": data.Dhcpv4RaStats.InBadPacketsFromServer,
210 "in_packets_from_client": data.Dhcpv4RaStats.InPacketsFromClient,
211 "in_packets_from_server": data.Dhcpv4RaStats.InPacketsFromServer,
212 "out_packets_to_server": data.Dhcpv4RaStats.OutPacketsToServer,
213 "out_packets_to_client": data.Dhcpv4RaStats.OutPacketsToClient,
214 "option_82_inserted_packets_to_server": data.Dhcpv4RaStats.Option_82InsertedPacketsToServer,
215 "option_82_removed_packets_to_client": data.Dhcpv4RaStats.Option_82RemovedPacketsToClient,
216 "option_82_not_inserted_to_server": data.Dhcpv4RaStats.Option_82NotInsertedToServer,
217 "additional_stats": convertMapStringToInterface(data.Dhcpv4RaStats.AdditionalStats),
balaji.nagarajan1f7c6392026-05-22 16:16:30 +0530218 }, DEFAULT_OFFLOAD_APP_STATS_DHCPv4_FORMAT
Akash Soni51b6b7a2024-11-20 11:39:38 +0530219
220 case *extension.GetOffloadedAppsStatisticsResponse_Dhcpv6RaStats:
221 return map[string]interface{}{
222 "in_bad_packets_from_client": data.Dhcpv6RaStats.InBadPacketsFromClient,
223 "in_bad_packets_from_server": data.Dhcpv6RaStats.InBadPacketsFromServer,
224 "option_17_inserted_packets_to_server": data.Dhcpv6RaStats.Option_17InsertedPacketsToServer,
225 "option_17_removed_packets_to_client": data.Dhcpv6RaStats.Option_17RemovedPacketsToClient,
226 "option_18_inserted_packets_to_server": data.Dhcpv6RaStats.Option_18InsertedPacketsToServer,
227 "option_18_removed_packets_to_client": data.Dhcpv6RaStats.Option_18RemovedPacketsToClient,
balaji.nagarajan1f7c6392026-05-22 16:16:30 +0530228 "option_37_inserted_packets_to_server": data.Dhcpv6RaStats.Option_37InsertedPacketsToServer,
229 "option_37_removed_packets_to_client": data.Dhcpv6RaStats.Option_37RemovedPacketsToClient,
Akash Soni51b6b7a2024-11-20 11:39:38 +0530230 "outgoing_mtu_exceeded_packets_from_client": data.Dhcpv6RaStats.OutgoingMtuExceededPacketsFromClient,
231 "additional_stats": convertMapStringToInterface(data.Dhcpv6RaStats.AdditionalStats),
balaji.nagarajan1f7c6392026-05-22 16:16:30 +0530232 }, DEFAULT_OFFLOAD_APP_STATS_DHCPv6_FORMAT
Akash Soni51b6b7a2024-11-20 11:39:38 +0530233
234 case *extension.GetOffloadedAppsStatisticsResponse_PppoeIaStats:
235 return map[string]interface{}{
236 "in_error_packets_from_client": data.PppoeIaStats.InErrorPacketsFromClient,
237 "in_error_packets_from_server": data.PppoeIaStats.InErrorPacketsFromServer,
238 "in_packets_from_client": data.PppoeIaStats.InPacketsFromClient,
239 "in_packets_from_server": data.PppoeIaStats.InPacketsFromServer,
240 "out_packets_to_server": data.PppoeIaStats.OutPacketsToServer,
241 "out_packets_to_client": data.PppoeIaStats.OutPacketsToClient,
242 "vendor_specific_tag_inserted_packets_to_server": data.PppoeIaStats.VendorSpecificTagInsertedPacketsToServer,
243 "vendor_specific_tag_removed_packets_to_client": data.PppoeIaStats.VendorSpecificTagRemovedPacketsToClient,
244 "outgoing_mtu_exceeded_packets_from_client": data.PppoeIaStats.OutgoingMtuExceededPacketsFromClient,
245 "additional_stats": convertMapStringToInterface(data.PppoeIaStats.AdditionalStats),
balaji.nagarajan1f7c6392026-05-22 16:16:30 +0530246 }, DEFAULT_OFFLOAD_APP_STATS_PPPOE_IA_FORMAT
Akash Soni51b6b7a2024-11-20 11:39:38 +0530247
248 default:
249 return map[string]interface{}{
250 "error": "Unsupported or unknown stats type",
251 }, formatStr
252 }
253}
254
255func convertMapStringToInterface(input map[string]string) map[string]interface{} {
256 result := make(map[string]interface{}, len(input))
257 for key, value := range input {
258 result[key] = value
259 }
260 return result
261}
262
Himani Chawla553a1392021-06-10 23:39:17 +0530263/*
264 * Construct a template format string based on the fields required by the
265 * results.
266 */
267func buildOnuEthernetFrameExtendedPmOutputFormat(counters *extension.GetOmciEthernetFrameExtendedPmResponse) model.OnuEthernetFrameExtendedPm {
268 onuStats := model.OnuEthernetFrameExtendedPm{}
269
Himani Chawlabac0f892021-08-25 17:14:06 +0530270 onuStats.PmFormat = counters.GetOmciEthernetFrameExtendedPmFormat().String()
271
Himani Chawla553a1392021-06-10 23:39:17 +0530272 dropEvents := counters.Upstream.GetDropEvents()
273 onuStats.UDropEvents = &dropEvents
274
275 octets := counters.Upstream.GetOctets()
276 onuStats.UOctets = &octets
277
278 frames := counters.Upstream.GetFrames()
279 onuStats.UFrames = &frames
280
281 broadcastFrames := counters.Upstream.GetBroadcastFrames()
282 onuStats.UBroadcastFrames = &broadcastFrames
283
284 multicastFrames := counters.Upstream.GetMulticastFrames()
285 onuStats.UMulticastFrames = &multicastFrames
286
287 crcErroredFrames := counters.Upstream.GetCrcErroredFrames()
288 onuStats.UCrcErroredFrames = &crcErroredFrames
289
290 undersizeFrames := counters.Upstream.GetUndersizeFrames()
291 onuStats.UUndersizeFrames = &undersizeFrames
292
293 oversizeFrames := counters.Upstream.GetOversizeFrames()
294 onuStats.UOversizeFrames = &oversizeFrames
295
296 frames_64Octets := counters.Upstream.GetFrames_64Octets()
297 onuStats.UFrames_64Octets = &frames_64Octets
298
299 frames_65To_127Octets := counters.Upstream.GetFrames_65To_127Octets()
300 onuStats.UFrames_65To_127Octets = &frames_65To_127Octets
301
302 frames_128To_255Octets := counters.Upstream.GetFrames_128To_255Octets()
303 onuStats.UFrames_128To_255Octets = &frames_128To_255Octets
304
305 frames_256To_511Octets := counters.Upstream.GetFrames_256To_511Octets()
306 onuStats.UFrames_256To_511Octets = &frames_256To_511Octets
307
308 frames_512To_1023Octets := counters.Upstream.GetFrames_512To_1023Octets()
309 onuStats.UFrames_512To_1023Octets = &frames_512To_1023Octets
310
311 frames_1024To_1518Octets := counters.Upstream.GetFrames_1024To_1518Octets()
312 onuStats.UFrames_1024To_1518Octets = &frames_1024To_1518Octets
313
314 dDropEvents := counters.Downstream.GetDropEvents()
315 onuStats.DDropEvents = &dDropEvents
316
317 dOctets := counters.Downstream.GetOctets()
318 onuStats.DOctets = &dOctets
319
320 dFrames := counters.Downstream.GetFrames()
321 onuStats.DFrames = &dFrames
322
323 dBroadcastFrames := counters.Downstream.GetBroadcastFrames()
324 onuStats.DBroadcastFrames = &dBroadcastFrames
325
326 dMulticastFrames := counters.Downstream.GetMulticastFrames()
327 onuStats.DMulticastFrames = &dMulticastFrames
328
329 dCrcErroredFrames := counters.Downstream.GetCrcErroredFrames()
330 onuStats.DCrcErroredFrames = &dCrcErroredFrames
331
332 dUndersizeFrames := counters.Downstream.GetUndersizeFrames()
333 onuStats.DUndersizeFrames = &dUndersizeFrames
334
335 dOversizeFrames := counters.Downstream.GetOversizeFrames()
336 onuStats.DOversizeFrames = &dOversizeFrames
337
338 dFrames_64Octets := counters.Downstream.GetFrames_64Octets()
339 onuStats.DFrames_64Octets = &dFrames_64Octets
340
341 dFrames_65To_127Octets := counters.Downstream.GetFrames_65To_127Octets()
342 onuStats.DFrames_65To_127Octets = &dFrames_65To_127Octets
343
344 dFrames_128To_255Octets := counters.Downstream.GetFrames_128To_255Octets()
345 onuStats.DFrames_128To_255Octets = &dFrames_128To_255Octets
346
347 dFrames_256To_511Octets := counters.Downstream.GetFrames_256To_511Octets()
348 onuStats.DFrames_256To_511Octets = &dFrames_256To_511Octets
349
350 dFrames_512To_1023Octets := counters.Downstream.GetFrames_512To_1023Octets()
351 onuStats.DFrames_512To_1023Octets = &dFrames_512To_1023Octets
352
353 dFrames_1024To_1518Octets := counters.Downstream.GetFrames_1024To_1518Octets()
354 onuStats.DFrames_1024To_1518Octets = &dFrames_1024To_1518Octets
355 return onuStats
356}