blob: 19d4fd04c5dc084883869531a7d3be8c35bbac24 [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"
Scott Bakera55e6452019-06-25 11:10:30 -070024 corderrors "github.com/opencord/cordctl/error"
Scott Baker2c0ebda2019-05-06 16:55:47 -070025)
26
27const (
28 DEFAULT_SERVICE_FORMAT = "table{{ .Name }}\t{{.Version}}\t{{.State}}"
29)
30
31type ServiceList struct {
Scott Bakera00418a2019-06-03 16:15:28 -070032 ListOutputOptions
Scott Baker2c0ebda2019-05-06 16:55:47 -070033}
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 {
Scott Baker867aa302019-06-19 13:18:45 -070052 conn, descriptor, err := InitClient(INIT_DEFAULT)
Scott Baker2c0ebda2019-05-06 16:55:47 -070053 if err != nil {
54 return err
55 }
56 defer conn.Close()
57
Scott Baker2c0ebda2019-05-06 16:55:47 -070058 ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
59 defer cancel()
60
61 headers := GenerateHeaders()
62
63 h := &RpcEventHandler{}
Scott Bakerf53bf152019-05-29 17:50:37 -070064 err = grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.dynamicload.GetLoadStatus", headers, h, h.GetParams)
Scott Baker2c0ebda2019-05-06 16:55:47 -070065 if err != nil {
Scott Bakera55e6452019-06-25 11:10:30 -070066 return corderrors.RpcErrorToCordError(err)
Scott Baker2c0ebda2019-05-06 16:55:47 -070067 }
68
69 if h.Status != nil && h.Status.Err() != nil {
Scott Bakera55e6452019-06-25 11:10:30 -070070 return corderrors.RpcErrorToCordError(h.Status.Err())
Scott Baker2c0ebda2019-05-06 16:55:47 -070071 }
72
73 d, err := dynamic.AsDynamicMessage(h.Response)
74 if err != nil {
75 return err
76 }
77
78 items, err := d.TryGetFieldByName("services")
79 if err != nil {
80 return err
81 }
82
Scott Baker2c0ebda2019-05-06 16:55:47 -070083 data := make([]ServiceListOutput, len(items.([]interface{})))
84
85 for i, item := range items.([]interface{}) {
86 val := item.(*dynamic.Message)
87 data[i].Name = val.GetFieldByName("name").(string)
88 data[i].Version = val.GetFieldByName("version").(string)
89 data[i].State = val.GetFieldByName("state").(string)
90 }
91
Scott Bakera00418a2019-06-03 16:15:28 -070092 FormatAndGenerateListOutput(&options.ListOutputOptions, DEFAULT_SERVICE_FORMAT, "{{.Name}}", data)
Scott Baker2c0ebda2019-05-06 16:55:47 -070093
Scott Baker2c0ebda2019-05-06 16:55:47 -070094 return nil
95}