1 package org.opendaylight.nemo.tool.eclipse.plugin.rest.actions;
3 import java.util.ArrayList;
6 import org.opendaylight.nemo.tool.eclipse.plugin.rest.actions.model.*;
9 * Created by hj on 10/29/15.
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 = "";
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";
28 public NemoParserImpl() {
30 this.commands = new ArrayList<String>();
33 public String getErrorInfo() {
37 public void setErrorInfo(String errorInfo) {
38 this.errorInfo = errorInfo;
41 public String getKeepResult() {
45 public void setKeepResult(String keepResult) {
46 this.keepResult = keepResult;
50 public void format(String content) {
52 String[] comArray = content.split(";");
53 for (String command : comArray) {
54 String com = command.trim();
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;
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);
96 public boolean send() {
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());
101 String begin = NemoClient.send(TRANSACTION_BEGIN, new NemoInput(user,null).toJsonFormatString());
102 // if(!BEGIN.equals(begin)){
103 // errorInfo = "Begin failed : "+ user.toString();
106 for (String nemo : commands) {
107 String result = handleOne(nemo);
108 if (result.indexOf(INTENT) < 0) {
112 keepResult += ("\r\n" + result);
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();
127 private String handleOne(String command) {
128 String url = STRUCTURE_UPDATE_USERS;
129 NemoType type = getType(command);
134 Node node = NemoParserUtils.getNode(command);
135 if (type.equals(NemoType.DELETE_NODE)) {
136 url = STRUCTURE_DELETE_USERS;
138 return NemoClient.send(url,
139 new NemoInput(user, node).toJsonFormatString());
143 Connection connection = NemoParserUtils.getConnection(command);
144 if (type.equals(NemoType.DELETE_CONN)) {
145 url = STRUCTURE_DELETE_USERS;
147 return NemoClient.send(url,
148 new NemoInput(user, connection).toJsonFormatString());
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());
163 return "Unknown type fail";
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;
172 if (nemo.startsWith("CREATE")) {
173 return createType(nemo);
175 if (nemo.startsWith("UPDATE")) {
176 return updateType(nemo);
178 if (nemo.startsWith("DELETE")) {
179 return deleteType(nemo);
181 return NemoType.UNKNOWN;
184 private NemoType createType(String nemo) {
185 nemo = nemo.replace("CREATE", "").trim();
186 if (nemo.startsWith("Node")) {
187 return NemoType.CREATE_NODE;
189 if (nemo.startsWith("Connection")) {
190 return NemoType.CREATE_CONN;
192 if (nemo.startsWith("Operation")) {
193 return NemoType.CREATE_OPERATION;
195 if (nemo.startsWith("Flow")) {
196 return NemoType.CREATE_FLOW;
198 return NemoType.USER;
201 private NemoType updateType(String nemo) {
202 nemo = nemo.replace("UPDATE", "").trim();
203 if (nemo.startsWith("Node")) {
204 return NemoType.UPDATE_NODE;
206 if (nemo.startsWith("Connection")) {
207 return NemoType.UPDATE_CONN;
209 if (nemo.startsWith("Flow")) {
210 return NemoType.UPDATE_FLOW;
215 private NemoType deleteType(String nemo) {
216 nemo = nemo.replace("DELETE", "").trim();
217 if (nemo.startsWith("Node")) {
218 return NemoType.DELETE_NODE;
220 if (nemo.startsWith("Connection")) {
221 return NemoType.DELETE_CONN;