blob: e1f6bc00c9fc3554c5ae6a15d33383b7531e1d6b [file] [log] [blame]
Himani Chawla3c161c62021-05-13 16:36:51 +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 */
16
17package commands
18
19import (
20 "context"
21 "fmt"
22
23 "github.com/opencord/voltctl/pkg/format"
David K. Bainbridgebd6b2882021-08-26 13:31:02 +000024 "github.com/opencord/voltha-protos/v5/go/openflow_13"
25 "github.com/opencord/voltha-protos/v5/go/voltha"
Himani Chawla3c161c62021-05-13 16:36:51 +053026)
27
28const (
29 DEFAULT_DEVICE_GROUPS_FORMAT = "table{{.GroupId}}\t{{.Type}}"
30 DEFAULT_DEVICE_GROUPS_BUCKET_FORMAT = "table{{.GroupId}}\t{{.Buckets}}\t{{.Type}}"
31)
32
33type GroupList struct {
34 ListOutputOptions
35 GroupListOptions
36 Args struct {
37 Id string `positional-arg-name:"DEVICE_ID" required:"yes"`
38 } `positional-args:"yes"`
39
40 Method string
41}
42
43func (options *GroupList) Execute(args []string) error {
44
45 conn, err := NewConnection()
46 if err != nil {
47 return err
48 }
49 defer conn.Close()
50
51 client := voltha.NewVolthaServiceClient(conn)
52
53 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Current().Grpc.Timeout)
54 defer cancel()
55
56 id := voltha.ID{Id: string(options.Args.Id)}
57
58 var groups *openflow_13.FlowGroups
59 switch options.Method {
60 case "device-groups":
61 groups, err = client.ListDeviceFlowGroups(ctx, &id)
62 case "logical-device-groups":
63 groups, err = client.ListLogicalDeviceFlowGroups(ctx, &id)
64 default:
65 Error.Fatalf("Unknown method name: '%s'", options.Method)
66 }
67
68 var groupList []*openflow_13.OfpGroupDesc
69 if err != nil {
70 return err
71 }
72 if toOutputType(options.OutputAs) == OUTPUT_TABLE && (groups == nil || len(groups.Items) == 0) {
73 fmt.Println("*** NO GROUPS ***")
74 return nil
75 }
76 for _, item := range groups.Items {
77 if item.Desc.Type != openflow_13.OfpGroupType_OFPGT_FF {
78 // Since onos is setting watch port and watch group as max uint32,
79 // for group type other than FF, we need to remove watch port and group for them.
80 for _, bucket := range item.Desc.Buckets {
81 bucket.WatchPort = 0
82 bucket.WatchGroup = 0
83 }
84 }
85 groupList = append(groupList, item.Desc)
86 }
87
88 outputFormat := CharReplacer.Replace(options.Format)
89 if options.Quiet {
90 outputFormat = "{{.GroupId}}"
91 } else if outputFormat == "" {
92 if options.Bucket {
93 outputFormat = GetCommandOptionWithDefault(options.Method, "format", DEFAULT_DEVICE_GROUPS_BUCKET_FORMAT)
94 } else {
95 outputFormat = GetCommandOptionWithDefault(options.Method, "format", DEFAULT_DEVICE_GROUPS_FORMAT)
96 }
97 }
98
99 orderBy := options.OrderBy
100 if orderBy == "" {
101 orderBy = GetCommandOptionWithDefault(options.Method, "order", "")
102 }
103
104 result := CommandResult{
105 Format: format.Format(outputFormat),
106 Filter: options.Filter,
107 OrderBy: orderBy,
108 OutputAs: toOutputType(options.OutputAs),
109 NameLimit: options.NameLimit,
110 Data: groupList,
111 }
112 GenerateOutput(&result)
113
114 return nil
115}