blob: aa5d40b3cae9bbc6b452813bc42b7f7e33c442f9 [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.
vinokuma926cb3e2023-03-29 11:41:06 +053014 */
Naveen Sampath04696f72022-06-13 15:19:14 +053015
16package commands
17
18import (
19 "fmt"
20 "log"
21
vinokuma926cb3e2023-03-29 11:41:06 +053022 db "voltha-go-controller/database"
Naveen Sampath04696f72022-06-13 15:19:14 +053023 "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"
vinokuma926cb3e2023-03-29 11:41:06 +053026
27 flags "github.com/jessevdk/go-flags"
Naveen Sampath04696f72022-06-13 15:19:14 +053028)
29
30// RegisterVNETCommands to register vnet command
31func RegisterVNETCommands(parser *flags.Parser) {
Naveen Sampath04696f72022-06-13 15:19:14 +053032 if _, err := parser.AddCommand("vnet", "Lists configured VNET profiles", "Commands to display VNET configuration", &vnetCommand); err != nil {
33 log.Fatalf("Unexpected error while attempting to register vnet commands : %s", err)
34 }
Naveen Sampath04696f72022-06-13 15:19:14 +053035}
36
37// VNETCommand structure
38type VNETCommand struct{}
39
40var vnetCommand VNETCommand
41
42// Execute for execution of vnet command
43func (ic *VNETCommand) Execute(args []string) error {
44 rc, err := database.GetRedisClient()
45 if err != nil {
46 return fmt.Errorf("Failed to make connection to KV Store: %v ", err)
47 }
48
49 switch len(args) {
50 case 0:
51 // Get all VNET profiles
52 profiles, err := rc.GetAll(db.GetKeyPath(db.VnetPath))
53 if err != nil {
54 return fmt.Errorf("Error fetching the VNET profile: %s", err)
55 }
56 if len(profiles) == 0 {
57 return fmt.Errorf("No VNET profiles found")
58 }
59 // call the formating function and display it in a table
60 format.NewTable(models.AllVNET, models.Horizontal).MultipleEntries(profiles)
61 case 1:
62 // VNET ID provided in the command
63 vnetID := args[0]
64 profile, err := rc.Get(db.GetKeyPath(db.VnetPath), vnetID)
65 if err != nil {
66 return fmt.Errorf("Error fetching the VNET details: %s", err)
67 }
68 if profile == nil {
69 return fmt.Errorf("No VNET profile found with ID %s", vnetID)
70 }
71 // call the formating function and display it in a table
72 tableTitle := models.TableTitle(fmt.Sprintf(string(models.SingleVNET), vnetID))
73 format.NewTable(tableTitle, models.Horizontal).SingleEntry(profile)
74 default:
75 return fmt.Errorf("Usage: %s", models.VNETUsage)
76 }
77 return nil
78}