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