Modidy a package name of nemo
[nemo.git] / nemo-tools / eclipse-plugin / nemo-rest / org.opendaylight.nemo.tool.eclipse.plugin.rest / src / org / opendaylight / nemo / tool / eclipse / plugin / rest / actions / NemoParserImpl.java
1 package org.opendaylight.nemo.tool.eclipse.plugin.rest.actions;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.opendaylight.nemo.tool.eclipse.plugin.rest.actions.model.*;
7
8 /**
9  * Created by hj on 10/29/15.
10  */
11 public class NemoParserImpl implements INemoParser {
12         private static final String BEGIN = "Transaction Begin";
13         private static final String END = "Transaction ends";
14         private static final String INTENT = "successfully";
15         private String keepResult = "";
16         private String errorInfo = "";
17
18         private List<String> commands;
19         private String targetIpv4;
20         private String baseUrl;
21         private String TRANSACTION_BEGIN = "begin-transaction";
22         private String TRANSACTION_END = "end-transaction";
23         private String REGISTER_USER = "register-user";
24         private String STRUCTURE_UPDATE_USERS = "structure-style-nemo-update";
25         private String STRUCTURE_DELETE_USERS = "structure-style-nemo-delete";
26         private User user;
27
28         public NemoParserImpl() {
29
30                 this.commands = new ArrayList<String>();
31         }
32
33         public String getErrorInfo() {
34                 return errorInfo;
35         }
36
37         public void setErrorInfo(String errorInfo) {
38                 this.errorInfo = errorInfo;
39         }
40
41         public String getKeepResult() {
42                 return keepResult;
43         }
44
45         public void setKeepResult(String keepResult) {
46                 this.keepResult = keepResult;
47         }
48
49         @Override
50         public void format(String content) {
51
52                 String[] comArray = content.split(";");
53                 for (String command : comArray) {
54                         String com = command.trim();
55                         if (!com.equals(""))
56                                 commands.add(com);
57                 }
58         }
59
60         @Override
61         public boolean findRest() {
62                 for (String command : commands) {
63                         NemoType type = getType(command);
64                         if (type.equals(NemoType.ENGINE)) {
65                                 targetIpv4 = NemoParserUtils.getIpv4(command);
66                                 if (targetIpv4 != null) {
67                                         baseUrl = "http://" + targetIpv4
68                                                         + ":8181/restconf/operations/nemo-intent:";
69                                         TRANSACTION_BEGIN = baseUrl + TRANSACTION_BEGIN;
70                                         TRANSACTION_END = baseUrl + TRANSACTION_END;
71                                         REGISTER_USER = baseUrl + REGISTER_USER;
72                                         STRUCTURE_UPDATE_USERS = baseUrl + STRUCTURE_UPDATE_USERS;
73                                         STRUCTURE_DELETE_USERS = baseUrl + STRUCTURE_DELETE_USERS;
74                                         return true;
75                                 }
76                         }
77                 }
78                 return false;
79         }
80
81         @Override
82         public boolean findUser() {
83                 for (String command : commands) {
84                         NemoType type = getType(command);
85                         if (type.equals(NemoType.USER)) {
86                                 user = NemoParserUtils.getUser(command);
87                                 if (user != null) {
88                                         return true;
89                                 }
90                         }
91                 }
92                 return false;
93         }
94
95         @Override
96         public boolean send() {
97                 // TODO:begin
98                 String register = NemoClient.send(REGISTER_USER, new NemoInput(user).toJsonFormatString());
99 //              String register2 = NemoClient.send("http://191.4.3.31:10081/hello", new NemoInput(user,null).toJsonFormatString());
100
101                 String begin = NemoClient.send(TRANSACTION_BEGIN, new NemoInput(user,null).toJsonFormatString());
102                 // if(!BEGIN.equals(begin)){
103                 // errorInfo = "Begin failed : "+ user.toString();
104                 // return false;
105                 // }
106                 for (String nemo : commands) {
107                         String result = handleOne(nemo);
108                         if (result.indexOf(INTENT) < 0) {
109                                 errorInfo += result;
110                                 // return false;
111                         } else {
112                                 keepResult += ("\r\n" + result);
113                         }
114                 }
115
116                 // TODO:end
117                  String end = NemoClient.send(TRANSACTION_END,new
118                  NemoInput(user,null).toJsonFormatString());
119                  keepResult+=("\r\n" + end);
120                 // if(!BEGIN.equals(end)){
121                 // errorInfo = "End failed : "+ user.toString();
122                 // return false;
123                 // }
124                 return true;
125         }
126
127         private String handleOne(String command) {
128                 String url = STRUCTURE_UPDATE_USERS;
129                 NemoType type = getType(command);
130                 switch (type) {
131                 case CREATE_NODE:
132                 case DELETE_NODE:
133                 case UPDATE_NODE:
134                         Node node = NemoParserUtils.getNode(command);
135                         if (type.equals(NemoType.DELETE_NODE)) {
136                                 url = STRUCTURE_DELETE_USERS;
137                         }
138                         return NemoClient.send(url,
139                                         new NemoInput(user, node).toJsonFormatString());
140                 case CREATE_CONN:
141                 case UPDATE_CONN:
142                 case DELETE_CONN:
143                         Connection connection = NemoParserUtils.getConnection(command);
144                         if (type.equals(NemoType.DELETE_CONN)) {
145                                 url = STRUCTURE_DELETE_USERS;
146                         }
147                         return NemoClient.send(url,
148                                         new NemoInput(user, connection).toJsonFormatString());
149                 case CREATE_FLOW:
150                 case UPDATE_FLOW:
151                         Flow flow = NemoParserUtils.getFlow(command);
152                         return NemoClient.send(url,
153                                         new NemoInput(user, flow).toJsonFormatString());
154                 case CREATE_OPERATION:
155                         Operation operation = NemoParserUtils.getOperation(command);
156                         return NemoClient.send(url,
157                                         new NemoInput(user, operation).toJsonFormatString());
158                 case ENGINE:
159                 case USER:
160                         return INTENT;
161
162                 }
163                 return "Unknown type fail";
164         }
165
166         private NemoType getType(String nemo) {
167                 if (nemo.startsWith("Engines:"))
168                         return NemoType.ENGINE;
169                 if (nemo.startsWith("IMPORT")) {
170                         return NemoType.CREATE_NODE;
171                 }
172                 if (nemo.startsWith("CREATE")) {
173                         return createType(nemo);
174                 }
175                 if (nemo.startsWith("UPDATE")) {
176                         return updateType(nemo);
177                 }
178                 if (nemo.startsWith("DELETE")) {
179                         return deleteType(nemo);
180                 }
181                 return NemoType.UNKNOWN;
182         }
183
184         private NemoType createType(String nemo) {
185                 nemo = nemo.replace("CREATE", "").trim();
186                 if (nemo.startsWith("Node")) {
187                         return NemoType.CREATE_NODE;
188                 }
189                 if (nemo.startsWith("Connection")) {
190                         return NemoType.CREATE_CONN;
191                 }
192                 if (nemo.startsWith("Operation")) {
193                         return NemoType.CREATE_OPERATION;
194                 }
195                 if (nemo.startsWith("Flow")) {
196                         return NemoType.CREATE_FLOW;
197                 }
198                 return NemoType.USER;
199         }
200
201         private NemoType updateType(String nemo) {
202                 nemo = nemo.replace("UPDATE", "").trim();
203                 if (nemo.startsWith("Node")) {
204                         return NemoType.UPDATE_NODE;
205                 }
206                 if (nemo.startsWith("Connection")) {
207                         return NemoType.UPDATE_CONN;
208                 }
209                 if (nemo.startsWith("Flow")) {
210                         return NemoType.UPDATE_FLOW;
211                 }
212                 return null;
213         }
214
215         private NemoType deleteType(String nemo) {
216                 nemo = nemo.replace("DELETE", "").trim();
217                 if (nemo.startsWith("Node")) {
218                         return NemoType.DELETE_NODE;
219                 }
220                 if (nemo.startsWith("Connection")) {
221                         return NemoType.DELETE_CONN;
222                 }
223                 return null;
224         }
225
226 }