blob: a38c46994bb6e23d74d0d5e12c7461fe74bd2b87 [file] [log] [blame]
Scott Baker2c0ebda2019-05-06 16:55:47 -07001/*
2 * Portions copyright 2019-present Open Networking Foundation
3 * Original copyright 2019-present Ciena Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package commands
18
19import (
20 "context"
21 "github.com/fullstorydev/grpcurl"
22 flags "github.com/jessevdk/go-flags"
23 "github.com/jhump/protoreflect/dynamic"
24 "github.com/opencord/cordctl/format"
25)
26
27const (
28 DEFAULT_SERVICE_FORMAT = "table{{ .Name }}\t{{.Version}}\t{{.State}}"
29)
30
31type ServiceList struct {
32 OutputOptions
33}
34
35type ServiceListOutput struct {
36 Name string `json:"name"`
37 Version string `json:"version"`
38 State string `json:"state"`
39}
40
41type ServiceOpts struct {
42 List ServiceList `command:"list"`
43}
44
45var serviceOpts = ServiceOpts{}
46
47func RegisterServiceCommands(parser *flags.Parser) {
48 parser.AddCommand("service", "service commands", "Commands to query and manipulate dynamically loaded XOS Services", &serviceOpts)
49}
50
51func (options *ServiceList) Execute(args []string) error {
52
53 conn, err := NewConnection()
54 if err != nil {
55 return err
56 }
57 defer conn.Close()
58
59 descriptor, method, err := GetReflectionMethod(conn, "xos.dynamicload.GetLoadStatus")
60 if err != nil {
61 return err
62 }
63
64 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
65 defer cancel()
66
67 headers := GenerateHeaders()
68
69 h := &RpcEventHandler{}
70 err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, headers, h, h.GetParams)
71 if err != nil {
72 return err
73 }
74
75 if h.Status != nil && h.Status.Err() != nil {
76 return h.Status.Err()
77 }
78
79 d, err := dynamic.AsDynamicMessage(h.Response)
80 if err != nil {
81 return err
82 }
83
84 items, err := d.TryGetFieldByName("services")
85 if err != nil {
86 return err
87 }
88
89 outputFormat := CharReplacer.Replace(options.Format)
90 if outputFormat == "" {
91 outputFormat = DEFAULT_SERVICE_FORMAT
92 }
93 if options.Quiet {
94 outputFormat = "{{.Id}}"
95 }
96
97 data := make([]ServiceListOutput, len(items.([]interface{})))
98
99 for i, item := range items.([]interface{}) {
100 val := item.(*dynamic.Message)
101 data[i].Name = val.GetFieldByName("name").(string)
102 data[i].Version = val.GetFieldByName("version").(string)
103 data[i].State = val.GetFieldByName("state").(string)
104 }
105
106 result := CommandResult{
107 Format: format.Format(outputFormat),
108 OutputAs: toOutputType(options.OutputAs),
109 Data: data,
110 }
111
112 GenerateOutput(&result)
113 return nil
114}