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 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 = "";
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";
28 public NemoParserImpl2() {
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 (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) {
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;
84 public boolean findUser() {
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);
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());
106 String begin = NemoClient.send(TRANSACTION_BEGIN, new NemoInput(user, null).toJsonFormatString());
108 for (String nemo : commands) {
109 String result = handleOne(nemo);
110 if (result.indexOf(INTENT) < 0) {
114 keepResult += ("\r\n" + result);
119 String end = NemoClient.send(TRANSACTION_END, new NemoInput(user, null).toJsonFormatString());
120 keepResult += ("\r\n" + end);
125 private String handleOne(String command) {
126 return NemoClient.send(targetUrl, new NemoInput2(user, command).toJsonFormatString());
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;
135 if (nemo.startsWith("CREATE")) {
136 return createType(nemo);
138 if (nemo.startsWith("UPDATE")) {
139 return updateType(nemo);
141 if (nemo.startsWith("DELETE")) {
142 return deleteType(nemo);
144 return NemoType.UNKNOWN;
147 private NemoType createType(String nemo) {
148 nemo = nemo.replace("CREATE", "").trim();
149 if (nemo.startsWith("Node")) {
150 return NemoType.CREATE_NODE;
152 if (nemo.startsWith("Connection")) {
153 return NemoType.CREATE_CONN;
155 if (nemo.startsWith("Operation")) {
156 return NemoType.CREATE_OPERATION;
158 if (nemo.startsWith("Flow")) {
159 return NemoType.CREATE_FLOW;
161 return NemoType.USER;
164 private NemoType updateType(String nemo) {
165 nemo = nemo.replace("UPDATE", "").trim();
166 if (nemo.startsWith("Node")) {
167 return NemoType.UPDATE_NODE;
169 if (nemo.startsWith("Connection")) {
170 return NemoType.UPDATE_CONN;
172 if (nemo.startsWith("Flow")) {
173 return NemoType.UPDATE_FLOW;
178 private NemoType deleteType(String nemo) {
179 nemo = nemo.replace("DELETE", "").trim();
180 if (nemo.startsWith("Node")) {
181 return NemoType.DELETE_NODE;
183 if (nemo.startsWith("Connection")) {
184 return NemoType.DELETE_CONN;