hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | |
| 17 | package commands |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "fmt" |
| 22 | |
| 23 | "github.com/jessevdk/go-flags" |
| 24 | "github.com/opencord/bbsim/api/bbsim" |
| 25 | "github.com/opencord/bbsim/internal/bbsimctl/config" |
| 26 | log "github.com/sirupsen/logrus" |
| 27 | "google.golang.org/grpc" |
| 28 | ) |
| 29 | |
| 30 | type DMIOptions struct { |
| 31 | Events DmiEventOptions `command:"events"` |
| 32 | } |
| 33 | |
| 34 | type DmiEventCreate struct { |
| 35 | Args struct { |
| 36 | Name string |
| 37 | } `positional-args:"yes" required:"yes"` |
| 38 | } |
| 39 | |
| 40 | type DmiEventOptions struct { |
| 41 | Create DmiEventCreate `command:"create"` |
| 42 | } |
| 43 | |
| 44 | func RegisterDMICommands(parser *flags.Parser) { |
| 45 | _, _ = parser.AddCommand("dmi", "DMI Commands", "Commands to create events", &DMIOptions{}) |
| 46 | } |
| 47 | |
| 48 | func dmiEventGrpcClient() (bbsim.BBsimDmiClient, *grpc.ClientConn) { |
| 49 | conn, err := grpc.Dial(config.DmiConfig.Server, grpc.WithInsecure()) |
| 50 | if err != nil { |
| 51 | log.Errorf("BBsimDmiClient connection failed : %v", err) |
| 52 | return nil, conn |
| 53 | } |
| 54 | return bbsim.NewBBsimDmiClient(conn), conn |
| 55 | } |
| 56 | |
| 57 | // Execute create event |
| 58 | func (o *DmiEventCreate) Execute(args []string) error { |
| 59 | client, conn := dmiEventGrpcClient() |
| 60 | defer conn.Close() |
| 61 | |
| 62 | ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout) |
| 63 | defer cancel() |
| 64 | |
| 65 | req := bbsim.DmiEvent{EventName: o.Args.Name} |
| 66 | res, err := client.CreateEvent(ctx, &req) |
| 67 | if err != nil { |
| 68 | log.Errorf("Cannot create DMI event: %v", err) |
| 69 | return err |
| 70 | } |
| 71 | |
| 72 | fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message)) |
| 73 | return nil |
| 74 | } |