blob: 26697acc980f9e422affb91cf6e5ab2fa1943c1e [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
199
200 if stats == nil {
201 return map[string]interface{}{
202 "error": "No stats available in response",
203 }, formatStr
204 }
205
206 switch data := stats.GetStats().(type) {
207 case *extension.GetOffloadedAppsStatisticsResponse_Dhcpv4RaStats:
208 return map[string]interface{}{
209 "in_bad_packets_from_client": data.Dhcpv4RaStats.InBadPacketsFromClient,
210 "in_bad_packets_from_server": data.Dhcpv4RaStats.InBadPacketsFromServer,
211 "in_packets_from_client": data.Dhcpv4RaStats.InPacketsFromClient,
212 "in_packets_from_server": data.Dhcpv4RaStats.InPacketsFromServer,
213 "out_packets_to_server": data.Dhcpv4RaStats.OutPacketsToServer,
214 "out_packets_to_client": data.Dhcpv4RaStats.OutPacketsToClient,
215 "option_82_inserted_packets_to_server": data.Dhcpv4RaStats.Option_82InsertedPacketsToServer,
216 "option_82_removed_packets_to_client": data.Dhcpv4RaStats.Option_82RemovedPacketsToClient,
217 "option_82_not_inserted_to_server": data.Dhcpv4RaStats.Option_82NotInsertedToServer,
218 "additional_stats": convertMapStringToInterface(data.Dhcpv4RaStats.AdditionalStats),
219 }, formatStr
220
221 case *extension.GetOffloadedAppsStatisticsResponse_Dhcpv6RaStats:
222 return map[string]interface{}{
223 "in_bad_packets_from_client": data.Dhcpv6RaStats.InBadPacketsFromClient,
224 "in_bad_packets_from_server": data.Dhcpv6RaStats.InBadPacketsFromServer,
225 "option_17_inserted_packets_to_server": data.Dhcpv6RaStats.Option_17InsertedPacketsToServer,
226 "option_17_removed_packets_to_client": data.Dhcpv6RaStats.Option_17RemovedPacketsToClient,
227 "option_18_inserted_packets_to_server": data.Dhcpv6RaStats.Option_18InsertedPacketsToServer,
228 "option_18_removed_packets_to_client": data.Dhcpv6RaStats.Option_18RemovedPacketsToClient,
229 "option_37_inserted_packets_to_server": data.Dhcpv6RaStats.Option_18InsertedPacketsToServer,
230 "option_37_removed_packets_to_client": data.Dhcpv6RaStats.Option_18RemovedPacketsToClient,
231 "outgoing_mtu_exceeded_packets_from_client": data.Dhcpv6RaStats.OutgoingMtuExceededPacketsFromClient,
232 "additional_stats": convertMapStringToInterface(data.Dhcpv6RaStats.AdditionalStats),
233 }, formatStr
234
235 case *extension.GetOffloadedAppsStatisticsResponse_PppoeIaStats:
236 return map[string]interface{}{
237 "in_error_packets_from_client": data.PppoeIaStats.InErrorPacketsFromClient,
238 "in_error_packets_from_server": data.PppoeIaStats.InErrorPacketsFromServer,
239 "in_packets_from_client": data.PppoeIaStats.InPacketsFromClient,
240 "in_packets_from_server": data.PppoeIaStats.InPacketsFromServer,
241 "out_packets_to_server": data.PppoeIaStats.OutPacketsToServer,
242 "out_packets_to_client": data.PppoeIaStats.OutPacketsToClient,
243 "vendor_specific_tag_inserted_packets_to_server": data.PppoeIaStats.VendorSpecificTagInsertedPacketsToServer,
244 "vendor_specific_tag_removed_packets_to_client": data.PppoeIaStats.VendorSpecificTagRemovedPacketsToClient,
245 "outgoing_mtu_exceeded_packets_from_client": data.PppoeIaStats.OutgoingMtuExceededPacketsFromClient,
246 "additional_stats": convertMapStringToInterface(data.PppoeIaStats.AdditionalStats),
247 }, formatStr
248
249 default:
250 return map[string]interface{}{
251 "error": "Unsupported or unknown stats type",
252 }, formatStr
253 }
254}
255
256func convertMapStringToInterface(input map[string]string) map[string]interface{} {
257 result := make(map[string]interface{}, len(input))
258 for key, value := range input {
259 result[key] = value
260 }
261 return result
262}
263
Himani Chawla553a1392021-06-10 23:39:17 +0530264/*
265 * Construct a template format string based on the fields required by the
266 * results.
267 */
268func buildOnuEthernetFrameExtendedPmOutputFormat(counters *extension.GetOmciEthernetFrameExtendedPmResponse) model.OnuEthernetFrameExtendedPm {
269 onuStats := model.OnuEthernetFrameExtendedPm{}
270
Himani Chawlabac0f892021-08-25 17:14:06 +0530271 onuStats.PmFormat = counters.GetOmciEthernetFrameExtendedPmFormat().String()
272
Himani Chawla553a1392021-06-10 23:39:17 +0530273 dropEvents := counters.Upstream.GetDropEvents()
274 onuStats.UDropEvents = &dropEvents
275
276 octets := counters.Upstream.GetOctets()
277 onuStats.UOctets = &octets
278
279 frames := counters.Upstream.GetFrames()
280 onuStats.UFrames = &frames
281
282 broadcastFrames := counters.Upstream.GetBroadcastFrames()
283 onuStats.UBroadcastFrames = &broadcastFrames
284
285 multicastFrames := counters.Upstream.GetMulticastFrames()
286 onuStats.UMulticastFrames = &multicastFrames
287
288 crcErroredFrames := counters.Upstream.GetCrcErroredFrames()
289 onuStats.UCrcErroredFrames = &crcErroredFrames
290
291 undersizeFrames := counters.Upstream.GetUndersizeFrames()
292 onuStats.UUndersizeFrames = &undersizeFrames
293
294 oversizeFrames := counters.Upstream.GetOversizeFrames()
295 onuStats.UOversizeFrames = &oversizeFrames
296
297 frames_64Octets := counters.Upstream.GetFrames_64Octets()
298 onuStats.UFrames_64Octets = &frames_64Octets
299
300 frames_65To_127Octets := counters.Upstream.GetFrames_65To_127Octets()
301 onuStats.UFrames_65To_127Octets = &frames_65To_127Octets
302
303 frames_128To_255Octets := counters.Upstream.GetFrames_128To_255Octets()
304 onuStats.UFrames_128To_255Octets = &frames_128To_255Octets
305
306 frames_256To_511Octets := counters.Upstream.GetFrames_256To_511Octets()
307 onuStats.UFrames_256To_511Octets = &frames_256To_511Octets
308
309 frames_512To_1023Octets := counters.Upstream.GetFrames_512To_1023Octets()
310 onuStats.UFrames_512To_1023Octets = &frames_512To_1023Octets
311
312 frames_1024To_1518Octets := counters.Upstream.GetFrames_1024To_1518Octets()
313 onuStats.UFrames_1024To_1518Octets = &frames_1024To_1518Octets
314
315 dDropEvents := counters.Downstream.GetDropEvents()
316 onuStats.DDropEvents = &dDropEvents
317
318 dOctets := counters.Downstream.GetOctets()
319 onuStats.DOctets = &dOctets
320
321 dFrames := counters.Downstream.GetFrames()
322 onuStats.DFrames = &dFrames
323
324 dBroadcastFrames := counters.Downstream.GetBroadcastFrames()
325 onuStats.DBroadcastFrames = &dBroadcastFrames
326
327 dMulticastFrames := counters.Downstream.GetMulticastFrames()
328 onuStats.DMulticastFrames = &dMulticastFrames
329
330 dCrcErroredFrames := counters.Downstream.GetCrcErroredFrames()
331 onuStats.DCrcErroredFrames = &dCrcErroredFrames
332
333 dUndersizeFrames := counters.Downstream.GetUndersizeFrames()
334 onuStats.DUndersizeFrames = &dUndersizeFrames
335
336 dOversizeFrames := counters.Downstream.GetOversizeFrames()
337 onuStats.DOversizeFrames = &dOversizeFrames
338
339 dFrames_64Octets := counters.Downstream.GetFrames_64Octets()
340 onuStats.DFrames_64Octets = &dFrames_64Octets
341
342 dFrames_65To_127Octets := counters.Downstream.GetFrames_65To_127Octets()
343 onuStats.DFrames_65To_127Octets = &dFrames_65To_127Octets
344
345 dFrames_128To_255Octets := counters.Downstream.GetFrames_128To_255Octets()
346 onuStats.DFrames_128To_255Octets = &dFrames_128To_255Octets
347
348 dFrames_256To_511Octets := counters.Downstream.GetFrames_256To_511Octets()
349 onuStats.DFrames_256To_511Octets = &dFrames_256To_511Octets
350
351 dFrames_512To_1023Octets := counters.Downstream.GetFrames_512To_1023Octets()
352 onuStats.DFrames_512To_1023Octets = &dFrames_512To_1023Octets
353
354 dFrames_1024To_1518Octets := counters.Downstream.GetFrames_1024To_1518Octets()
355 onuStats.DFrames_1024To_1518Octets = &dFrames_1024To_1518Octets
356 return onuStats
357}