|
| 1 | +package system |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/openconfig/gnoi/common" |
| 9 | + "github.com/openconfig/gnoi/system" |
| 10 | + "github.com/sonic-net/sonic-gnmi/gnoi_client/utils" |
| 11 | + "google.golang.org/grpc" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + // Flags for System.SetPackage |
| 16 | + filename = flag.String("package_filename", "", "Destination path and filename of the package") |
| 17 | + version = flag.String("package_version", "", "Version of the package, i.e. vendor internal name") |
| 18 | + url = flag.String("package_url", "", "URL to download the package from") |
| 19 | + activate = flag.Bool("package_activate", true, "Whether to activate the package after setting it") |
| 20 | +) |
| 21 | + |
| 22 | +// newSystemClient is a package-level variable that returns a new system.SystemClient. |
| 23 | +// We define it here so that unit tests can replace it with a mock constructor if needed. |
| 24 | +var newSystemClient = func(conn *grpc.ClientConn) system.SystemClient { |
| 25 | + return system.NewSystemClient(conn) |
| 26 | +} |
| 27 | + |
| 28 | +// SetPackage is the main entry point. It validates flags, creates the SystemClient, |
| 29 | +// and then calls setPackageClient to perform the actual SetPackage gRPC flow. |
| 30 | +func SetPackage(conn *grpc.ClientConn, ctx context.Context) { |
| 31 | + fmt.Println("System SetPackage") |
| 32 | + |
| 33 | + // Attach user credentials if needed. |
| 34 | + ctx = utils.SetUserCreds(ctx) |
| 35 | + |
| 36 | + // Validate the flags before proceeding. |
| 37 | + err := validateFlags() |
| 38 | + if err != nil { |
| 39 | + fmt.Println("Error validating flags:", err) |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + // Create a new gNOI SystemClient using the function defined above. |
| 44 | + sc := newSystemClient(conn) |
| 45 | + |
| 46 | + // Call the helper that implements the SetPackage logic (sending requests, closing, etc.). |
| 47 | + if err := setPackageClient(sc, ctx); err != nil { |
| 48 | + fmt.Println("Error during SetPackage:", err) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// setPackageClient contains the core gRPC calls to send the package request and |
| 53 | +// receive the response. We separate it out for easier testing and mocking. |
| 54 | +func setPackageClient(sc system.SystemClient, ctx context.Context) error { |
| 55 | + // Prepare the remote download info. |
| 56 | + download := &common.RemoteDownload{ |
| 57 | + Path: *url, |
| 58 | + } |
| 59 | + |
| 60 | + // Build the package with flags. |
| 61 | + pkg := &system.Package{ |
| 62 | + Filename: *filename, |
| 63 | + Version: *version, |
| 64 | + Activate: *activate, |
| 65 | + RemoteDownload: download, |
| 66 | + } |
| 67 | + |
| 68 | + // The gNOI SetPackageRequest can contain different request types, but we only |
| 69 | + // use the "Package" request type here. |
| 70 | + req := &system.SetPackageRequest{ |
| 71 | + Request: &system.SetPackageRequest_Package{ |
| 72 | + Package: pkg, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + // Open a streaming RPC. |
| 77 | + stream, err := sc.SetPackage(ctx) |
| 78 | + if err != nil { |
| 79 | + return fmt.Errorf("error creating stream: %v", err) |
| 80 | + } |
| 81 | + |
| 82 | + // Send the package information. |
| 83 | + if err := stream.Send(req); err != nil { |
| 84 | + return fmt.Errorf("error sending package request: %v", err) |
| 85 | + } |
| 86 | + |
| 87 | + // Close the send direction of the stream. The device should download the |
| 88 | + // package itself, so we are not sending direct data or checksums here. |
| 89 | + if err := stream.CloseSend(); err != nil { |
| 90 | + return fmt.Errorf("error closing send direction: %v", err) |
| 91 | + } |
| 92 | + |
| 93 | + // Receive the final response from the device. |
| 94 | + resp, err := stream.CloseAndRecv() |
| 95 | + if err != nil { |
| 96 | + return fmt.Errorf("error receiving response: %v", err) |
| 97 | + } |
| 98 | + |
| 99 | + // For demonstration purposes, we simply print the response. |
| 100 | + fmt.Println(resp) |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +// validateFlags ensures all required flags are set correctly before we proceed. |
| 105 | +func validateFlags() error { |
| 106 | + if *filename == "" { |
| 107 | + return fmt.Errorf("missing -package_filename: Destination path and filename of the package is required for the SetPackage operation") |
| 108 | + } |
| 109 | + if *version == "" { |
| 110 | + return fmt.Errorf("missing -package_version: Version of the package is required for the SetPackage operation") |
| 111 | + } |
| 112 | + if *url == "" { |
| 113 | + return fmt.Errorf("missing -package_url: URL to download the package from is required for the SetPackage operation. Direct transfer is not supported yet") |
| 114 | + } |
| 115 | + if !*activate { |
| 116 | + // TODO: Currently, installing the image will always set it to default. |
| 117 | + return fmt.Errorf("-package_activate=false is not yet supported: The package will always be activated after setting it") |
| 118 | + } |
| 119 | + return nil |
| 120 | +} |
0 commit comments