2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
32 app "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
40 func NewUtils() *Utils {
41 b := app.Config.GetString("controls.symptomdata.baseDir")
43 b = "/tmp/symptomdata/"
51 func (u *Utils) FileExists(f string) bool {
53 return err == nil || os.IsExist(err)
56 func (u *Utils) CreateDir(path string) error {
57 if u.FileExists(path) {
60 err := os.MkdirAll(path, os.ModePerm)
64 os.Chmod(path, os.ModePerm)
68 func (u *Utils) DeleteFile(fileName string) {
72 func (u *Utils) AddFileToZip(zipWriter *zip.Writer, filePath string, filename string) error {
73 fileToZip, err := os.Open(filename)
77 defer fileToZip.Close()
79 info, err := fileToZip.Stat()
84 header, err := zip.FileInfoHeader(info)
89 if strings.HasPrefix(filename, filePath) {
90 filename = strings.TrimPrefix(filename, filePath)
92 header.Name = filename
93 header.Method = zip.Deflate
95 writer, err := zipWriter.CreateHeader(header)
100 _, err = io.Copy(writer, fileToZip)
105 func (u *Utils) ZipFiles(newZipFile *os.File, filePath string, files []string) error {
106 defer newZipFile.Close()
107 zipWriter := zip.NewWriter(newZipFile)
108 defer zipWriter.Close()
109 for _, file := range files {
110 if err := u.AddFileToZip(zipWriter, filePath, file); err != nil {
111 app.Logger.Error("AddFileToZip() failed: %+v", err.Error())
119 func (u *Utils) FetchFiles(filePath string, fileList []string) []string {
120 files, err := ioutil.ReadDir(filePath)
122 app.Logger.Error("ioutil.ReadDir failed: %+v", err)
125 for _, file := range files {
127 fileList = append(fileList, filepath.Join(filePath, file.Name()))
129 subPath := filepath.Join(filePath, file.Name())
130 subFiles, _ := ioutil.ReadDir(subPath)
131 for _, subFile := range subFiles {
132 if !subFile.IsDir() {
133 fileList = append(fileList, filepath.Join(subPath, subFile.Name()))
135 fileList = u.FetchFiles(filepath.Join(subPath, subFile.Name()), fileList)
143 func (u *Utils) WriteToFile(fileName string, data string) error {
144 f, err := os.Create(fileName)
148 app.Logger.Error("Unable to create file %s': %+v", fileName, err)
150 _, err := io.WriteString(f, data)
152 app.Logger.Error("Unable to write to file '%s': %+v", fileName, err)
153 u.DeleteFile(fileName)
159 func (u *Utils) SendSymptomDataJson(w http.ResponseWriter, req *http.Request, data interface{}, n string) {
160 w.Header().Set("Content-Type", "application/json")
161 w.Header().Set("Content-Disposition", "attachment; filename="+n)
162 w.WriteHeader(http.StatusOK)
164 response, _ := json.Marshal(data)
169 func (u *Utils) SendSymptomDataFile(w http.ResponseWriter, req *http.Request, baseDir, zipFile string) {
170 // Compress and reply with attachment
171 tmpFile, err := ioutil.TempFile("", "symptom")
173 u.SendSymptomDataError(w, req, "Failed to create a tmp file: "+err.Error())
176 defer os.Remove(tmpFile.Name())
178 var fileList []string
179 fileList = u.FetchFiles(baseDir, fileList)
180 err = u.ZipFiles(tmpFile, baseDir, fileList)
182 u.SendSymptomDataError(w, req, "Failed to zip the files: "+err.Error())
186 w.Header().Set("Content-Disposition", "attachment; filename="+zipFile)
187 http.ServeFile(w, req, tmpFile.Name())
190 func (u *Utils) SendSymptomDataError(w http.ResponseWriter, req *http.Request, message string) {
191 w.Header().Set("Content-Disposition", "attachment; filename=error_status.txt")
192 http.Error(w, message, http.StatusInternalServerError)