Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2022-present Open Networking Foundation |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | package format |
| 17 | |
| 18 | import ( |
| 19 | "encoding/json" |
| 20 | "fmt" |
| 21 | "io" |
| 22 | "log" |
| 23 | |
| 24 | "github.com/guumaster/tablewriter" |
| 25 | app "voltha-go-controller/internal/pkg/application" |
| 26 | "voltha-go-controller/voltha-go-controller/cli/database" |
| 27 | "voltha-go-controller/voltha-go-controller/cli/models" |
| 28 | "voltha-go-controller/voltha-go-controller/nbi" |
| 29 | ) |
| 30 | |
| 31 | type horizontalTable struct { |
| 32 | title models.TableTitle |
| 33 | writer *tablewriter.Table |
| 34 | } |
| 35 | |
| 36 | func newHorizontalTable(title models.TableTitle, outputBuffer io.Writer) Table { |
| 37 | ht := horizontalTable{} |
| 38 | ht.title = title |
| 39 | ht.writer = tablewriter.NewWriter(outputBuffer) |
| 40 | ht.writer.SetAlignment(tablewriter.ALIGN_RIGHT) |
| 41 | return &ht |
| 42 | } |
| 43 | |
| 44 | func (ht *horizontalTable) SingleEntry(value *database.Data) { |
| 45 | values := make(map[string]*database.Data, 1) |
| 46 | values["singleEntry"] = value |
| 47 | ht.MultipleEntries(values) |
| 48 | } |
| 49 | |
| 50 | func (ht *horizontalTable) SinglePortDataEntry(value map[string][]*app.VoltPort) { |
| 51 | ht.MultiplePortDataEntries(value) |
| 52 | } |
| 53 | |
| 54 | func (ht *horizontalTable) SinglePonPortDataEntry(value map[string][]*app.PonPortCfg) { |
| 55 | ht.MultiplePonPortDataEntries(value) |
| 56 | } |
| 57 | |
| 58 | func (ht *horizontalTable) SingleDataEntry(value map[string]map[string]bool) { |
| 59 | ht.MultipleDataEntries(value) |
| 60 | } |
| 61 | |
| 62 | func (ht *horizontalTable) SingleIcmpDataEntry(value map[string]map[string]int) { |
| 63 | ht.MultipleIcmpDataEntries(value) |
| 64 | } |
| 65 | |
| 66 | func (ht *horizontalTable) SingleDeviceTaskList(value map[string]map[int]*app.TaskInfo) { |
| 67 | ht.MultipleDeviceTaskList(value) |
| 68 | } |
| 69 | |
| 70 | func (ht *horizontalTable) SingleDeviceInfo(value map[string]map[string]*nbi.DeviceInfo) { |
| 71 | ht.MultipleDeviceInfo(value) |
| 72 | } |
| 73 | |
| 74 | func (ht *horizontalTable) SingleDhcpSessionInfo(value []*nbi.DhcpSessionInfo) { |
| 75 | ht.MultipleDhcpSessionInfo(value) |
| 76 | } |
| 77 | |
| 78 | func (ht *horizontalTable) MultipleEntries(values map[string]*database.Data) { |
| 79 | for _, value := range values { |
| 80 | var data map[string]interface{} |
| 81 | err := json.Unmarshal(value.Value, &data) |
| 82 | if err != nil { |
| 83 | log.Fatalf("Data saved in database seems to be corrupted: %s", err) |
| 84 | } |
| 85 | sortedData := sortData(data) |
| 86 | var header []string |
| 87 | for i := range sortedData { |
| 88 | header = append(header, sortedData[i].Key) |
| 89 | } |
| 90 | ht.writer.Append(header) |
| 91 | ht.writer.AddSeparator() |
| 92 | break |
| 93 | } |
| 94 | var rows [][]string |
| 95 | for _, value := range values { |
| 96 | var data map[string]interface{} |
| 97 | err := json.Unmarshal(value.Value, &data) |
| 98 | if err != nil { |
| 99 | log.Fatalf("Data saved in database seems to be corrupted: %s", err) |
| 100 | } |
| 101 | sortedData := sortData(data) |
| 102 | var row []string |
| 103 | for i := range sortedData { |
| 104 | switch (sortedData[i].Value).(type) { |
| 105 | case float64: |
| 106 | row = append(row, fmt.Sprintf("%d", int((sortedData[i].Value).(float64)))) |
| 107 | case uint64, uint32: |
| 108 | row = append(row, fmt.Sprintf("%d", sortedData[i].Value)) |
| 109 | default: |
| 110 | row = append(row, fmt.Sprintf("%v", sortedData[i].Value)) |
| 111 | } |
| 112 | } |
| 113 | rows = append(rows, row) |
| 114 | } |
| 115 | ht.writer.AppendBulk(rows) |
| 116 | ht.writer.Render() |
| 117 | } |
| 118 | |
| 119 | func (ht *horizontalTable) MultipleDataEntries(configs map[string]map[string]bool) { |
| 120 | // TO DO |
| 121 | } |
| 122 | |
| 123 | func (ht *horizontalTable) MultipleIcmpDataEntries(configs map[string]map[string]int) { |
| 124 | // TO DO |
| 125 | } |
| 126 | |
| 127 | func (ht *horizontalTable) MultiplePortDataEntries(configs map[string][]*app.VoltPort) { |
| 128 | // TO DO |
| 129 | } |
| 130 | |
| 131 | func (ht *horizontalTable) MultiplePonPortDataEntries(configs map[string][]*app.PonPortCfg) { |
| 132 | // TO DO |
| 133 | } |
| 134 | |
| 135 | func (ht *horizontalTable) MultipleDeviceTaskList(configs map[string]map[int]*app.TaskInfo) { |
| 136 | // TO DO |
| 137 | } |
| 138 | |
| 139 | func (ht *horizontalTable) MultipleDeviceInfo(configs map[string]map[string]*nbi.DeviceInfo) { |
| 140 | // TO DO |
| 141 | } |
| 142 | |
| 143 | func (ht *horizontalTable) MultipleDhcpSessionInfo(configs []*nbi.DhcpSessionInfo) { |
| 144 | // TO DO |
| 145 | } |