backuprestore tables
[ealt-edge.git] / example-apps / ROBO / backup_BE / src / main / java / org / edgegallery / example_app / util / ShellCommand.java
1 package org.edgegallery.example_app.util;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.LinkedList;
10 import java.util.List;
11 import java.util.StringTokenizer;
12
13 import org.apache.commons.lang.StringUtils;
14 import org.edgegallery.example_app.model.EALTEdgeBackup;
15 import org.edgegallery.example_app.model.EALTEdgeRestore;
16 import org.springframework.stereotype.Service;
17 import com.jcraft.jsch.*;
18
19
20 @Service
21 public class ShellCommand {
22
23          public String executeCommand(String command) {
24
25             StringBuffer output = new StringBuffer();
26
27             Process p;
28             try {
29
30                 p = Runtime.getRuntime().exec(command);
31                 p.waitFor();
32                         BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
33
34                 String line = "";
35                 while ((line = reader.readLine())!= null) {
36                     output.append(line + "\n");
37                 }
38                 }
39                 catch (Exception e) {
40                 e.printStackTrace();
41             }
42             return output.toString();
43
44         }
45
46     public List<EALTEdgeBackup> executeBackupCommand(String command) {
47
48         EALTEdgeBackup backup = new EALTEdgeBackup();
49                 List<EALTEdgeBackup> backupsList = new ArrayList<EALTEdgeBackup>();
50
51                 try {
52                         Process p;
53             p = Runtime.getRuntime().exec(command);
54             p.waitFor();
55             BufferedReader reader =
56                     new BufferedReader(new InputStreamReader(p.getInputStream()));
57
58             String line = "";
59             while ((line = reader.readLine())!= null) {
60                 if(line.startsWith("NAME")) {
61                         continue;
62                 }
63                 else {
64                         backup = parseBackupResult(line);
65                         backupsList.add(backup);
66                 }
67             }
68
69         } catch (Exception e) {
70             e.printStackTrace();
71         }
72         return backupsList;
73     }
74     
75     public static EALTEdgeBackup parseBackupResult(String newstr){
76
77         EALTEdgeBackup backup = new EALTEdgeBackup();
78         List<String> al = new ArrayList<String>();
79                 
80                 StringTokenizer st = new StringTokenizer(newstr, " ");
81                 StringBuffer sb = new StringBuffer();
82                 
83                 while(st.hasMoreElements()) {
84                         sb.append(st.nextElement()).append(" ");
85                 }
86                 
87                 String newstrwithProperSpacing = sb.toString();
88                 String str[] = newstrwithProperSpacing.split(" ");
89                 
90                 str[4] = str[4] + str[5] + str[6] + str[7];
91                 
92                 al = Arrays.asList(str);
93                 
94                 for(int i = 0; i < al.size(); i++) {
95                         if( i == 0 ) {
96                                 backup.setName(al.get(i));
97                         }
98                         if( i == 1) {
99                                 backup.setStatus(al.get(i));
100                         }
101                         if( i == 2) {
102                                 backup.setErrors(al.get(i));
103                         }
104                         if( i == 3){
105                                 backup.setWarnings(al.get(i));
106                         }
107                         if( i == 4) {
108                                 backup.setCreated(al.get(i));
109                         }
110
111                 }
112         return backup;
113     }
114
115     public List<EALTEdgeRestore> executeRestoreCommand(String command) {
116
117         EALTEdgeRestore restore = new EALTEdgeRestore();
118         List<EALTEdgeRestore> restoresList = new ArrayList<EALTEdgeRestore>();
119         
120         try {
121                 Process p;
122             p = Runtime.getRuntime().exec(command);
123             p.waitFor();
124             BufferedReader reader =
125                     new BufferedReader(new InputStreamReader(p.getInputStream()));
126
127             String line = "";
128             while ((line = reader.readLine())!= null) {
129                 if(line.startsWith("NAME")) {
130                         continue;
131                 }
132                 else {
133                         restore = parseRestoreResult(line);
134                         restoresList.add(restore);
135                 }
136             }
137         } catch (Exception e) {
138             e.printStackTrace();
139         }
140         return restoresList;
141     }
142
143     public static EALTEdgeRestore parseRestoreResult(String newstr){
144
145         EALTEdgeRestore restore = new EALTEdgeRestore();
146
147         StringTokenizer st = new StringTokenizer(newstr, " ");
148                 StringBuffer sb = new StringBuffer();
149
150                 while(st.hasMoreElements()) {
151                         sb.append(st.nextElement()).append(" ");
152                 }
153
154                 String newstrwithProperSpacing = sb.toString();
155                 String str[] = newstrwithProperSpacing.split(" ");
156                 
157                 List<String> ll = new LinkedList<String>(Arrays.asList(str));
158                 
159                 for(int i = 0; i < ll.size(); i++) {
160                         if( i == 0 ) {
161                                 restore.setName(ll.get(i));
162                         }
163                         if( i == 1) {
164                                 restore.setBackup(ll.get(i));
165                         }
166                         if( i == 2) {
167                                 restore.setStatus(ll.get(i));
168                         }
169                 }
170
171         return restore;
172     }
173 }