blob: dfdbc9414d96ebb548c48a4f0811ffce65c5a7ee [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301/*
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
16package commands
17
18import (
19 "fmt"
20 "log"
21
22 flags "github.com/jessevdk/go-flags"
23 "voltha-go-controller/voltha-go-controller/cli/database"
24 "voltha-go-controller/voltha-go-controller/cli/format"
25 "voltha-go-controller/voltha-go-controller/cli/models"
26 db "voltha-go-controller/database"
27)
28
29// RegisterPortCommands to register port command
30func RegisterPortCommands(parser *flags.Parser) {
31
32 if _, err := parser.AddCommand("port", "Lists all the logical ports", "Commands to display ports for vgc", &portCommand); err != nil {
33 log.Fatalf("Unexpected error while attempting to register port commands : %s", err)
34 }
35
36}
37
38// PortCommand structure
39type PortCommand struct{}
40
41var portCommand PortCommand
42
43// Execute for execution of port command
44func (port *PortCommand) Execute(args []string) error {
45 rc, err := database.GetRedisClient()
46 if err != nil {
47 return fmt.Errorf("Failed to make connection to KV Store: %v ", err)
48 }
49
50 switch len(args) {
51 case 0:
52 deviceIDList := DeviceIDForGetAll()
53 if deviceIDList == nil {
54 return fmt.Errorf("No ports found")
55 }
56 for _, deviceID := range deviceIDList {
57 portInfo, err := rc.GetAll(fmt.Sprintf(db.GetKeyPath(db.DevicePortPath), deviceID))
58 if err != nil {
59 return fmt.Errorf("Error fetching the port details: %s", err)
60 }
61 if len(portInfo) == 0 {
62 return fmt.Errorf("No ports found")
63 }
64 // call the formating function and display it in a table
65 format.NewTable(models.AllPorts, models.Horizontal).MultipleEntries(portInfo)
66 }
67 case 1:
68 deviceID := args[0]
69 portInfo, err := rc.GetAll(fmt.Sprintf(db.GetKeyPath(db.DevicePortPath), deviceID))
70 if err != nil {
71 return fmt.Errorf("Error fetching the port details: %s", err)
72 }
73 if len(portInfo) == 0 {
74 return fmt.Errorf("No ports found for Device-ID %s", deviceID)
75 }
76 // call the formating function and display it in a table
77 format.NewTable(models.AllPorts, models.Horizontal).MultipleEntries(portInfo)
78 case 2:
79 deviceID := args[0]
80 portID := args[1]
81 portInfo, err := rc.Get(fmt.Sprintf(db.GetKeyPath(db.DevicePortPath), deviceID), portID)
82 if err != nil {
83 return fmt.Errorf("Error fetching the port details: %s", err)
84 }
85 if portInfo == nil {
86 return fmt.Errorf("No port found with Device-ID %s and Port-ID %s", deviceID, portID)
87 }
88 // call the formating function and display it in a table
89 tableTitle := models.TableTitle(fmt.Sprintf(string(models.SinglePort), deviceID, portID))
90 format.NewTable(tableTitle, models.Horizontal).SingleEntry(portInfo)
91 default:
92 return fmt.Errorf("Usage: %s", models.PortUsage)
93 }
94 return nil
95}