Himani Chawla | 40acc12 | 2021-05-26 18:52:29 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package commands |
| 17 | |
| 18 | import ( |
| 19 | "github.com/opencord/voltctl/pkg/model" |
| 20 | "github.com/opencord/voltha-protos/v4/go/extension" |
| 21 | "strings" |
| 22 | ) |
| 23 | |
| 24 | type tagBuilder struct { |
| 25 | firstField bool |
| 26 | tag strings.Builder |
| 27 | } |
| 28 | |
| 29 | func NewTagBuilder() tagBuilder { |
| 30 | return tagBuilder{ |
| 31 | firstField: true, |
| 32 | tag: strings.Builder{}, |
| 33 | } |
| 34 | } |
| 35 | func (t *tagBuilder) buildOutputString() string { |
| 36 | return t.tag.String() |
| 37 | } |
| 38 | func (t *tagBuilder) addFieldInFormat(name string) { |
| 39 | if !t.firstField { |
| 40 | t.tag.WriteString("\t") |
| 41 | } |
| 42 | t.firstField = false |
| 43 | t.tag.WriteString("{{.") |
| 44 | t.tag.WriteString(name) |
| 45 | t.tag.WriteString("}}") |
| 46 | } |
| 47 | func (t *tagBuilder) addTableInFormat() { |
| 48 | t.tag.WriteString("table") |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * Construct a template format string based on the fields required by the |
| 53 | * results. |
| 54 | */ |
| 55 | func buildOnuStatsOutputFormat(counters *extension.GetOnuCountersResponse) (model.OnuStats, string) { |
| 56 | onuStats := model.OnuStats{} |
| 57 | tagBuilder := NewTagBuilder() |
| 58 | tagBuilder.addTableInFormat() |
| 59 | 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 = ×tamp |
| 192 | tagBuilder.addFieldInFormat("Timestamp") |
| 193 | } |
| 194 | return onuStats, tagBuilder.buildOutputString() |
| 195 | } |