mpagenko | c8bba41 | 2021-01-15 15:38:44 +0000 | [diff] [blame] | 1 | /* |
Joey Armstrong | e8c091f | 2023-01-17 16:56:26 -0500 | [diff] [blame] | 2 | * Copyright 2020-2023 Open Networking Foundation (ONF) and the ONF Contributors |
mpagenko | c8bba41 | 2021-01-15 15:38:44 +0000 | [diff] [blame] | 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 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 17 | //Package swupg provides the utilities for onu sw upgrade |
| 18 | package swupg |
mpagenko | c8bba41 | 2021-01-15 15:38:44 +0000 | [diff] [blame] | 19 | |
| 20 | import ( |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 21 | "bufio" |
mpagenko | c8bba41 | 2021-01-15 15:38:44 +0000 | [diff] [blame] | 22 | "context" |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 23 | "errors" |
mpagenko | bf67a09 | 2021-03-17 09:52:28 +0000 | [diff] [blame] | 24 | "fmt" |
mpagenko | 02cf1b2 | 2021-03-12 17:30:30 +0000 | [diff] [blame] | 25 | "io" |
| 26 | "net/http" |
| 27 | "net/url" |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 28 | "os" |
Andrey Pozolotin | 1394a1c | 2021-06-01 00:54:18 +0300 | [diff] [blame] | 29 | "path/filepath" |
mpagenko | c8bba41 | 2021-01-15 15:38:44 +0000 | [diff] [blame] | 30 | "sync" |
mpagenko | 02cf1b2 | 2021-03-12 17:30:30 +0000 | [diff] [blame] | 31 | "time" |
mpagenko | c8bba41 | 2021-01-15 15:38:44 +0000 | [diff] [blame] | 32 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 33 | "github.com/opencord/voltha-protos/v5/go/voltha" |
mpagenko | c8bba41 | 2021-01-15 15:38:44 +0000 | [diff] [blame] | 34 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 35 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
mpagenko | c8bba41 | 2021-01-15 15:38:44 +0000 | [diff] [blame] | 36 | ) |
| 37 | |
| 38 | // ### downloadToAdapter related definitions #### |
| 39 | |
| 40 | //not yet defined to go with sca..., later also some configure options ?? |
| 41 | //const defaultDownloadTimeout = 60 // (?) Seconds |
| 42 | //const localImgPath = "/home/lcui/work/tmp" |
| 43 | |
| 44 | // ### downloadToAdapter - end #### |
| 45 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 46 | //AdapterDownloadManager structure holds information needed for downloading to and storing images within the adapter |
| 47 | type AdapterDownloadManager struct { |
mpagenko | c8bba41 | 2021-01-15 15:38:44 +0000 | [diff] [blame] | 48 | mutexDownloadImageDsc sync.RWMutex |
| 49 | downloadImageDscSlice []*voltha.ImageDownload |
| 50 | } |
| 51 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 52 | //NewAdapterDownloadManager constructor returns a new instance of a AdapterDownloadManager |
mpagenko | c8bba41 | 2021-01-15 15:38:44 +0000 | [diff] [blame] | 53 | //mib_db (as well as not inluded alarm_db not really used in this code? VERIFY!!) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 54 | func NewAdapterDownloadManager(ctx context.Context) *AdapterDownloadManager { |
| 55 | logger.Debug(ctx, "init-AdapterDownloadManager") |
| 56 | var localDnldMgr AdapterDownloadManager |
mpagenko | c8bba41 | 2021-01-15 15:38:44 +0000 | [diff] [blame] | 57 | localDnldMgr.downloadImageDscSlice = make([]*voltha.ImageDownload, 0) |
mpagenko | c8bba41 | 2021-01-15 15:38:44 +0000 | [diff] [blame] | 58 | return &localDnldMgr |
| 59 | } |
| 60 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 61 | //ImageExists returns true if the requested image already exists within the adapter |
| 62 | func (dm *AdapterDownloadManager) ImageExists(ctx context.Context, apImageDsc *voltha.ImageDownload) bool { |
mpagenko | c8bba41 | 2021-01-15 15:38:44 +0000 | [diff] [blame] | 63 | logger.Debugw(ctx, "checking on existence of the image", log.Fields{"image-name": (*apImageDsc).Name}) |
| 64 | dm.mutexDownloadImageDsc.RLock() |
| 65 | defer dm.mutexDownloadImageDsc.RUnlock() |
| 66 | |
| 67 | for _, pDnldImgDsc := range dm.downloadImageDscSlice { |
| 68 | if (*pDnldImgDsc).Name == (*apImageDsc).Name { |
| 69 | //image found (by name) |
| 70 | return true |
| 71 | } |
| 72 | } |
| 73 | //image not found (by name) |
| 74 | return false |
| 75 | } |
| 76 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 77 | //ImageLocallyDownloaded returns true if the requested image already exists within the adapter |
| 78 | func (dm *AdapterDownloadManager) ImageLocallyDownloaded(ctx context.Context, apImageDsc *voltha.ImageDownload) bool { |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 79 | logger.Debugw(ctx, "checking if image is fully downloaded", log.Fields{"image-name": (*apImageDsc).Name}) |
| 80 | dm.mutexDownloadImageDsc.RLock() |
| 81 | defer dm.mutexDownloadImageDsc.RUnlock() |
| 82 | |
| 83 | for _, pDnldImgDsc := range dm.downloadImageDscSlice { |
| 84 | if (*pDnldImgDsc).Name == (*apImageDsc).Name { |
| 85 | //image found (by name) |
| 86 | if (*pDnldImgDsc).DownloadState == voltha.ImageDownload_DOWNLOAD_SUCCEEDED { |
| 87 | logger.Debugw(ctx, "image has been fully downloaded", log.Fields{"image-name": (*apImageDsc).Name}) |
| 88 | return true |
| 89 | } |
| 90 | logger.Debugw(ctx, "image not yet fully downloaded", log.Fields{"image-name": (*apImageDsc).Name}) |
| 91 | return false |
| 92 | } |
| 93 | } |
| 94 | //image not found (by name) |
| 95 | logger.Errorw(ctx, "image does not exist", log.Fields{"image-name": (*apImageDsc).Name}) |
| 96 | return false |
| 97 | } |
| 98 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 99 | //StartDownload returns true if the download of the requested image could be started |
| 100 | func (dm *AdapterDownloadManager) StartDownload(ctx context.Context, apImageDsc *voltha.ImageDownload) error { |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 101 | if apImageDsc.LocalDir != "" { |
mpagenko | 02cf1b2 | 2021-03-12 17:30:30 +0000 | [diff] [blame] | 102 | logger.Infow(ctx, "image download-to-adapter requested", log.Fields{ |
| 103 | "image-path": apImageDsc.LocalDir, "image-name": apImageDsc.Name}) |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 104 | newImageDscPos := len(dm.downloadImageDscSlice) |
| 105 | dm.downloadImageDscSlice = append(dm.downloadImageDscSlice, apImageDsc) |
| 106 | dm.downloadImageDscSlice[newImageDscPos].DownloadState = voltha.ImageDownload_DOWNLOAD_STARTED |
mpagenko | 02cf1b2 | 2021-03-12 17:30:30 +0000 | [diff] [blame] | 107 | //try to download from http |
| 108 | urlName := apImageDsc.Url + "/" + apImageDsc.Name |
mpagenko | bf67a09 | 2021-03-17 09:52:28 +0000 | [diff] [blame] | 109 | err := dm.downloadFile(ctx, urlName, apImageDsc.LocalDir, apImageDsc.Name) |
| 110 | if err != nil { |
| 111 | return (err) |
| 112 | } |
mpagenko | 15ff4a5 | 2021-03-02 10:09:20 +0000 | [diff] [blame] | 113 | //return success to comfort the core processing during integration |
| 114 | return nil |
| 115 | } |
| 116 | // we can use the missing local path temporary also to test some failure behavior (system reation on failure) |
| 117 | // with updated control API's or at some adequate time we could also set some defined fixed localPath internally |
| 118 | logger.Errorw(ctx, "could not start download: no valid local directory to write to", log.Fields{"image-name": (*apImageDsc).Name}) |
| 119 | return errors.New("could not start download: no valid local directory to write to") |
mpagenko | c8bba41 | 2021-01-15 15:38:44 +0000 | [diff] [blame] | 120 | } |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 121 | |
mpagenko | 02cf1b2 | 2021-03-12 17:30:30 +0000 | [diff] [blame] | 122 | //downloadFile downloads the specified file from the given http location |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 123 | func (dm *AdapterDownloadManager) downloadFile(ctx context.Context, aURLName string, aFilePath string, aFileName string) error { |
mpagenko | 02cf1b2 | 2021-03-12 17:30:30 +0000 | [diff] [blame] | 124 | // Get the data |
| 125 | logger.Infow(ctx, "downloading from http", log.Fields{"url": aURLName, "localPath": aFilePath}) |
| 126 | // http command is already part of the aURLName argument |
| 127 | urlBase, err1 := url.Parse(aURLName) |
| 128 | if err1 != nil { |
| 129 | logger.Errorw(ctx, "could not set base url command", log.Fields{"url": aURLName, "error": err1}) |
mpagenko | bf67a09 | 2021-03-17 09:52:28 +0000 | [diff] [blame] | 130 | return fmt.Errorf("could not set base url command: %s, error: %s", aURLName, err1) |
mpagenko | 02cf1b2 | 2021-03-12 17:30:30 +0000 | [diff] [blame] | 131 | } |
| 132 | urlParams := url.Values{} |
| 133 | urlBase.RawQuery = urlParams.Encode() |
mpagenko | 02cf1b2 | 2021-03-12 17:30:30 +0000 | [diff] [blame] | 134 | |
mpagenko | bf67a09 | 2021-03-17 09:52:28 +0000 | [diff] [blame] | 135 | //pre-check on file existence |
| 136 | reqExist, errExist2 := http.NewRequest("HEAD", urlBase.String(), nil) |
| 137 | if errExist2 != nil { |
| 138 | logger.Errorw(ctx, "could not generate http head request", log.Fields{"url": urlBase.String(), "error": errExist2}) |
| 139 | return fmt.Errorf("could not generate http head request: %s, error: %s", aURLName, errExist2) |
| 140 | } |
| 141 | ctxExist, cancelExist := context.WithDeadline(ctx, time.Now().Add(3*time.Second)) //waiting for some fast answer |
| 142 | defer cancelExist() |
| 143 | _ = reqExist.WithContext(ctxExist) |
| 144 | respExist, errExist3 := http.DefaultClient.Do(reqExist) |
Andrey Pozolotin | 1394a1c | 2021-06-01 00:54:18 +0300 | [diff] [blame] | 145 | if errExist3 != nil || (respExist != nil && respExist.StatusCode != http.StatusOK) { |
| 146 | if respExist != nil { |
| 147 | logger.Errorw(ctx, "could not http head from url", log.Fields{"url": urlBase.String(), |
mpagenko | bf67a09 | 2021-03-17 09:52:28 +0000 | [diff] [blame] | 148 | "error": errExist3, "status": respExist.StatusCode}) |
Andrey Pozolotin | 1394a1c | 2021-06-01 00:54:18 +0300 | [diff] [blame] | 149 | //if head is not supported by server we cannot use this test and just try to continue |
| 150 | if respExist.StatusCode != http.StatusMethodNotAllowed { |
| 151 | logger.Errorw(ctx, "http head from url: file does not exist here, aborting", log.Fields{"url": urlBase.String(), |
| 152 | "error": errExist3, "status": respExist.StatusCode}) |
| 153 | return fmt.Errorf("http head from url: file does not exist here, aborting: %s, error: %s, status: %d", |
| 154 | aURLName, errExist2, respExist.StatusCode) |
| 155 | } |
| 156 | } else { |
| 157 | logger.Errorw(ctx, "could not http head from url", log.Fields{"url": urlBase.String(), |
| 158 | "error": errExist3}) |
mpagenko | bf67a09 | 2021-03-17 09:52:28 +0000 | [diff] [blame] | 159 | } |
mpagenko | 02cf1b2 | 2021-03-12 17:30:30 +0000 | [diff] [blame] | 160 | } |
Andrey Pozolotin | 1394a1c | 2021-06-01 00:54:18 +0300 | [diff] [blame] | 161 | |
| 162 | if errExist3 == nil && respExist != nil { |
| 163 | defer func() { |
| 164 | deferredErr := respExist.Body.Close() |
| 165 | if deferredErr != nil { |
| 166 | logger.Errorw(ctx, "error at closing http head response body", log.Fields{"url": urlBase.String(), "error": deferredErr}) |
| 167 | } |
| 168 | }() |
| 169 | } |
mpagenko | 02cf1b2 | 2021-03-12 17:30:30 +0000 | [diff] [blame] | 170 | |
mpagenko | bf67a09 | 2021-03-17 09:52:28 +0000 | [diff] [blame] | 171 | //trying to download - do it in background as it may take some time ... |
Andrey Pozolotin | 1394a1c | 2021-06-01 00:54:18 +0300 | [diff] [blame] | 172 | go dm.requestDownload(ctx, urlBase, aFilePath, aFileName) |
| 173 | return nil |
| 174 | } |
mpagenko | bf67a09 | 2021-03-17 09:52:28 +0000 | [diff] [blame] | 175 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 176 | func (dm *AdapterDownloadManager) requestDownload(ctx context.Context, urlBase *url.URL, aFilePath, aFileName string) { |
Andrey Pozolotin | 1394a1c | 2021-06-01 00:54:18 +0300 | [diff] [blame] | 177 | req, err2 := http.NewRequest("GET", urlBase.String(), nil) |
| 178 | if err2 != nil { |
| 179 | logger.Errorw(ctx, "could not generate http request", log.Fields{"url": urlBase.String(), "error": err2}) |
| 180 | return |
| 181 | } |
| 182 | ctx, cancel := context.WithDeadline(ctx, time.Now().Add(10*time.Second)) //long timeout for remote server and big file |
| 183 | defer cancel() |
| 184 | _ = req.WithContext(ctx) |
| 185 | resp, err3 := http.DefaultClient.Do(req) |
| 186 | if err3 != nil { |
| 187 | logger.Errorw(ctx, "could not http get from url", log.Fields{"url": urlBase.String(), "error": err3}) |
| 188 | return |
| 189 | } |
| 190 | defer func() { |
| 191 | deferredErr := resp.Body.Close() |
| 192 | if deferredErr != nil { |
| 193 | logger.Errorw(ctx, "error at closing http get response body", log.Fields{"url": urlBase.String(), "error": deferredErr}) |
mpagenko | 02cf1b2 | 2021-03-12 17:30:30 +0000 | [diff] [blame] | 194 | } |
| 195 | }() |
Andrey Pozolotin | 1394a1c | 2021-06-01 00:54:18 +0300 | [diff] [blame] | 196 | |
| 197 | if resp.StatusCode != http.StatusOK { |
| 198 | logger.Errorw(ctx, "could not http get from url", log.Fields{"url": urlBase.String(), "status": resp.StatusCode}) |
| 199 | return |
| 200 | } |
| 201 | |
| 202 | // Create the file |
| 203 | aLocalPathName := aFilePath + "/" + aFileName |
| 204 | file, err := os.Create(aLocalPathName) |
| 205 | if err != nil { |
| 206 | logger.Errorw(ctx, "could not create local file", log.Fields{"path_file": aLocalPathName, "error": err}) |
| 207 | return |
| 208 | } |
| 209 | defer func() { |
| 210 | deferredErr := file.Close() |
| 211 | if deferredErr != nil { |
| 212 | logger.Errorw(ctx, "error at closing new file", log.Fields{"path_file": aLocalPathName, "error": deferredErr}) |
| 213 | } |
| 214 | }() |
| 215 | |
| 216 | // Write the body to file |
| 217 | _, err = io.Copy(file, resp.Body) |
| 218 | if err != nil { |
| 219 | logger.Errorw(ctx, "could not copy file content", log.Fields{"url": urlBase.String(), "file": aLocalPathName, "error": err}) |
| 220 | return |
| 221 | } |
| 222 | |
| 223 | fileStats, statsErr := file.Stat() |
| 224 | if statsErr != nil { |
| 225 | logger.Errorw(ctx, "created file can't be accessed", log.Fields{"file": aLocalPathName, "stat-error": statsErr}) |
| 226 | } |
| 227 | if fileStats != nil { |
| 228 | logger.Infow(ctx, "written file size is", log.Fields{"file": aLocalPathName, "length": fileStats.Size()}) |
| 229 | } |
| 230 | |
| 231 | for _, pDnldImgDsc := range dm.downloadImageDscSlice { |
| 232 | if (*pDnldImgDsc).Name == aFileName { |
| 233 | //image found (by name) |
| 234 | (*pDnldImgDsc).DownloadState = voltha.ImageDownload_DOWNLOAD_SUCCEEDED |
| 235 | return //can leave directly |
| 236 | } |
| 237 | } |
mpagenko | 02cf1b2 | 2021-03-12 17:30:30 +0000 | [diff] [blame] | 238 | } |
| 239 | |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 240 | //getImageBufferLen returns the length of the specified file in bytes (file size) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 241 | func (dm *AdapterDownloadManager) getImageBufferLen(ctx context.Context, aFileName string, |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 242 | aLocalPath string) (int64, error) { |
| 243 | //maybe we can also use FileSize from dm.downloadImageDscSlice - future option? |
| 244 | |
Andrey Pozolotin | 1394a1c | 2021-06-01 00:54:18 +0300 | [diff] [blame] | 245 | file, err := os.Open(filepath.Clean(aLocalPath + "/" + aFileName)) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 246 | if err != nil { |
| 247 | return 0, err |
| 248 | } |
Andrey Pozolotin | 1394a1c | 2021-06-01 00:54:18 +0300 | [diff] [blame] | 249 | defer func() { |
| 250 | err := file.Close() |
| 251 | if err != nil { |
| 252 | logger.Errorw(ctx, "failed to close file", log.Fields{"error": err}) |
| 253 | } |
| 254 | }() |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 255 | |
| 256 | stats, statsErr := file.Stat() |
| 257 | if statsErr != nil { |
| 258 | return 0, statsErr |
| 259 | } |
| 260 | |
| 261 | return stats.Size(), nil |
| 262 | } |
| 263 | |
| 264 | //getDownloadImageBuffer returns the content of the requested file as byte slice |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 265 | func (dm *AdapterDownloadManager) getDownloadImageBuffer(ctx context.Context, aFileName string, |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 266 | aLocalPath string) ([]byte, error) { |
Andrey Pozolotin | 1394a1c | 2021-06-01 00:54:18 +0300 | [diff] [blame] | 267 | file, err := os.Open(filepath.Clean(aLocalPath + "/" + aFileName)) |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 268 | if err != nil { |
| 269 | return nil, err |
| 270 | } |
Andrey Pozolotin | 1394a1c | 2021-06-01 00:54:18 +0300 | [diff] [blame] | 271 | defer func() { |
| 272 | err := file.Close() |
| 273 | if err != nil { |
| 274 | logger.Errorw(ctx, "failed to close file", log.Fields{"error": err}) |
| 275 | } |
| 276 | }() |
mpagenko | 80622a5 | 2021-02-09 16:53:23 +0000 | [diff] [blame] | 277 | |
| 278 | stats, statsErr := file.Stat() |
| 279 | if statsErr != nil { |
| 280 | return nil, statsErr |
| 281 | } |
| 282 | |
| 283 | var size int64 = stats.Size() |
| 284 | bytes := make([]byte, size) |
| 285 | |
| 286 | buffer := bufio.NewReader(file) |
| 287 | _, err = buffer.Read(bytes) |
| 288 | |
| 289 | return bytes, err |
| 290 | } |