Modify pom file
[nemo.git] / nemo-tools / eclipse-plugin / nemo-rest / org.opendaylight.nemo.tool.eclipse.plugin.rest / src / org / opendaylight / nemo / tool / eclipse / plugin / rest / actions / NemoParserImpl2.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 NemoParserImpl2 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 targetUrl = "language-style-nemo-request";
21         private String baseUrl;
22         private String TRANSACTION_BEGIN = "begin-transaction";
23         private String TRANSACTION_END = "end-transaction";
24         private String REGISTER_USER = "register-user";
25
26         private User user;
27
28         public NemoParserImpl2() {
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 (int i = 0; i < commands.size(); i++) {
63                         String command = commands.get(i);
64                         NemoType type = getType(command);
65                         if (type.equals(NemoType.ENGINE)) {
66                                 targetIpv4 = NemoParserUtils.getIpv4(command);
67                                 if (targetIpv4 != null) {
68
69                                         baseUrl = "http://" + targetIpv4 + ":8181/restconf/operations/nemo-intent:";
70                                         TRANSACTION_BEGIN = baseUrl + TRANSACTION_BEGIN;
71                                         TRANSACTION_END = baseUrl + TRANSACTION_END;
72                                         REGISTER_USER = baseUrl + REGISTER_USER;
73                                         targetUrl = baseUrl + targetUrl;
74
75                                         commands.remove(i);
76                                         return true;
77                                 }
78                         }
79                 }
80                 return false;
81         }
82
83         @Override
84         public boolean findUser() {
85
86                 for (int i = 0; i < commands.size(); i++) {
87                         String command = commands.get(i);
88                         NemoType type = getType(command);
89                         if (type.equals(NemoType.USER)) {
90                                 user = NemoParserUtils.getUser(command);
91                                 if (user != null) {
92                                         commands.remove(i);
93                                         return true;
94                                 }
95                         }
96                 }
97                 return false;
98         }
99
100         @Override
101         public boolean send() {
102                 String register = NemoClient.send(REGISTER_USER, new NemoInput(user).toJsonFormatString());
103                 // String register2 = NemoClient.send("http://191.4.3.31:10081/hello",
104                 // new NemoInput(user,null).toJsonFormatString());
105
106                 String begin = NemoClient.send(TRANSACTION_BEGIN, new NemoInput(user, null).toJsonFormatString());
107
108                 for (String nemo : commands) {
109                         String result = handleOne(nemo);
110                         if (result.indexOf(INTENT) < 0) {
111                                 errorInfo = result;
112
113                         } else {
114                                 keepResult += ("\r\n" + result);
115                         }
116                 }
117
118                 // TODO:end
119                 String end = NemoClient.send(TRANSACTION_END, new NemoInput(user, null).toJsonFormatString());
120                 keepResult += ("\r\n" + end);
121
122                 return true;
123         }
124
125         private String handleOne(String command) {
126                 return NemoClient.send(targetUrl, new NemoInput2(user, command).toJsonFormatString());
127         }
128
129         private NemoType getType(String nemo) {
130                 if (nemo.startsWith("Engines:"))
131                         return NemoType.ENGINE;
132                 if (nemo.startsWith("IMPORT")) {
133                         return NemoType.CREATE_NODE;
134                 }
135                 if (nemo.startsWith("CREATE")) {
136                         return createType(nemo);
137                 }
138                 if (nemo.startsWith("UPDATE")) {
139                         return updateType(nemo);
140                 }
141                 if (nemo.startsWith("DELETE")) {
142                         return deleteType(nemo);
143                 }
144                 return NemoType.UNKNOWN;
145         }
146
147         private NemoType createType(String nemo) {
148                 nemo = nemo.replace("CREATE", "").trim();
149                 if (nemo.startsWith("Node")) {
150                         return NemoType.CREATE_NODE;
151                 }
152                 if (nemo.startsWith("Connection")) {
153                         return NemoType.CREATE_CONN;
154                 }
155                 if (nemo.startsWith("Operation")) {
156                         return NemoType.CREATE_OPERATION;
157                 }
158                 if (nemo.startsWith("Flow")) {
159                         return NemoType.CREATE_FLOW;
160                 }
161                 return NemoType.USER;
162         }
163
164         private NemoType updateType(String nemo) {
165                 nemo = nemo.replace("UPDATE", "").trim();
166                 if (nemo.startsWith("Node")) {
167                         return NemoType.UPDATE_NODE;
168                 }
169                 if (nemo.startsWith("Connection")) {
170                         return NemoType.UPDATE_CONN;
171                 }
172                 if (nemo.startsWith("Flow")) {
173                         return NemoType.UPDATE_FLOW;
174                 }
175                 return null;
176         }
177
178         private NemoType deleteType(String nemo) {
179                 nemo = nemo.replace("DELETE", "").trim();
180                 if (nemo.startsWith("Node")) {
181                         return NemoType.DELETE_NODE;
182                 }
183                 if (nemo.startsWith("Connection")) {
184                         return NemoType.DELETE_CONN;
185                 }
186                 return null;
187         }
188
189 }