Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 17 | package commands |
| 18 | |
| 19 | import ( |
| 20 | "context" |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 21 | "crypto/sha256" |
| 22 | "fmt" |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 23 | "github.com/fullstorydev/grpcurl" |
| 24 | "github.com/golang/protobuf/proto" |
| 25 | "github.com/jhump/protoreflect/dynamic" |
| 26 | "google.golang.org/grpc" |
| 27 | "hash" |
| 28 | "io" |
| 29 | "os" |
| 30 | ) |
| 31 | |
| 32 | /* Handlers for streaming upload and download */ |
| 33 | |
| 34 | type DownloadHandler struct { |
| 35 | RpcEventHandler |
| 36 | f *os.File |
| 37 | chunks int |
| 38 | bytes int |
| 39 | status string |
| 40 | hash hash.Hash |
| 41 | } |
| 42 | |
| 43 | type UploadHandler struct { |
| 44 | RpcEventHandler |
| 45 | chunksize int |
| 46 | f *os.File |
| 47 | uri string |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 48 | hash hash.Hash |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | func (h *DownloadHandler) OnReceiveResponse(m proto.Message) { |
| 52 | d, err := dynamic.AsDynamicMessage(m) |
| 53 | if err != nil { |
| 54 | h.status = "ERROR" |
| 55 | // TODO(smbaker): How to raise an exception? |
| 56 | return |
| 57 | } |
| 58 | chunk := d.GetFieldByName("chunk").(string) |
| 59 | io.WriteString(h.hash, chunk) |
| 60 | h.f.Write([]byte(chunk)) |
| 61 | h.chunks += 1 |
| 62 | h.bytes += len(chunk) |
| 63 | } |
| 64 | |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 65 | func (h *DownloadHandler) GetChecksum() string { |
| 66 | return fmt.Sprintf("sha256:%x", h.hash.Sum(nil)) |
| 67 | } |
| 68 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 69 | func (h *UploadHandler) GetParams(msg proto.Message) error { |
| 70 | dmsg, err := dynamic.AsDynamicMessage(msg) |
| 71 | if err != nil { |
| 72 | return err |
| 73 | } |
| 74 | |
| 75 | //fmt.Printf("streamer, MessageName: %s\n", dmsg.XXX_MessageName()) |
| 76 | |
| 77 | block := make([]byte, h.chunksize) |
| 78 | bytes_read, err := h.f.Read(block) |
| 79 | |
| 80 | if err == io.EOF { |
| 81 | h.f.Close() |
| 82 | //fmt.Print("EOF\n") |
| 83 | return err |
| 84 | } |
| 85 | |
| 86 | if err != nil { |
| 87 | //fmt.Print("ERROR!\n") |
| 88 | return err |
| 89 | } |
| 90 | |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 91 | chunk := string(block[:bytes_read]) |
| 92 | io.WriteString(h.hash, chunk) |
| 93 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 94 | dmsg.TrySetFieldByName("uri", h.uri) |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 95 | dmsg.TrySetFieldByName("chunk", chunk) |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 96 | |
| 97 | return nil |
| 98 | } |
| 99 | |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 100 | func (h *UploadHandler) GetChecksum() string { |
| 101 | return fmt.Sprintf("sha256:%x", h.hash.Sum(nil)) |
| 102 | } |
| 103 | |
| 104 | func UploadFile(conn *grpc.ClientConn, descriptor grpcurl.DescriptorSource, local_name string, uri string, chunkSize int) (*UploadHandler, *dynamic.Message, error) { |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 105 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 106 | defer cancel() |
| 107 | |
| 108 | headers := GenerateHeaders() |
| 109 | |
| 110 | f, err := os.Open(local_name) |
| 111 | if err != nil { |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 112 | return nil, nil, err |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 115 | h := &UploadHandler{uri: uri, |
| 116 | f: f, |
| 117 | chunksize: chunkSize, |
| 118 | hash: sha256.New()} |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 119 | |
| 120 | err = grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.filetransfer/Upload", headers, h, h.GetParams) |
| 121 | if err != nil { |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 122 | return nil, nil, err |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 123 | } |
Scott Baker | 55694ca | 2019-06-13 14:52:28 -0700 | [diff] [blame] | 124 | if h.Status.Err() != nil { |
| 125 | return nil, nil, h.Status.Err() |
| 126 | } |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 127 | d, err := dynamic.AsDynamicMessage(h.Response) |
| 128 | if err != nil { |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 129 | return nil, nil, err |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 132 | return h, d, err |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | func DownloadFile(conn *grpc.ClientConn, descriptor grpcurl.DescriptorSource, uri string, local_name string) (*DownloadHandler, error) { |
| 136 | ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout) |
| 137 | defer cancel() |
| 138 | |
| 139 | headers := GenerateHeaders() |
| 140 | |
| 141 | f, err := os.Create(local_name) |
| 142 | if err != nil { |
| 143 | return nil, err |
| 144 | } |
| 145 | |
| 146 | dm := make(map[string]interface{}) |
| 147 | dm["uri"] = uri |
| 148 | |
| 149 | h := &DownloadHandler{ |
| 150 | RpcEventHandler: RpcEventHandler{ |
| 151 | Fields: map[string]map[string]interface{}{"xos.FileRequest": dm}, |
| 152 | }, |
| 153 | f: f, |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 154 | hash: sha256.New(), |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 155 | status: "SUCCESS"} |
| 156 | |
| 157 | err = grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.filetransfer/Download", headers, h, h.GetParams) |
| 158 | if err != nil { |
| 159 | return nil, err |
| 160 | } |
| 161 | |
Scott Baker | 55694ca | 2019-06-13 14:52:28 -0700 | [diff] [blame] | 162 | if h.Status.Err() != nil { |
| 163 | return nil, h.Status.Err() |
| 164 | } |
| 165 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 166 | return h, err |
| 167 | } |